[IOT-1593] TLS ports and CA_SECURE flag added for secure socket accept
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / tcp_adapter / catcpadapter.c
1 /* ****************************************************************
2  *
3  * Copyright 2015 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 <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdint.h>
25
26 #ifndef __STDC_FORMAT_MACROS
27 #define __STDC_FORMAT_MACROS
28 #endif
29 #include <inttypes.h>
30
31 #include "cainterface.h"
32 #include "caipnwmonitor.h"
33 #include "catcpadapter.h"
34 #include "catcpinterface.h"
35 #include "caqueueingthread.h"
36 #include "caadapterutils.h"
37 #include "camutex.h"
38 #include "uarraylist.h"
39 #include "caremotehandler.h"
40 #include "logger.h"
41 #include "oic_malloc.h"
42 #ifdef __WITH_TLS__
43 #include "ca_adapter_net_ssl.h"
44 #endif
45
46 /**
47  * Logging tag for module name.
48  */
49 #define TAG "OIC_CA_TCP_ADAP"
50
51 /**
52  * Holds internal thread TCP data information.
53  */
54 typedef struct
55 {
56     CAEndpoint_t *remoteEndpoint;
57     void *data;
58     size_t dataLen;
59     bool isMulticast;
60 } CATCPData;
61
62 #define CA_TCP_LISTEN_BACKLOG  3
63
64 #define CA_TCP_SELECT_TIMEOUT 10
65
66 /**
67  * Queue handle for Send Data.
68  */
69 static CAQueueingThread_t *g_sendQueueHandle = NULL;
70
71 /**
72  * Network Packet Received Callback to CA.
73  */
74 static CANetworkPacketReceivedCallback g_networkPacketCallback = NULL;
75
76 /**
77  * Adapter Changed Callback to CA.
78  */
79 static CAAdapterChangeCallback g_networkChangeCallback = NULL;
80
81 /**
82  * Connection Changed Callback to CA.
83  */
84 static CAConnectionChangeCallback g_connectionChangeCallback = NULL;
85
86 /**
87  * error Callback to CA adapter.
88  */
89 static CAErrorHandleCallback g_errorCallback = NULL;
90
91 static void CATCPPacketReceivedCB(const CASecureEndpoint_t *sep,
92                                   const void *data, uint32_t dataLength);
93
94 static void CATCPErrorHandler(const CAEndpoint_t *endpoint, const void *data,
95                               size_t dataLength, CAResult_t result);
96
97 /**
98  * KeepAlive Connected or Disconnected Callback to CA adapter.
99  */
100 static CAKeepAliveConnectionCallback g_connKeepAliveCallback = NULL;
101
102 static CAResult_t CATCPInitializeQueueHandles();
103
104 static void CATCPDeinitializeQueueHandles();
105
106 static void CATCPSendDataThread(void *threadData);
107
108 static CATCPData *CACreateTCPData(const CAEndpoint_t *remoteEndpoint,
109                                   const void *data, size_t dataLength,
110                                   bool isMulticast);
111 void CAFreeTCPData(CATCPData *ipData);
112
113 static void CADataDestroyer(void *data, uint32_t size);
114
115 CAResult_t CATCPInitializeQueueHandles()
116 {
117     // Check if the message queue is already initialized
118     if (g_sendQueueHandle)
119     {
120         OIC_LOG(DEBUG, TAG, "send queue handle is already initialized!");
121         return CA_STATUS_OK;
122     }
123
124     // Create send message queue
125     g_sendQueueHandle = OICMalloc(sizeof(CAQueueingThread_t));
126     if (!g_sendQueueHandle)
127     {
128         OIC_LOG(ERROR, TAG, "Memory allocation failed!");
129         return CA_MEMORY_ALLOC_FAILED;
130     }
131
132     if (CA_STATUS_OK != CAQueueingThreadInitialize(g_sendQueueHandle,
133                                 (const ca_thread_pool_t)caglobals.tcp.threadpool,
134                                 CATCPSendDataThread, CADataDestroyer))
135     {
136         OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread");
137         OICFree(g_sendQueueHandle);
138         g_sendQueueHandle = NULL;
139         return CA_STATUS_FAILED;
140     }
141
142     return CA_STATUS_OK;
143 }
144
145 void CATCPDeinitializeQueueHandles()
146 {
147     CAQueueingThreadDestroy(g_sendQueueHandle);
148     OICFree(g_sendQueueHandle);
149     g_sendQueueHandle = NULL;
150 }
151
152 void CATCPConnectionStateCB(const char *ipAddress, CANetworkStatus_t status)
153 {
154     (void)ipAddress;
155     (void)status;
156 }
157
158 void CATCPPacketReceivedCB(const CASecureEndpoint_t *sep, const void *data,
159                            uint32_t dataLength)
160 {
161     VERIFY_NON_NULL_VOID(sep, TAG, "sep is NULL");
162     VERIFY_NON_NULL_VOID(data, TAG, "data is NULL");
163
164     OIC_LOG_V(DEBUG, TAG, "Address: %s, port:%d", sep->endpoint.addr, sep->endpoint.port);
165
166 #ifdef SINGLE_THREAD
167     if (g_networkPacketCallback)
168     {
169         g_networkPacketCallback(sep, data, dataLength);
170     }
171 #else
172     unsigned char *buffer = (unsigned char*)data;
173     size_t bufferLen = dataLength;
174     size_t index = 0;
175
176     //get remote device information from file descriptor.
177     CATCPSessionInfo_t *svritem = CAGetTCPSessionInfoFromEndpoint(&sep->endpoint, &index);
178     if (!svritem)
179     {
180         OIC_LOG(ERROR, TAG, "there is no connection information in list");
181         return;
182     }
183     if (UNKNOWN == svritem->protocol)
184     {
185         OIC_LOG(ERROR, TAG, "invalid protocol type");
186         return;
187     }
188
189     //totalLen filled only when header fully read and parsed
190     while (0 != bufferLen)
191     {
192         CAResult_t res = CAConstructCoAP(svritem, &buffer, &bufferLen);
193         if (CA_STATUS_OK != res)
194         {
195             OIC_LOG_V(ERROR, TAG, "CAConstructCoAP return error : %d", res);
196             return;
197         }
198
199         //when successfully read all required data - pass them to upper layer.
200         if (svritem->len == svritem->totalLen)
201         {
202             if (g_networkPacketCallback)
203             {
204                 g_networkPacketCallback(sep, svritem->data, svritem->totalLen);
205             }
206             CACleanData(svritem);
207         }
208         else
209         {
210             OIC_LOG_V(DEBUG, TAG, "%u bytes required for complete CoAP",
211                                 svritem->totalLen - svritem->len);
212         }
213     }
214 #endif
215 }
216
217 #ifdef __WITH_TLS__
218 static ssize_t CATCPPacketSendCB(CAEndpoint_t *endpoint, const void *data, size_t dataLength)
219 {
220     OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
221     VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint is NULL", -1);
222     VERIFY_NON_NULL_RET(data, TAG, "data is NULL", -1);
223
224     OIC_LOG_V(DEBUG, TAG, "Address: %s, port:%d", endpoint->addr, endpoint->port);
225     OIC_LOG_BUFFER(DEBUG, TAG, data, dataLength);
226
227     ssize_t ret = CATCPSendData(endpoint, data, dataLength);
228     OIC_LOG_V(DEBUG, TAG, "Out %s : %d bytes sent", __func__, ret);
229     return ret;
230 }
231 #endif
232
233 static void CATCPErrorHandler(const CAEndpoint_t *endpoint, const void *data,
234                               size_t dataLength, CAResult_t result)
235 {
236     VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint is NULL");
237     VERIFY_NON_NULL_VOID(data, TAG, "data is NULL");
238
239     if (g_errorCallback)
240     {
241         g_errorCallback(endpoint, data, dataLength, result);
242     }
243 }
244
245 static void CATCPConnectionHandler(const CAEndpoint_t *endpoint, bool isConnected)
246 {
247     // Pass the changed connection status to RI Layer for keepalive.
248     if (g_connKeepAliveCallback)
249     {
250         g_connKeepAliveCallback(endpoint, isConnected);
251     }
252
253     // Pass the changed connection status to CAUtil.
254     if (g_connectionChangeCallback)
255     {
256         g_connectionChangeCallback(endpoint, isConnected);
257     }
258 }
259
260 void CATCPSetKeepAliveCallbacks(CAKeepAliveConnectionCallback ConnHandler)
261 {
262     g_connKeepAliveCallback = ConnHandler;
263 }
264
265 void CATCPAdapterHandler(CATransportAdapter_t adapter, CANetworkStatus_t status)
266 {
267     if (g_networkChangeCallback)
268     {
269         g_networkChangeCallback(adapter, status);
270     }
271
272     if (CA_INTERFACE_DOWN == status)
273     {
274         OIC_LOG(DEBUG, TAG, "Network status is down, close all session");
275         CATCPStopServer();
276     }
277     else if (CA_INTERFACE_UP == status)
278     {
279         OIC_LOG(DEBUG, TAG, "Network status is up, create new socket for listening");
280
281         CAResult_t ret = CA_STATUS_FAILED;
282 #ifndef SINGLE_THREAD
283         ret = CATCPStartServer((const ca_thread_pool_t)caglobals.tcp.threadpool);
284 #else
285         ret = CATCPStartServer();
286 #endif
287         if (CA_STATUS_OK != ret)
288         {
289             OIC_LOG_V(DEBUG, TAG, "CATCPStartServer failed[%d]", ret);
290         }
291     }
292 }
293
294 static void CAInitializeTCPGlobals()
295 {
296     caglobals.tcp.ipv4.fd = -1;
297     caglobals.tcp.ipv4s.fd = -1;
298     caglobals.tcp.ipv6.fd = -1;
299     caglobals.tcp.ipv6s.fd = -1;
300
301     // Set the port number received from application.
302     caglobals.tcp.ipv4.port = caglobals.ports.tcp.u4;
303     caglobals.tcp.ipv4s.port = caglobals.ports.tcp.u4s;
304     caglobals.tcp.ipv6.port = caglobals.ports.tcp.u6;
305     caglobals.tcp.ipv6s.port = caglobals.ports.tcp.u6s;
306
307     caglobals.tcp.selectTimeout = CA_TCP_SELECT_TIMEOUT;
308     caglobals.tcp.listenBacklog = CA_TCP_LISTEN_BACKLOG;
309     caglobals.tcp.svrlist = NULL;
310
311     CATransportFlags_t flags = 0;
312     if (caglobals.client)
313     {
314         flags |= caglobals.clientFlags;
315     }
316     if (caglobals.server)
317     {
318         flags |= caglobals.serverFlags;
319     }
320
321     caglobals.tcp.ipv4tcpenabled = flags & CA_IPV4;
322     caglobals.tcp.ipv6tcpenabled = flags & CA_IPV6;
323 }
324
325 CAResult_t CAInitializeTCP(CARegisterConnectivityCallback registerCallback,
326                            CANetworkPacketReceivedCallback networkPacketCallback,
327                            CAAdapterChangeCallback netCallback,
328                            CAConnectionChangeCallback connCallback,
329                            CAErrorHandleCallback errorCallback, ca_thread_pool_t handle)
330 {
331     OIC_LOG(DEBUG, TAG, "IN");
332     VERIFY_NON_NULL(registerCallback, TAG, "registerCallback");
333     VERIFY_NON_NULL(networkPacketCallback, TAG, "networkPacketCallback");
334     VERIFY_NON_NULL(netCallback, TAG, "netCallback");
335 #ifndef SINGLE_THREAD
336     VERIFY_NON_NULL(handle, TAG, "thread pool handle");
337 #endif
338
339     g_networkChangeCallback = netCallback;
340     g_connectionChangeCallback = connCallback;
341     g_networkPacketCallback = networkPacketCallback;
342     g_errorCallback = errorCallback;
343
344     CAInitializeTCPGlobals();
345 #ifndef SINGLE_THREAD
346     caglobals.tcp.threadpool = handle;
347 #endif
348
349     CATCPSetConnectionChangedCallback(CATCPConnectionHandler);
350     CATCPSetPacketReceiveCallback(CATCPPacketReceivedCB);
351     CATCPSetErrorHandler(CATCPErrorHandler);
352
353 #ifdef __WITH_TLS__
354     if (CA_STATUS_OK != CAinitSslAdapter())
355     {
356         OIC_LOG(ERROR, TAG, "Failed to init SSL adapter");
357     }
358     else
359     {
360         CAsetSslAdapterCallbacks(CATCPPacketReceivedCB, CATCPPacketSendCB, CA_ADAPTER_TCP);
361     }
362 #endif
363
364     CAConnectivityHandler_t tcpHandler = {
365         .startAdapter = CAStartTCP,
366         .startListenServer = CAStartTCPListeningServer,
367         .stopListenServer = CAStopTCPListeningServer,
368         .startDiscoveryServer = CAStartTCPDiscoveryServer,
369         .sendData = CASendTCPUnicastData,
370         .sendDataToAll = CASendTCPMulticastData,
371         .GetnetInfo = CAGetTCPInterfaceInformation,
372         .readData = CAReadTCPData,
373         .stopAdapter = CAStopTCP,
374         .terminate = CATerminateTCP,
375         .cType = CA_ADAPTER_TCP};
376
377     registerCallback(tcpHandler);
378
379     OIC_LOG(INFO, TAG, "OUT IntializeTCP is Success");
380     return CA_STATUS_OK;
381 }
382
383 CAResult_t CAStartTCP()
384 {
385     OIC_LOG(DEBUG, TAG, "IN");
386
387     // Start network monitoring to receive adapter status changes.
388     CAIPStartNetworkMonitor(CATCPAdapterHandler, CA_ADAPTER_TCP);
389
390 #ifndef SINGLE_THREAD
391     if (CA_STATUS_OK != CATCPInitializeQueueHandles())
392     {
393         OIC_LOG(ERROR, TAG, "Failed to Initialize Queue Handle");
394         CATerminateTCP();
395         return CA_STATUS_FAILED;
396     }
397
398     // Start send queue thread
399     if (CA_STATUS_OK != CAQueueingThreadStart(g_sendQueueHandle))
400     {
401         OIC_LOG(ERROR, TAG, "Failed to Start Send Data Thread");
402         return CA_STATUS_FAILED;
403     }
404 #else
405     CAResult_t ret = CATCPStartServer();
406     if (CA_STATUS_OK != ret)
407     {
408         OIC_LOG_V(DEBUG, TAG, "CATCPStartServer failed[%d]", ret);
409         return ret;
410     }
411 #endif
412
413     return CA_STATUS_OK;
414 }
415
416 CAResult_t CAStartTCPListeningServer()
417 {
418 #ifndef SINGLE_THREAD
419     if (!caglobals.server)
420     {
421         caglobals.server = true;    // only needed to run CA tests
422     }
423
424     CAResult_t ret = CATCPStartServer((const ca_thread_pool_t)caglobals.tcp.threadpool);
425     if (CA_STATUS_OK != ret)
426     {
427         OIC_LOG_V(ERROR, TAG, "Failed to start listening server![%d]", ret);
428         return ret;
429     }
430 #endif
431
432     return CA_STATUS_OK;
433 }
434
435 CAResult_t CAStopTCPListeningServer()
436 {
437     return CA_STATUS_OK;
438 }
439
440 CAResult_t CAStartTCPDiscoveryServer()
441 {
442     if (!caglobals.client)
443     {
444         caglobals.client = true;    // only needed to run CA tests
445     }
446
447     CAResult_t ret = CATCPStartServer((const ca_thread_pool_t)caglobals.tcp.threadpool);
448     if (CA_STATUS_OK != ret)
449     {
450         OIC_LOG_V(ERROR, TAG, "Failed to start discovery server![%d]", ret);
451         return ret;
452     }
453
454     return CA_STATUS_OK;
455 }
456
457 static size_t CAQueueTCPData(bool isMulticast, const CAEndpoint_t *endpoint,
458                              const void *data, size_t dataLength)
459 {
460     VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", -1);
461     VERIFY_NON_NULL_RET(data, TAG, "data", -1);
462
463     if (0 == dataLength)
464     {
465         OIC_LOG(ERROR, TAG, "Invalid Data Length");
466         return -1;
467     }
468
469     VERIFY_NON_NULL_RET(g_sendQueueHandle, TAG, "sendQueueHandle", -1);
470
471     // Create TCPData to add to queue
472     CATCPData *tcpData = CACreateTCPData(endpoint, data, dataLength, isMulticast);
473     if (!tcpData)
474     {
475         OIC_LOG(ERROR, TAG, "Failed to create ipData!");
476         return -1;
477     }
478     // Add message to send queue
479     CAQueueingThreadAddData(g_sendQueueHandle, tcpData, sizeof(CATCPData));
480
481     return dataLength;
482 }
483
484 int32_t CASendTCPUnicastData(const CAEndpoint_t *endpoint,
485                              const void *data, uint32_t dataLength,
486                              CADataType_t dataType)
487 {
488     OIC_LOG(DEBUG, TAG, "IN");
489     (void)dataType;
490 #ifndef SINGLE_THREAD
491     return CAQueueTCPData(false, endpoint, data, dataLength);
492 #else
493     return CATCPSendData(endpoint, data, dataLength);
494 #endif
495 }
496
497 int32_t CASendTCPMulticastData(const CAEndpoint_t *endpoint,
498                                const void *data, uint32_t dataLength,
499                                CADataType_t dataType)
500 {
501     (void)dataType;
502     return CAQueueTCPData(true, endpoint, data, dataLength);
503 }
504
505 CAResult_t CAReadTCPData()
506 {
507     OIC_LOG(DEBUG, TAG, "IN");
508 #ifdef SINGLE_THREAD
509     CATCPPullData();
510 #endif
511     return CA_STATUS_OK;
512 }
513
514 CAResult_t CAStopTCP()
515 {
516     CAIPStopNetworkMonitor(CA_ADAPTER_TCP);
517
518 #ifndef SINGLE_THREAD
519     if (g_sendQueueHandle && g_sendQueueHandle->threadMutex)
520     {
521         CAQueueingThreadStop(g_sendQueueHandle);
522     }
523     CATCPDeinitializeQueueHandles();
524 #endif
525
526     CATCPStopServer();
527
528     //Re-initializing the Globals to start them again
529     CAInitializeTCPGlobals();
530
531 #ifdef __WITH_TLS__
532     CAdeinitSslAdapter();
533 #endif
534
535     return CA_STATUS_OK;
536 }
537
538 void CATerminateTCP()
539 {
540     CAStopTCP();
541     CATCPSetPacketReceiveCallback(NULL);
542 }
543
544 void CATCPSendDataThread(void *threadData)
545 {
546     CATCPData *tcpData = (CATCPData *) threadData;
547     if (!tcpData)
548     {
549         OIC_LOG(DEBUG, TAG, "Invalid TCP data!");
550         return;
551     }
552
553     if (caglobals.tcp.terminate)
554     {
555         OIC_LOG(DEBUG, TAG, "Adapter is not enabled");
556         CATCPErrorHandler(tcpData->remoteEndpoint, tcpData->data, tcpData->dataLen,
557                           CA_SEND_FAILED);
558         return;
559     }
560
561     if (tcpData->isMulticast)
562     {
563         //Processing for sending multicast
564         OIC_LOG(DEBUG, TAG, "Send Multicast Data is called, not supported");
565         return;
566     }
567     else
568     {
569         // Check payload length from CoAP over TCP format header.
570         CAResult_t result = CA_STATUS_OK;
571         size_t payloadLen = CACheckPayloadLengthFromHeader(tcpData->data, tcpData->dataLen);
572         if (!payloadLen)
573         {
574             // if payload length is zero, disconnect from remote device.
575             OIC_LOG(DEBUG, TAG, "payload length is zero, disconnect from remote device");
576 #ifdef __WITH_TLS__
577             if (CA_STATUS_OK != CAcloseSslConnection(tcpData->remoteEndpoint))
578             {
579                 OIC_LOG(ERROR, TAG, "Failed to close TLS session");
580             }
581 #endif
582             CASearchAndDeleteTCPSession(tcpData->remoteEndpoint);
583             return;
584         }
585
586 #ifdef __WITH_TLS__
587          if (tcpData->remoteEndpoint && tcpData->remoteEndpoint->flags & CA_SECURE)
588          {
589              OIC_LOG(DEBUG, TAG, "CAencryptSsl called!");
590              result = CAencryptSsl(tcpData->remoteEndpoint, tcpData->data, tcpData->dataLen);
591
592              if (CA_STATUS_OK != result)
593              {
594                  OIC_LOG(ERROR, TAG, "CAAdapterNetDtlsEncrypt failed!");
595                  CASearchAndDeleteTCPSession(tcpData->remoteEndpoint);
596                  CATCPErrorHandler(tcpData->remoteEndpoint, tcpData->data, tcpData->dataLen,
597                                    CA_SEND_FAILED);
598              }
599              OIC_LOG_V(DEBUG, TAG,
600                        "CAAdapterNetDtlsEncrypt returned with result[%d]", result);
601             return;
602          }
603 #endif
604         //Processing for sending unicast
605          ssize_t dlen = CATCPSendData(tcpData->remoteEndpoint, tcpData->data, tcpData->dataLen);
606          if (-1 == dlen)
607          {
608              OIC_LOG(ERROR, TAG, "CATCPSendData failed");
609              CASearchAndDeleteTCPSession(tcpData->remoteEndpoint);
610              CATCPErrorHandler(tcpData->remoteEndpoint, tcpData->data, tcpData->dataLen,
611                                CA_SEND_FAILED);
612          }
613     }
614 }
615
616 CATCPData *CACreateTCPData(const CAEndpoint_t *remoteEndpoint, const void *data,
617                            size_t dataLength, bool isMulticast)
618 {
619     VERIFY_NON_NULL_RET(remoteEndpoint, TAG, "remoteEndpoint is NULL", NULL);
620     VERIFY_NON_NULL_RET(data, TAG, "data is NULL", NULL);
621
622     CATCPData *tcpData = (CATCPData *) OICCalloc(1, sizeof(*tcpData));
623     if (!tcpData)
624     {
625         OIC_LOG(ERROR, TAG, "Memory allocation failed!");
626         return NULL;
627     }
628
629     tcpData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
630     tcpData->data = (void *) OICMalloc(dataLength);
631     if (!tcpData->data)
632     {
633         OIC_LOG(ERROR, TAG, "Memory allocation failed!");
634         CAFreeTCPData(tcpData);
635         return NULL;
636     }
637
638     memcpy(tcpData->data, data, dataLength);
639     tcpData->dataLen = dataLength;
640
641     tcpData->isMulticast = isMulticast;
642
643     return tcpData;
644 }
645
646 void CAFreeTCPData(CATCPData *tcpData)
647 {
648     VERIFY_NON_NULL_VOID(tcpData, TAG, "tcpData is NULL");
649
650     CAFreeEndpoint(tcpData->remoteEndpoint);
651     OICFree(tcpData->data);
652     OICFree(tcpData);
653 }
654
655 void CADataDestroyer(void *data, uint32_t size)
656 {
657     if (size < sizeof(CATCPData))
658     {
659         OIC_LOG_V(ERROR, TAG, "Destroy data too small %p %" PRIu32, data, size);
660     }
661     CATCPData *TCPData = (CATCPData *) data;
662
663     CAFreeTCPData(TCPData);
664 }
665
666 #ifdef SINGLE_THREAD
667 size_t CAGetTotalLengthFromPacketHeader(const unsigned char *recvBuffer, size_t size)
668 {
669     OIC_LOG(DEBUG, TAG, "IN - CAGetTotalLengthFromHeader");
670
671     if (NULL == recvBuffer || !size)
672     {
673         OIC_LOG(ERROR, TAG, "recvBuffer is NULL");
674         return 0;
675     }
676
677     coap_transport_t transport = coap_get_tcp_header_type_from_initbyte(
678             ((unsigned char *)recvBuffer)[0] >> 4);
679     size_t optPaylaodLen = coap_get_length_from_header((unsigned char *)recvBuffer,
680                                                         transport);
681     size_t headerLen = coap_get_tcp_header_length((unsigned char *)recvBuffer);
682
683     OIC_LOG_V(DEBUG, TAG, "option/paylaod length [%d]", optPaylaodLen);
684     OIC_LOG_V(DEBUG, TAG, "header length [%d]", headerLen);
685     OIC_LOG_V(DEBUG, TAG, "total data length [%d]", headerLen + optPaylaodLen);
686
687     OIC_LOG(DEBUG, TAG, "OUT - CAGetTotalLengthFromHeader");
688     return headerLen + optPaylaodLen;
689 }
690
691 void CAGetTCPHeaderDetails(unsigned char* recvBuffer, coap_transport_t *transport,
692                            size_t *headerlen)
693 {
694     if (NULL == recvBuffer)
695     {
696         OIC_LOG(ERROR, TAG, "recvBuffer is NULL");
697         return;
698     }
699
700     if (NULL == transport)
701     {
702         OIC_LOG(ERROR, TAG, "transport is NULL");
703         return;
704     }
705
706     if (NULL == headerlen)
707     {
708         OIC_LOG(ERROR, TAG, "headerlen is NULL");
709         return;
710     }
711
712     *transport = coap_get_tcp_header_type_from_initbyte(
713         ((unsigned char *)recvBuffer)[0] >> 4);
714     *headerlen = coap_get_tcp_header_length_for_transport(*transport);
715 }
716 #endif