#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
+#if defined(__TIZEN__)
+#include <ifaddrs.h>
+#endif
#ifdef __TIZENRT__
#include <tinyara/config.h>
#include <poll.h>
#if defined(__TIZEN__)
static char g_cloudproxyUri[CA_MAX_URI_LENGTH];
+static char g_interfaceName[CA_MAX_INTERFACE_NAME_LEN];
+
CAResult_t CASetCloudAddressForProxy(const char *uri)
{
if (uri == NULL)
return NULL;
return g_cloudproxyUri;
}
+
+CAResult_t CASetTCPServerSocketBindIP(const char* ifname)
+{
+ CAResult_t res = CA_STATUS_OK;
+#ifdef TCP_ADAPTER
+ VERIFY_NON_NULL_RET(ifname, TAG, "Interface name is NULL", CA_STATUS_INVALID_PARAM);
+
+ OIC_LOG(DEBUG, TAG, "Setting Socket Bind");
+ caglobals.tcp.bindenabled = true;
+ OICStrcpy(g_interfaceName, sizeof (g_interfaceName), ifname);
+#else
+ OIC_LOG(DEBUG, TAG, "Not supported!");
+ res = CA_NOT_SUPPORTED;
+#endif
+ return res;
+}
#endif
#define MAX_TCP_SOCK_COUNT 4
}
#if defined(__TIZEN__)
+static char * CATCPGetInterfaceAddr()
+{
+ struct ifaddrs *ifaddr, *ifa;
+ int family, s, n;
+ char *host = (uint8_t *)OICMalloc(NI_MAXHOST);
+
+ if (getifaddrs(&ifaddr) == -1)
+ {
+ OIC_LOG_V(ERROR, TAG, "getifaddrs failed: errno %s", strerror(errno));
+ OICFree(host);
+ return NULL;
+ }
+
+ for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++)
+ {
+ if (ifa->ifa_addr == NULL)
+ continue;
+
+ family = ifa->ifa_addr->sa_family;
+ OIC_LOG_V(DEBUG, TAG,"Interface name: %s",ifa->ifa_name);
+
+ if ((strncmp(g_interfaceName,ifa->ifa_name,strlen(ifa->ifa_name))==0)
+ && (family == AF_INET || family == AF_INET6))
+ {
+ s = getnameinfo(ifa->ifa_addr, (family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6),
+ host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
+ if (s != 0)
+ {
+ OIC_LOG_V(ERROR, TAG, "getnameinfo failed: errno %s", strerror(errno));
+ freeifaddrs(ifaddr);
+ OICFree(host);
+ return NULL;
+ }
+
+ OIC_LOG_V(DEBUG, TAG, "Interface address: %s", host);
+ freeifaddrs(ifaddr);
+ return host;
+ }
+ }
+
+ freeifaddrs(ifaddr);
+ OICFree(host);
+ OIC_LOG(ERROR, TAG, "Interface address not found");
+ return NULL;
+}
+#endif
+
+#if defined(__TIZEN__)
static int CAGetHTTPStatusCode(char * response) {
char *resp, *code_plus, *ptrSave;
int ret = -1;
socklen = sizeof(struct sockaddr_in);
}
+#if defined(__TIZEN__)
+ if (caglobals.tcp.bindenabled)
+ {
+ char *host_address = CATCPGetInterfaceAddr();
+ if (host_address)
+ {
+ if (sa.ss_family == AF_INET)
+ {
+ struct sockaddr_in client;
+ memset(&client, 0x00, sizeof(client));
+ inet_pton(AF_INET, host_address, &(client.sin_addr.s_addr));
+ if (-1 == bind(fd, (struct sockaddr *)&client, socklen))
+ {
+ OIC_LOG_V(ERROR, TAG, "bind socket failed: %s", strerror(errno));
+ OICFree(host_address);
+ return CA_SOCKET_OPERATION_FAILED;
+ }
+ }
+ else
+ {
+ struct sockaddr_in6 client6;
+ memset(&client6, 0x00, sizeof(client6));
+ client6.sin6_family = AF_INET6;
+ inet_pton(AF_INET6, host_address, &client6.sin6_addr.s6_addr);
+ if (-1 == bind(fd, (struct sockaddr *)&client6, socklen))
+ {
+ OIC_LOG_V(ERROR, TAG, "bind socket failed: %s", strerror(errno));
+ OICFree(host_address);
+ return CA_SOCKET_OPERATION_FAILED;
+ }
+ }
+ OICFree(host_address);
+ }
+ }
+#endif
+
// #4. connect to remote server device.
if (connect(fd, (struct sockaddr *)&sa, socklen) < 0)
{