annotate src/network.c @ 8966:cd1c9bbab491

[gaim-migrate @ 9740] Bring back "raise window on events" for IMs and chats. We don't think these were supposed to be removed. My precious. committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Tue, 18 May 2004 02:33:12 +0000
parents 01c3db200c8f
children e40f9afd420e
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
8838
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
33 const unsigned char *
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
34 gaim_network_ip_atoi(const char *ip)
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
35 {
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
36 static unsigned char ret[4];
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
37 gchar delimiter = '.';
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
38 gchar **split;
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
39 int i;
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
40
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
41 g_return_val_if_fail(ip != NULL, NULL);
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
42
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
43 split = g_strsplit(ip, &delimiter, 4);
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
44 for (i = 0; split[i] != NULL; i++)
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
45 ret[i] = atoi(split[i]);
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
46 g_strfreev(split);
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
47
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
48 /* i should always be 4 */
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
49 if (i != 4)
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
50 return NULL;
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
51
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
52 return ret;
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
53 }
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
54
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
55 void
8834
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
56 gaim_network_set_public_ip(const char *ip)
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
57 {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
58 g_return_if_fail(ip != NULL);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
59
8838
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
60 /* XXX - Ensure the IP address is valid */
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
61
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
62 gaim_prefs_set_string("/core/network/public_ip", ip);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
63 }
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 const char *
8834
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
66 gaim_network_get_public_ip(void)
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
67 {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
68 const char *ip;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
69
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
70 ip = gaim_prefs_get_string("/core/network/public_ip");
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 if (ip == NULL || *ip == '\0')
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
73 return NULL;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
74
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 static const char *
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
79 gaim_network_get_local_ip_from_fd(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 sockaddr_in addr;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
82 socklen_t len;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
83 static char ip[16];
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
84 const char *tmp;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
85
8840
01c3db200c8f [gaim-migrate @ 9607]
Mark Doliner <mark@kingant.net>
parents: 8838
diff changeset
86 g_return_val_if_fail(fd >= 0, NULL);
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
87
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
88 len = sizeof(addr);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
89 if (getsockname(fd, (struct sockaddr *) &addr, &len) == -1) {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
90 gaim_debug_warning("network", "getsockname: %s\n", strerror(errno));
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
91 return NULL;
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
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
94 tmp = inet_ntoa(addr.sin_addr);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
95 strncpy(ip, tmp, sizeof(ip));
8838
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
96
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
97 return ip;
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
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
100 const char *
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
101 gaim_network_get_local_system_ip(int fd)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
102 {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
103 struct hostent *host;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
104 char localhost[129];
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
105 long unsigned add;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
106 static char ip[46];
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
107 const char *tmp = NULL;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
108
8840
01c3db200c8f [gaim-migrate @ 9607]
Mark Doliner <mark@kingant.net>
parents: 8838
diff changeset
109 if (fd >= 0)
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
110 tmp = gaim_network_get_local_ip_from_fd(fd);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
111
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
112 if (tmp)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
113 return tmp;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
114
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
115 if (gethostname(localhost, 128) < 0)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
116 return NULL;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
117
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
118 if ((host = gethostbyname(localhost)) == NULL)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
119 return NULL;
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 memcpy(&add, host->h_addr_list[0], 4);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
122 add = htonl(add);
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 g_snprintf(ip, 16, "%lu.%lu.%lu.%lu",
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
125 ((add >> 24) & 255),
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
126 ((add >> 16) & 255),
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
127 ((add >> 8) & 255),
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
128 add & 255);
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 return ip;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
131 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
132
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
133 const char *
8838
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
134 gaim_network_get_my_ip(int fd)
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
135 {
8834
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
136 const char *ip = NULL;
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
137
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
138 /* Check if the user specified an IP manually */
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
139 if (!gaim_prefs_get_bool("/core/network/auto_ip")) {
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
140 ip = gaim_network_get_public_ip();
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
141 if (ip != NULL)
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
142 return ip;
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
143 }
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
144
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
145 /* Just fetch the IP of the local system */
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
146 return gaim_network_get_local_system_ip(fd);
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
147 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
148
8834
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
149 static int
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
150 gaim_network_do_listen(unsigned short port)
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
151 {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
152 #if HAVE_GETADDRINFO
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
153 int listenfd;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
154 const int on = 1;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
155 struct addrinfo hints, *res, *ressave;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
156 char serv[5];
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
157
8251
86b8d8b4287e [gaim-migrate @ 8974]
Mark Doliner <mark@kingant.net>
parents: 8250
diff changeset
158 snprintf(serv, sizeof(serv), "%d", port);
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
159 memset(&hints, 0, sizeof(struct addrinfo));
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
160 hints.ai_flags = AI_PASSIVE;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
161 hints.ai_family = AF_UNSPEC;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
162 hints.ai_socktype = SOCK_STREAM;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
163 if (getaddrinfo(NULL /* any IP */, serv, &hints, &res) != 0) {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
164 gaim_debug_warning("network", "getaddrinfo: %s\n", strerror(errno));
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
165 return -1;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
166 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
167 ressave = res;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
168 do {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
169 listenfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
170 if (listenfd < 0)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
171 continue;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
172 setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
173 if (bind(listenfd, res->ai_addr, res->ai_addrlen) == 0)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
174 break; /* success */
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
175 close(listenfd);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
176 } while ( (res = res->ai_next) );
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
177
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
178 if (!res)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
179 return -1;
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 freeaddrinfo(ressave);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
182 #else
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
183 int listenfd;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
184 const int on = 1;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
185 struct sockaddr_in sockin;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
186
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
187 if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
188 gaim_debug_warning("network", "socket: %s\n", strerror(errno));
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
189 return -1;
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
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
192 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) != 0) {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
193 gaim_debug_warning("network", "setsockopt: %s\n", strerror(errno));
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
194 close(listenfd);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
195 return -1;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
196 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
197
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
198 memset(&sockin, 0, sizeof(struct sockaddr_in));
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
199 sockin.sin_family = AF_INET;
8251
86b8d8b4287e [gaim-migrate @ 8974]
Mark Doliner <mark@kingant.net>
parents: 8250
diff changeset
200 sockin.sin_port = htons(port);
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
201
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
202 if (bind(listenfd, (struct sockaddr *)&sockin, sizeof(struct sockaddr_in)) != 0) {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
203 gaim_debug_warning("network", "bind: %s\n", strerror(errno));
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
204 close(listenfd);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
205 return -1;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
206 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
207 #endif
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
208
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
209 if (listen(listenfd, 4) != 0) {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
210 gaim_debug_warning("network", "listen: %s\n", strerror(errno));
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
211 close(listenfd);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
212 return -1;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
213 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
214 fcntl(listenfd, F_SETFL, O_NONBLOCK);
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 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
217 return listenfd;
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
8834
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
220 int
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
221 gaim_network_listen(unsigned short port)
8246
e7524f4b4ed2 [gaim-migrate @ 8969]
Mark Doliner <mark@kingant.net>
parents: 8245
diff changeset
222 {
8250
b248c1f4efbd [gaim-migrate @ 8973]
Mark Doliner <mark@kingant.net>
parents: 8248
diff changeset
223 g_return_val_if_fail(port != 0, -1);
b248c1f4efbd [gaim-migrate @ 8973]
Mark Doliner <mark@kingant.net>
parents: 8248
diff changeset
224
8246
e7524f4b4ed2 [gaim-migrate @ 8969]
Mark Doliner <mark@kingant.net>
parents: 8245
diff changeset
225 return gaim_network_do_listen(port);
e7524f4b4ed2 [gaim-migrate @ 8969]
Mark Doliner <mark@kingant.net>
parents: 8245
diff changeset
226 }
e7524f4b4ed2 [gaim-migrate @ 8969]
Mark Doliner <mark@kingant.net>
parents: 8245
diff changeset
227
8834
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
228 int
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
229 gaim_network_listen_range(unsigned short start, unsigned short end)
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
230 {
8240
609a62b8e748 [gaim-migrate @ 8963]
Mark Doliner <mark@kingant.net>
parents: 8239
diff changeset
231 int ret = -1;
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
232
8250
b248c1f4efbd [gaim-migrate @ 8973]
Mark Doliner <mark@kingant.net>
parents: 8248
diff changeset
233 if (gaim_prefs_get_bool("/core/network/ports_range_use")) {
8239
5220e0898252 [gaim-migrate @ 8962]
Mark Doliner <mark@kingant.net>
parents: 8231
diff changeset
234 start = gaim_prefs_get_int("/core/network/ports_range_start");
5220e0898252 [gaim-migrate @ 8962]
Mark Doliner <mark@kingant.net>
parents: 8231
diff changeset
235 end = gaim_prefs_get_int("/core/network/ports_range_end");
8250
b248c1f4efbd [gaim-migrate @ 8973]
Mark Doliner <mark@kingant.net>
parents: 8248
diff changeset
236 } else {
b248c1f4efbd [gaim-migrate @ 8973]
Mark Doliner <mark@kingant.net>
parents: 8248
diff changeset
237 if (end < start)
b248c1f4efbd [gaim-migrate @ 8973]
Mark Doliner <mark@kingant.net>
parents: 8248
diff changeset
238 end = start;
8239
5220e0898252 [gaim-migrate @ 8962]
Mark Doliner <mark@kingant.net>
parents: 8231
diff changeset
239 }
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
240
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
241 for (; start <= end; start++) {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
242 ret = gaim_network_do_listen(start);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
243 if (ret >= 0)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
244 break;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
245 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
246
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
247 return ret;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
248 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
249
8834
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
250 unsigned short
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
251 gaim_network_get_port_from_fd(int fd)
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
252 {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
253 struct sockaddr_in addr;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
254 socklen_t len;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
255
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
256 g_return_val_if_fail(fd > 0, 0);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
257
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
258 len = sizeof(addr);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
259 if (getsockname(fd, (struct sockaddr *) &addr, &len) == -1) {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
260 gaim_debug_warning("network", "getsockname: %s\n", strerror(errno));
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
261 return 0;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
262 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
263
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
264 return ntohs(addr.sin_port);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
265 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
266
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
267 void
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
268 gaim_network_init(void)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
269 {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
270 gaim_prefs_add_none ("/core/network");
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
271 gaim_prefs_add_bool ("/core/network/auto_ip", TRUE);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
272 gaim_prefs_add_string("/core/network/public_ip", "");
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
273 gaim_prefs_add_bool ("/core/network/ports_range_use", FALSE);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
274 gaim_prefs_add_int ("/core/network/ports_range_start", 1024);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
275 gaim_prefs_add_int ("/core/network/ports_range_end", 2048);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
276 }