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 ******************************************************************/
23 #include "oic_malloc.h"
24 #include "caremotehandler.h"
29 CARemoteEndpoint_t *CACloneRemoteEndpoint(const CARemoteEndpoint_t *rep)
33 OIC_LOG(ERROR, TAG, "parameter is null");
37 // allocate the remote end point structure.
38 CARemoteEndpoint_t *clone = (CARemoteEndpoint_t *) OICMalloc(sizeof(CARemoteEndpoint_t));
41 OIC_LOG(ERROR, TAG, "CACloneRemoteEndpoint Out of memory");
44 memcpy(clone, rep, sizeof(CARemoteEndpoint_t));
46 if (NULL != rep->resourceUri)
48 // allocate reference uri field
49 size_t len = strlen(rep->resourceUri);
51 char *temp = (char *) OICCalloc(len + 1, sizeof(char));
54 OIC_LOG(ERROR, TAG, "CACloneRemoteEndpoint Out of memory");
56 CADestroyRemoteEndpointInternal(clone);
61 strncpy(temp, rep->resourceUri, len);
64 clone->resourceUri = temp;
70 #define COAP_PREFIX "coap://"
71 #define COAP_PREFIX_LEN 7
72 #define COAPS_PREFIX "coaps://"
73 #define COAPS_PREFIX_LEN 8
78 static int32_t getCAAddress(const char *pAddress, CAAddress_t *outAddress)
80 if (NULL == pAddress || NULL == outAddress)
82 OIC_LOG(ERROR, TAG, "parameter is null");
86 // simple parse, it will be change.
87 // 10.11.12.13:4545 (ip)
88 // 10:11:12:13:45:45 (mac)
90 int32_t len = strlen(pAddress);
96 for (i = 0; i < len; i++)
98 if (pAddress[i] == '.')
103 // found port number start index
104 if (isIp && pAddress[i] == ':')
113 if(ipLen && ipLen < sizeof(outAddress->IP.ipAddress))
115 strncpy(outAddress->IP.ipAddress, pAddress, ipLen);
116 outAddress->IP.ipAddress[ipLen] = '\0';
118 else if (!ipLen && len < sizeof(outAddress->IP.ipAddress))
120 strncpy(outAddress->IP.ipAddress, pAddress, len);
121 outAddress->IP.ipAddress[len] = '\0';
125 OIC_LOG_V(ERROR, TAG, "IP Address too long: %d", ipLen==0 ? len : ipLen);
132 outAddress->IP.port = atoi(pAddress + ipLen + 1);
135 OIC_LOG_V(DEBUG, TAG, "ip: %s, port: %d", outAddress->IP.ipAddress, outAddress->IP.port);
139 strncpy(outAddress->BT.btMacAddress, pAddress, CA_MACADDR_SIZE - 1);
141 OIC_LOG_V(DEBUG, TAG, "mac address : %s", outAddress->BT.btMacAddress);
147 CARemoteEndpoint_t *CACreateRemoteEndpointUriInternal(const CAURI_t uri,
148 const CATransportType_t transportType)
151 // coap://10.11.12.13:4545/resource_uri
152 // coap://10:11:12:13:45:45/resource_uri
156 OIC_LOG(ERROR, TAG, "parameter is null");
163 bool secured = false;
164 if (strncmp(COAP_PREFIX, uri, COAP_PREFIX_LEN) == 0)
166 OIC_LOG_V(DEBUG, TAG, "uri has '%s' prefix.", COAP_PREFIX);
167 startIndex = COAP_PREFIX_LEN;
170 if (strncmp(COAPS_PREFIX, uri, COAPS_PREFIX_LEN) == 0)
172 OIC_LOG_V(DEBUG, TAG, "uri has '%s' prefix.", COAPS_PREFIX);
173 startIndex = COAPS_PREFIX_LEN;
177 // #2. copy uri for parse
178 int32_t len = strlen(uri) - startIndex;
182 OIC_LOG(ERROR, TAG, "uri length is 0!");
186 char *cloneUri = (char *) OICCalloc(len + 1, sizeof(char));
187 if (NULL == cloneUri)
189 OIC_LOG(ERROR, TAG, "CACreateRemoteEndpointUriInternal Out of memory");
193 memcpy(cloneUri, &uri[startIndex], sizeof(char) * len);
194 cloneUri[len] = '\0';
197 // #4. parse resource uri
198 char *pAddress = cloneUri;
199 char *pResourceUri = NULL;
202 for (i = 0; i < len; i++)
204 if (cloneUri[i] == '/')
209 pResourceUri = &cloneUri[i + 1];
216 OIC_LOG_V(DEBUG, TAG, "pAddress : %s", pAddress);
218 OIC_LOG_V(DEBUG, TAG, "pResourceUri : %s", pResourceUri == NULL ? "" : pResourceUri);
221 CAAddress_t address = {};
223 int resType = getCAAddress(pAddress, &address);
226 OIC_LOG(DEBUG, TAG, "address parse error");
233 CAURI_t resourceUri = pResourceUri;
235 CARemoteEndpoint_t *remoteEndpoint = CACreateRemoteEndpointInternal(resourceUri, address,
237 if (NULL == remoteEndpoint)
239 OIC_LOG(ERROR, TAG, "create remote endpoint fail");
244 remoteEndpoint->isSecured = secured;
248 OIC_LOG_V(DEBUG, TAG, "Remote endpoint successfully created [%d]!", remoteEndpoint->isSecured);
249 return remoteEndpoint;
252 CARemoteEndpoint_t *CACreateRemoteEndpointInternal(const CAURI_t resourceUri,
253 const CAAddress_t addr,
254 const CATransportType_t type)
256 if (NULL == resourceUri)
258 OIC_LOG(ERROR, TAG, "uri is null value");
262 // allocate the remote end point structure.
263 CARemoteEndpoint_t *rep = (CARemoteEndpoint_t *) OICCalloc(1, sizeof(CARemoteEndpoint_t));
267 OIC_LOG(ERROR, TAG, "CACreateRemoteEndpointInternal of memory");
271 // allocate reference uri field
272 size_t len = strlen(resourceUri);
274 char *temp = (char *) OICMalloc(sizeof(char) * (len + 1));
277 OIC_LOG(ERROR, TAG, "CACreateRemoteEndpointInternal Out of memory");
279 CADestroyRemoteEndpointInternal(rep);
283 strncpy(temp, resourceUri, len);
287 rep->resourceUri = temp;
289 // save the addressInfo
290 memcpy(&(rep->addressInfo), &addr, sizeof(CAAddress_t));
293 rep->transportType = type;
298 CARequestInfo_t *CACloneRequestInfo(const CARequestInfo_t *rep)
302 OIC_LOG(ERROR, TAG, "parameter is null");
306 // allocate the request info structure.
307 CARequestInfo_t *clone = (CARequestInfo_t *) OICMalloc(sizeof(CARequestInfo_t));
310 OIC_LOG(ERROR, TAG, "CACloneRequestInfo Out of memory");
314 memcpy(clone, rep, sizeof(CARequestInfo_t));
320 // allocate token field
321 uint8_t len = rep->info.tokenLength;
325 temp = (char *) OICCalloc(len, sizeof(char));
328 OIC_LOG(ERROR, TAG, "CACloneRequestInfo Out of memory");
330 CADestroyRequestInfoInternal(clone);
334 memcpy(temp, rep->info.token, len);
338 clone->info.token = temp;
339 clone->info.tokenLength = len;
342 if (NULL != rep->info.options && 0 < rep->info.numOptions)
345 clone->info.options =
346 (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * rep->info.numOptions);
347 if (NULL == clone->info.options)
349 OIC_LOG(ERROR, TAG, "CACloneRequestInfo Out of memory");
350 OICFree(clone->info.token);
354 memcpy(clone->info.options, rep->info.options,
355 sizeof(CAHeaderOption_t) * rep->info.numOptions);
359 clone->info.options = NULL;
360 clone->info.numOptions = 0;
363 if (NULL != rep->info.payload)
365 // allocate payload field
366 size_t len = strlen(rep->info.payload);
368 char *temp = (char *) OICMalloc(sizeof(char) * (len + 1));
371 OIC_LOG(ERROR, TAG, "CACloneRequestInfo Out of memory");
373 CADestroyRequestInfoInternal(clone);
377 strncpy(temp, rep->info.payload, len);
381 clone->info.payload = temp;
387 CAResponseInfo_t *CACloneResponseInfo(const CAResponseInfo_t *rep)
391 OIC_LOG(ERROR, TAG, "Response pointer is NULL");
395 // allocate the response info structure.
396 CAResponseInfo_t *clone = (CAResponseInfo_t *) OICCalloc(1, sizeof(CAResponseInfo_t));
399 OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
402 memcpy(clone, rep, sizeof(CAResponseInfo_t));
408 // allocate token field
409 uint8_t len = rep->info.tokenLength;
413 temp = (char *) OICCalloc(len, sizeof(char));
416 OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
418 CADestroyResponseInfoInternal(clone);
422 memcpy(temp, rep->info.token, len);
425 clone->info.token = temp;
426 clone->info.tokenLength = len;
429 if (NULL != rep->info.options && rep->info.numOptions)
432 clone->info.options =
433 (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * rep->info.numOptions);
435 if (NULL == clone->info.options)
437 OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
439 OICFree(clone->info.token);
443 memcpy(clone->info.options, rep->info.options,
444 sizeof(CAHeaderOption_t) * rep->info.numOptions);
448 clone->info.options = NULL;
449 clone->info.numOptions = 0;
452 if (NULL != rep->info.payload)
454 // allocate payload field
455 int32_t len = strlen(rep->info.payload);
457 char *temp = (char *) OICCalloc(len + 1, sizeof(char));
460 OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
462 CADestroyResponseInfoInternal(clone);
466 strncpy(temp, rep->info.payload, len);
469 clone->info.payload = temp;
475 void CADestroyRemoteEndpointInternal(CARemoteEndpoint_t *rep)
479 OIC_LOG(ERROR, TAG, "parameter is null");
484 OICFree((char *) rep->resourceUri);
486 // free remote end point structure.
490 void CADestroyRequestInfoInternal(CARequestInfo_t *rep)
494 OIC_LOG(ERROR, TAG, "parameter is null");
499 OICFree(rep->info.token);
501 // free options field
502 OICFree((CAHeaderOption_t *) rep->info.options);
504 // free payload field
505 OICFree((char *) rep->info.payload);
510 void CADestroyResponseInfoInternal(CAResponseInfo_t *rep)
514 OIC_LOG(ERROR, TAG, "parameter is null");
519 OICFree(rep->info.token);
521 // free options field
522 if (rep->info.options != NULL && rep->info.numOptions)
524 OICFree((CAHeaderOption_t *) rep->info.options);
527 // free payload field
528 OICFree((char *) rep->info.payload);