adium: clean up paths in xcconfigs
[siplcs.git] / src / purple / purple-network.c
bloba8e7fc1aa4d3d1ee0187e8f7a63555fa055d8075
1 /**
2 * @file purple-network.c
4 * pidgin-sipe
6 * Copyright (C) 2010-2016 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"
31 #include "conversation.h"
32 #include "network.h"
33 #include "eventloop.h"
35 #ifdef _WIN32
36 /* wrappers for write() & friends for socket handling */
37 #include "win32/win32dep.h"
38 #include <nspapi.h>
39 #else
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <arpa/inet.h>
43 #endif
45 #ifdef HAVE_UNISTD_H
46 #include <unistd.h>
47 #endif
49 #include "sipe-common.h"
50 #include "sipe-backend.h"
51 #include "purple-private.h"
53 struct sipe_backend_listendata {
54 sipe_listen_start_cb listen_cb;
55 sipe_client_connected_cb connect_cb;
57 PurpleNetworkListenData *listener;
58 int watcher;
60 int listenfd;
61 gpointer data;
64 static void
65 client_connected_cb(struct sipe_backend_listendata *ldata, gint listenfd,
66 SIPE_UNUSED_PARAMETER PurpleInputCondition cond)
68 struct sockaddr_in saddr;
69 socklen_t slen = sizeof (saddr);
71 int fd = accept(listenfd, (struct sockaddr*)&saddr, &slen);
73 purple_input_remove(ldata->watcher);
74 ldata->watcher = 0;
75 close(listenfd);
76 ldata->listenfd = -1;
78 if (fd >= 0) {
79 if (ldata->connect_cb) {
80 ldata->connect_cb(sipe_backend_fd_from_int(fd), ldata->data);
81 } else {
82 close(fd);
86 g_free(ldata);
89 static void
90 backend_listen_cb(int listenfd, struct sipe_backend_listendata *ldata)
92 struct sockaddr_in addr;
93 socklen_t socklen = sizeof (addr);
95 ldata->listenfd = -1;
96 ldata->listener = NULL;
97 ldata->listenfd = listenfd;
99 /* ignore error code */
100 (void) getsockname(listenfd, (struct sockaddr*)&addr, &socklen);
101 if (ldata->listen_cb)
102 ldata->listen_cb(ntohs(addr.sin_port), ldata->data);
104 ldata->watcher = purple_input_add(listenfd, PURPLE_INPUT_READ,
105 (PurpleInputFunction)client_connected_cb,
106 ldata);
109 struct sipe_backend_listendata *
110 sipe_backend_network_listen_range(unsigned short port_min,
111 unsigned short port_max,
112 sipe_listen_start_cb listen_cb,
113 sipe_client_connected_cb connect_cb,
114 gpointer data)
116 struct sipe_backend_listendata *ldata;
117 ldata = g_new0(struct sipe_backend_listendata, 1);
119 ldata->listen_cb = listen_cb;
120 ldata->connect_cb = connect_cb;
121 ldata->data = data;
122 ldata->listener = purple_network_listen_range(port_min, port_max,
123 #if PURPLE_VERSION_CHECK(3,0,0)
124 /* @TODO: does FT work with IPv6? */
125 AF_INET,
126 #endif
127 SOCK_STREAM,
128 #if PURPLE_VERSION_CHECK(3,0,0)
129 /* @TODO: should we allow external mapping? */
130 FALSE,
131 #endif
132 (PurpleNetworkListenCallback)backend_listen_cb,
133 ldata);
135 if (!ldata->listener) {
136 g_free(ldata);
137 return NULL;
140 return ldata;
143 void sipe_backend_network_listen_cancel(struct sipe_backend_listendata *ldata)
145 g_return_if_fail(ldata);
147 if (ldata->listener)
148 purple_network_listen_cancel(ldata->listener);
149 if (ldata->listenfd)
150 close(ldata->listenfd);
151 g_free(ldata);
154 struct sipe_backend_fd *
155 sipe_backend_fd_from_int(int fd)
157 struct sipe_backend_fd *sipe_fd = g_new(struct sipe_backend_fd, 1);
158 sipe_fd->fd = fd;
159 return sipe_fd;
162 gboolean
163 sipe_backend_fd_is_valid(struct sipe_backend_fd *fd)
165 return fd && fd->fd >= 0;
168 void
169 sipe_backend_fd_free(struct sipe_backend_fd *fd)
171 g_free(fd);
175 Local Variables:
176 mode: c
177 c-file-style: "bsd"
178 indent-tabs-mode: t
179 tab-width: 8
180 End: