Ejemplo 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);
                }
            }
        }
    }
}
Ejemplo n.º 2
0
/*---------------------------------------------------------------------------*/
static
PT_THREAD(coap_server_thread(struct pt *pt))
{
    PT_BEGIN(pt);

    uint16_t xt;
    uint16_t tmp;

    while(1)
    {
        PT_WAIT_UNTIL(pt,check_flag(new_packet,coap_server_flag));

        if((buf[IP_PROTO_P]==IP_PROTO_UDP_V) && (buf[UDP_DST_PORT_H_P]==(COAPPORT>>8)) && (buf[UDP_DST_PORT_L_P]==(COAPPORT&0xff)))
        {
            /* UDP message length calculation */
            xt = ((buf[UDP_LEN_H_P]<<8)+buf[UDP_LEN_L_P])-8;

            #if 1

                if(coap_parse(&pkt,buf+UDP_DATA_P,xt) != 0)
                {
                    dbg(PSTR("\r\n> Bad coap packet\r\n"));             
                }
                else
                {            
                    coap_handle_req(&scratch_buf, &pkt, &rsppkt);
                    
                    xt = sizeof(response);

                    coap_build(response, &xt, &rsppkt);
                    
                    make_udp_reply_from_request(buf,response,xt,COAPPORT);                                       
                }

            #endif
        }

        clear_flag(new_packet,coap_server_flag);
    }

    PT_END(pt);
}/*---------------------------------------------------------------------------*/
Ejemplo n.º 3
0
void *microcoap_server(void *arg)
{
    (void) arg;
    msg_init_queue(_coap_msg_q, Q_SZ);

    uint8_t laddr[16] = { 0 };
    uint8_t raddr[16] = { 0 };
    size_t raddr_len;
    uint16_t rport;
    conn_udp_t conn;
    int rc = conn_udp_create(&conn, laddr, sizeof(laddr), AF_INET6, COAP_SERVER_PORT);

    while (1) {
        if ((rc = conn_udp_recvfrom(&conn, (char *)udp_buf, sizeof(udp_buf),
                                    raddr, &raddr_len, &rport)) < 0) {
            continue;
        }

        coap_packet_t pkt;
        /* parse UDP packet to CoAP */
        if (0 == (rc = coap_parse(&pkt, udp_buf, rc))) {
            coap_packet_t rsppkt;

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

            /* build reply */
            size_t rsplen = sizeof(udp_buf);
            if ((rc = coap_build(udp_buf, &rsplen, &rsppkt)) == 0) {
                /* send reply via UDP */
                rc = conn_udp_sendto(udp_buf, rsplen, NULL, 0, raddr, raddr_len,
                                     AF_INET6, COAP_SERVER_PORT, rport);
            }
        }
    }

    /* never reached */
    return NULL;
}
Ejemplo n.º 4
0
Archivo: sock.c Proyecto: A-Paul/RIOT
int nanocoap_server(sock_udp_ep_t *local, uint8_t *buf, size_t bufsize)
{
    sock_udp_t sock;
    sock_udp_ep_t remote;

    if (!local->port) {
        local->port = COAP_PORT;
    }

    ssize_t res = sock_udp_create(&sock, local, NULL, 0);
    if (res != 0) {
        return -1;
    }

    while (1) {
        res = sock_udp_recv(&sock, buf, bufsize, -1, &remote);
        if (res < 0) {
            DEBUG("error receiving UDP packet %d\n", (int)res);
        }
        else if (res > 0) {
            coap_pkt_t pkt;
            if (coap_parse(&pkt, (uint8_t *)buf, res) < 0) {
                DEBUG("error parsing packet\n");
                continue;
            }
            if ((res = coap_handle_req(&pkt, buf, bufsize)) > 0) {
                res = sock_udp_send(&sock, buf, res, &remote);
            }
            else {
                DEBUG("error handling request %d\n", (int)res);
            }
        }
    }

    return 0;
}
Ejemplo n.º 5
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));
            }
        }
    }
}
Ejemplo n.º 6
0
static void *coap_server_thread(void *args)
{
        struct sockaddr_in6 server_addr;
        uint16_t            port;
        coap_packet_t       inpkt;
        coap_packet_t       outpkt;
        int                 bad_packet;
        size_t              rsplen;
        
        msg_init_queue(server_msg_queue, SERVER_MSG_QUEUE_SIZE);
        
        server_socket = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
        
        /* parse port */
        port = (uint16_t)atoi((char *)args);
        
        if (port == 0) {
                puts("ERROR: invalid port specified");
                return NULL;
        }
        
        server_addr.sin6_family = AF_INET6;
        
        memset(&server_addr.sin6_addr, 0, sizeof(server_addr.sin6_addr));
        
        server_addr.sin6_port = htons(port);
        
        if (server_socket < 0) {
                puts("ERROR initializing socket");
                server_socket = 0;
                return NULL;
        }
        
        if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
                server_socket = -1;
                puts("ERROR binding socket");
                return NULL;
        }
        
        printf("Success: started UDP server on port %" PRIu16 "\n", port);
        
        while (1) {
                int                 res;
                struct sockaddr_in6 src;
                socklen_t           src_len = sizeof(struct sockaddr_in6);
                
                res = recvfrom(server_socket, server_buffer, sizeof(server_buffer), 0,
                               (struct sockaddr *)&src, &src_len);
                
                if (res < 0) {
                        puts("ERROR on receive");
                        continue;
                }
                
                if (res == 0) {
                        puts("Peer shut down");
                        continue;
                }
                
                printf("Received data: ");
                puts(server_buffer);
                bad_packet = coap_parse(&inpkt, (uint8_t *)server_buffer, res);
                
                if (bad_packet) {
                        puts("ERROR: malformed CoAP packet");
                        continue;
                }
                
                coap_dump_packet(&inpkt);
                
                bad_packet = coap_handle_req(&scratch, &inpkt, &outpkt, true, false);
                
                if (bad_packet) {
                        puts("ERROR: coap_handle_req failed");
                }
                
                bad_packet = coap_build(pkt_buf, &rsplen, &outpkt);
                
                if (bad_packet) {
                        printf("ERROR: could not build CoAP packet: %d\n", bad_packet);
                        continue;
                }
                
                res = sendto(server_socket, (void *)&pkt_buf, rsplen, 0,
                             (struct sockaddr *)&src, src_len);
                
                if (res == -1) {
                        puts("ERROR: could not send reply");
                }
        }
        
        return NULL;
}
Ejemplo n.º 7
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));
            }
        }
    }
}
Ejemplo n.º 8
0
void CoapServ(void* parg)
{
    int fd;
    struct sockaddr_in servaddr, cliaddr;
    coap_rw_buffer_t scratch_buf = {scratch_raw, sizeof(scratch_raw)};
	
	(void)parg;
	
    if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
    {
        printf("Socket Error\r\n");
        exit(0);
    }
	
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(COAPSERV_PORT);
    memset(&(servaddr.sin_zero), 0, sizeof(servaddr.sin_zero));
   
    if ((bind(fd, (struct sockaddr *)&servaddr, sizeof(servaddr))) == -1)
    {
       printf("Bind error\r\n");
       exit(0);      
    }
	
    endpoint_setup();
    printf("Coap Server Start!\r\n");
    
	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);
		
        if (0 != (rc = coap_parse(&pkt, buf, n)))
        {
            printf("Bad packet rc=%d\r\n", rc);
        }
        else
        {
			printf("payload : %s \r\n", pkt.payload.p);
			
			//coapServ reply client
			{
				size_t rsplen = sizeof(buf);
				coap_packet_t rsppkt;
				coap_handle_req(&scratch_buf, &pkt, &rsppkt);
				if (0 != (rc = coap_build(buf, &rsplen, &rsppkt)))
				{
					 printf("coap_build failed rc=%d\n", rc);
				}
				else
				{
					sendto(fd, buf, rsplen, 0, (struct sockaddr *)&cliaddr, sizeof(cliaddr));
				}
			}
        }
		
    }/*while(1)--END*/
}
Ejemplo n.º 9
0
void ICACHE_FLASH_ATTR coap_server(void *pvParameters)

