summaryrefslogtreecommitdiff
path: root/libgssdp/gssdp-socket-functions.c
blob: ecbe5f974a9eee741132f86b7f02bcef640906b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * Copyright (C) 2010 Jens Georg <mail@jensge.org>
 *
 * Author: Jens Georg <mail@jensge.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include <config.h>
#include <errno.h>
#include <string.h>

#include <glib.h>

#ifdef G_OS_WIN32
    #include <winsock2.h>
    #include <ws2tcpip.h>
    typedef int socklen_t;
#else
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
#endif

#include "gssdp-error.h"
#include "gssdp-socket-functions.h"
#include "gssdp-pktinfo-message.h"

static char*
gssdp_socket_error_message (int error) {
#ifdef G_OS_WIN32
        return g_win32_error_message (error);
#else
        return g_strdup (g_strerror (error));
#endif
}

static int
gssdp_socket_errno () {
#ifdef G_OS_WIN32
        return WSAGetLastError ();
#else
        return errno;
#endif
}


static gboolean
gssdp_socket_option_set (GSocket    *socket,
                         int         level,
                         int         option,
                         const void *optval,
                         socklen_t   optlen,
                         GError    **error) {
        int res;

        res = setsockopt (g_socket_get_fd (socket),
                          level,
                          option,
                          optval,
                          optlen);

        if (res == -1) {
                char *message;
                int error_code;

                error_code = gssdp_socket_errno ();
                message = gssdp_socket_error_message (error_code);
                g_set_error_literal (error,
                                     GSSDP_ERROR,
                                     GSSDP_ERROR_FAILED,
                                     message);
                g_free (message);
        }

        return res != -1;
}

gboolean
gssdp_socket_mcast_interface_set (GSocket      *socket,
                                  GInetAddress *iface_address,
                                  GError      **error) {

        const guint8 *address;
        gsize native_size;

        address = g_inet_address_to_bytes (iface_address);
        native_size = g_inet_address_get_native_size (iface_address);

        return gssdp_socket_option_set (socket,
                                        IPPROTO_IP,
                                        IP_MULTICAST_IF,
                                        (char *) address,
                                        native_size,
                                        error);
}

#define __GSSDP_UNUSED(x) (void)(x)

gboolean
gssdp_socket_reuse_address (GSocket *socket,
                            gboolean enable,
                            GError **error) {
#if defined(G_OS_WIN32) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
        return gssdp_socket_option_set (socket,
                                        SOL_SOCKET,
#if defined(__OpenBSD__) || defined (__NetBSD__) || defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
                                        SO_REUSEPORT,
#else
                                        SO_REUSEADDR,
#endif

                                        (char *) &enable,
                                        sizeof (enable),
                                        error);
#else
        __GSSDP_UNUSED(socket);
        __GSSDP_UNUSED(enable);
        __GSSDP_UNUSED(error);
#endif
        return TRUE;
}

gboolean
gssdp_socket_enable_info         (GSocket *socket,
                                  gboolean enable,
                                  GError **error)
{
#ifdef HAVE_PKTINFO
        /* Register the type so g_socket_control_message_deserialize() will
         * find it */
        g_object_unref (g_object_new (GSSDP_TYPE_PKTINFO_MESSAGE, NULL));

        return gssdp_socket_option_set (socket,
                                        IPPROTO_IP,
                                        IP_PKTINFO,
                                        (char *) &enable,
                                        sizeof (enable),
                                        error);
#else
    __GSSDP_UNUSED (socket);
    __GSSDP_UNUSED (enable);
    __GSSDP_UNUSED (error);

    return TRUE;
#endif
}