2 * Copyright (C) 2006, 2007, 2008 OpenedHand Ltd.
3 * Copyright (C) 2009 Nokia Corporation, all rights reserved.
5 * Author: Jorn Baayen <jorn@openedhand.com>
6 * Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
7 * <zeeshan.ali@nokia.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
27 #include <sys/socket.h>
28 #include <sys/types.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
36 #include "gssdp-socket-source.h"
37 #include "gssdp-protocol.h"
39 struct _GSSDPSocketSource {
46 gssdp_socket_source_prepare (GSource *source,
49 gssdp_socket_source_check (GSource *source);
51 gssdp_socket_source_dispatch (GSource *source,
55 gssdp_socket_source_finalize (GSource *source);
57 static const GSourceFuncs gssdp_socket_source_funcs = {
58 gssdp_socket_source_prepare,
59 gssdp_socket_source_check,
60 gssdp_socket_source_dispatch,
61 gssdp_socket_source_finalize
65 * gssdp_socket_source_new
67 * Return value: A new #GSSDPSocketSource
70 gssdp_socket_source_new (GSSDPSocketSourceType type,
74 GSSDPSocketSource *socket_source;
75 struct sockaddr_in bind_addr;
76 struct in_addr iface_addr;
78 gboolean boolean = TRUE;
83 source = g_source_new ((GSourceFuncs*)&gssdp_socket_source_funcs,
84 sizeof (GSSDPSocketSource));
86 socket_source = (GSSDPSocketSource *) source;
89 socket_source->poll_fd.fd = socket (AF_INET,
92 if (socket_source->poll_fd.fd == -1)
95 socket_source->poll_fd.events = G_IO_IN | G_IO_ERR;
97 g_source_add_poll (source, &socket_source->poll_fd);
99 /* Enable broadcasting */
100 res = setsockopt (socket_source->poll_fd.fd,
109 res = setsockopt (socket_source->poll_fd.fd,
117 memset (&bind_addr, 0, sizeof (bind_addr));
118 bind_addr.sin_family = AF_INET;
120 res = inet_aton (host_ip, &iface_addr);
124 /* Set up additional things according to the type of socket desired */
125 if (type == GSSDP_SOCKET_SOURCE_TYPE_MULTICAST) {
126 /* Allow multiple sockets to use the same PORT number */
127 res = setsockopt (socket_source->poll_fd.fd,
139 /* Enable multicast loopback */
140 res = setsockopt (socket_source->poll_fd.fd,
148 /* Set the interface */
149 res = setsockopt (socket_source->poll_fd.fd,
153 sizeof (struct in_addr));
157 /* Subscribe to multicast channel */
158 res = inet_aton (SSDP_ADDR, &(mreq.imr_multiaddr));
162 memcpy (&(mreq.imr_interface),
164 sizeof (struct in_addr));
166 res = setsockopt (socket_source->poll_fd.fd,
174 bind_addr.sin_port = htons (SSDP_PORT);
175 res = inet_aton (SSDP_ADDR, &(bind_addr.sin_addr));
179 bind_addr.sin_port = 0;
180 memcpy (&(bind_addr.sin_addr),
182 sizeof (struct in_addr));
185 /* Bind to requested port and address */
186 res = bind (socket_source->poll_fd.fd,
187 (struct sockaddr *) &bind_addr,
192 return socket_source;
195 g_source_destroy (source);
201 gssdp_socket_source_prepare (GSource *source,
208 gssdp_socket_source_check (GSource *source)
210 GSSDPSocketSource *socket_source;
212 socket_source = (GSSDPSocketSource *) source;
214 return socket_source->poll_fd.revents & (G_IO_IN | G_IO_ERR);
218 gssdp_socket_source_dispatch (GSource *source,
219 GSourceFunc callback,
222 GSSDPSocketSource *socket_source;
224 socket_source = (GSSDPSocketSource *) source;
226 if (socket_source->poll_fd.revents & G_IO_IN) {
229 callback (user_data);
230 } else if (socket_source->poll_fd.revents & G_IO_ERR) {
236 size_int = sizeof (int);
238 /* Get errno from socket */
239 getsockopt (socket_source->poll_fd.fd,
245 g_warning ("Socket error %d received: %s",
254 gssdp_socket_source_finalize (GSource *source)
256 GSSDPSocketSource *socket_source;
258 socket_source = (GSSDPSocketSource *) source;
260 /* Close the socket */
261 close (socket_source->poll_fd.fd);
265 * gssdp_socket_source_get_fd
267 * Return value: The socket's FD.
270 gssdp_socket_source_get_fd (GSSDPSocketSource *socket_source)
272 g_return_val_if_fail (socket_source != NULL, -1);
274 return socket_source->poll_fd.fd;