Mercurial > pidgin
annotate src/network.c @ 8834:beb7be215db3
[gaim-migrate @ 9598]
I removed account->ip because it isn't used anywhere and I think it's dumb.
Also added handling for a and aaaa records to rendezvous. Gaim peeps
show up in iChat rendezvous lists now. There are still problems.
committer: Tailor Script <tailor@pidgin.im>
| author | Mark Doliner <mark@kingant.net> |
|---|---|
| date | Wed, 28 Apr 2004 00:48:21 +0000 |
| parents | 86b8d8b4287e |
| children | 518455386538 |
| 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" | |
| 32 | |
| 33 void | |
| 8834 | 34 gaim_network_set_public_ip(const char *ip) |
| 8231 | 35 { |
| 36 g_return_if_fail(ip != NULL); | |
| 37 | |
| 38 gaim_prefs_set_string("/core/network/public_ip", ip); | |
| 39 } | |
| 40 | |
| 41 const char * | |
| 8834 | 42 gaim_network_get_public_ip(void) |
| 8231 | 43 { |
| 44 const char *ip; | |
| 45 | |
| 46 ip = gaim_prefs_get_string("/core/network/public_ip"); | |
| 47 | |
| 48 if (ip == NULL || *ip == '\0') | |
| 49 return NULL; | |
| 50 | |
| 51 return ip; | |
| 52 } | |
| 53 | |
| 54 static const char * | |
| 55 gaim_network_get_local_ip_from_fd(int fd) | |
| 56 { | |
| 57 struct sockaddr_in addr; | |
| 58 socklen_t len; | |
| 59 static char ip[16]; | |
| 60 const char *tmp; | |
| 61 | |
| 62 g_return_val_if_fail(fd > 0, NULL); | |
| 63 | |
| 64 len = sizeof(addr); | |
| 65 if (getsockname(fd, (struct sockaddr *) &addr, &len) == -1) { | |
| 66 gaim_debug_warning("network", "getsockname: %s\n", strerror(errno)); | |
| 67 return NULL; | |
| 68 } | |
| 69 | |
| 70 tmp = inet_ntoa(addr.sin_addr); | |
| 71 strncpy(ip, tmp, sizeof(ip)); | |
| 72 return ip; | |
| 73 } | |
| 74 | |
| 75 const char * | |
| 76 gaim_network_get_local_system_ip(int fd) | |
| 77 { | |
| 78 struct hostent *host; | |
| 79 char localhost[129]; | |
| 80 long unsigned add; | |
| 81 static char ip[46]; | |
| 82 const char *tmp = NULL; | |
| 83 | |
| 84 if (fd != -1) | |
| 85 tmp = gaim_network_get_local_ip_from_fd(fd); | |
| 86 | |
| 87 if (tmp) | |
| 88 return tmp; | |
| 89 | |
| 90 if (gethostname(localhost, 128) < 0) | |
| 91 return NULL; | |
| 92 | |
| 93 if ((host = gethostbyname(localhost)) == NULL) | |
| 94 return NULL; | |
| 95 | |
| 96 memcpy(&add, host->h_addr_list[0], 4); | |
| 97 add = htonl(add); | |
| 98 | |
| 99 g_snprintf(ip, 16, "%lu.%lu.%lu.%lu", | |
| 100 ((add >> 24) & 255), | |
| 101 ((add >> 16) & 255), | |
| 102 ((add >> 8) & 255), | |
| 103 add & 255); | |
| 104 | |
| 105 return ip; | |
| 106 } | |
| 107 | |
| 108 const char * | |
| 109 gaim_network_get_ip_for_account(const GaimAccount *account, int fd) | |
| 110 { | |
| 8834 | 111 const char *ip = NULL; |
| 112 | |
| 113 /* Check if the user specified an IP manually */ | |
| 114 if (!gaim_prefs_get_bool("/core/network/auto_ip")) { | |
| 115 ip = gaim_network_get_public_ip(); | |
| 116 if (ip != NULL) | |
| 117 return ip; | |
| 118 } | |
| 119 | |
| 120 /* Just fetch the IP of the local system */ | |
| 121 return gaim_network_get_local_system_ip(fd); | |
| 8231 | 122 } |
| 123 | |
| 8834 | 124 static int |
| 125 gaim_network_do_listen(unsigned short port) | |
| 8231 | 126 { |
| 127 #if HAVE_GETADDRINFO | |
| 128 int listenfd; | |
| 129 const int on = 1; | |
| 130 struct addrinfo hints, *res, *ressave; | |
| 131 char serv[5]; | |
| 132 | |
| 8251 | 133 snprintf(serv, sizeof(serv), "%d", port); |
| 8231 | 134 memset(&hints, 0, sizeof(struct addrinfo)); |
| 135 hints.ai_flags = AI_PASSIVE; | |
| 136 hints.ai_family = AF_UNSPEC; | |
| 137 hints.ai_socktype = SOCK_STREAM; | |
| 138 if (getaddrinfo(NULL /* any IP */, serv, &hints, &res) != 0) { | |
| 139 gaim_debug_warning("network", "getaddrinfo: %s\n", strerror(errno)); | |
| 140 return -1; | |
| 141 } | |
| 142 ressave = res; | |
| 143 do { | |
| 144 listenfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); | |
| 145 if (listenfd < 0) | |
| 146 continue; | |
| 147 setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); | |
| 148 if (bind(listenfd, res->ai_addr, res->ai_addrlen) == 0) | |
| 149 break; /* success */ | |
| 150 close(listenfd); | |
| 151 } while ( (res = res->ai_next) ); | |
| 152 | |
| 153 if (!res) | |
| 154 return -1; | |
| 155 | |
| 156 freeaddrinfo(ressave); | |
| 157 #else | |
| 158 int listenfd; | |
| 159 const int on = 1; | |
| 160 struct sockaddr_in sockin; | |
| 161 | |
| 162 if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { | |
| 163 gaim_debug_warning("network", "socket: %s\n", strerror(errno)); | |
| 164 return -1; | |
| 165 } | |
| 166 | |
| 167 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) != 0) { | |
| 168 gaim_debug_warning("network", "setsockopt: %s\n", strerror(errno)); | |
| 169 close(listenfd); | |
| 170 return -1; | |
| 171 } | |
| 172 | |
| 173 memset(&sockin, 0, sizeof(struct sockaddr_in)); | |
| 174 sockin.sin_family = AF_INET; | |
| 8251 | 175 sockin.sin_port = htons(port); |
| 8231 | 176 |
| 177 if (bind(listenfd, (struct sockaddr *)&sockin, sizeof(struct sockaddr_in)) != 0) { | |
| 178 gaim_debug_warning("network", "bind: %s\n", strerror(errno)); | |
| 179 close(listenfd); | |
| 180 return -1; | |
| 181 } | |
| 182 #endif | |
| 183 | |
| 184 if (listen(listenfd, 4) != 0) { | |
| 185 gaim_debug_warning("network", "listen: %s\n", strerror(errno)); | |
| 186 close(listenfd); | |
| 187 return -1; | |
| 188 } | |
| 189 fcntl(listenfd, F_SETFL, O_NONBLOCK); | |
| 190 | |
| 191 gaim_debug_info("network", "Listening on port: %hu\n", gaim_network_get_port_from_fd(listenfd)); | |
| 192 return listenfd; | |
| 193 } | |
| 194 | |
| 8834 | 195 int |
| 196 gaim_network_listen(unsigned short port) | |
| 8246 | 197 { |
| 8250 | 198 g_return_val_if_fail(port != 0, -1); |
| 199 | |
| 8246 | 200 return gaim_network_do_listen(port); |
| 201 } | |
| 202 | |
| 8834 | 203 int |
| 204 gaim_network_listen_range(unsigned short start, unsigned short end) | |
| 8231 | 205 { |
| 8240 | 206 int ret = -1; |
| 8231 | 207 |
| 8250 | 208 if (gaim_prefs_get_bool("/core/network/ports_range_use")) { |
| 8239 | 209 start = gaim_prefs_get_int("/core/network/ports_range_start"); |
| 210 end = gaim_prefs_get_int("/core/network/ports_range_end"); | |
| 8250 | 211 } else { |
| 212 if (end < start) | |
| 213 end = start; | |
| 8239 | 214 } |
| 8231 | 215 |
| 216 for (; start <= end; start++) { | |
| 217 ret = gaim_network_do_listen(start); | |
| 218 if (ret >= 0) | |
| 219 break; | |
| 220 } | |
| 221 | |
| 222 return ret; | |
| 223 } | |
| 224 | |
| 8834 | 225 unsigned short |
| 226 gaim_network_get_port_from_fd(int fd) | |
| 8231 | 227 { |
| 228 struct sockaddr_in addr; | |
| 229 socklen_t len; | |
| 230 | |
| 231 g_return_val_if_fail(fd > 0, 0); | |
| 232 | |
| 233 len = sizeof(addr); | |
| 234 if (getsockname(fd, (struct sockaddr *) &addr, &len) == -1) { | |
| 235 gaim_debug_warning("network", "getsockname: %s\n", strerror(errno)); | |
| 236 return 0; | |
| 237 } | |
| 238 | |
| 239 return ntohs(addr.sin_port); | |
| 240 } | |
| 241 | |
| 242 void | |
| 243 gaim_network_init(void) | |
| 244 { | |
| 245 gaim_prefs_add_none ("/core/network"); | |
| 246 gaim_prefs_add_bool ("/core/network/auto_ip", TRUE); | |
| 247 gaim_prefs_add_string("/core/network/public_ip", ""); | |
| 248 gaim_prefs_add_bool ("/core/network/ports_range_use", FALSE); | |
| 249 gaim_prefs_add_int ("/core/network/ports_range_start", 1024); | |
| 250 gaim_prefs_add_int ("/core/network/ports_range_end", 2048); | |
| 251 } |
