Exemplo n.º 1
0
/*
 * Starts a blocking and never-returning loop dispatching CoAP requests.
 *
 * When using gnrc, make sure the calling thread has an initialized msg queue.
 */
void microcoap_server_loop(void)
{

    static const sock_udp_ep_t local = { .family = AF_INET6,
                                         .port = COAP_SERVER_PORT };
    sock_udp_ep_t remote;

    sock_udp_t sock;

    int rc = sock_udp_create(&sock, &local, NULL, 0);

    while (1) {
        DEBUG("Waiting for incoming UDP packet...\n");
        rc = sock_udp_recv(&sock, (char *)_udp_buf, sizeof(_udp_buf),
                           SOCK_NO_TIMEOUT, &remote);
        if (rc < 0) {
            DEBUG("Error in sock_udp_recv(). rc=%u\n", rc);
            continue;
        }

        size_t n = rc;

        coap_packet_t pkt;
        DEBUG("Received packet: ");
        coap_dump(_udp_buf, n, true);
        DEBUG("\n");

        /* parse UDP packet to CoAP */
        if (0 != (rc = coap_parse(&pkt, _udp_buf, n))) {
            DEBUG("Bad packet rc=%d\n", rc);
        }
        else {
            coap_packet_t rsppkt;
            DEBUG("content:\n");
            coap_dumpPacket(&pkt);

            /* handle CoAP request */
            coap_handle_req(&scratch_buf, &pkt, &rsppkt);

            /* build reply */
            size_t rsplen = sizeof(_udp_buf);
            if ((rc = coap_build(_udp_buf, &rsplen, &rsppkt)) != 0) {
                DEBUG("coap_build failed rc=%d\n", rc);
            }
            else {
                DEBUG("Sending packet: ");
                coap_dump(_udp_buf, rsplen, true);
                DEBUG("\n");
                DEBUG("content:\n");
                coap_dumpPacket(&rsppkt);

                /* send reply via UDP */
                rc = sock_udp_send(&sock, _udp_buf, rsplen, &remote);
                if (rc < 0) {
                    DEBUG("Error sending CoAP reply via udp; %u\n", rc);
                }
            }
        }
    }
}
Exemplo n.º 2
0
/* organic, local, gluten-free */
int make_homemade_request(uint8_t* snd_buf)
{
    printf("creating example GET request...\n");
    static char* msg= "hello";
    size_t req_pkt_sz;
    int errcode;

    // cobble together CoAP packet
    coap_header_t req_hdr = {
        .ver = 1,
        .t = COAP_TYPE_NONCON,
        .tkl = 0,                  /* microcoap can't generate tokens anyway */
        .code = MAKE_RSPCODE(0, COAP_METHOD_GET),  /* class 0, detail 1 */
        .id = {22,22}              /*let's see if this works :D */
    };

    coap_buffer_t payload = {
        .p = (const uint8_t *) msg,
        .len = strlen(msg)
    };

    coap_packet_t req_pkt = {
        .hdr = req_hdr,
        .tok = (coap_buffer_t) {}, /* No token */
        .numopts = 0,
        .opts = {},
        .payload = payload
    };

    req_pkt_sz = sizeof(req_pkt);

#ifdef DEBUG
    printf("[main-posix] content:\n");
    coap_dumpPacket(&req_pkt);
#endif

    // try to  write packet to send buffer
    if (0 != (errcode = coap_build(snd_buf, &req_pkt_sz, &req_pkt))) {
        printf("Error building packet! Error code: %i\nAborting. \n", errcode);
        return 1;
    }
    return 0;
}
Exemplo n.º 3
0
int main(void){

//READ IN HOW MANY TAP DEVICE TO CREATE (MAX 9999)
//File locker
    struct flock fl;
    pid_t tid = syscall(SYS_gettid);
    fl.l_type   = F_RDLCK;  /* F_RDLCK, F_WRLCK, F_UNLCK    */
    fl.l_whence = SEEK_SET; /* SEEK_SET, SEEK_CUR, SEEK_END */
    fl.l_start  = 0;        /* Offset from l_whence         */
    fl.l_len    = 0;        /* length, 0 = to EOF           */
    fl.l_pid    = tid; /* our PID                      */

//READ THE SIZE OF TAP_CONTROL_SIZE FILE
    char  ffile_size[2];
    int file_size,tap_num;
    int fp = open("./coap/tap_control_size.txt", O_RDONLY);
    fcntl(fp, F_SETLKW, &fl);  //Locks the file for reading
    if(0 > fp)
    {
        printf("\n tap_control_size:open() Error!!!\n");
        return 1;
    }
    read(fp,ffile_size,2);
//CLOSE FILE
    if(0 > close(fp))
    {
        printf("\n tap_control_size:close() Error!!!\n");
        return 1;
    }
    fl.l_type = F_UNLCK;  /* tell it to unlock the region */
    fcntl(fp, F_SETLK, &fl); /* set the region to unlocked */

    file_size = (int)(ffile_size[0] - '0');
//OPEN FILE TO DETERMINE WHICH CLIENT IS THIS ONE
    char  file_num[file_size];
    fl.l_type   = F_RDLCK;
    fp = open("./coap/tap_control.txt", O_RDONLY);
    fcntl(fp, F_SETLKW, &fl);  //Locks the file for reading
    if(0 > fp)
    {
        printf("\n tap_control:open() Error!!!\n");
        return 1;
    }
    read(fp,file_num,file_size);
//CLOSE FILE
    if(0 > close(fp))
    {
        printf("\n tap_control:close() Error!!!\n");
        return 1;
    }
    fl.l_type = F_UNLCK;  /* tell it to unlock the region */
    fcntl(fp, F_SETLK, &fl); /* set the region to unlocked */

//CONVERT IT TO INT
    tap_num=0;
    if(file_size == 4){
	tap_num += (int)(file_num[0] - '0')*1000;
	tap_num += (int)(file_num[1] - '0')*100;
	tap_num += (int)(file_num[2] - '0')*10;
	tap_num += (int)(file_num[3] - '0');
    }
    else if(file_size == 3){
	tap_num += (int)(file_num[0] - '0')*100;
	tap_num += (int)(file_num[1] - '0')*10;
	tap_num += (int)(file_num[2] - '0');
    }
    else if(file_size == 2){
	tap_num += (int)(file_num[0] - '0')*10;
	tap_num += (int)(file_num[1] - '0');
    }
    else 
	tap_num += (int)(file_num[0] - '0');

//CREATE TAP DEVICE
    puts("Starting the RIOT\n");
    int fd;
    char name[3+file_size];
    strcpy(name,"tap");
    int i;
    for(i=0;i<file_size;i++)
    name[3+i]=file_num[i];
    name[3+file_size]=0;

    printf("TAP DEIVCE: %s\n",name);

    struct sockaddr_in6 servaddr;
//    struct ifreq ifr;//
    struct sockaddr_in6 cliaddr;
    uint8_t buf[4096];
    uint8_t scratch_raw[4096];
    coap_rw_buffer_t scratch_buf = {scratch_raw, sizeof(scratch_raw)};
   
    fd = socket(AF_INET6,SOCK_DGRAM,0);//Socket file descriptor init

    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin6_family = AF_INET6;//inet family
    servaddr.sin6_flowinfo = 0;//??

    int ip8_1=0,ip8_2=0;
    servaddr.sin6_addr.s6_addr[0] = (uint8_t)0x30;//IPv6 Address 1
    servaddr.sin6_addr.s6_addr[1] = (uint8_t)0x00;
    servaddr.sin6_addr.s6_addr[2] = (uint8_t)0x00;//IPv6 Address 2
    servaddr.sin6_addr.s6_addr[3] = (uint8_t)0x00;
    servaddr.sin6_addr.s6_addr[4] = (uint8_t)0x00;//IPv6 Address 3
    servaddr.sin6_addr.s6_addr[5] = (uint8_t)0x00;
    servaddr.sin6_addr.s6_addr[6] = (uint8_t)0x00;//IPv6 Address 4
    servaddr.sin6_addr.s6_addr[7] = (uint8_t)0x00;
    servaddr.sin6_addr.s6_addr[8] = (uint8_t)0x11;//IPv6 Address 5
    servaddr.sin6_addr.s6_addr[9] = (uint8_t)0x11;
    servaddr.sin6_addr.s6_addr[10] = (uint8_t)0x22;//IPv6 Address 6
    servaddr.sin6_addr.s6_addr[11] = (uint8_t)0x22;
    servaddr.sin6_addr.s6_addr[12] = (uint8_t)0x33;//IPv6 Address 7
    servaddr.sin6_addr.s6_addr[13] = (uint8_t)0x33;
    if(file_size == 4){
	ip8_1 += (int)(file_num[0] - '0')*16;
	ip8_1 += (int)(file_num[1] - '0');
	ip8_2 += (int)(file_num[2] - '0')*16;
	ip8_2 += (int)(file_num[3] - '0');
    }
    else if(file_size == 3){
	ip8_1 += (int)(file_num[0] - '0');
	ip8_2 += (int)(file_num[1] - '0')*16;
	ip8_2 += (int)(file_num[2] - '0');
    }
    else if(file_size == 2){
	ip8_2 += (int)(file_num[0] - '0')*16;
	ip8_2 += (int)(file_num[1] - '0');
    }
    else 
	ip8_2 += (int)(file_num[0] - '0');

    servaddr.sin6_addr.s6_addr[14] = (uint8_t)ip8_1;//IPv6 Address 8 //TODO
    servaddr.sin6_addr.s6_addr[15] = (uint8_t)ip8_2;

    servaddr.sin6_port = htons(PORT);		//PORT (5683)
    bind(fd,(struct sockaddr *)&servaddr, sizeof(servaddr));
    endpoint_setup();

    while(1)
    {
        int n, rc;
        socklen_t len = sizeof(cliaddr);
        coap_packet_t pkt;
        n = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *)&cliaddr, &len);
