diff options
author | Holger Hans Peter Freyther <holger@moiji-mobile.com> | 2014-10-01 08:18:43 +0200 |
---|---|---|
committer | Holger Hans Peter Freyther <holger@moiji-mobile.com> | 2014-10-01 08:18:43 +0200 |
commit | b7c45ce7bec29094180998d0a6aad88607ca3bf6 (patch) | |
tree | 4898c1db17e803ccfbc5087968af5fc15d2d714c | |
parent | 84f2905c5bfed2e1f5bae52a2bb9bc771fd8b895 (diff) |
test: Be able to bind to a local source/port
Modify the code to show how to configure it.
-rw-r--r-- | test_apps/esme.xml | 2 | ||||
-rw-r--r-- | 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 @@ <?xml version="1.0"?> <config> - <conn_tcp host="127.0.0.1" port="9090"/> + <conn_tcp host="127.0.0.1" port="9090" src_addr="127.0.0.1" src_port="6666"/> <conn_smpp system_id="sytem" password="asdfg34" system_type="type01"/> <smpp_msg src="5565" dst="0911110000" msg="Este es un ejemplo 01"/> </config> 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 <sys/types.h> #include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> #include <netdb.h> #include <stdio.h> #include <unistd.h> @@ -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 */ |