annotate libgaim/network.c @ 14911:9f3be32d0913

[gaim-migrate @ 17683] I meant to remove this a revision or three ago committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Mon, 06 Nov 2006 07:52:17 +0000
parents e429c4ebbae5
children f58283ce58ea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
1 /**
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
2 * @file network.c Network Implementation
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
3 * @ingroup core
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
4 *
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
5 * gaim
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
6 *
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
7 * Gaim is the legal property of its developers, whose names are too numerous
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
8 * to list here. Please refer to the COPYRIGHT file distributed with this
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
9 * source distribution.
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
10 *
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
11 * This program is free software; you can redistribute it and/or modify
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
12 * it under the terms of the GNU General Public License as published by
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
13 * the Free Software Foundation; either version 2 of the License, or
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
14 * (at your option) any later version.
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
15 *
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
16 * This program is distributed in the hope that it will be useful,
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
19 * GNU General Public License for more details.
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
20 *
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
21 * You should have received a copy of the GNU General Public License
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
22 * along with this program; if not, write to the Free Software
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
24 */
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
25
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
26 #include "internal.h"
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
27
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
28 #ifndef _WIN32
14802
4138063d89c9 [gaim-migrate @ 17567]
Sean Egan <seanegan@gmail.com>
parents: 14800
diff changeset
29 #include <resolv.h>
4138063d89c9 [gaim-migrate @ 17567]
Sean Egan <seanegan@gmail.com>
parents: 14800
diff changeset
30 #include <netinet/in.h>
4138063d89c9 [gaim-migrate @ 17567]
Sean Egan <seanegan@gmail.com>
parents: 14800
diff changeset
31 #include <arpa/nameser.h>
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
32 #include <net/if.h>
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
33 #include <sys/ioctl.h>
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
34 #else
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
35 #include <nspapi.h>
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
36 #endif
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
37
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
38 /* Solaris */
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
39 #if defined (__SVR4) && defined (__sun)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
40 #include <sys/sockio.h>
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
41 #endif
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
42
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
43 #include "debug.h"
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
44 #include "account.h"
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
45 #include "network.h"
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
46 #include "prefs.h"
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
47 #include "stun.h"
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
48 #include "upnp.h"
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
49
14696
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
50 #ifdef HAVE_LIBNM
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
51 #include <libnm_glib.h>
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
52
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
53 libnm_glib_ctx *nm_context = NULL;
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
54 guint nm_callback_idx = 0;
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
55
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
56 #elif defined _WIN32
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
57 static int current_network_count;
14696
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
58 #endif
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
59
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
60 struct _GaimNetworkListenData {
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
61 int listenfd;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
62 int socket_type;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
63 gboolean retry;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
64 gboolean adding;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
65 GaimNetworkListenCallback cb;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
66 gpointer cb_data;
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
67 };
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
68
14696
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
69 #ifdef HAVE_LIBNM
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
70 void nm_callback_func(libnm_glib_ctx* ctx, gpointer user_data);
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
71 #endif
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
72
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
73 const unsigned char *
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
74 gaim_network_ip_atoi(const char *ip)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
75 {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
76 static unsigned char ret[4];
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
77 gchar *delimiter = ".";
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
78 gchar **split;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
79 int i;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
80
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
81 g_return_val_if_fail(ip != NULL, NULL);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
82
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
83 split = g_strsplit(ip, delimiter, 4);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
84 for (i = 0; split[i] != NULL; i++)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
85 ret[i] = atoi(split[i]);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
86 g_strfreev(split);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
87
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
88 /* i should always be 4 */
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
89 if (i != 4)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
90 return NULL;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
91
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
92 return ret;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
93 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
94
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
95 void
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
96 gaim_network_set_public_ip(const char *ip)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
97 {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
98 g_return_if_fail(ip != NULL);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
99
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
100 /* XXX - Ensure the IP address is valid */
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
101
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
102 gaim_prefs_set_string("/core/network/public_ip", ip);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
103 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
104
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
105 const char *
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
106 gaim_network_get_public_ip(void)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
107 {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
108 return gaim_prefs_get_string("/core/network/public_ip");
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
109 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
110
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
111 const char *
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
112 gaim_network_get_local_system_ip(int fd)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
113 {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
114 char buffer[1024];
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
115 static char ip[16];
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
116 char *tmp;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
117 struct ifconf ifc;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
118 struct ifreq *ifr;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
119 struct sockaddr_in *sinptr;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
120 guint32 lhost = htonl(127 * 256 * 256 * 256 + 1);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
121 long unsigned int add;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
122 int source = fd;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
123
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
124 if (fd < 0)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
125 source = socket(PF_INET,SOCK_STREAM, 0);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
126
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
127 ifc.ifc_len = sizeof(buffer);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
128 ifc.ifc_req = (struct ifreq *)buffer;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
129 ioctl(source, SIOCGIFCONF, &ifc);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
130
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
131 if (fd < 0)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
132 close(source);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
133
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
134 tmp = buffer;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
135 while (tmp < buffer + ifc.ifc_len)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
136 {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
137 ifr = (struct ifreq *)tmp;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
138 tmp += sizeof(struct ifreq);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
139
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
140 if (ifr->ifr_addr.sa_family == AF_INET)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
141 {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
142 sinptr = (struct sockaddr_in *)&ifr->ifr_addr;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
143 if (sinptr->sin_addr.s_addr != lhost)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
144 {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
145 add = ntohl(sinptr->sin_addr.s_addr);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
146 g_snprintf(ip, 16, "%lu.%lu.%lu.%lu",
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
147 ((add >> 24) & 255),
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
148 ((add >> 16) & 255),
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
149 ((add >> 8) & 255),
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
150 add & 255);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
151
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
152 return ip;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
153 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
154 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
155 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
156
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
157 return "0.0.0.0";
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
158 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
159
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
160 const char *
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
161 gaim_network_get_my_ip(int fd)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
162 {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
163 const char *ip = NULL;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
164 GaimStunNatDiscovery *stun;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
165
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
166 /* Check if the user specified an IP manually */
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
167 if (!gaim_prefs_get_bool("/core/network/auto_ip")) {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
168 ip = gaim_network_get_public_ip();
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
169 if ((ip != NULL) && (*ip != '\0'))
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
170 return ip;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
171 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
172
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
173 /* Check if STUN discovery was already done */
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
174 stun = gaim_stun_discover(NULL);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
175 if ((stun != NULL) && (stun->status == GAIM_STUN_STATUS_DISCOVERED))
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
176 return stun->publicip;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
177
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
178 /* Attempt to get the IP from a NAT device using UPnP */
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
179 ip = gaim_upnp_get_public_ip();
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
180 if (ip != NULL)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
181 return ip;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
182
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
183 /* Just fetch the IP of the local system */
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
184 return gaim_network_get_local_system_ip(fd);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
185 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
186
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
187
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
188 static void
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
189 gaim_network_set_upnp_port_mapping_cb(gboolean success, gpointer data)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
190 {
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
191 GaimNetworkListenData *listen_data;
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
192
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
193 listen_data = data;
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
194 /* TODO: Once we're keeping track of upnp requests... */
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
195 /* listen_data->pnp_data = NULL; */
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
196
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
197 if (!success) {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
198 gaim_debug_info("network", "Couldn't create UPnP mapping\n");
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
199 if (listen_data->retry) {
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
200 listen_data->retry = FALSE;
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
201 listen_data->adding = FALSE;
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
202 /* TODO: Need to keep track of this return value! */
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
203 gaim_upnp_remove_port_mapping(
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
204 gaim_network_get_port_from_fd(listen_data->listenfd),
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
205 (listen_data->socket_type == SOCK_STREAM) ? "TCP" : "UDP",
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
206 gaim_network_set_upnp_port_mapping_cb, listen_data);
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
207 return;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
208 }
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
209 } else if (!listen_data->adding) {
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
210 /* We've tried successfully to remove the port mapping.
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
211 * Try to add it again */
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
212 listen_data->adding = TRUE;
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
213 /* TODO: Need to keep track of this return value! */
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
214 gaim_upnp_set_port_mapping(
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
215 gaim_network_get_port_from_fd(listen_data->listenfd),
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
216 (listen_data->socket_type == SOCK_STREAM) ? "TCP" : "UDP",
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
217 gaim_network_set_upnp_port_mapping_cb, listen_data);
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
218 return;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
219 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
220
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
221 if (listen_data->cb)
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
222 listen_data->cb(listen_data->listenfd, listen_data->cb_data);
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
223
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
224 gaim_network_listen_cancel(listen_data);
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
225 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
226
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
227
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
228 static GaimNetworkListenData *
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
229 gaim_network_do_listen(unsigned short port, int socket_type, GaimNetworkListenCallback cb, gpointer cb_data)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
230 {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
231 int listenfd = -1;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
232 const int on = 1;
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
233 GaimNetworkListenData *listen_data;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
234 #ifdef HAVE_GETADDRINFO
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
235 int errnum;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
236 struct addrinfo hints, *res, *next;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
237 char serv[6];
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
238
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
239 /*
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
240 * Get a list of addresses on this machine.
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
241 */
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
242 snprintf(serv, sizeof(serv), "%hu", port);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
243 memset(&hints, 0, sizeof(struct addrinfo));
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
244 hints.ai_flags = AI_PASSIVE;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
245 hints.ai_family = AF_UNSPEC;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
246 hints.ai_socktype = socket_type;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
247 errnum = getaddrinfo(NULL /* any IP */, serv, &hints, &res);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
248 if (errnum != 0) {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
249 #ifndef _WIN32
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
250 gaim_debug_warning("network", "getaddrinfo: %s\n", gai_strerror(errnum));
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
251 if (errnum == EAI_SYSTEM)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
252 gaim_debug_warning("network", "getaddrinfo: system error: %s\n", strerror(errno));
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
253 #else
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
254 gaim_debug_warning("network", "getaddrinfo: Error Code = %d\n", errnum);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
255 #endif
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
256 return NULL;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
257 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
258
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
259 /*
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
260 * Go through the list of addresses and attempt to listen on
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
261 * one of them.
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
262 * XXX - Try IPv6 addresses first?
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
263 */
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
264 for (next = res; next != NULL; next = next->ai_next) {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
265 listenfd = socket(next->ai_family, next->ai_socktype, next->ai_protocol);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
266 if (listenfd < 0)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
267 continue;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
268 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
269 gaim_debug_warning("network", "setsockopt: %s\n", strerror(errno));
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
270 if (bind(listenfd, next->ai_addr, next->ai_addrlen) == 0)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
271 break; /* success */
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
272 /* XXX - It is unclear to me (datallah) whether we need to be
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
273 using a new socket each time */
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
274 close(listenfd);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
275 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
276
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
277 freeaddrinfo(res);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
278
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
279 if (next == NULL)
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
280 return NULL;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
281 #else
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
282 struct sockaddr_in sockin;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
283
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
284 if ((listenfd = socket(AF_INET, socket_type, 0)) < 0) {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
285 gaim_debug_warning("network", "socket: %s\n", strerror(errno));
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
286 return NULL;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
287 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
288
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
289 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
290 gaim_debug_warning("network", "setsockopt: %s\n", strerror(errno));
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
291
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
292 memset(&sockin, 0, sizeof(struct sockaddr_in));
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
293 sockin.sin_family = PF_INET;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
294 sockin.sin_port = htons(port);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
295
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
296 if (bind(listenfd, (struct sockaddr *)&sockin, sizeof(struct sockaddr_in)) != 0) {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
297 gaim_debug_warning("network", "bind: %s\n", strerror(errno));
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
298 close(listenfd);
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
299 return NULL;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
300 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
301 #endif
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
302
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
303 if (socket_type == SOCK_STREAM && listen(listenfd, 4) != 0) {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
304 gaim_debug_warning("network", "listen: %s\n", strerror(errno));
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
305 close(listenfd);
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
306 return NULL;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
307 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
308 fcntl(listenfd, F_SETFL, O_NONBLOCK);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
309
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
310 gaim_debug_info("network", "Listening on port: %hu\n", gaim_network_get_port_from_fd(listenfd));
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
311
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
312 listen_data = g_new0(GaimNetworkListenData, 1);
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
313 listen_data->listenfd = listenfd;
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
314 listen_data->adding = TRUE;
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
315 listen_data->retry = TRUE;
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
316 listen_data->cb = cb;
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
317 listen_data->cb_data = cb_data;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
318
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
319 /* TODO: Need to keep track of this return value! */
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
320 gaim_upnp_set_port_mapping(
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
321 gaim_network_get_port_from_fd(listenfd),
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
322 (socket_type == SOCK_STREAM) ? "TCP" : "UDP",
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
323 gaim_network_set_upnp_port_mapping_cb, listen_data);
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
324
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
325 return listen_data;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
326 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
327
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
328 GaimNetworkListenData *
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
329 gaim_network_listen(unsigned short port, int socket_type,
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
330 GaimNetworkListenCallback cb, gpointer cb_data)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
331 {
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
332 g_return_val_if_fail(port != 0, NULL);
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
333
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
334 return gaim_network_do_listen(port, socket_type, cb, cb_data);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
335 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
336
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
337 GaimNetworkListenData *
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
338 gaim_network_listen_range(unsigned short start, unsigned short end,
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
339 int socket_type, GaimNetworkListenCallback cb, gpointer cb_data)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
340 {
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
341 GaimNetworkListenData *ret = NULL;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
342
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
343 if (gaim_prefs_get_bool("/core/network/ports_range_use")) {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
344 start = gaim_prefs_get_int("/core/network/ports_range_start");
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
345 end = gaim_prefs_get_int("/core/network/ports_range_end");
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
346 } else {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
347 if (end < start)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
348 end = start;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
349 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
350
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
351 for (; start <= end; start++) {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
352 ret = gaim_network_do_listen(start, socket_type, cb, cb_data);
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
353 if (ret != NULL)
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
354 break;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
355 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
356
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
357 return ret;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
358 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
359
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
360 void gaim_network_listen_cancel(GaimNetworkListenData *listen_data)
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
361 {
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
362 g_free(listen_data);
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
363 }
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
364
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
365 unsigned short
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
366 gaim_network_get_port_from_fd(int fd)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
367 {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
368 struct sockaddr_in addr;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
369 socklen_t len;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
370
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
371 g_return_val_if_fail(fd >= 0, 0);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
372
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
373 len = sizeof(addr);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
374 if (getsockname(fd, (struct sockaddr *) &addr, &len) == -1) {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
375 gaim_debug_warning("network", "getsockname: %s\n", strerror(errno));
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
376 return 0;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
377 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
378
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
379 return ntohs(addr.sin_port);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
380 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
381
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
382 #ifdef _WIN32
14791
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
383 #ifndef NS_NLA
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
384 #define NS_NLA 15
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
385 #endif
14780
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
386 static gint
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
387 wgaim_get_connected_network_count(void)
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
388 {
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
389 guint net_cnt = 0;
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
390
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
391 WSAQUERYSET qs;
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
392 HANDLE h;
14895
717ea4589f8d [gaim-migrate @ 17667]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14815
diff changeset
393 gint retval;
717ea4589f8d [gaim-migrate @ 17667]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14815
diff changeset
394 int errorid;
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
395
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
396 memset(&qs, 0, sizeof(WSAQUERYSET));
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
397 qs.dwSize = sizeof(WSAQUERYSET);
14791
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
398 qs.dwNameSpace = NS_NLA;
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
399
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
400 retval = WSALookupServiceBegin(&qs, LUP_RETURN_ALL, &h);
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
401 if (retval != ERROR_SUCCESS) {
14895
717ea4589f8d [gaim-migrate @ 17667]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14815
diff changeset
402 gchar *msg;
717ea4589f8d [gaim-migrate @ 17667]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14815
diff changeset
403 errorid = WSAGetLastError();
717ea4589f8d [gaim-migrate @ 17667]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14815
diff changeset
404 msg = g_win32_error_message(errorid);
14791
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
405 gaim_debug_warning("network", "Couldn't retrieve NLA SP lookup handle. "
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
406 "NLA service is probably not running. Message: %s (%d).\n",
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
407 msg, errorid);
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
408 g_free(msg);
14776
a87c53e7b1e2 [gaim-migrate @ 17540]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14722
diff changeset
409
14780
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
410 return -1;
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
411 } else {
14897
e429c4ebbae5 [gaim-migrate @ 17669]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14895
diff changeset
412 char buf[4096];
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
413 WSAQUERYSET *res = (LPWSAQUERYSET) buf;
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
414 DWORD size = sizeof(buf);
14895
717ea4589f8d [gaim-migrate @ 17667]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14815
diff changeset
415 while ((retval = WSALookupServiceNext(h, 0, &size, res)) == ERROR_SUCCESS) {
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
416 net_cnt++;
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
417 gaim_debug_info("network", "found network '%s'\n",
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
418 res->lpszServiceInstanceName ? res->lpszServiceInstanceName : "(NULL)");
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
419 size = sizeof(buf);
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
420 }
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
421
14895
717ea4589f8d [gaim-migrate @ 17667]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14815
diff changeset
422 errorid = WSAGetLastError();
717ea4589f8d [gaim-migrate @ 17667]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14815
diff changeset
423 if (!(errorid == WSA_E_NO_MORE || errorid == WSAENOMORE)) {
717ea4589f8d [gaim-migrate @ 17667]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14815
diff changeset
424 gchar *msg = g_win32_error_message(errorid);
717ea4589f8d [gaim-migrate @ 17667]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14815
diff changeset
425 gaim_debug_error("network", "got unexpected NLA response %s (%d)\n", msg, errorid);
717ea4589f8d [gaim-migrate @ 17667]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14815
diff changeset
426 g_free(msg);
717ea4589f8d [gaim-migrate @ 17667]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14815
diff changeset
427
717ea4589f8d [gaim-migrate @ 17667]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14815
diff changeset
428 net_cnt = -1;
717ea4589f8d [gaim-migrate @ 17667]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14815
diff changeset
429 }
717ea4589f8d [gaim-migrate @ 17667]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14815
diff changeset
430
717ea4589f8d [gaim-migrate @ 17667]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14815
diff changeset
431 retval = WSALookupServiceEnd(h);
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
432 }
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
433
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
434 return net_cnt;
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
435
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
436 }
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
437
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
438 static gboolean wgaim_network_change_thread_cb(gpointer data)
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
439 {
14780
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
440 gint new_count;
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
441 GaimConnectionUiOps *ui_ops = gaim_connections_get_ui_ops();
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
442
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
443 new_count = wgaim_get_connected_network_count();
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
444
14780
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
445 if (new_count < 0)
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
446 return FALSE;
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
447
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
448 gaim_debug_info("network", "Received Network Change Notification. Current network count is %d, previous count was %d.\n", new_count, current_network_count);
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
449
14815
49450a1bc58e [gaim-migrate @ 17581]
Stu Tomlinson <stu@nosnilmot.com>
parents: 14802
diff changeset
450 if (new_count > 0 && ui_ops != NULL && ui_ops->network_connected != NULL) {
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
451 ui_ops->network_connected();
14815
49450a1bc58e [gaim-migrate @ 17581]
Stu Tomlinson <stu@nosnilmot.com>
parents: 14802
diff changeset
452 } else if (new_count == 0 && current_network_count > 0 &&
49450a1bc58e [gaim-migrate @ 17581]
Stu Tomlinson <stu@nosnilmot.com>
parents: 14802
diff changeset
453 ui_ops != NULL && ui_ops->network_disconnected != NULL) {
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
454 ui_ops->network_disconnected();
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
455 }
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
456
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
457 current_network_count = new_count;
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
458
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
459 return FALSE;
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
460 }
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
461
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
462 static gpointer wgaim_network_change_thread(gpointer data)
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
463 {
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
464 HANDLE h;
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
465 WSAQUERYSET qs;
14780
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
466 time_t last_trigger = time(NULL);
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
467
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
468 int WSAAPI (*MyWSANSPIoctl) (
14791
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
469 HANDLE hLookup, DWORD dwControlCode, LPVOID lpvInBuffer,
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
470 DWORD cbInBuffer, LPVOID lpvOutBuffer, DWORD cbOutBuffer,
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
471 LPDWORD lpcbBytesReturned, LPWSACOMPLETION lpCompletion) = NULL;
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
472
14791
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
473 if (!(MyWSANSPIoctl = (void*) wgaim_find_and_loadproc("ws2_32.dll", "WSANSPIoctl"))) {
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
474 g_thread_exit(NULL);
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
475 return NULL;
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
476 }
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
477
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
478 while (TRUE) {
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
479 int retval;
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
480 DWORD retLen = 0;
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
481
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
482 memset(&qs, 0, sizeof(WSAQUERYSET));
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
483 qs.dwSize = sizeof(WSAQUERYSET);
14791
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
484 qs.dwNameSpace = NS_NLA;
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
485 if (WSALookupServiceBegin(&qs, 0, &h) == SOCKET_ERROR) {
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
486 int errorid = WSAGetLastError();
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
487 gchar *msg = g_win32_error_message(errorid);
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
488 gaim_debug_warning("network", "Couldn't retrieve NLA SP lookup handle. "
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
489 "NLA service is probably not running. Message: %s (%d).\n",
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
490 msg, errorid);
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
491 g_free(msg);
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
492 g_thread_exit(NULL);
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
493 return NULL;
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
494 }
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
495
14780
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
496 /* Make sure at least 30 seconds have elapsed since the last
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
497 * notification so we don't peg the cpu if this keeps changing. */
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
498 if ((time(NULL) - last_trigger) < 30)
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
499 Sleep(30000);
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
500
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
501 last_trigger = time(NULL);
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
502
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
503 /* This will block until there is a network change */
14791
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
504 if (MyWSANSPIoctl(h, SIO_NSP_NOTIFY_CHANGE, NULL, 0, NULL, 0, &retLen, NULL) == SOCKET_ERROR) {
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
505 int errorid = WSAGetLastError();
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
506 gchar *msg = g_win32_error_message(errorid);
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
507 gaim_debug_warning("network", "Unable to wait for changes. Message: %s (%d).\n",
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
508 msg, errorid);
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
509 g_free(msg);
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
510 }
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
511
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
512 retval = WSALookupServiceEnd(h);
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
513
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
514 g_idle_add(wgaim_network_change_thread_cb, NULL);
14780
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
515
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
516 }
14791
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
517
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
518 g_thread_exit(NULL);
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
519 return NULL;
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
520 }
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
521 #endif
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
522
14696
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
523 gboolean
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
524 gaim_network_is_available(void)
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
525 {
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
526 #ifdef HAVE_LIBNM
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
527 /* Try NetworkManager first, maybe we'll get lucky */
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
528 int libnm_retval = -1;
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
529
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
530 if (nm_context)
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
531 {
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
532 if ((libnm_retval = libnm_glib_get_network_state(nm_context)) == LIBNM_NO_NETWORK_CONNECTION)
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
533 {
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
534 gaim_debug_warning("network", "NetworkManager not active or reports no connection (retval = %i)\n", libnm_retval);
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
535 return FALSE;
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
536 }
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
537 if (libnm_retval == LIBNM_ACTIVE_NETWORK_CONNECTION) return TRUE;
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
538 }
14777
182f77347680 [gaim-migrate @ 17541]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14776
diff changeset
539 #elif defined _WIN32
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
540 return (current_network_count > 0);
14696
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
541 #endif
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
542 return TRUE;
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
543 }
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
544
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
545 #ifdef HAVE_LIBNM
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
546 void
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
547 nm_callback_func(libnm_glib_ctx* ctx, gpointer user_data)
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
548 {
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
549 GList *l;
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
550 GaimAccount *account;
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
551 static libnm_glib_state prev = LIBNM_NO_DBUS;
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
552 libnm_glib_state current;
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
553 GaimConnectionUiOps *ui_ops = gaim_connections_get_ui_ops();
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
554
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
555 current = libnm_glib_get_network_state(ctx);
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
556 gaim_debug_info("network","Entering nm_callback_func!\n");
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
557
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
558 switch(current)
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
559 {
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
560 case LIBNM_ACTIVE_NETWORK_CONNECTION:
14800
c678034a7e3e [gaim-migrate @ 17565]
Sean Egan <seanegan@gmail.com>
parents: 14791
diff changeset
561 /* Call res_init in case DNS servers have changed */
c678034a7e3e [gaim-migrate @ 17565]
Sean Egan <seanegan@gmail.com>
parents: 14791
diff changeset
562 res_init();
14815
49450a1bc58e [gaim-migrate @ 17581]
Stu Tomlinson <stu@nosnilmot.com>
parents: 14802
diff changeset
563 if (ui_ops != NULL && ui_ops->network_connected != NULL)
49450a1bc58e [gaim-migrate @ 17581]
Stu Tomlinson <stu@nosnilmot.com>
parents: 14802
diff changeset
564 ui_ops->network_connected();
14696
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
565 prev = current;
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
566 break;
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
567 case LIBNM_NO_NETWORK_CONNECTION:
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
568 if (prev != LIBNM_ACTIVE_NETWORK_CONNECTION)
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
569 break;
14815
49450a1bc58e [gaim-migrate @ 17581]
Stu Tomlinson <stu@nosnilmot.com>
parents: 14802
diff changeset
570 if (ui_ops != NULL && ui_ops->network_disconnected != NULL)
49450a1bc58e [gaim-migrate @ 17581]
Stu Tomlinson <stu@nosnilmot.com>
parents: 14802
diff changeset
571 ui_ops->network_disconnected();
14696
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
572 prev = current;
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
573 break;
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
574 case LIBNM_NO_DBUS:
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
575 case LIBNM_NO_NETWORKMANAGER:
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
576 case LIBNM_INVALID_CONTEXT:
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
577 default:
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
578 break;
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
579 }
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
580 }
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
581 #endif
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
582
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
583 void
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
584 gaim_network_init(void)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
585 {
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
586 #ifdef _WIN32
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
587 GError *err = NULL;
14780
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
588 gint cnt = wgaim_get_connected_network_count();
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
589
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
590 if (cnt < 0) /* Assume there is a network */
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
591 current_network_count = 1;
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
592 /* Don't listen for network changes if we can't tell anyway */
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
593 else
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
594 {
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
595 current_network_count = cnt;
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
596 if (!g_thread_create(wgaim_network_change_thread, NULL, FALSE, &err))
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
597 gaim_debug_error("network", "Couldn't create Network Monitor thread: %s\n", err ? err->message : "");
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
598 }
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
599 #endif
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
600
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
601 gaim_prefs_add_none ("/core/network");
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
602 gaim_prefs_add_bool ("/core/network/auto_ip", TRUE);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
603 gaim_prefs_add_string("/core/network/public_ip", "");
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
604 gaim_prefs_add_bool ("/core/network/ports_range_use", FALSE);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
605 gaim_prefs_add_int ("/core/network/ports_range_start", 1024);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
606 gaim_prefs_add_int ("/core/network/ports_range_end", 2048);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
607
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
608 gaim_upnp_discover(NULL, NULL);
14696
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
609
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
610 #ifdef HAVE_LIBNM
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
611 nm_context = libnm_glib_init();
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
612 if(nm_context)
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
613 nm_callback_idx = libnm_glib_register_callback(nm_context, nm_callback_func, NULL, g_main_context_default());
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
614 #endif
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
615 }
14696
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
616
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
617 void
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
618 gaim_network_uninit(void)
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
619 {
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
620 #ifdef HAVE_LIBNM
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
621 /* FIXME: If anyone can think of a more clever way to shut down libnm without
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
622 * using a global variable + this function, please do. */
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
623 if(nm_context && nm_callback_idx)
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
624 libnm_glib_unregister_callback(nm_context, nm_callback_idx);
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
625
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
626 if(nm_context)
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
627 libnm_glib_shutdown(nm_context);
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
628 #endif
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
629 }