Ethernet test for tinydtls-0.5.0
Dependencies: EthernetInterface mbed-rtos mbed tinydtls
Fork of tinydtls_test_ethernet by
Revision 4:4d466a913c11, committed 2013-10-18
- Comitter:
- ashleymills
- Date:
- Fri Oct 18 14:29:21 2013 +0000
- Parent:
- 3:0caeed7fd9f0
- Commit message:
- Updated to tinydtls v0.5.0;
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
tinydtls.lib | Show annotated file Show diff for this revision Revisions of this file |
diff -r 0caeed7fd9f0 -r 4d466a913c11 main.cpp --- a/main.cpp Fri Oct 11 14:04:03 2013 +0000 +++ b/main.cpp Fri Oct 18 14:29:21 2013 +0000 @@ -8,7 +8,7 @@ #include "rtos.h" #include "bsd_socket.h" -#include <dtls.h> +#include "dtls.h" #include "global.h" #include "debug.h" #include "errno.h" @@ -24,28 +24,71 @@ } } +static const unsigned char ecdsa_priv_key[] = { + 0x41, 0xC1, 0xCB, 0x6B, 0x51, 0x24, 0x7A, 0x14, + 0x43, 0x21, 0x43, 0x5B, 0x7A, 0x80, 0xE7, 0x14, + 0x89, 0x6A, 0x33, 0xBB, 0xAD, 0x72, 0x94, 0xCA, + 0x40, 0x14, 0x55, 0xA1, 0x94, 0xA9, 0x49, 0xFA}; + +static const unsigned char ecdsa_pub_key_x[] = { + 0x36, 0xDF, 0xE2, 0xC6, 0xF9, 0xF2, 0xED, 0x29, + 0xDA, 0x0A, 0x9A, 0x8F, 0x62, 0x68, 0x4E, 0x91, + 0x63, 0x75, 0xBA, 0x10, 0x30, 0x0C, 0x28, 0xC5, + 0xE4, 0x7C, 0xFB, 0xF2, 0x5F, 0xA5, 0x8F, 0x52}; + +static const unsigned char ecdsa_pub_key_y[] = { + 0x71, 0xA0, 0xD4, 0xFC, 0xDE, 0x1A, 0xB8, 0x78, + 0x5A, 0x3C, 0x78, 0x69, 0x35, 0xA7, 0xCF, 0xAB, + 0xE9, 0x3F, 0x98, 0x72, 0x09, 0xDA, 0xED, 0x0B, + 0x4F, 0xAB, 0xC3, 0x6F, 0xC7, 0x72, 0xF8, 0x29}; + + +static const dtls_psk_key_t client_psk = { + .id = (unsigned char *)"Client_identity", + .id_length = 15, + .key = (unsigned char *)"secretPSK", + .key_length = 9 +}; + /* This function is the "key store" for tinyDTLS. It is called to * retrieve a key for the given identiy within this particular * session. */ 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) { - DBG("Entered get_key"); +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) { + DBG("id: %d, len: %d",id,id_len); + *result = &client_psk; - static const dtls_key_t psk = { - .type = DTLS_KEY_PSK, - .key.psk.id = (unsigned char *)"Client_identity", - .key.psk.id_length = 15, - .key.psk.key = (unsigned char *)"secretPSK", - .key.psk.key_length = 9 - }; - - *result = &psk; return 0; } +int +get_ecdsa_key(struct dtls_context_t *ctx, + const session_t *session, + const dtls_ecdsa_key_t **result) { + static const dtls_ecdsa_key_t ecdsa_key = { + .curve = DTLS_ECDH_CURVE_SECP256R1, + .priv_key = ecdsa_priv_key, + .pub_key_x = ecdsa_pub_key_x, + .pub_key_y = ecdsa_pub_key_y + }; + + *result = &ecdsa_key; + return 0; +} + +int +verify_ecdsa_key(struct dtls_context_t *ctx, + const session_t *session, + const unsigned char *other_pub_x, + const unsigned char *other_pub_y, + size_t key_size) { + return 0; +} + + #define APN_GDSP //#define APN_CONTRACT @@ -102,7 +145,7 @@ } int read_from_peer(struct dtls_context_t *ctx, session_t *session, uint8 *data, size_t len) { - DBG("read_from_peer called"); + DBG("read_from_peer called: %d",len); size_t i; for (i = 0; i < len; i++) printf("%c", data[i]); @@ -110,7 +153,7 @@ } int send_to_peer(struct dtls_context_t *ctx, session_t *session, uint8 *data, size_t len) { - DBG("send_to_peer called"); + DBG("send_to_peer called: %d",len); int fd = *(int *)dtls_get_app_data(ctx); return sendto(fd, data, len, MSG_DONTWAIT, &session->addr.sa, session->size); @@ -133,29 +176,11 @@ return -1; }*/ - memset(&session, 0x00, sizeof(session_t)); - DBG("BEFORE: "); - for(uint8_t i=0; i<sizeof(session_t); i++) { - DBGX("%x ",((uint8_t*)&session)[i]); - } - DBGX("\r\n"); - session.size = sizeof(sockaddr_in); - uint32_t bullshit = sizeof(sockaddr_in); + memset(&session, 0, sizeof(session_t)); + session.size = sizeof(session.addr); len = recvfrom(fd, buf, MAX_READ_BUF, 0, - &session.addr.sa, &bullshit); - - //((uint8_t*)&session)[4] = 0x00; - //session.addr.sin.sin_family = AF_INET; - DBG("AFTER: %d",bullshit); - for(uint8_t i=0; i<sizeof(session_t); i++) { - DBGX("%x ",((uint8_t*)&session)[i]); - } - DBGX("\r\n"); - short x = session.addr.sin.sin_family; - if(session.addr.sin.sin_family==AF_INET) { - DBG("AF_INET alright"); - } - DBG("%x %x",((uint8_t*)x)[0],((uint8_t*)x)[1]); + &session.addr.sa, &session.size); + if (len < 0) { DBG("Got nothing from read"); perror("recvfrom"); @@ -165,22 +190,38 @@ unsigned char addrbuf[72]; dsrv_print_addr(&session, addrbuf, sizeof(addrbuf)); DBG("got %d bytes from %s\n", len, (char *)addrbuf); + /* dump((unsigned char *)&session, sizeof(session_t)); DBGX("\r\n"); dump(buf, len); DBGX("\r\n"); + */ #endif } return dtls_handle_message(ctx, &session, buf, len); } +int gDTLSConnected = 0; + +// callback called on tinydtls events (currently only ever called for DTLS_EVENT_CONNECTED) +int event_handler( + struct dtls_context_t *ctx, + session_t *session, + dtls_alert_level_t level, + unsigned short code) { + DBG("DTLS SESSION SETUP COMPLETE"); + gDTLSConnected = 1; + return 0; +} static dtls_handler_t cb = { - .write = send_to_peer, - .read = read_from_peer, - .event = NULL, - .get_key = get_key - }; + .write = send_to_peer, + .read = read_from_peer, + .event = event_handler, + .get_psk_key = get_psk_key, + .get_ecdsa_key = NULL,// get_ecdsa_key, + .verify_ecdsa_key = NULL//verify_ecdsa_key +}; int main() { DBG_INIT(); @@ -221,17 +262,17 @@ int sockfd = NULL; - if(!connectToSocketUDP("109.74.199.96", 4433, &sockfd)) { - //if(!connectToSocketUDP("192.168.1.99", 4433, &sockfd)) { + //if(!connectToSocketUDP("109.74.199.96", 4433, &sockfd)) { + if(!connectToSocketUDP("192.168.1.99", 4433, &sockfd)) { DBG("Error connecting to socket"); fail(1); } DBG("\"Connected\" to UDP socket, sockfd: %d",sockfd); - /* + int on = 1; if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on) ) < 0) { dsrv_log(LOG_ALERT, "setsockopt SO_REUSEADDR: %s\n", strerror(errno)); - }*/ + } // tinydtls stuff @@ -274,6 +315,8 @@ fail(5); } DBG("dtls_connect successfull"); + int counter = 0; + char outBuf[64]; while (1) { // setup file descriptor lists for select FD_ZERO(&rfds); @@ -282,9 +325,9 @@ FD_SET(sockfd, &rfds); // FD_SET(sockfd, &wfds); - timeout.tv_sec = 5; + timeout.tv_sec = 15; timeout.tv_usec = 0; - + DBG("Waiting for data"); int result = select(sockfd+1, &rfds, &wfds, 0, &timeout); if(result < 0) { // error @@ -292,6 +335,12 @@ perror("select"); } else if (result == 0) { // timeout + if(gDTLSConnected) { + DBG("Sending data"); + sprintf(outBuf,"This is a pointless test message: %d\r\n",counter++); + dtls_write(dtls_context, &dst, (uint8 *)outBuf, strlen(outBuf)); + //try_send(dtls_context, &dst); + } } else { // OK // check which file descriptor had an event
diff -r 0caeed7fd9f0 -r 4d466a913c11 tinydtls.lib --- a/tinydtls.lib Fri Oct 11 14:04:03 2013 +0000 +++ b/tinydtls.lib Fri Oct 18 14:29:21 2013 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/ashleymills/code/tinydtls/#bc8a649bad13 +http://mbed.org/users/ashleymills/code/tinydtls/#ff9ebe0cf0e9