/* * 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); } } } } }
void send_coap_post(uint8_t *data, size_t len) { uint8_t snd_buf[512]; size_t req_pkt_sz; coap_buffer_t payload = { .p = data, .len = len }; coap_packet_t req_pkt = { .header = req_hdr, .token = (coap_buffer_t) { 0 }, .numopts = 1, .opts = {{{(uint8_t *)"senml", 5}, (uint8_t)COAP_OPTION_URI_PATH}}, .payload = payload }; req_pkt_sz = sizeof(req_pkt); if (coap_build(snd_buf, &req_pkt_sz, &req_pkt) != 0) { printf("CoAP build failed :(\n"); return; } conn_udp_sendto(snd_buf, req_pkt_sz, NULL, 0, &dst_addr, sizeof(dst_addr), AF_INET6, SPORT, UDP_PORT); }
/* 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; }
/*---------------------------------------------------------------------------*/ 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); }/*---------------------------------------------------------------------------*/
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; }
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)); } } } }
int main(void) { int fd; #ifdef IPV6 struct sockaddr_in6 servaddr, cliaddr; #else /* IPV6 */ struct sockaddr_in servaddr, cliaddr; #endif /* IPV6 */ uint8_t buf[1024]; #ifdef IPV6 fd = socket(AF_INET6,SOCK_DGRAM,0); #else /* IPV6 */ fd = socket(AF_INET,SOCK_DGRAM,0); #endif /* IPV6 */ bzero(&servaddr,sizeof(servaddr)); #ifdef IPV6 servaddr.sin6_family = AF_INET6; servaddr.sin6_addr = in6addr_any; servaddr.sin6_port = htons(COAP_DEFAULT_PORT); #else /* IPV6 */ servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(COAP_DEFAULT_PORT); #endif /* IPV6 */ bind(fd,(struct sockaddr *)&servaddr, sizeof(servaddr)); resource_setup(resources); 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 YACOAP_DEBUG printf("Received: "); coap_dump(buf, n, true); printf("\n"); #endif if ((rc = coap_parse(buf, n, &pkt)) > COAP_ERR) printf("Bad packet rc=%d\n", rc); else { size_t buflen = sizeof(buf); coap_packet_t rsppkt; #ifdef YACOAP_DEBUG coap_dump_packet(&pkt); #endif coap_handle_request(resources, &pkt, &rsppkt); if ((rc = coap_build(&rsppkt, buf, &buflen)) > COAP_ERR) printf("coap_build failed rc=%d\n", rc); else { #ifdef YACOAP_DEBUG printf("Sending: "); coap_dump(buf, buflen, true); printf("\n"); #endif #ifdef YACOAP_DEBUG coap_dump_packet(&rsppkt); #endif sendto(fd, buf, buflen, 0, (struct sockaddr *)&cliaddr, sizeof(cliaddr)); } } } }
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; }
/** 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; }
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)); } } } }
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*/ }
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)); } } } }