/*---------------------------------------------------------------------------*/ void init_dtls() { static dtls_handler_t cb = { .write = send_to_peer, .read = read_from_peer, .event = NULL, #ifdef DTLS_PSK .get_psk_info = get_psk_info, #endif #ifdef DTLS_ECC .get_ecdsa_key = NULL, .verify_ecdsa_key = NULL, #endif }; server_conn = udp_new(NULL, 0, NULL); udp_bind(server_conn, UIP_HTONS(DTLS_ECHO_PORT)); dtls_set_log_level(DTLS_LOG_DEBUG); dtls_context = dtls_new_context(server_conn); if (dtls_context) dtls_set_handler(dtls_context, &cb); printf("DTLS server started\n"); }
void init_dtls() { static dtls_handler_t cb = { .write = send_to_peer, .read = read_from_peer, .event = NULL, #ifdef DTLS_PSK .get_psk_info = get_psk_info, #endif /* DTLS_PSK */ #ifdef DTLS_ECC .get_ecdsa_key = get_ecdsa_key, .verify_ecdsa_key = verify_ecdsa_key #endif /* DTLS_ECC */ }; #if 0 uip_ipaddr_t ipaddr; /* struct uip_ds6_addr *root_if; */ #endif /* UIP_CONF_ROUTER */ PRINTF("DTLS server started\n"); #if 0 /* TEST */ memset(&tmp_addr, 0, sizeof(rimeaddr_t)); if(get_eui64_from_eeprom(tmp_addr.u8)); #if UIP_CONF_IPV6 memcpy(&uip_lladdr.addr, &tmp_addr.u8, 8); #endif #endif /* TEST */ #if 0 /* uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0); */ /* uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr); */ /* uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF); */ /* create_rpl_dag(&ipaddr); */ /* #else */ /* uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF); */ uip_ip6addr(&ipaddr, 0xaaaa, 0,0,0,0x0200,0,0,0x0003); uip_ds6_addr_add(&ipaddr, 0, ADDR_MANUAL); create_rpl_dag(&ipaddr); #endif /* UIP_CONF_ROUTER */ server_conn = udp_new(NULL, 0, NULL); udp_bind(server_conn, UIP_HTONS(20220)); dtls_set_log_level(DTLS_LOG_DEBUG); dtls_context = dtls_new_context(server_conn); if (dtls_context) dtls_set_handler(dtls_context, &cb); }
void init_dtls() { static dtls_handler_t cb = { .write = send_to_peer, .read = read_from_peer, .event = NULL, .get_psk_key = get_psk_key }; #if UIP_CONF_ROUTER uip_ipaddr_t ipaddr; #endif /* UIP_CONF_ROUTER */ PRINTF("DTLS server started\n"); #if 0 /* TEST */ memset(&tmp_addr, 0, sizeof(linkaddr_t)); if(get_eui64_from_eeprom(tmp_addr.u8)); #if NETSTACK_CONF_WITH_IPV6 memcpy(&uip_lladdr.addr, &tmp_addr.u8, 8); #endif #endif /* TEST */ #if UIP_CONF_ROUTER uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0); uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr); uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF); #endif /* UIP_CONF_ROUTER */ server_conn = udp_new(NULL, 0, NULL); udp_bind(server_conn, UIP_HTONS(20220)); dtls_set_log_level(LOG_DEBUG); dtls_context = dtls_new_context(server_conn); if (dtls_context) dtls_set_handler(dtls_context, &cb); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(rest_server_example, ev, data) { uip_ipaddr_t server_ipaddr; uip_ds6_maddr_t *rv; PROCESS_BEGIN(); uip_ip6addr(&server_ipaddr, 0xff1e, 0, 0, 0, 0, 0, 0x89, 0xabcd); rv = uip_ds6_maddr_add(&server_ipaddr); if(rv) { PRINTF("Joined multicast group "); PRINT6ADDR(&uip_ds6_maddr_lookup(&server_ipaddr)->ipaddr); PRINTF("\n"); } print_local_addresses(); #ifdef WITH_DTLS dtls_set_log_level(LOG_DEBUG); set_dtls_handler(get_psk_key, NULL); #endif /* WITH_DTLS */ PRINTF("\r\nStarting Erbium Example Server\r\n"); /* Initialize the REST engine. */ rest_init_engine(); /** Creating an entry in routing table to able to answer to multicast request with the first message */ uip_ipaddr_t client_addr; uip_lladdr_t lladdr = {{0x04,0x0f,0x07,0xb2,0x00, 0x12,0x4b,0x00}}; uip_ip6addr(&client_addr, 0xfe80, 0, 0, 0, 0x060f, 0x07b2, 0x0012, 0x4b00); uip_ds6_nbr_add(&client_addr, &lladdr, 0, 1); #ifdef WITH_DTLS session_t session; /**< Creating a secure session */ uip_ipaddr_copy(&session.addr, &server_ipaddr); session.port = UIP_HTONS(COAP_DEFAULT_PORT+1); session.size = sizeof(session.addr) + sizeof(session.port); session.ifindex = 1; #ifdef WITH_MULTICAST secure_group_creation(&session); /**< Create a DTLS group in case of multicast communication */ #endif // WITH_MULTICAST #endif // WITH_DTLS /* Activate the application-specific resources. */ #if REST_RES_HELLO rest_activate_resource(&resource_helloworld); #endif #if REST_RES_BATTERY SENSORS_ACTIVATE(battery_sensor); rest_activate_resource(&resource_battery); #endif PROCESS_END(); }
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); }
static int get_key(struct dtls_context_t *ctx, const session_t *session, const unsigned char *id, size_t id_len, const dtls_key_t **result) { static const dtls_key_t psk = { .type = DTLS_KEY_PSK, .key.psk.id = (unsigned char *)DTLS_IDENTITY_HINT, .key.psk.id_length = DTLS_IDENTITY_HINT_LENGTH, .key.psk.key = (unsigned char *)DTLS_PSK_KEY, .key.psk.key_length = DTLS_PSK_KEY_LENGTH }; *result = &psk; return 0; } #else static int get_psk_key(struct dtls_context_t *ctx, const session_t *session, const unsigned char *id, size_t id_len, const dtls_psk_key_t **result) { static const dtls_psk_key_t psk = { .id = (unsigned char *)DTLS_IDENTITY_HINT, .id_length = DTLS_IDENTITY_HINT_LENGTH, .key = (unsigned char *)DTLS_PSK_KEY, .key_length = DTLS_PSK_KEY_LENGTH }; *result = &psk; return 0; } #endif /*-----------------------------------------------------------------------------------*/ void coap_init_communication_layer(uint16_t port) { static dtls_handler_t cb = { .write = send_to_peer, .read = read_from_peer, .event = NULL, #if DTLS_VERSION_0_4_0 .get_key = get_key #else .get_psk_key = get_psk_key, .get_ecdsa_key = NULL, .verify_ecdsa_key = NULL #endif }; server_conn = udp_new(NULL, 0, NULL); udp_bind(server_conn, port); dtls_set_log_level(LOG_DEBUG); dtls_context = dtls_new_context(server_conn); if (dtls_context) dtls_set_handler(dtls_context, &cb); /* new connection with remote host */ printf("COAP-DTLS listening on port %u\n", uip_ntohs(server_conn->lport)); } /*-----------------------------------------------------------------------------------*/ static int send_to_peer(struct dtls_context_t *ctx, session_t *session, uint8 *data, size_t len) { struct uip_udp_conn *conn = (struct uip_udp_conn *)dtls_get_app_data(ctx); uip_ipaddr_copy(&conn->ripaddr, &session->addr); conn->rport = session->port; uip_udp_packet_send(conn, data, len); /* Restore server connection to allow data from any node */ memset(&conn->ripaddr, 0, sizeof(conn->ripaddr)); memset(&conn->rport, 0, sizeof(conn->rport)); return len; } /*-----------------------------------------------------------------------------------*/ void coap_send_message(context_t * ctx, uip_ipaddr_t *addr, uint16_t port, uint8_t *data, uint16_t length) { session_t session; dtls_session_init(&session); uip_ipaddr_copy(&session.addr, addr); session.port = port; dtls_write(ctx, &session, data, length); } /*-----------------------------------------------------------------------------------*/ static int read_from_peer(struct dtls_context_t *ctx, session_t *session, uint8 *data, size_t len) { uip_len = len; memmove(uip_appdata, data, len); coap_receive(ctx); return 0; } /*-----------------------------------------------------------------------------------*/ void coap_handle_receive() { session_t session; if(uip_newdata()) { dtls_session_init(&session); uip_ipaddr_copy(&session.addr, &UIP_IP_BUF->srcipaddr); session.port = UIP_UDP_BUF->srcport; dtls_handle_message(dtls_context, &session, uip_appdata, uip_datalen()); } }