telepathy: fix crashes in error/shutdown cases
[siplcs.git] / src / purple / purple-network.c
blob8328b419c6b274383384516e99346cc736402988
1 /**
2 * @file purple-network.c
4 * pidgin-sipe
6 * Copyright (C) 2010-12 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"
32 #include "version.h"
34 #ifdef _WIN32
35 /* wrappers for write() & friends for socket handling */
36 #include "win32/win32dep.h"
37 #include <nspapi.h>
38 #else
39 #include <sys/types.h>
40 #include <sys/ioctl.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <net/if.h>
44 #include <arpa/inet.h>
45 #ifdef HAVE_SYS_SOCKIO_H
46 #include <sys/sockio.h> /* SIOCGIFCONF for Solaris */
47 #endif
48 #endif
50 #include <unistd.h>
52 #include "sipe-common.h"
53 #include "sipe-backend.h"
54 #include "purple-private.h"
56 #if 0
57 /**
58 * @TODO: get_suitable_local_ip()
60 * The code is most likely broken for Mac OS X as it seems that that platform
61 * returns variable-sized "struct ifreq". The new, alignment compliant code
62 * assumes a fix-sized "struct ifreq", i.e. it uses array access.
64 * If somebody is bothered by this, please provide a *VERIFIED* alternative
65 * implementation for platforms that define _SIZEOF_ADDR_IFREQ().
67 **/
70 * Calling sizeof(struct ifreq) isn't always correct on
71 * Mac OS X (and maybe others).
73 #ifdef _SIZEOF_ADDR_IFREQ
74 # define HX_SIZE_OF_IFREQ(a) _SIZEOF_ADDR_IFREQ(a)
75 #else
76 # define HX_SIZE_OF_IFREQ(a) sizeof(a)
77 #endif
78 #endif
80 #define IFREQ_MAX 32
82 /**
83 * Returns local IP address suitable for connection.
85 * purple_network_get_my_ip() will not do this, because it might return an
86 * address within 169.254.x.x range that was assigned to interface disconnected
87 * from the network (when multiple network adapters are available). This is a
88 * copy-paste from libpurple's network.c, only change is that link local addresses
89 * are ignored.
91 * Maybe this should be fixed in libpurple or some better solution found.
93 static const gchar *get_suitable_local_ip(void)
95 int source = socket(PF_INET,SOCK_STREAM, 0);
97 if (source >= 0) {
98 struct ifreq *buffer = g_new0(struct ifreq, IFREQ_MAX);
99 struct ifconf ifc;
100 guint32 lhost = htonl(127 * 256 * 256 * 256 + 1);
101 guint32 llocal = htonl((169 << 24) + (254 << 16));
102 guint i;
103 static char ip[16];
105 /* @TODO: assumes constant sizeof(struct ifreq) [see above] */
106 ifc.ifc_len = sizeof(struct ifreq) * IFREQ_MAX;
107 ifc.ifc_req = buffer;
108 ioctl(source, SIOCGIFCONF, &ifc);
110 close(source);
112 for (i = 0; i < IFREQ_MAX; i++)
114 /* @TODO: assumes constant sizeof(struct ifreq) [see above] */
115 struct ifreq *ifr = &buffer[i];
117 if (ifr->ifr_addr.sa_family == AF_INET)
119 struct sockaddr_in sin;
120 memcpy(&sin, &ifr->ifr_addr, sizeof(struct sockaddr_in));
121 if (sin.sin_addr.s_addr != lhost
122 && (sin.sin_addr.s_addr & htonl(0xFFFF0000)) != llocal)
124 long unsigned int add = ntohl(sin.sin_addr.s_addr);
125 g_snprintf(ip, 16, "%lu.%lu.%lu.%lu",
126 ((add >> 24) & 255),
127 ((add >> 16) & 255),
128 ((add >> 8) & 255),
129 add & 255);
131 g_free(buffer);
132 return ip;
136 g_free(buffer);
139 return "0.0.0.0";
142 const gchar *sipe_backend_network_ip_address(SIPE_UNUSED_PARAMETER struct sipe_core_public *sipe_public)
144 const gchar *ip = purple_network_get_my_ip(-1);
145 if (g_str_has_prefix(ip, "169.254."))
146 ip = get_suitable_local_ip();
147 return ip;
150 struct sipe_backend_listendata {
151 sipe_listen_start_cb listen_cb;
152 sipe_client_connected_cb connect_cb;
154 PurpleNetworkListenData *listener;
155 int watcher;
157 int listenfd;
158 gpointer data;
161 static void
162 client_connected_cb(struct sipe_backend_listendata *ldata, gint listenfd,
163 SIPE_UNUSED_PARAMETER PurpleInputCondition cond)
165 struct sockaddr_in saddr;
166 socklen_t slen = sizeof (saddr);
168 int fd = accept(listenfd, (struct sockaddr*)&saddr, &slen);
170 purple_input_remove(ldata->watcher);
171 ldata->watcher = 0;
172 close(listenfd);
173 ldata->listenfd = -1;
175 if (ldata->connect_cb) {
176 struct sipe_backend_fd *sipe_fd = g_new(struct sipe_backend_fd, 1);
177 sipe_fd->fd = fd;
178 ldata->connect_cb(sipe_fd, ldata->data);
181 g_free(ldata);
184 static void
185 backend_listen_cb(int listenfd, struct sipe_backend_listendata *ldata)
187 struct sockaddr_in addr;
188 socklen_t socklen = sizeof (addr);
190 ldata->listenfd = -1;
191 ldata->listener = NULL;
192 ldata->listenfd = listenfd;
194 /* ignore error code */
195 (void) getsockname(listenfd, (struct sockaddr*)&addr, &socklen);
196 if (ldata->listen_cb)
197 ldata->listen_cb(ntohs(addr.sin_port), ldata->data);
199 ldata->watcher = purple_input_add(listenfd, PURPLE_INPUT_READ,
200 (PurpleInputFunction)client_connected_cb,
201 ldata);
204 struct sipe_backend_listendata *
205 sipe_backend_network_listen_range(unsigned short port_min,
206 unsigned short port_max,
207 sipe_listen_start_cb listen_cb,
208 sipe_client_connected_cb connect_cb,
209 gpointer data)
211 struct sipe_backend_listendata *ldata;
212 ldata = g_new0(struct sipe_backend_listendata, 1);
214 ldata->listen_cb = listen_cb;
215 ldata->connect_cb = connect_cb;
216 ldata->data = data;
217 ldata->listener = purple_network_listen_range(port_min, port_max,
218 #if PURPLE_VERSION_CHECK(3,0,0)
219 /* @TODO: does FT work with IPv6? */
220 AF_INET,
221 #endif
222 SOCK_STREAM,
223 #if PURPLE_VERSION_CHECK(3,0,0)
224 /* @TODO: should we allow external mapping? */
225 FALSE,
226 #endif
227 (PurpleNetworkListenCallback)backend_listen_cb,
228 ldata);
230 if (!ldata->listener) {
231 g_free(ldata);
232 return NULL;
235 return ldata;
238 void sipe_backend_network_listen_cancel(struct sipe_backend_listendata *ldata)
240 g_return_if_fail(ldata);
242 if (ldata->listener)
243 purple_network_listen_cancel(ldata->listener);
244 if (ldata->listenfd)
245 close(ldata->listenfd);
246 g_free(ldata);
249 gboolean
250 sipe_backend_fd_is_valid(struct sipe_backend_fd *fd)
252 return fd && fd->fd >= 0;
255 void
256 sipe_backend_fd_free(struct sipe_backend_fd *fd)
258 g_free(fd);
262 Local Variables:
263 mode: c
264 c-file-style: "bsd"
265 indent-tabs-mode: t
266 tab-width: 8
267 End: