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
|
#include "rubysocket.h"
#ifdef HAVE_GETIFADDRS
#ifdef HAVE_LONG_LONG
typedef unsigned LONG_LONG ifa_flags_t;
#define PRIxIFAFLAGS PRI_LL_PREFIX"x"
#define IFAFLAGS2NUM(flags) ULL2NUM(flags)
#else
typedef unsigned int ifa_flags_t;
#define PRIxIFAFLAGS "x"
#define IFAFLAGS2NUM(flags) UINT2NUM(flags)
#endif
VALUE rb_cSockIfaddr;
typedef struct rb_ifaddr_tag rb_ifaddr_t;
typedef struct rb_ifaddr_root_tag rb_ifaddr_root_t;
struct rb_ifaddr_tag {
int ord;
struct ifaddrs *ifaddr;
rb_ifaddr_root_t *root;
};
struct rb_ifaddr_root_tag {
int refcount;
int numifaddrs;
rb_ifaddr_t ary[1];
};
static rb_ifaddr_root_t *
get_root(const rb_ifaddr_t *ifaddr)
{
return (rb_ifaddr_root_t *)((char *)&ifaddr[-ifaddr->ord] -
offsetof(rb_ifaddr_root_t, ary));
}
static void
ifaddr_mark(void *ptr)
{
}
static void
ifaddr_free(void *ptr)
{
rb_ifaddr_t *ifaddr = ptr;
rb_ifaddr_root_t *root = get_root(ifaddr);
root->refcount--;
if (root->refcount == 0) {
freeifaddrs(root->ary[0].ifaddr);
xfree(root);
}
}
static size_t
ifaddr_memsize(const void *ptr)
{
const rb_ifaddr_t *ifaddr;
const rb_ifaddr_root_t *root;
if (ptr == NULL)
return 0;
ifaddr = ptr;
root = get_root(ifaddr);
return sizeof(rb_ifaddr_root_t) + (root->numifaddrs - 1) * sizeof(rb_ifaddr_t);
}
static const rb_data_type_t ifaddr_type = {
"socket/ifaddr",
{ifaddr_mark, ifaddr_free, ifaddr_memsize,},
};
static inline rb_ifaddr_t *
check_ifaddr(VALUE self)
{
return rb_check_typeddata(self, &ifaddr_type);
}
static rb_ifaddr_t *
get_ifaddr(VALUE self)
{
rb_ifaddr_t *rifaddr = check_ifaddr(self);
if (!rifaddr) {
rb_raise(rb_eTypeError, "uninitialized ifaddr");
}
return rifaddr;
}
static VALUE
rsock_getifaddrs(void)
{
int ret;
int numifaddrs, i;
struct ifaddrs *ifaddrs, *ifa;
rb_ifaddr_root_t *root;
VALUE result;
ret = getifaddrs(&ifaddrs);
if (ret == -1)
rb_sys_fail("getifaddrs");
if (!ifaddrs) {
return rb_ary_new();
}
numifaddrs = 0;
for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next)
numifaddrs++;
root = xmalloc(sizeof(rb_ifaddr_root_t) + (numifaddrs-1) * sizeof(rb_ifaddr_t));
root->refcount = root->numifaddrs = numifaddrs;
ifa = ifaddrs;
for (i = 0; i < numifaddrs; i++) {
root->ary[i].ord = i;
root->ary[i].ifaddr = ifa;
root->ary[i].root = root;
ifa = ifa->ifa_next;
}
result = rb_ary_new2(numifaddrs);
for (i = 0; i < numifaddrs; i++) {
rb_ary_push(result, TypedData_Wrap_Struct(rb_cSockIfaddr, &ifaddr_type, &root->ary[i]));
}
return result;
}
static VALUE
ifaddr_name(VALUE self)
{
rb_ifaddr_t *rifaddr = get_ifaddr(self);
struct ifaddrs *ifa = rifaddr->ifaddr;
return rb_str_new_cstr(ifa->ifa_name);
}
#ifdef HAVE_IF_NAMETOINDEX
static VALUE
ifaddr_ifindex(VALUE self)
{
rb_ifaddr_t *rifaddr = get_ifaddr(self);
struct ifaddrs *ifa = rifaddr->ifaddr;
unsigned int ifindex = if_nametoindex(ifa->ifa_name);
if (ifindex == 0) {
rb_raise(rb_eArgError, "invalid interface name: %s", ifa->ifa_name);
}
return UINT2NUM(ifindex);
}
#else
#define ifaddr_ifindex rb_f_notimplement
#endif
static VALUE
ifaddr_flags(VALUE self)
{
rb_ifaddr_t *rifaddr = get_ifaddr(self);
struct ifaddrs *ifa = rifaddr->ifaddr;
return IFAFLAGS2NUM(ifa->ifa_flags);
}
static VALUE
ifaddr_addr(VALUE self)
{
rb_ifaddr_t *rifaddr = get_ifaddr(self);
struct ifaddrs *ifa = rifaddr->ifaddr;
if (ifa->ifa_addr)
return rsock_sockaddr_obj(ifa->ifa_addr, rsock_sockaddr_len(ifa->ifa_addr));
return Qnil;
}
static VALUE
ifaddr_netmask(VALUE self)
{
rb_ifaddr_t *rifaddr = get_ifaddr(self);
struct ifaddrs *ifa = rifaddr->ifaddr;
if (ifa->ifa_netmask)
return rsock_sockaddr_obj(ifa->ifa_netmask, rsock_sockaddr_len(ifa->ifa_netmask));
return Qnil;
}
static VALUE
ifaddr_broadaddr(VALUE self)
{
rb_ifaddr_t *rifaddr = get_ifaddr(self);
struct ifaddrs *ifa = rifaddr->ifaddr;
if ((ifa->ifa_flags & IFF_BROADCAST) && ifa->ifa_broadaddr)
return rsock_sockaddr_obj(ifa->ifa_broadaddr, rsock_sockaddr_len(ifa->ifa_broadaddr));
return Qnil;
}
static VALUE
ifaddr_dstaddr(VALUE self)
{
rb_ifaddr_t *rifaddr = get_ifaddr(self);
struct ifaddrs *ifa = rifaddr->ifaddr;
if ((ifa->ifa_flags & IFF_POINTOPOINT) && ifa->ifa_dstaddr)
return rsock_sockaddr_obj(ifa->ifa_dstaddr, rsock_sockaddr_len(ifa->ifa_dstaddr));
return Qnil;
}
static void
ifaddr_inspect_flags(ifa_flags_t flags, VALUE result)
{
const char *sep = " ";
#define INSPECT_BIT(bit, name) \
if (flags & (bit)) { rb_str_catf(result, "%s" name, sep); flags &= ~(ifa_flags_t)(bit); sep = ","; }
#ifdef IFF_UP
INSPECT_BIT(IFF_UP, "UP")
#endif
#ifdef IFF_BROADCAST
INSPECT_BIT(IFF_BROADCAST, "BROADCAST")
#endif
#ifdef IFF_DEBUG
INSPECT_BIT(IFF_DEBUG, "DEBUG")
#endif
#ifdef IFF_LOOPBACK
INSPECT_BIT(IFF_LOOPBACK, "LOOPBACK")
#endif
#ifdef IFF_POINTOPOINT
INSPECT_BIT(IFF_POINTOPOINT, "POINTOPOINT")
#endif
#ifdef IFF_RUNNING
INSPECT_BIT(IFF_RUNNING, "RUNNING")
#endif
#ifdef IFF_NOARP
INSPECT_BIT(IFF_NOARP, "NOARP")
#endif
#ifdef IFF_PROMISC
INSPECT_BIT(IFF_PROMISC, "PROMISC")
#endif
#ifdef IFF_NOTRAILERS
INSPECT_BIT(IFF_NOTRAILERS, "NOTRAILERS")
#endif
#ifdef IFF_ALLMULTI
INSPECT_BIT(IFF_ALLMULTI, "ALLMULTI")
#endif
#ifdef IFF_SIMPLEX
INSPECT_BIT(IFF_SIMPLEX, "SIMPLEX")
#endif
#ifdef IFF_MASTER
INSPECT_BIT(IFF_MASTER, "MASTER")
#endif
#ifdef IFF_SLAVE
INSPECT_BIT(IFF_SLAVE, "SLAVE")
#endif
#ifdef IFF_MULTICAST
INSPECT_BIT(IFF_MULTICAST, "MULTICAST")
#endif
#ifdef IFF_PORTSEL
INSPECT_BIT(IFF_PORTSEL, "PORTSEL")
#endif
#ifdef IFF_AUTOMEDIA
INSPECT_BIT(IFF_AUTOMEDIA, "AUTOMEDIA")
#endif
#ifdef IFF_DYNAMIC
INSPECT_BIT(IFF_DYNAMIC, "DYNAMIC")
#endif
#ifdef IFF_LOWER_UP
INSPECT_BIT(IFF_LOWER_UP, "LOWER_UP")
#endif
#ifdef IFF_DORMANT
INSPECT_BIT(IFF_DORMANT, "DORMANT")
#endif
#ifdef IFF_ECHO
INSPECT_BIT(IFF_ECHO, "ECHO")
#endif
#undef INSPECT_BIT
if (flags) {
rb_str_catf(result, "%s%#"PRIxIFAFLAGS, sep, flags);
}
}
static VALUE
ifaddr_inspect(VALUE self)
{
rb_ifaddr_t *rifaddr = get_ifaddr(self);
struct ifaddrs *ifa;
VALUE result;
ifa = rifaddr->ifaddr;
result = rb_str_new_cstr("#<");
rb_str_append(result, rb_class_name(CLASS_OF(self)));
rb_str_cat2(result, " ");
rb_str_cat2(result, ifa->ifa_name);
if (ifa->ifa_flags)
ifaddr_inspect_flags(ifa->ifa_flags, result);
if (ifa->ifa_addr) {
rb_str_cat2(result, " ");
rsock_inspect_sockaddr(ifa->ifa_addr,
rsock_sockaddr_len(ifa->ifa_addr),
result);
}
if (ifa->ifa_netmask) {
rb_str_cat2(result, " netmask=");
rsock_inspect_sockaddr(ifa->ifa_netmask,
rsock_sockaddr_len(ifa->ifa_netmask),
result);
}
if ((ifa->ifa_flags & IFF_BROADCAST) && ifa->ifa_broadaddr) {
rb_str_cat2(result, " broadcast=");
rsock_inspect_sockaddr(ifa->ifa_broadaddr,
rsock_sockaddr_len(ifa->ifa_broadaddr),
result);
}
if ((ifa->ifa_flags & IFF_POINTOPOINT) && ifa->ifa_dstaddr) {
rb_str_cat2(result, " dstaddr=");
rsock_inspect_sockaddr(ifa->ifa_dstaddr,
rsock_sockaddr_len(ifa->ifa_dstaddr),
result);
}
rb_str_cat2(result, ">");
return result;
}
#endif
#ifdef HAVE_GETIFADDRS
static VALUE
socket_s_getifaddrs(VALUE self)
{
return rsock_getifaddrs();
}
#else
#define socket_s_getifaddrs rb_f_notimplement
#endif
void
rsock_init_sockifaddr(void)
{
#ifdef HAVE_GETIFADDRS
rb_cSockIfaddr = rb_define_class_under(rb_cSocket, "Ifaddr", rb_cData);
rb_define_method(rb_cSockIfaddr, "inspect", ifaddr_inspect, 0);
rb_define_method(rb_cSockIfaddr, "name", ifaddr_name, 0);
rb_define_method(rb_cSockIfaddr, "ifindex", ifaddr_ifindex, 0);
rb_define_method(rb_cSockIfaddr, "flags", ifaddr_flags, 0);
rb_define_method(rb_cSockIfaddr, "addr", ifaddr_addr, 0);
rb_define_method(rb_cSockIfaddr, "netmask", ifaddr_netmask, 0);
rb_define_method(rb_cSockIfaddr, "broadaddr", ifaddr_broadaddr, 0);
rb_define_method(rb_cSockIfaddr, "dstaddr", ifaddr_dstaddr, 0);
#endif
rb_define_singleton_method(rb_cSocket, "getifaddrs", socket_s_getifaddrs, 0);
}
|