예제 #1
0
static void test_pktbuf_realloc_data__further_down_the_line(void)
{
    ng_pktsnip_t *pkt1, *pkt2, *header;
    void *exp_data;

    pkt1 = ng_pktbuf_add(NULL, TEST_STRING16, sizeof(TEST_STRING16), NG_NETTYPE_UNDEF);
    exp_data = pkt1->data;

    TEST_ASSERT_NOT_NULL(pkt1);

    header = ng_pktbuf_add(pkt1, pkt1->data, 4, NG_NETTYPE_UNDEF);
    pkt2 = ng_pktbuf_add(NULL, TEST_STRING16, sizeof(TEST_STRING16), NG_NETTYPE_UNDEF);

    TEST_ASSERT_NOT_NULL(header);
    TEST_ASSERT(header == pkt1->next);
    TEST_ASSERT_EQUAL_INT(4, header->size);
    TEST_ASSERT(((uint8_t *)pkt1->data) == (((uint8_t *)header->data) + 4));
    TEST_ASSERT_EQUAL_INT(sizeof(TEST_STRING16) - 4, pkt1->size);

    TEST_ASSERT_EQUAL_INT(0, ng_pktbuf_realloc_data(header, 40));
    TEST_ASSERT(exp_data != header->data);
    TEST_ASSERT_NULL(header->next);
    TEST_ASSERT_EQUAL_STRING(TEST_STRING16, header->data);
    TEST_ASSERT_EQUAL_INT(40, header->size);
    TEST_ASSERT_EQUAL_INT(NG_NETTYPE_UNDEF, header->type);
    TEST_ASSERT_EQUAL_INT(1, header->users);
    ng_pktbuf_release(pkt1);
    ng_pktbuf_release(pkt2);
    TEST_ASSERT(ng_pktbuf_is_empty());
}
예제 #2
0
파일: ng_udp.c 프로젝트: anishkt/RIOT
static void _send(ng_pktsnip_t *pkt)
{
    ng_udp_hdr_t *hdr;
    ng_pktsnip_t *udp_snip;
    ng_netreg_entry_t *sendto;

    /* get udp snip and hdr */
    LL_SEARCH_SCALAR(pkt, udp_snip, type, NG_NETTYPE_UDP);
    udp_snip = ng_pktbuf_start_write(udp_snip);
    if (udp_snip == NULL) {
        DEBUG("udp: cannot send packet: unable to allocate packet\n");
        ng_pktbuf_release(pkt);
        return;
    }
    hdr = (ng_udp_hdr_t *)udp_snip->data;
    /* fill in size field */
    hdr->length = byteorder_htons(ng_pkt_len(udp_snip));

    /* and forward packet to the network layer */
    sendto = ng_netreg_lookup(pkt->type, NG_NETREG_DEMUX_CTX_ALL);
    /* throw away packet if no one is interested */
    if (sendto == NULL) {
        DEBUG("udp: cannot send packet: network layer not found\n");
        ng_pktbuf_release(pkt);
        return;
    }
    /* send packet to network layer */
    ng_pktbuf_hold(pkt, ng_netreg_num(pkt->type, NG_NETREG_DEMUX_CTX_ALL) - 1);
    while (sendto != NULL) {
        ng_netapi_send(sendto->pid, pkt);
        sendto = ng_netreg_getnext(sendto);
    }
}
예제 #3
0
void ng_sixlowpan_frag_handle_pkt(ng_pktsnip_t *pkt)
{
    ng_netif_hdr_t *hdr = pkt->next->data;
    ng_sixlowpan_frag_t *frag = pkt->data;
    uint16_t offset = 0;
    size_t frag_size;

    switch (frag->disp_size.u8[0] & NG_SIXLOWPAN_FRAG_DISP_MASK) {
        case NG_SIXLOWPAN_FRAG_1_DISP:
            frag_size = (pkt->size - sizeof(ng_sixlowpan_frag_t));
            break;

        case NG_SIXLOWPAN_FRAG_N_DISP:
            offset = (((ng_sixlowpan_frag_n_t *)frag)->offset * 8);
            frag_size = (pkt->size - sizeof(ng_sixlowpan_frag_n_t));
            break;

        default:
            DEBUG("6lo rbuf: Not a fragment header.\n");
            ng_pktbuf_release(pkt);

            return;
    }

    rbuf_add(hdr, frag, frag_size, offset);

    ng_pktbuf_release(pkt);
}
예제 #4
0
static void _rx_event(ng_netdev_eth_t *netdev)
{
    dev_eth_t *dev = netdev->ethdev;
    int nread = dev->driver->recv(dev, (char*)recv_buffer, ETHERNET_MAX_LEN);

    DEBUG("ng_netdev_eth: read %d bytes\n", nread);

    if (nread > 0) {
        ethernet_hdr_t *hdr = (ethernet_hdr_t *)recv_buffer;
        ng_pktsnip_t *netif_hdr, *pkt;
        ng_nettype_t receive_type = NG_NETTYPE_UNDEF;
        size_t data_len = (nread - sizeof(ethernet_hdr_t));

        /* TODO: implement multicast groups? */

        netif_hdr = ng_pktbuf_add(NULL, NULL,
                                  sizeof(ng_netif_hdr_t) + (2 * ETHERNET_ADDR_LEN),
                                  NG_NETTYPE_NETIF);

        if (netif_hdr == NULL) {
            DEBUG("ng_netdev_eth: no space left in packet buffer\n");
            return;
        }

        ng_netif_hdr_init(netif_hdr->data, ETHERNET_ADDR_LEN, ETHERNET_ADDR_LEN);
        ng_netif_hdr_set_src_addr(netif_hdr->data, hdr->src, ETHERNET_ADDR_LEN);
        ng_netif_hdr_set_dst_addr(netif_hdr->data, hdr->dst, ETHERNET_ADDR_LEN);
        ((ng_netif_hdr_t *)netif_hdr->data)->if_pid = thread_getpid();

        receive_type = ng_nettype_from_ethertype(byteorder_ntohs(hdr->type));

        DEBUG("ng_netdev_eth: received packet from %02x:%02x:%02x:%02x:%02x:%02x "
              "of length %zu\n",
              hdr->src[0], hdr->src[1], hdr->src[2], hdr->src[3], hdr->src[4],
              hdr->src[5], data_len);
#if defined(MODULE_OD) && ENABLE_DEBUG
        od_hex_dump(hdr, nread, OD_WIDTH_DEFAULT);
#endif

        /* Mark netif header and payload for next layer */
        if ((pkt = ng_pktbuf_add(netif_hdr, recv_buffer + sizeof(ethernet_hdr_t),
                                 data_len, receive_type)) == NULL) {
            ng_pktbuf_release(netif_hdr);
            DEBUG("ng_netdev_eth: no space left in packet buffer\n");
            return;
        }

        if (netdev->event_cb != NULL) {
            netdev->event_cb(NETDEV_EVENT_RX_COMPLETE, pkt);
        }
        else {
            ng_pktbuf_release(pkt); /* netif_hdr is released automatically too */
        }
    }
    else {
        DEBUG("ng_netdev_eth: spurious _rx_event: %d\n", nread);
    }
}
예제 #5
0
파일: udp.c 프로젝트: PeterKietzmann/Demos
static void send(char *addr_str, char *port_str, char *data)
{
    uint8_t port[2];
    uint16_t tmp;
    ng_pktsnip_t *payload, *udp, *ip;
    ng_ipv6_addr_t addr;
    ng_netreg_entry_t *sendto;

    /* parse destination address */
    if (ng_ipv6_addr_from_str(&addr, addr_str) == NULL) {
        puts("Error: unable to parse destination address");
        return;
    }
    /* parse port */
    tmp = (uint16_t)atoi(port_str);
    if (tmp == 0) {
        puts("Error: unable to parse destination port");
        return;
    }
    port[0] = (uint8_t)tmp;
    port[1] = tmp >> 8;

    /* allocate payload */
    payload = ng_pktbuf_add(NULL, data, strlen(data), NG_NETTYPE_UNDEF);
    if (payload == NULL) {
        puts("Error: unable to copy data to packet buffer");
        return;
    }
    /* allocate UDP header, set source port := destination port */
    udp = ng_udp_hdr_build(payload, port, 2, port, 2);
    if (udp == NULL) {
        puts("Error: unable to allocate UDP header");
        ng_pktbuf_release(payload);
        return;
    }
    /* allocate IPv6 header */
    ip = ng_ipv6_hdr_build(udp, NULL, 0, (uint8_t *)&addr, sizeof(addr));
    if (ip == NULL) {
        puts("Error: unable to allocate IPv6 header");
        ng_pktbuf_release(udp);
        return;
    }
    /* send packet */
    sendto = ng_netreg_lookup(NG_NETTYPE_UDP, NG_NETREG_DEMUX_CTX_ALL);
    if (sendto == NULL) {
        puts("Error: unable to locate UDP thread");
        ng_pktbuf_release(ip);
        return;
    }
    ng_pktbuf_hold(ip, ng_netreg_num(NG_NETTYPE_UDP,
                                     NG_NETREG_DEMUX_CTX_ALL) - 1);
    while (sendto != NULL) {
        ng_netapi_send(sendto->pid, ip);
        sendto = ng_netreg_getnext(sendto);
    }
    printf("Success: send %i byte to %s:%u\n", payload->size, addr_str, tmp);
}
예제 #6
0
파일: ng_ipv6.c 프로젝트: jferreir/RIOT
static void _send(ng_pktsnip_t *pkt, bool prep_hdr)
{
    kernel_pid_t iface = KERNEL_PID_UNDEF;
    ng_pktsnip_t *ipv6, *payload;
    ng_ipv6_hdr_t *hdr;
    /* seize payload as temporary variable */
    payload = ng_pktbuf_start_write(pkt);

    if (payload == NULL) {
        DEBUG("ipv6: unable to get write access to packet, dropping packet\n");
        ng_pktbuf_release(pkt);
        return;
    }

    pkt = payload;  /* Reset pkt from temporary variable */

    /* get IPv6 snip and (if present) generic interface header */
    if (pkt->type == NG_NETTYPE_NETIF) {
        /* If there is already a netif header (routing protocols and
         * neighbor discovery might add them to preset sending interface) */
        iface = ((ng_netif_hdr_t *)pkt->data)->if_pid;
        ipv6 = pkt->next;
    }
    else {
        ipv6 = pkt;
    }

    hdr = ipv6->data;
    payload = ipv6->next;       /* TODO: parse extension headers */

    if (ng_ipv6_addr_is_multicast(&hdr->dst)) {
        _send_multicast(iface, pkt, ipv6, payload, prep_hdr);
    }
    else {
        uint8_t l2addr_len = NG_IPV6_NC_L2_ADDR_MAX;
        uint8_t l2addr[l2addr_len];

        iface = ng_ndp_next_hop_l2addr(l2addr, &l2addr_len, iface, &hdr->dst,
                                       pkt);

        if (iface == KERNEL_PID_UNDEF) {
            DEBUG("ipv6: error determining next hop's link layer address\n");
            ng_pktbuf_release(pkt);
            return;
        }

        if (prep_hdr) {
            if (_fill_ipv6_hdr(iface, ipv6, payload) < 0) {
                /* error on filling up header */
                ng_pktbuf_release(pkt);
                return;
            }
        }

        _send_unicast(iface, l2addr, l2addr_len, pkt);
    }
}
예제 #7
0
static void test_pktbuf_realloc_data__pkt_users_gt_1(void)
{
    ng_pktsnip_t *pkt = ng_pktbuf_add(NULL, NULL, sizeof(TEST_STRING8), NG_NETTYPE_UNDEF);
    ng_pktbuf_hold(pkt, 1);

    TEST_ASSERT_EQUAL_INT(EINVAL, ng_pktbuf_realloc_data(pkt, sizeof(TEST_STRING8) - 1));
    ng_pktbuf_release(pkt);
    ng_pktbuf_release(pkt);
    TEST_ASSERT(ng_pktbuf_is_empty());
}
예제 #8
0
파일: ng_slip.c 프로젝트: robby14/RIOT
/* SLIP receive handler */
static void _slip_receive(ng_slip_dev_t *dev, size_t bytes)
{
    ng_netif_hdr_t *hdr;
    ng_netreg_entry_t *sendto;
    ng_pktsnip_t *pkt, *netif_hdr;

    pkt = ng_pktbuf_add(NULL, NULL, bytes, NG_NETTYPE_UNDEF);

    if (pkt == NULL) {
        DEBUG("slip: no space left in packet buffer\n");
        return;
    }

    netif_hdr = ng_pktbuf_add(pkt, NULL, sizeof(ng_netif_hdr_t),
                              NG_NETTYPE_NETIF);

    if (netif_hdr == NULL) {
        DEBUG("slip: no space left in packet buffer\n");
        ng_pktbuf_release(pkt);
        return;
    }

    hdr = netif_hdr->data;
    ng_netif_hdr_init(hdr, 0, 0);
    hdr->if_pid = thread_getpid();

    if (ringbuffer_get(dev->in_buf, pkt->data, bytes) != bytes) {
        DEBUG("slip: could not read %zu bytes from ringbuffer\n", bytes);
        ng_pktbuf_release(pkt);
        return;
    }

#ifdef MODULE_NG_IPV6
    if ((pkt->size >= sizeof(ipv6_hdr_t)) && ipv6_hdr_is(pkt->data)) {
        pkt->type = NG_NETTYPE_IPV6;
    }
#endif

    sendto = ng_netreg_lookup(pkt->type, NG_NETREG_DEMUX_CTX_ALL);

    if (sendto == NULL) {
        DEBUG("slip: unable to forward packet of type %i\n", pkt->type);
        ng_pktbuf_release(pkt);
    }

    ng_pktbuf_hold(pkt, ng_netreg_num(pkt->type, NG_NETREG_DEMUX_CTX_ALL) - 1);

    while (sendto != NULL) {
        DEBUG("slip: sending pkt %p to PID %u\n", pkt, sendto->pid);
        ng_netapi_receive(sendto->pid, pkt);
        sendto = ng_netreg_getnext(sendto);
    }
}
예제 #9
0
파일: ng_udp.c 프로젝트: phiros/testci
static void _receive(ng_pktsnip_t *pkt)
{
    ng_pktsnip_t *udp, *ipv6;
    ng_udp_hdr_t *hdr;
    uint32_t port;
    ng_netreg_entry_t *sendto;

    /* mark UDP header */
    udp = ng_pktbuf_start_write(pkt);
    if (udp == NULL) {
        DEBUG("udp: unable to get write access to packet\n");
        ng_pktbuf_release(pkt);
        return;
    }
    pkt = udp;
    udp = ng_pktbuf_add(pkt, pkt->data, sizeof(ng_udp_hdr_t), NG_NETTYPE_UDP);
    if (udp == NULL) {
        DEBUG("udp: error marking UDP header, dropping packet\n");
        ng_pktbuf_release(pkt);
        return;
    }
    /* mark payload as Type: UNDEF */
    pkt->type = NG_NETTYPE_UNDEF;
    /* get explicit pointer to UDP header */
    hdr = (ng_udp_hdr_t *)udp->data;

    LL_SEARCH_SCALAR(pkt, ipv6, type, NG_NETTYPE_IPV6);

    /* validate checksum */
    if (_calc_csum(udp, ipv6, pkt)) {
        DEBUG("udp: received packet with invalid checksum, dropping it\n");
        ng_pktbuf_release(pkt);
        return;
    }

    /* get port (netreg demux context) */
    port = (uint32_t)byteorder_ntohs(hdr->dst_port);

    /* send payload to receivers */
    sendto = ng_netreg_lookup(NG_NETTYPE_UDP, port);
    if (sendto == NULL) {
        DEBUG("udp: unable to forward packet as no one is interested in it\n");
        ng_pktbuf_release(pkt);
        return;
    }
    ng_pktbuf_hold(pkt, ng_netreg_num(NG_NETTYPE_UDP, port) - 1);
    while (sendto != NULL) {
        ng_netapi_receive(sendto->pid, pkt);
        sendto = ng_netreg_getnext(sendto);
    }
}
예제 #10
0
파일: ng_ipv6.c 프로젝트: jferreir/RIOT
void ng_ipv6_demux(kernel_pid_t iface, ng_pktsnip_t *pkt, uint8_t nh)
{
    int receiver_num;

    pkt->type = ng_nettype_from_protnum(nh);

    switch (nh) {
        case NG_PROTNUM_ICMPV6:
            DEBUG("ipv6: handle ICMPv6 packet (nh = %" PRIu8 ")\n", nh);
            ng_icmpv6_demux(iface, pkt);
            break;
#ifdef MODULE_NG_IPV6_EXT
        case NG_PROTNUM_IPV6_EXT_HOPOPT:
        case NG_PROTNUM_IPV6_EXT_DST:
        case NG_PROTNUM_IPV6_EXT_RH:
        case NG_PROTNUM_IPV6_EXT_FRAG:
        case NG_PROTNUM_IPV6_EXT_AH:
        case NG_PROTNUM_IPV6_EXT_ESP:
        case NG_PROTNUM_IPV6_EXT_MOB:
            DEBUG("ipv6: handle extension header (nh = %" PRIu8 ")\n", nh);
            if (!ng_ipv6_ext_demux(iface, pkt, nh)) {
                DEBUG("ipv6: unable to parse extension headers.\n");
                ng_pktbuf_release(pkt);
                return;
            }
#endif
        case NG_PROTNUM_IPV6:
            DEBUG("ipv6: handle encapsulated IPv6 packet (nh = %" PRIu8 ")\n", nh);
            _decapsulate(pkt);
            break;
        default:
            break;
    }

    DEBUG("ipv6: forward nh = %" PRIu8 " to other threads\n", nh);
    receiver_num = ng_netreg_num(pkt->type, NG_NETREG_DEMUX_CTX_ALL) +
                   ng_netreg_num(NG_NETTYPE_IPV6, nh);

    if (receiver_num == 0) {
        DEBUG("ipv6: unable to forward packet as no one is interested in it\n");
        ng_pktbuf_release(pkt);
        return;
    }

    ng_pktbuf_hold(pkt, receiver_num - 1);
    /* IPv6 is not interested anymore so `- 1` */
    _dispatch_rcv_pkt(pkt->type, NG_NETREG_DEMUX_CTX_ALL, pkt);
    _dispatch_rcv_pkt(NG_NETTYPE_IPV6, nh, pkt);
}
예제 #11
0
파일: ng_nomac.c 프로젝트: very3b/RIOT
/**
 * @brief   Function called by the device driver on device events
 *
 * @param[in] event         type of event
 * @param[in] data          optional parameter
 */
