Change UDPConnection to set listen address

Add some documentation, fix NetworkTest with new change.
This commit is contained in:
Stephen Seo 2019-03-06 19:53:15 +09:00
parent 71fd812137
commit 2bd2e868b2
3 changed files with 21 additions and 10 deletions

View file

@ -6,7 +6,7 @@
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
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)); UDPC_Context *context = malloc(sizeof(UDPC_Context));
context->error = UDPC_SUCCESS; context->error = UDPC_SUCCESS;
@ -44,8 +44,8 @@ UDPC_Context* UDPC_init(uint16_t listenPort, int isClient)
// bind socket // bind socket
context->socketInfo.sin_family = AF_INET; context->socketInfo.sin_family = AF_INET;
// TODO specify what addr to listen on context->socketInfo.sin_addr.s_addr =
context->socketInfo.sin_addr.s_addr = INADDR_ANY; (listenAddr == 0 ? INADDR_ANY : listenAddr);
context->socketInfo.sin_port = htons(listenPort); context->socketInfo.sin_port = htons(listenPort);
if(bind( if(bind(
context->socketHandle, context->socketHandle,
@ -85,9 +85,9 @@ UDPC_Context* UDPC_init(uint16_t listenPort, int isClient)
return context; 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); context->error = mtx_init(&context->tCVMtx, mtx_timed);
if(context->error != thrd_success) if(context->error != thrd_success)

View file

@ -137,9 +137,9 @@ typedef struct {
UDPC_Context *ctx; UDPC_Context *ctx;
} UDPC_INTERNAL_update_struct; } 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); 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); 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); uint32_t UDPC_strtoa(const char *addrStr);
#endif #endif

View file

@ -14,7 +14,8 @@ typedef struct
void printUsage() void printUsage()
{ {
printf("Usage: [-c] [-t] -a <addr> -p <target_port> -l <listen_port>\n"); printf("Usage: [-c] [-t] -a <addr> -p <target_port> -l <listen_port>"
" [-d <listen_address>]\n");
} }
void conCallback(void *userdata, uint32_t addr) void conCallback(void *userdata, uint32_t addr)
@ -64,6 +65,7 @@ int main(int argc, char** argv)
int isThreaded = 0; int isThreaded = 0;
uint32_t targetAddress = 0; uint32_t targetAddress = 0;
uint16_t targetPort = 0; uint16_t targetPort = 0;
uint32_t listenAddress = 0;
uint16_t listenPort = 0; uint16_t listenPort = 0;
TestContext testCtx = {0, 0}; TestContext testCtx = {0, 0};
@ -100,6 +102,11 @@ int main(int argc, char** argv)
listenPort = strtoul(argv[1], NULL, 10); listenPort = strtoul(argv[1], NULL, 10);
--argc; ++argv; --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) else if(strcmp("-h", argv[0]) == 0 || strcmp("--help", argv[0]) == 0)
{ {
printUsage(); printUsage();
@ -111,11 +118,11 @@ int main(int argc, char** argv)
UDPC_Context *ctx; UDPC_Context *ctx;
if(isThreaded == 0) if(isThreaded == 0)
{ {
ctx = UDPC_init(listenPort, isClient); ctx = UDPC_init(listenPort, listenAddress, isClient);
} }
else 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", printf("isClient: %s, targetAddr: %s, targetPort: %u, listenPort: %u\n",