Mercurial > pidgin.yaz
annotate src/network.c @ 11301:a09a5d64f33f
[gaim-migrate @ 13501]
This apparently fixes setup-gettext for gettext versions in x.y format
rather than x.y.z format. see sf patch 1218157
committer: Tailor Script <tailor@pidgin.im>
author | Luke Schierer <lschiere@pidgin.im> |
---|---|
date | Thu, 18 Aug 2005 18:11:12 +0000 |
parents | bb0d7b719af2 |
children | 7d7dd22215ec |
rev | line source |
---|---|
8231 | 1 /** |
2 * @file network.c Network Implementation | |
3 * @ingroup core | |
4 * | |
5 * gaim | |
6 * | |
7 * Gaim is the legal property of its developers, whose names are too numerous | |
8 * to list here. Please refer to the COPYRIGHT file distributed with this | |
9 * source distribution. | |
10 * | |
11 * This program is free software; you can redistribute it and/or modify | |
12 * it under the terms of the GNU General Public License as published by | |
13 * the Free Software Foundation; either version 2 of the License, or | |
14 * (at your option) any later version. | |
15 * | |
16 * This program is distributed in the hope that it will be useful, | |
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 * GNU General Public License for more details. | |
20 * | |
21 * You should have received a copy of the GNU General Public License | |
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 | |
24 */ | |
25 | |
8245
91c6629b1ee8
[gaim-migrate @ 8968]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
8240
diff
changeset
|
26 #include "internal.h" |
8231 | 27 |
28 #include "debug.h" | |
29 #include "account.h" | |
30 #include "network.h" | |
31 #include "prefs.h" | |
11195 | 32 #include "upnp.h" |
8231 | 33 |
8838 | 34 const unsigned char * |
35 gaim_network_ip_atoi(const char *ip) | |
36 { | |
37 static unsigned char ret[4]; | |
8981 | 38 gchar *delimiter = "."; |
8838 | 39 gchar **split; |
40 int i; | |
41 | |
42 g_return_val_if_fail(ip != NULL, NULL); | |
43 | |
8981 | 44 split = g_strsplit(ip, delimiter, 4); |
8838 | 45 for (i = 0; split[i] != NULL; i++) |
46 ret[i] = atoi(split[i]); | |
47 g_strfreev(split); | |
48 | |
49 /* i should always be 4 */ | |
50 if (i != 4) | |
51 return NULL; | |
52 | |
53 return ret; | |
54 } | |
55 | |
8231 | 56 void |
8834 | 57 gaim_network_set_public_ip(const char *ip) |
8231 | 58 { |
59 g_return_if_fail(ip != NULL); | |
60 | |
8838 | 61 /* XXX - Ensure the IP address is valid */ |
62 | |
8231 | 63 gaim_prefs_set_string("/core/network/public_ip", ip); |
64 } | |
65 | |
66 const char * | |
8834 | 67 gaim_network_get_public_ip(void) |
8231 | 68 { |
69 const char *ip; | |
70 | |
71 ip = gaim_prefs_get_string("/core/network/public_ip"); | |
72 | |
73 if (ip == NULL || *ip == '\0') | |
74 return NULL; | |
75 | |
76 return ip; | |
77 } | |
78 | |
79 static const char * | |
80 gaim_network_get_local_ip_from_fd(int fd) | |
81 { | |
82 struct sockaddr_in addr; | |
83 socklen_t len; | |
84 static char ip[16]; | |
85 const char *tmp; | |
86 | |
8840 | 87 g_return_val_if_fail(fd >= 0, NULL); |
8231 | 88 |
89 len = sizeof(addr); | |
90 if (getsockname(fd, (struct sockaddr *) &addr, &len) == -1) { | |
91 gaim_debug_warning("network", "getsockname: %s\n", strerror(errno)); | |
92 return NULL; | |
93 } | |
94 | |
95 tmp = inet_ntoa(addr.sin_addr); | |
96 strncpy(ip, tmp, sizeof(ip)); | |
8838 | 97 |
8231 | 98 return ip; |
99 } | |
100 | |
101 const char * | |
102 gaim_network_get_local_system_ip(int fd) | |
103 { | |
104 struct hostent *host; | |
105 char localhost[129]; | |
106 long unsigned add; | |
107 static char ip[46]; | |
108 const char *tmp = NULL; | |
109 | |
8840 | 110 if (fd >= 0) |
8231 | 111 tmp = gaim_network_get_local_ip_from_fd(fd); |
112 | |
113 if (tmp) | |
114 return tmp; | |
115 | |
116 if (gethostname(localhost, 128) < 0) | |
117 return NULL; | |
118 | |
119 if ((host = gethostbyname(localhost)) == NULL) | |
120 return NULL; | |
121 | |
122 memcpy(&add, host->h_addr_list[0], 4); | |
123 add = htonl(add); | |
124 | |
125 g_snprintf(ip, 16, "%lu.%lu.%lu.%lu", | |
126 ((add >> 24) & 255), | |
127 ((add >> 16) & 255), | |
128 ((add >> 8) & 255), | |
129 add & 255); | |
130 | |
131 return ip; | |
132 } | |
133 | |
134 const char * | |
8838 | 135 gaim_network_get_my_ip(int fd) |
8231 | 136 { |
11215
986160f7b6ca
[gaim-migrate @ 13347]
Richard Laager <rlaager@wiktel.com>
parents:
11213
diff
changeset
|
137 const char *ip = NULL; |
11213 | 138 char *controlURL = NULL; |
8834 | 139 |
140 /* Check if the user specified an IP manually */ | |
141 if (!gaim_prefs_get_bool("/core/network/auto_ip")) { | |
142 ip = gaim_network_get_public_ip(); | |
143 if (ip != NULL) | |
144 return ip; | |
145 } | |
146 | |
11195 | 147 /* attempt to get the ip from a NAT device */ |
148 if ((controlURL = gaim_upnp_discover()) != NULL) { | |
149 ip = gaim_upnp_get_public_ip(controlURL); | |
11213 | 150 free(controlURL); |
11195 | 151 if (ip != NULL) |
152 return ip; | |
153 } | |
154 | |
8834 | 155 /* Just fetch the IP of the local system */ |
156 return gaim_network_get_local_system_ip(fd); | |
8231 | 157 } |
158 | |
8834 | 159 static int |
160 gaim_network_do_listen(unsigned short port) | |
8231 | 161 { |
9452 | 162 int listenfd = -1; |
8231 | 163 const int on = 1; |
11213 | 164 char *controlURL = NULL; |
9449 | 165 #if HAVE_GETADDRINFO |
166 int errnum; | |
167 struct addrinfo hints, *res, *next; | |
9456 | 168 char serv[6]; |
8231 | 169 |
9449 | 170 /* |
171 * Get a list of addresses on this machine. | |
172 */ | |
173 snprintf(serv, sizeof(serv), "%hu", port); | |
8231 | 174 memset(&hints, 0, sizeof(struct addrinfo)); |
175 hints.ai_flags = AI_PASSIVE; | |
176 hints.ai_family = AF_UNSPEC; | |
177 hints.ai_socktype = SOCK_STREAM; | |
9449 | 178 errnum = getaddrinfo(NULL /* any IP */, serv, &hints, &res); |
179 if (errnum != 0) { | |
11221
5ed33bb06a84
[gaim-migrate @ 13353]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11215
diff
changeset
|
180 #ifndef _WIN32 |
9449 | 181 gaim_debug_warning("network", "getaddrinfo: %s\n", gai_strerror(errnum)); |
182 if (errnum == EAI_SYSTEM) | |
183 gaim_debug_warning("network", "getaddrinfo: system error: %s\n", strerror(errno)); | |
11221
5ed33bb06a84
[gaim-migrate @ 13353]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11215
diff
changeset
|
184 #else |
5ed33bb06a84
[gaim-migrate @ 13353]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11215
diff
changeset
|
185 gaim_debug_warning("network", "getaddrinfo: Error Code = %d\n", errnum); |
5ed33bb06a84
[gaim-migrate @ 13353]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11215
diff
changeset
|
186 #endif |
8231 | 187 return -1; |
188 } | |
9449 | 189 |
190 /* | |
191 * Go through the list of addresses and attempt to listen on | |
192 * one of them. | |
193 * XXX - Try IPv6 addresses first? | |
194 */ | |
195 for (next = res; next != NULL; next = next->ai_next) { | |
9455 | 196 listenfd = socket(next->ai_family, next->ai_socktype, next->ai_protocol); |
8231 | 197 if (listenfd < 0) |
198 continue; | |
9449 | 199 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0) |
200 gaim_debug_warning("network", "setsockopt: %s\n", strerror(errno)); | |
9455 | 201 if (bind(listenfd, next->ai_addr, next->ai_addrlen) == 0) |
8231 | 202 break; /* success */ |
203 close(listenfd); | |
9449 | 204 } |
8231 | 205 |
9449 | 206 freeaddrinfo(res); |
8231 | 207 |
9449 | 208 if (next == NULL) |
209 return -1; | |
8231 | 210 #else |
211 struct sockaddr_in sockin; | |
212 | |
213 if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { | |
214 gaim_debug_warning("network", "socket: %s\n", strerror(errno)); | |
215 return -1; | |
216 } | |
217 | |
9449 | 218 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0) |
8231 | 219 gaim_debug_warning("network", "setsockopt: %s\n", strerror(errno)); |
220 | |
221 memset(&sockin, 0, sizeof(struct sockaddr_in)); | |
9449 | 222 sockin.sin_family = PF_INET; |
8251 | 223 sockin.sin_port = htons(port); |
8231 | 224 |
225 if (bind(listenfd, (struct sockaddr *)&sockin, sizeof(struct sockaddr_in)) != 0) { | |
226 gaim_debug_warning("network", "bind: %s\n", strerror(errno)); | |
227 close(listenfd); | |
228 return -1; | |
229 } | |
230 #endif | |
231 | |
232 if (listen(listenfd, 4) != 0) { | |
233 gaim_debug_warning("network", "listen: %s\n", strerror(errno)); | |
234 close(listenfd); | |
235 return -1; | |
236 } | |
237 fcntl(listenfd, F_SETFL, O_NONBLOCK); | |
238 | |
11195 | 239 if((controlURL = gaim_upnp_discover()) != NULL) { |
11228 | 240 if(!gaim_upnp_set_port_mapping(controlURL, gaim_network_get_port_from_fd(listenfd), "TCP")) { |
241 gaim_upnp_remove_port_mapping(controlURL, gaim_network_get_port_from_fd(listenfd), "TCP"); | |
242 gaim_upnp_set_port_mapping(controlURL, gaim_network_get_port_from_fd(listenfd), "TCP"); | |
11195 | 243 } |
11213 | 244 free(controlURL); |
11195 | 245 } |
246 | |
8231 | 247 gaim_debug_info("network", "Listening on port: %hu\n", gaim_network_get_port_from_fd(listenfd)); |
248 return listenfd; | |
249 } | |
250 | |
8834 | 251 int |
252 gaim_network_listen(unsigned short port) | |
8246 | 253 { |
8250 | 254 g_return_val_if_fail(port != 0, -1); |
255 | |
8246 | 256 return gaim_network_do_listen(port); |
257 } | |
258 | |
8834 | 259 int |
260 gaim_network_listen_range(unsigned short start, unsigned short end) | |
8231 | 261 { |
8240 | 262 int ret = -1; |
8231 | 263 |
8250 | 264 if (gaim_prefs_get_bool("/core/network/ports_range_use")) { |
8239 | 265 start = gaim_prefs_get_int("/core/network/ports_range_start"); |
266 end = gaim_prefs_get_int("/core/network/ports_range_end"); | |
8250 | 267 } else { |
268 if (end < start) | |
269 end = start; | |
8239 | 270 } |
8231 | 271 |
272 for (; start <= end; start++) { | |
273 ret = gaim_network_do_listen(start); | |
274 if (ret >= 0) | |
275 break; | |
276 } | |
277 | |
278 return ret; | |
279 } | |
280 | |
8834 | 281 unsigned short |
282 gaim_network_get_port_from_fd(int fd) | |
8231 | 283 { |
284 struct sockaddr_in addr; | |
285 socklen_t len; | |
286 | |
9449 | 287 g_return_val_if_fail(fd >= 0, 0); |
8231 | 288 |
289 len = sizeof(addr); | |
290 if (getsockname(fd, (struct sockaddr *) &addr, &len) == -1) { | |
291 gaim_debug_warning("network", "getsockname: %s\n", strerror(errno)); | |
292 return 0; | |
293 } | |
294 | |
295 return ntohs(addr.sin_port); | |
296 } | |
297 | |
298 void | |
299 gaim_network_init(void) | |
300 { | |
301 gaim_prefs_add_none ("/core/network"); | |
302 gaim_prefs_add_bool ("/core/network/auto_ip", TRUE); | |
303 gaim_prefs_add_string("/core/network/public_ip", ""); | |
304 gaim_prefs_add_bool ("/core/network/ports_range_use", FALSE); | |
305 gaim_prefs_add_int ("/core/network/ports_range_start", 1024); | |
306 gaim_prefs_add_int ("/core/network/ports_range_end", 2048); | |
307 } |