purple: update purple-user for 3.x.x API
[siplcs.git] / src / purple / purple-network.c
blobe8ae2d2dff04c0665002b7f35359593a3a2b95a8
1 /**
2 * @file purple-network.c
4 * pidgin-sipe
6 * Copyright (C) 2010-2013 SIPE Project <http://sipe.sourceforge.net/>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #ifdef HAVE_CONFIG_H
24 #include "config.h" /* coverity[hfa: FALSE] */
25 #endif
27 #include <string.h>
29 #include "glib.h"
30 #include "network.h"
31 #include "eventloop.h"
33 #ifdef _WIN32
34 /* wrappers for write() & friends for socket handling */
35 #include "win32/win32dep.h"
36 #include <nspapi.h>
37 #else
38 #include <sys/types.h>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <net/if.h>
43 #include <arpa/inet.h>
44 #ifdef HAVE_SYS_SOCKIO_H
45 #include <sys/sockio.h> /* SIOCGIFCONF for Solaris */
46 #endif
47 #endif
49 #ifdef HAVE_UNISTD_H
50 #include <unistd.h>
51 #endif
53 #include "sipe-common.h"
54 #include "sipe-backend.h"
55 #include "purple-private.h"
57 #if 0
58 /**
59 * @TODO: get_suitable_local_ip()
61 * The code is most likely broken for Mac OS X as it seems that that platform
62 * returns variable-sized "struct ifreq". The new, alignment compliant code
63 * assumes a fix-sized "struct ifreq", i.e. it uses array access.
65 * If somebody is bothered by this, please provide a *VERIFIED* alternative
66 * implementation for platforms that define _SIZEOF_ADDR_IFREQ().
68 **/
71 * Calling sizeof(struct ifreq) isn't always correct on
72 * Mac OS X (and maybe others).
74 #ifdef _SIZEOF_ADDR_IFREQ
75 # define HX_SIZE_OF_IFREQ(a) _SIZEOF_ADDR_IFREQ(a)
76 #else
77 # define HX_SIZE_OF_IFREQ(a) sizeof(a)
78 #endif
79 #endif
81 #define IFREQ_MAX 32
83 /**
84 * Returns local IP address suitable for connection.
86 * purple_network_get_my_ip() will not do this, because it might return an
87 * address within 169.254.x.x range that was assigned to interface disconnected
88 * from the network (when multiple network adapters are available). This is a
89 * copy-paste from libpurple's network.c, only change is that link local addresses
90 * are ignored.
92 * Maybe this should be fixed in libpurple or some better solution found.
94 static const gchar *get_suitable_local_ip(void)
96 int source = socket(PF_INET,SOCK_STREAM, 0);
98 if (source >= 0) {
99 struct ifreq *buffer = g_new0(struct ifreq, IFREQ_MAX);
100 struct ifconf ifc;
101 guint32 lhost = htonl(127 * 256 * 256 * 256 + 1);
102 guint32 llocal = htonl((169 << 24) + (254 << 16));
103 guint i;
104 static char ip[16];
106 /* @TODO: assumes constant sizeof(struct ifreq) [see above] */
107 ifc.ifc_len = sizeof(struct ifreq) * IFREQ_MAX;
108 ifc.ifc_req = buffer;
109 ioctl(source, SIOCGIFCONF, &ifc);
111 close(source);
113 for (i = 0; i < IFREQ_MAX; i++)
115 /* @TODO: assumes constant sizeof(struct ifreq) [see above] */
116 struct ifreq *ifr = &buffer[i];
118 if (ifr->ifr_addr.sa_family == AF_INET)
120 struct sockaddr_in sin;
121 memcpy(&sin, &ifr->ifr_addr, sizeof(struct sockaddr_in));
122 if (sin.sin_addr.s_addr != lhost
123 && (sin.sin_addr.s_addr & htonl(0xFFFF0000)) != llocal)
125 long unsigned int add = ntohl(sin.sin_addr.s_addr);
126 g_snprintf(ip, 16, "%lu.%lu.%lu.%lu",
127 ((add >> 24) & 255),
128 ((add >> 16) & 255),
129 ((add >> 8) & 255),
130 add & 255);
132 g_free(buffer);
133 return ip;
137 g_free(buffer);
140 return "0.0.0.0";
143 const gchar *sipe_backend_network_ip_address(SIPE_UNUSED_PARAMETER struct sipe_core_public *sipe_public)
145 const gchar *ip = purple_network_get_my_ip(-1);
146 if (g_str_has_prefix(ip, "169.254."))
147 ip = get_suitable_local_ip();
148 return ip;
151 struct sipe_backend_listendata {
152 sipe_listen_start_cb listen_cb;
153 sipe_client_connected_cb connect_cb;
155 PurpleNetworkListenData *listener;
156 int watcher;
158 int listenfd;
159 gpointer data;
162 static void
163 client_connected_cb(struct sipe_backend_listendata *ldata, gint listenfd,
164 SIPE_UNUSED_PARAMETER PurpleInputCondition cond)
166 struct sockaddr_in saddr;
167 socklen_t slen = sizeof (saddr);
169 int fd = accept(listenfd, (struct sockaddr*)&saddr, &slen);
171 purple_input_remove(ldata->watcher);
172 ldata->watcher = 0;
173 close(listenfd);
174 ldata->listenfd = -1;
176 if (ldata->connect_cb) {
177 struct sipe_backend_fd *sipe_fd = g_new(struct sipe_backend_fd, 1);
178 sipe_fd->fd = fd;
179 ldata->connect_cb(sipe_fd, ldata->data);
182 g_free(ldata);
185 static void
186 backend_listen_cb(int listenfd, struct sipe_backend_listendata *ldata)
188 struct sockaddr_in addr;
189 socklen_t socklen = sizeof (addr);
191 ldata->listenfd = -1;
192 ldata->listener = NULL;
193 ldata->listenfd = listenfd;
195 /* ignore error code */
196 (void) getsockname(listenfd, (struct sockaddr*)&addr, &socklen);
197 if (ldata->listen_cb)
198 ldata->listen_cb(ntohs(addr.sin_port), ldata->data);
200 ldata->watcher = purple_input_add(listenfd, PURPLE_INPUT_READ,
201 (PurpleInputFunction)client_connected_cb,
202 ldata);
205 struct sipe_backend_listendata *
206 sipe_backend_network_listen_range(unsigned short port_min,
207 unsigned short port_max,
208 sipe_listen_start_cb listen_cb,
209 sipe_client_connected_cb connect_cb,
210 gpointer data)
212 struct sipe_backend_listendata *ldata;
213 ldata = g_new0(struct sipe_backend_listendata, 1);
215 ldata->listen_cb = listen_cb;
216 ldata->connect_cb = connect_cb;
217 ldata->data = data;
218 ldata->listener = purple_network_listen_range(port_min, port_max,
219 #if PURPLE_VERSION_CHECK(3,0,0)
220 /* @TODO: does FT work with IPv6? */
221 AF_INET,
222 #endif
223 SOCK_STREAM,
224 #if PURPLE_VERSION_CHECK(3,0,0)
225 /* @TODO: should we allow external mapping? */
226 FALSE,
227 #endif
228 (PurpleNetworkListenCallback)backend_listen_cb,
229 ldata);
231 if (!ldata->listener) {
232 g_free(ldata);
233 return NULL;
236 return ldata;
239 void sipe_backend_network_listen_cancel(struct sipe_backend_listendata *ldata)
241 g_return_if_fail(ldata);
243 if (ldata->listener)
244 purple_network_listen_cancel(ldata->listener);
245 if (ldata->listenfd)
246 close(ldata->listenfd);
247 g_free(ldata);
250 gboolean
251 sipe_backend_fd_is_valid(struct sipe_backend_fd *fd)
253 return fd && fd->fd >= 0;
256 void
257 sipe_backend_fd_free(struct sipe_backend_fd *fd)
259 g_free(fd);
263 Local Variables:
264 mode: c
265 c-file-style: "bsd"
266 indent-tabs-mode: t
267 tab-width: 8
268 End: