Mercurial > pidgin.yaz
annotate src/network.c @ 11357:f0bc5f121684
[gaim-migrate @ 13579]
Image field added to the request API.
committer: Tailor Script <tailor@pidgin.im>
author | Bartoz Oler <bartosz@pidgin.im> |
---|---|
date | Sun, 28 Aug 2005 22:16:17 +0000 |
parents | 7d7dd22215ec |
children | d3755a7ddd82 |
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" | |
11336 | 32 #include "stun.h" |
11195 | 33 #include "upnp.h" |
8231 | 34 |
8838 | 35 const unsigned char * |
36 gaim_network_ip_atoi(const char *ip) | |
37 { | |
38 static unsigned char ret[4]; | |
8981 | 39 gchar *delimiter = "."; |
8838 | 40 gchar **split; |
41 int i; | |
42 | |
43 g_return_val_if_fail(ip != NULL, NULL); | |
44 | |
8981 | 45 split = g_strsplit(ip, delimiter, 4); |
8838 | 46 for (i = 0; split[i] != NULL; i++) |
47 ret[i] = atoi(split[i]); | |
48 g_strfreev(split); | |
49 | |
50 /* i should always be 4 */ | |
51 if (i != 4) | |
52 return NULL; | |
53 | |
54 return ret; | |
55 } | |
56 | |
8231 | 57 void |
8834 | 58 gaim_network_set_public_ip(const char *ip) |
8231 | 59 { |
60 g_return_if_fail(ip != NULL); | |
61 | |
8838 | 62 /* XXX - Ensure the IP address is valid */ |
63 | |
8231 | 64 gaim_prefs_set_string("/core/network/public_ip", ip); |
65 } | |
66 | |
67 const char * | |
8834 | 68 gaim_network_get_public_ip(void) |
8231 | 69 { |
70 const char *ip; | |
11336 | 71 struct stun_nattype *stun; |
72 | |
8231 | 73 ip = gaim_prefs_get_string("/core/network/public_ip"); |
74 | |
11336 | 75 if (ip == NULL || *ip == '\0') { |
76 /* Check if STUN discovery was already done */ | |
77 stun = gaim_stun_discover(NULL); | |
78 if(stun && stun->status>1) | |
79 return stun->publicip; | |
8231 | 80 return NULL; |
11336 | 81 } |
8231 | 82 |
83 return ip; | |
84 } | |
85 | |
86 static const char * | |
87 gaim_network_get_local_ip_from_fd(int fd) | |
88 { | |
89 struct sockaddr_in addr; | |
90 socklen_t len; | |
91 static char ip[16]; | |
92 const char *tmp; | |
93 | |
8840 | 94 g_return_val_if_fail(fd >= 0, NULL); |
8231 | 95 |
96 len = sizeof(addr); | |
97 if (getsockname(fd, (struct sockaddr *) &addr, &len) == -1) { | |
98 gaim_debug_warning("network", "getsockname: %s\n", strerror(errno)); | |
99 return NULL; | |
100 } | |
101 | |
102 tmp = inet_ntoa(addr.sin_addr); | |
103 strncpy(ip, tmp, sizeof(ip)); | |
8838 | 104 |
8231 | 105 return ip; |
106 } | |
107 | |
108 const char * | |
109 gaim_network_get_local_system_ip(int fd) | |
110 { | |
111 struct hostent *host; | |
112 char localhost[129]; | |
113 long unsigned add; | |
114 static char ip[46]; | |
115 const char *tmp = NULL; | |
116 | |
8840 | 117 if (fd >= 0) |
8231 | 118 tmp = gaim_network_get_local_ip_from_fd(fd); |
119 | |
120 if (tmp) | |
121 return tmp; | |
122 | |
123 if (gethostname(localhost, 128) < 0) | |
124 return NULL; | |
125 | |
126 if ((host = gethostbyname(localhost)) == NULL) | |
127 return NULL; | |
128 | |
129 memcpy(&add, host->h_addr_list[0], 4); | |
130 add = htonl(add); | |
131 | |
132 g_snprintf(ip, 16, "%lu.%lu.%lu.%lu", | |
133 ((add >> 24) & 255), | |
134 ((add >> 16) & 255), | |
135 ((add >> 8) & 255), | |
136 add & 255); | |
137 | |
138 return ip; | |
139 } | |
140 | |
141 const char * | |
8838 | 142 gaim_network_get_my_ip(int fd) |
8231 | 143 { |
11215
986160f7b6ca
[gaim-migrate @ 13347]
Richard Laager <rlaager@wiktel.com>
parents:
11213
diff
changeset
|
144 const char *ip = NULL; |
11213 | 145 char *controlURL = NULL; |
8834 | 146 |
147 /* Check if the user specified an IP manually */ | |
148 if (!gaim_prefs_get_bool("/core/network/auto_ip")) { | |
149 ip = gaim_network_get_public_ip(); | |
150 if (ip != NULL) | |
151 return ip; | |
152 } | |
153 | |
11195 | 154 /* attempt to get the ip from a NAT device */ |
155 if ((controlURL = gaim_upnp_discover()) != NULL) { | |
156 ip = gaim_upnp_get_public_ip(controlURL); | |
11213 | 157 free(controlURL); |
11195 | 158 if (ip != NULL) |
159 return ip; | |
160 } | |
161 | |
8834 | 162 /* Just fetch the IP of the local system */ |
163 return gaim_network_get_local_system_ip(fd); | |
8231 | 164 } |
165 | |
8834 | 166 static int |
167 gaim_network_do_listen(unsigned short port) | |
8231 | 168 { |
9452 | 169 int listenfd = -1; |
8231 | 170 const int on = 1; |
11213 | 171 char *controlURL = NULL; |
9449 | 172 #if HAVE_GETADDRINFO |
173 int errnum; | |
174 struct addrinfo hints, *res, *next; | |
9456 | 175 char serv[6]; |
8231 | 176 |
9449 | 177 /* |
178 * Get a list of addresses on this machine. | |
179 */ | |
180 snprintf(serv, sizeof(serv), "%hu", port); | |
8231 | 181 memset(&hints, 0, sizeof(struct addrinfo)); |
182 hints.ai_flags = AI_PASSIVE; | |
183 hints.ai_family = AF_UNSPEC; | |
184 hints.ai_socktype = SOCK_STREAM; | |
9449 | 185 errnum = getaddrinfo(NULL /* any IP */, serv, &hints, &res); |
186 if (errnum != 0) { | |
11221
5ed33bb06a84
[gaim-migrate @ 13353]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11215
diff
changeset
|
187 #ifndef _WIN32 |
9449 | 188 gaim_debug_warning("network", "getaddrinfo: %s\n", gai_strerror(errnum)); |
189 if (errnum == EAI_SYSTEM) | |
190 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
|
191 #else |
5ed33bb06a84
[gaim-migrate @ 13353]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11215
diff
changeset
|
192 gaim_debug_warning("network", "getaddrinfo: Error Code = %d\n", errnum); |
5ed33bb06a84
[gaim-migrate @ 13353]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11215
diff
changeset
|
193 #endif |
8231 | 194 return -1; |
195 } | |
9449 | 196 |
197 /* | |
198 * Go through the list of addresses and attempt to listen on | |
199 * one of them. | |
200 * XXX - Try IPv6 addresses first? | |
201 */ | |
202 for (next = res; next != NULL; next = next->ai_next) { | |
9455 | 203 listenfd = socket(next->ai_family, next->ai_socktype, next->ai_protocol); |
8231 | 204 if (listenfd < 0) |
205 continue; | |
9449 | 206 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0) |
207 gaim_debug_warning("network", "setsockopt: %s\n", strerror(errno)); | |
9455 | 208 if (bind(listenfd, next->ai_addr, next->ai_addrlen) == 0) |
8231 | 209 break; /* success */ |
210 close(listenfd); | |
9449 | 211 } |
8231 | 212 |
9449 | 213 freeaddrinfo(res); |
8231 | 214 |
9449 | 215 if (next == NULL) |
216 return -1; | |
8231 | 217 #else |
218 struct sockaddr_in sockin; | |
219 | |
220 if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { | |
221 gaim_debug_warning("network", "socket: %s\n", strerror(errno)); | |
222 return -1; | |
223 } | |
224 | |
9449 | 225 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0) |
8231 | 226 gaim_debug_warning("network", "setsockopt: %s\n", strerror(errno)); |
227 | |
228 memset(&sockin, 0, sizeof(struct sockaddr_in)); | |
9449 | 229 sockin.sin_family = PF_INET; |
8251 | 230 sockin.sin_port = htons(port); |
8231 | 231 |
232 if (bind(listenfd, (struct sockaddr *)&sockin, sizeof(struct sockaddr_in)) != 0) { | |
233 gaim_debug_warning("network", "bind: %s\n", strerror(errno)); | |
234 close(listenfd); | |
235 return -1; | |
236 } | |
237 #endif | |
238 | |
239 if (listen(listenfd, 4) != 0) { | |
240 gaim_debug_warning("network", "listen: %s\n", strerror(errno)); | |
241 close(listenfd); | |
242 return -1; | |
243 } | |
244 fcntl(listenfd, F_SETFL, O_NONBLOCK); | |
245 | |
11195 | 246 if((controlURL = gaim_upnp_discover()) != NULL) { |
11228 | 247 if(!gaim_upnp_set_port_mapping(controlURL, gaim_network_get_port_from_fd(listenfd), "TCP")) { |
248 gaim_upnp_remove_port_mapping(controlURL, gaim_network_get_port_from_fd(listenfd), "TCP"); | |
249 gaim_upnp_set_port_mapping(controlURL, gaim_network_get_port_from_fd(listenfd), "TCP"); | |
11195 | 250 } |
11213 | 251 free(controlURL); |
11195 | 252 } |
253 | |
8231 | 254 gaim_debug_info("network", "Listening on port: %hu\n", gaim_network_get_port_from_fd(listenfd)); |
255 return listenfd; | |
256 } | |
257 | |
8834 | 258 int |
259 gaim_network_listen(unsigned short port) | |
8246 | 260 { |
8250 | 261 g_return_val_if_fail(port != 0, -1); |
262 | |
8246 | 263 return gaim_network_do_listen(port); |
264 } | |
265 | |
8834 | 266 int |
267 gaim_network_listen_range(unsigned short start, unsigned short end) | |
8231 | 268 { |
8240 | 269 int ret = -1; |
8231 | 270 |
8250 | 271 if (gaim_prefs_get_bool("/core/network/ports_range_use")) { |
8239 | 272 start = gaim_prefs_get_int("/core/network/ports_range_start"); |
273 end = gaim_prefs_get_int("/core/network/ports_range_end"); | |
8250 | 274 } else { |
275 if (end < start) | |
276 end = start; | |
8239 | 277 } |
8231 | 278 |
279 for (; start <= end; start++) { | |
280 ret = gaim_network_do_listen(start); | |
281 if (ret >= 0) | |
282 break; | |
283 } | |
284 | |
285 return ret; | |
286 } | |
287 | |
8834 | 288 unsigned short |
289 gaim_network_get_port_from_fd(int fd) | |
8231 | 290 { |
291 struct sockaddr_in addr; | |
292 socklen_t len; | |
293 | |
9449 | 294 g_return_val_if_fail(fd >= 0, 0); |
8231 | 295 |
296 len = sizeof(addr); | |
297 if (getsockname(fd, (struct sockaddr *) &addr, &len) == -1) { | |
298 gaim_debug_warning("network", "getsockname: %s\n", strerror(errno)); | |
299 return 0; | |
300 } | |
301 | |
302 return ntohs(addr.sin_port); | |
303 } | |
304 | |
305 void | |
306 gaim_network_init(void) | |
307 { | |
308 gaim_prefs_add_none ("/core/network"); | |
309 gaim_prefs_add_bool ("/core/network/auto_ip", TRUE); | |
310 gaim_prefs_add_string("/core/network/public_ip", ""); | |
311 gaim_prefs_add_bool ("/core/network/ports_range_use", FALSE); | |
312 gaim_prefs_add_int ("/core/network/ports_range_start", 1024); | |
313 gaim_prefs_add_int ("/core/network/ports_range_end", 2048); | |
314 } |