annotate libgaim/network.c @ 15358:7c41ecf41614

[gaim-migrate @ 18151] Revert GtkIMHtml hacks that tried to fix gtkimhtml bugs, replace it with heuristic workaround. If you receive a message with more than 100 HTML tags strip all formatting before displaying it. Hopefully nobody has any legitimate uses for 100 HTML tags in a message. I guess we'll see in beta6... which I think we're good for now. I think that post-2.0.0 we'll wind up replacing GtkIMHtml with a legitimate HTML widget and do the Adium/Kopete "chat theme" thing. An HTML widget should be able to handle formatting more efficiently than GtkTextView. committer: Tailor Script <tailor@pidgin.im>
author Sean Egan <seanegan@gmail.com>
date Thu, 18 Jan 2007 02:35:14 +0000
parents ae58cf5a441f
children
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
15216
ae58cf5a441f [gaim-migrate @ 18006]
Mark Doliner <mark@kingant.net>
parents: 15054
diff changeset
50 /*
ae58cf5a441f [gaim-migrate @ 18006]
Mark Doliner <mark@kingant.net>
parents: 15054
diff changeset
51 * Calling sizeof(struct ifreq) isn't always correct on
ae58cf5a441f [gaim-migrate @ 18006]
Mark Doliner <mark@kingant.net>
parents: 15054
diff changeset
52 * Mac OS X (and maybe others).
ae58cf5a441f [gaim-migrate @ 18006]
Mark Doliner <mark@kingant.net>
parents: 15054
diff changeset
53 */
ae58cf5a441f [gaim-migrate @ 18006]
Mark Doliner <mark@kingant.net>
parents: 15054
diff changeset
54 #ifdef _SIZEOF_ADDR_IFREQ
ae58cf5a441f [gaim-migrate @ 18006]
Mark Doliner <mark@kingant.net>
parents: 15054
diff changeset
55 # define HX_SIZE_OF_IFREQ(a) _SIZEOF_ADDR_IFREQ(a)
ae58cf5a441f [gaim-migrate @ 18006]
Mark Doliner <mark@kingant.net>
parents: 15054
diff changeset
56 #else
ae58cf5a441f [gaim-migrate @ 18006]
Mark Doliner <mark@kingant.net>
parents: 15054
diff changeset
57 # define HX_SIZE_OF_IFREQ(a) sizeof(a)
ae58cf5a441f [gaim-migrate @ 18006]
Mark Doliner <mark@kingant.net>
parents: 15054
diff changeset
58 #endif
ae58cf5a441f [gaim-migrate @ 18006]
Mark Doliner <mark@kingant.net>
parents: 15054
diff changeset
59
14696
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
60 #ifdef HAVE_LIBNM
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
61 #include <libnm_glib.h>
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
62
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
63 libnm_glib_ctx *nm_context = NULL;
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
64 guint nm_callback_idx = 0;
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
65
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
66 #elif defined _WIN32
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
67 static int current_network_count;
14696
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
68 #endif
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
69
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
70 struct _GaimNetworkListenData {
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
71 int listenfd;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
72 int socket_type;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
73 gboolean retry;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
74 gboolean adding;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
75 GaimNetworkListenCallback cb;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
76 gpointer cb_data;
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
77 };
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
78
14696
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
79 #ifdef HAVE_LIBNM
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
80 void nm_callback_func(libnm_glib_ctx* ctx, gpointer user_data);
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
81 #endif
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
82
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
83 const unsigned char *
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
84 gaim_network_ip_atoi(const char *ip)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
85 {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
86 static unsigned char ret[4];
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
87 gchar *delimiter = ".";
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
88 gchar **split;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
89 int i;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
90
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
91 g_return_val_if_fail(ip != NULL, NULL);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
92
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
93 split = g_strsplit(ip, delimiter, 4);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
94 for (i = 0; split[i] != NULL; i++)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
95 ret[i] = atoi(split[i]);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
96 g_strfreev(split);
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 /* i should always be 4 */
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
99 if (i != 4)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
100 return NULL;
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 return ret;
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 void
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
106 gaim_network_set_public_ip(const char *ip)
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 g_return_if_fail(ip != NULL);
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 /* XXX - Ensure the IP address is valid */
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
111
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
112 gaim_prefs_set_string("/core/network/public_ip", ip);
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
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
115 const char *
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
116 gaim_network_get_public_ip(void)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
117 {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
118 return gaim_prefs_get_string("/core/network/public_ip");
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
119 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
120
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
121 const char *
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
122 gaim_network_get_local_system_ip(int 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 char buffer[1024];
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
125 static char ip[16];
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
126 char *tmp;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
127 struct ifconf ifc;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
128 struct ifreq *ifr;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
129 struct sockaddr_in *sinptr;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
130 guint32 lhost = htonl(127 * 256 * 256 * 256 + 1);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
131 long unsigned int add;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
132 int source = fd;
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 if (fd < 0)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
135 source = socket(PF_INET,SOCK_STREAM, 0);
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 ifc.ifc_len = sizeof(buffer);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
138 ifc.ifc_req = (struct ifreq *)buffer;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
139 ioctl(source, SIOCGIFCONF, &ifc);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
140
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
141 if (fd < 0)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
142 close(source);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
143
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
144 tmp = buffer;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
145 while (tmp < buffer + ifc.ifc_len)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
146 {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
147 ifr = (struct ifreq *)tmp;
15216
ae58cf5a441f [gaim-migrate @ 18006]
Mark Doliner <mark@kingant.net>
parents: 15054
diff changeset
148 tmp += HX_SIZE_OF_IFREQ(*ifr);
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
149
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
150 if (ifr->ifr_addr.sa_family == AF_INET)
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 sinptr = (struct sockaddr_in *)&ifr->ifr_addr;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
153 if (sinptr->sin_addr.s_addr != lhost)
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 add = ntohl(sinptr->sin_addr.s_addr);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
156 g_snprintf(ip, 16, "%lu.%lu.%lu.%lu",
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
157 ((add >> 24) & 255),
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
158 ((add >> 16) & 255),
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
159 ((add >> 8) & 255),
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
160 add & 255);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
161
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
162 return ip;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
163 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
164 }
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
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
167 return "0.0.0.0";
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
168 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
169
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
170 const char *
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
171 gaim_network_get_my_ip(int fd)
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 const char *ip = NULL;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
174 GaimStunNatDiscovery *stun;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
175
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
176 /* Check if the user specified an IP manually */
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
177 if (!gaim_prefs_get_bool("/core/network/auto_ip")) {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
178 ip = gaim_network_get_public_ip();
15054
f58283ce58ea [gaim-migrate @ 17838]
Mark Doliner <mark@kingant.net>
parents: 14897
diff changeset
179 /* Make sure the IP address entered by the user is valid */
f58283ce58ea [gaim-migrate @ 17838]
Mark Doliner <mark@kingant.net>
parents: 14897
diff changeset
180 if ((ip != NULL) && (gaim_network_ip_atoi(ip) != NULL))
14192
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
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
184 /* Check if STUN discovery was already done */
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
185 stun = gaim_stun_discover(NULL);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
186 if ((stun != NULL) && (stun->status == GAIM_STUN_STATUS_DISCOVERED))
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
187 return stun->publicip;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
188
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
189 /* Attempt to get the IP from a NAT device using UPnP */
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
190 ip = gaim_upnp_get_public_ip();
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
191 if (ip != NULL)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
192 return ip;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
193
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
194 /* Just fetch the IP of the local system */
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
195 return gaim_network_get_local_system_ip(fd);
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
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
198
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
199 static void
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
200 gaim_network_set_upnp_port_mapping_cb(gboolean success, gpointer data)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
201 {
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
202 GaimNetworkListenData *listen_data;
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
203
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
204 listen_data = data;
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
205 /* TODO: Once we're keeping track of upnp requests... */
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
206 /* listen_data->pnp_data = NULL; */
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
207
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
208 if (!success) {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
209 gaim_debug_info("network", "Couldn't create UPnP mapping\n");
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
210 if (listen_data->retry) {
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
211 listen_data->retry = FALSE;
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
212 listen_data->adding = FALSE;
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_remove_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 }
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
220 } else if (!listen_data->adding) {
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
221 /* We've tried successfully to remove the port mapping.
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
222 * Try to add it again */
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
223 listen_data->adding = TRUE;
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
224 /* TODO: Need to keep track of this return value! */
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
225 gaim_upnp_set_port_mapping(
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
226 gaim_network_get_port_from_fd(listen_data->listenfd),
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
227 (listen_data->socket_type == SOCK_STREAM) ? "TCP" : "UDP",
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
228 gaim_network_set_upnp_port_mapping_cb, listen_data);
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
229 return;
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
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
232 if (listen_data->cb)
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
233 listen_data->cb(listen_data->listenfd, listen_data->cb_data);
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
234
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
235 gaim_network_listen_cancel(listen_data);
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
236 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
237
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
238
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
239 static GaimNetworkListenData *
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
240 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
241 {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
242 int listenfd = -1;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
243 const int on = 1;
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
244 GaimNetworkListenData *listen_data;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
245 #ifdef HAVE_GETADDRINFO
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
246 int errnum;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
247 struct addrinfo hints, *res, *next;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
248 char serv[6];
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
249
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
250 /*
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
251 * Get a list of addresses on this machine.
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
252 */
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
253 snprintf(serv, sizeof(serv), "%hu", port);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
254 memset(&hints, 0, sizeof(struct addrinfo));
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
255 hints.ai_flags = AI_PASSIVE;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
256 hints.ai_family = AF_UNSPEC;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
257 hints.ai_socktype = socket_type;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
258 errnum = getaddrinfo(NULL /* any IP */, serv, &hints, &res);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
259 if (errnum != 0) {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
260 #ifndef _WIN32
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
261 gaim_debug_warning("network", "getaddrinfo: %s\n", gai_strerror(errnum));
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
262 if (errnum == EAI_SYSTEM)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
263 gaim_debug_warning("network", "getaddrinfo: system error: %s\n", strerror(errno));
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
264 #else
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
265 gaim_debug_warning("network", "getaddrinfo: Error Code = %d\n", errnum);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
266 #endif
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
267 return NULL;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
268 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
269
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
270 /*
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
271 * Go through the list of addresses and attempt to listen on
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
272 * one of them.
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
273 * XXX - Try IPv6 addresses first?
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
274 */
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
275 for (next = res; next != NULL; next = next->ai_next) {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
276 listenfd = socket(next->ai_family, next->ai_socktype, next->ai_protocol);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
277 if (listenfd < 0)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
278 continue;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
279 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
280 gaim_debug_warning("network", "setsockopt: %s\n", strerror(errno));
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
281 if (bind(listenfd, next->ai_addr, next->ai_addrlen) == 0)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
282 break; /* success */
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
283 /* 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
284 using a new socket each time */
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
285 close(listenfd);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
286 }
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 freeaddrinfo(res);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
289
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
290 if (next == NULL)
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
291 return NULL;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
292 #else
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
293 struct sockaddr_in sockin;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
294
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
295 if ((listenfd = socket(AF_INET, socket_type, 0)) < 0) {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
296 gaim_debug_warning("network", "socket: %s\n", strerror(errno));
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
297 return NULL;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
298 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
299
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
300 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
301 gaim_debug_warning("network", "setsockopt: %s\n", strerror(errno));
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 memset(&sockin, 0, sizeof(struct sockaddr_in));
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
304 sockin.sin_family = PF_INET;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
305 sockin.sin_port = htons(port);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
306
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
307 if (bind(listenfd, (struct sockaddr *)&sockin, sizeof(struct sockaddr_in)) != 0) {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
308 gaim_debug_warning("network", "bind: %s\n", strerror(errno));
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
309 close(listenfd);
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
310 return NULL;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
311 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
312 #endif
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
313
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
314 if (socket_type == SOCK_STREAM && listen(listenfd, 4) != 0) {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
315 gaim_debug_warning("network", "listen: %s\n", strerror(errno));
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
316 close(listenfd);
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
317 return NULL;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
318 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
319 fcntl(listenfd, F_SETFL, O_NONBLOCK);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
320
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
321 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
322
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
323 listen_data = g_new0(GaimNetworkListenData, 1);
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
324 listen_data->listenfd = listenfd;
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
325 listen_data->adding = TRUE;
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
326 listen_data->retry = TRUE;
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
327 listen_data->cb = cb;
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
328 listen_data->cb_data = cb_data;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
329
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
330 /* TODO: Need to keep track of this return value! */
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
331 gaim_upnp_set_port_mapping(
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
332 gaim_network_get_port_from_fd(listenfd),
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
333 (socket_type == SOCK_STREAM) ? "TCP" : "UDP",
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
334 gaim_network_set_upnp_port_mapping_cb, listen_data);
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
335
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
336 return listen_data;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
337 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
338
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
339 GaimNetworkListenData *
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
340 gaim_network_listen(unsigned short port, int socket_type,
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
341 GaimNetworkListenCallback cb, gpointer cb_data)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
342 {
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
343 g_return_val_if_fail(port != 0, NULL);
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
344
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
345 return gaim_network_do_listen(port, socket_type, cb, cb_data);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
346 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
347
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
348 GaimNetworkListenData *
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
349 gaim_network_listen_range(unsigned short start, unsigned short end,
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
350 int socket_type, GaimNetworkListenCallback cb, gpointer cb_data)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
351 {
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
352 GaimNetworkListenData *ret = NULL;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
353
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
354 if (gaim_prefs_get_bool("/core/network/ports_range_use")) {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
355 start = gaim_prefs_get_int("/core/network/ports_range_start");
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
356 end = gaim_prefs_get_int("/core/network/ports_range_end");
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
357 } else {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
358 if (end < start)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
359 end = start;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
360 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
361
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
362 for (; start <= end; start++) {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
363 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
364 if (ret != NULL)
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
365 break;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
366 }
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 return ret;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
369 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
370
14267
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
371 void gaim_network_listen_cancel(GaimNetworkListenData *listen_data)
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
372 {
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
373 g_free(listen_data);
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
374 }
645598a4ec04 [gaim-migrate @ 16949]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
375
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
376 unsigned short
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
377 gaim_network_get_port_from_fd(int fd)
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 struct sockaddr_in addr;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
380 socklen_t len;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
381
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
382 g_return_val_if_fail(fd >= 0, 0);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
383
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
384 len = sizeof(addr);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
385 if (getsockname(fd, (struct sockaddr *) &addr, &len) == -1) {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
386 gaim_debug_warning("network", "getsockname: %s\n", strerror(errno));
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
387 return 0;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
388 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
389
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
390 return ntohs(addr.sin_port);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
391 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
392
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
393 #ifdef _WIN32
14791
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
394 #ifndef NS_NLA
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
395 #define NS_NLA 15
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
396 #endif
14780
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
397 static gint
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
398 wgaim_get_connected_network_count(void)
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 guint net_cnt = 0;
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
401
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
402 WSAQUERYSET qs;
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
403 HANDLE h;
14895
717ea4589f8d [gaim-migrate @ 17667]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14815
diff changeset
404 gint retval;
717ea4589f8d [gaim-migrate @ 17667]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14815
diff changeset
405 int errorid;
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
406
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
407 memset(&qs, 0, sizeof(WSAQUERYSET));
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
408 qs.dwSize = sizeof(WSAQUERYSET);
14791
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
409 qs.dwNameSpace = NS_NLA;
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
410
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
411 retval = WSALookupServiceBegin(&qs, LUP_RETURN_ALL, &h);
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
412 if (retval != ERROR_SUCCESS) {
14895
717ea4589f8d [gaim-migrate @ 17667]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14815
diff changeset
413 gchar *msg;
717ea4589f8d [gaim-migrate @ 17667]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14815
diff changeset
414 errorid = WSAGetLastError();
717ea4589f8d [gaim-migrate @ 17667]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14815
diff changeset
415 msg = g_win32_error_message(errorid);
14791
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
416 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
417 "NLA service is probably not running. Message: %s (%d).\n",
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
418 msg, errorid);
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
419 g_free(msg);
14776
a87c53e7b1e2 [gaim-migrate @ 17540]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14722
diff changeset
420
14780
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
421 return -1;
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
422 } else {
14897
e429c4ebbae5 [gaim-migrate @ 17669]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14895
diff changeset
423 char buf[4096];
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
424 WSAQUERYSET *res = (LPWSAQUERYSET) buf;
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
425 DWORD size = sizeof(buf);
14895
717ea4589f8d [gaim-migrate @ 17667]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14815
diff changeset
426 while ((retval = WSALookupServiceNext(h, 0, &size, res)) == ERROR_SUCCESS) {
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
427 net_cnt++;
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
428 gaim_debug_info("network", "found network '%s'\n",
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
429 res->lpszServiceInstanceName ? res->lpszServiceInstanceName : "(NULL)");
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
430 size = sizeof(buf);
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
431 }
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
432
14895
717ea4589f8d [gaim-migrate @ 17667]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14815
diff changeset
433 errorid = WSAGetLastError();
717ea4589f8d [gaim-migrate @ 17667]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14815
diff changeset
434 if (!(errorid == WSA_E_NO_MORE || errorid == WSAENOMORE)) {
717ea4589f8d [gaim-migrate @ 17667]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14815
diff changeset
435 gchar *msg = g_win32_error_message(errorid);
717ea4589f8d [gaim-migrate @ 17667]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14815
diff changeset
436 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
437 g_free(msg);
717ea4589f8d [gaim-migrate @ 17667]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14815
diff changeset
438
717ea4589f8d [gaim-migrate @ 17667]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14815
diff changeset
439 net_cnt = -1;
717ea4589f8d [gaim-migrate @ 17667]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14815
diff changeset
440 }
717ea4589f8d [gaim-migrate @ 17667]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14815
diff changeset
441
717ea4589f8d [gaim-migrate @ 17667]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14815
diff changeset
442 retval = WSALookupServiceEnd(h);
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
443 }
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
444
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
445 return net_cnt;
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
446
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
447 }
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
448
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
449 static gboolean wgaim_network_change_thread_cb(gpointer data)
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
450 {
14780
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
451 gint new_count;
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
452 GaimConnectionUiOps *ui_ops = gaim_connections_get_ui_ops();
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
453
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
454 new_count = wgaim_get_connected_network_count();
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
455
14780
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
456 if (new_count < 0)
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
457 return FALSE;
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
458
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
459 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
460
14815
49450a1bc58e [gaim-migrate @ 17581]
Stu Tomlinson <stu@nosnilmot.com>
parents: 14802
diff changeset
461 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
462 ui_ops->network_connected();
14815
49450a1bc58e [gaim-migrate @ 17581]
Stu Tomlinson <stu@nosnilmot.com>
parents: 14802
diff changeset
463 } else if (new_count == 0 && current_network_count > 0 &&
49450a1bc58e [gaim-migrate @ 17581]
Stu Tomlinson <stu@nosnilmot.com>
parents: 14802
diff changeset
464 ui_ops != NULL && ui_ops->network_disconnected != NULL) {
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
465 ui_ops->network_disconnected();
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
466 }
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 current_network_count = new_count;
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
469
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
470 return FALSE;
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
471 }
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
472
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
473 static gpointer wgaim_network_change_thread(gpointer data)
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
474 {
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
475 HANDLE h;
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
476 WSAQUERYSET qs;
14780
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
477 time_t last_trigger = time(NULL);
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
478
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
479 int WSAAPI (*MyWSANSPIoctl) (
14791
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
480 HANDLE hLookup, DWORD dwControlCode, LPVOID lpvInBuffer,
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
481 DWORD cbInBuffer, LPVOID lpvOutBuffer, DWORD cbOutBuffer,
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
482 LPDWORD lpcbBytesReturned, LPWSACOMPLETION lpCompletion) = NULL;
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
483
14791
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
484 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
485 g_thread_exit(NULL);
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
486 return NULL;
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
487 }
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
488
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
489 while (TRUE) {
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
490 int retval;
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
491 DWORD retLen = 0;
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
492
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
493 memset(&qs, 0, sizeof(WSAQUERYSET));
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
494 qs.dwSize = sizeof(WSAQUERYSET);
14791
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
495 qs.dwNameSpace = NS_NLA;
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
496 if (WSALookupServiceBegin(&qs, 0, &h) == SOCKET_ERROR) {
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
497 int errorid = WSAGetLastError();
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
498 gchar *msg = g_win32_error_message(errorid);
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
499 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
500 "NLA service is probably not running. Message: %s (%d).\n",
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
501 msg, errorid);
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
502 g_free(msg);
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
503 g_thread_exit(NULL);
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
504 return NULL;
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
505 }
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
506
14780
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
507 /* 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
508 * 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
509 if ((time(NULL) - last_trigger) < 30)
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
510 Sleep(30000);
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
511
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
512 last_trigger = time(NULL);
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
513
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
514 /* This will block until there is a network change */
14791
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
515 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
516 int errorid = WSAGetLastError();
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
517 gchar *msg = g_win32_error_message(errorid);
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
518 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
519 msg, errorid);
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
520 g_free(msg);
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
521 }
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
522
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
523 retval = WSALookupServiceEnd(h);
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
524
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
525 g_idle_add(wgaim_network_change_thread_cb, NULL);
14780
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
526
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
527 }
14791
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
528
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
529 g_thread_exit(NULL);
2727e465602f [gaim-migrate @ 17556]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14780
diff changeset
530 return NULL;
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
531 }
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
532 #endif
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
533
14696
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
534 gboolean
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
535 gaim_network_is_available(void)
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 #ifdef HAVE_LIBNM
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
538 /* Try NetworkManager first, maybe we'll get lucky */
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
539 int libnm_retval = -1;
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
540
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
541 if (nm_context)
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
542 {
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
543 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
544 {
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
545 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
546 return FALSE;
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
547 }
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
548 if (libnm_retval == LIBNM_ACTIVE_NETWORK_CONNECTION) return TRUE;
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
549 }
14777
182f77347680 [gaim-migrate @ 17541]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14776
diff changeset
550 #elif defined _WIN32
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
551 return (current_network_count > 0);
14696
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
552 #endif
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
553 return TRUE;
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
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
556 #ifdef HAVE_LIBNM
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
557 void
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
558 nm_callback_func(libnm_glib_ctx* ctx, gpointer user_data)
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 GList *l;
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
561 GaimAccount *account;
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
562 static libnm_glib_state prev = LIBNM_NO_DBUS;
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
563 libnm_glib_state current;
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
564 GaimConnectionUiOps *ui_ops = gaim_connections_get_ui_ops();
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
565
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
566 current = libnm_glib_get_network_state(ctx);
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
567 gaim_debug_info("network","Entering nm_callback_func!\n");
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
568
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
569 switch(current)
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
570 {
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
571 case LIBNM_ACTIVE_NETWORK_CONNECTION:
14800
c678034a7e3e [gaim-migrate @ 17565]
Sean Egan <seanegan@gmail.com>
parents: 14791
diff changeset
572 /* Call res_init in case DNS servers have changed */
c678034a7e3e [gaim-migrate @ 17565]
Sean Egan <seanegan@gmail.com>
parents: 14791
diff changeset
573 res_init();
14815
49450a1bc58e [gaim-migrate @ 17581]
Stu Tomlinson <stu@nosnilmot.com>
parents: 14802
diff changeset
574 if (ui_ops != NULL && ui_ops->network_connected != NULL)
49450a1bc58e [gaim-migrate @ 17581]
Stu Tomlinson <stu@nosnilmot.com>
parents: 14802
diff changeset
575 ui_ops->network_connected();
14696
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
576 prev = current;
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
577 break;
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
578 case LIBNM_NO_NETWORK_CONNECTION:
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
579 if (prev != LIBNM_ACTIVE_NETWORK_CONNECTION)
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
580 break;
14815
49450a1bc58e [gaim-migrate @ 17581]
Stu Tomlinson <stu@nosnilmot.com>
parents: 14802
diff changeset
581 if (ui_ops != NULL && ui_ops->network_disconnected != NULL)
49450a1bc58e [gaim-migrate @ 17581]
Stu Tomlinson <stu@nosnilmot.com>
parents: 14802
diff changeset
582 ui_ops->network_disconnected();
14696
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
583 prev = current;
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
584 break;
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
585 case LIBNM_NO_DBUS:
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
586 case LIBNM_NO_NETWORKMANAGER:
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
587 case LIBNM_INVALID_CONTEXT:
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
588 default:
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
589 break;
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
590 }
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
591 }
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
592 #endif
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
593
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
594 void
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
595 gaim_network_init(void)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
596 {
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
597 #ifdef _WIN32
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
598 GError *err = NULL;
14780
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
599 gint cnt = wgaim_get_connected_network_count();
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
600
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
601 if (cnt < 0) /* Assume there is a network */
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
602 current_network_count = 1;
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
603 /* 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
604 else
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
605 {
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
606 current_network_count = cnt;
ec9cc2219e55 [gaim-migrate @ 17544]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14777
diff changeset
607 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
608 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
609 }
14722
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
610 #endif
51685370de57 [gaim-migrate @ 17476]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14696
diff changeset
611
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
612 gaim_prefs_add_none ("/core/network");
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
613 gaim_prefs_add_bool ("/core/network/auto_ip", TRUE);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
614 gaim_prefs_add_string("/core/network/public_ip", "");
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
615 gaim_prefs_add_bool ("/core/network/ports_range_use", FALSE);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
616 gaim_prefs_add_int ("/core/network/ports_range_start", 1024);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
617 gaim_prefs_add_int ("/core/network/ports_range_end", 2048);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
618
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
619 gaim_upnp_discover(NULL, NULL);
14696
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
620
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
621 #ifdef HAVE_LIBNM
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
622 nm_context = libnm_glib_init();
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
623 if(nm_context)
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
624 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
625 #endif
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
626 }
14696
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
627
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
628 void
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
629 gaim_network_uninit(void)
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
630 {
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
631 #ifdef HAVE_LIBNM
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
632 /* 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
633 * using a global variable + this function, please do. */
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
634 if(nm_context && nm_callback_idx)
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
635 libnm_glib_unregister_callback(nm_context, nm_callback_idx);
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
636
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
637 if(nm_context)
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
638 libnm_glib_shutdown(nm_context);
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
639 #endif
34083fe39891 [gaim-migrate @ 17448]
Sean Egan <seanegan@gmail.com>
parents: 14267
diff changeset
640 }