Skip to content

Commit

Permalink
dtls-server.c: apply source code formating.
Browse files Browse the repository at this point in the history
Add license header.

Signed-off-by: Achim Kraus <achim.kraus@cloudcoap.net>
  • Loading branch information
boaks committed Jan 24, 2023
1 parent 5aca36f commit 0c4ddd0
Showing 1 changed file with 74 additions and 58 deletions.
132 changes: 74 additions & 58 deletions tests/dtls-server.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
/*******************************************************************************
*
* Copyright (c) 2022 Contributors to the Eclipse Foundation.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompanies this distribution.
*
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
*******************************************************************************/

/* This is needed for apple */
#define __APPLE_USE_RFC_3542
Expand All @@ -13,8 +26,8 @@
#endif /* HAVE_SYS_TIME_H */
#include <signal.h>

#include "tinydtls.h"
#include "dtls.h"
#include "tinydtls.h"
#include "dtls.h"
#include "dtls_debug.h"

#ifdef IS_WINDOWS
Expand All @@ -34,22 +47,22 @@ static dtls_context_t *the_context = NULL;

#ifdef DTLS_ECC
static const unsigned char ecdsa_priv_key[] = {
0xD9, 0xE2, 0x70, 0x7A, 0x72, 0xDA, 0x6A, 0x05,
0x04, 0x99, 0x5C, 0x86, 0xED, 0xDB, 0xE3, 0xEF,
0xC7, 0xF1, 0xCD, 0x74, 0x83, 0x8F, 0x75, 0x70,
0xC8, 0x07, 0x2D, 0x0A, 0x76, 0x26, 0x1B, 0xD4};
0xD9, 0xE2, 0x70, 0x7A, 0x72, 0xDA, 0x6A, 0x05,
0x04, 0x99, 0x5C, 0x86, 0xED, 0xDB, 0xE3, 0xEF,
0xC7, 0xF1, 0xCD, 0x74, 0x83, 0x8F, 0x75, 0x70,
0xC8, 0x07, 0x2D, 0x0A, 0x76, 0x26, 0x1B, 0xD4};

static const unsigned char ecdsa_pub_key_x[] = {
0xD0, 0x55, 0xEE, 0x14, 0x08, 0x4D, 0x6E, 0x06,
0x15, 0x59, 0x9D, 0xB5, 0x83, 0x91, 0x3E, 0x4A,
0x3E, 0x45, 0x26, 0xA2, 0x70, 0x4D, 0x61, 0xF2,
0x7A, 0x4C, 0xCF, 0xBA, 0x97, 0x58, 0xEF, 0x9A};
0xD0, 0x55, 0xEE, 0x14, 0x08, 0x4D, 0x6E, 0x06,
0x15, 0x59, 0x9D, 0xB5, 0x83, 0x91, 0x3E, 0x4A,
0x3E, 0x45, 0x26, 0xA2, 0x70, 0x4D, 0x61, 0xF2,
0x7A, 0x4C, 0xCF, 0xBA, 0x97, 0x58, 0xEF, 0x9A};

static const unsigned char ecdsa_pub_key_y[] = {
0xB4, 0x18, 0xB6, 0x4A, 0xFE, 0x80, 0x30, 0xDA,
0x1D, 0xDC, 0xF4, 0xF4, 0x2E, 0x2F, 0x26, 0x31,
0xD0, 0x43, 0xB1, 0xFB, 0x03, 0xE2, 0x2F, 0x4D,
0x17, 0xDE, 0x43, 0xF9, 0xF9, 0xAD, 0xEE, 0x70};
0xB4, 0x18, 0xB6, 0x4A, 0xFE, 0x80, 0x30, 0xDA,
0x1D, 0xDC, 0xF4, 0xF4, 0x2E, 0x2F, 0x26, 0x31,
0xD0, 0x43, 0xB1, 0xFB, 0x03, 0xE2, 0x2F, 0x4D,
0x17, 0xDE, 0x43, 0xF9, 0xF9, 0xAD, 0xEE, 0x70};
#endif /* DTLS_ECC */

