[Live-devel] Get own IP on MIPS

Andreas Frisch fox at purplegecko.de
Mon Dec 11 12:00:42 PST 2006


The code to find out the own server IP address in GroupsockHelper.cpp
netAddressBits ourSourceAddressForMulticast(UsageEnvironment& env)
always resulted in an "This computer has an invalid IP address: 0x00" 
error on my MIPS architecture embedded system.
i attached a diff of a workaround which discovers a valid IP number on 
this system.
may i remark that i am a linux noob bungler-type student person and this 
is my first time posting to a mailing list, so feel free to tell me 
whether there's anything wrong with what i did here.

with best regards,
andreas
-------------- next part --------------
--- /home/andy/GroupsockHelper.cpp	2006-12-08 10:08:15.000000000 +0100
+++ GroupsockHelper.cpp	2006-12-09 20:19:11.000000000 +0100
@@ -31,6 +31,10 @@
 #endif
 #include <stdio.h>
 
+// FRISCH 2006-12-09
+#include <sys/ioctl.h>
+#include <net/if.h>
+
 // By default, use INADDR_ANY for the sending and receiving interfaces:
 netAddressBits SendingInterfaceAddr = INADDR_ANY;
 netAddressBits ReceivingInterfaceAddr = INADDR_ANY;
@@ -650,6 +654,37 @@
 	      (netAddressBits)(ntohl(from)));
       env.setResultMsg(tmp);
       from = 0;
+
+// FRISCH 2006-12-09
+		// if we get stuck here then the original technique to find out our source IP address didn't work.
+		// for some reason it fails on our DM7025 Linux, therefore we try to discover it differently...
+
+		int sockfd = socket(PF_INET,SOCK_DGRAM,0);
+		if (sockfd == -1) {
+			sprintf(tmp,"frisch: failed to get network socket");
+			env.setResultMsg(tmp);
+			return 0;
+		}
+
+		char *name = "eth0";
+
+		struct sockaddr_in *sin;
+		struct ifreq ifr;
+
+		strncpy(ifr.ifr_name,name,IFNAMSIZ);	// Copies the interface name into the buffer
+		if (ioctl(sockfd,SIOCGIFADDR,&ifr) == -1) {
+			sprintf(tmp,"frisch: failed to icotl IP address");
+			env.setResultMsg(tmp);
+			return 0;
+		}
+		close(sockfd);
+
+		sin = (struct sockaddr_in *) ((struct sockaddr *) &(ifr.ifr_addr));
+
+		fprintf(stderr, "discovered own IP address: %s\n",inet_ntoa(sin->sin_addr));
+
+		from = sin->sin_addr.s_addr;
+// /FRISCH
     }
     
     ourAddress = from;


More information about the live-devel mailing list