//#ifdef DEBUG
        printf("Received: ");
        coap_dump(buf, n, true);
        printf("\n");
//#endif

        if (0 != (rc = coap_parse(&pkt, buf, n)))
            printf("Bad packet rc=%d\n", rc);
        else
        {
            size_t rsplen = sizeof(buf);
            coap_packet_t rsppkt;
#ifdef DEBUG
            coap_dumpPacket(&pkt);
#endif
            coap_handle_req(&scratch_buf, &pkt, &rsppkt);

            if (0 != (rc = coap_build(buf, &rsplen, &rsppkt)))
                printf("coap_build failed rc=%d\n", rc);
            else
            {
#ifdef DEBUG
                printf("Sending: ");
                coap_dump(buf, rsplen, true);
                printf("\n");
#endif
#ifdef DEBUG
                coap_dumpPacket(&rsppkt);
#endif

                sendto(fd, buf, rsplen, 0, (struct sockaddr *)&cliaddr, sizeof(cliaddr));
            }
        }
    }
}
Exemplo n.º 4
0
/**
Build a PUT request.
The value of buflen will be the request packet's size after successful completion. 
*/
int coap_ext_build_PUT(uint8_t *buf, size_t *buflen, char *payload, coap_endpoint_path_t *path)
{
    /* 
     * Note: the resource URI is coded as an option! -> COAP_OPTION_URI_PATH
     */

    DEBUG("creating PUT request...\n");
    size_t req_pkt_sz;
    int errcode;
    int segment_count = path->count;

    /* cobble together CoAP header */
    coap_header_t req_hdr = {
        .ver = 1,
        .t = COAP_TYPE_CON,
        .tkl = 0,                                  /* microcoap can't generate tokens anyway */
        .code = MAKE_RSPCODE(0, COAP_METHOD_PUT),  /* class 0, detail 1: this is a PUT. */
        .id = {22,22}                              /* TODO: create dynamic ID (seqnum style?) */
    };

    coap_buffer_t payload_buf = {
        .p = (const uint8_t *) payload,
        .len = strlen(payload)
    };

    coap_packet_t req_pkt = {
        .hdr = req_hdr,
        .tok = (coap_buffer_t) {},  /* No token */
        .numopts = segment_count,   /* all segments of the path to the resource */
        .payload = payload_buf
    };

    /* Create one option for each segment of the URI path and fill req_pkt.opts*/
    for (int i=0; i < segment_count; i++ ) {
        coap_option_t path_option = {
            .num = COAP_OPTION_URI_PATH,
            .buf = {.p = (const uint8_t *) path->elems[i],
                    .len = strlen(path->elems[i])}
        };

        req_pkt.opts[i] = path_option;
    }

    req_pkt_sz = sizeof(req_pkt);

    if (buflen < req_pkt_sz) {
        DEBUG("Error: buflen too small:\n\tbuflen:%zd\n\treq_pkt_sz:%zd\n", buflen, req_pkt_sz);
        return -1;
    }

#ifdef DEBUG
    printf("[main-posix] content:\n");
    coap_dumpPacket(&req_pkt);
#endif

    printf("xoxo\n");
    // try to  write packet to send buffer
    if (0 != (errcode = coap_build(buf, buflen, &req_pkt))) {
        printf("Error building packet! Error code: %i\nAborting. \n", errcode);
        return -1;
    }
    return 0;
}
Exemplo n.º 5
0
int main(int argc, char **argv)
{
    (void)argc;
    (void)argv;
    puts("Starting the RIOT\n");
    int fd,tap_fd;
    const char *clonedev = "/dev/net/tun";
    char *name = "tap0";
    struct sockaddr_in6 servaddr, cliaddr;
    struct ifreq ifr;
    uint8_t buf[4096];
    uint8_t scratch_raw[4096];
    coap_rw_buffer_t scratch_buf = {scratch_raw, sizeof(scratch_raw)};

    fd = socket(AF_INET6,SOCK_DGRAM,0);//Socket file descriptor init

    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin6_family = AF_INET6;//inet family
    servaddr.sin6_flowinfo = 0;//??

    servaddr.sin6_addr.s6_addr[0] = (uint8_t)0x30;//IPv6 Address 1
    servaddr.sin6_addr.s6_addr[1] = (uint8_t)0x00;
    servaddr.sin6_addr.s6_addr[2] = (uint8_t)0x00;//IPv6 Address 2
    servaddr.sin6_addr.s6_addr[3] = (uint8_t)0x00;
    servaddr.sin6_addr.s6_addr[4] = (uint8_t)0x00;//IPv6 Address 3
    servaddr.sin6_addr.s6_addr[5] = (uint8_t)0x00;
    servaddr.sin6_addr.s6_addr[6] = (uint8_t)0x00;//IPv6 Address 4
    servaddr.sin6_addr.s6_addr[7] = (uint8_t)0x00;
    servaddr.sin6_addr.s6_addr[8] = (uint8_t)0x11;//IPv6 Address 5
    servaddr.sin6_addr.s6_addr[9] = (uint8_t)0x11;
    servaddr.sin6_addr.s6_addr[10] = (uint8_t)0x22;//IPv6 Address 6
    servaddr.sin6_addr.s6_addr[11] = (uint8_t)0x22;
    servaddr.sin6_addr.s6_addr[12] = (uint8_t)0x33;//IPv6 Address 7
    servaddr.sin6_addr.s6_addr[13] = (uint8_t)0x33;
    servaddr.sin6_addr.s6_addr[14] = (uint8_t)0x00;//IPv6 Address 8
    servaddr.sin6_addr.s6_addr[15] = (uint8_t)0x01;



    servaddr.sin6_port = htons(PORT);		//PORT (5683)
    bind(fd,(struct sockaddr *)&servaddr, sizeof(servaddr));

//Set TAP device up, give it local ipv6 address
    /* implicitly create the tap interface */
    if ((tap_fd = real_open(clonedev , O_RDWR)) == -1) {
        err(EXIT_FAILURE, "open(%s)", clonedev);
    }
    memset(&ifr, 0, sizeof(ifr));
    ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
    strncpy(ifr.ifr_name, name, IFNAMSIZ);
    if (real_ioctl(tap_fd, TUNSETIFF, (void *)&ifr) == -1) {
        _native_in_syscall++;
        warn("ioctl TUNSETIFF");
        warnx("probably the tap interface (%s) does not exist or is already in use", name);
        real_exit(EXIT_FAILURE);
    }
//TODO Add Global IP



    endpoint_setup();

    while(1)
    {
        int n, rc;
        socklen_t len = sizeof(cliaddr);
        coap_packet_t pkt;

        n = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *)&cliaddr, &len);
//#ifdef DEBUG
        printf("Received: ");
        coap_dump(buf, n, true);
        printf("\n");
//#endif

        if (0 != (rc = coap_parse(&pkt, buf, n)))
            printf("Bad packet rc=%d\n", rc);
        else
        {
            size_t rsplen = sizeof(buf);
            coap_packet_t rsppkt;
#ifdef DEBUG
            coap_dumpPacket(&pkt);
#endif
            coap_handle_req(&scratch_buf, &pkt, &rsppkt);

            if (0 != (rc = coap_build(buf, &rsplen, &rsppkt)))
                printf("coap_build failed rc=%d\n", rc);
            else
            {
#ifdef DEBUG
                printf("Sending: ");
                coap_dump(buf, rsplen, true);
                printf("\n");
#endif
#ifdef DEBUG
                coap_dumpPacket(&rsppkt);
#endif

                sendto(fd, buf, rsplen, 0, (struct sockaddr *)&cliaddr, sizeof(cliaddr));
            }
        }
    }
}