83558865fe464b62b61add57f368769a215993dd
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / adapter_util / caadapterutils.c
1 /******************************************************************
2  *
3  * Copyright 2014 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 #include "iotivity_config.h"
22 #include "caadapterutils.h"
23
24 #include <string.h>
25 #include <ctype.h>
26 #include "oic_string.h"
27 #include "oic_malloc.h"
28 #include <errno.h>
29
30 #ifdef HAVE_WS2TCPIP_H
31 #include <ws2tcpip.h>
32 #endif
33 #ifdef HAVE_SYS_SOCKET_H
34 #include <sys/socket.h>
35 #endif
36 #ifdef HAVE_NETINET_IN_H
37 #include <netinet/in.h>
38 #endif
39 #if defined(HAVE_WINSOCK2_H) && defined(HAVE_WS2TCPIP_H)
40 #include <winsock2.h>
41 #include <ws2tcpip.h>
42 #endif
43 #ifdef HAVE_NETDB_H
44 #include <netdb.h>
45 #endif
46
47 #ifdef __ANDROID__
48 #include <jni.h>
49 #endif
50
51 #define CA_ADAPTER_UTILS_TAG "OIC_CA_ADAP_UTILS"
52
53 #ifdef __ANDROID__
54 /**
55  * @var g_jvm
56  * @brief pointer to store JavaVM
57  */
58 static JavaVM *g_jvm = NULL;
59
60 /**
61  * @var gContext
62  * @brief pointer to store context for android callback interface
63  */
64 static jobject g_Context = NULL;
65 static jobject g_Activity = NULL;
66 #endif
67
68 #ifdef WITH_ARDUINO
69 CAResult_t CAParseIPv4AddressInternal(const char *ipAddrStr, uint8_t *ipAddr,
70                                    size_t ipAddrLen, uint16_t *port)
71 {
72     if (!ipAddr || !isdigit(ipAddrStr[0]) || !port)
73     {
74         OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "invalid param");
75         return CA_STATUS_INVALID_PARAM;
76     }
77
78     size_t index = 0;
79     uint8_t dotCount = 0;
80
81     ipAddr[index] = 0;
82     *port = 0;
83     while (*ipAddrStr)
84     {
85         if (isdigit(*ipAddrStr))
86         {
87             if(index >= ipAddrLen)
88             {
89                 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "invalid param");
90                 return CA_STATUS_INVALID_PARAM;
91             }
92             ipAddr[index] *= 10;
93             ipAddr[index] += *ipAddrStr - '0';
94         }
95         else if (*ipAddrStr == '.')
96         {
97             index++;
98             dotCount++;
99             ipAddr[index] = 0;
100         }
101         else
102         {
103             break;
104         }
105         ipAddrStr++;
106     }
107
108     if (*ipAddrStr == ':')
109     {
110         ipAddrStr++;
111         while (*ipAddrStr)
112         {
113             if (isdigit(*ipAddrStr))
114             {
115                 *port *= 10;
116                 *port += *ipAddrStr - '0';
117             }
118             else
119             {
120                 break;
121             }
122             ipAddrStr++;
123         }
124     }
125
126     if (dotCount == 3)
127     {
128         return CA_STATUS_OK;
129     }
130     return CA_STATUS_FAILED;
131 }
132
133 #else // not with_arduino
134 /*
135  * These two conversion functions return void because errors can't happen
136  * (because of NI_NUMERIC), and there's nothing to do if they do happen.
137  */
138 void CAConvertAddrToName(const struct sockaddr_storage *sockAddr, socklen_t sockAddrLen,
139                          char *host, uint16_t *port)
140 {
141     VERIFY_NON_NULL_VOID(sockAddr, CA_ADAPTER_UTILS_TAG, "sockAddr is null");
142     VERIFY_NON_NULL_VOID(host, CA_ADAPTER_UTILS_TAG, "host is null");
143     VERIFY_NON_NULL_VOID(port, CA_ADAPTER_UTILS_TAG, "port is null");
144
145     int r = getnameinfo((struct sockaddr *)sockAddr,
146                         sockAddrLen,
147                         host, MAX_ADDR_STR_SIZE_CA,
148                         NULL, 0,
149                         NI_NUMERICHOST|NI_NUMERICSERV);
150     if (r)
151     {
152 #if defined(EAI_SYSTEM)
153         if (EAI_SYSTEM == r)
154         {
155             OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
156                             "getnameinfo failed: errno %s", strerror(errno));
157         }
158         else
159         {
160             OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
161                             "getnameinfo failed: %s", gai_strerror(r));
162         }
163 #elif defined(_WIN32)
164         OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
165                             "getnameinfo failed: errno %i", WSAGetLastError());
166 #else
167         OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
168                             "getnameinfo failed: %s", gai_strerror(r));
169 #endif
170         return;
171     }
172     *port = ntohs(((struct sockaddr_in *)sockAddr)->sin_port); // IPv4 and IPv6
173 }
174
175 void CAConvertNameToAddr(const char *host, uint16_t port, struct sockaddr_storage *sockaddr)
176 {
177     VERIFY_NON_NULL_VOID(host, CA_ADAPTER_UTILS_TAG, "host is null");
178     VERIFY_NON_NULL_VOID(sockaddr, CA_ADAPTER_UTILS_TAG, "sockaddr is null");
179
180     struct addrinfo *addrs = NULL;
181     struct addrinfo hints = { .ai_family = AF_UNSPEC,
182                               .ai_socktype = SOCK_DGRAM,
183                               .ai_flags = AI_NUMERICHOST };
184
185     int r = getaddrinfo(host, NULL, &hints, &addrs);
186     if (r)
187     {
188 #if defined(EAI_SYSTEM)
189         if (EAI_SYSTEM == r)
190         {
191             OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
192                             "getaddrinfo failed: errno %s", strerror(errno));
193         }
194         else
195         {
196             OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
197                             "getaddrinfo failed: %s", gai_strerror(r));
198         }
199 #elif defined(_WIN32)
200         OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
201                             "getaddrinfo failed: errno %i", WSAGetLastError());
202 #else
203         OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
204                             "getaddrinfo failed: %s", gai_strerror(r));
205 #endif
206         return;
207     }
208     // assumption: in this case, getaddrinfo will only return one addrinfo
209     // or first is the one we want.
210     if (addrs[0].ai_family == AF_INET6)
211     {
212         memcpy(sockaddr, addrs[0].ai_addr, sizeof (struct sockaddr_in6));
213         ((struct sockaddr_in6 *)sockaddr)->sin6_port = htons(port);
214     }
215     else
216     {
217         memcpy(sockaddr, addrs[0].ai_addr, sizeof (struct sockaddr_in));
218         ((struct sockaddr_in *)sockaddr)->sin_port = htons(port);
219     }
220     freeaddrinfo(addrs);
221 }
222 #endif // WITH_ARDUINO
223
224 #ifdef __ANDROID__
225 void CANativeJNISetContext(JNIEnv *env, jobject context)
226 {
227     OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeJNISetContext");
228
229     if (!context)
230     {
231         OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "context is null");
232         return;
233     }
234
235     if (!g_Context)
236     {
237         g_Context = (*env)->NewGlobalRef(env, context);
238     }
239     else
240     {
241         OIC_LOG(INFO, CA_ADAPTER_UTILS_TAG, "context is already set");
242     }
243 }
244
245 void CANativeJNISetJavaVM(JavaVM *jvm)
246 {
247     OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeJNISetJavaVM");
248     g_jvm = jvm;
249 }
250
251 jobject CANativeJNIGetContext()
252 {
253     return g_Context;
254 }
255
256 JavaVM *CANativeJNIGetJavaVM()
257 {
258     return g_jvm;
259 }
260
261 void CANativeSetActivity(JNIEnv *env, jobject activity)
262 {
263     OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeSetActivity");
264
265     if (!activity)
266     {
267         OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "activity is null");
268         return;
269     }
270
271     if (!g_Activity)
272     {
273         g_Activity = (*env)->NewGlobalRef(env, activity);
274     }
275     else
276     {
277         OIC_LOG(INFO, CA_ADAPTER_UTILS_TAG, "activity is already set");
278     }
279 }
280
281 jobject *CANativeGetActivity()
282 {
283     return g_Activity;
284 }
285
286 jmethodID CAGetJNIMethodID(JNIEnv *env, const char* className,
287                            const char* methodName,
288                            const char* methodFormat)
289 {
290     VERIFY_NON_NULL_RET(env, CA_ADAPTER_UTILS_TAG, "env", NULL);
291     VERIFY_NON_NULL_RET(className, CA_ADAPTER_UTILS_TAG, "className", NULL);
292     VERIFY_NON_NULL_RET(methodName, CA_ADAPTER_UTILS_TAG, "methodName", NULL);
293     VERIFY_NON_NULL_RET(methodFormat, CA_ADAPTER_UTILS_TAG, "methodFormat", NULL);
294
295     jclass jni_cid = (*env)->FindClass(env, className);
296     if (!jni_cid)
297     {
298         OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "jni_cid [%s] is null", className);
299         return NULL;
300     }
301
302     jmethodID jni_midID = (*env)->GetMethodID(env, jni_cid, methodName, methodFormat);
303     if (!jni_midID)
304     {
305         OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "jni_midID [%s] is null", methodName);
306         (*env)->DeleteLocalRef(env, jni_cid);
307         return NULL;
308     }
309
310     (*env)->DeleteLocalRef(env, jni_cid);
311     return jni_midID;
312 }
313
314 void CADeleteGlobalReferences(JNIEnv *env)
315 {
316     if (g_Context)
317     {
318         (*env)->DeleteGlobalRef(env, g_Context);
319         g_Context = NULL;
320     }
321
322     if (g_Activity)
323     {
324         (*env)->DeleteGlobalRef(env, g_Activity);
325         g_Activity = NULL;
326     }
327 }
328 #endif