void connection_free(dtls_connection_t * connList) { while (connList != NULL) { dtls_connection_t * nextP; nextP = connList->next; free(connList); connList = nextP; } dtls_free_context(dtlsContext); dtlsContext = NULL; }
/*---------------------------------------------------------------------------*/ void coap_context_close(coap_context_t *coap_ctx) { if(coap_ctx == NULL || coap_ctx->is_used == 0) { /* Not opened */ return; } if(coap_ctx->dtls_context != NULL) { dtls_free_context(coap_ctx->dtls_context); } if(coap_ctx->buf && uip_udp_conn(coap_ctx->buf) != NULL) { uip_udp_remove(uip_udp_conn(coap_ctx->buf)); } coap_ctx->is_used = 0; }
int main(int argc, char **argv) { dtls_context_t *dtls_context = NULL; fd_set rfds, wfds; struct timeval timeout; unsigned short port = DEFAULT_PORT; char port_str[NI_MAXSERV] = "0"; log_t log_level = LOG_WARN; int fd, result; int on = 1; int opt, res; session_t dst; dtls_init(); snprintf(port_str, sizeof(port_str), "%d", port); while ((opt = getopt(argc, argv, "p:o:v:")) != -1) { switch (opt) { case 'p' : strncpy(port_str, optarg, NI_MAXSERV-1); port_str[NI_MAXSERV - 1] = '\0'; break; case 'o' : output_file.length = strlen(optarg); output_file.s = (unsigned char *)malloc(output_file.length + 1); if (!output_file.s) { dsrv_log(LOG_CRIT, "cannot set output file: insufficient memory\n"); exit(-1); } else { /* copy filename including trailing zero */ memcpy(output_file.s, optarg, output_file.length + 1); } break; case 'v' : log_level = strtol(optarg, NULL, 10); break; default: usage(argv[0], PACKAGE_VERSION); exit(1); } } set_log_level(log_level); if (argc <= optind) { usage(argv[0], PACKAGE_VERSION); exit(1); } memset(&dst, 0, sizeof(session_t)); /* resolve destination address where server should be sent */ res = resolve_address(argv[optind++], &dst.addr.sa); if (res < 0) { dsrv_log(LOG_EMERG, "failed to resolve address\n"); exit(-1); } dst.size = res; /* use port number from command line when specified or the listen port, otherwise */ dst.addr.sin.sin_port = htons(atoi(optind < argc ? argv[optind++] : port_str)); /* init socket and set it to non-blocking */ fd = socket(dst.addr.sa.sa_family, SOCK_DGRAM, 0); if (fd < 0) { dsrv_log(LOG_ALERT, "socket: %s\n", strerror(errno)); return 0; } if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on) ) < 0) { dsrv_log(LOG_ALERT, "setsockopt SO_REUSEADDR: %s\n", strerror(errno)); } #if 0 flags = fcntl(fd, F_GETFL, 0); if (flags < 0 || fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) { dsrv_log(LOG_ALERT, "fcntl: %s\n", strerror(errno)); goto error; } #endif on = 1; #ifdef IPV6_RECVPKTINFO if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, sizeof(on) ) < 0) { #else /* IPV6_RECVPKTINFO */ if (setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, &on, sizeof(on) ) < 0) { #endif /* IPV6_RECVPKTINFO */ dsrv_log(LOG_ALERT, "setsockopt IPV6_PKTINFO: %s\n", strerror(errno)); } dtls_context = dtls_new_context(&fd); if (!dtls_context) { dsrv_log(LOG_EMERG, "cannot create context\n"); exit(-1); } dtls_set_handler(dtls_context, &cb); dtls_connect(dtls_context, &dst); while (1) { FD_ZERO(&rfds); FD_ZERO(&wfds); FD_SET(fileno(stdin), &rfds); FD_SET(fd, &rfds); /* FD_SET(fd, &wfds); */ timeout.tv_sec = 5; timeout.tv_usec = 0; result = select(fd+1, &rfds, &wfds, 0, &timeout); if (result < 0) { /* error */ if (errno != EINTR) perror("select"); } else if (result == 0) { /* timeout */ } else { /* ok */ if (FD_ISSET(fd, &wfds)) /* FIXME */; else if (FD_ISSET(fd, &rfds)) dtls_handle_read(dtls_context); else if (FD_ISSET(fileno(stdin), &rfds)) handle_stdin(); } if (len) try_send(dtls_context, &dst); } dtls_free_context(dtls_context); exit(0); }
int main(int argc, char **argv) { dtls_context_t *the_context = NULL; log_t log_level = DTLS_LOG_WARN; fd_set rfds, wfds; struct timeval timeout; int fd, opt, result; int on = 1; int ecdh_anon_enalbe = DTLS_CIPHER_DISABLE; struct sockaddr_in6 listen_addr; memset(&listen_addr, 0, sizeof(struct sockaddr_in6)); /* fill extra field for 4.4BSD-based systems (see RFC 3493, section 3.4) */ #if defined(SIN6_LEN) || defined(HAVE_SOCKADDR_IN6_SIN6_LEN) listen_addr.sin6_len = sizeof(struct sockaddr_in6); #endif listen_addr.sin6_family = AF_INET6; listen_addr.sin6_port = htons(DEFAULT_PORT); listen_addr.sin6_addr = in6addr_any; while ((opt = getopt(argc, argv, "A:p:v:a:")) != -1) { switch (opt) { case 'A' : if (resolve_address(optarg, (struct sockaddr *)&listen_addr) < 0) { fprintf(stderr, "cannot resolve address\n"); exit(-1); } break; case 'p' : listen_addr.sin6_port = htons(atoi(optarg)); break; case 'v' : log_level = strtol(optarg, NULL, 10); break; case 'a': if( strcmp(optarg, "enable") == 0) ecdh_anon_enalbe = DTLS_CIPHER_ENABLE; break; default: usage(argv[0], dtls_package_version()); exit(1); } } dtls_set_log_level(log_level); /* init socket and set it to non-blocking */ fd = socket(listen_addr.sin6_family, SOCK_DGRAM, 0); if (fd < 0) { dtls_alert("socket: %s\n", strerror(errno)); return 0; } if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on) ) < 0) { dtls_alert("setsockopt SO_REUSEADDR: %s\n", strerror(errno)); } #if 0 flags = fcntl(fd, F_GETFL, 0); if (flags < 0 || fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) { dtls_alert("fcntl: %s\n", strerror(errno)); goto error; } #endif on = 1; #ifdef IPV6_RECVPKTINFO if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, sizeof(on) ) < 0) { #else /* IPV6_RECVPKTINFO */ if (setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, &on, sizeof(on) ) < 0) { #endif /* IPV6_RECVPKTINFO */ dtls_alert("setsockopt IPV6_PKTINFO: %s\n", strerror(errno)); } if (bind(fd, (struct sockaddr *)&listen_addr, sizeof(listen_addr)) < 0) { dtls_alert("bind: %s\n", strerror(errno)); goto error; } dtls_init(); the_context = dtls_new_context(&fd); /* enable/disable tls_ecdh_anon_with_aes_128_cbc_sha */ dtls_enables_anon_ecdh(the_context, ecdh_anon_enalbe); dtls_set_handler(the_context, &cb); while (1) { FD_ZERO(&rfds); FD_ZERO(&wfds); FD_SET(fd, &rfds); /* FD_SET(fd, &wfds); */ timeout.tv_sec = 5; timeout.tv_usec = 0; result = select( fd+1, &rfds, &wfds, 0, &timeout); if (result < 0) { /* error */ if (errno != EINTR) perror("select"); } else if (result == 0) { /* timeout */ } else { /* ok */ if (FD_ISSET(fd, &wfds)) ; else if (FD_ISSET(fd, &rfds)) { dtls_handle_read(the_context); } } } error: dtls_free_context(the_context); exit(0); }