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