udp-bsd.c 6.15 KB
Newer Older
1
2
3
/*
 * This file is part of the Nice GLib ICE library.
 *
4
5
6
 * (C) 2006-2009 Collabora Ltd.
 *  Contact: Youness Alaoui
 * (C) 2006-2009 Nokia Corporation. All rights reserved.
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 *  Contact: Kai Vehmanen
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the Nice GLib ICE library.
 *
 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
 * Corporation. All Rights Reserved.
 *
 * Contributors:
 *   Dafydd Harries, Collabora Ltd.
26
 *   Youness Alaoui, Collabora Ltd.
27
28
29
30
31
32
33
34
35
36
37
 *
 * Alternatively, the contents of this file may be used under the terms of the
 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
 * case the provisions of LGPL are applicable instead of those above. If you
 * wish to allow use of your version of this file only under the terms of the
 * LGPL and not to allow others to use your version of this file under the
 * MPL, indicate your decision by deleting the provisions above and replace
 * them with the notice and other provisions required by the LGPL. If you do
 * not delete the provisions above, a recipient may use your version of this
 * file under either the MPL or the LGPL.
 */
38
39

/*
40
41
 * Implementation of UDP socket interface using Berkeley sockets. (See
 * http://en.wikipedia.org/wiki/Berkeley_sockets.)
42
 */
43
44
45
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
46

47

48
#include <string.h>
49
#include <errno.h>
50
#include <fcntl.h>
51

52
#include "udp-bsd.h"
53

54
#ifndef G_OS_WIN32
55
56
57
#include <unistd.h>
#endif

58

59
60
61
62
63
64
static void socket_close (NiceSocket *sock);
static gint socket_recv (NiceSocket *sock, NiceAddress *from,
    guint len, gchar *buf);
static gboolean socket_send (NiceSocket *sock, const NiceAddress *to,
    guint len, const gchar *buf);
static gboolean socket_is_reliable (NiceSocket *sock);
65

66
67
68
69
70
71
struct UdpBsdSocketPrivate
{
  NiceAddress niceaddr;
  GSocketAddress *gaddr;
};

72
73
NiceSocket *
nice_udp_bsd_socket_new (NiceAddress *addr)
74
{
75
  struct sockaddr_storage name;
76
  NiceSocket *sock = g_slice_new0 (NiceSocket);
Livio Madaro's avatar
Livio Madaro committed
77
  GSocket *gsock = NULL;
Youness Alaoui's avatar
Youness Alaoui committed
78
  gboolean gret = FALSE;
Livio Madaro's avatar
Livio Madaro committed
79
  GSocketAddress *gaddr;
80
  struct UdpBsdSocketPrivate *priv;
81

82
83
84
85
86
87
  if (addr != NULL) {
    nice_address_copy_to_sockaddr(addr, (struct sockaddr *)&name);
  } else {
    memset (&name, 0, sizeof (name));
    name.ss_family = AF_UNSPEC;
  }
88

89
  if (name.ss_family == AF_UNSPEC || name.ss_family == AF_INET) {
Youness Alaoui's avatar
Youness Alaoui committed
90
91
    gsock = g_socket_new (G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_DATAGRAM,
        G_SOCKET_PROTOCOL_UDP, NULL);
92
    name.ss_family = AF_INET;
93
#ifdef HAVE_SA_LEN
94
    name.ss_len = sizeof (struct sockaddr_in);
95
96
#endif
  } else if (name.ss_family == AF_INET6) {
Youness Alaoui's avatar
Youness Alaoui committed
97
98
    gsock = g_socket_new (G_SOCKET_FAMILY_IPV6, G_SOCKET_TYPE_DATAGRAM,
        G_SOCKET_PROTOCOL_UDP, NULL);
99
100
101
    name.ss_family = AF_INET6;
#ifdef HAVE_SA_LEN
    name.ss_len = sizeof (struct sockaddr_in6);
102
#endif
103
  }
104

Youness Alaoui's avatar
Youness Alaoui committed
105
  if (gsock == NULL) {
106
107
    g_slice_free (NiceSocket, sock);
    return NULL;
Youness Alaoui's avatar
Youness Alaoui committed
108
  }
109

Youness Alaoui's avatar
Youness Alaoui committed
110
111
112
113
114
115
116
  /* GSocket: All socket file descriptors are set to be close-on-exec. */
  g_socket_set_blocking (gsock, false);
  gaddr = g_socket_address_new_from_native (&name, sizeof (name));
  if (gaddr != NULL) {
    gret = g_socket_bind (gsock, gaddr, FALSE, NULL);
    g_object_unref (gaddr);
  }
117

Youness Alaoui's avatar
Youness Alaoui committed
118
  if (gret == FALSE) {
119
    g_slice_free (NiceSocket, sock);
Youness Alaoui's avatar
Youness Alaoui committed
120
121
    g_socket_close (gsock, NULL);
    g_object_unref (gsock);
122
123
    return NULL;
  }
124

Youness Alaoui's avatar
Youness Alaoui committed
125
126
127
  gaddr = g_socket_get_local_address (gsock, NULL);
  if (gaddr == NULL ||
      !g_socket_address_to_native (gaddr, &name, sizeof(name), NULL)) {
128
    g_slice_free (NiceSocket, sock);
Youness Alaoui's avatar
Youness Alaoui committed
129
130
    g_socket_close (gsock, NULL);
    g_object_unref (gsock);
131
132
    return NULL;
  }
133

Youness Alaoui's avatar
Youness Alaoui committed
134
  g_object_unref (gaddr);
135

Livio Madaro's avatar
Livio Madaro committed
136
  nice_address_set_from_sockaddr (&sock->addr, (struct sockaddr *)&name);
Youness Alaoui's avatar
Youness Alaoui committed
137

138
139
140
  priv = sock->priv = g_slice_new (struct UdpBsdSocketPrivate);
  nice_address_init (&priv->niceaddr);

Livio Madaro's avatar
Livio Madaro committed
141
  sock->fileno = gsock;
142
143
  sock->send = socket_send;
  sock->recv = socket_recv;
144
  sock->is_reliable = socket_is_reliable;
145
  sock->close = socket_close;
146

147
  return sock;
148
}
149
150
151
152

