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