buddy: add interface for number of buddies
[siplcs.git] / src / purple / purple-network.c
blobd59895507e953ef48e955f886a47b6d678574030
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 #ifdef HAVE_UNISTD_H
51 #include <unistd.h>
52 #endif
54 #include "sipe-common.h"
55 #include "sipe-backend.h"
56 #include "purple-private.h"
58 #if 0
59 /**
60 * @TODO: get_suitable_local_ip()
62 * The code is most likely broken for Mac OS X as it seems that that platform
63 * returns variable-sized "struct ifreq". The new, alignment compliant code
64 * assumes a fix-sized "struct ifreq", i.e. it uses array access.
66 * If somebody is bothered by this, please provide a *VERIFIED* alternative
67 * implementation for platforms that define _SIZEOF_ADDR_IFREQ().
69 **/
72 * Calling sizeof(struct ifreq) isn't always correct on
73 * Mac OS X (and maybe others).
75 #ifdef _SIZEOF_ADDR_IFREQ
76 # define HX_SIZE_OF_IFREQ(a) _SIZEOF_ADDR_IFREQ(a)
77 #else
78 # define HX_SIZE_OF_IFREQ(a) sizeof(a)
79 #endif
80 #endif
82 #define IFREQ_MAX 32
84 /**
85 * Returns local IP address suitable for connection.
87 * purple_network_get_my_ip() will not do this, because it might return an
88 * address within 169.254.x.x range that was assigned to interface disconnected
89 * from the network (when multiple network adapters are available). This is a
90 * copy-paste from libpurple's network.c, only change is that link local addresses
91 * are ignored.
93 * Maybe this should be fixed in libpurple or some better solution found.
95 static const gchar *get_suitable_local_ip(void)
97 int source = socket(PF_INET,SOCK_STREAM, 0);
99 if (source >= 0) {
100 struct ifreq *buffer = g_new0(struct ifreq, IFREQ_MAX);
101 struct ifconf ifc;
102 guint32 lhost = htonl(127 * 256 * 256 * 256 + 1);
103 guint32 llocal = htonl((169 << 24) + (254 << 16));
104 guint i;
105 static char ip[16];
107 /* @TODO: assumes constant sizeof(struct ifreq) [see above] */
108 ifc.ifc_len = sizeof(struct ifreq) * IFREQ_MAX;
109 ifc.ifc_req = buffer;
110 ioctl(source, SIOCGIFCONF, &ifc);
112 close(source);
114 for (i = 0; i < IFREQ_MAX; i++)
116 /* @TODO: assumes constant sizeof(struct ifreq) [see above] */
117 struct ifreq *ifr = &buffer[i];
119 if (ifr->ifr_addr.sa_family == AF_INET)
121 struct sockaddr_in sin;
122 memcpy(&sin, &ifr->ifr_addr, sizeof(struct sockaddr_in));
123 if (sin.sin_addr.s_addr != lhost
124 && (sin.sin_addr.s_addr & htonl(0xFFFF0000)) != llocal)
126 long unsigned int add = ntohl(sin.sin_addr.s_addr);
127 g_snprintf(ip, 16, "%lu.%lu.%lu.%lu",
128 ((add >> 24) & 255),
129 ((add >> 16) & 255),
130 ((add >> 8) & 255),
131 add & 255);
133 g_free(buffer);
134 return ip;
138 g_free(buffer);
141 return "0.0.0.0";
144 const gchar *sipe_backend_network_ip_address(SIPE_UNUSED_PARAMETER struct sipe_core_public *sipe_public)
146 const gchar *ip = purple_network_get_my_ip(-1);
147 if (g_str_has_prefix(ip, "169.254."))
148 ip = get_suitable_local_ip();
149 return ip;
152 struct sipe_backend_listendata {
153 sipe_listen_start_cb listen_cb;
154 sipe_client_connected_cb connect_cb;
156 PurpleNetworkListenData *listener;
157 int watcher;
159 int listenfd;
160 gpointer data;
163 static void
164 client_connected_cb(struct sipe_backend_listendata *ldata, gint listenfd,
165 SIPE_UNUSED_PARAMETER PurpleInputCondition cond)
167 struct sockaddr_in saddr;
168 socklen_t slen = sizeof (saddr);
170 int fd = accept(listenfd, (struct sockaddr*)&saddr, &slen);
172 purple_input_remove(ldata->watcher);
173 ldata->watcher = 0;
174 close(listenfd);
175 ldata->listenfd = -1;
177 if (ldata->connect_cb) {
178 struct sipe_backend_fd *sipe_fd = g_new(struct sipe_backend_fd, 1);
179 sipe_fd->fd = fd;
180 ldata->connect_cb(sipe_fd, ldata->data);
183 g_free(ldata);
186 static void
187 backend_listen_cb(int listenfd, struct sipe_backend_listendata *ldata)
189 struct sockaddr_in addr;
190 socklen_t socklen = sizeof (addr);
192 ldata->listenfd = -1;
193 ldata->listener = NULL;
194 ldata->listenfd = listenfd;
196 /* ignore error code */
197 (void) getsockname(listenfd, (struct sockaddr*)&addr, &socklen);
198 if (ldata->listen_cb)
199 ldata->listen_cb(ntohs(addr.sin_port), ldata->data);
201 ldata->watcher = purple_input_add(listenfd, PURPLE_INPUT_READ,
202 (PurpleInputFunction)client_connected_cb,
203 ldata);
206 struct sipe_backend_listendata *
207 sipe_backend_network_listen_range(unsigned short port_min,
208 unsigned short port_max,
209 sipe_listen_start_cb listen_cb,
210 sipe_client_connected_cb connect_cb,
211 gpointer data)
213 struct sipe_backend_listendata *ldata;
214 ldata = g_new0(struct sipe_backend_listendata, 1);
216 ldata->listen_cb = listen_cb;
217 ldata->connect_cb = connect_cb;
218 ldata->data = data;
219 ldata->listener = purple_network_listen_range(port_min, port_max,
220 #if PURPLE_VERSION_CHECK(3,0,0)
221 /* @TODO: does FT work with IPv6? */
222 AF_INET,
223 #endif
224 SOCK_STREAM,
225 #if PURPLE_VERSION_CHECK(3,0,0)
226 /* @TODO: should we allow external mapping? */
227 FALSE,
228 #endif
229 (PurpleNetworkListenCallback)backend_listen_cb,
230 ldata);
232 if (!ldata->listener) {
233 g_free(ldata);
234 return NULL;
237 return ldata;
240 void sipe_backend_network_listen_cancel(struct sipe_backend_listendata *ldata)
242 g_return_if_fail(ldata);
244 if (ldata->listener)
245 purple_network_listen_cancel(ldata->listener);
246 if (ldata->listenfd)
247 close(ldata->listenfd);
248 g_free(ldata);
251 gboolean
252 sipe_backend_fd_is_valid(struct sipe_backend_fd *fd)
254 return fd && fd->fd >= 0;
257 void
258 sipe_backend_fd_free(struct sipe_backend_fd *fd)
260 g_free(fd);
264 Local Variables:
265 mode: c
266 c-file-style: "bsd"
267 indent-tabs-mode: t
268 tab-width: 8
269 End: