IP address plumbing changes to support IPv6
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / caremotehandler.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 <string.h>
22
23 #include "oic_malloc.h"
24 #include "oic_string.h"
25 #include "caremotehandler.h"
26 #include "logger.h"
27
28 #define TAG "CA"
29
30 CAEndpoint_t *CACloneEndpoint(const CAEndpoint_t *rep)
31 {
32     if (NULL == rep)
33     {
34         OIC_LOG(ERROR, TAG, "parameter is null");
35         return NULL;
36     }
37
38     // allocate the remote end point structure.
39     CAEndpoint_t *clone = (CAEndpoint_t *)OICMalloc(sizeof (CAEndpoint_t));
40     if (NULL == clone)
41     {
42         OIC_LOG(ERROR, TAG, "CACloneRemoteEndpoint Out of memory");
43         return NULL;
44     }
45     *clone = *rep;
46
47     return clone;
48 }
49
50 CARequestInfo_t *CACloneRequestInfo(const CARequestInfo_t *rep)
51 {
52     if (NULL == rep)
53     {
54         OIC_LOG(ERROR, TAG, "parameter is null");
55         return NULL;
56     }
57
58     // allocate the request info structure.
59     CARequestInfo_t *clone = (CARequestInfo_t *) OICMalloc(sizeof(CARequestInfo_t));
60     if (!clone)
61     {
62         OIC_LOG(ERROR, TAG, "CACloneRequestInfo Out of memory");
63         return NULL;
64     }
65
66     *clone = *rep;
67
68     if (rep->info.token)
69     {
70         char *temp = NULL;
71
72         // allocate token field
73         uint8_t len = rep->info.tokenLength;
74
75         if (len)
76         {
77             temp = (char *) OICCalloc(len, sizeof(char));
78             if (!temp)
79             {
80                 OIC_LOG(ERROR, TAG, "CACloneRequestInfo Out of memory");
81
82                 CADestroyRequestInfoInternal(clone);
83
84                 return NULL;
85             }
86             memcpy(temp, rep->info.token, len);
87         }
88
89         // save the token
90         clone->info.token = temp;
91         clone->info.tokenLength = len;
92     }
93
94     if (NULL != rep->info.options && 0 < rep->info.numOptions)
95     {
96         // save the options
97         clone->info.options =
98             (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * rep->info.numOptions);
99         if (NULL == clone->info.options)
100         {
101             OIC_LOG(ERROR, TAG, "CACloneRequestInfo Out of memory");
102             OICFree(clone->info.token);
103             OICFree(clone);
104             return NULL;
105         }
106         memcpy(clone->info.options, rep->info.options,
107                sizeof(CAHeaderOption_t) * rep->info.numOptions);
108     }
109     else
110     {
111         clone->info.options = NULL;
112         clone->info.numOptions = 0;
113     }
114
115     if (NULL != rep->info.payload)
116     {
117         // allocate payload field
118         char *temp = OICStrdup(rep->info.payload);
119         if (NULL == temp)
120         {
121             OIC_LOG(ERROR, TAG, "CACloneRequestInfo Out of memory");
122
123             CADestroyRequestInfoInternal(clone);
124
125             return NULL;
126         }
127
128         // save the payload
129         clone->info.payload = temp;
130     }
131
132     return clone;
133 }
134
135 CAResponseInfo_t *CACloneResponseInfo(const CAResponseInfo_t *rep)
136 {
137     if (NULL == rep)
138     {
139         OIC_LOG(ERROR, TAG, "Response pointer is NULL");
140         return NULL;
141     }
142
143     // check the result value of response info.
144     switch (rep->result)
145     {
146         case CA_EMPTY:
147         case CA_SUCCESS:
148         case CA_CREATED:
149         case CA_DELETED:
150         case CA_BAD_REQ:
151         case CA_BAD_OPT:
152         case CA_NOT_FOUND:
153         case CA_INTERNAL_SERVER_ERROR:
154         case CA_RETRANSMIT_TIMEOUT:
155             break;
156
157         default:
158             OIC_LOG(ERROR, TAG, "Response status code is invalid number");
159             return NULL;
160     }
161
162     // allocate the response info structure.
163     CAResponseInfo_t *clone = (CAResponseInfo_t *) OICCalloc(1, sizeof(CAResponseInfo_t));
164     if (NULL == clone)
165     {
166         OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
167         return NULL;
168     }
169     *clone = *rep;
170
171     if (rep->info.token)
172     {
173         char *temp = NULL;
174
175         // allocate token field
176         uint8_t len = rep->info.tokenLength;
177
178         if (len)
179         {
180             temp = (char *) OICCalloc(len, sizeof(char));
181             if (!temp)
182             {
183                 OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
184
185                 CADestroyResponseInfoInternal(clone);
186
187                 return NULL;
188             }
189             memcpy(temp, rep->info.token, len);
190         }
191         // save the token
192         clone->info.token = temp;
193         clone->info.tokenLength = len;
194     }
195
196     if (NULL != rep->info.options && rep->info.numOptions)
197     {
198         // save the options
199         clone->info.options =
200                 (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * rep->info.numOptions);
201
202         if (NULL == clone->info.options)
203         {
204             OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
205
206             OICFree(clone->info.token);
207             OICFree(clone);
208             return NULL;
209         }
210         memcpy(clone->info.options, rep->info.options,
211                 sizeof(CAHeaderOption_t) * rep->info.numOptions);
212     }
213     else
214     {
215         clone->info.options = NULL;
216         clone->info.numOptions = 0;
217     }
218
219     if (NULL != rep->info.payload)
220     {
221         // allocate payload field
222         char *temp = OICStrdup(rep->info.payload);
223         if (NULL == temp)
224         {
225             OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
226
227             CADestroyResponseInfoInternal(clone);
228
229             return NULL;
230         }
231
232         // save the payload
233         clone->info.payload = temp;
234     }
235
236     return clone;
237 }
238
239 void CADestroyEndpointInternal(CAEndpoint_t *rep)
240 {
241     OICFree(rep);
242 }
243
244 void CADestroyRequestInfoInternal(CARequestInfo_t *rep)
245 {
246     if (NULL == rep)
247     {
248         OIC_LOG(ERROR, TAG, "parameter is null");
249         return;
250     }
251
252     // free token field
253     OICFree(rep->info.token);
254
255     // free options field
256     OICFree((CAHeaderOption_t *) rep->info.options);
257
258     // free payload field
259     OICFree((char *) rep->info.payload);
260
261     OICFree(rep);
262 }
263
264 void CADestroyResponseInfoInternal(CAResponseInfo_t *rep)
265 {
266     if (NULL == rep)
267     {
268         OIC_LOG(ERROR, TAG, "parameter is null");
269         return;
270     }
271
272     // free token field
273     OICFree(rep->info.token);
274
275     // free options field
276     if (rep->info.options != NULL && rep->info.numOptions)
277     {
278         OICFree((CAHeaderOption_t *) rep->info.options);
279     }
280
281     // free payload field
282     OICFree((char *) rep->info.payload);
283
284     OICFree(rep);
285 }
286