mirror of
https://github.com/wolfSSL/wolfssl.git
synced 2025-08-05 13:44:41 +02:00
add -u for DTLS UPD command line client/server examples
This commit is contained in:
@@ -299,7 +299,7 @@ static INLINE void showPeer(CYASSL* ssl)
|
|||||||
|
|
||||||
|
|
||||||
static INLINE void tcp_socket(SOCKET_T* sockfd, SOCKADDR_IN_T* addr,
|
static INLINE void tcp_socket(SOCKET_T* sockfd, SOCKADDR_IN_T* addr,
|
||||||
const char* peer, word16 port)
|
const char* peer, word16 port, int udp)
|
||||||
{
|
{
|
||||||
#ifndef TEST_IPV6
|
#ifndef TEST_IPV6
|
||||||
const char* host = peer;
|
const char* host = peer;
|
||||||
@@ -320,11 +320,10 @@ static INLINE void tcp_socket(SOCKET_T* sockfd, SOCKADDR_IN_T* addr,
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CYASSL_DTLS
|
if (udp)
|
||||||
*sockfd = socket(AF_INET_V, SOCK_DGRAM, 0);
|
*sockfd = socket(AF_INET_V, SOCK_DGRAM, 0);
|
||||||
#else
|
else
|
||||||
*sockfd = socket(AF_INET_V, SOCK_STREAM, 0);
|
*sockfd = socket(AF_INET_V, SOCK_STREAM, 0);
|
||||||
#endif
|
|
||||||
memset(addr, 0, sizeof(SOCKADDR_IN_T));
|
memset(addr, 0, sizeof(SOCKADDR_IN_T));
|
||||||
|
|
||||||
#ifndef TEST_IPV6
|
#ifndef TEST_IPV6
|
||||||
@@ -351,7 +350,8 @@ static INLINE void tcp_socket(SOCKET_T* sockfd, SOCKADDR_IN_T* addr,
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(TCP_NODELAY) && !defined(CYASSL_DTLS)
|
#if defined(TCP_NODELAY)
|
||||||
|
if (!udp)
|
||||||
{
|
{
|
||||||
int on = 1;
|
int on = 1;
|
||||||
socklen_t len = sizeof(on);
|
socklen_t len = sizeof(on);
|
||||||
@@ -364,26 +364,28 @@ static INLINE void tcp_socket(SOCKET_T* sockfd, SOCKADDR_IN_T* addr,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static INLINE void tcp_connect(SOCKET_T* sockfd, const char* ip, word16 port)
|
static INLINE void tcp_connect(SOCKET_T* sockfd, const char* ip, word16 port,
|
||||||
|
int udp)
|
||||||
{
|
{
|
||||||
SOCKADDR_IN_T addr;
|
SOCKADDR_IN_T addr;
|
||||||
tcp_socket(sockfd, &addr, ip, port);
|
tcp_socket(sockfd, &addr, ip, port, udp);
|
||||||
|
|
||||||
if (connect(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0)
|
if (connect(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0)
|
||||||
err_sys("tcp connect failed");
|
err_sys("tcp connect failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static INLINE void tcp_listen(SOCKET_T* sockfd, int port, int useAnyAddr)
|
static INLINE void tcp_listen(SOCKET_T* sockfd, int port, int useAnyAddr,
|
||||||
|
int udp)
|
||||||
{
|
{
|
||||||
SOCKADDR_IN_T addr;
|
SOCKADDR_IN_T addr;
|
||||||
|
|
||||||
/* don't use INADDR_ANY by default, firewall may block, make user switch
|
/* don't use INADDR_ANY by default, firewall may block, make user switch
|
||||||
on */
|
on */
|
||||||
if (useAnyAddr)
|
if (useAnyAddr)
|
||||||
tcp_socket(sockfd, &addr, INADDR_ANY, port);
|
tcp_socket(sockfd, &addr, INADDR_ANY, port, udp);
|
||||||
else
|
else
|
||||||
tcp_socket(sockfd, &addr, yasslIP, port);
|
tcp_socket(sockfd, &addr, yasslIP, port, udp);
|
||||||
|
|
||||||
#ifndef USE_WINDOWS_API
|
#ifndef USE_WINDOWS_API
|
||||||
{
|
{
|
||||||
@@ -395,10 +397,10 @@ static INLINE void tcp_listen(SOCKET_T* sockfd, int port, int useAnyAddr)
|
|||||||
|
|
||||||
if (bind(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0)
|
if (bind(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0)
|
||||||
err_sys("tcp bind failed");
|
err_sys("tcp bind failed");
|
||||||
#ifndef CYASSL_DTLS
|
if (!udp) {
|
||||||
if (listen(*sockfd, 5) != 0)
|
if (listen(*sockfd, 5) != 0)
|
||||||
err_sys("tcp listen failed");
|
err_sys("tcp listen failed");
|
||||||
#endif
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -426,7 +428,7 @@ static INLINE void udp_accept(SOCKET_T* sockfd, int* clientfd, func_args* args)
|
|||||||
{
|
{
|
||||||
SOCKADDR_IN_T addr;
|
SOCKADDR_IN_T addr;
|
||||||
|
|
||||||
tcp_socket(sockfd, &addr, yasslIP, yasslPort);
|
tcp_socket(sockfd, &addr, yasslIP, yasslPort, 1);
|
||||||
|
|
||||||
|
|
||||||
#ifndef USE_WINDOWS_API
|
#ifndef USE_WINDOWS_API
|
||||||
@@ -455,17 +457,17 @@ static INLINE void udp_accept(SOCKET_T* sockfd, int* clientfd, func_args* args)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static INLINE void tcp_accept(SOCKET_T* sockfd, int* clientfd, func_args* args,
|
static INLINE void tcp_accept(SOCKET_T* sockfd, int* clientfd, func_args* args,
|
||||||
int port, int useAnyAddr)
|
int port, int useAnyAddr, int udp)
|
||||||
{
|
{
|
||||||
SOCKADDR_IN_T client;
|
SOCKADDR_IN_T client;
|
||||||
socklen_t client_len = sizeof(client);
|
socklen_t client_len = sizeof(client);
|
||||||
|
|
||||||
#ifdef CYASSL_DTLS
|
if (udp) {
|
||||||
udp_accept(sockfd, clientfd, args);
|
udp_accept(sockfd, clientfd, args);
|
||||||
return;
|
return;
|
||||||
#endif
|
}
|
||||||
|
|
||||||
tcp_listen(sockfd, port, useAnyAddr);
|
tcp_listen(sockfd, port, useAnyAddr, udp);
|
||||||
|
|
||||||
#if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER)
|
#if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER)
|
||||||
/* signal ready to tcp_accept */
|
/* signal ready to tcp_accept */
|
||||||
|
@@ -119,6 +119,7 @@ void client_test(void* args)
|
|||||||
int usePsk = 0;
|
int usePsk = 0;
|
||||||
int sendGET = 0;
|
int sendGET = 0;
|
||||||
int benchmark = 0;
|
int benchmark = 0;
|
||||||
|
int doDTLS = 0;
|
||||||
int doPeerCheck = 1;
|
int doPeerCheck = 1;
|
||||||
char* cipherList = NULL;
|
char* cipherList = NULL;
|
||||||
char* verifyCert = (char*)caCert;
|
char* verifyCert = (char*)caCert;
|
||||||
@@ -130,7 +131,7 @@ void client_test(void* args)
|
|||||||
|
|
||||||
((func_args*)args)->return_code = -1; /* error state */
|
((func_args*)args)->return_code = -1; /* error state */
|
||||||
|
|
||||||
while ((ch = mygetopt(argc, argv, "?gdsh:p:v:l:A:c:k:b:")) != -1) {
|
while ((ch = mygetopt(argc, argv, "?gdush:p:v:l:A:c:k:b:")) != -1) {
|
||||||
switch (ch) {
|
switch (ch) {
|
||||||
case '?' :
|
case '?' :
|
||||||
Usage();
|
Usage();
|
||||||
@@ -144,6 +145,11 @@ void client_test(void* args)
|
|||||||
doPeerCheck = 0;
|
doPeerCheck = 0;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'u' :
|
||||||
|
doDTLS = 1;
|
||||||
|
version = -1; /* DTLS flag */
|
||||||
|
break;
|
||||||
|
|
||||||
case 's' :
|
case 's' :
|
||||||
usePsk = 1;
|
usePsk = 1;
|
||||||
break;
|
break;
|
||||||
@@ -163,6 +169,8 @@ void client_test(void* args)
|
|||||||
Usage();
|
Usage();
|
||||||
exit(MY_EX_USAGE);
|
exit(MY_EX_USAGE);
|
||||||
}
|
}
|
||||||
|
if (doDTLS)
|
||||||
|
version = -1; /* DTLS flag */
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'l' :
|
case 'l' :
|
||||||
@@ -216,6 +224,12 @@ void client_test(void* args)
|
|||||||
method = CyaTLSv1_2_client_method();
|
method = CyaTLSv1_2_client_method();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
#ifdef CYASSL_DTLS
|
||||||
|
case -1:
|
||||||
|
method = CyaDTLSv1_client_method();
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
default:
|
default:
|
||||||
err_sys("Bad SSL version");
|
err_sys("Bad SSL version");
|
||||||
}
|
}
|
||||||
@@ -279,7 +293,7 @@ void client_test(void* args)
|
|||||||
double start = current_time(), avg;
|
double start = current_time(), avg;
|
||||||
|
|
||||||
for (i = 0; i < times; i++) {
|
for (i = 0; i < times; i++) {
|
||||||
tcp_connect(&sockfd, host, port);
|
tcp_connect(&sockfd, host, port, doDTLS);
|
||||||
ssl = CyaSSL_new(ctx);
|
ssl = CyaSSL_new(ctx);
|
||||||
CyaSSL_set_fd(ssl, sockfd);
|
CyaSSL_set_fd(ssl, sockfd);
|
||||||
if (CyaSSL_connect(ssl) != SSL_SUCCESS)
|
if (CyaSSL_connect(ssl) != SSL_SUCCESS)
|
||||||
@@ -300,7 +314,7 @@ void client_test(void* args)
|
|||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
tcp_connect(&sockfd, host, port);
|
tcp_connect(&sockfd, host, port, doDTLS);
|
||||||
ssl = CyaSSL_new(ctx);
|
ssl = CyaSSL_new(ctx);
|
||||||
if (ssl == NULL)
|
if (ssl == NULL)
|
||||||
err_sys("unable to get SSL object");
|
err_sys("unable to get SSL object");
|
||||||
@@ -358,12 +372,12 @@ void client_test(void* args)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef TEST_RESUME
|
#ifdef TEST_RESUME
|
||||||
#ifdef CYASSL_DTLS
|
if (doDTLS) {
|
||||||
strncpy(msg, "break", 6);
|
strncpy(msg, "break", 6);
|
||||||
msgSz = (int)strlen(msg);
|
msgSz = (int)strlen(msg);
|
||||||
/* try to send session close */
|
/* try to send session close */
|
||||||
CyaSSL_write(ssl, msg, msgSz);
|
CyaSSL_write(ssl, msg, msgSz);
|
||||||
#endif
|
}
|
||||||
session = CyaSSL_get_session(ssl);
|
session = CyaSSL_get_session(ssl);
|
||||||
sslResume = CyaSSL_new(ctx);
|
sslResume = CyaSSL_new(ctx);
|
||||||
#endif
|
#endif
|
||||||
@@ -373,13 +387,13 @@ void client_test(void* args)
|
|||||||
CloseSocket(sockfd);
|
CloseSocket(sockfd);
|
||||||
|
|
||||||
#ifdef TEST_RESUME
|
#ifdef TEST_RESUME
|
||||||
#ifdef CYASSL_DTLS
|
if (doDTLS) {
|
||||||
#ifdef USE_WINDOWS_API
|
#ifdef USE_WINDOWS_API
|
||||||
Sleep(500);
|
Sleep(500);
|
||||||
#else
|
#else
|
||||||
sleep(1);
|
sleep(1);
|
||||||
#endif
|
#endif
|
||||||
#endif
|
}
|
||||||
tcp_connect(&sockfd, host, port);
|
tcp_connect(&sockfd, host, port);
|
||||||
CyaSSL_set_fd(sslResume, sockfd);
|
CyaSSL_set_fd(sslResume, sockfd);
|
||||||
CyaSSL_set_session(sslResume, session);
|
CyaSSL_set_session(sslResume, session);
|
||||||
|
@@ -44,6 +44,7 @@ void echoclient_test(void* args)
|
|||||||
SSL_CTX* ctx = 0;
|
SSL_CTX* ctx = 0;
|
||||||
SSL* ssl = 0;
|
SSL* ssl = 0;
|
||||||
|
|
||||||
|
int doDTLS = 0;
|
||||||
int sendSz;
|
int sendSz;
|
||||||
int argc = 0;
|
int argc = 0;
|
||||||
char** argv = 0;
|
char** argv = 0;
|
||||||
@@ -64,7 +65,11 @@ void echoclient_test(void* args)
|
|||||||
if (!fin) err_sys("can't open input file");
|
if (!fin) err_sys("can't open input file");
|
||||||
if (!fout) err_sys("can't open output file");
|
if (!fout) err_sys("can't open output file");
|
||||||
|
|
||||||
tcp_connect(&sockfd, yasslIP, yasslPort);
|
#ifdef CYASSL_DTLS
|
||||||
|
doDTLS = 1;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
tcp_connect(&sockfd, yasslIP, yasslPort, doDTLS);
|
||||||
|
|
||||||
#if defined(CYASSL_DTLS)
|
#if defined(CYASSL_DTLS)
|
||||||
method = DTLSv1_client_method();
|
method = DTLSv1_client_method();
|
||||||
|
@@ -56,6 +56,7 @@ THREAD_RETURN CYASSL_THREAD echoserver_test(void* args)
|
|||||||
CYASSL_METHOD* method = 0;
|
CYASSL_METHOD* method = 0;
|
||||||
CYASSL_CTX* ctx = 0;
|
CYASSL_CTX* ctx = 0;
|
||||||
|
|
||||||
|
int doDTLS = 0;
|
||||||
int outCreated = 0;
|
int outCreated = 0;
|
||||||
int shutdown = 0;
|
int shutdown = 0;
|
||||||
int useAnyAddr = 0;
|
int useAnyAddr = 0;
|
||||||
@@ -73,7 +74,11 @@ THREAD_RETURN CYASSL_THREAD echoserver_test(void* args)
|
|||||||
|
|
||||||
((func_args*)args)->return_code = -1; /* error state */
|
((func_args*)args)->return_code = -1; /* error state */
|
||||||
|
|
||||||
tcp_listen(&sockfd, yasslPort, useAnyAddr);
|
#ifdef CYASSL_DTLS
|
||||||
|
doDTLS = 1;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
tcp_listen(&sockfd, yasslPort, useAnyAddr, doDTLS);
|
||||||
|
|
||||||
#if defined(CYASSL_DTLS)
|
#if defined(CYASSL_DTLS)
|
||||||
method = CyaDTLSv1_server_method();
|
method = CyaDTLSv1_server_method();
|
||||||
@@ -237,7 +242,7 @@ THREAD_RETURN CYASSL_THREAD echoserver_test(void* args)
|
|||||||
CyaSSL_free(ssl);
|
CyaSSL_free(ssl);
|
||||||
CloseSocket(clientfd);
|
CloseSocket(clientfd);
|
||||||
#ifdef CYASSL_DTLS
|
#ifdef CYASSL_DTLS
|
||||||
tcp_listen(&sockfd, yasslPort, useAnyAddr);
|
tcp_listen(&sockfd, yasslPort, useAnyAddr, doDTLS);
|
||||||
SignalReady(args);
|
SignalReady(args);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
@@ -78,6 +78,7 @@ static void Usage(void)
|
|||||||
printf("-d Disable client cert check\n");
|
printf("-d Disable client cert check\n");
|
||||||
printf("-b Bind to any interface instead of localhost only\n");
|
printf("-b Bind to any interface instead of localhost only\n");
|
||||||
printf("-s Use pre Shared keys\n");
|
printf("-s Use pre Shared keys\n");
|
||||||
|
printf("-u Use UDP DTLS\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -99,6 +100,7 @@ THREAD_RETURN CYASSL_THREAD server_test(void* args)
|
|||||||
int useAnyAddr = 0;
|
int useAnyAddr = 0;
|
||||||
int port = yasslPort;
|
int port = yasslPort;
|
||||||
int usePsk = 0;
|
int usePsk = 0;
|
||||||
|
int doDTLS = 0;
|
||||||
char* cipherList = NULL;
|
char* cipherList = NULL;
|
||||||
char* verifyCert = (char*)cliCert;
|
char* verifyCert = (char*)cliCert;
|
||||||
char* ourCert = (char*)svrCert;
|
char* ourCert = (char*)svrCert;
|
||||||
@@ -108,7 +110,7 @@ THREAD_RETURN CYASSL_THREAD server_test(void* args)
|
|||||||
|
|
||||||
((func_args*)args)->return_code = -1; /* error state */
|
((func_args*)args)->return_code = -1; /* error state */
|
||||||
|
|
||||||
while ((ch = mygetopt(argc, argv, "?dbsp:v:l:A:c:k:")) != -1) {
|
while ((ch = mygetopt(argc, argv, "?dbsup:v:l:A:c:k:")) != -1) {
|
||||||
switch (ch) {
|
switch (ch) {
|
||||||
case '?' :
|
case '?' :
|
||||||
Usage();
|
Usage();
|
||||||
@@ -126,6 +128,11 @@ THREAD_RETURN CYASSL_THREAD server_test(void* args)
|
|||||||
usePsk = 1;
|
usePsk = 1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'u' :
|
||||||
|
doDTLS = 1;
|
||||||
|
version = -1; /* DTLS flag */
|
||||||
|
break;
|
||||||
|
|
||||||
case 'p' :
|
case 'p' :
|
||||||
port = atoi(myoptarg);
|
port = atoi(myoptarg);
|
||||||
break;
|
break;
|
||||||
@@ -136,6 +143,8 @@ THREAD_RETURN CYASSL_THREAD server_test(void* args)
|
|||||||
Usage();
|
Usage();
|
||||||
exit(MY_EX_USAGE);
|
exit(MY_EX_USAGE);
|
||||||
}
|
}
|
||||||
|
if (doDTLS)
|
||||||
|
version = -1; /* stay with DTLS */
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'l' :
|
case 'l' :
|
||||||
@@ -181,6 +190,12 @@ THREAD_RETURN CYASSL_THREAD server_test(void* args)
|
|||||||
method = TLSv1_2_server_method();
|
method = TLSv1_2_server_method();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
#ifdef CYASSL_DTLS
|
||||||
|
case -1:
|
||||||
|
method = DTLSv1_server_method();
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
default:
|
default:
|
||||||
err_sys("Bad SSL version");
|
err_sys("Bad SSL version");
|
||||||
}
|
}
|
||||||
@@ -244,10 +259,9 @@ THREAD_RETURN CYASSL_THREAD server_test(void* args)
|
|||||||
CYASSL_CRL_START_MON);
|
CYASSL_CRL_START_MON);
|
||||||
CyaSSL_SetCRL_Cb(ssl, CRL_CallBack);
|
CyaSSL_SetCRL_Cb(ssl, CRL_CallBack);
|
||||||
#endif
|
#endif
|
||||||
tcp_accept(&sockfd, &clientfd, (func_args*)args, port, useAnyAddr);
|
tcp_accept(&sockfd, &clientfd, (func_args*)args, port, useAnyAddr, doDTLS);
|
||||||
#ifndef CYASSL_DTLS
|
if (!doDTLS)
|
||||||
CloseSocket(sockfd);
|
CloseSocket(sockfd);
|
||||||
#endif
|
|
||||||
|
|
||||||
SSL_set_fd(ssl, clientfd);
|
SSL_set_fd(ssl, clientfd);
|
||||||
#ifdef NO_PSK
|
#ifdef NO_PSK
|
||||||
|
@@ -603,7 +603,7 @@ THREAD_RETURN CYASSL_THREAD test_server_nofail(void* args)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
ssl = CyaSSL_new(ctx);
|
ssl = CyaSSL_new(ctx);
|
||||||
tcp_accept(&sockfd, &clientfd, (func_args*)args, yasslPort, 0);
|
tcp_accept(&sockfd, &clientfd, (func_args*)args, yasslPort, 0, 0);
|
||||||
#ifndef CYASSL_DTLS
|
#ifndef CYASSL_DTLS
|
||||||
CloseSocket(sockfd);
|
CloseSocket(sockfd);
|
||||||
#endif
|
#endif
|
||||||
@@ -691,7 +691,7 @@ void test_client_nofail(void* args)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
tcp_connect(&sockfd, yasslIP, yasslPort);
|
tcp_connect(&sockfd, yasslIP, yasslPort, 0);
|
||||||
|
|
||||||
ssl = CyaSSL_new(ctx);
|
ssl = CyaSSL_new(ctx);
|
||||||
CyaSSL_set_fd(ssl, sockfd);
|
CyaSSL_set_fd(ssl, sockfd);
|
||||||
|
Reference in New Issue
Block a user