From b7c45ce7bec29094180998d0a6aad88607ca3bf6 Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Wed, 1 Oct 2014 08:18:43 +0200 Subject: test: Be able to bind to a local source/port Modify the code to show how to configure it. --- test_apps/esme.xml | 2 +- test_apps/tcp.c | 21 +++++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/test_apps/esme.xml b/test_apps/esme.xml index 7669880..eb0d08d 100644 --- a/test_apps/esme.xml +++ b/test_apps/esme.xml @@ -1,6 +1,6 @@ - + diff --git a/test_apps/tcp.c b/test_apps/tcp.c index 5733294..4e24fa5 100644 --- a/test_apps/tcp.c +++ b/test_apps/tcp.c @@ -24,6 +24,8 @@ #include #include +#include +#include #include #include #include @@ -46,13 +48,15 @@ int do_tcp_connect( xmlNodePtr p, int *s ) struct in_addr addr; struct sockaddr_in name; - char h[256]; + char h[256], local_src[256]; char ahost[1024]; - int port = 0; + int port = 0, local_port = 0; GET_PROP_STR(h, p, "host"); GET_PROP_INT(port, p, "port"); + GET_PROP_STR(local_src, p, "src_addr"); + GET_PROP_INT(local_port, p, "src_port"); if((*s = socket(AF_INET, SOCK_STREAM, 0)) == -1){ printf("Error in socket()\n"); @@ -63,6 +67,19 @@ int do_tcp_connect( xmlNodePtr p, int *s ) ret = -1; goto lb_tcp_connect_end; }; + /* bind to a local addr */ + if (strlen(local_src) != 0) { + struct sockaddr_in name; + name.sin_family = AF_INET; + name.sin_port = htons(local_port); + name.sin_addr.s_addr = inet_addr(local_src); + + if ( bind(*s, (struct sockaddr *) &name, sizeof(name)) < 0){ + printf("Error in bind().\n"); + ret = -1; goto lb_tcp_connect_end; + } + } + #if defined(__linux__) || defined(__FreeBSD__) if( gethostbyname_r(h,&_host,ahost,sizeof(ahost),&__host_result,&n) != 0) #else /* solaris */ -- cgit v1.2.3