annotate src/network.c @ 8250:b248c1f4efbd

[gaim-migrate @ 8973] 1) Minor changes to the network listen code again. Tim, let me know if you have any other suggestions. 2) Changed how charsets are handled in oscar a tad bit. I think this should guarantee that Gaim doesn't crash when people send funky messages, or have funky away messages or really anything that is using a charset that isn't utf8, iso-8859-1, ucs-2be, or ascii. Ethan, this should fix the problem with that person's away message. Although, the message itself still looks kinda funky to me. The encoding is Windows-31J, which is apparently a valid iconv encoding? You would know more than I. 3) Fix the following crash: 1. IM yourself a message on AIM 2. Do NOT begin to type a second message, but instead hit CTRL+up committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Fri, 13 Feb 2004 05:37:12 +0000
parents fabcfd9a7c1c
children 86b8d8b4287e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
1 /**
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
2 * @file network.c Network Implementation
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
3 * @ingroup core
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
4 *
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
5 * gaim
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
6 *
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
7 * Gaim is the legal property of its developers, whose names are too numerous
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
8 * to list here. Please refer to the COPYRIGHT file distributed with this
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
9 * source distribution.
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
10 *
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
11 * This program is free software; you can redistribute it and/or modify
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
12 * it under the terms of the GNU General Public License as published by
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
13 * the Free Software Foundation; either version 2 of the License, or
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
14 * (at your option) any later version.
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
15 *
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
16 * This program is distributed in the hope that it will be useful,
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
19 * GNU General Public License for more details.
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
20 *
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
21 * You should have received a copy of the GNU General Public License
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
22 * along with this program; if not, write to the Free Software
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
24 */
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
25
8245
91c6629b1ee8 [gaim-migrate @ 8968]
Herman Bloggs <hermanator12002@yahoo.com>
parents: 8240
diff changeset
26 #include "internal.h"
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
27
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
28 #include "debug.h"
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
29 #include "account.h"
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
30 #include "network.h"
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
31 #include "prefs.h"
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
32
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
33 void
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
34 gaim_network_set_local_ip(const char *ip)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
35 {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
36 g_return_if_fail(ip != NULL);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
37
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
38 gaim_prefs_set_string("/core/network/public_ip", ip);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
39 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
40
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
41 const char *
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
42 gaim_network_get_local_ip(void)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
43 {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
44 const char *ip;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
45
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
46 if (gaim_prefs_get_bool("/core/network/auto_ip"))
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
47 return NULL;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
48
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
49 ip = gaim_prefs_get_string("/core/network/public_ip");
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
50
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
51 if (ip == NULL || *ip == '\0')
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
52 return NULL;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
53
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
54 return ip;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
55 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
56
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
57 static const char *
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
58 gaim_network_get_local_ip_from_fd(int fd)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
59 {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
60 struct sockaddr_in addr;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
61 socklen_t len;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
62 static char ip[16];
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
63 const char *tmp;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
64
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
65 g_return_val_if_fail(fd > 0, NULL);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
66
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
67 len = sizeof(addr);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
68 if (getsockname(fd, (struct sockaddr *) &addr, &len) == -1) {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
69 gaim_debug_warning("network", "getsockname: %s\n", strerror(errno));
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
70 return NULL;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
71 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
72
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
73 tmp = inet_ntoa(addr.sin_addr);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
74 strncpy(ip, tmp, sizeof(ip));
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
75 return ip;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
76 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
77
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
78 const char *
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
79 gaim_network_get_local_system_ip(int fd)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
80 {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
81 struct hostent *host;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
82 char localhost[129];
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
83 long unsigned add;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
84 static char ip[46];
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
85 const char *tmp = NULL;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
86
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
87 if (fd != -1)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
88 tmp = gaim_network_get_local_ip_from_fd(fd);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
89
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
90 if (tmp)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
91 return tmp;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
92
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
93 if (gethostname(localhost, 128) < 0)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
94 return NULL;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
95
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
96 if ((host = gethostbyname(localhost)) == NULL)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
97 return NULL;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
98
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
99 memcpy(&add, host->h_addr_list[0], 4);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
100 add = htonl(add);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
101
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
102 g_snprintf(ip, 16, "%lu.%lu.%lu.%lu",
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
103 ((add >> 24) & 255),
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
104 ((add >> 16) & 255),
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
105 ((add >> 8) & 255),
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
106 add & 255);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
107
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
108 return ip;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
109 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
110
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
111 const char *
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
112 gaim_network_get_ip_for_account(const GaimAccount *account, int fd)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
113 {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
114 if (account && (gaim_account_get_public_ip(account) != NULL))
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
115 return gaim_account_get_public_ip(account);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
116 else if (gaim_network_get_local_ip() != NULL)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
117 return gaim_network_get_local_ip();
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
118 else
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
119 return gaim_network_get_local_system_ip(fd);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
120 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
121
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
122 static int gaim_network_do_listen(short portnum)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
123 {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
124 #if HAVE_GETADDRINFO
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
125 int listenfd;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
126 const int on = 1;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
127 struct addrinfo hints, *res, *ressave;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
128 char serv[5];
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
129
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
130 snprintf(serv, sizeof(serv), "%d", portnum);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
131 memset(&hints, 0, sizeof(struct addrinfo));
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
132 hints.ai_flags = AI_PASSIVE;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
133 hints.ai_family = AF_UNSPEC;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
134 hints.ai_socktype = SOCK_STREAM;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
135 if (getaddrinfo(NULL /* any IP */, serv, &hints, &res) != 0) {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
136 gaim_debug_warning("network", "getaddrinfo: %s\n", strerror(errno));
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
137 return -1;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
138 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
139 ressave = res;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
140 do {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
141 listenfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
142 if (listenfd < 0)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
143 continue;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
144 setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
145 if (bind(listenfd, res->ai_addr, res->ai_addrlen) == 0)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
146 break; /* success */
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
147 close(listenfd);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
148 } while ( (res = res->ai_next) );
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
149
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
150 if (!res)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
151 return -1;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
152
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
153 freeaddrinfo(ressave);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
154 #else
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
155 int listenfd;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
156 const int on = 1;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
157 struct sockaddr_in sockin;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
158
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
159 if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
160 gaim_debug_warning("network", "socket: %s\n", strerror(errno));
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
161 return -1;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
162 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
163
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
164 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) != 0) {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
165 gaim_debug_warning("network", "setsockopt: %s\n", strerror(errno));
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
166 close(listenfd);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
167 return -1;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
168 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
169
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
170 memset(&sockin, 0, sizeof(struct sockaddr_in));
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
171 sockin.sin_family = AF_INET;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
172 sockin.sin_port = htons(portnum);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
173
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
174 if (bind(listenfd, (struct sockaddr *)&sockin, sizeof(struct sockaddr_in)) != 0) {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
175 gaim_debug_warning("network", "bind: %s\n", strerror(errno));
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
176 close(listenfd);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
177 return -1;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
178 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
179 #endif
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
180
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
181 if (listen(listenfd, 4) != 0) {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
182 gaim_debug_warning("network", "listen: %s\n", strerror(errno));
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
183 close(listenfd);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
184 return -1;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
185 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
186 fcntl(listenfd, F_SETFL, O_NONBLOCK);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
187
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
188 gaim_debug_info("network", "Listening on port: %hu\n", gaim_network_get_port_from_fd(listenfd));
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
189 return listenfd;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
190 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
191
8250
b248c1f4efbd [gaim-migrate @ 8973]
Mark Doliner <mark@kingant.net>
parents: 8248
diff changeset
192 int gaim_network_listen(unsigned short port)
8246
e7524f4b4ed2 [gaim-migrate @ 8969]
Mark Doliner <mark@kingant.net>
parents: 8245
diff changeset
193 {
8250
b248c1f4efbd [gaim-migrate @ 8973]
Mark Doliner <mark@kingant.net>
parents: 8248
diff changeset
194 g_return_val_if_fail(port != 0, -1);
b248c1f4efbd [gaim-migrate @ 8973]
Mark Doliner <mark@kingant.net>
parents: 8248
diff changeset
195
8246
e7524f4b4ed2 [gaim-migrate @ 8969]
Mark Doliner <mark@kingant.net>
parents: 8245
diff changeset
196 return gaim_network_do_listen(port);
e7524f4b4ed2 [gaim-migrate @ 8969]
Mark Doliner <mark@kingant.net>
parents: 8245
diff changeset
197 }
e7524f4b4ed2 [gaim-migrate @ 8969]
Mark Doliner <mark@kingant.net>
parents: 8245
diff changeset
198
8250
b248c1f4efbd [gaim-migrate @ 8973]
Mark Doliner <mark@kingant.net>
parents: 8248
diff changeset
199 int gaim_network_listen_range(unsigned short start, unsigned short end)
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
200 {
8240
609a62b8e748 [gaim-migrate @ 8963]
Mark Doliner <mark@kingant.net>
parents: 8239
diff changeset
201 int ret = -1;
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
202
8250
b248c1f4efbd [gaim-migrate @ 8973]
Mark Doliner <mark@kingant.net>
parents: 8248
diff changeset
203 if (gaim_prefs_get_bool("/core/network/ports_range_use")) {
8239
5220e0898252 [gaim-migrate @ 8962]
Mark Doliner <mark@kingant.net>
parents: 8231
diff changeset
204 start = gaim_prefs_get_int("/core/network/ports_range_start");
5220e0898252 [gaim-migrate @ 8962]
Mark Doliner <mark@kingant.net>
parents: 8231
diff changeset
205 end = gaim_prefs_get_int("/core/network/ports_range_end");
8250
b248c1f4efbd [gaim-migrate @ 8973]
Mark Doliner <mark@kingant.net>
parents: 8248
diff changeset
206 } else {
b248c1f4efbd [gaim-migrate @ 8973]
Mark Doliner <mark@kingant.net>
parents: 8248
diff changeset
207 if (end < start)
b248c1f4efbd [gaim-migrate @ 8973]
Mark Doliner <mark@kingant.net>
parents: 8248
diff changeset
208 end = start;
8239
5220e0898252 [gaim-migrate @ 8962]
Mark Doliner <mark@kingant.net>
parents: 8231
diff changeset
209 }
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
210
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
211 for (; start <= end; start++) {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
212 ret = gaim_network_do_listen(start);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
213 if (ret >= 0)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
214 break;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
215 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
216
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
217 return ret;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
218 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
219
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
220 short gaim_network_get_port_from_fd(int fd)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
221 {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
222 struct sockaddr_in addr;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
223 socklen_t len;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
224
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
225 g_return_val_if_fail(fd > 0, 0);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
226
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
227 len = sizeof(addr);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
228 if (getsockname(fd, (struct sockaddr *) &addr, &len) == -1) {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
229 gaim_debug_warning("network", "getsockname: %s\n", strerror(errno));
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
230 return 0;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
231 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
232
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
233 return ntohs(addr.sin_port);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
234 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
235
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
236 void
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
237 gaim_network_init(void)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
238 {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
239 gaim_prefs_add_none ("/core/network");
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
240 gaim_prefs_add_bool ("/core/network/auto_ip", TRUE);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
241 gaim_prefs_add_string("/core/network/public_ip", "");
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
242 gaim_prefs_add_bool ("/core/network/ports_range_use", FALSE);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
243 gaim_prefs_add_int ("/core/network/ports_range_start", 1024);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
244 gaim_prefs_add_int ("/core/network/ports_range_end", 2048);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
245 }