Ethernet test for tinydtls-0.5.0
Dependencies: EthernetInterface mbed-rtos mbed tinydtls
Fork of tinydtls_test_ethernet by
main.cpp@4:4d466a913c11, 2013-10-18 (annotated)
- Committer:
- ashleymills
- Date:
- Fri Oct 18 14:29:21 2013 +0000
- Revision:
- 4:4d466a913c11
- Parent:
- 3:0caeed7fd9f0
Updated to tinydtls v0.5.0;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ashleymills | 1:391ec57807fa | 1 | #define __DEBUG__ 4 |
ashleymills | 1:391ec57807fa | 2 | |
ashleymills | 1:391ec57807fa | 3 | #ifndef __MODULE__ |
ashleymills | 1:391ec57807fa | 4 | #define __MODULE__ "main.cpp" |
ashleymills | 1:391ec57807fa | 5 | #endif |
ashleymills | 1:391ec57807fa | 6 | |
ashleymills | 0:6ae42a2aff75 | 7 | #include "mbed.h" |
ashleymills | 0:6ae42a2aff75 | 8 | #include "rtos.h" |
ashleymills | 0:6ae42a2aff75 | 9 | #include "bsd_socket.h" |
ashleymills | 0:6ae42a2aff75 | 10 | |
ashleymills | 4:4d466a913c11 | 11 | #include "dtls.h" |
ashleymills | 1:391ec57807fa | 12 | #include "global.h" |
ashleymills | 1:391ec57807fa | 13 | #include "debug.h" |
ashleymills | 1:391ec57807fa | 14 | #include "errno.h" |
ashleymills | 1:391ec57807fa | 15 | #include "dbg.h" |
ashleymills | 1:391ec57807fa | 16 | |
ashleymills | 1:391ec57807fa | 17 | #include "EthernetInterface.h" |
ashleymills | 0:6ae42a2aff75 | 18 | DigitalOut myled(LED1); |
ashleymills | 0:6ae42a2aff75 | 19 | |
ashleymills | 1:391ec57807fa | 20 | void fail(int code) { |
ashleymills | 1:391ec57807fa | 21 | while(1) { |
ashleymills | 1:391ec57807fa | 22 | myled = !myled; |
ashleymills | 1:391ec57807fa | 23 | Thread::wait(100); |
ashleymills | 1:391ec57807fa | 24 | } |
ashleymills | 1:391ec57807fa | 25 | } |
ashleymills | 1:391ec57807fa | 26 | |
ashleymills | 4:4d466a913c11 | 27 | static const unsigned char ecdsa_priv_key[] = { |
ashleymills | 4:4d466a913c11 | 28 | 0x41, 0xC1, 0xCB, 0x6B, 0x51, 0x24, 0x7A, 0x14, |
ashleymills | 4:4d466a913c11 | 29 | 0x43, 0x21, 0x43, 0x5B, 0x7A, 0x80, 0xE7, 0x14, |
ashleymills | 4:4d466a913c11 | 30 | 0x89, 0x6A, 0x33, 0xBB, 0xAD, 0x72, 0x94, 0xCA, |
ashleymills | 4:4d466a913c11 | 31 | 0x40, 0x14, 0x55, 0xA1, 0x94, 0xA9, 0x49, 0xFA}; |
ashleymills | 4:4d466a913c11 | 32 | |
ashleymills | 4:4d466a913c11 | 33 | static const unsigned char ecdsa_pub_key_x[] = { |
ashleymills | 4:4d466a913c11 | 34 | 0x36, 0xDF, 0xE2, 0xC6, 0xF9, 0xF2, 0xED, 0x29, |
ashleymills | 4:4d466a913c11 | 35 | 0xDA, 0x0A, 0x9A, 0x8F, 0x62, 0x68, 0x4E, 0x91, |
ashleymills | 4:4d466a913c11 | 36 | 0x63, 0x75, 0xBA, 0x10, 0x30, 0x0C, 0x28, 0xC5, |
ashleymills | 4:4d466a913c11 | 37 | 0xE4, 0x7C, 0xFB, 0xF2, 0x5F, 0xA5, 0x8F, 0x52}; |
ashleymills | 4:4d466a913c11 | 38 | |
ashleymills | 4:4d466a913c11 | 39 | static const unsigned char ecdsa_pub_key_y[] = { |
ashleymills | 4:4d466a913c11 | 40 | 0x71, 0xA0, 0xD4, 0xFC, 0xDE, 0x1A, 0xB8, 0x78, |
ashleymills | 4:4d466a913c11 | 41 | 0x5A, 0x3C, 0x78, 0x69, 0x35, 0xA7, 0xCF, 0xAB, |
ashleymills | 4:4d466a913c11 | 42 | 0xE9, 0x3F, 0x98, 0x72, 0x09, 0xDA, 0xED, 0x0B, |
ashleymills | 4:4d466a913c11 | 43 | 0x4F, 0xAB, 0xC3, 0x6F, 0xC7, 0x72, 0xF8, 0x29}; |
ashleymills | 4:4d466a913c11 | 44 | |
ashleymills | 4:4d466a913c11 | 45 | |
ashleymills | 4:4d466a913c11 | 46 | static const dtls_psk_key_t client_psk = { |
ashleymills | 4:4d466a913c11 | 47 | .id = (unsigned char *)"Client_identity", |
ashleymills | 4:4d466a913c11 | 48 | .id_length = 15, |
ashleymills | 4:4d466a913c11 | 49 | .key = (unsigned char *)"secretPSK", |
ashleymills | 4:4d466a913c11 | 50 | .key_length = 9 |
ashleymills | 4:4d466a913c11 | 51 | }; |
ashleymills | 4:4d466a913c11 | 52 | |
ashleymills | 1:391ec57807fa | 53 | /* This function is the "key store" for tinyDTLS. It is called to |
ashleymills | 1:391ec57807fa | 54 | * retrieve a key for the given identiy within this particular |
ashleymills | 1:391ec57807fa | 55 | * session. */ |
ashleymills | 1:391ec57807fa | 56 | int |
ashleymills | 4:4d466a913c11 | 57 | get_psk_key(struct dtls_context_t *ctx, |
ashleymills | 4:4d466a913c11 | 58 | const session_t *session, |
ashleymills | 4:4d466a913c11 | 59 | const unsigned char *id, size_t id_len, |
ashleymills | 4:4d466a913c11 | 60 | const dtls_psk_key_t **result) { |
ashleymills | 4:4d466a913c11 | 61 | DBG("id: %d, len: %d",id,id_len); |
ashleymills | 4:4d466a913c11 | 62 | *result = &client_psk; |
ashleymills | 1:391ec57807fa | 63 | |
ashleymills | 1:391ec57807fa | 64 | return 0; |
ashleymills | 1:391ec57807fa | 65 | } |
ashleymills | 1:391ec57807fa | 66 | |
ashleymills | 4:4d466a913c11 | 67 | int |
ashleymills | 4:4d466a913c11 | 68 | get_ecdsa_key(struct dtls_context_t *ctx, |
ashleymills | 4:4d466a913c11 | 69 | const session_t *session, |
ashleymills | 4:4d466a913c11 | 70 | const dtls_ecdsa_key_t **result) { |
ashleymills | 4:4d466a913c11 | 71 | static const dtls_ecdsa_key_t ecdsa_key = { |
ashleymills | 4:4d466a913c11 | 72 | .curve = DTLS_ECDH_CURVE_SECP256R1, |
ashleymills | 4:4d466a913c11 | 73 | .priv_key = ecdsa_priv_key, |
ashleymills | 4:4d466a913c11 | 74 | .pub_key_x = ecdsa_pub_key_x, |
ashleymills | 4:4d466a913c11 | 75 | .pub_key_y = ecdsa_pub_key_y |
ashleymills | 4:4d466a913c11 | 76 | }; |
ashleymills | 4:4d466a913c11 | 77 | |
ashleymills | 4:4d466a913c11 | 78 | *result = &ecdsa_key; |
ashleymills | 4:4d466a913c11 | 79 | return 0; |
ashleymills | 4:4d466a913c11 | 80 | } |
ashleymills | 4:4d466a913c11 | 81 | |
ashleymills | 4:4d466a913c11 | 82 | int |
ashleymills | 4:4d466a913c11 | 83 | verify_ecdsa_key(struct dtls_context_t *ctx, |
ashleymills | 4:4d466a913c11 | 84 | const session_t *session, |
ashleymills | 4:4d466a913c11 | 85 | const unsigned char *other_pub_x, |
ashleymills | 4:4d466a913c11 | 86 | const unsigned char *other_pub_y, |
ashleymills | 4:4d466a913c11 | 87 | size_t key_size) { |
ashleymills | 4:4d466a913c11 | 88 | return 0; |
ashleymills | 4:4d466a913c11 | 89 | } |
ashleymills | 4:4d466a913c11 | 90 | |
ashleymills | 4:4d466a913c11 | 91 | |
ashleymills | 1:391ec57807fa | 92 | #define APN_GDSP |
ashleymills | 1:391ec57807fa | 93 | //#define APN_CONTRACT |
ashleymills | 1:391ec57807fa | 94 | |
ashleymills | 1:391ec57807fa | 95 | #ifdef APN_GDSP |
ashleymills | 1:391ec57807fa | 96 | #define APN "ppinternetd.gdsp" |
ashleymills | 1:391ec57807fa | 97 | #define APN_USERNAME "" |
ashleymills | 1:391ec57807fa | 98 | #define APN_PASSWORD "" |
ashleymills | 1:391ec57807fa | 99 | #endif |
ashleymills | 1:391ec57807fa | 100 | |
ashleymills | 1:391ec57807fa | 101 | #ifdef APN_CONTRACT |
ashleymills | 1:391ec57807fa | 102 | #define APN "internet" |
ashleymills | 1:391ec57807fa | 103 | #define APN_USERNAME "web" |
ashleymills | 1:391ec57807fa | 104 | #define APN_PASSWORD "web" |
ashleymills | 1:391ec57807fa | 105 | #endif |
ashleymills | 1:391ec57807fa | 106 | |
ashleymills | 1:391ec57807fa | 107 | sockaddr_in bindAddr,serverAddress; |
ashleymills | 1:391ec57807fa | 108 | bool connectToSocketUDP(char *ipAddress, int port, int *sockfd) { |
ashleymills | 1:391ec57807fa | 109 | *sockfd = -1; |
ashleymills | 1:391ec57807fa | 110 | // create the socket |
ashleymills | 1:391ec57807fa | 111 | if((*sockfd=socket(AF_INET,SOCK_DGRAM,0))<0) { |
ashleymills | 1:391ec57807fa | 112 | DBG("Error opening socket"); |
ashleymills | 1:391ec57807fa | 113 | return false; |
ashleymills | 1:391ec57807fa | 114 | } |
ashleymills | 1:391ec57807fa | 115 | socklen_t sockAddrInLen = sizeof(struct sockaddr_in); |
ashleymills | 1:391ec57807fa | 116 | |
ashleymills | 1:391ec57807fa | 117 | // bind socket |
ashleymills | 1:391ec57807fa | 118 | memset(&bindAddr, 0x00, sockAddrInLen); |
ashleymills | 1:391ec57807fa | 119 | bindAddr.sin_family = AF_INET; // IP family |
ashleymills | 1:391ec57807fa | 120 | bindAddr.sin_port = htons(port); |
ashleymills | 1:391ec57807fa | 121 | bindAddr.sin_addr.s_addr = IPADDR_ANY; // 32 bit IP representation |
ashleymills | 1:391ec57807fa | 122 | // call bind |
ashleymills | 1:391ec57807fa | 123 | if(bind(*sockfd,(const struct sockaddr *)&bindAddr,sockAddrInLen)!=0) { |
ashleymills | 1:391ec57807fa | 124 | DBG("Error binding socket"); |
ashleymills | 1:391ec57807fa | 125 | perror(NULL); |
ashleymills | 1:391ec57807fa | 126 | } |
ashleymills | 1:391ec57807fa | 127 | |
ashleymills | 1:391ec57807fa | 128 | INFO("UDP socket created and bound to: %s:%d",inet_ntoa(bindAddr.sin_addr),ntohs(bindAddr.sin_port)); |
ashleymills | 1:391ec57807fa | 129 | |
ashleymills | 1:391ec57807fa | 130 | // create the socket address |
ashleymills | 1:391ec57807fa | 131 | memset(&serverAddress, 0x00, sizeof(struct sockaddr_in)); |
ashleymills | 1:391ec57807fa | 132 | serverAddress.sin_addr.s_addr = inet_addr(ipAddress); |
ashleymills | 1:391ec57807fa | 133 | serverAddress.sin_family = AF_INET; |
ashleymills | 1:391ec57807fa | 134 | serverAddress.sin_port = htons(port); |
ashleymills | 1:391ec57807fa | 135 | |
ashleymills | 1:391ec57807fa | 136 | // do socket connect |
ashleymills | 1:391ec57807fa | 137 | //LOG("Connecting socket to %s:%d", inet_ntoa(serverAddress.sin_addr), ntohs(serverAddress.sin_port)); |
ashleymills | 1:391ec57807fa | 138 | if(connect(*sockfd, (const struct sockaddr *)&serverAddress, sizeof(serverAddress))<0) { |
ashleymills | 1:391ec57807fa | 139 | shutdown(*sockfd,SHUT_RDWR); |
ashleymills | 1:391ec57807fa | 140 | close(*sockfd); |
ashleymills | 1:391ec57807fa | 141 | DBG("Could not connect"); |
ashleymills | 1:391ec57807fa | 142 | return false; |
ashleymills | 1:391ec57807fa | 143 | } |
ashleymills | 1:391ec57807fa | 144 | return true; |
ashleymills | 1:391ec57807fa | 145 | } |
ashleymills | 1:391ec57807fa | 146 | |
ashleymills | 1:391ec57807fa | 147 | int read_from_peer(struct dtls_context_t *ctx, session_t *session, uint8 *data, size_t len) { |
ashleymills | 4:4d466a913c11 | 148 | DBG("read_from_peer called: %d",len); |
ashleymills | 1:391ec57807fa | 149 | size_t i; |
ashleymills | 1:391ec57807fa | 150 | for (i = 0; i < len; i++) |
ashleymills | 1:391ec57807fa | 151 | printf("%c", data[i]); |
ashleymills | 1:391ec57807fa | 152 | return 0; |
ashleymills | 1:391ec57807fa | 153 | } |
ashleymills | 1:391ec57807fa | 154 | |
ashleymills | 1:391ec57807fa | 155 | int send_to_peer(struct dtls_context_t *ctx, session_t *session, uint8 *data, size_t len) { |
ashleymills | 4:4d466a913c11 | 156 | DBG("send_to_peer called: %d",len); |
ashleymills | 1:391ec57807fa | 157 | int fd = *(int *)dtls_get_app_data(ctx); |
ashleymills | 1:391ec57807fa | 158 | return sendto(fd, data, len, MSG_DONTWAIT, |
ashleymills | 1:391ec57807fa | 159 | &session->addr.sa, session->size); |
ashleymills | 1:391ec57807fa | 160 | } |
ashleymills | 1:391ec57807fa | 161 | |
ashleymills | 1:391ec57807fa | 162 | |
ashleymills | 1:391ec57807fa | 163 | int |
ashleymills | 1:391ec57807fa | 164 | dtls_handle_read(struct dtls_context_t *ctx) { |
ashleymills | 1:391ec57807fa | 165 | DBG("dtls_handle_read called"); |
ashleymills | 1:391ec57807fa | 166 | int fd; |
ashleymills | 1:391ec57807fa | 167 | session_t session; |
ashleymills | 1:391ec57807fa | 168 | #define MAX_READ_BUF 512 |
ashleymills | 1:391ec57807fa | 169 | static uint8 buf[MAX_READ_BUF]; |
ashleymills | 1:391ec57807fa | 170 | int len; |
ashleymills | 1:391ec57807fa | 171 | |
ashleymills | 1:391ec57807fa | 172 | fd = *(int *)dtls_get_app_data(ctx); |
ashleymills | 1:391ec57807fa | 173 | /* |
ashleymills | 1:391ec57807fa | 174 | if(!fd) { |
ashleymills | 1:391ec57807fa | 175 | DBG("FD NULL"); |
ashleymills | 1:391ec57807fa | 176 | return -1; |
ashleymills | 1:391ec57807fa | 177 | }*/ |
ashleymills | 1:391ec57807fa | 178 | |
ashleymills | 4:4d466a913c11 | 179 | memset(&session, 0, sizeof(session_t)); |
ashleymills | 4:4d466a913c11 | 180 | session.size = sizeof(session.addr); |
ashleymills | 1:391ec57807fa | 181 | len = recvfrom(fd, buf, MAX_READ_BUF, 0, |
ashleymills | 4:4d466a913c11 | 182 | &session.addr.sa, &session.size); |
ashleymills | 4:4d466a913c11 | 183 | |
ashleymills | 1:391ec57807fa | 184 | if (len < 0) { |
ashleymills | 1:391ec57807fa | 185 | DBG("Got nothing from read"); |
ashleymills | 1:391ec57807fa | 186 | perror("recvfrom"); |
ashleymills | 1:391ec57807fa | 187 | return -1; |
ashleymills | 1:391ec57807fa | 188 | } else { |
ashleymills | 1:391ec57807fa | 189 | #ifndef NDEBUG |
ashleymills | 1:391ec57807fa | 190 | unsigned char addrbuf[72]; |
ashleymills | 1:391ec57807fa | 191 | dsrv_print_addr(&session, addrbuf, sizeof(addrbuf)); |
ashleymills | 1:391ec57807fa | 192 | DBG("got %d bytes from %s\n", len, (char *)addrbuf); |
ashleymills | 4:4d466a913c11 | 193 | /* |
ashleymills | 1:391ec57807fa | 194 | dump((unsigned char *)&session, sizeof(session_t)); |
ashleymills | 1:391ec57807fa | 195 | DBGX("\r\n"); |
ashleymills | 1:391ec57807fa | 196 | dump(buf, len); |
ashleymills | 1:391ec57807fa | 197 | DBGX("\r\n"); |
ashleymills | 4:4d466a913c11 | 198 | */ |
ashleymills | 1:391ec57807fa | 199 | #endif |
ashleymills | 1:391ec57807fa | 200 | } |
ashleymills | 1:391ec57807fa | 201 | |
ashleymills | 1:391ec57807fa | 202 | return dtls_handle_message(ctx, &session, buf, len); |
ashleymills | 1:391ec57807fa | 203 | } |
ashleymills | 4:4d466a913c11 | 204 | int gDTLSConnected = 0; |
ashleymills | 4:4d466a913c11 | 205 | |
ashleymills | 4:4d466a913c11 | 206 | // callback called on tinydtls events (currently only ever called for DTLS_EVENT_CONNECTED) |
ashleymills | 4:4d466a913c11 | 207 | int event_handler( |
ashleymills | 4:4d466a913c11 | 208 | struct dtls_context_t *ctx, |
ashleymills | 4:4d466a913c11 | 209 | session_t *session, |
ashleymills | 4:4d466a913c11 | 210 | dtls_alert_level_t level, |
ashleymills | 4:4d466a913c11 | 211 | unsigned short code) { |
ashleymills | 4:4d466a913c11 | 212 | DBG("DTLS SESSION SETUP COMPLETE"); |
ashleymills | 4:4d466a913c11 | 213 | gDTLSConnected = 1; |
ashleymills | 4:4d466a913c11 | 214 | return 0; |
ashleymills | 4:4d466a913c11 | 215 | } |
ashleymills | 1:391ec57807fa | 216 | |
ashleymills | 1:391ec57807fa | 217 | static dtls_handler_t cb = { |
ashleymills | 4:4d466a913c11 | 218 | .write = send_to_peer, |
ashleymills | 4:4d466a913c11 | 219 | .read = read_from_peer, |
ashleymills | 4:4d466a913c11 | 220 | .event = event_handler, |
ashleymills | 4:4d466a913c11 | 221 | .get_psk_key = get_psk_key, |
ashleymills | 4:4d466a913c11 | 222 | .get_ecdsa_key = NULL,// get_ecdsa_key, |
ashleymills | 4:4d466a913c11 | 223 | .verify_ecdsa_key = NULL//verify_ecdsa_key |
ashleymills | 4:4d466a913c11 | 224 | }; |
ashleymills | 1:391ec57807fa | 225 | |
ashleymills | 0:6ae42a2aff75 | 226 | int main() { |
ashleymills | 1:391ec57807fa | 227 | DBG_INIT(); |
ashleymills | 1:391ec57807fa | 228 | DBG_SET_SPEED(115200); |
ashleymills | 1:391ec57807fa | 229 | DBG_SET_NEWLINE("\r\n"); |
ashleymills | 1:391ec57807fa | 230 | |
ashleymills | 1:391ec57807fa | 231 | DBG("Tiny DTLS test"); |
ashleymills | 1:391ec57807fa | 232 | |
ashleymills | 1:391ec57807fa | 233 | // DTLS context struct |
ashleymills | 1:391ec57807fa | 234 | dtls_context_t *dtls_context = NULL; |
ashleymills | 1:391ec57807fa | 235 | int ret = 0; |
ashleymills | 1:391ec57807fa | 236 | fd_set rfds, wfds; |
ashleymills | 1:391ec57807fa | 237 | struct timeval timeout; |
ashleymills | 1:391ec57807fa | 238 | session_t dst; |
ashleymills | 1:391ec57807fa | 239 | |
ashleymills | 1:391ec57807fa | 240 | // structure for getting address of incoming packets |
ashleymills | 1:391ec57807fa | 241 | sockaddr_in fromAddr; |
ashleymills | 1:391ec57807fa | 242 | socklen_t fromAddrLen = sizeof(struct sockaddr_in); |
ashleymills | 1:391ec57807fa | 243 | memset(&fromAddr,0x00,fromAddrLen); |
ashleymills | 1:391ec57807fa | 244 | |
ashleymills | 1:391ec57807fa | 245 | // connect to cellular network |
ashleymills | 1:391ec57807fa | 246 | /* |
ashleymills | 1:391ec57807fa | 247 | VodafoneUSBModem modem; |
ashleymills | 1:391ec57807fa | 248 | modem.connect(APN,APN_USERNAME,APN_PASSWORD); |
ashleymills | 1:391ec57807fa | 249 | */ |
ashleymills | 1:391ec57807fa | 250 | EthernetInterface modem; |
ashleymills | 1:391ec57807fa | 251 | DBG("Connecting to network interface"); |
ashleymills | 1:391ec57807fa | 252 | modem.init(); |
ashleymills | 1:391ec57807fa | 253 | if(modem.connect(10000)) { |
ashleymills | 1:391ec57807fa | 254 | DBG("Error initialising ethernet interface"); |
ashleymills | 1:391ec57807fa | 255 | } |
ashleymills | 1:391ec57807fa | 256 | DBG("DONE."); |
ashleymills | 1:391ec57807fa | 257 | |
ashleymills | 1:391ec57807fa | 258 | dtls_init(); |
ashleymills | 1:391ec57807fa | 259 | dtls_set_log_level(LOG_DEBUG); |
ashleymills | 1:391ec57807fa | 260 | |
ashleymills | 1:391ec57807fa | 261 | // setup socket to remote server |
ashleymills | 1:391ec57807fa | 262 | int sockfd = NULL; |
ashleymills | 1:391ec57807fa | 263 | |
ashleymills | 1:391ec57807fa | 264 | |
ashleymills | 4:4d466a913c11 | 265 | //if(!connectToSocketUDP("109.74.199.96", 4433, &sockfd)) { |
ashleymills | 4:4d466a913c11 | 266 | if(!connectToSocketUDP("192.168.1.99", 4433, &sockfd)) { |
ashleymills | 1:391ec57807fa | 267 | DBG("Error connecting to socket"); |
ashleymills | 1:391ec57807fa | 268 | fail(1); |
ashleymills | 0:6ae42a2aff75 | 269 | } |
ashleymills | 1:391ec57807fa | 270 | DBG("\"Connected\" to UDP socket, sockfd: %d",sockfd); |
ashleymills | 4:4d466a913c11 | 271 | |
ashleymills | 1:391ec57807fa | 272 | int on = 1; |
ashleymills | 1:391ec57807fa | 273 | if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on) ) < 0) { |
ashleymills | 1:391ec57807fa | 274 | dsrv_log(LOG_ALERT, "setsockopt SO_REUSEADDR: %s\n", strerror(errno)); |
ashleymills | 4:4d466a913c11 | 275 | } |
ashleymills | 1:391ec57807fa | 276 | |
ashleymills | 1:391ec57807fa | 277 | // tinydtls stuff |
ashleymills | 1:391ec57807fa | 278 | |
ashleymills | 1:391ec57807fa | 279 | // destination address is stored in a session type |
ashleymills | 1:391ec57807fa | 280 | |
ashleymills | 1:391ec57807fa | 281 | memset(&dst, 0x00, sizeof(session_t)); |
ashleymills | 1:391ec57807fa | 282 | dst.size = sizeof(sockaddr_in); |
ashleymills | 1:391ec57807fa | 283 | DBG("starting copy at offset: %d",(int)&dst.addr-(int)&dst); |
ashleymills | 1:391ec57807fa | 284 | serverAddress.sin_len = dst.size; |
ashleymills | 1:391ec57807fa | 285 | memcpy(&dst.addr, &serverAddress, dst.size); |
ashleymills | 1:391ec57807fa | 286 | |
ashleymills | 1:391ec57807fa | 287 | |
ashleymills | 1:391ec57807fa | 288 | //dst.addr.sin.sin_port = htons(4433); |
ashleymills | 1:391ec57807fa | 289 | |
ashleymills | 1:391ec57807fa | 290 | // dtls init must always be called for memory allocation |
ashleymills | 1:391ec57807fa | 291 | |
ashleymills | 1:391ec57807fa | 292 | // setup DTLS context |
ashleymills | 1:391ec57807fa | 293 | DBG("Creating DTLS context"); |
ashleymills | 1:391ec57807fa | 294 | dtls_context = dtls_new_context(&sockfd); |
ashleymills | 1:391ec57807fa | 295 | if(!dtls_context) { |
ashleymills | 1:391ec57807fa | 296 | DBG("Cannot create context"); |
ashleymills | 1:391ec57807fa | 297 | fail(3); |
ashleymills | 1:391ec57807fa | 298 | } |
ashleymills | 1:391ec57807fa | 299 | DBG("DTLS context created"); |
ashleymills | 1:391ec57807fa | 300 | |
ashleymills | 1:391ec57807fa | 301 | // forced to use this call back system |
ashleymills | 1:391ec57807fa | 302 | |
ashleymills | 1:391ec57807fa | 303 | dtls_set_handler(dtls_context, &cb); |
ashleymills | 1:391ec57807fa | 304 | |
ashleymills | 1:391ec57807fa | 305 | DBG("Issuing dtls_connect"); |
ashleymills | 1:391ec57807fa | 306 | ret = dtls_connect(dtls_context, &dst); |
ashleymills | 1:391ec57807fa | 307 | if(ret<0) { |
ashleymills | 1:391ec57807fa | 308 | DBG("Error in dtls_connect: %d",ret); |
ashleymills | 1:391ec57807fa | 309 | modem.disconnect(); |
ashleymills | 1:391ec57807fa | 310 | fail(4); |
ashleymills | 1:391ec57807fa | 311 | } |
ashleymills | 1:391ec57807fa | 312 | if(ret==0) { |
ashleymills | 1:391ec57807fa | 313 | DBG("Channel already exists"); |
ashleymills | 1:391ec57807fa | 314 | modem.disconnect(); |
ashleymills | 1:391ec57807fa | 315 | fail(5); |
ashleymills | 1:391ec57807fa | 316 | } |
ashleymills | 1:391ec57807fa | 317 | DBG("dtls_connect successfull"); |
ashleymills | 4:4d466a913c11 | 318 | int counter = 0; |
ashleymills | 4:4d466a913c11 | 319 | char outBuf[64]; |
ashleymills | 1:391ec57807fa | 320 | while (1) { |
ashleymills | 1:391ec57807fa | 321 | // setup file descriptor lists for select |
ashleymills | 1:391ec57807fa | 322 | FD_ZERO(&rfds); |
ashleymills | 1:391ec57807fa | 323 | FD_ZERO(&wfds); |
ashleymills | 1:391ec57807fa | 324 | //FD_SET(fileno(stdin), &rfds); |
ashleymills | 1:391ec57807fa | 325 | FD_SET(sockfd, &rfds); |
ashleymills | 1:391ec57807fa | 326 | // FD_SET(sockfd, &wfds); |
ashleymills | 1:391ec57807fa | 327 | |
ashleymills | 4:4d466a913c11 | 328 | timeout.tv_sec = 15; |
ashleymills | 1:391ec57807fa | 329 | timeout.tv_usec = 0; |
ashleymills | 4:4d466a913c11 | 330 | DBG("Waiting for data"); |
ashleymills | 1:391ec57807fa | 331 | int result = select(sockfd+1, &rfds, &wfds, 0, &timeout); |
ashleymills | 1:391ec57807fa | 332 | |
ashleymills | 1:391ec57807fa | 333 | if(result < 0) { // error |
ashleymills | 1:391ec57807fa | 334 | if (errno != EINTR) |
ashleymills | 1:391ec57807fa | 335 | perror("select"); |
ashleymills | 1:391ec57807fa | 336 | } else if (result == 0) { |
ashleymills | 1:391ec57807fa | 337 | // timeout |
ashleymills | 4:4d466a913c11 | 338 | if(gDTLSConnected) { |
ashleymills | 4:4d466a913c11 | 339 | DBG("Sending data"); |
ashleymills | 4:4d466a913c11 | 340 | sprintf(outBuf,"This is a pointless test message: %d\r\n",counter++); |
ashleymills | 4:4d466a913c11 | 341 | dtls_write(dtls_context, &dst, (uint8 *)outBuf, strlen(outBuf)); |
ashleymills | 4:4d466a913c11 | 342 | //try_send(dtls_context, &dst); |
ashleymills | 4:4d466a913c11 | 343 | } |
ashleymills | 1:391ec57807fa | 344 | } else { |
ashleymills | 1:391ec57807fa | 345 | // OK |
ashleymills | 1:391ec57807fa | 346 | // check which file descriptor had an event |
ashleymills | 1:391ec57807fa | 347 | if(FD_ISSET(sockfd, &wfds)) { |
ashleymills | 1:391ec57807fa | 348 | // FIXME (from tinydtls) |
ashleymills | 1:391ec57807fa | 349 | } else if (FD_ISSET(sockfd, &rfds)) |
ashleymills | 1:391ec57807fa | 350 | if(dtls_handle_read(dtls_context)<0) { |
ashleymills | 1:391ec57807fa | 351 | modem.disconnect(); |
ashleymills | 1:391ec57807fa | 352 | fail(6); |
ashleymills | 1:391ec57807fa | 353 | } |
ashleymills | 1:391ec57807fa | 354 | } |
ashleymills | 1:391ec57807fa | 355 | //else if (FD_ISSET(fileno(stdin), &rfds)) |
ashleymills | 1:391ec57807fa | 356 | //handle_stdin(); |
ashleymills | 1:391ec57807fa | 357 | //} |
ashleymills | 1:391ec57807fa | 358 | |
ashleymills | 1:391ec57807fa | 359 | //if(len) { |
ashleymills | 1:391ec57807fa | 360 | // try_send(dtls_context, &dst); |
ashleymills | 1:391ec57807fa | 361 | //} |
ashleymills | 1:391ec57807fa | 362 | } |
ashleymills | 1:391ec57807fa | 363 | |
ashleymills | 0:6ae42a2aff75 | 364 | } |