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 ******************************************************************/
21 #include "oic_malloc.h"
22 #include "caremotehandler.h"
27 CARemoteEndpoint_t *CACloneRemoteEndpoint(const CARemoteEndpoint_t *rep)
35 // allocate the remote end point structure.
36 CARemoteEndpoint_t *clone = (CARemoteEndpoint_t *) OICMalloc(sizeof(CARemoteEndpoint_t));
39 OIC_LOG(DEBUG, TAG, "CACloneRemoteEndpoint Out of memory");
42 memset(clone, 0, sizeof(CARemoteEndpoint_t));
43 memcpy(clone, rep, sizeof(CARemoteEndpoint_t));
45 if (rep->resourceUri != NULL)
47 // allocate reference uri field
48 len = strlen(rep->resourceUri);
50 temp = (char *) OICMalloc(sizeof(char) * (len + 1));
53 OIC_LOG(DEBUG, TAG, "CACloneRemoteEndpoint Out of memory");
55 CADestroyRemoteEndpointInternal(clone);
59 memset(temp, 0, sizeof(char) * (len + 1));
60 strncpy(temp, rep->resourceUri, len);
63 clone->resourceUri = temp;
69 #define COAP_PREFIX "coap://"
70 #define COAP_PREFIX_LEN 7
71 #define COAPS_PREFIX "coaps://"
72 #define COAPS_PREFIX_LEN 8
77 static int32_t getCAAddress(const char *pAddress, CAAddress_t *outAddress)
79 if (pAddress == NULL || outAddress == NULL)
82 // simple parse, it will be change.
83 // 10.11.12.13:4545 (ip)
84 // 10:11:12:13:45:45 (mac)
86 int32_t len = strlen(pAddress);
92 for (i = 0; i < len; i++)
94 if (pAddress[i] == '.')
99 // found port number start index
100 if (isIp && pAddress[i] == ':')
109 strncpy(outAddress->IP.ipAddress, pAddress, ipLen == 0 ? len : ipLen);
112 outAddress->IP.port = atoi(pAddress + ipLen + 1);
114 OIC_LOG_V(DEBUG, TAG, "ip: %s, port: %d", outAddress->IP.ipAddress, outAddress->IP.port);
118 strncpy(outAddress->BT.btMacAddress, pAddress, CA_MACADDR_SIZE - 1);
120 OIC_LOG_V(DEBUG, TAG, "mac address : %s", outAddress->BT.btMacAddress);
126 CARemoteEndpoint_t *CACreateRemoteEndpointUriInternal(const CAURI_t uri,
127 const CAConnectivityType_t connectivityType)
130 // coap://10.11.12.13:4545/resource_uri
131 // coap://10:11:12:13:45:45/resource_uri
139 CABool_t secured = CA_FALSE;
140 if (strncmp(COAP_PREFIX, uri, COAP_PREFIX_LEN) == 0)
142 OIC_LOG_V(DEBUG, TAG, "uri has '%s' prefix.", COAP_PREFIX);
143 startIndex = COAP_PREFIX_LEN;
146 if (strncmp(COAPS_PREFIX, uri, COAPS_PREFIX_LEN) == 0)
148 OIC_LOG_V(DEBUG, TAG, "uri has '%s' prefix.", COAPS_PREFIX);
149 startIndex = COAPS_PREFIX_LEN;
153 // #2. copy uri for parse
154 char *cloneUri = NULL;
155 int32_t len = strlen(uri) - startIndex;
159 OIC_LOG(DEBUG, TAG, "uri length is 0!");
163 cloneUri = (char *) OICMalloc(sizeof(char) * (len + 1));
164 if (cloneUri == NULL)
166 OIC_LOG(DEBUG, TAG, "CACreateRemoteEndpointUriInternal Out of memory");
169 memset(cloneUri, 0, sizeof(char) * (len + 1));
170 memcpy(cloneUri, &uri[startIndex], sizeof(char) * len);
173 // #4. parse resource uri
174 char *pAddress = cloneUri;
175 char *pResourceUri = NULL;
178 for (i = 0; i < len; i++)
180 if (cloneUri[i] == '/')
185 pResourceUri = &cloneUri[i + 1];
192 OIC_LOG_V(DEBUG, TAG, "pAddress : %s", pAddress);
194 OIC_LOG_V(DEBUG, TAG, "pResourceUri : %s", pResourceUri == NULL ? "" : pResourceUri);
198 memset(&address, 0, sizeof(CAAddress_t));
200 int resType = getCAAddress(pAddress, &address);
203 OIC_LOG(DEBUG, TAG, "address parse error");
210 CAURI_t resourceUri = pResourceUri;
212 CARemoteEndpoint_t *remoteEndpoint = CACreateRemoteEndpointInternal(resourceUri, address, connectivityType);
213 if (remoteEndpoint == NULL)
215 OIC_LOG(DEBUG, TAG, "create remote endpoint fail");
220 remoteEndpoint->isSecured = secured;
224 OIC_LOG_V(DEBUG, TAG, "Remote endpoint successfully created [%d]!", remoteEndpoint->isSecured);
225 return remoteEndpoint;
228 CARemoteEndpoint_t *CACreateRemoteEndpointInternal(const CAURI_t resourceUri,
229 const CAAddress_t addr, const CAConnectivityType_t type)
234 if (resourceUri == NULL)
236 OIC_LOG(DEBUG, TAG, "uri is null value");
240 // allocate the remote end point structure.
241 CARemoteEndpoint_t *rep = (CARemoteEndpoint_t *) OICMalloc(sizeof(CARemoteEndpoint_t));
245 OIC_LOG(DEBUG, TAG, "CACreateRemoteEndpointInternal of memory");
248 memset(rep, 0, sizeof(CARemoteEndpoint_t));
250 // allocate reference uri field
251 len = strlen(resourceUri);
253 temp = (char *) OICMalloc(sizeof(char) * (len + 1));
256 OIC_LOG(DEBUG, TAG, "CACreateRemoteEndpointInternal Out of memory");
258 CADestroyRemoteEndpointInternal(rep);
262 memset(temp, 0, sizeof(char) * (len + 1));
263 strncpy(temp, resourceUri, len);
266 rep->resourceUri = temp;
268 // save the addressInfo
269 memcpy(&(rep->addressInfo), &addr, sizeof(CAAddress_t));
272 rep->connectivityType = type;
277 CARequestInfo_t *CACloneRequestInfo(const CARequestInfo_t *rep)
285 // allocate the request info structure.
286 CARequestInfo_t *clone = (CARequestInfo_t *) OICMalloc(sizeof(CARequestInfo_t));
289 OIC_LOG(DEBUG, TAG, "CACloneRequestInfo Out of memory");
292 memset(clone, 0, sizeof(CARequestInfo_t));
293 memcpy(clone, rep, sizeof(CARequestInfo_t));
295 if (rep->info.token != NULL)
297 // allocate token field
298 len = CA_MAX_TOKEN_LEN;
300 temp = (char *) OICMalloc(sizeof(char) * (len + 1));
303 OIC_LOG(DEBUG, TAG, "CACloneRequestInfo Out of memory");
305 CADestroyRequestInfoInternal(clone);
309 memset(temp, 0, sizeof(char) * (len + 1));
310 memcpy(temp, rep->info.token, len);
313 clone->info.token = temp;
316 if (rep->info.options != NULL && rep->info.numOptions > 0)
319 clone->info.options =
320 (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * clone->info.numOptions);
321 if (clone->info.options == NULL)
323 OIC_LOG(DEBUG, TAG, "CACloneRequestInfo Out of memory");
324 OICFree(clone->info.token);
328 memcpy(clone->info.options,
330 sizeof(CAHeaderOption_t) * clone->info.numOptions);
333 if (rep->info.payload != NULL)
335 // allocate payload field
336 len = strlen(rep->info.payload);
338 temp = (char *) OICMalloc(sizeof(char) * (len + 1));
341 OIC_LOG(DEBUG, TAG, "CACloneRequestInfo Out of memory");
343 CADestroyRequestInfoInternal(clone);
347 memset(temp, 0, sizeof(char) * (len + 1));
348 strncpy(temp, rep->info.payload, len);
351 clone->info.payload = temp;
357 CAResponseInfo_t *CACloneResponseInfo(const CAResponseInfo_t *rep)
365 // allocate the response info structure.
366 CAResponseInfo_t *clone = (CAResponseInfo_t *) OICMalloc(sizeof(CAResponseInfo_t));
369 OIC_LOG(DEBUG, TAG, "CACloneResponseInfo Out of memory");
372 memset(clone, 0, sizeof(CAResponseInfo_t));
373 memcpy(clone, rep, sizeof(CAResponseInfo_t));
375 if (rep->info.token != NULL)
377 // allocate token field
378 len = CA_MAX_TOKEN_LEN;
380 temp = (char *) OICMalloc(sizeof(char) * (len + 1));
383 OIC_LOG(DEBUG, TAG, "CACloneResponseInfo Out of memory");
385 CADestroyResponseInfoInternal(clone);
389 memset(temp, 0, sizeof(char) * (len + 1));
390 memcpy(temp, rep->info.token, len);
393 clone->info.token = temp;
396 if (rep->info.options != NULL && rep->info.numOptions)
399 clone->info.options = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t));
400 if (clone->info.options == NULL)
402 OIC_LOG(DEBUG, TAG, "CACloneResponseInfo Out of memory");
403 OICFree(clone->info.token);
407 memset(clone->info.options, 0, sizeof(CAHeaderOption_t));
408 memcpy(clone->info.options, rep->info.options, sizeof(CAHeaderOption_t));
411 if (rep->info.payload != NULL)
413 // allocate payload field
414 len = strlen(rep->info.payload);
416 temp = (char *) OICMalloc(sizeof(char) * (len + 1));
419 OIC_LOG(DEBUG, TAG, "CACloneResponseInfo Out of memory");
421 CADestroyResponseInfoInternal(clone);
425 memset(temp, 0, sizeof(char) * (len + 1));
426 strncpy(temp, rep->info.payload, len);
429 clone->info.payload = temp;
435 void CADestroyRemoteEndpointInternal(CARemoteEndpoint_t *rep)
441 if (rep->resourceUri != NULL)
443 OICFree((char *) rep->resourceUri);
446 // free remote end point structure.
450 void CADestroyRequestInfoInternal(CARequestInfo_t *rep)
456 if (rep->info.token != NULL)
458 OICFree((char *) rep->info.token);
461 // free options field
462 if (rep->info.options != NULL && rep->info.numOptions)
464 OICFree((CAHeaderOption_t *) rep->info.options);
467 // free payload field
468 if (rep->info.payload != NULL)
470 OICFree((char *) rep->info.payload);
476 void CADestroyResponseInfoInternal(CAResponseInfo_t *rep)
482 if (rep->info.token != NULL)
484 OICFree((char *) rep->info.token);
487 // free options field
488 if (rep->info.options != NULL && rep->info.numOptions)
490 OICFree((CAHeaderOption_t *) rep->info.options);
493 // free payload field
494 if (rep->info.payload != NULL)
496 OICFree((char *) rep->info.payload);