#ifdef DTLS_PSK
Expand All @@ -58,9 +71,9 @@ static const unsigned char ecdsa_pub_key_y[] = {
* session. */
static int
get_psk_info(struct dtls_context_t *ctx, const session_t *session,
dtls_credentials_type_t type,
const unsigned char *id, size_t id_len,
unsigned char *result, size_t result_length) {
dtls_credentials_type_t type,
const unsigned char *id, size_t id_len,
unsigned char *result, size_t result_length) {

struct keymap_t {
unsigned char *id;
Expand All @@ -86,13 +99,13 @@ get_psk_info(struct dtls_context_t *ctx, const session_t *session,
size_t i;
for (i = 0; i < sizeof(psk)/sizeof(struct keymap_t); i++) {
if (id_len == psk[i].id_length && memcmp(id, psk[i].id, id_len) == 0) {
if (result_length < psk[i].key_length) {
dtls_warn("buffer too small for PSK");
return dtls_alert_fatal_create(DTLS_ALERT_INTERNAL_ERROR);
}
if (result_length < psk[i].key_length) {
dtls_warn("buffer too small for PSK");
return dtls_alert_fatal_create(DTLS_ALERT_INTERNAL_ERROR);
}

memcpy(result, psk[i].key, psk[i].key_length);
return psk[i].key_length;
memcpy(result, psk[i].key, psk[i].key_length);
return psk[i].key_length;
}
}
}
Expand All @@ -105,8 +118,8 @@ get_psk_info(struct dtls_context_t *ctx, const session_t *session,
#ifdef DTLS_ECC
static int
get_ecdsa_key(struct dtls_context_t *ctx,
const session_t *session,
const dtls_ecdsa_key_t **result) {
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,
Expand All @@ -122,10 +135,10 @@ get_ecdsa_key(struct dtls_context_t *ctx,

static 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) {
const session_t *session,
const unsigned char *other_pub_x,
const unsigned char *other_pub_y,
size_t key_size) {
(void)ctx;
(void)session;
(void)other_pub_x;
Expand All @@ -138,8 +151,8 @@ verify_ecdsa_key(struct dtls_context_t *ctx,
#define DTLS_SERVER_CMD_CLOSE "server:close"

static int
read_from_peer(struct dtls_context_t *ctx,
session_t *session, uint8 *data, size_t len) {
read_from_peer(struct dtls_context_t *ctx,
session_t *session, uint8 *data, size_t len) {
if (write(STDOUT_FILENO, data, len) == -1)
dtls_debug("write failed: %s\n", strerror(errno));
if (len >= strlen(DTLS_SERVER_CMD_CLOSE) &&
Expand All @@ -153,12 +166,12 @@ read_from_peer(struct dtls_context_t *ctx,
}

static int
send_to_peer(struct dtls_context_t *ctx,
session_t *session, uint8 *data, size_t len) {
send_to_peer(struct dtls_context_t *ctx,
session_t *session, uint8 *data, size_t len) {

int fd = *(int *)dtls_get_app_data(ctx);
return sendto(fd, data, len, MSG_DONTWAIT,
&session->addr.sa, session->size);
&session->addr.sa, session->size);
}

static int
Expand All @@ -175,21 +188,21 @@ dtls_handle_read(struct dtls_context_t *ctx) {
memset(&session, 0, sizeof(session_t));
session.size = sizeof(session.addr);
len = recvfrom(*fd, buf, sizeof(buf), MSG_TRUNC,
&session.addr.sa, &session.size);
&session.addr.sa, &session.size);

if (len < 0) {
perror("recvfrom");
return -1;
} else {
dtls_debug("got %d bytes from port %d\n", len,
ntohs(session.addr.sin6.sin6_port));
ntohs(session.addr.sin6.sin6_port));
if (sizeof(buf) < (size_t)len) {
dtls_warn("packet was truncated (%ld bytes lost)\n", len - sizeof(buf));
}
}

return dtls_handle_message(ctx, &session, buf, len);
}
}

static void dtls_handle_signal(int sig)
{
Expand All @@ -200,7 +213,7 @@ static void dtls_handle_signal(int sig)

static int
resolve_address(const char *server, struct sockaddr *dst) {

struct addrinfo *res, *ainfo;
struct addrinfo hints;
static char addrstr[256];
Expand Down Expand Up @@ -249,12 +262,12 @@ usage(const char *program, const char *version) {
program = ++p;

fprintf(stderr, "%s v%s -- DTLS server implementation\n"
"(c) 2011-2014 Olaf Bergmann <bergmann@tzi.org>\n\n"
"usage: %s [-A address] [-p port] [-v num]\n"
"\t-A address\t\tlisten on specified address (default is ::)\n"
"\t-p port\t\tlisten on specified port (default is %d)\n"
"\t-v num\t\tverbosity level (default: 3)\n",
program, version, program, DEFAULT_PORT);
"(c) 2011-2014 Olaf Bergmann <bergmann@tzi.org>\n\n"
"usage: %s [-A address] [-p port] [-v num]\n"
"\t-A address\t\tlisten on specified address (default is ::)\n"
"\t-p port\t\tlisten on specified port (default is %d)\n"
"\t-v num\t\tverbosity level (default: 3)\n",
program, version, program, DEFAULT_PORT);
}

static dtls_handler_t cb = {
Expand All @@ -270,7 +283,7 @@ static dtls_handler_t cb = {
#endif /* DTLS_ECC */
};

int
int
main(int argc, char **argv) {
log_t log_level = DTLS_LOG_WARN;
fd_set rfds, wfds;
Expand Down Expand Up @@ -298,8 +311,8 @@ main(int argc, char **argv) {
switch (opt) {
case 'A' :
if (resolve_address(optarg, (struct sockaddr *)&listen_addr) < 0) {
fprintf(stderr, "cannot resolve address\n");
exit(-1);
fprintf(stderr, "cannot resolve address\n");
exit(-1);
}
break;
case 'p' :
Expand Down Expand Up @@ -383,26 +396,29 @@ main(int argc, char **argv) {

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 (result < 0) {
/* error */
if (errno != EINTR)
perror("select");
} else if (result == 0) { /* timeout */
} else { /* ok */
perror("select");
} else if (result == 0) {
/* timeout */
} else {
/* ok */
if (FD_ISSET(fd, &wfds))
;
/* FIXME */;
else if (FD_ISSET(fd, &rfds)) {
dtls_handle_read(the_context);
dtls_handle_read(the_context);
}
}
}
error:

error:
dtls_free_context(the_context);
exit(0);
}

0 comments on commit 0c4ddd0

Please sign in to comment.