comparison src/network.c @ 13155:fae5a5517f18

[gaim-migrate @ 15518] That should fix gaim_network_get_local_system_ip returning 127.0.0.1. committer: Tailor Script <tailor@pidgin.im>
author Thomas Butter <tbutter>
date Tue, 07 Feb 2006 09:41:22 +0000
parents 66268ae5b94a
children be0cd152691d
comparison
equal deleted inserted replaced
13154:0d755026832c 13155:fae5a5517f18
22 * along with this program; if not, write to the Free Software 22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */ 24 */
25 25
26 #include "internal.h" 26 #include "internal.h"
27
28 #ifndef _WIN32
29 #include <net/if.h>
30 #include <sys/ioctl.h>
31 #endif
32
33 /* Solaris */
34 #if defined (__SVR4) && defined (__sun)
35 #include <sys/sockio.h>
36 #endif
27 37
28 #include "debug.h" 38 #include "debug.h"
29 #include "account.h" 39 #include "account.h"
30 #include "network.h" 40 #include "network.h"
31 #include "prefs.h" 41 #include "prefs.h"
102 } 112 }
103 113
104 const char * 114 const char *
105 gaim_network_get_local_system_ip(int fd) 115 gaim_network_get_local_system_ip(int fd)
106 { 116 {
107 struct hostent *host; 117 char buffer[1024];
108 char localhost[129]; 118 static char ip[16];
109 int i; 119 char *tmp;
110 static char ip[46]; 120 struct ifconf ifc;
111 const char *tmp = NULL; 121 struct ifreq *ifr;
112 122 struct sockaddr_in *sinptr;
113 if (fd >= 0) 123 guint32 lhost = htonl(127*256*256*256+1);
114 tmp = gaim_network_get_local_ip_from_fd(fd); 124 long unsigned int add;
115 125 int source = fd;
116 if (tmp) 126
117 return tmp; 127 if(source <= 0)
118 128 source = socket(PF_INET,SOCK_STREAM,0);
119 if (gethostname(localhost, 128) < 0) 129
120 return NULL; 130 ifc.ifc_len = sizeof(buffer);
121 131 ifc.ifc_req = (struct ifreq*) buffer;
122 if ((host = gethostbyname(localhost)) == NULL) 132 ioctl(source, SIOCGIFCONF, &ifc);
123 return NULL; 133
124 134 if(fd <= 0)
125 /* Avoid using 127.0.0.1 */ 135 close(source);
126 for (i = 0; (host->h_addr_list[i] != NULL); i++) 136
127 { 137 tmp = buffer;
128 if ((host->h_addr_list[i][0] != 127) || 138 while(tmp < buffer + ifc.ifc_len) {
129 (host->h_addr_list[i][1] != 0) || 139 ifr = (struct ifreq *) tmp;
130 (host->h_addr_list[i][2] != 0) || 140 tmp += sizeof(struct ifreq);
131 (host->h_addr_list[i][3] != 1)) 141
132 { 142 if(ifr->ifr_addr.sa_family == AF_INET) {
133 g_snprintf(ip, 16, "%hhu.%hhu.%hhu.%hhu", 143 sinptr = (struct sockaddr_in *) &ifr->ifr_addr;
134 host->h_addr_list[i][0], host->h_addr_list[i][1], 144 if(sinptr->sin_addr.s_addr != lhost) {
135 host->h_addr_list[i][2], host->h_addr_list[i][3]); 145 add = ntohl(sinptr->sin_addr.s_addr);
136 return ip; 146 g_snprintf(ip, 16, "%lu.%lu.%lu.%lu",
137 } 147 ((add >> 24) & 255),
138 } 148 ((add >> 16) & 255),
139 149 ((add >> 8) & 255),
140 return "127.0.0.1"; 150 add & 255);
151
152 return ip;
153 }
154 }
155 }
156 return "0.0.0.0";
141 } 157 }
142 158
143 const char * 159 const char *
144 gaim_network_get_my_ip(int fd) 160 gaim_network_get_my_ip(int fd)
145 { 161 {