X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=resource%2Fcsdk%2Fconnectivity%2Fsrc%2Fip_adapter%2Fcaipserver.c;h=d54de566ccf9c91442e1149921bf4ee095ad62f8;hb=c315c87e07c4080ecd0ef488e7a1047bc3c509b2;hp=2cd8e007dea80689321b45cf9a8e4c2fe79a3dfb;hpb=e5337af1b857936fce8295ffe468e1accdfc8350;p=platform%2Fupstream%2Fiotivity.git diff --git a/resource/csdk/connectivity/src/ip_adapter/caipserver.c b/resource/csdk/connectivity/src/ip_adapter/caipserver.c index 2cd8e00..d54de56 100644 --- a/resource/csdk/connectivity/src/ip_adapter/caipserver.c +++ b/resource/csdk/connectivity/src/ip_adapter/caipserver.c @@ -25,6 +25,12 @@ #define _GNU_SOURCE // for in6_pktinfo #endif +#ifdef __TIZENRT__ +#include +#include +#endif + +#include "iotivity_config.h" #include #if !defined(_WIN32) #include @@ -39,6 +45,7 @@ #endif #include +#include #if !defined(_MSC_VER) #include #endif //!defined(_MSC_VER) @@ -56,33 +63,52 @@ #include #endif -#include "pdu.h" +#ifdef __TIZENRT__ +#include +#include +#endif + +#include #include "caipinterface.h" +#include "caipnwmonitor.h" #include "caadapterutils.h" -#ifdef __WITH_DTLS__ -#include "caadapternetdtls.h" +#if defined(__WITH_DTLS__) || defined(__WITH_TLS__) +#include "ca_adapter_net_ssl.h" #endif -#include "camutex.h" +#include "octhread.h" #include "oic_malloc.h" #include "oic_string.h" -#include "platform_features.h" #define USE_IP_MREQN #if defined(_WIN32) #undef USE_IP_MREQN #endif +#ifdef __TIZEN__ +#include +#endif + /* * Logging tag for module name */ -#define TAG "OIC_CA_IP_SERVER" +//#define TAG "OIC_CA_IP_SERVER" +#define TAG IP_SERVER_TAG +#ifdef __TIZENRT__ +mqd_t g_nwevent_mqfd; +#ifdef CONFIG_NET_LWIP +#define SOCK_CLOEXEC 0 +#else +#define SOCK_CLOEXEC 1 +#endif +#endif #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 MOBILE_INTERFACES 2 #define IPv6_MULTICAST_INT "ff01::158" static struct in6_addr IPv6MulticastAddressInt; #define IPv6_MULTICAST_LNK "ff02::158" @@ -117,6 +143,41 @@ static char *ipv6mcnames[IPv6_DOMAINS] = { NULL }; +// Samsung Mobile +static char *mobileinferfaces[MOBILE_INTERFACES] = { + "rmnet", "pdp" +}; + +#ifdef __TIZENRT__ +struct in6_pktinfo { + struct in6_addr ipi6_addr; + int ipi6_ifindex; +}; + +struct in_pktinfo +{ + unsigned int ipi_ifindex; /* Interface index */ + struct in_addr ipi_spec_dst; /* Local address */ + struct in_addr ipi_addr; /* Header Destination + address */ +}; + + +#define RTMGRP_LINK 1 +#define IP_PKTINFO 8 +#define IPV6_PKTINFO 50 +#define IPV6_MULTICAST_IF 9 +#define IPV6_V6ONLY 27 +#define IPV6_RECVPKTINFO 50 +#define IPV6_JOIN_GROUP 12 +#endif + +/** + * By default, IP multicast datagrams are sent with a time-to-live (TTL) of 1. + * An application can choose an initial TTL. + */ +static size_t multicastTTL = 1; + #if defined (_WIN32) #define IFF_UP_RUNNING_FLAGS (IFF_UP) @@ -143,18 +204,59 @@ static void CASelectReturned(fd_set *readFds, int ret); #else static void CAEventReturned(CASocketFd_t socket); #endif -static void CAProcessNewInterface(CAInterface_t *ifchanged); + static CAResult_t CAReceiveMessage(CASocketFd_t fd, CATransportFlags_t flags); +#ifdef __TIZEN__ +static int cleanup_pop_arg = 1; + +static void CAIPCleanupHandler(void *arg) +{ + OIC_LOG(DEBUG, TAG, "Called clean-up handler"); + + if (caglobals.ip.shutdownFds[0] != OC_INVALID_SOCKET) + { + close(caglobals.ip.shutdownFds[0]); + caglobals.ip.shutdownFds[0] = OC_INVALID_SOCKET; + } +} + +static void CAReceiveHandler(void *data) +{ + (void)data; + OIC_LOG(DEBUG, TAG, "IN - CAReceiveHandler"); + + pthread_cleanup_push(CAIPCleanupHandler, NULL); + + while (!caglobals.ip.terminate) + { + CAFindReadyMessage(); + } + + pthread_cleanup_pop(cleanup_pop_arg); + + OIC_LOG(DEBUG, TAG, "OUT - CAReceiveHandler"); +} +#else static void CAReceiveHandler(void *data) { (void)data; + OIC_LOG(DEBUG, TAG, "IN - CAReceiveHandler"); while (!caglobals.ip.terminate) { CAFindReadyMessage(); } +#ifndef __TIZENRT__ + if (caglobals.ip.shutdownFds[0] != OC_INVALID_SOCKET) + { + close(caglobals.ip.shutdownFds[0]); + caglobals.ip.shutdownFds[0] = OC_INVALID_SOCKET; + } +#endif + OIC_LOG(DEBUG, TAG, "OUT - CAReceiveHandler"); } +#endif #if !defined(WSA_WAIT_EVENT_0) @@ -197,11 +299,12 @@ static void CAFindReadyMessage() SET(m6s, &readFds) SET(m4, &readFds) SET(m4s, &readFds) - +#ifndef __TIZENRT__ if (caglobals.ip.shutdownFds[0] != -1) { FD_SET(caglobals.ip.shutdownFds[0], &readFds); } +#endif if (caglobals.ip.netlinkFd != OC_INVALID_SOCKET) { FD_SET(caglobals.ip.netlinkFd, &readFds); @@ -211,20 +314,33 @@ static void CAFindReadyMessage() if (caglobals.ip.terminate) { - OIC_LOG_V(DEBUG, TAG, "Packet receiver Stop request received."); + OIC_LOG_V(INFO, TAG, "Packet receiver Stop request received."); return; } - - if (ret <= 0) +#ifdef __TIZENRT__ + u_arraylist_t *iflist = CAFindInterfaceChange(); + if (iflist) { - if (ret < 0) + uint32_t listLength = u_arraylist_length(iflist); + for (uint32_t i = 0; i < listLength; i++) { - OIC_LOG_V(FATAL, TAG, "select error %s", CAIPS_GET_ERROR); + CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i); + if (ifitem) + { + CAProcessNewInterface(ifitem); + } } - return; + u_arraylist_destroy(iflist); + } +#endif + if (0 < ret) + { + CASelectReturned(&readFds, ret); + } + else if (0 > ret) + { + OIC_LOG_V(FATAL, TAG, "select error %s", CAIPS_GET_ERROR); } - - CASelectReturned(&readFds, ret); } static void CASelectReturned(fd_set *readFds, int ret) @@ -245,14 +361,25 @@ static void CASelectReturned(fd_set *readFds, int ret) else ISSET(m4s, readFds, CA_MULTICAST | CA_IPV4 | CA_SECURE) else if ((caglobals.ip.netlinkFd != OC_INVALID_SOCKET) && FD_ISSET(caglobals.ip.netlinkFd, readFds)) { - CAInterface_t *ifchanged = CAFindInterfaceChange(); - if (ifchanged) +#ifndef __TIZENRT__ + u_arraylist_t *iflist = CAFindInterfaceChange(); + if (iflist) { - CAProcessNewInterface(ifchanged); - OICFree(ifchanged); + uint32_t listLength = u_arraylist_length(iflist); + for (uint32_t i = 0; i < listLength; i++) + { + CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i); + if (ifitem) + { + CAProcessNewInterface(ifitem); + } + } + u_arraylist_destroy(iflist); } break; +#endif } +#ifndef __TIZENRT__ else if (FD_ISSET(caglobals.ip.shutdownFds[0], readFds)) { char buf[10] = {0}; @@ -263,6 +390,7 @@ static void CASelectReturned(fd_set *readFds, int ret) } break; } +#endif else { break; @@ -399,7 +527,7 @@ static void CAFindReadyMessage() OIC_LOG_V(ERROR, TAG, "WSAResetEvent failed 0x%08x", WSAGetLastError()); } - // Break out if shutdownEvent is triggered + // Break out if shutdownEvent is triggered. if ((caglobals.ip.shutdownEvent != WSA_INVALID_EVENT) && (caglobals.ip.shutdownEvent == eventArray[eventIndex])) { @@ -461,14 +589,7 @@ static void CAEventReturned(CASocketFd_t socket) void CADeInitializeIPGlobals() { - CLOSE_SOCKET(u6); - CLOSE_SOCKET(u6s); - CLOSE_SOCKET(u4); - CLOSE_SOCKET(u4s); - CLOSE_SOCKET(m6); - CLOSE_SOCKET(m6s); - CLOSE_SOCKET(m4); - CLOSE_SOCKET(m4s); + CloseMulticastSocket(); if (caglobals.ip.netlinkFd != OC_INVALID_SOCKET) { @@ -483,6 +604,7 @@ void CADeInitializeIPGlobals() static CAResult_t CAReceiveMessage(CASocketFd_t fd, CATransportFlags_t flags) { + OIC_LOG(DEBUG, TAG, "IN - CAReceiveMessage"); char recvBuffer[COAP_MAX_PDU_SIZE] = {0}; size_t len = 0; @@ -528,6 +650,7 @@ static CAResult_t CAReceiveMessage(CASocketFd_t fd, CATransportFlags_t flags) OIC_LOG_V(ERROR, TAG, "Recvfrom failed %s", strerror(errno)); return CA_STATUS_FAILED; } + OIC_LOG_V(DEBUG, TAG, "recvd %u bytes from recvmsg", recvLen); if (flags & CA_MULTICAST) { @@ -590,6 +713,7 @@ static CAResult_t CAReceiveMessage(CASocketFd_t fd, CATransportFlags_t flags) #endif // !defined(WSA_CMSG_DATA) CASecureEndpoint_t sep = {.endpoint = {.adapter = CA_ADAPTER_IP, .flags = flags}}; +#ifndef __TIZENRT__ if (flags & CA_IPV6) { /** @todo figure out correct usage for ifindex, and sin6_scope_id.*/ @@ -604,6 +728,7 @@ static CAResult_t CAReceiveMessage(CASocketFd_t fd, CATransportFlags_t flags) } } else +#endif { if ((flags & CA_MULTICAST) && pktinfo) { @@ -622,8 +747,8 @@ static CAResult_t CAReceiveMessage(CASocketFd_t fd, CATransportFlags_t flags) if (flags & CA_SECURE) { #ifdef __WITH_DTLS__ - int ret = CAAdapterNetDtlsDecrypt(&sep, (uint8_t *)recvBuffer, recvLen); - OIC_LOG_V(DEBUG, TAG, "CAAdapterNetDtlsDecrypt returns [%d]", ret); + int ret = CAdecryptSsl(&sep, (uint8_t *)recvBuffer, recvLen); + OIC_LOG_V(INFO, TAG, "CAdecryptSsl returns [%d]", ret); #else OIC_LOG(ERROR, TAG, "Encrypted message but no DTLS"); #endif @@ -632,10 +757,12 @@ static CAResult_t CAReceiveMessage(CASocketFd_t fd, CATransportFlags_t flags) { if (g_packetReceivedCallback) { + OIC_LOG(DEBUG, TAG, "call receivedCB"); g_packetReceivedCallback(&sep, recvBuffer, recvLen); } } + OIC_LOG(DEBUG, TAG, "OUT - CAReceiveMessage"); return CA_STATUS_OK; } @@ -760,20 +887,64 @@ static CASocketFd_t CACreateSocket(int family, uint16_t *port, bool isMulticast) if (FD > caglobals.ip.maxfd) \ caglobals.ip.maxfd = FD; #define NEWSOCKET(FAMILY, NAME, MULTICAST) \ - caglobals.ip.NAME.fd = CACreateSocket(FAMILY, &caglobals.ip.NAME.port, MULTICAST); \ if (caglobals.ip.NAME.fd == OC_INVALID_SOCKET) \ { \ - caglobals.ip.NAME.port = 0; \ caglobals.ip.NAME.fd = CACreateSocket(FAMILY, &caglobals.ip.NAME.port, MULTICAST); \ + if (caglobals.ip.NAME.fd == OC_INVALID_SOCKET) \ + { \ + caglobals.ip.NAME.port = 0; \ + caglobals.ip.NAME.fd = CACreateSocket(FAMILY, &caglobals.ip.NAME.port, MULTICAST); \ + } \ + CHECKFD(caglobals.ip.NAME.fd) \ } \ - CHECKFD(caglobals.ip.NAME.fd) + +void CreateMulticastSocket() +{ + OIC_LOG_V(INFO, TAG, "In %s", __func__); + + if (caglobals.ip.ipv6enabled) + { + NEWSOCKET(AF_INET6, u6, false) + NEWSOCKET(AF_INET6, u6s, false) + NEWSOCKET(AF_INET6, m6, true) + NEWSOCKET(AF_INET6, m6s, true) + OIC_LOG_V(INFO, TAG, "IPv6 unicast port: %u", caglobals.ip.u6.port); + } + if (caglobals.ip.ipv4enabled) + { + NEWSOCKET(AF_INET, u4, false) + NEWSOCKET(AF_INET, u4s, false) + NEWSOCKET(AF_INET, m4, true) + NEWSOCKET(AF_INET, m4s, true) + OIC_LOG_V(INFO, TAG, "IPv4 unicast port: %u", caglobals.ip.u4.port); + } + + OIC_LOG_V(INFO, TAG, "Out %s", __func__); +} + +void CloseMulticastSocket() +{ + OIC_LOG_V(INFO, TAG, "In %s", __func__); + + CLOSE_SOCKET(u6); + CLOSE_SOCKET(u6s); + CLOSE_SOCKET(u4); + CLOSE_SOCKET(u4s); + CLOSE_SOCKET(m6); + CLOSE_SOCKET(m6s); + CLOSE_SOCKET(m4); + CLOSE_SOCKET(m4s); + + OIC_LOG_V(INFO, TAG, "Out %s", __func__); +} static void CAInitializeNetlink() { caglobals.ip.netlinkFd = OC_INVALID_SOCKET; #ifdef __linux__ // create NETLINK fd for interface change notifications - struct sockaddr_nl sa = { AF_NETLINK, 0, 0, RTMGRP_LINK }; + struct sockaddr_nl sa = { AF_NETLINK, 0, 0, + RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR }; caglobals.ip.netlinkFd = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC, NETLINK_ROUTE); if (caglobals.ip.netlinkFd == OC_INVALID_SOCKET) @@ -794,6 +965,17 @@ static void CAInitializeNetlink() CHECKFD(caglobals.ip.netlinkFd); } } +#elif defined (__TIZENRT__) // pkmsgq + struct mq_attr lq_attr; + lq_attr.mq_maxmsg = 10; + lq_attr.mq_msgsize = 4; + lq_attr.mq_flags = 0; + g_nwevent_mqfd = mq_open("netlink_evtq", O_RDWR | O_NONBLOCK | O_CREAT, 0666, &lq_attr); + if (g_nwevent_mqfd == (mqd_t) - 1) + { + OIC_LOG_V(ERROR, TAG,"RECV mq_open failed\n"); + return ; + } #endif } @@ -808,10 +990,13 @@ static void CAInitializeFastShutdownMechanism() ret = 0; } #elif defined(HAVE_PIPE2) +#ifndef __TIZENRT__ ret = pipe2(caglobals.ip.shutdownFds, O_CLOEXEC); CHECKFD(caglobals.ip.shutdownFds[0]); CHECKFD(caglobals.ip.shutdownFds[1]); +#endif #else +#ifndef __TIZENRT__ ret = pipe(caglobals.ip.shutdownFds); if (-1 != ret) { @@ -839,6 +1024,7 @@ static void CAInitializeFastShutdownMechanism() CHECKFD(caglobals.ip.shutdownFds[0]); CHECKFD(caglobals.ip.shutdownFds[1]); #endif +#endif if (-1 == ret) { OIC_LOG_V(ERROR, TAG, "fast shutdown mechanism init failed: %s", CAIPS_GET_ERROR); @@ -882,29 +1068,14 @@ CAResult_t CAIPStartServer(const ca_thread_pool_t threadPool) caglobals.ip.ipv4enabled = true; // only needed to run CA tests } - if (caglobals.ip.ipv6enabled) - { - NEWSOCKET(AF_INET6, u6, false) - NEWSOCKET(AF_INET6, u6s, false) - NEWSOCKET(AF_INET6, m6, true) - NEWSOCKET(AF_INET6, m6s, true) - OIC_LOG_V(INFO, TAG, "IPv6 unicast port: %u", caglobals.ip.u6.port); - } - if (caglobals.ip.ipv4enabled) - { - NEWSOCKET(AF_INET, u4, false) - NEWSOCKET(AF_INET, u4s, false) - NEWSOCKET(AF_INET, m4, true) - NEWSOCKET(AF_INET, m4s, true) - OIC_LOG_V(INFO, TAG, "IPv4 unicast port: %u", caglobals.ip.u4.port); - } + CreateMulticastSocket(); - OIC_LOG_V(DEBUG, TAG, + OIC_LOG_V(INFO, 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); - OIC_LOG_V(DEBUG, TAG, + OIC_LOG_V(INFO, TAG, "port summary: u6 port=%d, u6s port=%d, u4 port=%d, u4s port=%d, m6 port=%d," "m6s port=%d, m4 port=%d, m4s port=%d", caglobals.ip.u6.port, caglobals.ip.u6s.port, caglobals.ip.u4.port, @@ -937,13 +1108,19 @@ CAResult_t CAIPStartServer(const ca_thread_pool_t threadPool) } caglobals.ip.terminate = false; - res = ca_thread_pool_add_task(threadPool, CAReceiveHandler, NULL); +#ifndef __TIZENRT__ + res = ca_thread_pool_add_task(threadPool, CAReceiveHandler, NULL, NULL); +#else + res = ca_thread_pool_add_task(threadPool, CAReceiveHandler, NULL, NULL, "IoT_ReceiveHandler", + CONFIG_IOTIVITY_RECEIVEHANDLER_PTHREAD_STACKSIZE); +#endif if (CA_STATUS_OK != res) { OIC_LOG(ERROR, TAG, "thread_pool_add_task failed"); + CAIPStopServer(); return res; } - OIC_LOG(DEBUG, TAG, "CAReceiveHandler thread started successfully."); + OIC_LOG(INFO, TAG, "CAReceiveHandler thread started successfully."); caglobals.ip.started = true; return CA_STATUS_OK; @@ -954,7 +1131,10 @@ void CAIPStopServer() caglobals.ip.started = false; caglobals.ip.terminate = true; + CADeInitializeIPGlobals(); + #if !defined(WSA_WAIT_EVENT_0) +#ifndef __TIZENRT__ if (caglobals.ip.shutdownFds[1] != -1) { close(caglobals.ip.shutdownFds[1]); @@ -964,6 +1144,7 @@ void CAIPStopServer() { // receive thread will stop in SELECT_TIMEOUT seconds. } +#endif #else // receive thread will stop immediately. if (!WSASetEvent(caglobals.ip.shutdownEvent)) @@ -971,29 +1152,8 @@ void CAIPStopServer() OIC_LOG_V(DEBUG, TAG, "set shutdown event failed: %#08X", GetLastError()); } #endif -} -void CAWakeUpForChange() -{ -#if !defined(WSA_WAIT_EVENT_0) - if (caglobals.ip.shutdownFds[1] != -1) - { - 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)); - } - } -#else - if (!WSASetEvent(caglobals.ip.shutdownEvent)) - { - OIC_LOG_V(DEBUG, TAG, "set shutdown event failed: %#08X", GetLastError()); - } -#endif + OIC_LOG(INFO, TAG, "Adapter terminated successfully"); } static void applyMulticastToInterface4(uint32_t ifindex) @@ -1008,7 +1168,7 @@ static void applyMulticastToInterface4(uint32_t ifindex) .imr_address.s_addr = htonl(INADDR_ANY), .imr_ifindex = ifindex }; #else - struct ip_mreq mreq = { .imr_multiaddr = IPv4MulticastAddress, + struct ip_mreq mreq = { .imr_multiaddr.s_addr = IPv4MulticastAddress.s_addr, .imr_interface.s_addr = htonl(ifindex) }; #endif @@ -1040,8 +1200,13 @@ static void applyMulticastToInterface4(uint32_t ifindex) static void applyMulticast6(int fd, struct in6_addr *addr, uint32_t ifindex) { - struct ipv6_mreq mreq = {.ipv6mr_multiaddr = *addr, +#ifndef __TIZENRT__ + struct ipv6_mreq mreq = {.ipv6mr_multiaddr = {{{0}}}, .ipv6mr_interface = ifindex }; + + // VS2013 has problems with struct copies inside struct initializers, so copy separately. + mreq.ipv6mr_multiaddr = *addr; + int ret = setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, OPTVAL_T(&mreq), sizeof (mreq)); if (OC_SOCKET_ERROR == ret) { @@ -1054,6 +1219,7 @@ static void applyMulticast6(int fd, struct in6_addr *addr, uint32_t ifindex) OIC_LOG_V(ERROR, TAG, "IPv6 IPV6_JOIN_GROUP failed: %s", CAIPS_GET_ERROR); } } +#endif } static void applyMulticastToInterface6(uint32_t ifindex) @@ -1064,17 +1230,17 @@ static void applyMulticastToInterface6(uint32_t ifindex) } //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressInt, ifindex); applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressLnk, ifindex); - //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressRlm, ifindex); + applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressRlm, ifindex); //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressAdm, ifindex); - //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressSit, ifindex); + applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressSit, ifindex); //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressOrg, ifindex); //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressGlb, ifindex); //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressInt, ifindex); applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressLnk, ifindex); - //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressRlm, ifindex); + applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressRlm, ifindex); //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressAdm, ifindex); - //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressSit, ifindex); + applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressSit, ifindex); //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressOrg, ifindex); //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressGlb, ifindex); } @@ -1105,12 +1271,12 @@ CAResult_t CAIPStartListenServer() } if (ifitem->family == AF_INET) { - OIC_LOG_V(DEBUG, TAG, "Adding IPv4 interface %i to multicast group", ifitem->index); + OIC_LOG_V(DEBUG, TAG, "Adding IPv4 interface(%d) to multicast group", ifitem->index); applyMulticastToInterface4(ifitem->index); } if (ifitem->family == AF_INET6) { - OIC_LOG_V(DEBUG, TAG, "Adding IPv6 interface %i to multicast group", ifitem->index); + OIC_LOG_V(DEBUG, TAG, "Adding IPv6 interface(%d) to multicast group", ifitem->index); applyMulticastToInterface6(ifitem->index); } } @@ -1160,7 +1326,7 @@ CAResult_t CAIPStopListenServer() return CA_STATUS_OK; } -static void CAProcessNewInterface(CAInterface_t *ifitem) +void CAProcessNewInterface(CAInterface_t *ifitem) { if (!ifitem) { @@ -1170,10 +1336,12 @@ static void CAProcessNewInterface(CAInterface_t *ifitem) if (ifitem->family == AF_INET6) { + OIC_LOG_V(DEBUG, TAG, "Adding a new IPv6 interface(%d) to multicast group", ifitem->index); applyMulticastToInterface6(ifitem->index); } if (ifitem->family == AF_INET) { + OIC_LOG_V(DEBUG, TAG, "Adding a new IPv4 interface(%d) to multicast group", ifitem->index); applyMulticastToInterface4(ifitem->index); } } @@ -1183,11 +1351,6 @@ void CAIPSetPacketReceiveCallback(CAIPPacketReceivedCallback callback) g_packetReceivedCallback = callback; } -void CAIPSetConnectionStateChangeCallback(CAIPConnectionStateChangeCallback callback) -{ - CAIPSetNetworkMonitorCallback(callback); -} - static void sendData(int fd, const CAEndpoint_t *endpoint, const void *data, uint32_t dlen, const char *cast, const char *fam) @@ -1204,8 +1367,6 @@ static void sendData(int fd, const CAEndpoint_t *endpoint, return; } - char *secure = (endpoint->flags & CA_SECURE) ? "secure " : ""; - (void)cast; // eliminates release warning (void)fam; @@ -1222,6 +1383,10 @@ static void sendData(int fd, const CAEndpoint_t *endpoint, { socklen = sizeof(struct sockaddr_in); } + +#ifdef TB_LOG + const char *secure = (endpoint->flags & CA_SECURE) ? "secure " : ""; +#endif #if !defined(_WIN32) ssize_t len = sendto(fd, data, dlen, 0, (struct sockaddr *)&sock, socklen); if (OC_SOCKET_ERROR == len) @@ -1232,10 +1397,14 @@ static void sendData(int fd, const CAEndpoint_t *endpoint, g_ipErrorHandler(endpoint, data, dlen, CA_SEND_FAILED); } OIC_LOG_V(ERROR, TAG, "%s%s %s sendTo failed: %s", secure, cast, fam, strerror(errno)); + CALogSendStateInfo(endpoint->adapter, endpoint->addr, endpoint->port, + len, false, strerror(errno)); } else { OIC_LOG_V(INFO, TAG, "%s%s %s sendTo is successful: %zd bytes", secure, cast, fam, len); + CALogSendStateInfo(endpoint->adapter, endpoint->addr, endpoint->port, + len, true, NULL); } #else int err = 0; @@ -1262,11 +1431,11 @@ static void sendData(int fd, const CAEndpoint_t *endpoint, sent += len; if (sent != len) { - OIC_LOG_V(DEBUG, TAG, "%s%s %s sendTo (Partial Send) is successful: " - "currently sent: %ld bytes, " - "total sent: %ld bytes, " - "remaining: %ld bytes", - secure, cast, fam, len, sent, dlen-sent); + OIC_LOG_V(INFO, TAG, "%s%s %s sendTo (Partial Send) is successful: " + "currently sent: %ld bytes, " + "total sent: %ld bytes, " + "remaining: %ld bytes", + secure, cast, fam, len, sent, dlen-sent); } else { @@ -1315,12 +1484,34 @@ static void sendMulticastData6(const u_arraylist_t *iflist, continue; } + bool isMobile = false; + for (uint32_t j = 0; j < MOBILE_INTERFACES; j++) + { + if (strstr(ifitem->name, mobileinferfaces[j])) + { + isMobile = true; + break; + } + } + if (isMobile) + { + continue; + } + int index = ifitem->index; if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, OPTVAL_T(&index), sizeof (index))) { OIC_LOG_V(ERROR, TAG, "setsockopt6 failed: %s", CAIPS_GET_ERROR); return; } + +#ifndef __TIZENRT__ + // Set multicast packet TTL; default TTL is 1 + if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &multicastTTL, sizeof(multicastTTL))) + { + OIC_LOG_V(ERROR, TAG, "IPV6_MULTICAST_HOPS failed: %s", CAIPS_GET_ERROR); + } +#endif sendData(fd, endpoint, data, datalen, "multicast", "ipv6"); } } @@ -1336,7 +1527,7 @@ static void sendMulticastData4(const u_arraylist_t *iflist, .imr_address.s_addr = htonl(INADDR_ANY), .imr_ifindex = 0}; #else - struct ip_mreq mreq = { .imr_multiaddr = IPv4MulticastAddress, + struct ip_mreq mreq = { .imr_multiaddr.s_addr = IPv4MulticastAddress.s_addr, .imr_interface = {0}}; #endif @@ -1359,6 +1550,21 @@ static void sendMulticastData4(const u_arraylist_t *iflist, { continue; } + + bool isMobile = false; + for (uint32_t j = 0; j < MOBILE_INTERFACES; j++) + { + if (strstr(ifitem->name, mobileinferfaces[j])) + { + isMobile = true; + break; + } + } + if (isMobile) + { + continue; + } + #if defined(USE_IP_MREQN) mreq.imr_ifindex = ifitem->index; #else @@ -1369,6 +1575,12 @@ static void sendMulticastData4(const u_arraylist_t *iflist, OIC_LOG_V(ERROR, TAG, "send IP_MULTICAST_IF failed: %s (using defualt)", CAIPS_GET_ERROR); } + + // Set multicast packet TTL; default TTL is 1 + if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &multicastTTL, sizeof(multicastTTL))) + { + OIC_LOG_V(ERROR, TAG, "IP_MULTICAST_TTL failed: %s", CAIPS_GET_ERROR); + } sendData(fd, endpoint, data, datalen, "multicast", "ipv4"); } } @@ -1514,3 +1726,17 @@ void CAIPSetErrorHandler(CAIPErrorHandleCallback errorHandleCallback) { g_ipErrorHandler = errorHandleCallback; } + +CAResult_t CAIPSetMulticastTTL(size_t ttl) +{ + multicastTTL = ttl; + return CA_STATUS_OK; +} + +CAResult_t CAIPGetMulticastTTL(size_t *ttl) +{ + VERIFY_NON_NULL(ttl, TAG, "ttl is NULL"); + + *ttl = multicastTTL; + return CA_STATUS_OK; +}