2 * @file purple-network.c
6 * Copyright (C) 2010 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
25 #include "eventloop.h"
29 #include "win32/libc_interface.h"
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
36 #include <arpa/inet.h>
37 #ifdef HAVE_SYS_SOCKIO_H
38 #include <sys/sockio.h> /* SIOCGIFCONF for Solaris */
44 #include "sipe-common.h"
45 #include "sipe-backend.h"
47 const gchar
*sipe_backend_network_ip_address(void)
49 return purple_network_get_my_ip(-1);
52 struct sipe_backend_listendata
{
53 sipe_listen_start_cb listen_cb
;
54 sipe_client_connected_cb connect_cb
;
56 PurpleNetworkListenData
*listener
;
64 client_connected_cb(struct sipe_backend_listendata
*ldata
, gint listenfd
,
65 SIPE_UNUSED_PARAMETER PurpleInputCondition cond
)
67 struct sockaddr_in saddr
;
68 socklen_t slen
= sizeof (saddr
);
70 int fd
= accept(listenfd
, (struct sockaddr
*)&saddr
, &slen
);
72 purple_input_remove(ldata
->watcher
);
77 if (ldata
->connect_cb
)
78 ldata
->connect_cb(fd
, ldata
->data
);
84 backend_listen_cb(int listenfd
, struct sipe_backend_listendata
*ldata
)
86 struct sockaddr_in addr
;
87 socklen_t socklen
= sizeof (addr
);
90 ldata
->listener
= NULL
;
91 ldata
->listenfd
= listenfd
;
93 getsockname(listenfd
, (struct sockaddr
*)&addr
, &socklen
);
95 ldata
->listen_cb(ntohs(addr
.sin_port
), ldata
->data
);
97 ldata
->watcher
= purple_input_add(listenfd
, PURPLE_INPUT_READ
,
98 (PurpleInputFunction
)client_connected_cb
,
102 struct sipe_backend_listendata
*
103 sipe_backend_network_listen_range(unsigned short port_min
,
104 unsigned short port_max
,
105 sipe_listen_start_cb listen_cb
,
106 sipe_client_connected_cb connect_cb
,
109 struct sipe_backend_listendata
*ldata
;
110 ldata
= g_new0(struct sipe_backend_listendata
, 1);
112 ldata
->listen_cb
= listen_cb
;
113 ldata
->connect_cb
= connect_cb
;
115 ldata
->listener
= purple_network_listen_range(port_min
, port_max
,
117 (PurpleNetworkListenCallback
)backend_listen_cb
,
120 if (!ldata
->listener
) {
128 void sipe_backend_network_listen_cancel(struct sipe_backend_listendata
*ldata
)
130 g_return_if_fail(ldata
);
133 purple_network_listen_cancel(ldata
->listener
);
135 close(ldata
->listenfd
);