1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
20 #include "caipinterface.h"
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <arpa/inet.h>
25 #include <netinet/in.h>
28 #include "caadapterutils.h"
32 * @brief Logging tag for module name
34 #define IP_CLIENT_TAG "IP_CLIENT"
35 #define OC_MULTICAST_IP "224.0.1.187"
37 static uint32_t CASendData(const CAEndpoint_t *endpoint, const void *data,
38 uint32_t dataLength, int sockfd)
40 OIC_LOG(DEBUG, IP_CLIENT_TAG, "IN");
42 VERIFY_NON_NULL_RET(endpoint, IP_CLIENT_TAG, "IP address is NULL", 0);
43 VERIFY_NON_NULL_RET(data, IP_CLIENT_TAG, "data is NULL", 0);
47 OIC_LOG(ERROR, IP_CLIENT_TAG, "Data length is 0!");
53 OIC_LOG(ERROR, IP_CLIENT_TAG, "Unicast Server is not running!");
57 struct sockaddr_in destAddr = { 0 };
58 destAddr.sin_family = AF_INET;
59 destAddr.sin_port = htons(endpoint->port);
61 int ret = inet_pton(AF_INET, endpoint->addr, &(destAddr.sin_addr));
64 OIC_LOG(ERROR, IP_CLIENT_TAG, "inet_pton failed!");
68 ssize_t sendDataLength = sendto(sockfd, data, dataLength, 0, (struct sockaddr *) &destAddr,
70 if (-1 == sendDataLength)
72 OIC_LOG_V(ERROR, IP_CLIENT_TAG, "sendto failed, Error code: %s",
77 OIC_LOG_V(INFO, IP_CLIENT_TAG, "Sending data is successful, sent bytes[%d] to ip[%s:%d]",
78 sendDataLength, endpoint->addr, endpoint->port);
79 OIC_LOG(DEBUG, IP_CLIENT_TAG, "OUT");
80 return sendDataLength;
83 uint32_t CAIPSendData(const CAEndpoint_t *endpoint, const void *data,
84 uint32_t dataLength, bool isMulticast)
86 u_arraylist_t *tempServerInfoList = u_arraylist_create();
87 if (!tempServerInfoList)
89 OIC_LOG(ERROR, IP_CLIENT_TAG, "u_arraylist_create failed");
93 CAResult_t res = CAGetIPServerInfoList(&tempServerInfoList);
94 if (CA_STATUS_OK != res)
96 OIC_LOG(ERROR, IP_CLIENT_TAG, "CAGetIPServerInfoList failed");
97 CAClearServerInfoList(tempServerInfoList);
102 if (isMulticast || strcmp(endpoint->addr, OC_MULTICAST_IP) == 0)
104 uint32_t listIndex = 0;
105 uint32_t listLength = u_arraylist_length(tempServerInfoList);
106 for (listIndex = 0; listIndex < listLength; listIndex++)
108 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(tempServerInfoList,
110 if (!info || info->isMulticastServer || (info->endpoint.flags & CA_SECURE))
115 if (info->socketFd < 0)
117 OIC_LOG(ERROR, IP_CLIENT_TAG, "Invalid Socket Fd");
121 OIC_LOG_V(DEBUG, IP_CLIENT_TAG,
122 "CA IP Multicast SendData with src ip %s port %d sockFd %d",
123 info->endpoint.addr, info->endpoint.port, info->socketFd);
124 len = CASendData(endpoint, data, dataLength, info->socketFd);
129 int sockFd = CAGetSocketFdForUnicastServer(tempServerInfoList, isMulticast, endpoint);
132 OIC_LOG(ERROR, IP_CLIENT_TAG, "Invalid Socket Fd");
133 CAClearServerInfoList(tempServerInfoList);
137 OIC_LOG_V(DEBUG, IP_CLIENT_TAG, "IP unicast SendData sockFd %d", sockFd);
139 len = CASendData(endpoint, data, dataLength, sockFd);
142 CAClearServerInfoList(tempServerInfoList);