comparison src/network.c @ 13130:66268ae5b94a

[gaim-migrate @ 15492] Attempt to avoid picking 127.0.0.1 as our public IP address if there are other options available. However, I still think this just picks the first entry in /etc/hosts. Is there a function call that returns all IPs listed in /etc/hosts? I couldn't find one, and I really don't want to have to parse that file myself. committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Mon, 06 Feb 2006 05:14:33 +0000
parents d0ae6489a0fb
children fae5a5517f18
comparison
equal deleted inserted replaced
13129:d0ae6489a0fb 13130:66268ae5b94a
104 const char * 104 const char *
105 gaim_network_get_local_system_ip(int fd) 105 gaim_network_get_local_system_ip(int fd)
106 { 106 {
107 struct hostent *host; 107 struct hostent *host;
108 char localhost[129]; 108 char localhost[129];
109 long unsigned add; 109 int i;
110 static char ip[46]; 110 static char ip[46];
111 const char *tmp = NULL; 111 const char *tmp = NULL;
112 112
113 if (fd >= 0) 113 if (fd >= 0)
114 tmp = gaim_network_get_local_ip_from_fd(fd); 114 tmp = gaim_network_get_local_ip_from_fd(fd);
115 115
116 if (tmp) 116 if (tmp)
117 return tmp; 117 return tmp;
118 118
119 /* TODO: Make this avoid using localhost/127.0.0.1 */
120 if (gethostname(localhost, 128) < 0) 119 if (gethostname(localhost, 128) < 0)
121 return NULL; 120 return NULL;
122 121
123 if ((host = gethostbyname(localhost)) == NULL) 122 if ((host = gethostbyname(localhost)) == NULL)
124 return NULL; 123 return NULL;
125 124
126 memcpy(&add, host->h_addr_list[0], 4); 125 /* Avoid using 127.0.0.1 */
127 add = htonl(add); 126 for (i = 0; (host->h_addr_list[i] != NULL); i++)
128 127 {
129 g_snprintf(ip, 16, "%lu.%lu.%lu.%lu", 128 if ((host->h_addr_list[i][0] != 127) ||
130 ((add >> 24) & 255), 129 (host->h_addr_list[i][1] != 0) ||
131 ((add >> 16) & 255), 130 (host->h_addr_list[i][2] != 0) ||
132 ((add >> 8) & 255), 131 (host->h_addr_list[i][3] != 1))
133 add & 255); 132 {
134 133 g_snprintf(ip, 16, "%hhu.%hhu.%hhu.%hhu",
135 return ip; 134 host->h_addr_list[i][0], host->h_addr_list[i][1],
135 host->h_addr_list[i][2], host->h_addr_list[i][3]);
136 return ip;
137 }
138 }
139
140 return "127.0.0.1";
136 } 141 }
137 142
138 const char * 143 const char *
139 gaim_network_get_my_ip(int fd) 144 gaim_network_get_my_ip(int fd)
140 { 145 {