Mercurial > pidgin
annotate src/network.c @ 13137:1ed228c65e89
[gaim-migrate @ 15499]
Rest of SF Patch #1415135 from Sadrul
This is some code cleanup.
committer: Tailor Script <tailor@pidgin.im>
author | Richard Laager <rlaager@wiktel.com> |
---|---|
date | Mon, 06 Feb 2006 08:02:03 +0000 |
parents | 66268ae5b94a |
children | fae5a5517f18 |
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" | |
11411 | 32 #include "stun.h" |
11195 | 33 #include "upnp.h" |
8231 | 34 |
12909
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
35 typedef struct { |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
36 int listenfd; |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
37 int socket_type; |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
38 gboolean retry; |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
39 gboolean adding; |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
40 GaimNetworkListenCallback cb; |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
41 gpointer cb_data; |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
42 } ListenUPnPData; |
11391 | 43 |
8838 | 44 const unsigned char * |
45 gaim_network_ip_atoi(const char *ip) | |
46 { | |
47 static unsigned char ret[4]; | |
8981 | 48 gchar *delimiter = "."; |
8838 | 49 gchar **split; |
50 int i; | |
51 | |
52 g_return_val_if_fail(ip != NULL, NULL); | |
53 | |
8981 | 54 split = g_strsplit(ip, delimiter, 4); |
8838 | 55 for (i = 0; split[i] != NULL; i++) |
56 ret[i] = atoi(split[i]); | |
57 g_strfreev(split); | |
58 | |
59 /* i should always be 4 */ | |
60 if (i != 4) | |
61 return NULL; | |
62 | |
63 return ret; | |
64 } | |
65 | |
8231 | 66 void |
8834 | 67 gaim_network_set_public_ip(const char *ip) |
8231 | 68 { |
69 g_return_if_fail(ip != NULL); | |
70 | |
8838 | 71 /* XXX - Ensure the IP address is valid */ |
72 | |
8231 | 73 gaim_prefs_set_string("/core/network/public_ip", ip); |
74 } | |
75 | |
76 const char * | |
8834 | 77 gaim_network_get_public_ip(void) |
8231 | 78 { |
13129 | 79 return gaim_prefs_get_string("/core/network/public_ip"); |
8231 | 80 } |
81 | |
82 static const char * | |
83 gaim_network_get_local_ip_from_fd(int fd) | |
84 { | |
85 struct sockaddr_in addr; | |
86 socklen_t len; | |
87 static char ip[16]; | |
88 const char *tmp; | |
89 | |
8840 | 90 g_return_val_if_fail(fd >= 0, NULL); |
8231 | 91 |
92 len = sizeof(addr); | |
93 if (getsockname(fd, (struct sockaddr *) &addr, &len) == -1) { | |
94 gaim_debug_warning("network", "getsockname: %s\n", strerror(errno)); | |
95 return NULL; | |
96 } | |
97 | |
98 tmp = inet_ntoa(addr.sin_addr); | |
99 strncpy(ip, tmp, sizeof(ip)); | |
8838 | 100 |
8231 | 101 return ip; |
102 } | |
103 | |
104 const char * | |
105 gaim_network_get_local_system_ip(int fd) | |
106 { | |
107 struct hostent *host; | |
108 char localhost[129]; | |
13130 | 109 int i; |
8231 | 110 static char ip[46]; |
111 const char *tmp = NULL; | |
112 | |
8840 | 113 if (fd >= 0) |
8231 | 114 tmp = gaim_network_get_local_ip_from_fd(fd); |
115 | |
116 if (tmp) | |
117 return tmp; | |
118 | |
119 if (gethostname(localhost, 128) < 0) | |
120 return NULL; | |
121 | |
122 if ((host = gethostbyname(localhost)) == NULL) | |
123 return NULL; | |
124 | |
13130 | 125 /* Avoid using 127.0.0.1 */ |
126 for (i = 0; (host->h_addr_list[i] != NULL); i++) | |
127 { | |
128 if ((host->h_addr_list[i][0] != 127) || | |
129 (host->h_addr_list[i][1] != 0) || | |
130 (host->h_addr_list[i][2] != 0) || | |
131 (host->h_addr_list[i][3] != 1)) | |
132 { | |
133 g_snprintf(ip, 16, "%hhu.%hhu.%hhu.%hhu", | |
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 } | |
8231 | 139 |
13130 | 140 return "127.0.0.1"; |
8231 | 141 } |
142 | |
143 const char * | |
8838 | 144 gaim_network_get_my_ip(int fd) |
8231 | 145 { |
12686
5f65a0cca87c
[gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents:
11424
diff
changeset
|
146 const char *ip = NULL; |
5f65a0cca87c
[gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents:
11424
diff
changeset
|
147 GaimStunNatDiscovery *stun; |
8834 | 148 |
149 /* Check if the user specified an IP manually */ | |
150 if (!gaim_prefs_get_bool("/core/network/auto_ip")) { | |
151 ip = gaim_network_get_public_ip(); | |
13129 | 152 if ((ip != NULL) && (*ip != '\0')) |
8834 | 153 return ip; |
154 } | |
155 | |
13129 | 156 /* Check if STUN discovery was already done */ |
157 stun = gaim_stun_discover(NULL); | |
158 if ((stun != NULL) && (stun->status == GAIM_STUN_STATUS_DISCOVERED)) | |
159 return stun->publicip; | |
11424 | 160 |
13129 | 161 /* Attempt to get the IP from a NAT device using UPnP */ |
162 ip = gaim_upnp_get_public_ip(); | |
163 if (ip != NULL) | |
164 return ip; | |
11195 | 165 |
8834 | 166 /* Just fetch the IP of the local system */ |
167 return gaim_network_get_local_system_ip(fd); | |
8231 | 168 } |
169 | |
11391 | 170 |
12909
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
171 static void |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
172 gaim_network_set_upnp_port_mapping_cb(gboolean success, gpointer data) |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
173 { |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
174 ListenUPnPData *ldata = data; |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
175 |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
176 if (!success) { |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
177 gaim_debug_warning("network", "Couldn't create UPnP mapping for...\n"); |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
178 if (ldata->retry) { |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
179 ldata->retry = FALSE; |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
180 ldata->adding = FALSE; |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
181 gaim_upnp_remove_port_mapping( |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
182 gaim_network_get_port_from_fd(ldata->listenfd), |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
183 (ldata->socket_type == SOCK_STREAM) ? "TCP" : "UDP", |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
184 gaim_network_set_upnp_port_mapping_cb, ldata); |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
185 return; |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
186 } |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
187 } else if (!ldata->adding) { |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
188 /* We've tried successfully to remove the port mapping. |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
189 * Try to add it again */ |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
190 ldata->adding = TRUE; |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
191 gaim_upnp_set_port_mapping( |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
192 gaim_network_get_port_from_fd(ldata->listenfd), |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
193 (ldata->socket_type == SOCK_STREAM) ? "TCP" : "UDP", |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
194 gaim_network_set_upnp_port_mapping_cb, ldata); |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
195 return; |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
196 } |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
197 |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
198 if (ldata->cb) |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
199 ldata->cb(ldata->listenfd, ldata->cb_data); |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
200 |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
201 g_free(ldata); |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
202 } |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
203 |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
204 |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
205 static gboolean |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
206 gaim_network_do_listen(unsigned short port, int socket_type, GaimNetworkListenCallback cb, gpointer cb_data) |
8231 | 207 { |
9452 | 208 int listenfd = -1; |
8231 | 209 const int on = 1; |
12917
a85c8c11bfab
[gaim-migrate @ 15270]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12909
diff
changeset
|
210 ListenUPnPData *ld; |
9449 | 211 #if HAVE_GETADDRINFO |
212 int errnum; | |
213 struct addrinfo hints, *res, *next; | |
9456 | 214 char serv[6]; |
8231 | 215 |
9449 | 216 /* |
217 * Get a list of addresses on this machine. | |
218 */ | |
219 snprintf(serv, sizeof(serv), "%hu", port); | |
8231 | 220 memset(&hints, 0, sizeof(struct addrinfo)); |
221 hints.ai_flags = AI_PASSIVE; | |
222 hints.ai_family = AF_UNSPEC; | |
12730
d5b8f4dc1622
[gaim-migrate @ 15074]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12728
diff
changeset
|
223 hints.ai_socktype = socket_type; |
9449 | 224 errnum = getaddrinfo(NULL /* any IP */, serv, &hints, &res); |
225 if (errnum != 0) { | |
11221
5ed33bb06a84
[gaim-migrate @ 13353]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11215
diff
changeset
|
226 #ifndef _WIN32 |
9449 | 227 gaim_debug_warning("network", "getaddrinfo: %s\n", gai_strerror(errnum)); |
228 if (errnum == EAI_SYSTEM) | |
229 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
|
230 #else |
12909
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
231 gaim_debug_warning("network", "getaddrinfo: Error Code = %d\n", errnum); |
11221
5ed33bb06a84
[gaim-migrate @ 13353]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11215
diff
changeset
|
232 #endif |
12909
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
233 return FALSE; |
8231 | 234 } |
9449 | 235 |
236 /* | |
237 * Go through the list of addresses and attempt to listen on | |
238 * one of them. | |
239 * XXX - Try IPv6 addresses first? | |
240 */ | |
241 for (next = res; next != NULL; next = next->ai_next) { | |
9455 | 242 listenfd = socket(next->ai_family, next->ai_socktype, next->ai_protocol); |
8231 | 243 if (listenfd < 0) |
244 continue; | |
9449 | 245 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0) |
246 gaim_debug_warning("network", "setsockopt: %s\n", strerror(errno)); | |
9455 | 247 if (bind(listenfd, next->ai_addr, next->ai_addrlen) == 0) |
8231 | 248 break; /* success */ |
12730
d5b8f4dc1622
[gaim-migrate @ 15074]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12728
diff
changeset
|
249 /* XXX - It is unclear to me (datallah) whether we need to be |
d5b8f4dc1622
[gaim-migrate @ 15074]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12728
diff
changeset
|
250 using a new socket each time */ |
8231 | 251 close(listenfd); |
9449 | 252 } |
8231 | 253 |
9449 | 254 freeaddrinfo(res); |
8231 | 255 |
9449 | 256 if (next == NULL) |
12909
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
257 return FALSE; |
8231 | 258 #else |
259 struct sockaddr_in sockin; | |
260 | |
12730
d5b8f4dc1622
[gaim-migrate @ 15074]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12728
diff
changeset
|
261 if ((listenfd = socket(AF_INET, socket_type, 0)) < 0) { |
8231 | 262 gaim_debug_warning("network", "socket: %s\n", strerror(errno)); |
12909
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
263 return FALSE; |
8231 | 264 } |
265 | |
9449 | 266 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0) |
8231 | 267 gaim_debug_warning("network", "setsockopt: %s\n", strerror(errno)); |
268 | |
269 memset(&sockin, 0, sizeof(struct sockaddr_in)); | |
9449 | 270 sockin.sin_family = PF_INET; |
8251 | 271 sockin.sin_port = htons(port); |
8231 | 272 |
273 if (bind(listenfd, (struct sockaddr *)&sockin, sizeof(struct sockaddr_in)) != 0) { | |
274 gaim_debug_warning("network", "bind: %s\n", strerror(errno)); | |
275 close(listenfd); | |
12909
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
276 return FALSE; |
8231 | 277 } |
278 #endif | |
279 | |
12730
d5b8f4dc1622
[gaim-migrate @ 15074]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12728
diff
changeset
|
280 if (socket_type == SOCK_STREAM && listen(listenfd, 4) != 0) { |
8231 | 281 gaim_debug_warning("network", "listen: %s\n", strerror(errno)); |
282 close(listenfd); | |
12909
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
283 return FALSE; |
8231 | 284 } |
285 fcntl(listenfd, F_SETFL, O_NONBLOCK); | |
286 | |
12909
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
287 gaim_debug_info("network", "Listening on port: %hu\n", gaim_network_get_port_from_fd(listenfd)); |
11391 | 288 |
12909
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
289 ld = g_new0(ListenUPnPData, 1); |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
290 ld->listenfd = listenfd; |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
291 ld->adding = TRUE; |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
292 ld->retry = TRUE; |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
293 ld->cb = cb; |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
294 ld->cb_data = cb_data; |
11195 | 295 |
12909
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
296 gaim_upnp_set_port_mapping( |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
297 gaim_network_get_port_from_fd(listenfd), |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
298 (socket_type == SOCK_STREAM) ? "TCP" : "UDP", |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
299 gaim_network_set_upnp_port_mapping_cb, ld); |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
300 |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
301 return TRUE; |
8231 | 302 } |
303 | |
12909
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
304 gboolean |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
305 gaim_network_listen(unsigned short port, int socket_type, |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
306 GaimNetworkListenCallback cb, gpointer cb_data) |
8246 | 307 { |
8250 | 308 g_return_val_if_fail(port != 0, -1); |
309 | |
12909
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
310 return gaim_network_do_listen(port, socket_type, cb, cb_data); |
8246 | 311 } |
312 | |
12909
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
313 gboolean |
12730
d5b8f4dc1622
[gaim-migrate @ 15074]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12728
diff
changeset
|
314 gaim_network_listen_range(unsigned short start, unsigned short end, |
12909
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
315 int socket_type, GaimNetworkListenCallback cb, gpointer cb_data) |
8231 | 316 { |
12909
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
317 gboolean ret = FALSE; |
8231 | 318 |
8250 | 319 if (gaim_prefs_get_bool("/core/network/ports_range_use")) { |
8239 | 320 start = gaim_prefs_get_int("/core/network/ports_range_start"); |
321 end = gaim_prefs_get_int("/core/network/ports_range_end"); | |
8250 | 322 } else { |
323 if (end < start) | |
324 end = start; | |
8239 | 325 } |
8231 | 326 |
327 for (; start <= end; start++) { | |
12909
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
328 ret = gaim_network_do_listen(start, socket_type, cb, cb_data); |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
329 if (ret) |
8231 | 330 break; |
331 } | |
332 | |
333 return ret; | |
334 } | |
335 | |
8834 | 336 unsigned short |
337 gaim_network_get_port_from_fd(int fd) | |
8231 | 338 { |
339 struct sockaddr_in addr; | |
340 socklen_t len; | |
341 | |
9449 | 342 g_return_val_if_fail(fd >= 0, 0); |
8231 | 343 |
344 len = sizeof(addr); | |
345 if (getsockname(fd, (struct sockaddr *) &addr, &len) == -1) { | |
346 gaim_debug_warning("network", "getsockname: %s\n", strerror(errno)); | |
347 return 0; | |
348 } | |
349 | |
350 return ntohs(addr.sin_port); | |
351 } | |
352 | |
353 void | |
354 gaim_network_init(void) | |
355 { | |
356 gaim_prefs_add_none ("/core/network"); | |
357 gaim_prefs_add_bool ("/core/network/auto_ip", TRUE); | |
358 gaim_prefs_add_string("/core/network/public_ip", ""); | |
359 gaim_prefs_add_bool ("/core/network/ports_range_use", FALSE); | |
360 gaim_prefs_add_int ("/core/network/ports_range_start", 1024); | |
361 gaim_prefs_add_int ("/core/network/ports_range_end", 2048); | |
12909
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
362 |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12730
diff
changeset
|
363 gaim_upnp_discover(NULL, NULL); |
8231 | 364 } |