annotate src/network.c @ 13723:1983a8fc8e72

[gaim-migrate @ 16132] Make the ntlm code a little cleaner (to me, anyway). Add some helpful comments (to me, anyway). And hopefully fix a bug or two (hostname, domain, and username are all sent as UCS-2LE now). committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Thu, 04 May 2006 00:33:33 +0000
parents 873845ed5b32
children
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
13155
fae5a5517f18 [gaim-migrate @ 15518]
Thomas Butter <tbutter>
parents: 13130
diff changeset
28 #ifndef _WIN32
fae5a5517f18 [gaim-migrate @ 15518]
Thomas Butter <tbutter>
parents: 13130
diff changeset
29 #include <net/if.h>
fae5a5517f18 [gaim-migrate @ 15518]
Thomas Butter <tbutter>
parents: 13130
diff changeset
30 #include <sys/ioctl.h>
fae5a5517f18 [gaim-migrate @ 15518]
Thomas Butter <tbutter>
parents: 13130
diff changeset
31 #endif
fae5a5517f18 [gaim-migrate @ 15518]
Thomas Butter <tbutter>
parents: 13130
diff changeset
32
fae5a5517f18 [gaim-migrate @ 15518]
Thomas Butter <tbutter>
parents: 13130
diff changeset
33 /* Solaris */
fae5a5517f18 [gaim-migrate @ 15518]
Thomas Butter <tbutter>
parents: 13130
diff changeset
34 #if defined (__SVR4) && defined (__sun)
fae5a5517f18 [gaim-migrate @ 15518]
Thomas Butter <tbutter>
parents: 13130
diff changeset
35 #include <sys/sockio.h>
fae5a5517f18 [gaim-migrate @ 15518]
Thomas Butter <tbutter>
parents: 13130
diff changeset
36 #endif
fae5a5517f18 [gaim-migrate @ 15518]
Thomas Butter <tbutter>
parents: 13130
diff changeset
37
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
38 #include "debug.h"
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
39 #include "account.h"
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
40 #include "network.h"
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
41 #include "prefs.h"
11411
8caea199b018 [gaim-migrate @ 13648]
Thomas Butter <tbutter>
parents: 11391
diff changeset
42 #include "stun.h"
11195
3aeb85cc9cda [gaim-migrate @ 13319]
Adam Warrington <awarring>
parents: 11033
diff changeset
43 #include "upnp.h"
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
44
12909
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
45 typedef struct {
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
46 int listenfd;
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
47 int socket_type;
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
48 gboolean retry;
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
49 gboolean adding;
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
50 GaimNetworkListenCallback cb;
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
51 gpointer cb_data;
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
52 } ListenUPnPData;
11391
d3755a7ddd82 [gaim-migrate @ 13620]
Adam Warrington <awarring>
parents: 11336
diff changeset
53
8838
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
54 const unsigned char *
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
55 gaim_network_ip_atoi(const char *ip)
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 static unsigned char ret[4];
8981
e40f9afd420e [gaim-migrate @ 9756]
Tim Ringenbach <marv@pidgin.im>
parents: 8840
diff changeset
58 gchar *delimiter = ".";
8838
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
59 gchar **split;
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
60 int i;
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
61
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
62 g_return_val_if_fail(ip != NULL, NULL);
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
63
8981
e40f9afd420e [gaim-migrate @ 9756]
Tim Ringenbach <marv@pidgin.im>
parents: 8840
diff changeset
64 split = g_strsplit(ip, delimiter, 4);
8838
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
65 for (i = 0; split[i] != NULL; i++)
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
66 ret[i] = atoi(split[i]);
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
67 g_strfreev(split);
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
68
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
69 /* i should always be 4 */
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
70 if (i != 4)
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
71 return NULL;
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
72
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
73 return ret;
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
74 }
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
75
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
76 void
8834
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
77 gaim_network_set_public_ip(const char *ip)
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
78 {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
79 g_return_if_fail(ip != NULL);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
80
8838
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
81 /* XXX - Ensure the IP address is valid */
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
82
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
83 gaim_prefs_set_string("/core/network/public_ip", ip);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
84 }
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 const char *
8834
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
87 gaim_network_get_public_ip(void)
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
88 {
13129
d0ae6489a0fb [gaim-migrate @ 15491]
Mark Doliner <mark@kingant.net>
parents: 12917
diff changeset
89 return gaim_prefs_get_string("/core/network/public_ip");
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
90 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
91
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
92 const char *
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
93 gaim_network_get_local_system_ip(int fd)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
94 {
13345
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
95 char buffer[1024];
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
96 static char ip[16];
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
97 char *tmp;
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
98 struct ifconf ifc;
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
99 struct ifreq *ifr;
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
100 struct sockaddr_in *sinptr;
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
101 guint32 lhost = htonl(127 * 256 * 256 * 256 + 1);
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
102 long unsigned int add;
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
103 int source = fd;
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
104
13345
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
105 if (fd < 0)
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
106 source = socket(PF_INET,SOCK_STREAM, 0);
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
107
13345
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
108 ifc.ifc_len = sizeof(buffer);
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
109 ifc.ifc_req = (struct ifreq *)buffer;
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
110 ioctl(source, SIOCGIFCONF, &ifc);
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
111
13345
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
112 if (fd < 0)
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
113 close(source);
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
114
13345
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
115 tmp = buffer;
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
116 while (tmp < buffer + ifc.ifc_len)
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
117 {
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
118 ifr = (struct ifreq *)tmp;
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
119 tmp += sizeof(struct ifreq);
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
120
13345
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
121 if (ifr->ifr_addr.sa_family == AF_INET)
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
122 {
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
123 sinptr = (struct sockaddr_in *)&ifr->ifr_addr;
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
124 if (sinptr->sin_addr.s_addr != lhost)
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
125 {
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
126 add = ntohl(sinptr->sin_addr.s_addr);
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
127 g_snprintf(ip, 16, "%lu.%lu.%lu.%lu",
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
128 ((add >> 24) & 255),
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
129 ((add >> 16) & 255),
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
130 ((add >> 8) & 255),
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
131 add & 255);
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
132
13345
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
133 return ip;
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
134 }
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
135 }
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
136 }
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
137
d7b6e358493d [gaim-migrate @ 15715]
Mark Doliner <mark@kingant.net>
parents: 13167
diff changeset
138 return "0.0.0.0";
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
139 }
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 const char *
8838
518455386538 [gaim-migrate @ 9604]
Mark Doliner <mark@kingant.net>
parents: 8834
diff changeset
142 gaim_network_get_my_ip(int fd)
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
143 {
12686
5f65a0cca87c [gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents: 11424
diff changeset
144 const char *ip = NULL;
5f65a0cca87c [gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents: 11424
diff changeset
145 GaimStunNatDiscovery *stun;
8834
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
146
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
147 /* Check if the user specified an IP manually */
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
148 if (!gaim_prefs_get_bool("/core/network/auto_ip")) {
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
149 ip = gaim_network_get_public_ip();
13129
d0ae6489a0fb [gaim-migrate @ 15491]
Mark Doliner <mark@kingant.net>
parents: 12917
diff changeset
150 if ((ip != NULL) && (*ip != '\0'))
8834
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
151 return ip;
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
152 }
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
153
13129
d0ae6489a0fb [gaim-migrate @ 15491]
Mark Doliner <mark@kingant.net>
parents: 12917
diff changeset
154 /* Check if STUN discovery was already done */
d0ae6489a0fb [gaim-migrate @ 15491]
Mark Doliner <mark@kingant.net>
parents: 12917
diff changeset
155 stun = gaim_stun_discover(NULL);
d0ae6489a0fb [gaim-migrate @ 15491]
Mark Doliner <mark@kingant.net>
parents: 12917
diff changeset
156 if ((stun != NULL) && (stun->status == GAIM_STUN_STATUS_DISCOVERED))
d0ae6489a0fb [gaim-migrate @ 15491]
Mark Doliner <mark@kingant.net>
parents: 12917
diff changeset
157 return stun->publicip;
11424
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11411
diff changeset
158
13129
d0ae6489a0fb [gaim-migrate @ 15491]
Mark Doliner <mark@kingant.net>
parents: 12917
diff changeset
159 /* Attempt to get the IP from a NAT device using UPnP */
d0ae6489a0fb [gaim-migrate @ 15491]
Mark Doliner <mark@kingant.net>
parents: 12917
diff changeset
160 ip = gaim_upnp_get_public_ip();
d0ae6489a0fb [gaim-migrate @ 15491]
Mark Doliner <mark@kingant.net>
parents: 12917
diff changeset
161 if (ip != NULL)
d0ae6489a0fb [gaim-migrate @ 15491]
Mark Doliner <mark@kingant.net>
parents: 12917
diff changeset
162 return ip;
11195
3aeb85cc9cda [gaim-migrate @ 13319]
Adam Warrington <awarring>
parents: 11033
diff changeset
163
8834
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
164 /* Just fetch the IP of the local system */
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
165 return gaim_network_get_local_system_ip(fd);
8231
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
11391
d3755a7ddd82 [gaim-migrate @ 13620]
Adam Warrington <awarring>
parents: 11336
diff changeset
168
12909
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
169 static void
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
170 gaim_network_set_upnp_port_mapping_cb(gboolean success, gpointer data)
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
171 {
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
172 ListenUPnPData *ldata = data;
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
173
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
174 if (!success) {
13679
873845ed5b32 [gaim-migrate @ 16081]
Mark Doliner <mark@kingant.net>
parents: 13425
diff changeset
175 gaim_debug_info("network", "Couldn't create UPnP mapping\n");
12909
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
176 if (ldata->retry) {
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
177 ldata->retry = FALSE;
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
178 ldata->adding = FALSE;
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
179 gaim_upnp_remove_port_mapping(
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
180 gaim_network_get_port_from_fd(ldata->listenfd),
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
181 (ldata->socket_type == SOCK_STREAM) ? "TCP" : "UDP",
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
182 gaim_network_set_upnp_port_mapping_cb, ldata);
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
183 return;
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
184 }
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
185 } else if (!ldata->adding) {
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
186 /* We've tried successfully to remove the port mapping.
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
187 * Try to add it again */
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
188 ldata->adding = TRUE;
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
189 gaim_upnp_set_port_mapping(
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
190 gaim_network_get_port_from_fd(ldata->listenfd),
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
191 (ldata->socket_type == SOCK_STREAM) ? "TCP" : "UDP",
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
192 gaim_network_set_upnp_port_mapping_cb, ldata);
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
193 return;
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
194 }
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
195
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
196 if (ldata->cb)
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
197 ldata->cb(ldata->listenfd, ldata->cb_data);
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
198
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
199 g_free(ldata);
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
200 }
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
201
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
202
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
203 static gboolean
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
204 gaim_network_do_listen(unsigned short port, int socket_type, GaimNetworkListenCallback cb, gpointer cb_data)
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
205 {
9452
0de62d92fb3b [gaim-migrate @ 10276]
Mark Doliner <mark@kingant.net>
parents: 9449
diff changeset
206 int listenfd = -1;
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
207 const int on = 1;
12917
a85c8c11bfab [gaim-migrate @ 15270]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12909
diff changeset
208 ListenUPnPData *ld;
13425
95cc25e78ef6 [gaim-migrate @ 15800]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13345
diff changeset
209 #ifdef HAVE_GETADDRINFO
9449
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
210 int errnum;
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
211 struct addrinfo hints, *res, *next;
9456
0577bb34622d [gaim-migrate @ 10280]
Tim Ringenbach <marv@pidgin.im>
parents: 9455
diff changeset
212 char serv[6];
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
213
9449
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
214 /*
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
215 * Get a list of addresses on this machine.
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
216 */
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
217 snprintf(serv, sizeof(serv), "%hu", port);
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
218 memset(&hints, 0, sizeof(struct addrinfo));
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
219 hints.ai_flags = AI_PASSIVE;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
220 hints.ai_family = AF_UNSPEC;
12730
d5b8f4dc1622 [gaim-migrate @ 15074]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12728
diff changeset
221 hints.ai_socktype = socket_type;
9449
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
222 errnum = getaddrinfo(NULL /* any IP */, serv, &hints, &res);
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
223 if (errnum != 0) {
11221
5ed33bb06a84 [gaim-migrate @ 13353]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11215
diff changeset
224 #ifndef _WIN32
9449
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
225 gaim_debug_warning("network", "getaddrinfo: %s\n", gai_strerror(errnum));
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
226 if (errnum == EAI_SYSTEM)
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
227 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
228 #else
12909
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
229 gaim_debug_warning("network", "getaddrinfo: Error Code = %d\n", errnum);
11221
5ed33bb06a84 [gaim-migrate @ 13353]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11215
diff changeset
230 #endif
12909
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
231 return FALSE;
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
232 }
9449
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
233
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
234 /*
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
235 * Go through the list of addresses and attempt to listen on
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
236 * one of them.
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
237 * XXX - Try IPv6 addresses first?
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
238 */
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
239 for (next = res; next != NULL; next = next->ai_next) {
9455
5b4b3fb286b0 [gaim-migrate @ 10279]
Tim Ringenbach <marv@pidgin.im>
parents: 9452
diff changeset
240 listenfd = socket(next->ai_family, next->ai_socktype, next->ai_protocol);
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
241 if (listenfd < 0)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
242 continue;
9449
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
243 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0)
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
244 gaim_debug_warning("network", "setsockopt: %s\n", strerror(errno));
9455
5b4b3fb286b0 [gaim-migrate @ 10279]
Tim Ringenbach <marv@pidgin.im>
parents: 9452
diff changeset
245 if (bind(listenfd, next->ai_addr, next->ai_addrlen) == 0)
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
246 break; /* success */
12730
d5b8f4dc1622 [gaim-migrate @ 15074]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12728
diff changeset
247 /* XXX - It is unclear to me (datallah) whether we need to be
d5b8f4dc1622 [gaim-migrate @ 15074]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12728
diff changeset
248 using a new socket each time */
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
249 close(listenfd);
9449
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
250 }
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
251
9449
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
252 freeaddrinfo(res);
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
253
9449
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
254 if (next == NULL)
12909
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
255 return FALSE;
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
256 #else
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
257 struct sockaddr_in sockin;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
258
12730
d5b8f4dc1622 [gaim-migrate @ 15074]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12728
diff changeset
259 if ((listenfd = socket(AF_INET, socket_type, 0)) < 0) {
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
260 gaim_debug_warning("network", "socket: %s\n", strerror(errno));
12909
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
261 return FALSE;
8231
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
9449
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
264 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0)
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
265 gaim_debug_warning("network", "setsockopt: %s\n", strerror(errno));
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 memset(&sockin, 0, sizeof(struct sockaddr_in));
9449
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
268 sockin.sin_family = PF_INET;
8251
86b8d8b4287e [gaim-migrate @ 8974]
Mark Doliner <mark@kingant.net>
parents: 8250
diff changeset
269 sockin.sin_port = htons(port);
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
270
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
271 if (bind(listenfd, (struct sockaddr *)&sockin, sizeof(struct sockaddr_in)) != 0) {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
272 gaim_debug_warning("network", "bind: %s\n", strerror(errno));
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
273 close(listenfd);
12909
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
274 return FALSE;
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
275 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
276 #endif
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
277
12730
d5b8f4dc1622 [gaim-migrate @ 15074]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12728
diff changeset
278 if (socket_type == SOCK_STREAM && listen(listenfd, 4) != 0) {
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
279 gaim_debug_warning("network", "listen: %s\n", strerror(errno));
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
280 close(listenfd);
12909
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
281 return FALSE;
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
282 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
283 fcntl(listenfd, F_SETFL, O_NONBLOCK);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
284
12909
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
285 gaim_debug_info("network", "Listening on port: %hu\n", gaim_network_get_port_from_fd(listenfd));
11391
d3755a7ddd82 [gaim-migrate @ 13620]
Adam Warrington <awarring>
parents: 11336
diff changeset
286
12909
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
287 ld = g_new0(ListenUPnPData, 1);
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
288 ld->listenfd = listenfd;
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
289 ld->adding = TRUE;
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
290 ld->retry = TRUE;
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
291 ld->cb = cb;
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
292 ld->cb_data = cb_data;
11195
3aeb85cc9cda [gaim-migrate @ 13319]
Adam Warrington <awarring>
parents: 11033
diff changeset
293
12909
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
294 gaim_upnp_set_port_mapping(
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
295 gaim_network_get_port_from_fd(listenfd),
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
296 (socket_type == SOCK_STREAM) ? "TCP" : "UDP",
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
297 gaim_network_set_upnp_port_mapping_cb, ld);
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
298
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
299 return TRUE;
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
300 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
301
12909
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
302 gboolean
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
303 gaim_network_listen(unsigned short port, int socket_type,
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
304 GaimNetworkListenCallback cb, gpointer cb_data)
8246
e7524f4b4ed2 [gaim-migrate @ 8969]
Mark Doliner <mark@kingant.net>
parents: 8245
diff changeset
305 {
8250
b248c1f4efbd [gaim-migrate @ 8973]
Mark Doliner <mark@kingant.net>
parents: 8248
diff changeset
306 g_return_val_if_fail(port != 0, -1);
b248c1f4efbd [gaim-migrate @ 8973]
Mark Doliner <mark@kingant.net>
parents: 8248
diff changeset
307
12909
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
308 return gaim_network_do_listen(port, socket_type, cb, cb_data);
8246
e7524f4b4ed2 [gaim-migrate @ 8969]
Mark Doliner <mark@kingant.net>
parents: 8245
diff changeset
309 }
e7524f4b4ed2 [gaim-migrate @ 8969]
Mark Doliner <mark@kingant.net>
parents: 8245
diff changeset
310
12909
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
311 gboolean
12730
d5b8f4dc1622 [gaim-migrate @ 15074]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12728
diff changeset
312 gaim_network_listen_range(unsigned short start, unsigned short end,
12909
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
313 int socket_type, GaimNetworkListenCallback cb, gpointer cb_data)
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
314 {
12909
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
315 gboolean ret = FALSE;
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
316
8250
b248c1f4efbd [gaim-migrate @ 8973]
Mark Doliner <mark@kingant.net>
parents: 8248
diff changeset
317 if (gaim_prefs_get_bool("/core/network/ports_range_use")) {
8239
5220e0898252 [gaim-migrate @ 8962]
Mark Doliner <mark@kingant.net>
parents: 8231
diff changeset
318 start = gaim_prefs_get_int("/core/network/ports_range_start");
5220e0898252 [gaim-migrate @ 8962]
Mark Doliner <mark@kingant.net>
parents: 8231
diff changeset
319 end = gaim_prefs_get_int("/core/network/ports_range_end");
8250
b248c1f4efbd [gaim-migrate @ 8973]
Mark Doliner <mark@kingant.net>
parents: 8248
diff changeset
320 } else {
b248c1f4efbd [gaim-migrate @ 8973]
Mark Doliner <mark@kingant.net>
parents: 8248
diff changeset
321 if (end < start)
b248c1f4efbd [gaim-migrate @ 8973]
Mark Doliner <mark@kingant.net>
parents: 8248
diff changeset
322 end = start;
8239
5220e0898252 [gaim-migrate @ 8962]
Mark Doliner <mark@kingant.net>
parents: 8231
diff changeset
323 }
8231
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 for (; start <= end; start++) {
12909
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
326 ret = gaim_network_do_listen(start, socket_type, cb, cb_data);
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
327 if (ret)
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
328 break;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
329 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
330
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
331 return ret;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
332 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
333
8834
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
334 unsigned short
beb7be215db3 [gaim-migrate @ 9598]
Mark Doliner <mark@kingant.net>
parents: 8251
diff changeset
335 gaim_network_get_port_from_fd(int fd)
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
336 {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
337 struct sockaddr_in addr;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
338 socklen_t len;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
339
9449
ad93d76666dd [gaim-migrate @ 10273]
Mark Doliner <mark@kingant.net>
parents: 8981
diff changeset
340 g_return_val_if_fail(fd >= 0, 0);
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
341
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
342 len = sizeof(addr);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
343 if (getsockname(fd, (struct sockaddr *) &addr, &len) == -1) {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
344 gaim_debug_warning("network", "getsockname: %s\n", strerror(errno));
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
345 return 0;
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
346 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
347
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
348 return ntohs(addr.sin_port);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
349 }
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
350
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
351 void
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
352 gaim_network_init(void)
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
353 {
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
354 gaim_prefs_add_none ("/core/network");
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
355 gaim_prefs_add_bool ("/core/network/auto_ip", TRUE);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
356 gaim_prefs_add_string("/core/network/public_ip", "");
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
357 gaim_prefs_add_bool ("/core/network/ports_range_use", FALSE);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
358 gaim_prefs_add_int ("/core/network/ports_range_start", 1024);
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
359 gaim_prefs_add_int ("/core/network/ports_range_end", 2048);
12909
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
360
8e3b85fe4a55 [gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12730
diff changeset
361 gaim_upnp_discover(NULL, NULL);
8231
f50c059b6384 [gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
diff changeset
362 }