You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ma...@apache.org on 2016/12/07 04:46:40 UTC

[06/10] incubator-mynewt-core git commit: oic; coap - fixes to TCP header parsing. Add initial statistics.

oic; coap - fixes to TCP header parsing. Add initial statistics.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/9207785d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/9207785d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/9207785d

Branch: refs/heads/develop
Commit: 9207785da38608018f3577f4ab76dc7a948994a0
Parents: 4dd5a1f
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Tue Dec 6 20:39:23 2016 -0800
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Tue Dec 6 20:45:59 2016 -0800

----------------------------------------------------------------------
 net/oic/pkg.yml                        |  1 +
 net/oic/src/messaging/coap/coap.c      | 78 +++++++++++++++++++++++++++--
 net/oic/src/messaging/coap/coap.h      | 16 +++++-
 net/oic/src/messaging/coap/constants.h |  6 +--
 4 files changed, 94 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9207785d/net/oic/pkg.yml
----------------------------------------------------------------------
diff --git a/net/oic/pkg.yml b/net/oic/pkg.yml
index 92098cd..1c6c302 100644
--- a/net/oic/pkg.yml
+++ b/net/oic/pkg.yml
@@ -27,6 +27,7 @@ pkg.deps:
     - "encoding/tinycbor"
     - "kernel/os"
     - "sys/log"
+    - "sys/stats"
 
 pkg.deps.OC_TRANSPORT_GATT:
     - "net/nimble/host"

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9207785d/net/oic/src/messaging/coap/coap.c
----------------------------------------------------------------------
diff --git a/net/oic/src/messaging/coap/coap.c b/net/oic/src/messaging/coap/coap.c
index 470af9c..528a009 100644
--- a/net/oic/src/messaging/coap/coap.c
+++ b/net/oic/src/messaging/coap/coap.c
@@ -41,6 +41,13 @@
 #include "security/oc_dtls.h"
 #endif
 
+STATS_SECT_DECL(coap_stats) coap_stats;
+STATS_NAME_START(coap_stats)
+    STATS_NAME(coap_stats, iframe)
+    STATS_NAME(coap_stats, ierr)
+    STATS_NAME(coap_stats, oframe)
+STATS_NAME_END(coap_stats)
+
 /*---------------------------------------------------------------------------*/
 /*- Variables ---------------------------------------------------------------*/
 /*---------------------------------------------------------------------------*/
@@ -266,6 +273,10 @@ coap_init_connection(void)
 {
     /* initialize transaction ID */
     current_mid = oc_random_rand();
+
+    stats_init_and_reg(STATS_HDR(coap_stats),
+      STATS_SIZE_INIT_PARMS(coap_stats, STATS_SIZE_32),
+      STATS_NAME_INIT_PARMS(coap_stats), "coap");
 }
 /*---------------------------------------------------------------------------*/
 uint16_t
@@ -391,7 +402,7 @@ coap_serialize_message(coap_packet_t *pkt, uint8_t *buffer, int tcp_hdr)
             cth8 = (struct coap_tcp_hdr8 *)buffer;
             cth8->type = COAP_TCP_TYPE8;
             cth8->token_len = pkt->token_len;
-            cth8->data_len = data_len - 13;
+            cth8->data_len = data_len - COAP_TCP_LENGTH8_OFF;
             cth8->code = pkt->code;
             option = (uint8_t *)(cth8 + 1);
         } else if (data_len < 65805) {
@@ -399,7 +410,7 @@ coap_serialize_message(coap_packet_t *pkt, uint8_t *buffer, int tcp_hdr)
             cth16 = (struct coap_tcp_hdr16 *)buffer;
             cth16->type = COAP_TCP_TYPE16;
             cth16->token_len = pkt->token_len;
-            cth16->data_len = htons(data_len - 269);
+            cth16->data_len = htons(data_len - COAP_TCP_LENGTH16_OFF);
             cth16->code = pkt->code;
             option = (uint8_t *)(cth16 + 1);
         } else {
@@ -407,7 +418,7 @@ coap_serialize_message(coap_packet_t *pkt, uint8_t *buffer, int tcp_hdr)
             cth32 = (struct coap_tcp_hdr32 *)buffer;
             cth32->type = COAP_TCP_TYPE32;
             cth32->token_len = pkt->token_len;
-            cth32->data_len = htonl(data_len - 65805);
+            cth32->data_len = htonl(data_len - COAP_TCP_LENGTH32_OFF);
             cth32->code = pkt->code;
             option = (uint8_t *)(cth32 + 1);
         }