static void _event_cb(ng_netdev_event_t event, void *data)
{
    DEBUG("nomac: event triggered -> %i\n", event);
    /* NOMAC only understands the RX_COMPLETE event... */
    if (event == NETDEV_EVENT_RX_COMPLETE) {
        ng_pktsnip_t *pkt;
        ng_netreg_entry_t *sendto;

        /* get pointer to the received packet */
        pkt = (ng_pktsnip_t *)data;
        /* find out, who to send the packet to */
        sendto = ng_netreg_lookup(pkt->type, NG_NETREG_DEMUX_CTX_ALL);
        /* throw away packet if no one is interested */
        if (sendto == NULL) {
            DEBUG("nomac: unable to forward packet of type %i\n", pkt->type);
            ng_pktbuf_release(pkt);
            return;
        }
        /* send the packet to everyone interested in it's type */
        ng_pktbuf_hold(pkt, ng_netreg_num(pkt->type, NG_NETREG_DEMUX_CTX_ALL) - 1);
        while (sendto != NULL) {
            DEBUG("nomac: sending pkt %p to PID %u\n", (void*)pkt, sendto->pid);
            ng_netapi_receive(sendto->pid, pkt);
            sendto = ng_netreg_getnext(sendto);
        }
    }
}
예제 #12
0
static ng_pktsnip_t *_build_frag_pkt(ng_pktsnip_t *pkt, size_t payload_len,
                                     size_t size)
{
    ng_netif_hdr_t *hdr = pkt->data, *new_hdr;
    ng_pktsnip_t *netif, *frag;

    netif = ng_netif_hdr_build(ng_netif_hdr_get_src_addr(hdr), hdr->src_l2addr_len,
                               ng_netif_hdr_get_dst_addr(hdr), hdr->dst_l2addr_len);

    if (netif == NULL) {
        DEBUG("6lo frag: error allocating new link-layer header\n");
        return NULL;
    }

    new_hdr = netif->data;
    new_hdr->if_pid = hdr->if_pid;
    new_hdr->flags = hdr->flags;
    new_hdr->rssi = hdr->rssi;
    new_hdr->lqi = hdr->lqi;

    frag = ng_pktbuf_add(NULL, NULL, _min(size, payload_len),
                         NG_NETTYPE_SIXLOWPAN);

    if (frag == NULL) {
        DEBUG("6lo frag: error allocating first fragment\n");
        ng_pktbuf_release(netif);
        return NULL;
    }

    LL_PREPEND(frag, netif);

    return frag;
}
예제 #13
0
파일: ng_ipv6.c 프로젝트: jferreir/RIOT
/* functions for sending */
static void _send_unicast(kernel_pid_t iface, uint8_t *dst_l2addr,
                          uint16_t dst_l2addr_len, ng_pktsnip_t *pkt)
{
    ng_pktsnip_t *netif;

    if (pkt->type == NG_NETTYPE_NETIF) {
        /* great: someone already added a netif_hdr_t we assume it's wrong
         * to keep it simple
         * XXX: alternative would be to check if ng_netif_hdr_t::dst_l2addr_len
         * is long enough and only then to throw away the header. This causes
         * to much overhead IMHO */
        DEBUG("ipv6: removed old interface header\n");
        pkt = ng_pktbuf_remove_snip(pkt, pkt);
    }

    DEBUG("ipv6: add to interface header to packet\n");
    netif = ng_netif_hdr_build(NULL, 0, dst_l2addr, dst_l2addr_len);

    if (netif == NULL) {
        DEBUG("ipv6: error on interface header allocation, dropping packet\n");
        ng_pktbuf_release(pkt);
        return;
    }

    /* add netif to front of the pkt list */
    LL_PREPEND(pkt, netif);

    DEBUG("ipv6: send unicast over interface %" PRIkernel_pid "\n", iface);
    /* and send to interface */
    _send_to_iface(iface, pkt);
}
예제 #14
0
파일: ng_ipv6.c 프로젝트: mryndzionek/RIOT
void ng_ipv6_demux(kernel_pid_t iface, ng_pktsnip_t *pkt, uint8_t nh)
{
    int receiver_num;

    pkt->type = ng_nettype_from_protnum(nh);

    switch (nh) {
        case NG_PROTNUM_ICMPV6:
            ng_icmpv6_demux(iface, pkt);
            break;
        /* TODO: add extension header handling */
        default:
            break;
    }

    receiver_num = ng_netreg_num(pkt->type, NG_NETREG_DEMUX_CTX_ALL) +
                   ng_netreg_num(NG_NETTYPE_IPV6, nh);

    if (receiver_num == 0) {
        DEBUG("ipv6: unable to forward packet as no one is interested in it\n");
        ng_pktbuf_release(pkt);
        return;
    }

    ng_pktbuf_hold(pkt, receiver_num - 1);
    /* IPv6 is not interested anymore so `- 1` */
    _dispatch_rcv_pkt(pkt->type, NG_NETREG_DEMUX_CTX_ALL, pkt);
    _dispatch_rcv_pkt(NG_NETTYPE_IPV6, nh, pkt);
}
예제 #15
0
static void test_pktbuf_release__pkt_external(void)
{
    ng_pktsnip_t pkt = { 1, NULL, TEST_STRING8, sizeof(TEST_STRING8), NG_NETTYPE_UNDEF };

    ng_pktbuf_release(&pkt);
    TEST_ASSERT(ng_pktbuf_is_empty());
}
예제 #16
0
static void test_pktbuf_realloc_data__nomemenough(void)
{
    ng_pktsnip_t *pkt1, *pkt2;
    void *exp_data;

    pkt1 = ng_pktbuf_add(NULL, TEST_STRING8, sizeof(TEST_STRING8), NG_NETTYPE_UNDEF);
    exp_data = pkt1->data;

    TEST_ASSERT_NOT_NULL(pkt1);

    pkt2 = ng_pktbuf_add(NULL, NULL, 1, NG_NETTYPE_UNDEF);

    TEST_ASSERT_NOT_NULL(pkt2);
    TEST_ASSERT_NOT_NULL(ng_pktbuf_add(NULL, NULL, 4, NG_NETTYPE_UNDEF));

    ng_pktbuf_release(pkt2);

    TEST_ASSERT_EQUAL_INT(0, ng_pktbuf_realloc_data(pkt1, 200));
    TEST_ASSERT(exp_data != pkt1->data);
    TEST_ASSERT_NULL(pkt1->next);
    TEST_ASSERT_EQUAL_INT(200, pkt1->size);
    TEST_ASSERT_EQUAL_STRING(TEST_STRING8, pkt1->data);
    TEST_ASSERT_EQUAL_INT(NG_NETTYPE_UNDEF, pkt1->type);
    TEST_ASSERT_EQUAL_INT(1, pkt1->users);
}
예제 #17
0
static void test_pktbuf_realloc_data__size_0(void)
{
    ng_pktsnip_t *pkt = ng_pktbuf_add(NULL, NULL, sizeof(TEST_STRING8), NG_NETTYPE_UNDEF);

    TEST_ASSERT_EQUAL_INT(ENOMEM, ng_pktbuf_realloc_data(pkt, 0));
    ng_pktbuf_release(pkt);
    TEST_ASSERT(ng_pktbuf_is_empty());
}
예제 #18
0
파일: tests-pktbuf.c 프로젝트: robby14/RIOT
static void test_pktbuf_realloc_data__memfull(void)
{
    ng_pktsnip_t *pkt = ng_pktbuf_add(NULL, NULL, sizeof(TEST_STRING8), NG_NETTYPE_TEST);

    TEST_ASSERT_EQUAL_INT(ENOMEM, ng_pktbuf_realloc_data(pkt, NG_PKTBUF_SIZE + 1));
    ng_pktbuf_release(pkt);
    TEST_ASSERT(ng_pktbuf_is_empty());
}
예제 #19
0
파일: tests-pktbuf.c 프로젝트: robby14/RIOT
static void test_pktbuf_mark__success_aligned(void)
{
    uint8_t *data = (uint8_t *)(TEST_STRING16);
    ng_pktsnip_t *pkt1 = ng_pktbuf_add(NULL, data, sizeof(TEST_STRING16),
                                       NG_NETTYPE_TEST);
    ng_pktsnip_t *pkt2;
    uint8_t exp_data1[sizeof(TEST_STRING16) - 8];
    uint8_t exp_data2[8];

    memcpy(exp_data1, data + sizeof(exp_data2), sizeof(exp_data1));
    memcpy(exp_data2, data, sizeof(exp_data2));

    TEST_ASSERT_NOT_NULL(pkt1);
    TEST_ASSERT_NOT_NULL((pkt2 = ng_pktbuf_mark(pkt1, 8, NG_NETTYPE_UNDEF)));
    TEST_ASSERT(ng_pktbuf_is_sane());
    TEST_ASSERT(pkt1->next == pkt2);
    TEST_ASSERT_NOT_NULL(pkt1->data);
    TEST_ASSERT_EQUAL_INT(0, memcmp(exp_data1, pkt1->data, pkt1->size));
    TEST_ASSERT_EQUAL_INT(sizeof(TEST_STRING16) - 8,
                          pkt1->size);
    TEST_ASSERT_EQUAL_INT(NG_NETTYPE_TEST, pkt1->type);
    TEST_ASSERT_EQUAL_INT(1, pkt1->users);
    TEST_ASSERT_NULL(pkt2->next);
    TEST_ASSERT_NOT_NULL(pkt2->data);
    TEST_ASSERT_EQUAL_INT(0, memcmp(exp_data2, pkt2->data, pkt2->size));
    TEST_ASSERT_EQUAL_INT(8, pkt2->size);
    TEST_ASSERT_EQUAL_INT(NG_NETTYPE_UNDEF, pkt2->type);
    TEST_ASSERT_EQUAL_INT(1, pkt2->users);

    /* check if slightly larger packet would override data */
    ng_pktbuf_remove_snip(pkt1, pkt2);
    pkt2 = ng_pktbuf_add(NULL, TEST_STRING12, 12, NG_NETTYPE_TEST);
    TEST_ASSERT(ng_pktbuf_is_sane());
    TEST_ASSERT_NOT_NULL(pkt1->data);
    TEST_ASSERT_EQUAL_INT(0, memcmp(exp_data1, pkt1->data, pkt1->size));
    TEST_ASSERT_EQUAL_INT(sizeof(TEST_STRING16) - 8,
                          pkt1->size);
    TEST_ASSERT_EQUAL_INT(NG_NETTYPE_TEST, pkt1->type);
    TEST_ASSERT_EQUAL_INT(1, pkt1->users);

    /* check if everything can be cleaned up */
    ng_pktbuf_release(pkt1);
    ng_pktbuf_release(pkt2);
    TEST_ASSERT(ng_pktbuf_is_empty());
}
예제 #20
0
static void test_pktbuf_realloc_data__pkt_next_neq_NULL(void)
{
    ng_pktsnip_t *pkt = ng_pktbuf_add(NULL, NULL, sizeof(TEST_STRING8), NG_NETTYPE_UNDEF);

    TEST_ASSERT_NOT_NULL(ng_pktbuf_add(pkt, pkt->data, sizeof(TEST_STRING4), NG_NETTYPE_UNDEF));
    TEST_ASSERT_EQUAL_INT(EINVAL, ng_pktbuf_realloc_data(pkt, sizeof(TEST_STRING8) - 1));
    ng_pktbuf_release(pkt);
    TEST_ASSERT(ng_pktbuf_is_empty());
}
예제 #21
0
파일: tests-pktbuf.c 프로젝트: robby14/RIOT
static void test_pktbuf_add__unaligned_in_aligned_hole(void)
{
    ng_pktsnip_t *pkt1 = ng_pktbuf_add(NULL, NULL, 8, NG_NETTYPE_TEST);
    ng_pktsnip_t *pkt2 = ng_pktbuf_add(NULL, NULL, 8, NG_NETTYPE_TEST);
    ng_pktsnip_t *pkt3 = ng_pktbuf_add(NULL, NULL, 8, NG_NETTYPE_TEST);
    ng_pktsnip_t *pkt4;
    void *tmp_data2 = pkt2->data;

    ng_pktbuf_release(pkt2);
    pkt4 = ng_pktbuf_add(NULL, TEST_STRING12, 9, NG_NETTYPE_TEST);

    TEST_ASSERT(tmp_data2 != pkt4->data);

    ng_pktbuf_release(pkt1);
    ng_pktbuf_release(pkt3);
    ng_pktbuf_release(pkt4);
    TEST_ASSERT(ng_pktbuf_is_empty());
}
예제 #22
0
파일: main.c 프로젝트: PeterKietzmann/Demos
static void udp_send(const char *str)
{
    uint8_t data[20];
    ng_pktsnip_t *payload, *udp, *ip;
    ng_netreg_entry_t *sendto;

    memcpy(data, str, strlen(str));

    /* allocate payload */
    payload = ng_pktbuf_add(NULL, data, strlen(str), NG_NETTYPE_UNDEF);
    if (payload == NULL) {
        puts("Error: unable to copy data to packet buffer");
        return;
    }
    /* allocate UDP header, set source port := destination port */
    udp = ng_udp_hdr_build(payload, port, 2, port, 2);
    if (udp == NULL) {
        puts("Error: unable to allocate UDP header");
        ng_pktbuf_release(payload);
        return;
    }
    /* allocate IPv6 header */
    ip = ng_ipv6_hdr_build(udp, NULL, 0, (uint8_t *)&addr, sizeof(addr));
    if (ip == NULL) {
        puts("Error: unable to allocate IPv6 header");
        ng_pktbuf_release(udp);
        return;
    }
    /* send packet */
    sendto = ng_netreg_lookup(NG_NETTYPE_UDP, NG_NETREG_DEMUX_CTX_ALL);
    if (sendto == NULL) {
        puts("Error: unable to locate UDP thread");
        ng_pktbuf_release(ip);
        return;
    }
    ng_pktbuf_hold(ip, ng_netreg_num(NG_NETTYPE_UDP,
                                     NG_NETREG_DEMUX_CTX_ALL) - 1);
    while (sendto != NULL) {
        ng_netapi_send(sendto->pid, ip);
        sendto = ng_netreg_getnext(sendto);
    }
    printf("Send %s\n", str);
}
예제 #23
0
파일: tests-pktbuf.c 프로젝트: robby14/RIOT
static void test_pktbuf_start_write__pkt_users_2(void)
{
    ng_pktsnip_t *pkt_copy, *pkt = ng_pktbuf_add(NULL, TEST_STRING16, sizeof(TEST_STRING16),
                                   NG_NETTYPE_TEST);

    ng_pktbuf_hold(pkt, 1);
    TEST_ASSERT_NOT_NULL((pkt_copy = ng_pktbuf_start_write(pkt)));
    TEST_ASSERT(pkt != pkt_copy);
    TEST_ASSERT(pkt->next == pkt_copy->next);
    TEST_ASSERT_EQUAL_STRING(pkt->data, pkt_copy->data);
    TEST_ASSERT_EQUAL_INT(pkt->size, pkt_copy->size);
    TEST_ASSERT_EQUAL_INT(pkt->type, pkt_copy->type);
    TEST_ASSERT_EQUAL_INT(pkt->users, pkt_copy->users);
    TEST_ASSERT_EQUAL_INT(1, pkt->users);

    ng_pktbuf_release(pkt_copy);
    ng_pktbuf_release(pkt);
    TEST_ASSERT(ng_pktbuf_is_empty());
}
예제 #24
0
파일: tests-pktbuf.c 프로젝트: robby14/RIOT
static void test_pktbuf_start_write__pkt_users_1(void)
{
    ng_pktsnip_t *pkt_copy, *pkt = ng_pktbuf_add(NULL, TEST_STRING16, sizeof(TEST_STRING16),
                                   NG_NETTYPE_TEST);

    TEST_ASSERT_NOT_NULL((pkt_copy = ng_pktbuf_start_write(pkt)));
    TEST_ASSERT(pkt == pkt_copy);
    ng_pktbuf_release(pkt);
    TEST_ASSERT(ng_pktbuf_is_empty());
}
예제 #25
0
static void test_pktbuf_add__in_place(void)
{
    ng_pktsnip_t *pkt = ng_pktbuf_add(NULL, TEST_STRING16, sizeof(TEST_STRING16),
                                      NG_NETTYPE_UNDEF);
    ng_pktsnip_t *header;

    TEST_ASSERT_NOT_NULL((header = ng_pktbuf_add(pkt, pkt->data, 4, NG_NETTYPE_UNDEF)));
    TEST_ASSERT(header == pkt->next);
    TEST_ASSERT_EQUAL_STRING(TEST_STRING16, header->data); /* there is no 0 byte */
    TEST_ASSERT_EQUAL_INT(4, header->size);
    TEST_ASSERT_EQUAL_INT(NG_NETTYPE_UNDEF, header->type);
    TEST_ASSERT_EQUAL_INT(1, header->users);
    TEST_ASSERT_EQUAL_STRING(TEST_STRING16 + 4, pkt->data);
    TEST_ASSERT_EQUAL_INT(sizeof(TEST_STRING16) - 4, pkt->size);
    TEST_ASSERT_EQUAL_INT(NG_NETTYPE_UNDEF, pkt->type);
    TEST_ASSERT_EQUAL_INT(1, pkt->users);
    ng_pktbuf_release(header);
    ng_pktbuf_release(pkt);
    TEST_ASSERT(ng_pktbuf_is_empty());
}
예제 #26
0
static void test_pktbuf_realloc_data__pkt_data_wrong(void)
{
    ng_pktsnip_t *pkt = ng_pktbuf_add(NULL, NULL, sizeof(TEST_STRING8), NG_NETTYPE_UNDEF);
    void *orig_data = pkt->data;
    pkt->data = TEST_STRING8;

    TEST_ASSERT_EQUAL_INT(ENOENT, ng_pktbuf_realloc_data(pkt, 0));
    pkt->data = orig_data;
    ng_pktbuf_release(pkt);
    TEST_ASSERT(ng_pktbuf_is_empty());
}
예제 #27
0
static int _send_data(ng_netdev_t *netdev, ng_pktsnip_t *pkt)
{
    int nsent, to_send;
    ng_netdev_eth_t *dev = (ng_netdev_eth_t *)netdev;

    DEBUG("ng_netdev_eth: send data ");

    if (pkt == NULL) {
        return -EFAULT;
    }

    if ((dev == NULL) || (netdev->driver != &ng_netdev_eth_driver)) {
        DEBUG("[wrong device descriptor]\n");
        ng_pktbuf_release(pkt);
        return -ENODEV;
    }

    DEBUG("\n");

    to_send = _marshall_ethernet(dev, send_buffer, pkt);
    ng_pktbuf_release(pkt);

    if (to_send < 0) {
        errno = -to_send;
        DEBUG("marshall\n");
        return to_send;
    }

    DEBUG("ng_netdev_eth: send %d bytes\n", to_send);
#if defined(MODULE_OD) && ENABLE_DEBUG
    od_hex_dump(send_buffer, to_send, OD_WIDTH_DEFAULT);
#endif

    dev_eth_t *ethdev = dev->ethdev;
    if ((nsent = ethdev->driver->send(ethdev, (char*)send_buffer, to_send)) < 0) {
        DEBUG("write\n");
        return -EIO;
    }

    return nsent;
}
예제 #28
0
void ng_sixlowpan_frag_send(kernel_pid_t pid, ng_pktsnip_t *pkt,
                            size_t payload_len, size_t datagram_size)
{
    ng_sixlowpan_netif_t *iface = ng_sixlowpan_netif_get(pid);
    uint16_t offset = 0, res;

#if defined(DEVELHELP) && defined(ENABLE_DEBUG)
    if (iface == NULL) {
        DEBUG("6lo frag: iface == NULL, expect segmentation fault.\n");
        ng_pktbuf_release(pkt);
        return;
    }
#endif

    if ((res = _send_1st_fragment(iface, pkt, payload_len, datagram_size)) == 0) {
        /* error sending first fragment */
        DEBUG("6lo frag: error sending 1st fragment\n");
        ng_pktbuf_release(pkt);
        return;
    }

    offset += res;

    while (offset < datagram_size) {
        if ((res = _send_nth_fragment(iface, pkt, payload_len, datagram_size,
                                      offset)) == 0) {
            /* error sending subsequent fragment */
            DEBUG("6lo frag: error sending subsequent fragment (offset = %" PRIu16
                  ")\n", offset);
            ng_pktbuf_release(pkt);
            return;
        }

        offset += res;
    }

    /* remove original packet from packet buffer */
    ng_pktbuf_release(pkt);
    _tag++;
}
예제 #29
0
파일: tests-pktbuf.c 프로젝트: robby14/RIOT
static void test_pktbuf_release__success(void)
{
    ng_pktsnip_t *pkt = ng_pktbuf_add(NULL, TEST_STRING16, sizeof(TEST_STRING16), NG_NETTYPE_TEST);

    for (uint8_t i = 0; i < TEST_UINT8; i++) {
        uint8_t prev_users = pkt->users;
        ng_pktbuf_hold(pkt, 1);
        TEST_ASSERT_EQUAL_INT(prev_users + 1, pkt->users);
    }

    TEST_ASSERT(!ng_pktbuf_is_empty());

    for (uint8_t i = 0; i < TEST_UINT8; i++) {
        uint8_t prev_users = pkt->users;
        ng_pktbuf_release(pkt);
        TEST_ASSERT_EQUAL_INT(prev_users - 1, pkt->users);
    }

    TEST_ASSERT(!ng_pktbuf_is_empty());
    ng_pktbuf_release(pkt);
    TEST_ASSERT(ng_pktbuf_is_empty());
}
예제 #30
0
파일: ng_nettest.c 프로젝트: phiros/testci
static ng_nettest_res_t _pkt_test(uint16_t cmd_type, kernel_pid_t pid, ng_pktsnip_t *in,
                                  unsigned int exp_pkts, kernel_pid_t exp_senders[],
                                  ng_pktsnip_t *exp_out[])
{
    msg_t msg;
    timex_t t = { 0, NG_NETTEST_TIMEOUT };
    ng_nettest_res_t res = NG_NETTEST_SUCCESS;

    msg.type = cmd_type;
    msg.content.ptr = (char *)in;

    msg_send(&msg, pid);

    for (unsigned int i = 0; i < exp_pkts; i++) {
        ng_pktsnip_t *out;

        if ((vtimer_msg_receive_timeout(&msg, t) < 0) && res == NG_NETTEST_SUCCESS) {
            res = NG_NETTEST_TIMED_OUT;
        }

        if (msg.type != NG_NETAPI_MSG_TYPE_SND && (res == NG_NETTEST_SUCCESS)) {
            res = NG_NETTEST_WRONG_MSG;
        }

        if (msg.sender_pid != exp_senders[i] && (res == NG_NETTEST_SUCCESS)) {
            res = NG_NETTEST_WRONG_SENDER;
        }

        out = (ng_pktsnip_t *)msg.content.ptr;

        if ((out == NULL) && (res == NG_NETTEST_SUCCESS)) {
            res = NG_NETTEST_FAIL;
        }

        while (out) {
            if ((res == NG_NETTEST_SUCCESS) &&
                ((out->users != exp_out[i]->users) ||
                 (out->size != exp_out[i]->size) ||
                 (out->type != exp_out[i]->type) ||
                 (memcmp(out->data, exp_out[i]->data, out->size) != 0))) {
                res = NG_NETTEST_FAIL;
            }

            out = out->next;
        }

        ng_pktbuf_release((ng_pktsnip_t *)msg.content.ptr);
    }

    return res;
}