Exemple #1
0
int _ccnl_interest(int argc, char **argv)
{
    if (argc < 2) {
        _interest_usage(argv[0]);
        return -1;
    }

    if (argc > 2) {
        if (_intern_fib_add(argv[1], argv[2]) < 0) {
            _interest_usage(argv[0]);
            return -1;
        }
    }

    memset(_int_buf, '\0', BUF_SIZE);
    memset(_cont_buf, '\0', BUF_SIZE);
    for (int cnt = 0; cnt < CCNL_INTEREST_RETRIES; cnt++) {
        gnrc_netreg_entry_t _ne;
        /* register for content chunks */
        _ne.demux_ctx =  GNRC_NETREG_DEMUX_CTX_ALL;
        _ne.pid = sched_active_pid;
        gnrc_netreg_register(GNRC_NETTYPE_CCN_CHUNK, &_ne);

        ccnl_send_interest(CCNL_SUITE_NDNTLV, argv[1], NULL, _int_buf, BUF_SIZE);
        if (ccnl_wait_for_chunk(_cont_buf, BUF_SIZE, 0) > 0) {
            gnrc_netreg_unregister(GNRC_NETTYPE_CCN_CHUNK, &_ne);
            printf("Content received: %s\n", _cont_buf);
            return 0;
        }
        gnrc_netreg_unregister(GNRC_NETTYPE_CCN_CHUNK, &_ne);
    }
    printf("Timeout! No content received in response to the Interest for %s.\n", argv[1]);

    return -1;
}
Exemple #2
0
void conn_udp_close(conn_udp_t *conn)
{
    assert(conn->l4_type == GNRC_NETTYPE_UDP);
    if (conn->netreg_entry.pid != KERNEL_PID_UNDEF) {
        gnrc_netreg_unregister(GNRC_NETTYPE_UDP, &conn->netreg_entry);
    }
}
/* start sending beacons */
void beaconing_start(void)
{
    uint8_t remaining_beacons = DOW_BEACONING_COUNT;
    bool end_beaconing = false;

    /* register for RX */
    gnrc_netreg_entry_t _ne;
    _ne.target.pid = sched_active_pid;
    _ne.demux_ctx = GNRC_NETREG_DEMUX_CTX_ALL;
    gnrc_netreg_register(GNRC_NETTYPE_UNDEF, &_ne);
    /* schedule beacon */
    msg_t beacon_msg;
    beacon_msg.type = DOW_MSG_BEACON;
    xtimer_t beacon_timer;
    beacon_timer.target = beacon_timer.long_target = 0;
    /* let's delay the first beacon by DOW_BEACONING_WAIT */
    uint32_t tmp = DOW_BEACONING_PERIOD + DOW_BEACONING_WAIT;
    xtimer_set_msg(&beacon_timer, tmp, &beacon_msg, sched_active_pid);
    tmp -= DOW_BEACONING_WAIT;
    uint32_t start = xtimer_now().ticks32;
    while (1) {
        msg_t m;
        msg_receive(&m);
        switch (m.type) {
            case DOW_MSG_BEACON:
                LOG_DEBUG("beaconing: ready to send next beacon\n");
                beaconing_send();
                tmp = DOW_BEACONING_PERIOD;
                /* check if we need to do further beaconing */
                if (--remaining_beacons > 0) {
                    xtimer_set_msg(&beacon_timer, tmp, &beacon_msg, sched_active_pid);
                }
                else {
                    beacon_msg.type = DOW_MSG_BEACON_END;
                    tmp = 2 * DOW_BEACONING_WAIT - (xtimer_now().ticks32 - start);
                    LOG_INFO("beaconing: end beaconing period in %" PRIu32 \
                              " seconds\n", (tmp / US_PER_SEC));

                    xtimer_set_msg(&beacon_timer, tmp, &beacon_msg, sched_active_pid);
                }
                break;
            case DOW_MSG_BEACON_END:
                LOG_INFO("beaconing: end beaconing\n");
                end_beaconing = true;
                break;
            case GNRC_NETAPI_MSG_TYPE_RCV:
                LOG_DEBUG("beaconing: received packet, assume that it is a beacon\n");
                _handle_beacon((gnrc_pktsnip_t *)m.content.ptr);
                break;
            default:
                LOG_WARNING("beaconing: didn't expect message type %X\n", m.type);
                break;
        }
        if (end_beaconing) {
            break;
        }
    }
    gnrc_netreg_unregister(GNRC_NETTYPE_UNDEF, &_ne);
}
Exemple #4
0
gnrc_nettest_res_t gnrc_nettest_receive(kernel_pid_t pid, gnrc_pktsnip_t *in,
                                        unsigned int exp_pkts,
                                        const kernel_pid_t *exp_senders,
                                        const gnrc_pktsnip_t **exp_out,
                                        gnrc_nettype_t exp_type, uint32_t exp_demux_ctx)
{
    gnrc_netreg_entry_t reg_entry = { NULL, exp_demux_ctx, thread_getpid() };
    gnrc_nettest_res_t res;

    gnrc_netreg_register(exp_type, &reg_entry);

    res = _pkt_test(GNRC_NETAPI_MSG_TYPE_RCV, pid, in, exp_pkts, exp_senders,
                    exp_out);

    gnrc_netreg_unregister(exp_type, &reg_entry);

    return res;
}
Exemple #5
0
gnrc_nettest_res_t gnrc_nettest_receive(kernel_pid_t pid, gnrc_pktsnip_t *in,
                                        unsigned int exp_pkts,
                                        const kernel_pid_t *exp_senders,
                                        const gnrc_pktsnip_t **exp_out,
                                        gnrc_nettype_t exp_type, uint32_t exp_demux_ctx)
{
    gnrc_netreg_entry_t reg_entry = GNRC_NETREG_ENTRY_INIT_PID(exp_demux_ctx,
                                                               sched_active_pid);
    gnrc_nettest_res_t res;

    gnrc_netreg_register(exp_type, &reg_entry);

    res = _pkt_test(GNRC_NETAPI_MSG_TYPE_RCV, pid, in, exp_pkts, exp_senders,
                    exp_out);

    gnrc_netreg_unregister(exp_type, &reg_entry);

    return res;
}
Exemple #6
0
int _tftp_do_client_transfer(tftp_context_t *ctxt)
{
    msg_t msg;
    tftp_state ret = TS_BUSY;

    /* register our DNS response listener */
    gnrc_netreg_entry_t entry = GNRC_NETREG_ENTRY_INIT_PID(ctxt->src_port,
                                                           sched_active_pid);

    if (gnrc_netreg_register(GNRC_NETTYPE_UDP, &entry)) {
        DEBUG("tftp: error starting server.\n");
        return TS_FAILED;
    }

    /* try to start the TFTP transfer */
    ret = _tftp_state_processes(ctxt, NULL);
    if (ret != TS_BUSY) {
        DEBUG("tftp: transfer failed\n");
        /* if the start failed return */
        return ret;
    }

    /* main processing loop */
    while (ret == TS_BUSY) {
        /* wait for a message */
        msg_receive(&msg);
        DEBUG("tftp: message received\n");
        ret = _tftp_state_processes(ctxt, &msg);

        /* release packet if we received one */
        if (msg.type == GNRC_NETAPI_MSG_TYPE_RCV) {
            gnrc_pktbuf_release(msg.content.ptr);
        }
    }

    /* unregister our UDP listener on this thread */
    gnrc_netreg_unregister(GNRC_NETTYPE_UDP, &entry);

    return ret;
}
Exemple #7
0
int _tftp_server(tftp_context_t *ctxt)
{
    msg_t msg;
    bool active = true;
    gnrc_netreg_entry_t entry = GNRC_NETREG_ENTRY_INIT_PID(GNRC_TFTP_DEFAULT_DST_PORT,
                                                           sched_active_pid);

    while (active) {
        int ret = TS_BUSY;
        bool got_client = false;

        /* register the servers main listening port */
        if (gnrc_netreg_register(GNRC_NETTYPE_UDP, &entry)) {
            DEBUG("tftp: error starting server.\n");
            return TS_FAILED;
        }

        /* main processing loop */
        while (ret == TS_BUSY) {
            /* wait for a message */
            msg_receive(&msg);

            /* check if the server stop message has been received */
            if (msg.type == TFTP_STOP_SERVER_MSG) {
                active = false;
                ret = TS_FAILED;
                break;
            }
            else {
                /* continue normal server opration */
                DEBUG("tftp: message incoming\n");
                ret = _tftp_state_processes(ctxt, &msg);

                /* release packet if we received one */
                if (msg.type == GNRC_NETAPI_MSG_TYPE_RCV) {
                    gnrc_pktbuf_release(msg.content.ptr);
                }
            }

            /* if we just accepted a client, disable the server main listening port */
            if (!got_client) {
                gnrc_netreg_unregister(GNRC_NETTYPE_UDP, &entry);

                if (ret == TS_BUSY) {
                    got_client = true;
                    DEBUG("tftp: connection established\n");
                }
            }
        }

        /* remove any stall timers */
        xtimer_remove(&(ctxt->timer));

        /* if the server transfer has finished, unregister the client dst port */
        gnrc_netreg_unregister(GNRC_NETTYPE_UDP, &(ctxt->entry));
        DEBUG("tftp: connection terminated\n");
    }

    /* unregister our UDP listener on this thread */
    gnrc_netreg_unregister(GNRC_NETTYPE_UDP, &entry);

    return 0;
}
Exemple #8
0
/* tests receiving */
static int test_receive(void)
{
    ethernet_hdr_t *rcv_mac = (ethernet_hdr_t *)_tmp;
    uint8_t *rcv_payload = _tmp + sizeof(ethernet_hdr_t);
    gnrc_pktsnip_t *pkt, *hdr;
    gnrc_netreg_entry_t me = { NULL, GNRC_NETREG_DEMUX_CTX_ALL,
                               thread_getpid() };
    msg_t msg;

    if (_dev.netdev.event_callback == NULL) {
        puts("Device's event_callback not set");
        return 0;
    }
    /* prepare receive buffer */
    memcpy(rcv_mac->dst, _dev_addr, sizeof(_dev_addr));
    memcpy(rcv_mac->src, _test_src, sizeof(_test_src));
    /* no gnrc_ipv6 in compile unit => ETHERTYPE_IPV6 translates to
     * GNRC_NETTYPE_UNDEF */
    rcv_mac->type = byteorder_htons(ETHERTYPE_IPV6);
    memcpy(rcv_payload, _TEST_PAYLOAD2, sizeof(_TEST_PAYLOAD2) - 1);
    _tmp_len = sizeof(_TEST_PAYLOAD2) + sizeof(ethernet_hdr_t) - 1;

    /* register for GNRC_NETTYPE_UNDEF */
    gnrc_netreg_register(GNRC_NETTYPE_UNDEF, &me);
    /* fire ISR event */
    _dev.netdev.event_callback((netdev2_t *)&_dev.netdev, NETDEV2_EVENT_ISR);
    /* wait for packet from MAC layer*/
    msg_receive(&msg);
    /* check message */
    if (msg.sender_pid != _mac_pid) {
        puts("Unexpected sender of netapi receive message");
        return 0;
    }
    if (msg.type != GNRC_NETAPI_MSG_TYPE_RCV) {
        puts("Expected netapi receive message");
        return 0;
    }
    pkt = msg.content.ptr;
    /* check payload */
    if (pkt->size != _tmp_len - sizeof(ethernet_hdr_t)) {
        puts("Payload of unexpected size");
    }
    if ((pkt->type != GNRC_NETTYPE_UNDEF) ||
        (memcmp(pkt->data, _TEST_PAYLOAD2, pkt->size) != 0)) {
        puts("Unexpected payload");
        puts("===========================================================");
        puts("expected");
        puts("===========================================================");
        od_hex_dump(_TEST_PAYLOAD2, pkt->size, OD_WIDTH_DEFAULT);
        puts("===========================================================");
        puts("send data");
        puts("===========================================================");
        od_hex_dump(pkt->data, pkt->size, OD_WIDTH_DEFAULT);
        return 0;
    }
    hdr = pkt->next;
    /* check netif header */
    if ((hdr->type != GNRC_NETTYPE_NETIF) || (hdr->next != NULL) ||
        (hdr->size) != (sizeof(gnrc_netif_hdr_t) + (2 * ETHERNET_ADDR_LEN))) {
        puts("Malformed header received");
        return 0;
    }
    if (memcmp(gnrc_netif_hdr_get_src_addr(hdr->data), _test_src,
               ETHERNET_ADDR_LEN) != 0) {
        char addr_str[ETHERNET_ADDR_LEN * 3];
        puts("Unexpected source received");
        puts("=================");
        puts("expected");
        puts("=================");
        puts(gnrc_netif_addr_to_str(addr_str, sizeof(addr_str),
                                    _test_src,
                                    ETHERNET_ADDR_LEN));
        puts("=================");
        puts("received source");
        puts("=================");
        puts(gnrc_netif_addr_to_str(addr_str, sizeof(addr_str),
                                    gnrc_netif_hdr_get_src_addr(hdr->data),
                                    ETHERNET_ADDR_LEN));
        return 0;
    }
    if (memcmp(gnrc_netif_hdr_get_dst_addr(hdr->data), _dev_addr,
               ETHERNET_ADDR_LEN) != 0) {
        char addr_str[ETHERNET_ADDR_LEN * 3];
        puts("Unexpected destination received");
        puts("=================");
        puts("expected");
        puts("=================");
        puts(gnrc_netif_addr_to_str(addr_str, sizeof(addr_str),
                                    _dev_addr,
                                    ETHERNET_ADDR_LEN));
        puts("====================");
        puts("received destination");
        puts("====================");
        puts(gnrc_netif_addr_to_str(addr_str, sizeof(addr_str),
                                    gnrc_netif_hdr_get_dst_addr(hdr->data),
                                    ETHERNET_ADDR_LEN));
        return 0;
    }

    gnrc_pktbuf_release(pkt);
    gnrc_netreg_unregister(GNRC_NETTYPE_UNDEF, &me);
    return 1;
}
Exemple #9
0
void sock_ip_close(sock_ip_t *sock)
{
    assert(sock != NULL);
    gnrc_netreg_unregister(GNRC_NETTYPE_IPV6, &sock->reg.entry);
}