@@ -432,9 +443,47 @@ coap_send_message(oc_message_t *message)
 {
     LOG("-sending OCF message (%u)-\n", (unsigned int) message->length);
 
+    STATS_INC(coap_stats, oframe);
+
     oc_send_message(message);
 }
 
+/*
+ * Given COAP message header, return the number of bytes to expect for
+ * this frame.
+ */
+uint16_t
+coap_tcp_msg_size(uint8_t *hdr, int datalen)
+{
+    struct coap_tcp_hdr0 *cth0;
+    struct coap_tcp_hdr8 *cth8;
+    struct coap_tcp_hdr16 *cth16;
+    struct coap_tcp_hdr32 *cth32;
+
+    if (datalen < sizeof(*cth32)) {
+        return -1;
+    }
+    cth0 = (struct coap_tcp_hdr0 *)hdr;
+    if (cth0->data_len < COAP_TCP_TYPE8) {
+        return cth0->data_len + sizeof(*cth0) + cth0->token_len;
+    } else if (cth0->data_len == COAP_TCP_TYPE8) {
+        cth8 = (struct coap_tcp_hdr8 *)hdr;
+        return cth8->data_len + sizeof(*cth8) + cth8->token_len +
+               COAP_TCP_LENGTH8_OFF;
+    } else if (cth0->data_len == COAP_TCP_TYPE16) {
+        cth16 = (struct coap_tcp_hdr16 *)hdr;
+        return ntohs(cth16->data_len) + sizeof(*cth16) + cth16->token_len +
+               COAP_TCP_LENGTH16_OFF;
+    } else if (cth0->data_len == COAP_TCP_TYPE32) {
+        cth32 = (struct coap_tcp_hdr32 *)hdr;
+        return ntohl(cth32->data_len) + sizeof(*cth32) + cth32->token_len +
+               COAP_TCP_LENGTH32_OFF;
+    } else {
+        /* never here */
+        return -1;
+    }
+}
+
 /*---------------------------------------------------------------------------*/
 coap_status_t
 coap_parse_message(coap_packet_t *pkt, uint8_t *data, uint16_t data_len,
@@ -455,6 +504,8 @@ coap_parse_message(coap_packet_t *pkt, uint8_t *data, uint16_t data_len,
     /* pointer to packet bytes */
     pkt->buffer = data;
 
+    STATS_INC(coap_stats, iframe);
+
     /* parse header fields */
     if (!tcp_hdr) {
         udp = (struct coap_udp_hdr *)data;
@@ -465,6 +516,7 @@ coap_parse_message(coap_packet_t *pkt, uint8_t *data, uint16_t data_len,
         pkt->mid = ntohs(udp->id);
         if (pkt->version != 1) {
             coap_error_message = "CoAP version must be 1";
+            STATS_INC(coap_stats, ierr);
             return BAD_REQUEST_4_00;
         }
         cur_opt = (uint8_t *)(udp + 1);
@@ -477,27 +529,40 @@ coap_parse_message(coap_packet_t *pkt, uint8_t *data, uint16_t data_len,
         cth0 = (struct coap_tcp_hdr0 *)data;
         if (cth0->data_len < 13) {
             pkt->token_len = cth0->token_len;
+            if (data_len != cth0->data_len + sizeof(*cth0) + pkt->token_len) {
+                goto len_err;
+            }
             pkt->code = cth0->code;
             cur_opt = (uint8_t *)(cth0 + 1);
         } else if (cth0->data_len == 13) {
             cth8 = (struct coap_tcp_hdr8 *)data;
             pkt->token_len = cth8->token_len;
+            if (data_len != cth8->data_len + sizeof(*cth8) + pkt->token_len) {
+                goto len_err;
+            }
             pkt->code = cth8->code;
             cur_opt = (uint8_t *)(cth8 + 1);
         } else if (cth0->data_len == 14) {
             cth16 = (struct coap_tcp_hdr16 *)data;
             pkt->token_len = cth16->token_len;
+            if (data_len != cth16->data_len + sizeof(*cth16) + pkt->token_len) {
+                goto len_err;
+            }
             pkt->code = cth16->code;
             cur_opt = (uint8_t *)(cth16 + 1);
         } else {
             cth32 = (struct coap_tcp_hdr32 *)data;
             pkt->token_len = cth32->token_len;
+            if (data_len != cth32->data_len + sizeof(*cth32) + pkt->token_len) {
+                goto len_err;
+            }
             pkt->code = cth32->code;
             cur_opt = (uint8_t *)(cth32 + 1);
         }
     }
     if (pkt->token_len > COAP_TOKEN_LEN) {
         coap_error_message = "Token Length must not be more than 8";
+        STATS_INC(coap_stats, ierr);
         return BAD_REQUEST_4_00;
     }
 
@@ -604,6 +669,7 @@ coap_parse_message(coap_packet_t *pkt, uint8_t *data, uint16_t data_len,
             LOG("Proxy-Uri NOT IMPLEMENTED [%s]\n", (char *)pkt->proxy_uri);
             coap_error_message =
               "This is a constrained server (MyNewt)";
+            STATS_INC(coap_stats, ierr);
             return PROXYING_NOT_SUPPORTED_5_05;
             break;
         case COAP_OPTION_PROXY_SCHEME:
@@ -614,6 +680,7 @@ coap_parse_message(coap_packet_t *pkt, uint8_t *data, uint16_t data_len,
             LOG("Proxy-Scheme NOT IMPLEMENTED [%s]\n", pkt->proxy_scheme);
             coap_error_message =
               "This is a constrained server (MyNewt)";
+            STATS_INC(coap_stats, ierr);
             return PROXYING_NOT_SUPPORTED_5_05;
             break;
 
@@ -690,6 +757,7 @@ coap_parse_message(coap_packet_t *pkt, uint8_t *data, uint16_t data_len,
             /* check if critical (odd) */
             if (opt_num & 1) {
                 coap_error_message = "Unsupported critical option";
+                STATS_INC(coap_stats, ierr);
                 return BAD_OPTION_4_02;
             }
         }
@@ -698,6 +766,10 @@ coap_parse_message(coap_packet_t *pkt, uint8_t *data, uint16_t data_len,
     LOG("-Done parsing-------\n");
 
     return NO_ERROR;
+len_err:
+    STATS_INC(coap_stats, ierr);
+    coap_error_message = "Input len mismatch";
+    return BAD_REQUEST_4_00;
 }
 #if 0
 int

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9207785d/net/oic/src/messaging/coap/coap.h
----------------------------------------------------------------------
diff --git a/net/oic/src/messaging/coap/coap.h b/net/oic/src/messaging/coap/coap.h
index 987f34a..4ff64d7 100644
--- a/net/oic/src/messaging/coap/coap.h
+++ b/net/oic/src/messaging/coap/coap.h
@@ -44,11 +44,12 @@
 extern "C" {
 #endif
 
+#include <stats/stats.h>
 /* OIC stack headers */
 #include "../src/port/mynewt/config.h"
 #include "oic/oc_buffer.h"
+#include "oic/oc_log.h"
 #include "../src/port/oc_connectivity.h"
-#include "../src/port/oc_log.h"
 #include "../src/port/oc_random.h"
 
 #ifndef MAX
@@ -160,6 +161,17 @@ typedef struct coap_packet {
     uint8_t *payload;
 } coap_packet_t;
 
+/*
+ * COAP statistics
+ */
+STATS_SECT_START(coap_stats)
+    STATS_SECT_ENTRY(iframe)
+    STATS_SECT_ENTRY(ierr)
+    STATS_SECT_ENTRY(oframe)
+STATS_SECT_END
+
+extern STATS_SECT_DECL(coap_stats) coap_stats;
+
 /* option format serialization */
 #define COAP_SERIALIZE_INT_OPTION(coap_pkt, number, field, text)               \
   if (IS_OPTION(coap_pkt, number)) {                                           \
@@ -210,6 +222,8 @@ extern char *coap_error_message;
 void coap_init_connection(void);
 uint16_t coap_get_mid(void);
 
+uint16_t coap_tcp_msg_size(uint8_t *hdr, int datalen);
+
 void coap_init_message(coap_packet_t *, coap_message_type_t type,
                        uint8_t code, uint16_t mid);
 size_t coap_serialize_message(coap_packet_t *, uint8_t *buffer, int tcp_hdr);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9207785d/net/oic/src/messaging/coap/constants.h
----------------------------------------------------------------------
diff --git a/net/oic/src/messaging/coap/constants.h b/net/oic/src/messaging/coap/constants.h
index 385df69..8620a6d 100644
--- a/net/oic/src/messaging/coap/constants.h
+++ b/net/oic/src/messaging/coap/constants.h
@@ -71,9 +71,9 @@ struct coap_udp_hdr {
  * Header used by Iotivity for TCP-like transports.
  * 4 different kinds of headers.
  */
-#define COAP_TCP_LENGTH_FIELD_8_BIT      13
-#define COAP_TCP_LENGTH_FIELD_16_BIT     269
-#define COAP_TCP_LENGTH_FIELD_32_BIT     65805
+#define COAP_TCP_LENGTH8_OFF      13
+#define COAP_TCP_LENGTH16_OFF     269
+#define COAP_TCP_LENGTH32_OFF     65805
 
 #define COAP_TCP_TYPE0      0
 #define COAP_TCP_TYPE8      13