freehoo-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Freehoo-devel] [PATCH: freehoo] wrong way of finding local ip


From: Anand Avati
Subject: [Freehoo-devel] [PATCH: freehoo] wrong way of finding local ip
Date: Wed, 26 Jan 2005 23:59:15 +0530
User-agent: Mutt/1.4.2.1i

hi people,
   first of all thank you for the great messenger :) i downloaded it today and 
fell for it flat! but that was after a while..
   i'm currently on a configuration where my gateway does not belong to the 
subnet where my local ip binds to.. it is a strange and uncommon 
configuration.. but that's how it is.. and i'v seen similar configuration in a 
few other places as well.. the current method of finding the client IP address 
was to get the gateway address, assume your IP had a /24 subnet (mask off the 
last IP octet), and iterate through every interface IP of the sytem looking for 
that IP which string matches with the (wrongly) masked gateway.
   first of all the netmask could be < /24 meaning, second of all, the gateway 
could be out of the subnet where your interface belongs to. 
   for eg.
   case 1.
      IP - 10.20.30.40
      NM - 255.255.0.0
      GW - 10.20.10.1

      in this scenario the code breaks (the code searches for "10.20.10." 
substring in the IP address)

   case 2.
      IP - 10.20.30.40
      NM - 255.255.0.0
      EXTRA route to same interface - 192.168.1.0/255.255.255.0
      GW - 192.168.1.1

      in this scenario (my current scenario) , the code breaks too

   your coding logic had every right not accept this config and refuse to 
work.. but instead it segfaulted ;( (and i believe NO code has the right to 
segfault) but since it segfaulted i was curious to know the reason and found 
that this was being the cause (heh, if it hadnt segfaulted, i would'v thought 
it was just another non-working yahoo client, or that yahoo updated their 
protocol again :p)

   ok the patch is attached (pasted) below.. the logic with the patch is to 
find the interface which has the default gateway, and to get the ip address of 
that interface. i'm sure this logic is not 100% fool proof either, and can 
cause problem with secondary IP's added through iproute2 which isnt reflected 
in ifconfig.. but i couldnt get a right way to do it either.. (dint want to 
change popen("ifconfig ..") to popen("ip ..") for cross-platform reasons, and 
there is no way ifconfig gets secondary ip's added via iproute2) ..

   i have tested the patch on linux (slack 10.0) .. not sure if it works on 
other platforms.. could have tested on my netbsd but its power smps has conked 
out at the moment. the likely issue could be the formatting of "netstat -rn".

   ok here's the code..

--snip--
diff -pruN freehoo-3.1.1/src/yahoo-backend.c 
freehoo-3.1.1-patch/src/yahoo-backend.c
--- freehoo-3.1.1/src/yahoo-backend.c   2005-01-26 03:25:16.000000000 +0530
+++ freehoo-3.1.1-patch/src/yahoo-backend.c     2005-01-26 23:25:36.000000000 
+0530
@@ -157,7 +157,8 @@ get_local_addresses ()
 {
   static char addresses[1024];
   char buff[1024];
-  char gateway[16];
+  char interface[16];
+  char popen_cmd[32];
   char *c = NULL;
   struct hostent *hn;
   int i;
@@ -167,11 +168,17 @@ get_local_addresses ()
     goto IP_TEST_2;
   while (fgets (buff, sizeof (buff), f) != NULL)
     {
-      c = strtok (buff, " ");
+      c = strtok (buff, " "); /* dest */
       if ((strstr (c, "default") || strstr (c, "0.0.0.0")) &&
          !strstr (c, "127.0.0"))
        {
-         c = strtok (NULL, " ");
+         c = strtok (NULL, " "); /* gateway */
+         c = strtok (NULL, " "); /* mask */
+         c = strtok (NULL, " "); /* flags */
+         c = strtok (NULL, " "); /* mss */
+         c = strtok (NULL, " "); /* window */
+         c = strtok (NULL, " "); /* irtt */
+         c = strtok (NULL, " "); /* iface */
          pclose (f);
          break;
        }
@@ -182,23 +189,19 @@ get_local_addresses ()
   if (!c || !c[0])
     goto IP_TEST_2;
   
-  strncpy (gateway, c, 16);
+  strncpy (interface, c, 16);
+  interface[16] = '\0'; /* just in case */
+  if(interface[strlen(interface)-1] == '\n')
+    interface[strlen(interface)-1] = '\0';
 
-  for (i = strlen (gateway); gateway[i] != '.'; i--)
-    gateway[i] = 0;
-
-  gateway[i] = 0;
-
-  for (i = strlen (gateway); gateway[i] != '.'; i--)
-    gateway[i] = 0;
-
-  f = popen ("/sbin/ifconfig -a", "r");
+  sprintf(popen_cmd,"/sbin/ifconfig %s",interface);
+  f = popen (popen_cmd, "r");
   if ((int) f < 1)
     goto IP_TEST_2;
 
   while (fgets (buff, sizeof (buff), f) != NULL)
     {
-      if (strstr (buff, "inet") && strstr (buff, gateway))
+      if (strstr (buff, "inet"))
        break;
     }
   pclose (f);
   
--snip--

   once again, thanks people for the wonderful piece of work :) i'm really 
loving this!! i was satisfying myself with centericq.. but this one kicks ass! 
just the one what i wanted!

regards,
avati

Attachment: freehoo-patch-localip
Description: Text document


reply via email to

[Prev in Thread] Current Thread [Next in Thread]