diff --git a/src/cmdu.c b/src/cmdu.c index 708549d723acd92069ebbd5f0dcd257c3ce9e307..5cb9a0235318ba72fe7daf28e88c568ebf53c9d1 100644 --- a/src/cmdu.c +++ b/src/cmdu.c @@ -195,8 +195,8 @@ struct cmdu_buff *cmdu_alloc_simple(uint16_t type, uint16_t *mid) } f->cdata = (struct cmdu_linear *)(f->head + 18); - if (type <= CMDU_TYPE_MAX) - cmdu_set_type(f, type); + //if (type <= CMDU_TYPE_MAX) + cmdu_set_type(f, type); if (*mid == 0) *mid = cmdu_get_next_mid(); @@ -241,34 +241,6 @@ int cmdu_reserve(struct cmdu_buff *c, size_t s) int cmdu_copy_tlvs_linear(struct cmdu_buff *c, uint8_t *tlvs, uint32_t tlvslen) { -#if 0 - struct cmdu *n; - - - if (!is_cmdu_type_valid(type)) - return NULL; - - //if (!is_tlv_stream_valid(type, tlvs, tlvslen)) - // return NULL; - - - n = calloc(1, sizeof(*n) + tlvslen * sizeof(uint8_t)); - if (!n) - return NULL; - - n->hdr.version = 0; - n->hdr.type = type; - - if (*mid == 0) - *mid = cmdu_get_next_mid(); - - n->hdr.mid = *mid; - - if (cmdu_should_relay(type)) - CMDU_SET_RELAY_MCAST(n); - -#endif - if (c->end - c->tail < tlvslen) return -1; @@ -307,6 +279,37 @@ struct cmdu_buff *cmdu_alloc_custom(uint16_t type, uint16_t *mid, char *ifname, return f; } +struct cmdu_buff *cmdu_clone(struct cmdu_buff *frm) +{ + struct cmdu_buff *f; + int len; + + + if (!frm || !frm->cdata) { + fprintf(stderr, "%s: cmdu for cloning is invalid!\n", __func__); + return NULL; + } + + len = cmdu_size(frm); + f = cmdu_alloc(len); + if (!f) { + fprintf(stderr, "%s: -ENOMEM\n", __func__); + return NULL; + } + + f->cdata = (struct cmdu_linear *)(f->head + 18); + f->data = (uint8_t *)(f->cdata + 1); + f->datalen = frm->datalen; + f->tail = f->data + f->datalen; + memcpy(f->cdata, frm->cdata, len); + + memcpy(f->dev_macaddr, frm->dev_macaddr, 6); + strncpy(f->dev_ifname, frm->dev_ifname, 15); + memcpy(f->origin, frm->origin, 6); + + return f; +} + int cmdu_copy_tlvs(struct cmdu_buff *c, struct tlv *tv[], int tv_arrsize) { uint16_t tlvslen = 0; diff --git a/src/cmdu.h b/src/cmdu.h index 9a410f0f1ffe00f4760710ba748d233ba3860fc9..5a6a0e8d577f886940f1f43d080d3bf0a5e4b8fd 100644 --- a/src/cmdu.h +++ b/src/cmdu.h @@ -174,6 +174,7 @@ int cmdu_parse_tlvs(struct cmdu_buff *c, struct tlv *tv[][16], int cmdu_copy_tlvs_linear(struct cmdu_buff *c, uint8_t *tlvs, uint32_t tlvslen); int cmdu_copy_tlvs(struct cmdu_buff *c, struct tlv *tv[], int tv_arrsize); +struct cmdu_buff *cmdu_clone(struct cmdu_buff *frm); struct tlv *cmdu_reserve_tlv(struct cmdu_buff *c, uint16_t tlv_datalen); int cmdu_put_tlv(struct cmdu_buff *c, struct tlv *t);