Exemplo n.º 1
0
/* UDP send command */
void sixlowapp_udp_send(ipv6_addr_t *dest, uint16_t port, char *payload, size_t len)
{
    int sock;
    sockaddr6_t sa;
    int bytes_sent;

    sock = socket_base_socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);

    if (-1 == sock) {
        printf("Error Creating Socket!");
        return;
    }
    memset(&sa, 0, sizeof(sa));
    sa.sin6_family = AF_INET;
    memcpy(&sa.sin6_addr, dest, 16);
    sa.sin6_port = HTONS(port);

    printf("Trying to send %i bytes to %s:%" PRIu16 "\n", len,
           ipv6_addr_to_str(addr_str, IPV6_MAX_ADDR_STR_LEN, dest), port);
    bytes_sent = socket_base_sendto(sock, payload, len, 0, &sa, sizeof(sa));

    if (bytes_sent < 0) {
        printf("Error sending packet!\n");
    }
    else {
        printf("Successful deliverd %i bytes over UDP to %s to 6LoWPAN\n",
               bytes_sent, ipv6_addr_to_str(addr_str, IPV6_MAX_ADDR_STR_LEN,
                                            dest));
    }

    socket_base_close(sock);
}
Exemplo n.º 2
0
Arquivo: udp.c Projeto: nandojve/RIOT
/* UDP send command */
void udp_send(int argc, char **argv)
{
    int sock;
    sockaddr6_t sa;
    ipv6_addr_t ipaddr;
    int bytes_sent;
    int address;
    char text[5];

    if (argc != 3) {
        printf("usage: send <addr> <text>\n");
        return;
    }

    address = atoi(argv[1]);

    strncpy(text, argv[2], sizeof(text));
    text[sizeof(text) - 1] = 0;

    sock = socket_base_socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);

    if (-1 == sock) {
        printf("Error Creating Socket!");
        return;
    }

    memset(&sa, 0, sizeof(sa));

    if (address) {
        ipv6_addr_init(&ipaddr, 0xabcd, 0x0, 0x0, 0x0, 0x0, 0x00ff, 0xfe00, (uint16_t)address);
    }
    else {
        ipv6_addr_set_all_nodes_addr(&ipaddr);
    }

    sa.sin6_family = AF_INET;
    memcpy(&sa.sin6_addr, &ipaddr, 16);
    sa.sin6_port = HTONS(SERVER_PORT);

    bytes_sent = socket_base_sendto(sock, (char *)text,
                                       strlen(text) + 1, 0, &sa,
                                       sizeof(sa));

    if (bytes_sent < 0) {
        printf("Error sending packet!\n");
    }
    else {
        printf("Successful deliverd %i bytes over UDP to %s to 6LoWPAN\n",
               bytes_sent, ipv6_addr_to_str(addr_str, IPV6_MAX_ADDR_STR_LEN,
                                            &ipaddr));
    }

    socket_base_close(sock);
}
Exemplo n.º 3
0
Arquivo: nhdp.c Projeto: cococolo/RIOT
kernel_pid_t nhdp_start(void)
{
    if (nhdp_pid == KERNEL_PID_UNDEF) {
        /* Init destination address for NHDP's packets */
        sa_bcast.sin6_family = AF_INET6;
        sa_bcast.sin6_port = HTONS(MANET_PORT);
        ipv6_addr_set_all_nodes_addr(&sa_bcast.sin6_addr);

        /* Configure sending/receiving UDP socket */
        sock_rcv = socket_base_socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);

        /* Start the NHDP thread */
        nhdp_pid = thread_create(nhdp_stack, sizeof(nhdp_stack), THREAD_PRIORITY_MAIN - 1,
                                 CREATE_STACKTEST, _nhdp_runner, NULL, "NHDP");
    }

    return nhdp_pid;
}
Exemplo n.º 4
0
/* UDP server thread */
void *sixlowapp_udp_server_loop(void *arg)
{
    (void) arg;

    sockaddr6_t sa;
    char buffer_main[UDP_BUFFER_SIZE];
    uint32_t fromlen;
    int sock;
    fromlen = sizeof(sa);

    while (1) {
        while(!sixlowapp_netcat_listen_port) {
            thread_sleep();
        }

        sock = socket_base_socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);

        memset(&sa, 0, sizeof(sa));
        sa.sin6_family = AF_INET;
        sa.sin6_port = HTONS(sixlowapp_netcat_listen_port);

        if (-1 == socket_base_bind(sock, &sa, sizeof(sa))) {
            printf("Error bind failed!\n");
            socket_base_close(sock);
            sixlowapp_netcat_listen_port = 0;
            continue;
        }

        printf("Listening for incoming UDP connection at port %" PRIu16 "\n", sixlowapp_netcat_listen_port);
        int32_t recsize = socket_base_recvfrom(sock, (void *)buffer_main, UDP_BUFFER_SIZE, 0, &sa, &fromlen);

        if (recsize < 0) {
            printf("ERROR: recsize < 0!\n");
        }

        printf("UDP packet received, payload: %s\n", buffer_main);

        socket_base_close(sock);
        sixlowapp_netcat_listen_port = 0;
    }

    return NULL;
}
Exemplo n.º 5
0
Arquivo: udp.c Projeto: barryzxy/RIOT
static void *init_udp_server(void *arg)
{
    (void) arg;

    sockaddr6_t sa;
    char buffer_main[UDP_BUFFER_SIZE];
    int32_t recsize;
    uint32_t fromlen;
    int sock = socket_base_socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);

    memset(&sa, 0, sizeof(sa));

    sa.sin6_family = AF_INET;
    sa.sin6_port = HTONS(SERVER_PORT);

    fromlen = sizeof(sa);

    if (-1 == socket_base_bind(sock, &sa, sizeof(sa))) {
        printf("Error bind failed!\n");
        socket_base_close(sock);
        return NULL;
    }

    while (1) {
        recsize = socket_base_recvfrom(sock, (void *)buffer_main, UDP_BUFFER_SIZE, 0, &sa, &fromlen);

        if (recsize < 0) {
            printf("ERROR: recsize < 0!\n");
        }

        printf("UDP packet received, payload: %s\n", buffer_main);
    }

    socket_base_close(sock);

    return NULL;
}