1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

examples/dtls-sock: use new session init workflow

This commit is contained in:
Aiman Ismail 2020-05-08 12:17:26 +02:00
parent b5b7955743
commit fe5272b885
2 changed files with 24 additions and 17 deletions

View File

@ -78,6 +78,7 @@ static int client_send(char *addr_str, char *data, size_t datalen)
sock_udp_ep_t local = SOCK_IPV6_EP_ANY;
local.port = 12345;
remote.port = DTLS_DEFAULT_PORT;
uint8_t buf[DTLS_HANDSHAKE_BUFSIZE];
/* get interface */
char* iface = ipv6_addr_split_iface(addr_str);
@ -122,26 +123,32 @@ static int client_send(char *addr_str, char *data, size_t datalen)
return -1;
}
res = sock_dtls_session_create(&dtls_sock, &remote, &session);
if (res < 0) {
res = sock_dtls_session_init(&dtls_sock, &remote, &session);
if (res <= 0) {
return res;
}
res = sock_dtls_recv(&dtls_sock, &session, buf, sizeof(buf),
SOCK_NO_TIMEOUT);
if (res != -SOCK_DTLS_HANDSHAKE) {
printf("Error creating session: %d\n", (int)res);
sock_dtls_close(&dtls_sock);
sock_udp_close(&udp_sock);
return -1;
}
printf("Connection to server successful\n");
if (sock_dtls_send(&dtls_sock, &session, data, datalen) < 0) {
if (sock_dtls_send(&dtls_sock, &session, data, datalen, 0) < 0) {
puts("Error sending data");
}
else {
printf("Sent DTLS message\n");
uint8_t rcv[512];
if (sock_dtls_recv(&dtls_sock, &session, rcv, sizeof(rcv), SOCK_NO_TIMEOUT) < 0) {
printf("Error receiving DTLS message\n");
}
else {
printf("Received DTLS message\n");
if ((res = sock_dtls_recv(&dtls_sock, &session, rcv, sizeof(rcv),
SOCK_NO_TIMEOUT)) >= 0) {
printf("Received %d bytes: \"%.*s\"\n", (int)res, (int)res,
(char *)rcv);
}
}

View File

@ -108,22 +108,22 @@ void *dtls_server_wrapper(void *arg)
}
while (active) {
if ((msg_try_receive(&msg) == 1) && (msg.type == DTLS_STOP_SERVER_MSG)) {
if ((msg_try_receive(&msg) == 1) &&
(msg.type == DTLS_STOP_SERVER_MSG)){
active = false;
}
else {
res = sock_dtls_recv(&sock, &session, rcv, sizeof(rcv),
10 * US_PER_SEC);
if (res < 0) {
if (res != -ETIMEDOUT) {
printf("Error receiving UDP over DTLS %d", (int)res);
if (res >= 0) {
printf("Received %d bytes -- (echo)\n", (int)res);
res = sock_dtls_send(&sock, &session, rcv, (size_t)res, 0);
if (res < 0) {
printf("Error resending DTLS message: %d", (int)res);
}
continue;
}
printf("Received %d bytes -- (echo!)\n", (int)res);
res = sock_dtls_send(&sock, &session, rcv, (size_t)res);
if (res < 0) {
printf("Error resending DTLS message: %d", (int)res);
else if (res == -SOCK_DTLS_HANDSHAKE) {
printf("New client connected\n");
}
}
}