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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* soup-address.c: Internet address handing
*
* Copyright (C) 2000-2003, Ximian, Inc.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "soup-address.h"
#include "soup-dns.h"
#include "soup-marshal.h"
#include "soup-misc.h"
#ifndef INET_ADDRSTRLEN
# define INET_ADDRSTRLEN 16
# define INET6_ADDRSTRLEN 46
#endif
#ifndef INADDR_NONE
#define INADDR_NONE -1
#endif
/**
* SECTION:soup-address
* @short_description: DNS support
*
* #SoupAddress represents the address of a TCP connection endpoint;
* both the IP address and the port. (It is somewhat like an
* object-oriented version of struct sockaddr.)
*
* If libsoup was built with IPv6 support, #SoupAddress will allow
* both IPv4 and IPv6 addresses.
**/
typedef struct {
struct sockaddr *sockaddr;
char *name, *physical;
guint port;
SoupDNSLookup *lookup;
guint timeout_id;
} SoupAddressPrivate;
#define SOUP_ADDRESS_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SOUP_TYPE_ADDRESS, SoupAddressPrivate))
/* sockaddr generic macros */
#define SOUP_SIN(priv) ((struct sockaddr_in *)priv->sockaddr)
#ifdef HAVE_IPV6
#define SOUP_SIN6(priv) ((struct sockaddr_in6 *)priv->sockaddr)
#endif
/* sockaddr family macros */
#define SOUP_ADDRESS_GET_FAMILY(priv) (priv->sockaddr->sa_family)
#define SOUP_ADDRESS_SET_FAMILY(priv, family) \
(priv->sockaddr->sa_family = family)
#ifdef HAVE_IPV6
#define SOUP_ADDRESS_FAMILY_IS_VALID(family) \
(family == AF_INET || family == AF_INET6)
#define SOUP_ADDRESS_FAMILY_SOCKADDR_SIZE(family) \
(family == AF_INET ? sizeof (struct sockaddr_in) : \
sizeof (struct sockaddr_in6))
#define SOUP_ADDRESS_FAMILY_DATA_SIZE(family) \
(family == AF_INET ? sizeof (struct in_addr) : \
sizeof (struct in6_addr))
#else
#define SOUP_ADDRESS_FAMILY_IS_VALID(family) (family == AF_INET)
#define SOUP_ADDRESS_FAMILY_SOCKADDR_SIZE(family) sizeof (struct sockaddr_in)
#define SOUP_ADDRESS_FAMILY_DATA_SIZE(family) sizeof (struct in_addr)
#endif
/* sockaddr port macros */
#define SOUP_ADDRESS_PORT_IS_VALID(port) (port >= 0 && port <= 65535)
#ifdef HAVE_IPV6
#define SOUP_ADDRESS_GET_PORT(priv) \
(priv->sockaddr->sa_family == AF_INET ? \
SOUP_SIN(priv)->sin_port : \
SOUP_SIN6(priv)->sin6_port)
#define SOUP_ADDRESS_SET_PORT(priv, port) \
G_STMT_START { \
if (priv->sockaddr->sa_family == AF_INET) \
SOUP_SIN(priv)->sin_port = port; \
else \
SOUP_SIN6(priv)->sin6_port = port; \
} G_STMT_END
#else
#define SOUP_ADDRESS_GET_PORT(priv) (SOUP_SIN(priv)->sin_port)
#define SOUP_ADDRESS_SET_PORT(priv, port) (SOUP_SIN(priv)->sin_port = port)
#endif
/* sockaddr data macros */
#ifdef HAVE_IPV6
#define SOUP_ADDRESS_GET_DATA(priv) \
(priv->sockaddr->sa_family == AF_INET ? \
(gpointer)&SOUP_SIN(priv)->sin_addr : \
(gpointer)&SOUP_SIN6(priv)->sin6_addr)
#else
#define SOUP_ADDRESS_GET_DATA(priv) ((gpointer)&SOUP_SIN(priv)->sin_addr)
#endif
#define SOUP_ADDRESS_SET_DATA(priv, data, length) \
memcpy (SOUP_ADDRESS_GET_DATA (priv), data, length)
G_DEFINE_TYPE (SoupAddress, soup_address, G_TYPE_OBJECT)
static void
soup_address_init (SoupAddress *addr)
{
}
static void
finalize (GObject *object)
{
SoupAddress *addr = SOUP_ADDRESS (object);
SoupAddressPrivate *priv = SOUP_ADDRESS_GET_PRIVATE (addr);
if (priv->sockaddr)
g_free (priv->sockaddr);
if (priv->name)
g_free (priv->name);
if (priv->physical)
g_free (priv->physical);
if (priv->lookup)
soup_dns_lookup_free (priv->lookup);
if (priv->timeout_id)
g_source_remove (priv->timeout_id);
G_OBJECT_CLASS (soup_address_parent_class)->finalize (object);
}
static void
soup_address_class_init (SoupAddressClass *address_class)
{
GObjectClass *object_class = G_OBJECT_CLASS (address_class);
soup_dns_init ();
g_type_class_add_private (address_class, sizeof (SoupAddressPrivate));
/* virtual method override */
object_class->finalize = finalize;
#ifdef G_OS_WIN32
/* This hopefully is a good place to call WSAStartup */
{
WSADATA wsadata;
if (WSAStartup (MAKEWORD (2, 0), &wsadata) != 0)
g_error ("Windows Sockets could not be initialized");
}
#endif
}
/**
* soup_address_new:
* @name: a hostname or physical address
* @port: a port number
*
* Creates a #SoupAddress from @name and @port. The #SoupAddress's IP
* address may not be available right away; the caller can call
* soup_address_resolve_async() or soup_address_resolve_sync() to
* force a DNS resolution.
*
* Return value: a #SoupAddress
**/
SoupAddress *
soup_address_new (const char *name, guint port)
{
SoupAddress *addr;
SoupAddressPrivate *priv;
g_return_val_if_fail (name != NULL, NULL);
g_return_val_if_fail (SOUP_ADDRESS_PORT_IS_VALID (port), NULL);
addr = g_object_new (SOUP_TYPE_ADDRESS, NULL);
priv = SOUP_ADDRESS_GET_PRIVATE (addr);
priv->name = g_strdup (name);
priv->port = port;
priv->lookup = soup_dns_lookup_name (priv->name);
return addr;
}
/**
* soup_address_new_from_sockaddr:
* @sa: a pointer to a sockaddr
* @len: size of @sa
*
* Returns a #SoupAddress equivalent to @sa (or %NULL if @sa's
* address family isn't supported)
*
* Return value: the new #SoupAddress
**/
SoupAddress *
soup_address_new_from_sockaddr (struct sockaddr *sa, int len)
{
SoupAddress *addr;
SoupAddressPrivate *priv;
g_return_val_if_fail (sa != NULL, NULL);
g_return_val_if_fail (SOUP_ADDRESS_FAMILY_IS_VALID (sa->sa_family), NULL);
g_return_val_if_fail (len == SOUP_ADDRESS_FAMILY_SOCKADDR_SIZE (sa->sa_family), NULL);
addr = g_object_new (SOUP_TYPE_ADDRESS, NULL);
priv = SOUP_ADDRESS_GET_PRIVATE (addr);
priv->sockaddr = g_memdup (sa, len);
priv->port = ntohs (SOUP_ADDRESS_GET_PORT (priv));
priv->lookup = soup_dns_lookup_address (priv->sockaddr);
return addr;
}
/**
* SoupAddressFamily:
* @SOUP_ADDRESS_FAMILY_IPV4: an IPv4 address
* @SOUP_ADDRESS_FAMILY_IPV6: an IPv6 address
*
* The supported address families. Note that the
* %SOUP_ADDRESS_FAMILY_IPV6 constant is available even if libsoup was
* built without IPv6 support, but attempting to create an IPv6
* address will fail in that case.
**/
/**
* SOUP_ADDRESS_ANY_PORT:
*
* This can be passed to any #SoupAddress method that expects a port,
* to indicate that you don't care what port is used.
**/
/**
* soup_address_new_any:
* @family: the address family
* @port: the port number (usually %SOUP_ADDRESS_ANY_PORT)
*
* Returns a #SoupAddress corresponding to the "any" address
* for @family (or %NULL if @family isn't supported), suitable for
* passing to soup_socket_server_new().
*
* Return value: the new #SoupAddress
**/
SoupAddress *
soup_address_new_any (SoupAddressFamily family, guint port)
{
SoupAddress *addr;
SoupAddressPrivate *priv;
g_return_val_if_fail (SOUP_ADDRESS_FAMILY_IS_VALID (family), NULL);
g_return_val_if_fail (SOUP_ADDRESS_PORT_IS_VALID (port), NULL);
addr = g_object_new (SOUP_TYPE_ADDRESS, NULL);
priv = SOUP_ADDRESS_GET_PRIVATE (addr);
priv->port = port;
priv->sockaddr = g_malloc0 (SOUP_ADDRESS_FAMILY_SOCKADDR_SIZE (family));
SOUP_ADDRESS_SET_FAMILY (priv, family);
SOUP_ADDRESS_SET_PORT (priv, htons (port));
priv->lookup = soup_dns_lookup_address (priv->sockaddr);
return addr;
}
/**
* soup_address_get_name:
* @addr: a #SoupAddress
*
* Returns the hostname associated with @addr.
*
* Return value: the hostname, or %NULL if it is not known.
**/
const char *
soup_address_get_name (SoupAddress *addr)
{
g_return_val_if_fail (SOUP_IS_ADDRESS (addr), NULL);
return SOUP_ADDRESS_GET_PRIVATE (addr)->name;
}
/**
* soup_address_get_sockaddr:
* @addr: a #SoupAddress
* @len: return location for sockaddr length
*
* Returns the sockaddr associated with @addr, with its length in
* *@len. If the sockaddr is not yet known, returns %NULL.
*
* Return value: the sockaddr, or %NULL
**/
struct sockaddr *
soup_address_get_sockaddr (SoupAddress *addr, int *len)
{
SoupAddressPrivate *priv;
g_return_val_if_fail (SOUP_IS_ADDRESS (addr), NULL);
priv = SOUP_ADDRESS_GET_PRIVATE (addr);
if (priv->sockaddr && len)
*len = SOUP_ADDRESS_FAMILY_SOCKADDR_SIZE (SOUP_ADDRESS_GET_FAMILY (priv));
return priv->sockaddr;
}
/**
* soup_address_get_physical:
* @addr: a #SoupAddress
*
* Returns the physical address associated with @addr as a string.
* (Eg, "127.0.0.1"). If the address is not yet known, returns %NULL.
*
* Return value: the physical address, or %NULL
**/
const char *
soup_address_get_physical (SoupAddress *addr)
{
SoupAddressPrivate *priv;
g_return_val_if_fail (SOUP_IS_ADDRESS (addr), NULL);
priv = SOUP_ADDRESS_GET_PRIVATE (addr);
if (!priv->sockaddr)
return NULL;
if (!priv->physical)
priv->physical = soup_dns_ntop (priv->sockaddr);
return priv->physical;
}
/**
* soup_address_get_port:
* @addr: a #SoupAddress
*
* Returns the port associated with @addr.
*
* Return value: the port
**/
guint
soup_address_get_port (SoupAddress *addr)
{
g_return_val_if_fail (SOUP_IS_ADDRESS (addr), 0);
return SOUP_ADDRESS_GET_PRIVATE (addr)->port;
}
static void
update_address (SoupAddress *addr, SoupDNSLookup *lookup)
{
SoupAddressPrivate *priv = SOUP_ADDRESS_GET_PRIVATE (addr);
if (!priv->name)
priv->name = soup_dns_lookup_get_hostname (lookup);
if (!priv->sockaddr) {
priv->sockaddr = soup_dns_lookup_get_address (lookup);
SOUP_ADDRESS_SET_PORT (priv, htons (priv->port));
}
}
typedef struct {
SoupAddress *addr;
SoupAddressCallback callback;
gpointer callback_data;
} SoupAddressResolveAsyncData;
static void
lookup_resolved (SoupDNSLookup *lookup, guint status, gpointer user_data)
{
SoupAddressResolveAsyncData *res_data = user_data;
SoupAddress *addr;
SoupAddressCallback callback;
gpointer callback_data;
addr = res_data->addr;
callback = res_data->callback;
callback_data = res_data->callback_data;
g_free (res_data);
if (status == SOUP_STATUS_OK)
update_address (addr, lookup);
if (callback)
callback (addr, status, callback_data);
g_object_unref (addr);
}
/**
* SoupAddressCallback:
* @addr: the #SoupAddress that was resolved
* @status: %SOUP_STATUS_OK, %SOUP_STATUS_CANT_RESOLVE, or
* %SOUP_STATUS_CANCELLED
* @data: the user data that was passed to
* soup_address_resolve_async()
*
* The callback function passed to soup_address_resolve_async().
**/
/**
* soup_address_resolve_async:
* @addr: a #SoupAddress
* @async_context: the #GMainContext to call @callback from
* @cancellable: a #GCancellable object, or %NULL
* @callback: callback to call with the result
* @user_data: data for @callback
*
* Asynchronously resolves the missing half of @addr. (Its IP address
* if it was created with soup_address_new(), or its hostname if it
* was created with soup_address_new_from_sockaddr() or
* soup_address_new_any().)
*
* If @cancellable is non-%NULL, it can be used to cancel the
* resolution. @callback will still be invoked in this case, with a
* status of %SOUP_STATUS_CANCELLED.
**/
void
soup_address_resolve_async (SoupAddress *addr, GMainContext *async_context,
GCancellable *cancellable,
SoupAddressCallback callback, gpointer user_data)
{
SoupAddressPrivate *priv;
SoupAddressResolveAsyncData *res_data;
g_return_if_fail (SOUP_IS_ADDRESS (addr));
priv = SOUP_ADDRESS_GET_PRIVATE (addr);
res_data = g_new (SoupAddressResolveAsyncData, 1);
res_data->addr = addr;
res_data->callback = callback;
res_data->callback_data = user_data;
g_object_ref (addr);
soup_dns_lookup_resolve_async (priv->lookup, async_context, cancellable,
lookup_resolved, res_data);
}
/**
* soup_address_resolve_sync:
* @addr: a #SoupAddress
* @cancellable: a #GCancellable object, or %NULL
*
* Synchronously resolves the missing half of @addr, as with
* soup_address_resolve_async().
*
* If @cancellable is non-%NULL, it can be used to cancel the
* resolution. soup_address_resolve_sync() will then return a status
* of %SOUP_STATUS_CANCELLED.
*
* Return value: %SOUP_STATUS_OK, %SOUP_STATUS_CANT_RESOLVE, or
* %SOUP_STATUS_CANCELLED.
**/
guint
soup_address_resolve_sync (SoupAddress *addr, GCancellable *cancellable)
{
SoupAddressPrivate *priv;
guint status;
g_return_val_if_fail (SOUP_IS_ADDRESS (addr), SOUP_STATUS_MALFORMED);
priv = SOUP_ADDRESS_GET_PRIVATE (addr);
g_object_ref (addr);
status = soup_dns_lookup_resolve (priv->lookup, cancellable);
if (status == SOUP_STATUS_OK)
update_address (addr, priv->lookup);
g_object_unref (addr);
return status;
}
|