Fix for SVACE defects
[platform/upstream/iotivity.git] / service / notification / src / common / NSUtil.c
1 //******************************************************************
2 //
3 // Copyright 2016 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 "NSUtil.h"
22
23 OCEntityHandlerRequest *NSCopyOCEntityHandlerRequest(OCEntityHandlerRequest *entityHandlerRequest)
24 {
25     NS_LOG(DEBUG, "NSCopyOCEntityHandlerRequest - IN");
26
27     OCEntityHandlerRequest *copyOfRequest =
28             (OCEntityHandlerRequest *)OICMalloc(sizeof(OCEntityHandlerRequest));
29
30     if (copyOfRequest)
31     {
32         memcpy(copyOfRequest, entityHandlerRequest, sizeof(OCEntityHandlerRequest));
33         copyOfRequest->payload = NULL;
34         copyOfRequest->query = NULL;
35         copyOfRequest->numRcvdVendorSpecificHeaderOptions = 0;
36         copyOfRequest->rcvdVendorSpecificHeaderOptions = NULL;
37
38         if (entityHandlerRequest->query)
39         {
40             copyOfRequest->query = OICStrdup(entityHandlerRequest->query);
41             if (!copyOfRequest->query)
42             {
43                 NS_LOG(ERROR, "Copy failed due to allocation failure");
44                 OICFree(copyOfRequest);
45                 return NULL;
46             }
47         }
48
49         if (entityHandlerRequest->payload)
50         {
51             copyOfRequest->payload = (OCPayload *)
52                     (OCRepPayloadClone ((OCRepPayload*) entityHandlerRequest->payload));
53         }
54     }
55
56     if (copyOfRequest)
57     {
58         NS_LOG(DEBUG, "Copied client request");
59     }
60     else
61     {
62         NS_LOG(DEBUG, "Error copying client request");
63     }
64
65     NS_LOG(DEBUG, "NSCopyOCEntityHandlerRequest - OUT");
66     return copyOfRequest;
67 }
68
69 NSResult NSFreeOCEntityHandlerRequest(OCEntityHandlerRequest * entityHandlerRequest)
70 {
71     NS_LOG(DEBUG, "NSFreeOCEntityHandlerRequest - IN");
72
73     OICFree(entityHandlerRequest->query);
74     OCPayloadDestroy(entityHandlerRequest->payload);
75     OICFree(entityHandlerRequest);
76
77     NS_LOG(DEBUG, "NSFreeOCEntityHandlerRequest - OUT");
78
79     return NS_OK;
80 }
81
82 NSResult NSFreeMessage(NSMessage * obj)
83 {
84     if (!obj)
85     {
86         return NS_ERROR;
87     }
88
89     obj->messageId = 0;
90     (obj->providerId)[0] = '\0';
91
92     NSFreeMalloc(&(obj->dateTime));
93     obj->ttl = 0;
94     NSFreeMalloc(&(obj->title));
95     NSFreeMalloc(&(obj->contentText));
96     NSFreeMalloc(&(obj->sourceName));
97     NSFreeMalloc(&(obj->topic));
98     NSFreeMediaContents(obj->mediaContents);
99     OCRepPayloadDestroy(obj->extraInfo);
100     OICFree(obj);
101
102     return NS_OK;
103 }
104
105 NSMessage * NSDuplicateMessage(NSMessage * copyMsg)
106 {
107     NSMessage * newMsg = NULL;
108
109     if (copyMsg == NULL)
110     {
111         NS_LOG(ERROR, "Copy Msg is NULL");
112         return NULL;
113     }
114
115     newMsg = NSInitializeMessage();
116     NS_VERIFY_NOT_NULL(newMsg, NULL);
117
118     newMsg->messageId = copyMsg->messageId;
119     OICStrcpy(newMsg->providerId, UUID_STRING_SIZE, copyMsg->providerId);
120
121     if (copyMsg->dateTime)
122     {
123         newMsg->dateTime = OICStrdup(copyMsg->dateTime);
124     }
125
126     newMsg->ttl = copyMsg->ttl;
127
128     if (copyMsg->title)
129     {
130         newMsg->title = OICStrdup(copyMsg->title);
131     }
132
133     if (copyMsg->contentText)
134     {
135         newMsg->contentText = OICStrdup(copyMsg->contentText);
136     }
137
138     if (copyMsg->sourceName)
139     {
140        newMsg->sourceName = OICStrdup(copyMsg->sourceName);
141     }
142
143     if (copyMsg->mediaContents)
144     {
145        newMsg->mediaContents = NSDuplicateMediaContents(copyMsg->mediaContents);
146     }
147
148     if (copyMsg->topic)
149     {
150         newMsg->topic = OICStrdup(copyMsg->topic);
151     }
152
153     if (copyMsg->extraInfo)
154     {
155         newMsg->extraInfo = OCRepPayloadClone(copyMsg->extraInfo);
156     }
157
158     return newMsg;
159 }
160
161 NSResult NSFreeSync(NSSyncInfo * obj)
162 {
163     if (!obj)
164     {
165         return NS_ERROR;
166     }
167
168     OICFree(obj);
169
170     return NS_OK;
171 }
172
173 NSSyncInfo* NSDuplicateSync(NSSyncInfo * copyMsg)
174 {
175     NSSyncInfo * newMsg = NULL;
176
177     if (!copyMsg)
178     {
179         NS_LOG(ERROR, "Copy Msg is NULL");
180         return NULL;
181     }
182
183     newMsg = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
184
185     if (!newMsg)
186     {
187         NS_LOG(ERROR, "newMsg is NULL");
188         return NULL;
189     }
190
191     newMsg->messageId = copyMsg->messageId;
192     OICStrcpy(newMsg->providerId, UUID_STRING_SIZE, copyMsg->providerId);
193     newMsg->state = copyMsg->state;
194
195     return newMsg;
196 }
197
198 NSResult NSFreeConsumer(NSConsumer * obj)
199 {
200     if (!obj)
201     {
202         return NS_ERROR;
203     }
204
205     (obj->consumerId)[0] = '\0';
206
207     OICFree(obj);
208     obj = NULL;
209
210     return NS_OK;
211 }
212
213 NSConsumer* NSDuplicateConsumer(NSConsumer * copyMsg)
214 {
215     NSConsumer * newMsg = NULL;
216
217     if (copyMsg == NULL)
218     {
219         NS_LOG(ERROR, "Copy Msg is NULL");
220         return NULL;
221     }
222
223     newMsg = (NSConsumer *)OICMalloc(sizeof(NSConsumer));
224
225     if (!newMsg)
226     {
227         NS_LOG(ERROR, "newMsg is NULL");
228         return NULL;
229     }
230
231     (newMsg->consumerId)[0] = '\0';
232
233     OICStrcpy(newMsg->consumerId, UUID_STRING_SIZE, copyMsg->consumerId);
234
235     return newMsg;
236 }
237
238 void NSDuplicateSetPropertyString(OCRepPayload** msgPayload, const char * name,
239         const char * copyString)
240 {
241     if (copyString)
242     {
243         OCRepPayloadSetPropString(*msgPayload, name, copyString);
244     }
245 }
246
247 void NSDuplicateSetPropertyInt(OCRepPayload** msgPayload, const char * name,
248         int64_t value)
249 {
250     if (value)
251     {
252         OCRepPayloadSetPropInt(*msgPayload, name, value);
253     }
254 }
255
256 NSSyncInfo * NSGetSyncInfo(OCPayload * payload)
257 {
258     NS_LOG(DEBUG, "NSGetSyncInfo - IN");
259     char * providerId = NULL;
260     int64_t state = 0;
261
262     if (!payload)
263     {
264         return NULL;
265     }
266
267     NSSyncInfo * retSync = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
268
269     if (!retSync)
270     {
271         return NULL;
272     }
273
274     retSync->messageId = 0;
275     retSync->state = NS_SYNC_READ;
276     OCRepPayload * repPayload = (OCRepPayload *)payload;
277
278     if (!OCRepPayloadGetPropInt(repPayload, NS_ATTRIBUTE_MESSAGE_ID,
279             (int64_t *)&retSync->messageId))
280     {
281         OICFree(retSync);
282         return NULL;
283     }
284
285     if (!OCRepPayloadGetPropString(repPayload, NS_ATTRIBUTE_PROVIDER_ID, &providerId))
286     {
287         OICFree(retSync);
288         return NULL;
289     }
290
291     if (!OCRepPayloadGetPropInt(repPayload, NS_ATTRIBUTE_STATE, &state))
292     {
293         OICFree(retSync);
294         OICFree(providerId);
295         return NULL;
296     }
297
298     retSync->state = (NSSyncType) state;
299     OICStrcpy(retSync->providerId, UUID_STRING_SIZE, providerId);
300     OICFree(providerId);
301
302     NS_LOG_V(INFO_PRIVATE, "Provider ID : %s", retSync->providerId);
303     NS_LOG_V(DEBUG, "Sync ID : %lld", (long long int)retSync->messageId);
304     NS_LOG_V(DEBUG, "Sync State : %d", (int) retSync->state);
305
306     NS_LOG(DEBUG, "NSGetSyncInfo - OUT");
307
308     return retSync;
309 }
310
311 NSResult NSGenerateUUIDStr(char uuidStr[UUID_STRING_SIZE])
312 {
313     uint8_t uuid[UUID_SIZE] = { 0, };
314
315     if (RAND_UUID_OK == OCGenerateUuid(uuid))
316     {
317         if (RAND_UUID_OK == OCConvertUuidToString(uuid, uuidStr))
318         {
319             return NS_OK;
320         }
321     }
322     return NS_ERROR;
323 }
324
325 char * NSGetValueFromQuery(char *query, char * compareKey)
326 {
327     char *key = NULL;
328     char *value = NULL;
329     char *restOfQuery = NULL;
330     int numKeyValuePairsParsed = 0;
331
332     NS_LOG_V(INFO, "NS Query Params = %s", query);
333
334     if (!query || query[0] == '\0' || !strlen(query))
335     {
336         NS_LOG(ERROR, "query is null or \\0 or size is 0");
337         return NULL;
338     }
339
340     char *keyValuePair = strtok_r (query, NS_QUERY_SEPARATOR, &restOfQuery);
341
342     while(keyValuePair)
343     {
344         if (numKeyValuePairsParsed >= 2)
345         {
346             NS_LOG(ERROR, "More than 2 queries params in URI.");
347             return NULL;
348         }
349
350         key = strtok_r(keyValuePair, NS_KEY_VALUE_DELIMITER, &value);
351
352         if (!key || !value)
353         {
354             NS_LOG(ERROR, "More than 2 queries params in URI.");
355             return NULL;
356         }
357
358         if (strcmp(key, compareKey) == 0)
359         {
360             NS_LOG_V(DEBUG, "found Key : [%s] - Value : [%s] = ", key, value);
361             return value;
362         }
363
364         ++numKeyValuePairsParsed;
365
366         keyValuePair = strtok_r(NULL, NS_QUERY_SEPARATOR, &restOfQuery);
367     }
368
369     return NULL;
370 }
371
372 NSResult NSFreeMalloc(char ** obj)
373 {
374     if (*obj)
375     {
376         OICFree(*obj);
377         *obj = NULL;
378         return NS_OK;
379     }
380
381     return NS_FAIL;
382 }
383
384 NSMediaContents * NSDuplicateMediaContents(NSMediaContents * copyObj)
385 {
386     if (!copyObj)
387     {
388         return NULL;
389     }
390
391     NSMediaContents * newObj = (NSMediaContents *)OICMalloc(sizeof(NSMediaContents));
392
393     if (!newObj)
394     {
395         NS_LOG(ERROR, "contents newObj is NULL");
396         return NULL;
397     }
398
399     if (copyObj->iconImage)
400     {
401         newObj->iconImage = OICStrdup(copyObj->iconImage);
402     }
403
404     return newObj;
405 }
406
407 NSResult NSFreeMediaContents(NSMediaContents * obj)
408 {
409     if (!obj)
410     {
411         return NS_FAIL;
412     }
413
414     NSFreeMalloc(&(obj->iconImage));
415     OICFree(obj);
416
417     return NS_OK;
418 }
419
420 NSMessage * NSInitializeMessage()
421 {
422     NSMessage * msg = (NSMessage *)OICMalloc(sizeof(NSMessage));
423
424     if (!msg)
425     {
426         NS_LOG(ERROR, "Msg is NULL");
427         return NULL;
428     }
429
430     msg->messageId = OICGetCurrentTime(TIME_IN_MS);
431     msg->messageId = msg->messageId & 0x000000007FFFFFFF;
432     (msg->providerId)[0] = '\0';
433     msg->type = 0;
434     msg->dateTime = NULL;
435     msg->ttl = 0;
436     msg->title = NULL;
437     msg->contentText = NULL;
438     msg->sourceName = NULL;
439     msg->mediaContents = NULL;
440     msg->topic = NULL;
441     msg->extraInfo = NULL;
442
443     return msg;
444 }
445
446 OCRepPayloadValue* NSPayloadFindValue(const OCRepPayload* payload, const char* name)
447 {
448     if (!payload || !name)
449     {
450         return NULL;
451     }
452
453     OCRepPayloadValue* val = payload->values;
454
455     while (val)
456     {
457         if (0 == strcmp(val->name, name))
458         {
459             return val;
460         }
461         val = val->next;
462     }
463
464     return NULL;
465 }
466
467 NSTopicList * NSInitializeTopicList()
468 {
469     NSTopicList * topicList = (NSTopicList *)OICMalloc(sizeof(NSTopicList));
470
471     if (!topicList)
472     {
473         NS_LOG(ERROR, "topicList is NULL");
474         return NULL;
475     }
476
477     (topicList->consumerId)[0] = '\0';
478     topicList->head = NULL;
479     topicList->tail = NULL;
480
481     return topicList;
482 }
483
484 OCDevAddr * NSChangeAddress(const char * inputaddress)
485 {
486     NS_VERIFY_NOT_NULL(inputaddress, NULL);
487
488     char * address = (char *)inputaddress;
489     char * schema = strstr(inputaddress, "//");
490     if (schema)
491     {
492         address = schema + 2;
493     }
494     size_t prefixLen = schema - inputaddress;
495     if (prefixLen <= 0)
496     {
497         NS_LOG(ERROR, "Invalid Input address.");
498         return NULL;
499     }
500
501     OCTransportFlags flags = OC_DEFAULT_FLAGS;
502     OCTransportAdapter adapter = OC_ADAPTER_IP;
503     if (strstr(inputaddress, "coap+tcp://"))
504     {
505         NS_LOG(DEBUG, "address : TCP");
506         adapter = OC_ADAPTER_TCP;
507     }
508     else if (strstr(inputaddress, "coaps://"))
509     {
510         NS_LOG(DEBUG, "address : UDP + SECURED");
511         flags |= OC_FLAG_SECURE;
512     }
513     else if (strstr(inputaddress, "coaps+tcp://"))
514     {
515         NS_LOG(DEBUG, "address : TCP + SECURED");
516         flags |= OC_FLAG_SECURE;
517         adapter = OC_ADAPTER_TCP;
518     }
519     else if (strstr(inputaddress, "coap://"))
520     {
521         NS_LOG(DEBUG, "address : UDP");
522     }
523     else
524     {
525         NS_LOG(ERROR, "Invalid CoAP Schema.");
526         return NULL;
527     }
528
529     OCDevAddr * retAddr = NULL;
530     retAddr = (OCDevAddr *) OICMalloc(sizeof(OCDevAddr));
531     NS_VERIFY_NOT_NULL(retAddr, NULL);
532
533     char * start = address;
534     char * end = address;
535     if (address[0] == '[')
536     {
537         flags |= OC_IP_USE_V6;
538         end = strchr(++address, ']');
539         if (!end || end <= start)
540         {
541             NS_LOG(ERROR, "Invalid Input Address - IPv6.");
542             NSOICFree(retAddr);
543             return NULL;
544         }
545         memset(retAddr->addr, 0, (size_t)MAX_ADDR_STR_SIZE);
546         OICStrcpy(retAddr->addr, (size_t)(end-start), address);
547     }
548     else
549     {
550         flags |= OC_IP_USE_V4;
551         end = strchr(address, ':');
552         if (!end || end <= start)
553         {
554             NS_LOG(ERROR, "Invalid Input Address - IPv4.");
555             NSOICFree(retAddr);
556             return NULL;
557         }
558         char * end2 = strchr(end + 1, ':');
559         if (end2)
560         {
561             NS_LOG(ERROR, "Invalid Input Address - IPv4.");
562             NSOICFree(retAddr);
563             return NULL;
564         }
565         memset(retAddr->addr, 0, (size_t)MAX_ADDR_STR_SIZE);
566         OICStrcpy(retAddr->addr, (size_t)(end-start)+1, address);
567     }
568
569     retAddr->adapter = adapter;
570     retAddr->flags = flags;
571
572     address = end + 1;
573     int tmp = 0;
574     if (flags & OC_IP_USE_V6)
575     {
576         address++;
577     }
578     uint16_t port = address[tmp++] - '0';
579
580     while(true)
581     {
582         if (address[tmp] == '\0' || address[tmp] > '9' || address[tmp] < '0')
583         {
584             break;
585         }
586         if (tmp >= 5 || (port >= 6553 && (address[tmp] -'0') >= 6))
587         {
588             NS_LOG_V(ERROR, "Invalid Input Address - Port. %d", tmp+1);
589             NSOICFree(retAddr);
590             return NULL;
591         }
592         port *= 10;
593         port += address[tmp++] - '0';
594     }
595
596     retAddr->port = port;
597
598     NS_LOG(DEBUG, "Change Address for TCP request");
599     NS_LOG_V(INFO_PRIVATE, "Origin : %s", inputaddress);
600     NS_LOG_V(INFO_PRIVATE, "Changed Addr : %s", retAddr->addr);
601     NS_LOG_V(INFO_PRIVATE, "Changed Port : %d", retAddr->port);
602
603     return retAddr;
604 }
605
606 bool NSOCResultToSuccess(OCStackResult ret)
607 {
608     switch (ret)
609     {
610         case OC_STACK_OK:
611         case OC_STACK_RESOURCE_CREATED:
612         case OC_STACK_RESOURCE_DELETED:
613 #ifdef WITH_PRESENCE
614         case OC_STACK_PRESENCE_STOPPED:
615 #endif
616         case OC_STACK_CONTINUE:
617         case OC_STACK_RESOURCE_CHANGED:
618             return true;
619         default:
620             NS_LOG_V(DEBUG, "OCStackResult : %d", (int)ret);
621             return false;
622     }
623 }
624