Skip to content

Commit

Permalink
fixup! ng_icmpv6_error: initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
miri64 committed Aug 3, 2015
1 parent fbbf7cc commit e900705
Showing 1 changed file with 17 additions and 44 deletions.
61 changes: 17 additions & 44 deletions sys/net/network_layer/ng_icmpv6/error/ng_icmpv6_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
#include "net/ng_icmpv6/error.h"
#include "net/ng_icmpv6.h"

/* all error messages are basically the same size and format */
#define ICMPV6_ERROR_SZ (sizeof(ng_icmpv6_error_dst_unr_t))
#define ICMPV6_ERROR_SET_VALUE(data, value) \
((ng_icmpv6_error_pkt_too_big_t *)(data))->mtu = byteorder_htonl(value);

static inline size_t _min(size_t a, size_t b)
{
Expand All @@ -27,20 +30,19 @@ static inline size_t _min(size_t a, size_t b)

static inline size_t _fit(ng_pktsnip_t *pkt)
{
/* TODO: replace NG_IPV6_NETIF_DEFAULT_MTU with known path MTU? */
return _min((ng_pkt_len(pkt) + ICMPV6_ERROR_SZ), NG_IPV6_NETIF_DEFAULT_MTU);
}

ng_pktsnip_t *ng_icmpv6_error_dst_unr_build(uint8_t code, ng_pktsnip_t *orig_pkt)
static ng_pktsnip_t *_icmpv6_error_build(uint8_t type, uint8_t code,
ng_pktsnip_t *orig_pkt, uint32_t value)
{
ng_pktsnip_t *pkt = ng_icmpv6_build(NULL, NG_ICMPV6_DST_UNR, code,
_fit(orig_pkt));
ng_pktsnip_t *pkt = ng_icmpv6_build(NULL, type, code, _fit(orig_pkt));

if (pkt != NULL) {
size_t offset = sizeof(ng_icmpv6_error_dst_unr_t);
size_t offset = ICMPV6_ERROR_SZ;
uint8_t *data = pkt->data;

((ng_icmpv6_error_dst_unr_t *)data)->unused.u32 = 0;

ICMPV6_ERROR_SET_VALUE(data, value);
while ((orig_pkt != NULL) && (offset < pkt->size)) {
memcpy(data + offset, orig_pkt->data,
_min(pkt->size - offset, orig_pkt->size));
Expand All @@ -52,48 +54,19 @@ ng_pktsnip_t *ng_icmpv6_error_dst_unr_build(uint8_t code, ng_pktsnip_t *orig_pkt
return pkt;
}

ng_pktsnip_t *ng_icmpv6_error_pkt_too_big_build(uint32_t mtu, ng_pktsnip_t *orig_pkt)
ng_pktsnip_t *ng_icmpv6_error_dst_unr_build(uint8_t code, ng_pktsnip_t *orig_pkt)
{
ng_pktsnip_t *pkt = ng_icmpv6_build(NULL, NG_ICMPV6_PKT_TOO_BIG, 0,
_fit(orig_pkt));

if (pkt != NULL) {
size_t offset = sizeof(ng_icmpv6_error_pkt_too_big_t);
uint8_t *data = pkt->data;

((ng_icmpv6_error_pkt_too_big_t *)data)->mtu = byteorder_htonl(mtu);

while ((orig_pkt != NULL) && (offset < pkt->size)) {
memcpy(data + offset, orig_pkt->data,
_min(pkt->size - offset, orig_pkt->size));
offset += _min(pkt->size - offset, orig_pkt->size);
orig_pkt = orig_pkt->next;
}
}
return _icmpv6_error_build(NG_ICMPV6_DST_UNR, code, orig_pkt, 0);
}

return pkt;
ng_pktsnip_t *ng_icmpv6_error_pkt_too_big_build(uint32_t mtu, ng_pktsnip_t *orig_pkt)
{
return _icmpv6_error_build(NG_ICMPV6_PKT_TOO_BIG, 0, orig_pkt, mtu);
}

ng_pktsnip_t *ng_icmpv6_error_time_exc_build(uint8_t code, ng_pktsnip_t *orig_pkt)
{
ng_pktsnip_t *pkt = ng_icmpv6_build(NULL, NG_ICMPV6_TIME_EXC, code,
_fit(orig_pkt));

if (pkt != NULL) {
size_t offset = sizeof(ng_icmpv6_error_time_exc_t);
uint8_t *data = pkt->data;

((ng_icmpv6_error_time_exc_t *)data)->unused.u32 = 0;

while ((orig_pkt != NULL) && (offset < pkt->size)) {
memcpy(data + offset, orig_pkt->data,
_min(pkt->size - offset, orig_pkt->size));
offset += _min(pkt->size - offset, orig_pkt->size);
orig_pkt = orig_pkt->next;
}
}

return pkt;
return _icmpv6_error_build(NG_ICMPV6_TIME_EXC, code, orig_pkt, 0);
}

static inline bool _in_range(uint8_t *ptr, uint8_t *start, size_t sz)
Expand All @@ -112,7 +85,7 @@ ng_pktsnip_t *ng_icmpv6_error_param_prob_build(uint8_t code, void *ptr, ng_pktsn
uint32_t ptr_offset = 0U;
bool found_offset = false;

while ((orig_pkt != NULL)) {
while (orig_pkt != NULL) {
if (offset < pkt->size) {
memcpy(data + offset, orig_pkt->data,
_min(pkt->size - offset, orig_pkt->size));
Expand Down

0 comments on commit e900705

Please sign in to comment.