media: pass both TCP and UDP media relays to backend
[siplcs.git] / src / purple / purple-network.c
blobeed3ef367635bc48f25f8f7921f118b320fd8337
1 /**
2 * @file purple-network.c
4 * pidgin-sipe
6 * Copyright (C) 2010-11 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"
25 #endif
27 #include <string.h>
29 #include "glib.h"
30 #include "network.h"
31 #include "eventloop.h"
33 #ifdef _WIN32
34 /* for network */
35 #include "win32/libc_interface.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 #include <unistd.h>
51 #include "sipe-common.h"
52 #include "sipe-backend.h"
53 #include "purple-private.h"
55 #if 0
56 /**
57 * @TODO: get_suitable_local_ip()
59 * The code is most likely broken for Mac OS X as it seems that that platform
60 * returns variable-sized "struct ifreq". The new, alignment compliant code
61 * assumes a fix-sized "struct ifreq", i.e. it uses array access.
63 * If somebody is bothered by this, please provide a *VERIFIED* alternative
64 * implementation for platforms that define _SIZEOF_ADDR_IFREQ().
66 **/
69 * Calling sizeof(struct ifreq) isn't always correct on
70 * Mac OS X (and maybe others).
72 #ifdef _SIZEOF_ADDR_IFREQ
73 # define HX_SIZE_OF_IFREQ(a) _SIZEOF_ADDR_IFREQ(a)
74 #else
75 # define HX_SIZE_OF_IFREQ(a) sizeof(a)
76 #endif
77 #endif
79 #define IFREQ_MAX 32
81 /**
82 * Returns local IP address suitable for connection.
84 * purple_network_get_my_ip() will not do this, because it might return an
85 * address within 169.254.x.x range that was assigned to interface disconnected
86 * from the network (when multiple network adapters are available). This is a
87 * copy-paste from libpurple's network.c, only change is that link local addresses
88 * are ignored.
90 * Maybe this should be fixed in libpurple or some better solution found.
92 static const gchar *get_suitable_local_ip(void)
94 int source = socket(PF_INET,SOCK_STREAM, 0);
96 if (source >= 0) {
97 struct ifreq *buffer = g_new0(struct ifreq, IFREQ_MAX);
98 struct ifconf ifc;
99 guint32 lhost = htonl(127 * 256 * 256 * 256 + 1);
100 guint32 llocal = htonl((169 << 24) + (254 << 16));
101 guint i;
102 static char ip[16];
104 /* @TODO: assumes constant sizeof(struct ifreq) [see above] */
105 ifc.ifc_len = sizeof(struct ifreq) * IFREQ_MAX;
106 ifc.ifc_req = buffer;
107 ioctl(source, SIOCGIFCONF, &ifc);
109 close(source);
111 for (i = 0; i < IFREQ_MAX; i++)
113 /* @TODO: assumes constant sizeof(struct ifreq) [see above] */
114 struct ifreq *ifr = &buffer[i];
116 if (ifr->ifr_addr.sa_family == AF_INET)
118 struct sockaddr_in sin;
119 memcpy(&sin, &ifr->ifr_addr, sizeof(struct sockaddr_in));
120 if (sin.sin_addr.s_addr != lhost
121 && (sin.sin_addr.s_addr & htonl(0xFFFF0000)) != llocal)
123 long unsigned int add = ntohl(sin.sin_addr.s_addr);
124 g_snprintf(ip, 16, "%lu.%lu.%lu.%lu",
125 ((add >> 24) & 255),
126 ((add >> 16) & 255),
127 ((add >> 8) & 255),
128 add & 255);
130 g_free(buffer);
131 return ip;
135 g_free(buffer);
138 return "0.0.0.0";
141 const gchar *sipe_backend_network_ip_address(void)
143 const gchar *ip = purple_network_get_my_ip(-1);
144 if (g_str_has_prefix(ip, "169.254."))
145 ip = get_suitable_local_ip();
146 return ip;
149 struct sipe_backend_listendata {
150 sipe_listen_start_cb listen_cb;
151 sipe_client_connected_cb connect_cb;
153 PurpleNetworkListenData *listener;
154 int watcher;
156 int listenfd;
157 gpointer data;
160 static void
161 client_connected_cb(struct sipe_backend_listendata *ldata, gint listenfd,
162 SIPE_UNUSED_PARAMETER PurpleInputCondition cond)
164 struct sockaddr_in saddr;
165 socklen_t slen = sizeof (saddr);
167 int fd = accept(listenfd, (struct sockaddr*)&saddr, &slen);
169 purple_input_remove(ldata->watcher);
170 ldata->watcher = 0;
171 close(listenfd);
172 ldata->listenfd = -1;
174 if (ldata->connect_cb) {
175 struct sipe_backend_fd *sipe_fd = g_new(struct sipe_backend_fd, 1);
176 sipe_fd->fd = fd;
177 ldata->connect_cb(sipe_fd, ldata->data);
180 g_free(ldata);
183 static void
184 backend_listen_cb(int listenfd, struct sipe_backend_listendata *ldata)
186 struct sockaddr_in addr;
187 socklen_t socklen = sizeof (addr);
189 ldata->listenfd = -1;
190 ldata->listener = NULL;
191 ldata->listenfd = listenfd;
193 getsockname(listenfd, (struct sockaddr*)&addr, &socklen);
194 if (ldata->listen_cb)
195 ldata->listen_cb(ntohs(addr.sin_port), ldata->data);
197 ldata->watcher = purple_input_add(listenfd, PURPLE_INPUT_READ,
198 (PurpleInputFunction)client_connected_cb,
199 ldata);
202 struct sipe_backend_listendata *
203 sipe_backend_network_listen_range(unsigned short port_min,
204 unsigned short port_max,
205 sipe_listen_start_cb listen_cb,
206 sipe_client_connected_cb connect_cb,
207 gpointer data)
209 struct sipe_backend_listendata *ldata;
210 ldata = g_new0(struct sipe_backend_listendata, 1);
212 ldata->listen_cb = listen_cb;
213 ldata->connect_cb = connect_cb;
214 ldata->data = data;
215 ldata->listener = purple_network_listen_range(port_min, port_max,
216 SOCK_STREAM,
217 (PurpleNetworkListenCallback)backend_listen_cb,
218 ldata);
220 if (!ldata->listener) {
221 g_free(ldata);
222 return NULL;
225 return ldata;
228 void sipe_backend_network_listen_cancel(struct sipe_backend_listendata *ldata)
230 g_return_if_fail(ldata);
232 if (ldata->listener)
233 purple_network_listen_cancel(ldata->listener);
234 if (ldata->listenfd)
235 close(ldata->listenfd);
236 g_free(ldata);
239 gboolean
240 sipe_backend_fd_is_valid(struct sipe_backend_fd *fd)
242 return fd && fd->fd >= 0;
245 void
246 sipe_backend_fd_free(struct sipe_backend_fd *fd)
248 g_free(fd);
252 Local Variables:
253 mode: c
254 c-file-style: "bsd"
255 indent-tabs-mode: t
256 tab-width: 8
257 End: