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