Mercurial > pidgin.yaz
annotate src/network.c @ 11206:86d3ce670f05
[gaim-migrate @ 13334]
Warn when you close a conversation window with unread messages
committer: Tailor Script <tailor@pidgin.im>
author | Daniel Atallah <daniel.atallah@gmail.com> |
---|---|
date | Mon, 08 Aug 2005 03:44:32 +0000 |
parents | 3aeb85cc9cda |
children | ff728e84d59a |
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 { |
8834 | 137 const char *ip = NULL; |
11195 | 138 const 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); | |
150 if (ip != NULL) | |
151 return ip; | |
152 } | |
153 | |
8834 | 154 /* Just fetch the IP of the local system */ |
155 return gaim_network_get_local_system_ip(fd); | |
8231 | 156 } |
157 | |
8834 | 158 static int |
159 gaim_network_do_listen(unsigned short port) | |
8231 | 160 { |
9452 | 161 int listenfd = -1; |
8231 | 162 const int on = 1; |
11195 | 163 const char *controlURL = NULL; |
9449 | 164 #if HAVE_GETADDRINFO |
165 int errnum; | |
166 struct addrinfo hints, *res, *next; | |
9456 | 167 char serv[6]; |
8231 | 168 |
9449 | 169 /* |
170 * Get a list of addresses on this machine. | |
171 */ | |
172 snprintf(serv, sizeof(serv), "%hu", port); | |
8231 | 173 memset(&hints, 0, sizeof(struct addrinfo)); |
174 hints.ai_flags = AI_PASSIVE; | |
175 hints.ai_family = AF_UNSPEC; | |
176 hints.ai_socktype = SOCK_STREAM; | |
9449 | 177 errnum = getaddrinfo(NULL /* any IP */, serv, &hints, &res); |
178 if (errnum != 0) { | |
179 gaim_debug_warning("network", "getaddrinfo: %s\n", gai_strerror(errnum)); | |
180 if (errnum == EAI_SYSTEM) | |
181 gaim_debug_warning("network", "getaddrinfo: system error: %s\n", strerror(errno)); | |
8231 | 182 return -1; |
183 } | |
9449 | 184 |
185 /* | |
186 * Go through the list of addresses and attempt to listen on | |
187 * one of them. | |
188 * XXX - Try IPv6 addresses first? | |
189 */ | |
190 for (next = res; next != NULL; next = next->ai_next) { | |
9455 | 191 listenfd = socket(next->ai_family, next->ai_socktype, next->ai_protocol); |
8231 | 192 if (listenfd < 0) |
193 continue; | |
9449 | 194 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0) |
195 gaim_debug_warning("network", "setsockopt: %s\n", strerror(errno)); | |
9455 | 196 if (bind(listenfd, next->ai_addr, next->ai_addrlen) == 0) |
8231 | 197 break; /* success */ |
198 close(listenfd); | |
9449 | 199 } |
8231 | 200 |
9449 | 201 freeaddrinfo(res); |
8231 | 202 |
9449 | 203 if (next == NULL) |
204 return -1; | |
8231 | 205 #else |
206 struct sockaddr_in sockin; | |
207 | |
208 if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { | |
209 gaim_debug_warning("network", "socket: %s\n", strerror(errno)); | |
210 return -1; | |
211 } | |
212 | |
9449 | 213 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0) |
8231 | 214 gaim_debug_warning("network", "setsockopt: %s\n", strerror(errno)); |
215 | |
216 memset(&sockin, 0, sizeof(struct sockaddr_in)); | |
9449 | 217 sockin.sin_family = PF_INET; |
8251 | 218 sockin.sin_port = htons(port); |
8231 | 219 |
220 if (bind(listenfd, (struct sockaddr *)&sockin, sizeof(struct sockaddr_in)) != 0) { | |
221 gaim_debug_warning("network", "bind: %s\n", strerror(errno)); | |
222 close(listenfd); | |
223 return -1; | |
224 } | |
225 #endif | |
226 | |
227 if (listen(listenfd, 4) != 0) { | |
228 gaim_debug_warning("network", "listen: %s\n", strerror(errno)); | |
229 close(listenfd); | |
230 return -1; | |
231 } | |
232 fcntl(listenfd, F_SETFL, O_NONBLOCK); | |
233 | |
11195 | 234 if((controlURL = gaim_upnp_discover()) != NULL) { |
235 if(!gaim_upnp_set_port_mapping(controlURL, port, "TCP")) { | |
236 gaim_upnp_remove_port_mapping(controlURL, port, "TCP"); | |
237 gaim_upnp_set_port_mapping(controlURL, port, "TCP"); | |
238 } | |
239 } | |
240 | |
8231 | 241 gaim_debug_info("network", "Listening on port: %hu\n", gaim_network_get_port_from_fd(listenfd)); |
242 return listenfd; | |
243 } | |
244 | |
8834 | 245 int |
246 gaim_network_listen(unsigned short port) | |
8246 | 247 { |
8250 | 248 g_return_val_if_fail(port != 0, -1); |
249 | |
8246 | 250 return gaim_network_do_listen(port); |
251 } | |
252 | |
8834 | 253 int |
254 gaim_network_listen_range(unsigned short start, unsigned short end) | |
8231 | 255 { |
8240 | 256 int ret = -1; |
8231 | 257 |
8250 | 258 if (gaim_prefs_get_bool("/core/network/ports_range_use")) { |
8239 | 259 start = gaim_prefs_get_int("/core/network/ports_range_start"); |
260 end = gaim_prefs_get_int("/core/network/ports_range_end"); | |
8250 | 261 } else { |
262 if (end < start) | |
263 end = start; | |
8239 | 264 } |
8231 | 265 |
266 for (; start <= end; start++) { | |
267 ret = gaim_network_do_listen(start); | |
268 if (ret >= 0) | |
269 break; | |
270 } | |
271 | |
272 return ret; | |
273 } | |
274 | |
8834 | 275 unsigned short |
276 gaim_network_get_port_from_fd(int fd) | |
8231 | 277 { |
278 struct sockaddr_in addr; | |
279 socklen_t len; | |
280 | |
9449 | 281 g_return_val_if_fail(fd >= 0, 0); |
8231 | 282 |
283 len = sizeof(addr); | |
284 if (getsockname(fd, (struct sockaddr *) &addr, &len) == -1) { | |
285 gaim_debug_warning("network", "getsockname: %s\n", strerror(errno)); | |
286 return 0; | |
287 } | |
288 | |
289 return ntohs(addr.sin_port); | |
290 } | |
291 | |
292 void | |
293 gaim_network_init(void) | |
294 { | |
295 gaim_prefs_add_none ("/core/network"); | |
296 gaim_prefs_add_bool ("/core/network/auto_ip", TRUE); | |
297 gaim_prefs_add_string("/core/network/public_ip", ""); | |
298 gaim_prefs_add_bool ("/core/network/ports_range_use", FALSE); | |
299 gaim_prefs_add_int ("/core/network/ports_range_start", 1024); | |
300 gaim_prefs_add_int ("/core/network/ports_range_end", 2048); | |
301 } |