From 2bd2e868b29846ed4b533bf46d4d033f81e3d3c5 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Wed, 6 Mar 2019 19:53:15 +0900 Subject: [PATCH] Change UDPConnection to set listen address Add some documentation, fix NetworkTest with new change. --- src/UDPConnection.c | 10 +++++----- src/UDPConnection.h | 8 ++++++-- src/test/UDPC_NetworkTest.c | 13 ++++++++++--- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/UDPConnection.c b/src/UDPConnection.c index f626001..ad3966b 100644 --- a/src/UDPConnection.c +++ b/src/UDPConnection.c @@ -6,7 +6,7 @@ #include #include -UDPC_Context* UDPC_init(uint16_t listenPort, int isClient) +UDPC_Context* UDPC_init(uint16_t listenPort, uint32_t listenAddr, int isClient) { UDPC_Context *context = malloc(sizeof(UDPC_Context)); context->error = UDPC_SUCCESS; @@ -44,8 +44,8 @@ UDPC_Context* UDPC_init(uint16_t listenPort, int isClient) // bind socket context->socketInfo.sin_family = AF_INET; - // TODO specify what addr to listen on - context->socketInfo.sin_addr.s_addr = INADDR_ANY; + context->socketInfo.sin_addr.s_addr = + (listenAddr == 0 ? INADDR_ANY : listenAddr); context->socketInfo.sin_port = htons(listenPort); if(bind( context->socketHandle, @@ -85,9 +85,9 @@ UDPC_Context* UDPC_init(uint16_t listenPort, int isClient) return context; } -UDPC_Context* UDPC_init_threaded_update(uint16_t listenPort, int isClient) +UDPC_Context* UDPC_init_threaded_update(uint16_t listenPort, uint32_t listenAddr, int isClient) { - UDPC_Context *context = UDPC_init(listenPort, isClient); + UDPC_Context *context = UDPC_init(listenPort, listenAddr, isClient); context->error = mtx_init(&context->tCVMtx, mtx_timed); if(context->error != thrd_success) diff --git a/src/UDPConnection.h b/src/UDPConnection.h index af5cdc2..5ebad77 100644 --- a/src/UDPConnection.h +++ b/src/UDPConnection.h @@ -137,9 +137,9 @@ typedef struct { UDPC_Context *ctx; } UDPC_INTERNAL_update_struct; -UDPC_Context* UDPC_init(uint16_t listenPort, int isClient); +UDPC_Context* UDPC_init(uint16_t listenPort, uint32_t listenAddr, int isClient); -UDPC_Context* UDPC_init_threaded_update(uint16_t listenPort, int isClient); +UDPC_Context* UDPC_init_threaded_update(uint16_t listenPort, uint32_t listenAddr, int isClient); void UDPC_destroy(UDPC_Context *ctx); @@ -240,6 +240,10 @@ uint32_t UDPC_INTERNAL_generate_id(UDPC_Context *ctx); void UDPC_INTERNAL_check_ids(void *userData, uint32_t addr, char *data); +/*1 + * \brief Converts a IPV4 string to a 32-bit unsigned integer address in big-endian + * \return 0 if string is invalid, address in big-endian format otherwise + */ uint32_t UDPC_strtoa(const char *addrStr); #endif diff --git a/src/test/UDPC_NetworkTest.c b/src/test/UDPC_NetworkTest.c index d3b1819..380e472 100644 --- a/src/test/UDPC_NetworkTest.c +++ b/src/test/UDPC_NetworkTest.c @@ -14,7 +14,8 @@ typedef struct void printUsage() { - printf("Usage: [-c] [-t] -a -p -l \n"); + printf("Usage: [-c] [-t] -a -p -l " + " [-d ]\n"); } void conCallback(void *userdata, uint32_t addr) @@ -64,6 +65,7 @@ int main(int argc, char** argv) int isThreaded = 0; uint32_t targetAddress = 0; uint16_t targetPort = 0; + uint32_t listenAddress = 0; uint16_t listenPort = 0; TestContext testCtx = {0, 0}; @@ -100,6 +102,11 @@ int main(int argc, char** argv) listenPort = strtoul(argv[1], NULL, 10); --argc; ++argv; } + else if(strcmp("-d", argv[0]) == 0 && argc > 1) + { + listenAddress = UDPC_strtoa(argv[1]); + --argc; ++argv; + } else if(strcmp("-h", argv[0]) == 0 || strcmp("--help", argv[0]) == 0) { printUsage(); @@ -111,11 +118,11 @@ int main(int argc, char** argv) UDPC_Context *ctx; if(isThreaded == 0) { - ctx = UDPC_init(listenPort, isClient); + ctx = UDPC_init(listenPort, listenAddress, isClient); } else { - ctx = UDPC_init_threaded_update(listenPort, isClient); + ctx = UDPC_init_threaded_update(listenPort, listenAddress, isClient); } printf("isClient: %s, targetAddr: %s, targetPort: %u, listenPort: %u\n",