annotate src/network.c @ 11560:c3bbfca00e85

[gaim-migrate @ 13824] And don't report those errors in the statusbox committer: Tailor Script <tailor@pidgin.im>
author Sean Egan <seanegan@gmail.com>
date Tue, 20 Sep 2005 23:48:20 +0000
parents e1ab173ef3b5
children 5f65a0cca87c
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"
11411
8caea199b018 [gaim-migrate @ 13648]
Thomas Butter <tbutter>
parents: 11391
diff changeset
32 #include "stun.h"
11195
3aeb85cc9cda [gaim-migrate @ 13319]
Adam Warrington <awarring>
parents: 11033
diff changeset
33 #include "upnp.h"
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
34
11391
d3755a7ddd82 [gaim-migrate @ 13620]
Adam Warrington <awarring>
parents: 11336
diff changeset
35
8838
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
36 const unsigned char *
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
37 gaim_network_ip_atoi(const char *ip)
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
38 {
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
39 static unsigned char ret[4];
8981
e40f9afd420e [gaim-migrate @ 9756]
Tim Ringenbach <marv@pidgin.im>
parents: 8840
diff changeset
40 gchar *delimiter = ".";
8838
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
41 gchar **split;
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
42 int i;
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
43
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
44 g_return_val_if_fail(ip != NULL, NULL);
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
45
8981
e40f9afd420e [gaim-migrate @ 9756]
Tim Ringenbach <marv@pidgin.im>
parents: 8840
diff changeset
46 split = g_strsplit(ip, delimiter, 4);
8838
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
47 for (i = 0; split[i] != NULL; i++)
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
48 ret[i] = atoi(split[i]);
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
49 g_strfreev(split);
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
50
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
51 /* i should always be 4 */
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
52 if (i != 4)
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
53 return NULL;
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
54
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
55 return ret;
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
56 }
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
57
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
58 void
8834
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
59 gaim_network_set_public_ip(const char *ip)
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
60 {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
61 g_return_if_fail(ip != NULL);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
62
8838
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
63 /* XXX - Ensure the IP address is valid */
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
64
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
65 gaim_prefs_set_string("/core/network/public_ip", ip);
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
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
68 const char *
8834
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
69 gaim_network_get_public_ip(void)
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
70 {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
71 const char *ip;
11411
8caea199b018 [gaim-migrate @ 13648]
Thomas Butter <tbutter>
parents: 11391
diff changeset
72 struct stun_nattype *stun;
11391
d3755a7ddd82 [gaim-migrate @ 13620]
Adam Warrington <awarring>
parents: 11336
diff changeset
73
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
74 ip = gaim_prefs_get_string("/core/network/public_ip");
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
75
11411
8caea199b018 [gaim-migrate @ 13648]
Thomas Butter <tbutter>
parents: 11391
diff changeset
76 if (ip == NULL || *ip == '\0') {
8caea199b018 [gaim-migrate @ 13648]
Thomas Butter <tbutter>
parents: 11391
diff changeset
77 /* Check if STUN discovery was already done */
8caea199b018 [gaim-migrate @ 13648]
Thomas Butter <tbutter>
parents: 11391
diff changeset
78 stun = gaim_stun_discover(NULL);
8caea199b018 [gaim-migrate @ 13648]
Thomas Butter <tbutter>
parents: 11391
diff changeset
79 if(stun && stun->status>1)
8caea199b018 [gaim-migrate @ 13648]
Thomas Butter <tbutter>
parents: 11391
diff changeset
80 return stun->publicip;
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
81 return NULL;
11411
8caea199b018 [gaim-migrate @ 13648]
Thomas Butter <tbutter>
parents: 11391
diff changeset
82 }
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
83
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
84 return ip;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
85 }
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 static const char *
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
88 gaim_network_get_local_ip_from_fd(int 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 struct sockaddr_in addr;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
91 socklen_t len;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
92 static char ip[16];
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
93 const char *tmp;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
94
8840
01c3db200c8f [gaim-migrate @ 9607]
Mark Doliner <mark@kingant.net>
parents: 8838
diff changeset
95 g_return_val_if_fail(fd >= 0, NULL);
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
96
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
97 len = sizeof(addr);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
98 if (getsockname(fd, (struct sockaddr *) &addr, &len) == -1) {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
99 gaim_debug_warning("network", "getsockname: %s\n", strerror(errno));
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
100 return NULL;
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
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
103 tmp = inet_ntoa(addr.sin_addr);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
104 strncpy(ip, tmp, sizeof(ip));
8838
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
105
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
106 return ip;
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
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
109 const char *
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
110 gaim_network_get_local_system_ip(int 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 struct hostent *host;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
113 char localhost[129];
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
114 long unsigned add;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
115 static char ip[46];
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
116 const char *tmp = NULL;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
117
8840
01c3db200c8f [gaim-migrate @ 9607]
Mark Doliner <mark@kingant.net>
parents: 8838
diff changeset
118 if (fd >= 0)
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
119 tmp = gaim_network_get_local_ip_from_fd(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 if (tmp)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
122 return tmp;
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 (gethostname(localhost, 128) < 0)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
125 return NULL;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
126
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
127 if ((host = gethostbyname(localhost)) == NULL)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
128 return NULL;
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 memcpy(&add, host->h_addr_list[0], 4);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
131 add = htonl(add);
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 g_snprintf(ip, 16, "%lu.%lu.%lu.%lu",
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
134 ((add >> 24) & 255),
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
135 ((add >> 16) & 255),
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
136 ((add >> 8) & 255),
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
137 add & 255);
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 return ip;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
140 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
141
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
142 const char *
8838
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
143 gaim_network_get_my_ip(int fd)
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
144 {
11215
986160f7b6ca [gaim-migrate @ 13347]
Richard Laager <rlaager@wiktel.com>
parents: 11213
diff changeset
145 const char *ip = NULL;
11391
d3755a7ddd82 [gaim-migrate @ 13620]
Adam Warrington <awarring>
parents: 11336
diff changeset
146 GaimUPnPControlInfo* controlInfo = NULL;
11424
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11411
diff changeset
147 struct stun_nattype *stun;
8834
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
148
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
149 /* Check if the user specified an IP manually */
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
150 if (!gaim_prefs_get_bool("/core/network/auto_ip")) {
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
151 ip = gaim_network_get_public_ip();
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
152 if (ip != NULL)
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
153 return ip;
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
154 }
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
155
11424
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11411
diff changeset
156 if (ip == NULL || *ip == '\0') {
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11411
diff changeset
157 /* Check if STUN discovery was already done */
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11411
diff changeset
158 stun = gaim_stun_discover(NULL);
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11411
diff changeset
159 if(stun && stun->status>1)
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11411
diff changeset
160 return stun->publicip;
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11411
diff changeset
161 }
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11411
diff changeset
162
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11411
diff changeset
163
11195
3aeb85cc9cda [gaim-migrate @ 13319]
Adam Warrington <awarring>
parents: 11033
diff changeset
164 /* attempt to get the ip from a NAT device */
11391
d3755a7ddd82 [gaim-migrate @ 13620]
Adam Warrington <awarring>
parents: 11336
diff changeset
165 if ((controlInfo = gaim_upnp_discover()) != NULL) {
d3755a7ddd82 [gaim-migrate @ 13620]
Adam Warrington <awarring>
parents: 11336
diff changeset
166 ip = gaim_upnp_get_public_ip(controlInfo);
d3755a7ddd82 [gaim-migrate @ 13620]
Adam Warrington <awarring>
parents: 11336
diff changeset
167 g_free(controlInfo->controlURL);
d3755a7ddd82 [gaim-migrate @ 13620]
Adam Warrington <awarring>
parents: 11336
diff changeset
168 g_free(controlInfo->serviceType);
d3755a7ddd82 [gaim-migrate @ 13620]
Adam Warrington <awarring>
parents: 11336
diff changeset
169 g_free(controlInfo);
d3755a7ddd82 [gaim-migrate @ 13620]
Adam Warrington <awarring>
parents: 11336
diff changeset
170 if (ip != NULL) {
11195
3aeb85cc9cda [gaim-migrate @ 13319]
Adam Warrington <awarring>
parents: 11033
diff changeset
171 return ip;
11391
d3755a7ddd82 [gaim-migrate @ 13620]
Adam Warrington <awarring>
parents: 11336
diff changeset
172 }
11195
3aeb85cc9cda [gaim-migrate @ 13319]
Adam Warrington <awarring>
parents: 11033
diff changeset
173 }
3aeb85cc9cda [gaim-migrate @ 13319]
Adam Warrington <awarring>
parents: 11033
diff changeset
174
8834
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
175 /* Just fetch the IP of the local system */
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
176 return gaim_network_get_local_system_ip(fd);
8231
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
11391
d3755a7ddd82 [gaim-migrate @ 13620]
Adam Warrington <awarring>
parents: 11336
diff changeset
179
8834
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
180 static int
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
181 gaim_network_do_listen(unsigned short port)
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
182 {
9452
0de62d92fb3b [gaim-migrate @ 10276]
Mark Doliner <mark@kingant.net>
parents: 9449
diff changeset
183 int listenfd = -1;
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
184 const int on = 1;
11391
d3755a7ddd82 [gaim-migrate @ 13620]
Adam Warrington <awarring>
parents: 11336
diff changeset
185 GaimUPnPControlInfo* controlInfo = NULL;
9449
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
186 #if HAVE_GETADDRINFO
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
187 int errnum;
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
188 struct addrinfo hints, *res, *next;
9456
0577bb34622d [gaim-migrate @ 10280]
Tim Ringenbach <marv@pidgin.im>
parents: 9455
diff changeset
189 char serv[6];
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
190
9449
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
191 /*
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
192 * Get a list of addresses on this machine.
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
193 */
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
194 snprintf(serv, sizeof(serv), "%hu", port);
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
195 memset(&hints, 0, sizeof(struct addrinfo));
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
196 hints.ai_flags = AI_PASSIVE;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
197 hints.ai_family = AF_UNSPEC;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
198 hints.ai_socktype = SOCK_STREAM;
9449
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
199 errnum = getaddrinfo(NULL /* any IP */, serv, &hints, &res);
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
200 if (errnum != 0) {
11221
5ed33bb06a84 [gaim-migrate @ 13353]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11215
diff changeset
201 #ifndef _WIN32
9449
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
202 gaim_debug_warning("network", "getaddrinfo: %s\n", gai_strerror(errnum));
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
203 if (errnum == EAI_SYSTEM)
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
204 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
205 #else
5ed33bb06a84 [gaim-migrate @ 13353]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11215
diff changeset
206 gaim_debug_warning("network", "getaddrinfo: Error Code = %d\n", errnum);
5ed33bb06a84 [gaim-migrate @ 13353]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11215
diff changeset
207 #endif
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
208 return -1;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
209 }
9449
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
210
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
211 /*
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
212 * Go through the list of addresses and attempt to listen on
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
213 * one of them.
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
214 * XXX - Try IPv6 addresses first?
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
215 */
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
216 for (next = res; next != NULL; next = next->ai_next) {
9455
5b4b3fb286b0 [gaim-migrate @ 10279]
Tim Ringenbach <marv@pidgin.im>
parents: 9452
diff changeset
217 listenfd = socket(next->ai_family, next->ai_socktype, next->ai_protocol);
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
218 if (listenfd < 0)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
219 continue;
9449
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
220 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0)
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
221 gaim_debug_warning("network", "setsockopt: %s\n", strerror(errno));
9455
5b4b3fb286b0 [gaim-migrate @ 10279]
Tim Ringenbach <marv@pidgin.im>
parents: 9452
diff changeset
222 if (bind(listenfd, next->ai_addr, next->ai_addrlen) == 0)
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
223 break; /* success */
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
224 close(listenfd);
9449
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
225 }
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
226
9449
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
227 freeaddrinfo(res);
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
228
9449
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
229 if (next == NULL)
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
230 return -1;
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
231 #else
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
232 struct sockaddr_in sockin;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
233
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
234 if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
235 gaim_debug_warning("network", "socket: %s\n", strerror(errno));
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
236 return -1;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
237 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
238
9449
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
239 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0)
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
240 gaim_debug_warning("network", "setsockopt: %s\n", strerror(errno));
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
241
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
242 memset(&sockin, 0, sizeof(struct sockaddr_in));
9449
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
243 sockin.sin_family = PF_INET;
8251
86b8d8b4287e [gaim-migrate @ 8974]
Mark Doliner <mark@kingant.net>
parents: 8250
diff changeset
244 sockin.sin_port = htons(port);
8231
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 if (bind(listenfd, (struct sockaddr *)&sockin, sizeof(struct sockaddr_in)) != 0) {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
247 gaim_debug_warning("network", "bind: %s\n", strerror(errno));
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
248 close(listenfd);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
249 return -1;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
250 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
251 #endif
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 if (listen(listenfd, 4) != 0) {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
254 gaim_debug_warning("network", "listen: %s\n", strerror(errno));
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
255 close(listenfd);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
256 return -1;
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 fcntl(listenfd, F_SETFL, O_NONBLOCK);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
259
11391
d3755a7ddd82 [gaim-migrate @ 13620]
Adam Warrington <awarring>
parents: 11336
diff changeset
260 if((controlInfo = gaim_upnp_discover()) != NULL) {
d3755a7ddd82 [gaim-migrate @ 13620]
Adam Warrington <awarring>
parents: 11336
diff changeset
261 if(!gaim_upnp_set_port_mapping(controlInfo,
d3755a7ddd82 [gaim-migrate @ 13620]
Adam Warrington <awarring>
parents: 11336
diff changeset
262 gaim_network_get_port_from_fd(listenfd),
d3755a7ddd82 [gaim-migrate @ 13620]
Adam Warrington <awarring>
parents: 11336
diff changeset
263 "TCP")) {
d3755a7ddd82 [gaim-migrate @ 13620]
Adam Warrington <awarring>
parents: 11336
diff changeset
264 gaim_upnp_remove_port_mapping(controlInfo,
d3755a7ddd82 [gaim-migrate @ 13620]
Adam Warrington <awarring>
parents: 11336
diff changeset
265 gaim_network_get_port_from_fd(listenfd), "TCP");
d3755a7ddd82 [gaim-migrate @ 13620]
Adam Warrington <awarring>
parents: 11336
diff changeset
266 gaim_upnp_set_port_mapping(controlInfo,
d3755a7ddd82 [gaim-migrate @ 13620]
Adam Warrington <awarring>
parents: 11336
diff changeset
267 gaim_network_get_port_from_fd(listenfd), "TCP");
d3755a7ddd82 [gaim-migrate @ 13620]
Adam Warrington <awarring>
parents: 11336
diff changeset
268
d3755a7ddd82 [gaim-migrate @ 13620]
Adam Warrington <awarring>
parents: 11336
diff changeset
269 }
d3755a7ddd82 [gaim-migrate @ 13620]
Adam Warrington <awarring>
parents: 11336
diff changeset
270 g_free(controlInfo->serviceType);
d3755a7ddd82 [gaim-migrate @ 13620]
Adam Warrington <awarring>
parents: 11336
diff changeset
271 g_free(controlInfo->controlURL);
d3755a7ddd82 [gaim-migrate @ 13620]
Adam Warrington <awarring>
parents: 11336
diff changeset
272 g_free(controlInfo);
11195
3aeb85cc9cda [gaim-migrate @ 13319]
Adam Warrington <awarring>
parents: 11033
diff changeset
273 }
3aeb85cc9cda [gaim-migrate @ 13319]
Adam Warrington <awarring>
parents: 11033
diff changeset
274
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
275 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
276 return listenfd;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
277 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
278
8834
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
279 int
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
280 gaim_network_listen(unsigned short port)
8246
e7524f4b4ed2 [gaim-migrate @ 8969]
Mark Doliner <mark@kingant.net>
parents: 8245
diff changeset
281 {
8250
b248c1f4efbd [gaim-migrate @ 8973]
Mark Doliner <mark@kingant.net>
parents: 8248
diff changeset
282 g_return_val_if_fail(port != 0, -1);
b248c1f4efbd [gaim-migrate @ 8973]
Mark Doliner <mark@kingant.net>
parents: 8248
diff changeset
283
8246
e7524f4b4ed2 [gaim-migrate @ 8969]
Mark Doliner <mark@kingant.net>
parents: 8245
diff changeset
284 return gaim_network_do_listen(port);
e7524f4b4ed2 [gaim-migrate @ 8969]
Mark Doliner <mark@kingant.net>
parents: 8245
diff changeset
285 }
e7524f4b4ed2 [gaim-migrate @ 8969]
Mark Doliner <mark@kingant.net>
parents: 8245
diff changeset
286
8834
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
287 int
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
288 gaim_network_listen_range(unsigned short start, unsigned short end)
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
289 {
8240
609a62b8e748 [gaim-migrate @ 8963]
Mark Doliner <mark@kingant.net>
parents: 8239
diff changeset
290 int ret = -1;
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
291
8250
b248c1f4efbd [gaim-migrate @ 8973]
Mark Doliner <mark@kingant.net>
parents: 8248
diff changeset
292 if (gaim_prefs_get_bool("/core/network/ports_range_use")) {
8239
5220e0898252 [gaim-migrate @ 8962]
Mark Doliner <mark@kingant.net>
parents: 8231
diff changeset
293 start = gaim_prefs_get_int("/core/network/ports_range_start");
5220e0898252 [gaim-migrate @ 8962]
Mark Doliner <mark@kingant.net>
parents: 8231
diff changeset
294 end = gaim_prefs_get_int("/core/network/ports_range_end");
8250
b248c1f4efbd [gaim-migrate @ 8973]
Mark Doliner <mark@kingant.net>
parents: 8248
diff changeset
295 } else {
b248c1f4efbd [gaim-migrate @ 8973]
Mark Doliner <mark@kingant.net>
parents: 8248
diff changeset
296 if (end < start)
b248c1f4efbd [gaim-migrate @ 8973]
Mark Doliner <mark@kingant.net>
parents: 8248
diff changeset
297 end = start;
8239
5220e0898252 [gaim-migrate @ 8962]
Mark Doliner <mark@kingant.net>
parents: 8231
diff changeset
298 }
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
299
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
300 for (; start <= end; start++) {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
301 ret = gaim_network_do_listen(start);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
302 if (ret >= 0)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
303 break;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
304 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
305
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
306 return ret;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
307 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
308
8834
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
309 unsigned short
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
310 gaim_network_get_port_from_fd(int fd)
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
311 {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
312 struct sockaddr_in addr;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
313 socklen_t len;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
314
9449
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
315 g_return_val_if_fail(fd >= 0, 0);
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
316
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
317 len = sizeof(addr);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
318 if (getsockname(fd, (struct sockaddr *) &addr, &len) == -1) {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
319 gaim_debug_warning("network", "getsockname: %s\n", strerror(errno));
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
320 return 0;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
321 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
322
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
323 return ntohs(addr.sin_port);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
324 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
325
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
326 void
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
327 gaim_network_init(void)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
328 {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
329 gaim_prefs_add_none ("/core/network");
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
330 gaim_prefs_add_bool ("/core/network/auto_ip", TRUE);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
331 gaim_prefs_add_string("/core/network/public_ip", "");
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
332 gaim_prefs_add_bool ("/core/network/ports_range_use", FALSE);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
333 gaim_prefs_add_int ("/core/network/ports_range_start", 1024);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
334 gaim_prefs_add_int ("/core/network/ports_range_end", 2048);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
335 }