static void
socket_close (NiceSocket *sock)
{
153
154
155
156
157
158
  struct UdpBsdSocketPrivate *priv = sock->priv;

  if (priv->gaddr)
    g_object_unref (priv->gaddr);
  g_slice_free (struct UdpBsdSocketPrivate, sock->priv);

Youness Alaoui's avatar
Youness Alaoui committed
159
160
161
  if (sock->fileno) {
    g_socket_close (sock->fileno, NULL);
    g_object_unref (sock->fileno);
Livio Madaro's avatar
Livio Madaro committed
162
163
    sock->fileno = NULL;
  }
164
165
166
167
168
}

static gint
socket_recv (NiceSocket *sock, NiceAddress *from, guint len, gchar *buf)
{
Livio Madaro's avatar
Livio Madaro committed
169
170
  GSocketAddress *gaddr = NULL;
  GError *gerr = NULL;
171
  gint recvd;
172

Youness Alaoui's avatar
Youness Alaoui committed
173
  recvd = g_socket_receive_from (sock->fileno, &gaddr, buf, len, NULL, &gerr);
174

175
  if (recvd < 0) {
Youness Alaoui's avatar
Youness Alaoui committed
176
177
    if (g_error_matches(gerr, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK)
        || g_error_matches(gerr, G_IO_ERROR, G_IO_ERROR_FAILED))
Livio Madaro's avatar
Livio Madaro committed
178
179
      recvd = 0;

Youness Alaoui's avatar
Youness Alaoui committed
180
    g_error_free (gerr);
181
182
  }

183
184
185
186
  if (recvd > 0 && from != NULL && gaddr != NULL) {
    struct sockaddr_storage sa;

    g_socket_address_to_native (gaddr, &sa, sizeof (sa), NULL);
187
    nice_address_set_from_sockaddr (from, (struct sockaddr *)&sa);
Livio Madaro's avatar
Livio Madaro committed
188
  }
189

190
191
192
  if (gaddr != NULL)
    g_object_unref (gaddr);

193
194
195
196
197
198
199
  return recvd;
}

static gboolean
socket_send (NiceSocket *sock, const NiceAddress *to,
    guint len, const gchar *buf)
{
200
201
  struct UdpBsdSocketPrivate *priv = sock->priv;
  ssize_t sent;
202

203
204
205
206
207
208
209
210
211
212
213
214
215
  if (!nice_address_is_valid (&priv->niceaddr) ||
      !nice_address_equal (&priv->niceaddr, to)) {
    struct sockaddr_storage sa;
    GSocketAddress *gaddr;

    if (priv->gaddr)
      g_object_unref (priv->gaddr);
    nice_address_copy_to_sockaddr (to, (struct sockaddr *)&sa);
    gaddr = g_socket_address_new_from_native (&sa, sizeof(sa));
    if (priv->gaddr == NULL)
      return -1;
    priv->gaddr = gaddr;
    priv->niceaddr = *to;
216
  }
217

218
219
  sent = g_socket_send_to (sock->fileno, priv->gaddr, buf, len, NULL, NULL);

220
221
222
223
224
225
226
227
228
  return sent == (ssize_t)len;
}

static gboolean
socket_is_reliable (NiceSocket *sock)
{
  return FALSE;
}