[IOT-1548] Fix to transfer a large size of data on CoAPs over TCP
[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.ipv6.fd = -1;
298     caglobals.tcp.selectTimeout = CA_TCP_SELECT_TIMEOUT;
299     caglobals.tcp.listenBacklog = CA_TCP_LISTEN_BACKLOG;
300     caglobals.tcp.svrlist = NULL;
301
302     CATransportFlags_t flags = 0;
303     if (caglobals.client)
304     {
305         flags |= caglobals.clientFlags;
306     }
307     if (caglobals.server)
308     {
309         flags |= caglobals.serverFlags;
310     }
311
312     caglobals.tcp.ipv4tcpenabled = flags & CA_IPV4;
313     caglobals.tcp.ipv6tcpenabled = flags & CA_IPV6;
314 }
315
316 CAResult_t CAInitializeTCP(CARegisterConnectivityCallback registerCallback,
317                            CANetworkPacketReceivedCallback networkPacketCallback,
318                            CAAdapterChangeCallback netCallback,
319                            CAConnectionChangeCallback connCallback,
320                            CAErrorHandleCallback errorCallback, ca_thread_pool_t handle)
321 {
322     OIC_LOG(DEBUG, TAG, "IN");
323     VERIFY_NON_NULL(registerCallback, TAG, "registerCallback");
324     VERIFY_NON_NULL(networkPacketCallback, TAG, "networkPacketCallback");
325     VERIFY_NON_NULL(netCallback, TAG, "netCallback");
326 #ifndef SINGLE_THREAD
327     VERIFY_NON_NULL(handle, TAG, "thread pool handle");
328 #endif
329
330     g_networkChangeCallback = netCallback;
331     g_connectionChangeCallback = connCallback;
332     g_networkPacketCallback = networkPacketCallback;
333     g_errorCallback = errorCallback;
334
335     CAInitializeTCPGlobals();
336 #ifndef SINGLE_THREAD
337     caglobals.tcp.threadpool = handle;
338 #endif
339
340     CATCPSetConnectionChangedCallback(CATCPConnectionHandler);
341     CATCPSetPacketReceiveCallback(CATCPPacketReceivedCB);
342     CATCPSetErrorHandler(CATCPErrorHandler);
343
344 #ifdef __WITH_TLS__
345     if (CA_STATUS_OK != CAinitSslAdapter())
346     {
347         OIC_LOG(ERROR, TAG, "Failed to init SSL adapter");
348     }
349     else
350     {
351         CAsetSslAdapterCallbacks(CATCPPacketReceivedCB, CATCPPacketSendCB, CA_ADAPTER_TCP);
352     }
353 #endif
354
355     CAConnectivityHandler_t tcpHandler = {
356         .startAdapter = CAStartTCP,
357         .startListenServer = CAStartTCPListeningServer,
358         .stopListenServer = CAStopTCPListeningServer,
359         .startDiscoveryServer = CAStartTCPDiscoveryServer,
360         .sendData = CASendTCPUnicastData,
361         .sendDataToAll = CASendTCPMulticastData,
362         .GetnetInfo = CAGetTCPInterfaceInformation,
363         .readData = CAReadTCPData,
364         .stopAdapter = CAStopTCP,
365         .terminate = CATerminateTCP,
366         .cType = CA_ADAPTER_TCP};
367
368     registerCallback(tcpHandler);
369
370     OIC_LOG(INFO, TAG, "OUT IntializeTCP is Success");
371     return CA_STATUS_OK;
372 }
373
374 CAResult_t CAStartTCP()
375 {
376     OIC_LOG(DEBUG, TAG, "IN");
377
378     // Start network monitoring to receive adapter status changes.
379     CAIPStartNetworkMonitor(CATCPAdapterHandler, CA_ADAPTER_TCP);
380
381     // Set the port number received from application.
382     caglobals.tcp.ipv4.port = caglobals.ports.tcp.u4;
383     caglobals.tcp.ipv6.port = caglobals.ports.tcp.u6;
384
385 #ifndef SINGLE_THREAD
386     if (CA_STATUS_OK != CATCPInitializeQueueHandles())
387     {
388         OIC_LOG(ERROR, TAG, "Failed to Initialize Queue Handle");
389         CATerminateTCP();
390         return CA_STATUS_FAILED;
391     }
392
393     // Start send queue thread
394     if (CA_STATUS_OK != CAQueueingThreadStart(g_sendQueueHandle))
395     {
396         OIC_LOG(ERROR, TAG, "Failed to Start Send Data Thread");
397         return CA_STATUS_FAILED;
398     }
399 #else
400     CAResult_t ret = CATCPStartServer();
401     if (CA_STATUS_OK != ret)
402     {
403         OIC_LOG_V(DEBUG, TAG, "CATCPStartServer failed[%d]", ret);
404         return ret;
405     }
406 #endif
407
408     return CA_STATUS_OK;
409 }
410
411 CAResult_t CAStartTCPListeningServer()
412 {
413 #ifndef SINGLE_THREAD
414     if (!caglobals.server)
415     {
416         caglobals.server = true;    // only needed to run CA tests
417     }
418
419     CAResult_t ret = CATCPStartServer((const ca_thread_pool_t)caglobals.tcp.threadpool);
420     if (CA_STATUS_OK != ret)
421     {
422         OIC_LOG_V(ERROR, TAG, "Failed to start listening server![%d]", ret);
423         return ret;
424     }
425 #endif
426
427     return CA_STATUS_OK;
428 }
429
430 CAResult_t CAStopTCPListeningServer()
431 {
432     return CA_STATUS_OK;
433 }
434
435 CAResult_t CAStartTCPDiscoveryServer()
436 {
437     if (!caglobals.client)
438     {
439         caglobals.client = true;    // only needed to run CA tests
440     }
441
442     CAResult_t ret = CATCPStartServer((const ca_thread_pool_t)caglobals.tcp.threadpool);
443     if (CA_STATUS_OK != ret)
444     {
445         OIC_LOG_V(ERROR, TAG, "Failed to start discovery server![%d]", ret);
446         return ret;
447     }
448
449     return CA_STATUS_OK;
450 }
451
452 static size_t CAQueueTCPData(bool isMulticast, const CAEndpoint_t *endpoint,
453                              const void *data, size_t dataLength)
454 {
455     VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", -1);
456     VERIFY_NON_NULL_RET(data, TAG, "data", -1);
457
458     if (0 == dataLength)
459     {
460         OIC_LOG(ERROR, TAG, "Invalid Data Length");
461         return -1;
462     }
463
464     VERIFY_NON_NULL_RET(g_sendQueueHandle, TAG, "sendQueueHandle", -1);
465
466     // Create TCPData to add to queue
467     CATCPData *tcpData = CACreateTCPData(endpoint, data, dataLength, isMulticast);
468     if (!tcpData)
469     {
470         OIC_LOG(ERROR, TAG, "Failed to create ipData!");
471         return -1;
472     }
473     // Add message to send queue
474     CAQueueingThreadAddData(g_sendQueueHandle, tcpData, sizeof(CATCPData));
475
476     return dataLength;
477 }
478
479 int32_t CASendTCPUnicastData(const CAEndpoint_t *endpoint,
480                              const void *data, uint32_t dataLength,
481                              CADataType_t dataType)
482 {
483     OIC_LOG(DEBUG, TAG, "IN");
484     (void)dataType;
485 #ifndef SINGLE_THREAD
486     return CAQueueTCPData(false, endpoint, data, dataLength);
487 #else
488     return CATCPSendData(endpoint, data, dataLength);
489 #endif
490 }
491
492 int32_t CASendTCPMulticastData(const CAEndpoint_t *endpoint,
493                                const void *data, uint32_t dataLength,
494                                CADataType_t dataType)
495 {
496     (void)dataType;
497     return CAQueueTCPData(true, endpoint, data, dataLength);
498 }
499
500 CAResult_t CAReadTCPData()
501 {
502     OIC_LOG(DEBUG, TAG, "IN");
503 #ifdef SINGLE_THREAD
504     CATCPPullData();
505 #endif
506     return CA_STATUS_OK;
507 }
508
509 CAResult_t CAStopTCP()
510 {
511     CAIPStopNetworkMonitor(CA_ADAPTER_TCP);
512
513 #ifndef SINGLE_THREAD
514     if (g_sendQueueHandle && g_sendQueueHandle->threadMutex)
515     {
516         CAQueueingThreadStop(g_sendQueueHandle);
517     }
518     CATCPDeinitializeQueueHandles();
519 #endif
520
521     CATCPStopServer();
522
523     //Re-initializing the Globals to start them again
524     CAInitializeTCPGlobals();
525
526 #ifdef __WITH_TLS__
527     CAdeinitSslAdapter();
528 #endif
529
530     return CA_STATUS_OK;
531 }
532
533 void CATerminateTCP()
534 {
535     CAStopTCP();
536     CATCPSetPacketReceiveCallback(NULL);
537 }
538
539 void CATCPSendDataThread(void *threadData)
540 {
541     CATCPData *tcpData = (CATCPData *) threadData;
542     if (!tcpData)
543     {
544         OIC_LOG(DEBUG, TAG, "Invalid TCP data!");
545         return;
546     }
547
548     if (caglobals.tcp.terminate)
549     {
550         OIC_LOG(DEBUG, TAG, "Adapter is not enabled");
551         CATCPErrorHandler(tcpData->remoteEndpoint, tcpData->data, tcpData->dataLen,
552                           CA_SEND_FAILED);
553         return;
554     }
555
556     if (tcpData->isMulticast)
557     {
558         //Processing for sending multicast
559         OIC_LOG(DEBUG, TAG, "Send Multicast Data is called, not supported");
560         return;
561     }
562     else
563     {
564         // Check payload length from CoAP over TCP format header.
565         CAResult_t result = CA_STATUS_OK;
566         size_t payloadLen = CACheckPayloadLengthFromHeader(tcpData->data, tcpData->dataLen);
567         if (!payloadLen)
568         {
569             // if payload length is zero, disconnect from remote device.
570             OIC_LOG(DEBUG, TAG, "payload length is zero, disconnect from remote device");
571 #ifdef __WITH_TLS__
572             if (CA_STATUS_OK != CAcloseSslConnection(tcpData->remoteEndpoint))
573             {
574                 OIC_LOG(ERROR, TAG, "Failed to close TLS session");
575             }
576 #endif
577             CASearchAndDeleteTCPSession(tcpData->remoteEndpoint);
578             return;
579         }
580
581 #ifdef __WITH_TLS__
582          if (tcpData->remoteEndpoint && tcpData->remoteEndpoint->flags & CA_SECURE)
583          {
584              OIC_LOG(DEBUG, TAG, "CAencryptSsl called!");
585              result = CAencryptSsl(tcpData->remoteEndpoint, tcpData->data, tcpData->dataLen);
586
587              if (CA_STATUS_OK != result)
588              {
589                  OIC_LOG(ERROR, TAG, "CAAdapterNetDtlsEncrypt failed!");
590                  CASearchAndDeleteTCPSession(tcpData->remoteEndpoint);
591                  CATCPErrorHandler(tcpData->remoteEndpoint, tcpData->data, tcpData->dataLen,
592                                    CA_SEND_FAILED);
593              }
594              OIC_LOG_V(DEBUG, TAG,
595                        "CAAdapterNetDtlsEncrypt returned with result[%d]", result);
596             return;
597          }
598 #endif
599         //Processing for sending unicast
600          ssize_t dlen = CATCPSendData(tcpData->remoteEndpoint, tcpData->data, tcpData->dataLen);
601          if (-1 == dlen)
602          {
603              OIC_LOG(ERROR, TAG, "CATCPSendData failed");
604              CASearchAndDeleteTCPSession(tcpData->remoteEndpoint);
605              CATCPErrorHandler(tcpData->remoteEndpoint, tcpData->data, tcpData->dataLen,
606                                CA_SEND_FAILED);
607          }
608     }
609 }
610
611 CATCPData *CACreateTCPData(const CAEndpoint_t *remoteEndpoint, const void *data,
612                            size_t dataLength, bool isMulticast)
613 {
614     VERIFY_NON_NULL_RET(remoteEndpoint, TAG, "remoteEndpoint is NULL", NULL);
615     VERIFY_NON_NULL_RET(data, TAG, "data is NULL", NULL);
616
617     CATCPData *tcpData = (CATCPData *) OICCalloc(1, sizeof(*tcpData));
618     if (!tcpData)
619     {
620         OIC_LOG(ERROR, TAG, "Memory allocation failed!");
621         return NULL;
622     }
623
624     tcpData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
625     tcpData->data = (void *) OICMalloc(dataLength);
626     if (!tcpData->data)
627     {
628         OIC_LOG(ERROR, TAG, "Memory allocation failed!");
629         CAFreeTCPData(tcpData);
630         return NULL;
631     }
632
633     memcpy(tcpData->data, data, dataLength);
634     tcpData->dataLen = dataLength;
635
636     tcpData->isMulticast = isMulticast;
637
638     return tcpData;
639 }
640
641 void CAFreeTCPData(CATCPData *tcpData)
642 {
643     VERIFY_NON_NULL_VOID(tcpData, TAG, "tcpData is NULL");
644
645     CAFreeEndpoint(tcpData->remoteEndpoint);
646     OICFree(tcpData->data);
647     OICFree(tcpData);
648 }
649
650 void CADataDestroyer(void *data, uint32_t size)
651 {
652     if (size < sizeof(CATCPData))
653     {
654         OIC_LOG_V(ERROR, TAG, "Destroy data too small %p %" PRIu32, data, size);
655     }
656     CATCPData *TCPData = (CATCPData *) data;
657
658     CAFreeTCPData(TCPData);
659 }
660
661 #ifdef SINGLE_THREAD
662 size_t CAGetTotalLengthFromPacketHeader(const unsigned char *recvBuffer, size_t size)
663 {
664     OIC_LOG(DEBUG, TAG, "IN - CAGetTotalLengthFromHeader");
665
666     if (NULL == recvBuffer || !size)
667     {
668         OIC_LOG(ERROR, TAG, "recvBuffer is NULL");
669         return 0;
670     }
671
672     coap_transport_t transport = coap_get_tcp_header_type_from_initbyte(
673             ((unsigned char *)recvBuffer)[0] >> 4);
674     size_t optPaylaodLen = coap_get_length_from_header((unsigned char *)recvBuffer,
675                                                         transport);
676     size_t headerLen = coap_get_tcp_header_length((unsigned char *)recvBuffer);
677
678     OIC_LOG_V(DEBUG, TAG, "option/paylaod length [%d]", optPaylaodLen);
679     OIC_LOG_V(DEBUG, TAG, "header length [%d]", headerLen);
680     OIC_LOG_V(DEBUG, TAG, "total data length [%d]", headerLen + optPaylaodLen);
681
682     OIC_LOG(DEBUG, TAG, "OUT - CAGetTotalLengthFromHeader");
683     return headerLen + optPaylaodLen;
684 }
685
686 void CAGetTCPHeaderDetails(unsigned char* recvBuffer, coap_transport_t *transport,
687                            size_t *headerlen)
688 {
689     if (NULL == recvBuffer)
690     {
691         OIC_LOG(ERROR, TAG, "recvBuffer is NULL");
692         return;
693     }
694
695     if (NULL == transport)
696     {
697         OIC_LOG(ERROR, TAG, "transport is NULL");
698         return;
699     }
700
701     if (NULL == headerlen)
702     {
703         OIC_LOG(ERROR, TAG, "headerlen is NULL");
704         return;
705     }
706
707     *transport = coap_get_tcp_header_type_from_initbyte(
708         ((unsigned char *)recvBuffer)[0] >> 4);
709     *headerlen = coap_get_tcp_header_length_for_transport(*transport);
710 }
711 #endif