{
  int fd;
  struct sockaddr_in servaddr, cliaddr;

  coap_rw_buffer_t scratch_buf = {scratch_raw, sizeof(scratch_raw)};

  if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
    // shell_printf("\nSocket Error\r\n");
    return;
  }

  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(PORT);
  memset(&(servaddr.sin_zero), 0, sizeof(servaddr.sin_zero));

  if ((bind(fd, (struct sockaddr *)&servaddr, sizeof(servaddr))) == -1) {
    // shell_printf("Bind error\r\n");
    return;
  }

  endpoint_setup();
  // shell_printf("Coap Server Start!\r\n");
  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 MICROCOAP_DEBUG
// shell_printf("\r\n--------------------\r\n");
// shell_printf("Received Buffer: \r\n");
// coap_dump(buf, n, true);
// shell_printf("\r\n");
#endif

    if (0 != (rc = coap_parse(&pkt, buf, n))) {
      // shell_printf("Bad packet rc\r\n");
      // shell_printf("Bad packet rc=%d\r\n", rc);
    } else {
      size_t rsplen = sizeof(buf);
      coap_packet_t rsppkt;
      // printf("Dump Packet: \r\n");
      // coap_dumpPacket(&pkt);
      coap_handle_req(&scratch_buf, &pkt, &rsppkt);

      if (0 != (rc = coap_build(buf, &rsplen, &rsppkt))) {
        // printf("coap_build failed rc=%d\n", rc);
      } else {
        // shell_printf("--------------------\r\n");
        // shell_printf("Sending Buffer: \r\n");
        // coap_dump(buf, rsplen, true);
        // printf("\r\n");
        // coap_dumpPacket(&rsppkt);
        sendto(fd, buf, rsplen, 0, (struct sockaddr *)&cliaddr,
               sizeof(cliaddr));
      }
    }
  }
}