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