[PATCH] drop option -a and be independent from interface ip-address

Hannes Petermaier oe5hpm at oevsv.at
Mon Mar 28 20:00:44 UTC 2016


From: Hannes Schmelzer <oe5hpm at oevsv.at>

It is very inconvenient to have some RTL-stick on a machine with
multiple network interfaces if it is bound to a specific one.

So we break up this and make this dynamically.

The -a option therefore is not needed any longer.

Signed-off-by: Hannes Schmelzer <oe5hpm at oevsv.at>
---
 src/rtl_tcp.c | 34 ++++++++++++++++++----------------
 1 file changed, 18 insertions(+), 16 deletions(-)

diff --git a/src/rtl_tcp.c b/src/rtl_tcp.c
index 317e0f3..39d7c3e 100644
--- a/src/rtl_tcp.c
+++ b/src/rtl_tcp.c
@@ -31,6 +31,7 @@
 #include <sys/socket.h>
 #include <sys/time.h>
 #include <netinet/in.h>
+#include <netdb.h>
 #include <fcntl.h>
 #else
 #include <winsock2.h>
@@ -365,10 +366,10 @@ static void *command_worker(void *arg)
 int main(int argc, char **argv)
 {
 	int r, opt, i;
-	char* addr = "127.0.0.1";
-	int port = 1234;
+	char* port = "1234";
 	uint32_t frequency = 100000000, samp_rate = 2048000;
-	struct sockaddr_in local, remote;
+	struct addrinfo hints, *res;
+	struct sockaddr_in remote;
 	uint32_t buf_num = 0;
 	int dev_index = 0;
 	int dev_given = 0;
@@ -391,7 +392,7 @@ int main(int argc, char **argv)
 	struct sigaction sigact, sigign;
 #endif
 
-	while ((opt = getopt(argc, argv, "a:p:f:g:s:b:n:d:P:")) != -1) {
+	while ((opt = getopt(argc, argv, "p:f:g:s:b:n:d:P:")) != -1) {
 		switch (opt) {
 		case 'd':
 			dev_index = verbose_device_search(optarg);
@@ -406,11 +407,8 @@ int main(int argc, char **argv)
 		case 's':
 			samp_rate = (uint32_t)atofs(optarg);
 			break;
-		case 'a':
-			addr = optarg;
-			break;
 		case 'p':
-			port = atoi(optarg);
+			port = optarg;
 			break;
 		case 'b':
 			buf_num = atoi(optarg);
@@ -502,16 +500,20 @@ int main(int argc, char **argv)
 	pthread_cond_init(&cond, NULL);
 	pthread_cond_init(&exit_cond, NULL);
 
-	memset(&local,0,sizeof(local));
-	local.sin_family = AF_INET;
-	local.sin_port = htons(port);
-	local.sin_addr.s_addr = inet_addr(addr);
+	memset(&hints, 0, sizeof(hints));
+	hints.ai_family = AF_INET;
+	hints.ai_socktype = SOCK_STREAM;
+	hints.ai_flags = AI_PASSIVE;    /* fill in my IP for me */
+	hints.ai_protocol = IPPROTO_TCP;
+	getaddrinfo(NULL, port, &hints, &res);
+
+	listensocket = socket(res->ai_family,
+			      res->ai_socktype, res->ai_protocol);
 
-	listensocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
 	r = 1;
 	setsockopt(listensocket, SOL_SOCKET, SO_REUSEADDR, (char *)&r, sizeof(int));
 	setsockopt(listensocket, SOL_SOCKET, SO_LINGER, (char *)&ling, sizeof(ling));
-	bind(listensocket,(struct sockaddr *)&local,sizeof(local));
+	bind(listensocket, res->ai_addr, res->ai_addrlen);
 
 #ifdef _WIN32
 	ioctlsocket(listensocket, FIONBIO, &blockmode);
@@ -522,11 +524,11 @@ int main(int argc, char **argv)
 
 	while(1) {
 		printf("listening...\n");
-		printf("Use the device argument 'rtl_tcp=%s:%d' in OsmoSDR "
+		printf("Use the device argument 'rtl_tcp=%s:%s' in OsmoSDR "
 		       "(gr-osmosdr) source\n"
 		       "to receive samples in GRC and control "
 		       "rtl_tcp parameters (frequency, gain, ...).\n",
-		       addr, port);
+		       "yourip", port);
 		listen(listensocket,1);
 
 		while(1) {
-- 
1.9.1



More information about the osmocom-sdr mailing list