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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
*
* Copyright (C) 2007 William Jon McCann <mccann@jhu.edu>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#ifdef HAVE_STROPTS_H
#include <stropts.h>
#endif
#include <string.h>
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_SYS_SOCKIO_H
#include <sys/sockio.h>
#endif
#include <netdb.h>
#include <sys/ioctl.h>
#include <net/if.h>
#ifndef G_OS_WIN32
#include <sys/select.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#else
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
#include <glib-object.h>
#include "gdm-address.h"
struct _GdmAddress
{
struct sockaddr_storage *ss;
};
/* Register GdmAddress in the glib type system */
GType
gdm_address_get_type (void)
{
static GType addr_type = 0;
if (addr_type == 0) {
addr_type = g_boxed_type_register_static ("GdmAddress",
(GBoxedCopyFunc) gdm_address_copy,
(GBoxedFreeFunc) gdm_address_free);
}
return addr_type;
}
/**
* gdm_address_get_family_type:
* @address: A pointer to a #GdmAddress
*
* Use this function to retrive the address family of @address.
*
* Return value: The address family of @address.
**/
int
gdm_address_get_family_type (GdmAddress *address)
{
g_return_val_if_fail (address != NULL, -1);
return address->ss->ss_family;
}
/**
* gdm_address_new_from_sockaddr:
* @sa: A pointer to a sockaddr.
* @size: size of sockaddr in bytes.
*
* Creates a new #GdmAddress from @sa.
*
* Return value: The new #GdmAddress
* or %NULL if @sa was invalid or the address family isn't supported.
**/
GdmAddress *
gdm_address_new_from_sockaddr (struct sockaddr *sa,
size_t size)
{
GdmAddress *addr;
g_return_val_if_fail (sa != NULL, NULL);
g_return_val_if_fail (size >= sizeof (struct sockaddr), NULL);
g_return_val_if_fail (size <= sizeof (struct sockaddr_storage), NULL);
addr = g_new0 (GdmAddress, 1);
addr->ss = g_new0 (struct sockaddr_storage, 1);
memcpy (addr->ss, sa, size);
return addr;
}
/**
* gdm_address_get_sockaddr_storage:
* @address: A #GdmAddress
*
* This function tanslates @address into a equivalent
* sockaddr_storage
*
* Return value: A newly allocated sockaddr_storage structure the caller must free
* or %NULL if @address did not point to a valid #GdmAddress.
**/
struct sockaddr_storage *
gdm_address_get_sockaddr_storage (GdmAddress *address)
{
struct sockaddr_storage *ss;
g_return_val_if_fail (address != NULL, NULL);
g_return_val_if_fail (address->ss != NULL, NULL);
ss = g_memdup (address->ss, sizeof (struct sockaddr_storage));
return ss;
}
struct sockaddr_storage *
gdm_address_peek_sockaddr_storage (GdmAddress *address)
{
g_return_val_if_fail (address != NULL, NULL);
return address->ss;
}
static gboolean
v4_v4_equal (const struct sockaddr_in *a,
const struct sockaddr_in *b)
{
return a->sin_addr.s_addr == b->sin_addr.s_addr;
}
#ifdef ENABLE_IPV6
static gboolean
v6_v6_equal (struct sockaddr_in6 *a,
struct sockaddr_in6 *b)
{
return IN6_ARE_ADDR_EQUAL (&a->sin6_addr, &b->sin6_addr);
}
#endif
#define SA(__s) ((struct sockaddr *) __s)
#define SIN(__s) ((struct sockaddr_in *) __s)
#define SIN6(__s) ((struct sockaddr_in6 *) __s)
gboolean
gdm_address_equal (GdmAddress *a,
GdmAddress *b)
{
guint8 fam_a;
guint8 fam_b;
g_return_val_if_fail (a != NULL, FALSE);
g_return_val_if_fail (a->ss != NULL, FALSE);
g_return_val_if_fail (b != NULL, FALSE);
g_return_val_if_fail (b->ss != NULL, FALSE);
fam_a = a->ss->ss_family;
fam_b = b->ss->ss_family;
if (fam_a == AF_INET && fam_b == AF_INET) {
return v4_v4_equal (SIN (a->ss), SIN (b->ss));
}
#ifdef ENABLE_IPV6
else if (fam_a == AF_INET6 && fam_b == AF_INET6) {
return v6_v6_equal (SIN6 (a->ss), SIN6 (b->ss));
}
#endif
return FALSE;
}
/* for debugging */
static const char *
address_family_str (GdmAddress *address)
{
const char *str;
switch (address->ss->ss_family) {
case AF_INET:
str = "inet";
break;
case AF_INET6:
str = "inet6";
break;
case AF_UNIX:
str = "unix";
break;
case AF_UNSPEC:
str = "unspecified";
break;
default:
str = "unknown";
break;
}
return str;
}
static void
_gdm_address_debug (GdmAddress *address,
const char *hostname,
const char *host,
const char *port)
{
g_debug ("Address family:%d (%s) hostname:%s host:%s port:%s local:%d loopback:%d",
address->ss->ss_family,
address_family_str (address) ? address_family_str (address) : "(null)",
hostname ? hostname : "(null)",
host ? host : "(null)",
port ? port : "(null)",
gdm_address_is_local (address),
gdm_address_is_loopback (address));
}
void
gdm_address_debug (GdmAddress *address)
{
char *hostname;
char *host;
char *port;
gdm_address_get_hostname (address, &hostname);
gdm_address_get_numeric_info (address, &host, &port);
g_free (hostname);
g_free (host);
g_free (port);
}
gboolean
gdm_address_get_hostname (GdmAddress *address,
char **hostnamep)
{
char host [NI_MAXHOST];
int res;
gboolean ret;
g_return_val_if_fail (address != NULL, FALSE);
g_return_val_if_fail (address->ss != NULL, FALSE);
ret = FALSE;
host [0] = '\0';
res = getnameinfo ((const struct sockaddr *)address->ss,
(int) gdm_sockaddr_len (address->ss),
host, sizeof (host),
NULL, 0,
0);
if (res == 0) {
ret = TRUE;
goto done;
} else {
const char *err_msg;
err_msg = gai_strerror (res);
g_warning ("Unable to lookup hostname: %s",
err_msg ? err_msg : "(null)");
_gdm_address_debug (address, NULL, NULL, NULL);
}
/* try numeric? */
done:
if (hostnamep != NULL) {
*hostnamep = g_strdup (host);
}
return ret;
}
gboolean
gdm_address_get_numeric_info (GdmAddress *address,
char **hostp,
char **servp)
{
char host [NI_MAXHOST];
char serv [NI_MAXSERV];
int res;
gboolean ret;
g_return_val_if_fail (address != NULL, FALSE);
g_return_val_if_fail (address->ss != NULL, FALSE);
ret = FALSE;
host [0] = '\0';
serv [0] = '\0';
res = getnameinfo ((const struct sockaddr *)address->ss,
(int) gdm_sockaddr_len (address->ss),
host, sizeof (host),
serv, sizeof (serv),
NI_NUMERICHOST | NI_NUMERICSERV);
if (res != 0) {
const char *err_msg;
err_msg = gai_strerror (res);
g_warning ("Unable to lookup numeric info: %s",
err_msg ? err_msg : "(null)");
_gdm_address_debug (address, NULL, NULL, NULL);
} else {
ret = TRUE;
}
if (servp != NULL) {
if (g_str_has_prefix (serv, "::ffff:")) {
*servp = g_strdup (serv + 7);
} else {
*servp = g_strdup (serv);
}
}
if (hostp != NULL) {
if (g_str_has_prefix (host, "::ffff:")) {
*hostp = g_strdup (host + 7);
} else {
*hostp = g_strdup (host);
}
}
return ret;
}
gboolean
gdm_address_is_loopback (GdmAddress *address)
{
g_return_val_if_fail (address != NULL, FALSE);
g_return_val_if_fail (address->ss != NULL, FALSE);
switch (address->ss->ss_family){
#ifdef AF_INET6
case AF_INET6:
return IN6_IS_ADDR_LOOPBACK (&((struct sockaddr_in6 *)address->ss)->sin6_addr);
break;
#endif
case AF_INET:
return (INADDR_LOOPBACK == htonl (((struct sockaddr_in *)address->ss)->sin_addr.s_addr));
break;
default:
break;
}
return FALSE;
}
static void
add_local_siocgifconf (GList **list)
{
struct ifconf ifc;
struct ifreq ifreq;
struct ifreq *ifr;
struct ifreq *the_end;
int sock;
char buf[BUFSIZ];
if ((sock = socket (PF_INET, SOCK_DGRAM, 0)) < 0) {
perror ("socket");
return;
}
ifc.ifc_len = sizeof (buf);
ifc.ifc_buf = buf;
if (ioctl (sock, SIOCGIFCONF, (char *) &ifc) < 0) {
perror ("SIOCGIFCONF");
close (sock);
return;
}
/* Get IP address of each active IP network interface. */
the_end = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
for (ifr = ifc.ifc_req; ifr < the_end; ifr++) {
if (ifr->ifr_addr.sa_family == AF_INET) {
/* IP net interface */
ifreq = *ifr;
if (ioctl (sock, SIOCGIFFLAGS, (char *) &ifreq) < 0) {
perror("SIOCGIFFLAGS");
} else if (ifreq.ifr_flags & IFF_UP) { /* active interface */
if (ioctl (sock, SIOCGIFADDR, (char *) &ifreq) < 0) {
perror("SIOCGIFADDR");
} else {
GdmAddress *address;
address = gdm_address_new_from_sockaddr ((struct sockaddr *)&ifreq.ifr_addr,
sizeof (struct sockaddr));
gdm_address_debug (address);
*list = g_list_append (*list, address);
}
}
}
/* Support for variable-length addresses. */
#ifdef HAS_SA_LEN
ifr = (struct ifreq *) ((caddr_t) ifr
+ ifr->ifr_addr.sa_len - sizeof(struct sockaddr));
#endif
}
close (sock);
}
static void
add_local_addrinfo (GList **list)
{
char hostbuf[BUFSIZ];
struct addrinfo *result;
struct addrinfo *res;
struct addrinfo hints;
hostbuf[BUFSIZ-1] = '\0';
if (gethostname (hostbuf, BUFSIZ-1) != 0) {
g_debug ("%s: Could not get server hostname, using localhost", "gdm_peek_local_address_list");
snprintf (hostbuf, BUFSIZ-1, "localhost");
}
memset (&hints, 0, sizeof (hints));
hints.ai_family = AF_UNSPEC;
hints.ai_flags = AI_CANONNAME;
g_debug ("GdmAddress: looking up hostname: %s", hostbuf);
result = NULL;
if (getaddrinfo (hostbuf, NULL, &hints, &result) != 0) {
g_debug ("%s: Could not get address from hostname!", "gdm_peek_local_address_list");
return;
}
for (res = result; res != NULL; res = res->ai_next) {
GdmAddress *address;
g_debug ("family=%d sock_type=%d protocol=%d flags=0x%x canonname=%s\n",
res->ai_family,
res->ai_socktype,
res->ai_protocol,
res->ai_flags,
res->ai_canonname ? res->ai_canonname : "(null)");
address = gdm_address_new_from_sockaddr (res->ai_addr, res->ai_addrlen);
*list = g_list_append (*list, address);
}
if (result != NULL) {
freeaddrinfo (result);
result = NULL;
}
}
const GList *
gdm_address_peek_local_list (void)
{
static GList *list = NULL;
static time_t last_time = 0;
/* Don't check more then every 5 seconds */
if (last_time + 5 > time (NULL)) {
return list;
}
g_list_foreach (list, (GFunc)gdm_address_free, NULL);
g_list_free (list);
list = NULL;
last_time = time (NULL);
add_local_siocgifconf (&list);
add_local_addrinfo (&list);
return list;
}
gboolean
gdm_address_is_local (GdmAddress *address)
{
const GList *list;
if (gdm_address_is_loopback (address)) {
return TRUE;
}
list = gdm_address_peek_local_list ();
while (list != NULL) {
GdmAddress *addr = list->data;
if (gdm_address_equal (address, addr)) {
return TRUE;
}
list = list->next;
}
return FALSE;
}
/**
* gdm_address_copy:
* @address: A #GdmAddress.
*
* Duplicates @address.
*
* Return value: Duplicated @address or %NULL if @address was not valid.
**/
GdmAddress *
gdm_address_copy (GdmAddress *address)
{
GdmAddress *addr;
g_return_val_if_fail (address != NULL, NULL);
addr = g_new0 (GdmAddress, 1);
addr->ss = g_memdup (address->ss, sizeof (struct sockaddr_storage));
return addr;
}
/**
* gdm_address_free:
* @address: A #GdmAddress.
*
* Frees the memory allocated for @address.
**/
void
gdm_address_free (GdmAddress *address)
{
g_return_if_fail (address != NULL);
g_free (address->ss);
g_free (address);
}
|