-/******************************************************************
+/*****************************************************************j
*
* Copyright 2014 Samsung Electronics All Rights Reserved.
*
#include <sys/types.h>
#include <sys/socket.h>
+#include <stdio.h>
#include <unistd.h>
+#include <sys/types.h>
#include <fcntl.h>
#include <sys/select.h>
#include <arpa/inet.h>
#include <netinet/in.h>
+#include <net/if.h>
#include <errno.h>
+#ifdef __linux__
+#include <linux/netlink.h>
+#include <linux/rtnetlink.h>
+#endif
#include "pdu.h"
#include "caadapterutils.h"
#include "oic_string.h"
/**
- * @def IP_SERVER_TAG
+ * @def TAG
* @brief Logging tag for module name
*/
-#define IP_SERVER_TAG "IP_SERVER"
-
-/**
- * @def CA_UDP_BIND_RETRY_COUNT
- * @brief Retry count in case of socket bind failure.
- */
-#define CA_UDP_BIND_RETRY_COUNT 10
-
-/**
- * @def IPNAMESIZE
- * @brief Max length for ip address.
- */
-#define IPNAMESIZE 16
-
-/**
- * @def SOCKETOPTION
- * @brief Socket option.
- */
-#define SOCKETOPTION 1
-
-/**
- * @var g_packetHandlerStopFlag
- * @brief Flag for stopping packet handler thread.
- */
-static bool g_packetHandlerStopFlag = false;
+#define TAG "IP_SERVER"
+
+#define SELECT_TIMEOUT 1 // select() seconds (and termination latency)
+
+#define IPv4_MULTICAST "224.0.1.187"
+static struct in_addr IPv4MulticastAddress = { 0 };
+
+#define IPv6_DOMAINS 16
+#define IPv6_MULTICAST_INT "ff01::fd"
+static struct in6_addr IPv6MulticastAddressInt;
+#define IPv6_MULTICAST_LNK "ff02::fd"
+static struct in6_addr IPv6MulticastAddressLnk;
+#define IPv6_MULTICAST_RLM "ff03::fd"
+static struct in6_addr IPv6MulticastAddressRlm;
+#define IPv6_MULTICAST_ADM "ff04::fd"
+static struct in6_addr IPv6MulticastAddressAdm;
+#define IPv6_MULTICAST_SIT "ff05::fd"
+static struct in6_addr IPv6MulticastAddressSit;
+#define IPv6_MULTICAST_ORG "ff08::fd"
+static struct in6_addr IPv6MulticastAddressOrg;
+#define IPv6_MULTICAST_GLB "ff0e::fd"
+static struct in6_addr IPv6MulticastAddressGlb;
+
+static char *ipv6mcnames[IPv6_DOMAINS] = {
+ NULL,
+ IPv6_MULTICAST_INT,
+ IPv6_MULTICAST_LNK,
+ IPv6_MULTICAST_RLM,
+ IPv6_MULTICAST_ADM,
+ IPv6_MULTICAST_SIT,
+ NULL,
+ NULL,
+ IPv6_MULTICAST_ORG,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ IPv6_MULTICAST_GLB,
+ NULL
+};
+
+static CAIPExceptionCallback g_exceptionCallback;
+
+static CAIPPacketReceivedCallback g_packetReceivedCallback;
+
+static void CAHandleNetlink();
+static void CAApplyInterfaces();
+static void CAFindReadyMessage();
+static void CASelectReturned(fd_set *readFds, int ret);
+static void CAProcessNewInterface(CAInterface_t *ifchanged);
+static CAResult_t CAReceiveMessage(int fd, CATransportFlags_t flags);
+
+#define SET(TYPE, FDS) \
+ if (caglobals.ip.TYPE.fd != -1) \
+ { \
+ FD_SET(caglobals.ip.TYPE.fd, FDS); \
+ }
+
+#define ISSET(TYPE, FDS, FLAGS) \
+ if (caglobals.ip.TYPE.fd != -1 && FD_ISSET(caglobals.ip.TYPE.fd, FDS)) \
+ { \
+ fd = caglobals.ip.TYPE.fd; \
+ flags = FLAGS; \
+ }
-/**
- * @var CAAdapterIPServerContext_t
- * @brief Thread context information for callbacks and threadpool.
- */
-typedef struct
+static void CAReceiveHandler(void *data)
{
- ca_thread_pool_t threadPool;
- CAIPPacketReceivedCallback packetReceivedCallback;
- CAIPExceptionCallback exceptionCallback;
- CAIPErrorHandleCallback IPErrorCallback;
-} CAAdapterIPServerContext_t;
-
-/**
- * @var g_serverInfoList
- * @brief Mutex to synchronize ethenet adapter context.
- */
-static u_arraylist_t *g_serverInfoList = NULL;
+ (void)data;
+ OIC_LOG(DEBUG, TAG, "IN");
-/**
- * @var g_mutexServerInfoList
- * @brief Mutex to synchronize Server Information.
- */
-static ca_mutex g_mutexServerInfoList = NULL;
-
-/**
- * @var g_adapterIPServerContext
- * @brief Mutex to synchronize ethenet adapter context.
- */
-static CAAdapterIPServerContext_t *g_adapterIPServerContext = NULL;
+ while (!caglobals.ip.terminate)
+ {
+ CAFindReadyMessage();
+ }
-/**
- * @var g_mutexAdapterServerContext
- * @brief Mutex to synchronize unicast server
- */
-static ca_mutex g_mutexAdapterServerContext = NULL;
+ OIC_LOG(DEBUG, TAG, "OUT");
+}
-static void CAReceiveHandler(void *data)
+static void CAFindReadyMessage()
{
- OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
-
fd_set readFds;
- int maxSd = 0;
struct timeval timeout;
- char recvBuffer[COAP_MAX_PDU_SIZE] = { 0 };
- while (true != g_packetHandlerStopFlag)
+ timeout.tv_sec = caglobals.ip.selectTimeout;
+ timeout.tv_usec = 0;
+ struct timeval *tv = caglobals.ip.selectTimeout == -1 ? NULL : &timeout;
+
+ FD_ZERO(&readFds);
+ SET(u6, &readFds)
+ SET(u6s, &readFds)
+ SET(u4, &readFds)
+ SET(u4s, &readFds)
+ SET(m6, &readFds)
+ SET(m6s, &readFds)
+ SET(m4, &readFds)
+ SET(m4s, &readFds)
+ if (caglobals.ip.shutdownFds[0] != -1)
{
- timeout.tv_sec = 1;
- timeout.tv_usec = 0;
- FD_ZERO(&readFds);
+ FD_SET(caglobals.ip.shutdownFds[0], &readFds);
+ }
+ if (caglobals.ip.netlinkFd != -1)
+ {
+ FD_SET(caglobals.ip.netlinkFd, &readFds);
+ }
- ca_mutex_lock(g_mutexServerInfoList);
- uint32_t listIndex = 0;
- uint32_t listLength = u_arraylist_length(g_serverInfoList);
+ int ret = select(caglobals.ip.maxfd + 1, &readFds, NULL, NULL, tv);
- u_arraylist_t *tempServerInfoList = u_arraylist_create();
- if (!tempServerInfoList)
+ if (caglobals.ip.terminate)
+ {
+ OIC_LOG_V(DEBUG, TAG, "Packet receiver Stop request received.");
+ return;
+ }
+ if (ret <= 0)
+ {
+ if (ret < 0)
{
- OIC_LOG(ERROR, IP_SERVER_TAG, "u_arraylist_create failed");
- ca_mutex_unlock(g_mutexServerInfoList);
- return;
+ OIC_LOG_V(FATAL, TAG, "select error %s", strerror(errno));
}
+ return;
+ }
- for (listIndex = 0; listIndex < listLength; listIndex++)
- {
- CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(g_serverInfoList, listIndex);
- if (!info)
- {
- listIndex++;
- continue;
- }
-
- int sd = info->socketFd;
- //if valid socket descriptor then add to read list
- if (sd > 0)
- {
- FD_SET(sd, &readFds);
- }
+ CASelectReturned(&readFds, ret);
+}
- //highest file descriptor number, need it for the select function
- if (sd > maxSd)
+static void CASelectReturned(fd_set *readFds, int ret)
+{
+ (void)ret;
+ int fd = -1;
+ CATransportFlags_t flags = CA_DEFAULT_FLAGS;
+
+ while (!caglobals.ip.terminate)
+ {
+ ISSET(u6, readFds, CA_IPV6)
+ else ISSET(u6s, readFds, CA_IPV6 | CA_SECURE)
+ else ISSET(u4, readFds, CA_IPV4)
+ else ISSET(u4s, readFds, CA_IPV4 | CA_SECURE)
+ else ISSET(m6, readFds, CA_MULTICAST | CA_IPV6)
+ else ISSET(m6s, readFds, CA_MULTICAST | CA_IPV6 | CA_SECURE)
+ else ISSET(m4, readFds, CA_MULTICAST | CA_IPV4)
+ else ISSET(m4s, readFds, CA_MULTICAST | CA_IPV4 | CA_SECURE)
+ else if (FD_ISSET(caglobals.ip.netlinkFd, readFds))
+ {
+ CAHandleNetlink();
+ break;
+ }
+ else
+ {
+ CAInterface_t *ifchanged = CAFindInterfaceChange();
+ if (ifchanged)
{
- maxSd = sd;
+ CAProcessNewInterface(ifchanged);
}
+ break;
+ }
- CAServerInfo_t *newInfo = (CAServerInfo_t *) OICMalloc(sizeof(CAServerInfo_t));
- if (!newInfo)
- {
- OIC_LOG(ERROR, IP_SERVER_TAG, "Malloc failed");
- CAClearServerInfoList(tempServerInfoList);
- ca_mutex_unlock(g_mutexServerInfoList);
- return;
- }
+ (void)CAReceiveMessage(fd, flags);
+ FD_CLR(fd, readFds);
+ }
+}
- *newInfo = *info;
+static CAResult_t CAReceiveMessage(int fd, CATransportFlags_t flags)
+{
+ char recvBuffer[COAP_MAX_PDU_SIZE];
- CAResult_t result = u_arraylist_add(tempServerInfoList, (void *) newInfo);
- if (CA_STATUS_OK != result)
- {
- OIC_LOG(ERROR, IP_SERVER_TAG, "u_arraylist_add failed!Thread exit");
- CAClearServerInfoList(tempServerInfoList);
- ca_mutex_unlock(g_mutexServerInfoList);
- return;
- }
- }
+ struct sockaddr_storage srcAddr;
+ socklen_t srcAddrLen = sizeof (srcAddr);
- ca_mutex_unlock(g_mutexServerInfoList);
+ ssize_t recvLen = recvfrom(fd,
+ recvBuffer,
+ sizeof (recvBuffer),
+ 0,
+ (struct sockaddr *)&srcAddr,
+ &srcAddrLen);
+ if (-1 == recvLen)
+ {
+ OIC_LOG_V(ERROR, TAG, "Recvfrom failed %s", strerror(errno));
+ return CA_STATUS_FAILED;
+ }
- int ret = select(maxSd + 1, &readFds, NULL, NULL, &timeout);
- if (g_packetHandlerStopFlag)
- {
- OIC_LOG_V(DEBUG, IP_SERVER_TAG,
- "Packet receiver handler Stop request received. Thread exited");
- CAClearServerInfoList(tempServerInfoList);
- break;
- }
- if (ret < 0)
- {
- OIC_LOG_V(FATAL, IP_SERVER_TAG, "select returned error %s", strerror(errno));
- CAClearServerInfoList(tempServerInfoList);
- continue;
- }
+ CASecureEndpoint_t sep =
+ {.endpoint = {.adapter = CA_ADAPTER_IP, .flags = flags}};
- listLength = u_arraylist_length(tempServerInfoList);
- for (listIndex = 0; listIndex < listLength; listIndex++)
- {
- CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(tempServerInfoList,
- listIndex);
- if (!info)
- {
- continue;
- }
+ if (flags & CA_IPV6)
+ {
+ sep.endpoint.interface = ((struct sockaddr_in6 *)&srcAddr)->sin6_scope_id;
+ ((struct sockaddr_in6 *)&srcAddr)->sin6_scope_id = 0;
+ }
+ CAConvertAddrToName(&srcAddr, sep.endpoint.addr, &sep.endpoint.port);
- int sd = info->socketFd;
- if (FD_ISSET(sd , &readFds))
- {
- OIC_LOG_V(ERROR, IP_SERVER_TAG,
- "data Received server information ip %s, port %d socket %d",
- info->endpoint.addr, info->endpoint.port, sd);
- memset(recvBuffer, 0, sizeof(recvBuffer));
-
- struct sockaddr_in srcSockAddress = { 0 };
- socklen_t srcAddressLen = sizeof(srcSockAddress);
-
- //Reading from socket
- ssize_t recvLen = recvfrom(sd, recvBuffer, sizeof(recvBuffer), 0,
- (struct sockaddr *) &srcSockAddress, &srcAddressLen);
- if (-1 == recvLen)
- {
- OIC_LOG_V(ERROR, IP_SERVER_TAG, "Recvfrom failed %s", strerror(errno));
- continue;
- }
- else if (0 == recvLen)
- {
- OIC_LOG_V(ERROR, IP_SERVER_TAG, "Server socket shutdown sock fd[%d]", sd);
- ca_mutex_lock(g_mutexAdapterServerContext);
- // Notify upper layer this exception
- if (g_adapterIPServerContext->exceptionCallback)
- {
- // need to make proper exception callback.
- //g_adapterIPServerContext->exceptionCallback(ctx->type);
- }
- ca_mutex_unlock(g_mutexAdapterServerContext);
- }
-
- char srcIPAddress[CA_IPADDR_SIZE] = { 0 };
- if (!inet_ntop(AF_INET, &srcSockAddress.sin_addr.s_addr, srcIPAddress,
- sizeof(srcIPAddress)))
- {
-
- OIC_LOG(ERROR, IP_SERVER_TAG, "inet_ntop is failed!");
- continue;
- }
-
- uint16_t srcPort = ntohs(srcSockAddress.sin_port);
-
- OIC_LOG_V(DEBUG, IP_SERVER_TAG, "Received packet from %s:%d len %d",
- srcIPAddress, srcPort, recvLen);
-
- char *netMask = NULL;
- if (CA_STATUS_OK != CAIPGetInterfaceSubnetMask(info->ifAddr, &netMask))
- {
- OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to get IP subnet");
- continue;
- }
-
- if (!CAAdapterIsSameSubnet(info->ifAddr, srcIPAddress, netMask))
- {
- OIC_LOG(DEBUG, IP_SERVER_TAG,
- "Packet received from different subnet, Ignore!");
- OICFree(netMask);
- continue;
- }
- OICFree(netMask);
-
- CAEndpoint_t ep;
- strncpy(ep.addr, srcIPAddress, MAX_ADDR_STR_SIZE_CA);
- ep.port = srcPort;
- ep.flags = (CATransportFlags_t)CA_IPV4 | CA_IPV6;
- ep.adapter = CA_ADAPTER_IP;
-
- if (info->endpoint.flags & CA_SECURE)
- {
+ if (flags & CA_SECURE)
+ {
#ifdef __WITH_DTLS__
- ep.flags |= CA_SECURE;
- (void)CAAdapterNetDtlsDecrypt(&ep, (uint8_t *)recvBuffer, recvLen);
- OIC_LOG_V(DEBUG, IP_SERVER_TAG,
- "CAAdapterNetDtlsDecrypt returns [%d]", ret);
+ int ret = CAAdapterNetDtlsDecrypt(&sep, (uint8_t *)recvBuffer, recvLen);
+ OIC_LOG_V(DEBUG, TAG, "CAAdapterNetDtlsDecrypt returns [%d]", ret);
+#else
+ OIC_LOG(ERROR, TAG, "Encrypted message but no DTLS");
#endif
- }
- else //both multicast and unicast
- {
- ca_mutex_lock(g_mutexAdapterServerContext);
-
- if (g_adapterIPServerContext->packetReceivedCallback)
- {
- g_adapterIPServerContext->packetReceivedCallback(&ep,
- recvBuffer, recvLen);
- }
-
- ca_mutex_unlock(g_mutexAdapterServerContext);
- }
- }
+ }
+ else
+ {
+ if (g_packetReceivedCallback)
+ {
+ g_packetReceivedCallback(&sep, recvBuffer, recvLen);
}
- CAClearServerInfoList(tempServerInfoList);
}
- OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
+
+ return CA_STATUS_OK;
}
-static CAResult_t CACreateSocket(int *socketFD, const char *localIp, uint16_t *port, bool isSecured)
+void CAIPPullData()
{
- VERIFY_NON_NULL(socketFD, IP_SERVER_TAG, "socketFD is NULL");
- VERIFY_NON_NULL(localIp, IP_SERVER_TAG, "localIp is NULL");
- VERIFY_NON_NULL(port, IP_SERVER_TAG, "port is NULL");
- // Create a UDP socket
- int sock = -1;
-
-#ifdef SOCK_CLOEXEC
- sock = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
-#endif
+ OIC_LOG(DEBUG, TAG, "IN");
+ OIC_LOG(DEBUG, TAG, "OUT");
+}
- if (-1 == sock)
+static int CACreateSocket(int family, uint16_t *port)
+{
+ int socktype = SOCK_DGRAM;
+ #ifdef SOCK_CLOEXEC
+ socktype |= SOCK_CLOEXEC;
+ #endif
+ int fd = socket(family, socktype, IPPROTO_UDP);
+ if (-1 == fd)
{
- sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+ OIC_LOG_V(ERROR, TAG, "create socket failed: %s", strerror(errno));
+ return -1;
}
- if (-1 == sock)
+ #ifndef SOCK_CLOEXEC
+ int fl = fcntl(fd, F_GETFD);
+ if (-1 == fl || -1 == fcntl(fd, F_SETFD, fl|FD_CLOEXEC))
{
- OIC_LOG_V(ERROR, IP_SERVER_TAG, "Failed to create Socket, Error code: %s",
- strerror(errno));
- return CA_STATUS_FAILED;
+ OIC_LOG_V(ERROR, TAG, "set FD_CLOEXEC failed: %s", strerror(errno));
+ close(fd);
+ return -1;
}
+ #endif
- // Make the socket non-blocking
- if (-1 == fcntl(sock, F_SETFL, O_NONBLOCK))
- {
- OIC_LOG_V(ERROR, IP_SERVER_TAG, "Failed to set non-block mode, Error code: %s",
- strerror(errno));
-
- close(sock);
- return CA_STATUS_FAILED;
- }
+ struct sockaddr_storage sa = { .ss_family = family };
+ socklen_t socklen;
- if (0 != *port && !isSecured)
+ if (family == AF_INET6)
{
- int setOptionOn = SOCKETOPTION;
- if (-1 == setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &setOptionOn,
- sizeof(setOptionOn)))
+ int on = 1;
+ if (-1 == setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)))
{
- OIC_LOG_V(ERROR, IP_SERVER_TAG, "Failed to set SO_REUSEADDR! Error code: %s",
- strerror(errno));
- close(sock);
- return CA_STATUS_FAILED;
+ OIC_LOG_V(ERROR, TAG, "IPV6_V6ONLY failed: %s", strerror(errno));
}
+ ((struct sockaddr_in6 *)&sa)->sin6_port = htons(*port);
+ socklen = sizeof (struct sockaddr_in6);
+ }
+ else
+ {
+ ((struct sockaddr_in *)&sa)->sin_port = htons(*port);
+ socklen = sizeof (struct sockaddr_in);
}
- struct sockaddr_in sockAddr = { 0 };
- uint16_t serverPort = *port;
- sockAddr.sin_family = AF_INET;
- sockAddr.sin_port = htons(serverPort);
- if (localIp)
+ if (*port) // use the given port
{
- sockAddr.sin_addr.s_addr = inet_addr(localIp);
+ int on = 1;
+ if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof (on)))
+ {
+ OIC_LOG_V(ERROR, TAG, "SO_REUSEADDR failed: %s", strerror(errno));
+ close(fd);
+ return -1;
+ }
}
- bool isBound = false;
- if (-1 != bind(sock, (struct sockaddr *) &sockAddr, sizeof(sockAddr)))
+ if (-1 == bind(fd, (struct sockaddr *)&sa, socklen))
{
- isBound = true;
+ OIC_LOG_V(ERROR, TAG, "bind socket failed: %s", strerror(errno));
+ close(fd);
+ return -1;
}
- else if (isSecured)
+
+ if (!*port) // return the assigned port
{
- //if secure port 5684 is occupied, trying for another port
- serverPort = 0;
- sockAddr.sin_port = htons(serverPort);
- if (-1 != bind(sock, (struct sockaddr *) &sockAddr, sizeof(sockAddr)))
+ if (-1 == getsockname(fd, (struct sockaddr *)&sa, &socklen))
{
- isBound = true;
+ OIC_LOG_V(ERROR, TAG, "getsockname failed: %s", strerror(errno));
+ close(fd);
+ return -1;
}
+ *port = ntohs(family == AF_INET6 ?
+ ((struct sockaddr_in6 *)&sa)->sin6_port :
+ ((struct sockaddr_in *)&sa)->sin_port);
}
- if (true == isBound)
- {
- struct sockaddr_in sin;
- socklen_t len = sizeof(sin);
+ return fd;
+}
+
+#define CHECKFD(FD) \
+ if (FD > caglobals.ip.maxfd) \
+ caglobals.ip.maxfd = FD;
+#define NEWSOCKET(FAMILY, NAME) \
+ caglobals.ip.NAME.fd = CACreateSocket(FAMILY, &caglobals.ip.NAME.port); \
+ CHECKFD(caglobals.ip.NAME.fd)
- if (-1 == getsockname(sock, (struct sockaddr *)&sin, &len))
+static void CAInitializeNetlink()
+{
+#ifdef __linux__
+ // create NETLINK fd for interface change notifications
+ struct sockaddr_nl sa = { AF_NETLINK, 0, 0, RTMGRP_LINK };
+
+ caglobals.ip.netlinkFd = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC, NETLINK_ROUTE);
+ if (caglobals.ip.netlinkFd == -1)
+ {
+ OIC_LOG_V(ERROR, TAG, "netlink socket failed: %s", strerror(errno));
+ }
+ else
+ {
+ int r = bind(caglobals.ip.netlinkFd, (struct sockaddr *)&sa, sizeof (sa));
+ if (r)
{
- OIC_LOG_V(ERROR, IP_SERVER_TAG, "Failed to get socket[%s]!",
- strerror(errno));
- close(sock);
- return CA_STATUS_FAILED;
+ OIC_LOG_V(ERROR, TAG, "netlink bind failed: %s", strerror(errno));
+ close(caglobals.ip.netlinkFd);
+ caglobals.ip.netlinkFd = -1;
}
else
{
- serverPort = (uint16_t) ntohs(sin.sin_port);
+ CHECKFD(caglobals.ip.netlinkFd);
}
}
- else
- {
- OIC_LOG_V(ERROR, IP_SERVER_TAG, "Failed to bind socket[%s]!", strerror(errno));
- close(sock);
- return CA_STATUS_FAILED;
- }
-
- *port = serverPort;
- *socketFD = sock;
- return CA_STATUS_OK;
+#endif
}
-static void CACloseSocket(int socketFD)
+static void CAInitializePipe()
{
- if (-1 == socketFD)
+ caglobals.ip.selectTimeout = -1;
+#ifdef HAVE_PIPE2
+ int ret = pipe2(caglobals.ip.shutdownFds, O_CLOEXEC);
+#else
+ int ret = pipe(caglobals.ip.shutdownFds);
+ if (-1 != ret)
{
- OIC_LOG(ERROR, IP_SERVER_TAG, "Invalid Socket Fd");
- return;
+ ret = fcntl(caglobals.ip.shutdownFds[0], F_GETFD);
+ if (-1 != ret)
+ {
+ ret = fcntl(caglobals.ip.shutdownFds[0], F_SETFD, ret|FD_CLOEXEC);
+ }
+ if (-1 != ret)
+ {
+ ret = fcntl(caglobals.ip.shutdownFds[1], F_GETFD);
+ }
+ if (-1 != ret)
+ {
+ ret = fcntl(caglobals.ip.shutdownFds[1], F_SETFD, ret|FD_CLOEXEC);
+ }
+ if (-1 == ret)
+ {
+ close(caglobals.ip.shutdownFds[1]);
+ close(caglobals.ip.shutdownFds[0]);
+ caglobals.ip.shutdownFds[0] = -1;
+ caglobals.ip.shutdownFds[1] = -1;
+ }
}
-
- // close the socket
- if (-1 == close(socketFD))
+#endif
+ if (-1 == ret)
{
- OIC_LOG_V(ERROR, IP_SERVER_TAG, "Failed to close the socket, Error code: %s\n",
- strerror(errno));
+ OIC_LOG_V(ERROR, TAG, "pipe failed: %s", strerror(errno));
+ caglobals.ip.selectTimeout = SELECT_TIMEOUT; //poll needed for shutdown
}
}
-static CAResult_t CAStartUnicastServer(const char *localAddress, uint16_t *port,
- bool isSecured, int *serverFD)
+CAResult_t CAIPStartServer(const ca_thread_pool_t threadPool)
{
- OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
+ CAResult_t res = CA_STATUS_OK;
- VERIFY_NON_NULL(serverFD, IP_SERVER_TAG, "serverFD");
- VERIFY_NON_NULL(localAddress, IP_SERVER_TAG, "localAddress");
- VERIFY_NON_NULL(port, IP_SERVER_TAG, "port");
-
- CAResult_t ret = CACreateSocket(serverFD, localAddress, port, isSecured);
- if (CA_STATUS_OK != ret)
+ if (caglobals.ip.started)
{
- OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to create unicast socket");
+ return res;
}
- OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
- return ret;
-}
-
-static CAResult_t CAIPStartPacketReceiverHandler()
-{
- OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
-
- ca_mutex_lock(g_mutexServerInfoList);
-
- uint32_t listLength = u_arraylist_length(g_serverInfoList);
-
- ca_mutex_unlock(g_mutexServerInfoList);
-
- ca_mutex_lock(g_mutexAdapterServerContext);
+ if (!IPv4MulticastAddress.s_addr)
+ {
+ (void)inet_aton(IPv4_MULTICAST, &IPv4MulticastAddress);
+ (void)inet_pton(AF_INET6, IPv6_MULTICAST_INT, &IPv6MulticastAddressInt);
+ (void)inet_pton(AF_INET6, IPv6_MULTICAST_LNK, &IPv6MulticastAddressLnk);
+ (void)inet_pton(AF_INET6, IPv6_MULTICAST_RLM, &IPv6MulticastAddressRlm);
+ (void)inet_pton(AF_INET6, IPv6_MULTICAST_ADM, &IPv6MulticastAddressAdm);
+ (void)inet_pton(AF_INET6, IPv6_MULTICAST_SIT, &IPv6MulticastAddressSit);
+ (void)inet_pton(AF_INET6, IPv6_MULTICAST_ORG, &IPv6MulticastAddressOrg);
+ (void)inet_pton(AF_INET6, IPv6_MULTICAST_GLB, &IPv6MulticastAddressGlb);
+ }
- if (!g_adapterIPServerContext)
+ if (!caglobals.ip.ipv6enabled && !caglobals.ip.ipv4enabled)
{
- OIC_LOG(ERROR, IP_SERVER_TAG, "g_adapterIPServerContext NULL");
- ca_mutex_unlock(g_mutexAdapterServerContext);
- return CA_STATUS_FAILED;
+ caglobals.ip.ipv4enabled = true; // only needed to run CA tests
}
- if (1 == listLength) //Its first time.
+ if (caglobals.ip.ipv6enabled)
{
- g_packetHandlerStopFlag = false;
- if (CA_STATUS_OK != ca_thread_pool_add_task(g_adapterIPServerContext->threadPool,
- CAReceiveHandler, NULL ))
- {
- OIC_LOG(ERROR, IP_SERVER_TAG, "thread_pool_add_task failed!");
- ca_mutex_unlock(g_mutexAdapterServerContext);
- return CA_STATUS_FAILED;
- }
- OIC_LOG(DEBUG, IP_SERVER_TAG, "CAReceiveHandler thread started successfully.");
+ NEWSOCKET(AF_INET6, u6)
+ NEWSOCKET(AF_INET6, u6s)
+ NEWSOCKET(AF_INET6, m6)
+ NEWSOCKET(AF_INET6, m6s)
+ OIC_LOG_V(INFO, TAG, "IPv6 unicast port: %u", caglobals.ip.u6.port);
}
- else
+ if (caglobals.ip.ipv4enabled)
{
- OIC_LOG(DEBUG, IP_SERVER_TAG, "CAReceiveHandler thread already is running");
+ NEWSOCKET(AF_INET, u4)
+ NEWSOCKET(AF_INET, u4s)
+ NEWSOCKET(AF_INET, m4)
+ NEWSOCKET(AF_INET, m4s)
+ OIC_LOG_V(INFO, TAG, "IPv4 unicast port: %u", caglobals.ip.u4.port);
}
- ca_mutex_unlock(g_mutexAdapterServerContext);
- OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
+ OIC_LOG_V(DEBUG, TAG,
+ "socket summary: u6=%d, u6s=%d, u4=%d, u4s=%d, m6=%d, m6s=%d, m4=%d, m4s=%d",
+ caglobals.ip.u6.fd, caglobals.ip.u6s.fd,
+ caglobals.ip.u4.fd, caglobals.ip.u4s.fd,
+ caglobals.ip.m6.fd, caglobals.ip.m6s.fd,
+ caglobals.ip.m4.fd, caglobals.ip.m4s.fd);
- return CA_STATUS_OK;
-}
+ // create pipe for fast shutdown
+ CAInitializePipe();
+ CHECKFD(caglobals.ip.shutdownFds[0]);
+ CHECKFD(caglobals.ip.shutdownFds[1]);
-static void CAIPServerDestroyMutex(void)
-{
- OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
+ // create source of network interface change notifications
+ CAInitializeNetlink();
- if (g_mutexServerInfoList)
- {
- ca_mutex_free(g_mutexServerInfoList);
- g_mutexServerInfoList = NULL;
- }
+ caglobals.ip.selectTimeout = CAGetPollingInterval(caglobals.ip.selectTimeout);
+
+ CAApplyInterfaces();
- if (g_mutexAdapterServerContext)
+ caglobals.ip.terminate = false;
+ res = ca_thread_pool_add_task(threadPool, CAReceiveHandler, NULL);
+ if (CA_STATUS_OK != res)
{
- ca_mutex_free(g_mutexAdapterServerContext);
- g_mutexAdapterServerContext = NULL;
+ OIC_LOG(ERROR, TAG, "thread_pool_add_task failed");
+ return res;
}
+ OIC_LOG(DEBUG, TAG, "CAReceiveHandler thread started successfully.");
- OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
+ caglobals.ip.started = true;
+ return CA_STATUS_OK;
}
-static CAResult_t CAIPServerCreateMutex(void)
+void CAIPStopServer()
{
- OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
+ OIC_LOG(DEBUG, TAG, "IN");
- g_mutexServerInfoList = ca_mutex_new();
- if (!g_mutexServerInfoList)
+ caglobals.ip.terminate = true;
+
+ if (caglobals.ip.shutdownFds[1] != -1)
{
- OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
- return CA_MEMORY_ALLOC_FAILED;
+ close(caglobals.ip.shutdownFds[1]);
+ // receive thread will stop immediately
}
-
- g_mutexAdapterServerContext = ca_mutex_new();
- if (!g_mutexAdapterServerContext)
+ else
{
- OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to created mutex!");
- ca_mutex_free(g_mutexServerInfoList);
- g_mutexServerInfoList = NULL;
- return CA_MEMORY_ALLOC_FAILED;
+ // receive thread will stop in SELECT_TIMEOUT seconds.
}
- OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
- return CA_STATUS_OK;
+ OIC_LOG(DEBUG, TAG, "OUT");
}
-CAResult_t CAIPInitializeServer(const ca_thread_pool_t threadPool)
+void CAWakeUpForChange()
{
- OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
-
- // Input validation
- VERIFY_NON_NULL(threadPool, IP_SERVER_TAG, "Thread pool handle is NULL");
-
- // Initialize mutex
- if (CA_STATUS_OK != CAIPServerCreateMutex())
+ if (caglobals.ip.shutdownFds[1] != -1)
{
- OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to create mutex!");
- return CA_STATUS_FAILED;
+ ssize_t len = 0;
+ do
+ {
+ len = write(caglobals.ip.shutdownFds[1], "w", 1);
+ } while ((len == -1) && (errno == EINTR));
+ if ((len == -1) && (errno != EINTR) && (errno != EPIPE))
+ {
+ OIC_LOG_V(DEBUG, TAG, "write failed: %s", strerror(errno));
+ }
}
+}
- ca_mutex_lock(g_mutexAdapterServerContext);
- g_adapterIPServerContext = (CAAdapterIPServerContext_t *) OICCalloc(1,
- sizeof(CAAdapterIPServerContext_t));
-
- if (!g_adapterIPServerContext)
+static void applyMulticastToInterface4(struct in_addr inaddr)
+{
+ if (!caglobals.ip.ipv4enabled)
{
- OIC_LOG(ERROR, IP_SERVER_TAG, "Malloc failed");
- ca_mutex_unlock(g_mutexAdapterServerContext);
- return CA_MEMORY_ALLOC_FAILED;
+ return;
}
- g_adapterIPServerContext->threadPool = threadPool;
-
- ca_mutex_unlock(g_mutexAdapterServerContext);
-
- ca_mutex_lock(g_mutexServerInfoList);
+ struct ip_mreq mreq = { .imr_multiaddr = IPv4MulticastAddress,
+ .imr_interface = inaddr};
+ if (setsockopt(caglobals.ip.m4.fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof (mreq)))
+ {
+ if (EADDRINUSE != errno)
+ {
+ OIC_LOG_V(ERROR, TAG, "IPv4 IP_ADD_MEMBERSHIP failed: %s", strerror(errno));
+ }
+ }
+ if (setsockopt(caglobals.ip.m4s.fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof (mreq)))
+ {
+ if (EADDRINUSE != errno)
+ {
+ OIC_LOG_V(ERROR, TAG, "secure IPv4 IP_ADD_MEMBERSHIP failed: %s", strerror(errno));
+ }
+ }
+}
- g_serverInfoList = u_arraylist_create();
- if (!g_serverInfoList)
+static void applyMulticast6(int fd, struct in6_addr *addr, uint32_t interface)
+{
+ struct ipv6_mreq mreq;
+ mreq.ipv6mr_multiaddr = *addr;
+ mreq.ipv6mr_interface = interface;
+ if (setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq, sizeof (mreq)))
{
- OIC_LOG(ERROR, IP_SERVER_TAG, "u_arraylist_create failed");
- ca_mutex_unlock(g_mutexServerInfoList);
- return CA_MEMORY_ALLOC_FAILED;
+ if (EADDRINUSE != errno)
+ {
+ OIC_LOG_V(ERROR, TAG, "IPv6 IP_ADD_MEMBERSHIP failed: %s", strerror(errno));
+ }
}
- ca_mutex_unlock(g_mutexServerInfoList);
- OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
- return CA_STATUS_OK;
}
-void CAIPTerminateServer()
+static void applyMulticastToInterface6(uint32_t interface)
{
- OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
- ca_mutex_lock(g_mutexAdapterServerContext);
- if (!g_adapterIPServerContext)
+ if (!caglobals.ip.ipv6enabled)
{
- OIC_LOG(ERROR, IP_SERVER_TAG, "g_adapterIPServerContext NULL");
- ca_mutex_unlock(g_mutexAdapterServerContext);
return;
}
-
- OICFree(g_adapterIPServerContext);
- g_adapterIPServerContext = NULL;
-
- ca_mutex_unlock(g_mutexAdapterServerContext);
-
- ca_mutex_lock(g_mutexServerInfoList);
-
- CAClearServerInfoList(g_serverInfoList);
- g_serverInfoList = NULL;
-
- ca_mutex_unlock(g_mutexServerInfoList);
- // Destroy mutex
- CAIPServerDestroyMutex();
-
- OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
-
+ //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressInt, interface);
+ applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressLnk, interface);
+ //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressRlm, interface);
+ //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressAdm, interface);
+ //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressSit, interface);
+ //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressOrg, interface);
+ //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressGlb, interface);
+ //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressInt, interface);
+ applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressLnk, interface);
+ //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressRlm, interface);
+ //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressAdm, interface);
+ //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressSit, interface);
+ //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressOrg, interface);
+ //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressGlb, interface);
}
-CAResult_t CAIPStartUnicastServer(const char *localAddress, uint16_t *port, bool isSecured)
+static void CAApplyInterfaces()
{
- OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
+ u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
+ if (!iflist)
+ {
+ OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
+ return;
+ }
- // Input validation
- VERIFY_NON_NULL(localAddress, IP_SERVER_TAG, "localAddress");
- VERIFY_NON_NULL(port, IP_SERVER_TAG, "port");
+ uint32_t len = u_arraylist_length(iflist);
+ OIC_LOG_V(DEBUG, TAG, "IP network interfaces found: %d", len);
- ca_mutex_lock(g_mutexServerInfoList);
- bool isUnicastServerStarted = CAIsUnicastServerStarted(g_serverInfoList, localAddress, *port);
- if (!isUnicastServerStarted)
+ for (uint32_t i = 0; i < len; i++)
{
- int unicastServerFd = -1;
- if (CA_STATUS_OK != CAStartUnicastServer(localAddress, port, isSecured,
- &unicastServerFd))
- {
- OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to start unicast server!");
- ca_mutex_unlock(g_mutexServerInfoList);
- return CA_STATUS_FAILED;
- }
+ CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
- CAServerInfo_t *info = (CAServerInfo_t *) OICCalloc(1, sizeof(CAServerInfo_t));
- if (!info)
+ if (!ifitem)
{
- OIC_LOG(ERROR, IP_SERVER_TAG, "Malloc failed");
- close(unicastServerFd);
- ca_mutex_unlock(g_mutexServerInfoList);
- return CA_MEMORY_ALLOC_FAILED;
- }
-
- char *netMask = NULL;
- if (CA_STATUS_OK != CAIPGetInterfaceSubnetMask(localAddress, &netMask))
- {
- OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to get IP subnet");
+ continue;
}
- if (netMask)
+ if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
{
- OICStrcpy(info->subNetMask, sizeof(info->subNetMask), netMask);
- OICFree(netMask);
+ continue;
}
- OICStrcpy(info->endpoint.addr, sizeof(info->endpoint.addr), localAddress);
- info->endpoint.port = *port;
- info->endpoint.flags = isSecured ? CA_SECURE : 0;
- info->endpoint.adapter = CA_ADAPTER_IP;
- info->socketFd = unicastServerFd;
- info->isServerStarted = true;
- info->isMulticastServer = false;
- OICStrcpy(info->ifAddr, sizeof(info->ifAddr), localAddress);
-
- CAResult_t res = CAAddServerInfo(g_serverInfoList, info);
- if (CA_STATUS_OK != res)
+ if (ifitem->family == AF_INET)
{
- OIC_LOG(ERROR, IP_SERVER_TAG, "CAAddServerInfo failed!");
- close(unicastServerFd);
- ca_mutex_unlock(g_mutexServerInfoList);
- return res;
+ struct in_addr inaddr;
+ inaddr.s_addr = ifitem->ipv4addr;
+ applyMulticastToInterface4(inaddr);
+ OIC_LOG_V(DEBUG, TAG, "IPv4 network interface: %s", ifitem->name);
}
- ca_mutex_unlock(g_mutexServerInfoList);
-
- res = CAIPStartPacketReceiverHandler();
- if (CA_STATUS_OK != res)
+ if (ifitem->family == AF_INET6)
{
- OIC_LOG(ERROR, IP_SERVER_TAG, "CAIPStartPacketReceiverHandler failed!");
- close(unicastServerFd);
- return res;
+ applyMulticastToInterface6(ifitem->index);
+ OIC_LOG_V(DEBUG, TAG, "IPv6 network interface: %s", ifitem->name);
}
}
- else
- {
- OIC_LOG_V(DEBUG, IP_SERVER_TAG, "Already Unicast Server Started ip [%s] port [%d]",
- localAddress, *port);
- ca_mutex_unlock(g_mutexServerInfoList);
- }
- OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
- return CA_STATUS_OK;
+ u_arraylist_destroy(iflist);
}
-CAResult_t CAIPStartMulticastServer(const char *localAddress, const char *multicastAddress,
- uint16_t multicastPort)
+static void CAProcessNewInterface(CAInterface_t *ifitem)
+{
+ applyMulticastToInterface6(ifitem->index);
+ struct in_addr inaddr;
+ inaddr.s_addr = ifitem->ipv4addr;
+ applyMulticastToInterface4(inaddr);
+}
+static void CAHandleNetlink()
{
- OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
+#ifdef __linux__
+ char buf[4096];
+ struct nlmsghdr *nh;
+ struct sockaddr_nl sa;
+ struct iovec iov = { buf, sizeof (buf) };
+ struct msghdr msg = { (void *)&sa, sizeof (sa), &iov, 1, NULL, 0, 0 };
- // Input validation
- VERIFY_NON_NULL(localAddress, IP_SERVER_TAG, "localAddress");
- VERIFY_NON_NULL(multicastAddress, IP_SERVER_TAG, "port");
+ size_t len = recvmsg(caglobals.ip.netlinkFd, &msg, 0);
- uint16_t port = multicastPort;
- if (0 >= port)
+ for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len))
{
- OIC_LOG(ERROR, IP_SERVER_TAG, "Invalid input: Multicast port is invalid!");
- return CA_STATUS_INVALID_PARAM;
- }
-
- ca_mutex_lock(g_mutexServerInfoList);
- bool isMulticastServerStarted = CAIsMulticastServerStarted(g_serverInfoList, localAddress,
- multicastAddress, port);
- if (!isMulticastServerStarted)
- {
- int mulicastServerFd = -1;
- CAResult_t ret = CACreateSocket(&mulicastServerFd, multicastAddress, &port, false);
- if (ret != CA_STATUS_OK)
+ if (nh->nlmsg_type != RTM_NEWLINK)
{
- OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to create multicast socket");
- ca_mutex_unlock(g_mutexServerInfoList);
- return ret;
+ continue;
}
- struct ip_mreq multicastMemberReq = {.imr_interface.s_addr = inet_addr(localAddress)};
- inet_aton(multicastAddress, &multicastMemberReq.imr_multiaddr);
-
- if (-1 == setsockopt(mulicastServerFd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
- (char *) &multicastMemberReq, sizeof(struct ip_mreq)))
+ struct ifinfomsg *ifi = (struct ifinfomsg *)NLMSG_DATA(nh);
+ if (!ifi || (ifi->ifi_flags & IFF_LOOPBACK) || !(ifi->ifi_flags & IFF_RUNNING))
{
- OIC_LOG_V(ERROR, IP_SERVER_TAG,
- "Failed to add to multicast group, Error code: %s\n", strerror(errno));
- close(mulicastServerFd);
- ca_mutex_unlock(g_mutexServerInfoList);
- return CA_STATUS_FAILED;
+ continue;
}
- CAServerInfo_t *info = (CAServerInfo_t *) OICCalloc(1, sizeof(CAServerInfo_t));
- if (!info)
- {
- OIC_LOG(ERROR, IP_SERVER_TAG, "Malloc failed");
- close(mulicastServerFd);
- ca_mutex_unlock(g_mutexServerInfoList);
- return CA_MEMORY_ALLOC_FAILED;
- }
+ int newIndex = ifi->ifi_index;
- char *netMask = NULL;
- if (CA_STATUS_OK != CAIPGetInterfaceSubnetMask(localAddress, &netMask))
+ u_arraylist_t *iflist = CAIPGetInterfaceInformation(newIndex);
+ if (!iflist)
{
- OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to get IP subnet");
+ OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
+ return;
}
- if (netMask)
+
+ uint32_t listLength = u_arraylist_length(iflist);
+ for (uint32_t i = 0; i < listLength; i++)
{
- OICStrcpy(info->subNetMask, sizeof(info->subNetMask), netMask);
- OICFree(netMask);
+ CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
+ if (!ifitem)
+ {
+ continue;
+ }
+
+ if ((int)ifitem->index != newIndex)
+ {
+ continue;
+ }
+
+ CAProcessNewInterface(ifitem);
+ break; // we found the one we were looking for
}
+ u_arraylist_destroy(iflist);
+ }
+#endif // __linux__
+}
- OICStrcpy(info->endpoint.addr, sizeof(info->endpoint.addr), multicastAddress);
- info->endpoint.port = multicastPort;
- info->endpoint.flags = 0;
- info->socketFd = mulicastServerFd;
- info->isServerStarted = true;
- info->isMulticastServer = true;
- OICStrcpy(info->ifAddr, sizeof(info->ifAddr), localAddress);
+void CAIPSetPacketReceiveCallback(CAIPPacketReceivedCallback callback)
+{
+ OIC_LOG(DEBUG, TAG, "IN");
- ret = CAAddServerInfo(g_serverInfoList, info);
+ g_packetReceivedCallback = callback;
- if (CA_STATUS_OK != ret)
- {
- OIC_LOG(ERROR, IP_SERVER_TAG, "CAAddServerInfo failed!");
- close(mulicastServerFd);
- ca_mutex_unlock(g_mutexServerInfoList);
- return ret;
- }
- ca_mutex_unlock(g_mutexServerInfoList);
+ OIC_LOG(DEBUG, TAG, "OUT");
+}
+
+void CAIPSetExceptionCallback(CAIPExceptionCallback callback)
+{
+ OIC_LOG(DEBUG, TAG, "IN");
- ret = CAIPStartPacketReceiverHandler();
- if (CA_STATUS_OK != ret)
+ g_exceptionCallback = callback;
+
+ OIC_LOG(DEBUG, TAG, "OUT");
+}
+
+static void sendData(int fd, const CAEndpoint_t *endpoint,
+ const void *data, uint32_t dlen,
+ const char *cast, const char *fam)
+{
+ OIC_LOG(DEBUG, TAG, "IN");
+
+ char *secure = (endpoint->flags & CA_SECURE) ? "secure " : "";
+ (void)secure; // eliminates release warning
+ struct sockaddr_storage sock;
+ CAConvertNameToAddr(endpoint->addr, endpoint->port, &sock);
+
+ socklen_t socklen;
+ if (sock.ss_family == AF_INET6)
+ {
+ struct sockaddr_in6 *sock6 = (struct sockaddr_in6 *)&sock;
+ if (!sock6->sin6_scope_id)
{
- OIC_LOG(ERROR, IP_SERVER_TAG, "CAIPStartPacketReceiverHandler failed!");
- close(mulicastServerFd);
- return ret;
+ sock6->sin6_scope_id = endpoint->interface;
}
+ socklen = sizeof(struct sockaddr_in6);
}
else
{
- OIC_LOG_V(DEBUG, IP_SERVER_TAG,
- "Multicast Server is already started on interface addr[%s]", localAddress);
- ca_mutex_unlock(g_mutexServerInfoList);
+ socklen = sizeof(struct sockaddr_in);
}
- OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
- return CA_STATUS_OK;
+ ssize_t len = sendto(fd, data, dlen, 0, (struct sockaddr *)&sock, socklen);
+ if (-1 == len)
+ {
+ // If logging is not defined/enabled.
+ (void)cast;
+ (void)fam;
+ OIC_LOG_V(ERROR, TAG, "%s%s %s sendTo failed: %s", secure, cast, fam, strerror(errno));
+ }
+ else
+ {
+ OIC_LOG_V(INFO, TAG, "%s%s %s sendTo is successful: %d bytes", secure, cast, fam, len);
+ }
}
-CAResult_t CAIPStopServer(const char *interfaceAddress)
+static void sendMulticastData6(const u_arraylist_t *iflist,
+ CAEndpoint_t *endpoint,
+ const void *data, uint32_t datalen)
{
- OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
-
- VERIFY_NON_NULL(interfaceAddress, IP_SERVER_TAG, "interfaceAddress is NULL");
-
- ca_mutex_lock(g_mutexServerInfoList);
- uint32_t listIndex = 0;
- uint32_t listLength = u_arraylist_length(g_serverInfoList);
+ int scope = endpoint->flags & CA_SCOPE_MASK;
+ char *ipv6mcname = ipv6mcnames[scope];
+ if (!ipv6mcname)
+ {
+ OIC_LOG_V(INFO, TAG, "IPv6 multicast scope invalid: %d", scope);
+ return;
+ }
+ OICStrcpy(endpoint->addr, sizeof(endpoint->addr), ipv6mcname);
+ int fd = caglobals.ip.u6.fd;
- for (listIndex = 0; listIndex < listLength;)
+ uint32_t len = u_arraylist_length(iflist);
+ for (uint32_t i = 0; i < len; i++)
{
- CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(g_serverInfoList, listIndex);
- if (!info)
+ CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
+ if (!ifitem)
{
- listIndex++;
continue;
}
-
- if (info->isMulticastServer && strncmp(interfaceAddress, info->ifAddr, strlen(info->ifAddr))
- == 0)
+ if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
{
- if (u_arraylist_remove(g_serverInfoList, listIndex))
- {
- struct ip_mreq multicastMemberReq = { { 0 }, { 0 } };
-
- multicastMemberReq.imr_interface.s_addr = inet_addr(info->ifAddr);
- inet_aton(info->endpoint.addr, &multicastMemberReq.imr_multiaddr);
- if (-1 == setsockopt(info->socketFd, IPPROTO_IP, IP_DROP_MEMBERSHIP,
- (char *) &multicastMemberReq, sizeof(struct ip_mreq)))
- {
- OIC_LOG_V(ERROR, IP_SERVER_TAG,
- "Failed to leave multicast group, Error code: %s", strerror(errno));
- }
- CACloseSocket(info->socketFd);
- OICFree(info);
- OIC_LOG(DEBUG, IP_SERVER_TAG, "Multicast server is stopped successfully.");
- // Reduce list length by 1 as we removed one element.
- listLength--;
- }
- else
- {
- OIC_LOG(ERROR, IP_SERVER_TAG, "u_arraylist_remove failed.");
- ca_mutex_unlock(g_mutexServerInfoList);
- return CA_STATUS_FAILED;
- }
+ continue;
}
- else if (strncmp(interfaceAddress, info->endpoint.addr, strlen(info->endpoint.addr)) == 0)
+ if (ifitem->family != AF_INET6)
{
- if (u_arraylist_remove(g_serverInfoList, listIndex))
- {
- CACloseSocket(info->socketFd);
- OICFree(info);
- OIC_LOG(DEBUG, IP_SERVER_TAG, "Unicast server is stopped successfully.");
- // Reduce list length by 1 as we removed one element.
- listLength--;
- }
- else
- {
- OIC_LOG(ERROR, IP_SERVER_TAG, "u_arraylist_remove failed.");
- ca_mutex_unlock(g_mutexServerInfoList);
- return CA_STATUS_FAILED;
- }
+ continue;
}
- else
+
+ int index = ifitem->index;
+ if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &index, sizeof (index)))
{
- listIndex++;
+ OIC_LOG_V(ERROR, TAG, "setsockopt6 failed: %s", strerror(errno));
+ return;
}
+ sendData(fd, endpoint, data, datalen, "multicast", "ipv6");
}
-
- OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
- ca_mutex_unlock(g_mutexServerInfoList);
- return CA_STATUS_OK;
}
-CAResult_t CAIPStopAllServers()
+static void sendMulticastData4(const u_arraylist_t *iflist,
+ CAEndpoint_t *endpoint,
+ const void *data, uint32_t datalen)
{
- OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
+ struct ip_mreq mreq = { .imr_multiaddr = IPv4MulticastAddress };
+ OICStrcpy(endpoint->addr, sizeof(endpoint->addr), IPv4_MULTICAST);
+ int fd = caglobals.ip.u4.fd;
- g_packetHandlerStopFlag = true;
-
- ca_mutex_lock(g_mutexServerInfoList);
-
- uint32_t listIndex = 0;
- uint32_t listLength = u_arraylist_length(g_serverInfoList);
- for (listIndex = 0; listIndex < listLength;)
+ uint32_t len = u_arraylist_length(iflist);
+ for (uint32_t i = 0; i < len; i++)
{
- CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(g_serverInfoList, listIndex);
- if (!info)
+ CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
+ if (!ifitem)
{
- listIndex++;
continue;
}
- if (u_arraylist_remove(g_serverInfoList, listIndex))
+ if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
{
- if (info->isMulticastServer)
- {
- struct ip_mreq multicastMemberReq = { { 0 }, { 0 } };
-
- multicastMemberReq.imr_interface.s_addr = inet_addr(info->ifAddr);
- inet_aton(info->endpoint.addr, &multicastMemberReq.imr_multiaddr);
- if (-1 == setsockopt(info->socketFd, IPPROTO_IP, IP_DROP_MEMBERSHIP,
- (char *) &multicastMemberReq, sizeof(struct ip_mreq)))
- {
- OIC_LOG_V(ERROR, IP_SERVER_TAG,
- "Failed to leave multicast group, Error code: %s", strerror(errno));
- }
- }
- CACloseSocket(info->socketFd);
- //Freeing server info.
- OICFree(info);
- // Reduce list length by 1 as we removed one element.
- listLength--;
+ continue;
}
- else
+ if (ifitem->family != AF_INET)
{
- OIC_LOG(ERROR, IP_SERVER_TAG, "u_arraylist_remove failed.");
- ca_mutex_unlock(g_mutexServerInfoList);
- return CA_STATUS_FAILED;
+ continue;
}
- }
- ca_mutex_unlock(g_mutexServerInfoList);
-
- OIC_LOG(DEBUG, IP_SERVER_TAG, "All Server stopped successfully. OUT");
- return CA_STATUS_OK;
-}
-
-void CAIPPullData()
-{
- OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
- OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
+ struct in_addr inaddr;
+ inaddr.s_addr = ifitem->ipv4addr;
+ mreq.imr_interface = inaddr;
+ if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &mreq, sizeof (mreq)))
+ {
+ OIC_LOG_V(ERROR, TAG, "send IP_MULTICAST_IF failed: %s (using defualt)",
+ strerror(errno));
+ }
+ sendData(fd, endpoint, data, datalen, "multicast", "ipv4");
+ }
}
-uint16_t CAGetServerPortNum(const char *ipAddress, bool isSecured)
+void CAIPSendData(CAEndpoint_t *endpoint, const void *data, uint32_t datalen,
+ bool isMulticast)
{
- ca_mutex_lock(g_mutexServerInfoList);
-
- uint16_t port = CAGetServerPort(g_serverInfoList, ipAddress, isSecured);
+ VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint is NULL");
+ VERIFY_NON_NULL_VOID(data, TAG, "data is NULL");
- ca_mutex_unlock(g_mutexServerInfoList);
+ bool isSecure = (endpoint->flags & CA_SECURE) != 0;
- return port;
-}
-
-CAResult_t CAGetIPServerInfoList(u_arraylist_t **serverInfoList)
-{
- OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
- ca_mutex_lock(g_mutexServerInfoList);
-
- uint32_t list_index = 0;
- uint32_t list_length = u_arraylist_length(g_serverInfoList);
- for (list_index = 0; list_index < list_length; list_index++)
+ if (isMulticast)
{
- CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(g_serverInfoList, list_index);
- if (!info)
+ endpoint->port = isSecure ? CA_SECURE_COAP : CA_COAP;
+
+ u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
+ if (!iflist)
{
- continue;
+ OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
+ return;
}
- CAServerInfo_t *newNetinfo = (CAServerInfo_t *) OICMalloc(sizeof(CAServerInfo_t));
- if (!newNetinfo)
+ if ((endpoint->flags & CA_IPV6) && caglobals.ip.ipv6enabled)
{
- OIC_LOG(ERROR, IP_SERVER_TAG, "Malloc failed!");
- ca_mutex_unlock(g_mutexServerInfoList);
- return CA_MEMORY_ALLOC_FAILED;
+ sendMulticastData6(iflist, endpoint, data, datalen);
}
-
- *newNetinfo = *info;
-
- CAResult_t result = u_arraylist_add(*serverInfoList, (void *) newNetinfo);
- if (CA_STATUS_OK != result)
+ if ((endpoint->flags & CA_IPV4) && caglobals.ip.ipv4enabled)
{
- OIC_LOG(ERROR, IP_SERVER_TAG, "u_arraylist_add failed!");
- ca_mutex_unlock(g_mutexServerInfoList);
- return CA_STATUS_FAILED;
+ sendMulticastData4(iflist, endpoint, data, datalen);
}
- }
- ca_mutex_unlock(g_mutexServerInfoList);
- OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
- return CA_STATUS_OK;
-}
-
-void CAIPSetPacketReceiveCallback(CAIPPacketReceivedCallback callback)
-{
- OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
-
- ca_mutex_lock(g_mutexAdapterServerContext);
- if (g_adapterIPServerContext)
- {
- g_adapterIPServerContext->packetReceivedCallback = callback;
+ u_arraylist_destroy(iflist);
}
else
{
- OIC_LOG(ERROR, IP_SERVER_TAG, "g_adapterIPServerContext NULL");
- }
-
- ca_mutex_unlock(g_mutexAdapterServerContext);
+ if (!endpoint->port) // unicast discovery
+ {
+ endpoint->port = isSecure ? CA_SECURE_COAP : CA_COAP;
+ }
- OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
+ int fd;
+ if (caglobals.ip.ipv6enabled && (endpoint->flags & CA_IPV6))
+ {
+ fd = isSecure ? caglobals.ip.u6s.fd : caglobals.ip.u6.fd;
+ #ifndef __WITH_DTLS__
+ fd = caglobals.ip.u6.fd;
+ #endif
+ sendData(fd, endpoint, data, datalen, "unicast", "ipv6");
+ }
+ if (caglobals.ip.ipv4enabled && (endpoint->flags & CA_IPV4))
+ {
+ fd = isSecure ? caglobals.ip.u4s.fd : caglobals.ip.u4.fd;
+ #ifndef __WITH_DTLS__
+ fd = caglobals.ip.u4.fd;
+ #endif
+ sendData(fd, endpoint, data, datalen, "unicast", "ipv4");
+ }
+ }
}
-void CAIPSetErrorHandleCallback(CAIPErrorHandleCallback ipErrorCallback)
+CAResult_t CAGetIPInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
{
- OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
+ OIC_LOG(DEBUG, TAG, "IN");
- ca_mutex_lock(g_mutexAdapterServerContext);
+ VERIFY_NON_NULL(info, TAG, "info is NULL");
+ VERIFY_NON_NULL(size, TAG, "size is NULL");
- if (g_adapterIPServerContext)
+ u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
+ if (!iflist)
{
- g_adapterIPServerContext->IPErrorCallback = ipErrorCallback;
+ OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
+ return CA_STATUS_FAILED;
}
- ca_mutex_unlock(g_mutexAdapterServerContext);
-
- OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
-}
-
-void CAIPSetExceptionCallback(CAIPExceptionCallback callback)
-{
- OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
- ca_mutex_lock(g_mutexAdapterServerContext);
+ uint32_t len = u_arraylist_length(iflist);
- if (g_adapterIPServerContext)
+ CAEndpoint_t *eps = (CAEndpoint_t *)OICCalloc(len, sizeof (CAEndpoint_t));
+ if (!eps)
{
- g_adapterIPServerContext->exceptionCallback = callback;
+ OIC_LOG(ERROR, TAG, "Malloc Failed");
+ u_arraylist_destroy(iflist);
+ return CA_MEMORY_ALLOC_FAILED;
}
- else
+
+ for (uint32_t i = 0, j = 0; i < len; i++)
{
- OIC_LOG(ERROR, IP_SERVER_TAG, "g_adapterIPServerContext NULL");
+ CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
+ if(!ifitem)
+ {
+ continue;
+ }
+
+ OICStrcpy(eps[j].addr, CA_INTERFACE_NAME_SIZE, ifitem->name);
+ eps[j].flags = ifitem->family == AF_INET6 ? CA_IPV6 : CA_IPV4;
+ eps[j].adapter = CA_ADAPTER_IP;
+ eps[j].interface = 0;
+ eps[j].port = 0;
+ j++;
}
- ca_mutex_unlock(g_mutexAdapterServerContext);
+ *info = eps;
+ *size = len;
- OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
+ u_arraylist_destroy(iflist);
+
+ OIC_LOG(DEBUG, TAG, "OUT");
+ return CA_STATUS_OK;
}