Ruby: ext/socket/ifaddr.c Source File

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

ifaddr.c
Go to the documentation of this file.
1#include "rubysocket.h"
2
3#ifdef HAVE_GETIFADDRS
4
5/*
6 * ifa_flags is usually unsigned int.
7 * However it is uint64_t on SunOS 5.11 (OpenIndiana).
8 */
9#ifdef HAVE_LONG_LONG
10typedef unsigned LONG_LONG ifa_flags_t;
11#define PRIxIFAFLAGS PRI_LL_PREFIX"x"
12#define IFAFLAGS2NUM(flags) ULL2NUM(flags)
13#else
14typedef unsigned int ifa_flags_t;
15#define PRIxIFAFLAGS "x"
16#define IFAFLAGS2NUM(flags) UINT2NUM(flags)
17#endif
18
19 VALUE rb_cSockIfaddr;
20
21typedef struct rb_ifaddr_tag rb_ifaddr_t;
22typedef struct rb_ifaddr_root_tag rb_ifaddr_root_t;
23
24struct rb_ifaddr_tag {
25 int ord;
26 struct ifaddrs *ifaddr;
27};
28
29struct rb_ifaddr_root_tag {
30 int refcount;
31 int numifaddrs;
32 rb_ifaddr_t ary[1];
33};
34
35static rb_ifaddr_root_t *
36get_root(const rb_ifaddr_t *ifaddr)
37{
38 return (rb_ifaddr_root_t *)((char *)&ifaddr[-ifaddr->ord] -
39 offsetof(rb_ifaddr_root_t, ary));
40}
41
42static void
43ifaddr_free(void *ptr)
44{
45 rb_ifaddr_t *ifaddr = ptr;
46 rb_ifaddr_root_t *root = get_root(ifaddr);
47 root->refcount--;
48 if (root->refcount == 0) {
49 freeifaddrs(root->ary[0].ifaddr);
50 xfree(root);
51 }
52}
53
54static size_t
55ifaddr_memsize(const void *ptr)
56{
57 size_t size = offsetof(rb_ifaddr_root_t, ary);
58 const rb_ifaddr_t *ifaddr;
59 ifaddr = ptr;
60 if (ifaddr->ord == 0) size = sizeof(rb_ifaddr_root_t);
61 size += sizeof(struct ifaddrs);
62 return size;
63}
64
65static const rb_data_type_t ifaddr_type = {
66 "socket/ifaddr",
67 {0, ifaddr_free, ifaddr_memsize,},
68};
69
70static inline rb_ifaddr_t *
71check_ifaddr(VALUE self)
72{
73 return rb_check_typeddata(self, &ifaddr_type);
74}
75
76static rb_ifaddr_t *
77get_ifaddr(VALUE self)
78{
79 rb_ifaddr_t *rifaddr = check_ifaddr(self);
80
81 if (!rifaddr) {
82 rb_raise(rb_eTypeError, "uninitialized ifaddr");
83 }
84 return rifaddr;
85}
86
87static struct ifaddrs *
88get_ifaddrs(VALUE self)
89{
90 return get_ifaddr(self)->ifaddr;
91}
92
93static VALUE
94rsock_getifaddrs(void)
95{
96 int ret;
97 int numifaddrs, i;
98 struct ifaddrs *ifaddrs, *ifa;
99 rb_ifaddr_root_t *root;
100 VALUE result, addr;
101
102 ret = getifaddrs(&ifaddrs);
103 if (ret == -1)
104 rb_sys_fail("getifaddrs");
105
106 if (!ifaddrs) {
107 return rb_ary_new();
108 }
109
110 numifaddrs = 0;
111 for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next)
112 numifaddrs++;
113
114 addr = TypedData_Wrap_Struct(rb_cSockIfaddr, &ifaddr_type, 0);
115 root = xmalloc(offsetof(rb_ifaddr_root_t, ary) + numifaddrs * sizeof(rb_ifaddr_t));
116 root->refcount = 0;
117 root->numifaddrs = numifaddrs;
118
119 ifa = ifaddrs;
120 for (i = 0; i < numifaddrs; i++) {
121 root->ary[i].ord = i;
122 root->ary[i].ifaddr = ifa;
123 ifa = ifa->ifa_next;
124 }
125 RTYPEDDATA_DATA(addr) = &root->ary[0];
126 root->refcount++;
127
128 result = rb_ary_new2(numifaddrs);
129 rb_ary_push(result, addr);
130 for (i = 1; i < numifaddrs; i++) {
131 addr = TypedData_Wrap_Struct(rb_cSockIfaddr, &ifaddr_type, &root->ary[i]);
132 root->refcount++;
133 rb_ary_push(result, addr);
134 }
135
136 return result;
137}
138
139/*
140 * call-seq:
141 * ifaddr.name => string
142 *
143 * Returns the interface name of _ifaddr_.
144 */
145
146static VALUE
147ifaddr_name(VALUE self)
148{
149 struct ifaddrs *ifa = get_ifaddrs(self);
150 return rb_str_new_cstr(ifa->ifa_name);
151}
152
153#ifdef HAVE_IF_NAMETOINDEX
154/*
155 * call-seq:
156 * ifaddr.ifindex => integer
157 *
158 * Returns the interface index of _ifaddr_.
159 */
160
161static VALUE
162ifaddr_ifindex(VALUE self)
163{
164 struct ifaddrs *ifa = get_ifaddrs(self);
165 unsigned int ifindex = if_nametoindex(ifa->ifa_name);
166 if (ifindex == 0) {
167 rb_raise(rb_eArgError, "invalid interface name: %s", ifa->ifa_name);
168 }
169 return UINT2NUM(ifindex);
170}
171#else
172#define ifaddr_ifindex rb_f_notimplement
173#endif
174
175/*
176 * call-seq:
177 * ifaddr.flags => integer
178 *
179 * Returns the flags of _ifaddr_.
180 */
181
182static VALUE
183ifaddr_flags(VALUE self)
184{
185 struct ifaddrs *ifa = get_ifaddrs(self);
186 return IFAFLAGS2NUM(ifa->ifa_flags);
187}
188
189/*
190 * call-seq:
191 * ifaddr.addr => addrinfo
192 *
193 * Returns the address of _ifaddr_.
194 * nil is returned if address is not available in _ifaddr_.
195 */
196
197static VALUE
198ifaddr_addr(VALUE self)
199{
200 struct ifaddrs *ifa = get_ifaddrs(self);
201 if (ifa->ifa_addr)
202 return rsock_sockaddr_obj(ifa->ifa_addr, rsock_sockaddr_len(ifa->ifa_addr));
203 return Qnil;
204}
205
206/*
207 * call-seq:
208 * ifaddr.netmask => addrinfo
209 *
210 * Returns the netmask address of _ifaddr_.
211 * nil is returned if netmask is not available in _ifaddr_.
212 */
213
214static VALUE
215ifaddr_netmask(VALUE self)
216{
217 struct ifaddrs *ifa = get_ifaddrs(self);
218 if (ifa->ifa_netmask)
219 return rsock_sockaddr_obj(ifa->ifa_netmask, rsock_sockaddr_len(ifa->ifa_netmask));
220 return Qnil;
221}
222
223/*
224 * call-seq:
225 * ifaddr.broadaddr => addrinfo
226 *
227 * Returns the broadcast address of _ifaddr_.
228 * nil is returned if the flags doesn't have IFF_BROADCAST.
229 */
230
231static VALUE
232ifaddr_broadaddr(VALUE self)
233{
234 struct ifaddrs *ifa = get_ifaddrs(self);
235 if ((ifa->ifa_flags & IFF_BROADCAST) && ifa->ifa_broadaddr)
236 return rsock_sockaddr_obj(ifa->ifa_broadaddr, rsock_sockaddr_len(ifa->ifa_broadaddr));
237 return Qnil;
238}
239
240/*
241 * call-seq:
242 * ifaddr.dstaddr => addrinfo
243 *
244 * Returns the destination address of _ifaddr_.
245 * nil is returned if the flags doesn't have IFF_POINTOPOINT.
246 */
247
248static VALUE
249ifaddr_dstaddr(VALUE self)
250{
251 struct ifaddrs *ifa = get_ifaddrs(self);
252 if ((ifa->ifa_flags & IFF_POINTOPOINT) && ifa->ifa_dstaddr)
253 return rsock_sockaddr_obj(ifa->ifa_dstaddr, rsock_sockaddr_len(ifa->ifa_dstaddr));
254 return Qnil;
255}
256
257#ifdef HAVE_STRUCT_IF_DATA_IFI_VHID
258/*
259 * call-seq:
260 * ifaddr.vhid => Integer
261 *
262 * Returns the vhid address of _ifaddr_.
263 * nil is returned if there is no vhid.
264 */
265
266static VALUE
267ifaddr_vhid(VALUE self)
268{
269 struct ifaddrs *ifa = get_ifaddrs(self);
270 if (ifa->ifa_data)
271 return (INT2FIX(((struct if_data*)ifa->ifa_data)->ifi_vhid));
272 else
273 return Qnil;
274}
275#endif
276
277static void
278ifaddr_inspect_flags(ifa_flags_t flags, VALUE result)
279{
280 const char *sep = " ";
281#define INSPECT_BIT(bit, name) \
282 if (flags & (bit)) { rb_str_catf(result, "%s" name, sep); flags &= ~(ifa_flags_t)(bit); sep = ","; }
283#ifdef IFF_UP
284 INSPECT_BIT(IFF_UP, "UP")
285#endif
286#ifdef IFF_BROADCAST
287 INSPECT_BIT(IFF_BROADCAST, "BROADCAST")
288#endif
289#ifdef IFF_DEBUG
290 INSPECT_BIT(IFF_DEBUG, "DEBUG")
291#endif
292#ifdef IFF_LOOPBACK
293 INSPECT_BIT(IFF_LOOPBACK, "LOOPBACK")
294#endif
295#ifdef IFF_POINTOPOINT
296 INSPECT_BIT(IFF_POINTOPOINT, "POINTOPOINT")
297#endif
298#ifdef IFF_RUNNING
299 INSPECT_BIT(IFF_RUNNING, "RUNNING")
300#endif
301#ifdef IFF_NOARP
302 INSPECT_BIT(IFF_NOARP, "NOARP")
303#endif
304#ifdef IFF_PROMISC
305 INSPECT_BIT(IFF_PROMISC, "PROMISC")
306#endif
307#ifdef IFF_NOTRAILERS
308 INSPECT_BIT(IFF_NOTRAILERS, "NOTRAILERS")
309#endif
310#ifdef IFF_ALLMULTI
311 INSPECT_BIT(IFF_ALLMULTI, "ALLMULTI")
312#endif
313#ifdef IFF_SIMPLEX
314 INSPECT_BIT(IFF_SIMPLEX, "SIMPLEX")
315#endif
316#ifdef IFF_MASTER
317 INSPECT_BIT(IFF_MASTER, "MASTER")
318#endif
319#ifdef IFF_SLAVE
320 INSPECT_BIT(IFF_SLAVE, "SLAVE")
321#endif
322#ifdef IFF_MULTICAST
323 INSPECT_BIT(IFF_MULTICAST, "MULTICAST")
324#endif
325#ifdef IFF_PORTSEL
326 INSPECT_BIT(IFF_PORTSEL, "PORTSEL")
327#endif
328#ifdef IFF_AUTOMEDIA
329 INSPECT_BIT(IFF_AUTOMEDIA, "AUTOMEDIA")
330#endif
331#ifdef IFF_DYNAMIC
332 INSPECT_BIT(IFF_DYNAMIC, "DYNAMIC")
333#endif
334#ifdef IFF_LOWER_UP
335 INSPECT_BIT(IFF_LOWER_UP, "LOWER_UP")
336#endif
337#ifdef IFF_DORMANT
338 INSPECT_BIT(IFF_DORMANT, "DORMANT")
339#endif
340#ifdef IFF_ECHO
341 INSPECT_BIT(IFF_ECHO, "ECHO")
342#endif
343#undef INSPECT_BIT
344 if (flags) {
345 rb_str_catf(result, "%s%#"PRIxIFAFLAGS, sep, flags);
346 }
347}
348
349/*
350 * call-seq:
351 * ifaddr.inspect => string
352 *
353 * Returns a string to show contents of _ifaddr_.
354 */
355
356static VALUE
357ifaddr_inspect(VALUE self)
358{
359 struct ifaddrs *ifa = get_ifaddrs(self);
360 VALUE result;
361
362 result = rb_str_new_cstr("#<");
363
364 rb_str_append(result, rb_class_name(CLASS_OF(self)));
365 rb_str_cat2(result, " ");
366 rb_str_cat2(result, ifa->ifa_name);
367
368 if (ifa->ifa_flags)
369 ifaddr_inspect_flags(ifa->ifa_flags, result);
370
371 if (ifa->ifa_addr) {
372 rb_str_cat2(result, " ");
373 rsock_inspect_sockaddr(ifa->ifa_addr,
374 rsock_sockaddr_len(ifa->ifa_addr),
375 result);
376 }
377 if (ifa->ifa_netmask) {
378 rb_str_cat2(result, " netmask=");
379 rsock_inspect_sockaddr(ifa->ifa_netmask,
380 rsock_sockaddr_len(ifa->ifa_netmask),
381 result);
382 }
383
384 if ((ifa->ifa_flags & IFF_BROADCAST) && ifa->ifa_broadaddr) {
385 rb_str_cat2(result, " broadcast=");
386 rsock_inspect_sockaddr(ifa->ifa_broadaddr,
387 rsock_sockaddr_len(ifa->ifa_broadaddr),
388 result);
389 }
390
391 if ((ifa->ifa_flags & IFF_POINTOPOINT) && ifa->ifa_dstaddr) {
392 rb_str_cat2(result, " dstaddr=");
393 rsock_inspect_sockaddr(ifa->ifa_dstaddr,
394 rsock_sockaddr_len(ifa->ifa_dstaddr),
395 result);
396 }
397
398 rb_str_cat2(result, ">");
399 return result;
400}
401#endif
402
403#ifdef HAVE_GETIFADDRS
404/*
405 * call-seq:
406 * Socket.getifaddrs => [ifaddr1, ...]
407 *
408 * Returns an array of interface addresses.
409 * An element of the array is an instance of Socket::Ifaddr.
410 *
411 * This method can be used to find multicast-enabled interfaces:
412 *
413 * pp Socket.getifaddrs.reject {|ifaddr|
414 * !ifaddr.addr.ip? || (ifaddr.flags & Socket::IFF_MULTICAST == 0)
415 * }.map {|ifaddr| [ifaddr.name, ifaddr.ifindex, ifaddr.addr] }
416 * #=> [["eth0", 2, #<Addrinfo: 221.186.184.67>],
417 * # ["eth0", 2, #<Addrinfo: fe80::216:3eff:fe95:88bb%eth0>]]
418 *
419 * Example result on GNU/Linux:
420 * pp Socket.getifaddrs
421 * #=> [#<Socket::Ifaddr lo UP,LOOPBACK,RUNNING,0x10000 PACKET[protocol=0 lo hatype=772 HOST hwaddr=00:00:00:00:00:00]>,
422 * # #<Socket::Ifaddr eth0 UP,BROADCAST,RUNNING,MULTICAST,0x10000 PACKET[protocol=0 eth0 hatype=1 HOST hwaddr=00:16:3e:95:88:bb] broadcast=PACKET[protocol=0 eth0 hatype=1 HOST hwaddr=ff:ff:ff:ff:ff:ff]>,
423 * # #<Socket::Ifaddr sit0 NOARP PACKET[protocol=0 sit0 hatype=776 HOST hwaddr=00:00:00:00]>,
424 * # #<Socket::Ifaddr lo UP,LOOPBACK,RUNNING,0x10000 127.0.0.1 netmask=255.0.0.0>,
425 * # #<Socket::Ifaddr eth0 UP,BROADCAST,RUNNING,MULTICAST,0x10000 221.186.184.67 netmask=255.255.255.240 broadcast=221.186.184.79>,
426 * # #<Socket::Ifaddr lo UP,LOOPBACK,RUNNING,0x10000 ::1 netmask=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>,
427 * # #<Socket::Ifaddr eth0 UP,BROADCAST,RUNNING,MULTICAST,0x10000 fe80::216:3eff:fe95:88bb%eth0 netmask=ffff:ffff:ffff:ffff::>]
428 *
429 * Example result on FreeBSD:
430 * pp Socket.getifaddrs
431 * #=> [#<Socket::Ifaddr usbus0 UP,0x10000 LINK[usbus0]>,
432 * # #<Socket::Ifaddr re0 UP,BROADCAST,RUNNING,MULTICAST,0x800 LINK[re0 3a:d0:40:9a:fe:e8]>,
433 * # #<Socket::Ifaddr re0 UP,BROADCAST,RUNNING,MULTICAST,0x800 10.250.10.18 netmask=255.255.255.? (7 bytes for 16 bytes sockaddr_in) broadcast=10.250.10.255>,
434 * # #<Socket::Ifaddr re0 UP,BROADCAST,RUNNING,MULTICAST,0x800 fe80:2::38d0:40ff:fe9a:fee8 netmask=ffff:ffff:ffff:ffff::>,
435 * # #<Socket::Ifaddr re0 UP,BROADCAST,RUNNING,MULTICAST,0x800 2001:2e8:408:10::12 netmask=UNSPEC>,
436 * # #<Socket::Ifaddr plip0 POINTOPOINT,MULTICAST,0x800 LINK[plip0]>,
437 * # #<Socket::Ifaddr lo0 UP,LOOPBACK,RUNNING,MULTICAST LINK[lo0]>,
438 * # #<Socket::Ifaddr lo0 UP,LOOPBACK,RUNNING,MULTICAST ::1 netmask=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>,
439 * # #<Socket::Ifaddr lo0 UP,LOOPBACK,RUNNING,MULTICAST fe80:4::1 netmask=ffff:ffff:ffff:ffff::>,
440 * # #<Socket::Ifaddr lo0 UP,LOOPBACK,RUNNING,MULTICAST 127.0.0.1 netmask=255.?.?.? (5 bytes for 16 bytes sockaddr_in)>]
441 *
442 */
443
444static VALUE
445 socket_s_getifaddrs(VALUE self)
446{
447 return rsock_getifaddrs();
448}
449#else
450#define socket_s_getifaddrs rb_f_notimplement
451#endif
452
453void
454rsock_init_sockifaddr(void)
455{
456#ifdef HAVE_GETIFADDRS
457 /*
458 * Document-class: Socket::Ifaddr
459 *
460 * Socket::Ifaddr represents a result of getifaddrs() function.
461 */
462 rb_cSockIfaddr = rb_define_class_under(rb_cSocket, "Ifaddr", rb_cObject);
463 rb_undef_alloc_func(rb_cSockIfaddr);
464 rb_define_method(rb_cSockIfaddr, "inspect", ifaddr_inspect, 0);
465 rb_define_method(rb_cSockIfaddr, "name", ifaddr_name, 0);
466 rb_define_method(rb_cSockIfaddr, "ifindex", ifaddr_ifindex, 0);
467 rb_define_method(rb_cSockIfaddr, "flags", ifaddr_flags, 0);
468 rb_define_method(rb_cSockIfaddr, "addr", ifaddr_addr, 0);
469 rb_define_method(rb_cSockIfaddr, "netmask", ifaddr_netmask, 0);
470 rb_define_method(rb_cSockIfaddr, "broadaddr", ifaddr_broadaddr, 0);
471 rb_define_method(rb_cSockIfaddr, "dstaddr", ifaddr_dstaddr, 0);
472#ifdef HAVE_STRUCT_IF_DATA_IFI_VHID
473 rb_define_method(rb_cSockIfaddr, "vhid", ifaddr_vhid, 0);
474#endif
475#endif
476
477 rb_define_singleton_method(rb_cSocket, "getifaddrs", socket_s_getifaddrs, 0);
478}
offsetof
#define offsetof(p_type, field)
Definition: addrinfo.h:186
rb_ary_push
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:1301
rb_ary_new
VALUE rb_ary_new(void)
Definition: array.c:749
rb_define_method
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
Definition: cxxanyargs.hpp:653
rb_define_singleton_method
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
Definition: cxxanyargs.hpp:668
ptr
struct RIMemo * ptr
Definition: debug.c:88
root
int root
Definition: enough.c:226
CLASS_OF
#define CLASS_OF
Definition: globals.h:153
rb_define_class_under
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:797
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2917
rb_eTypeError
VALUE rb_eTypeError
Definition: error.c:1057
rb_check_typeddata
void * rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type)
Definition: error.c:1024
rb_eArgError
VALUE rb_eArgError
Definition: error.c:1058
rb_sys_fail
void rb_sys_fail(const char *mesg)
Definition: error.c:3041
rb_cObject
VALUE rb_cObject
Object class.
Definition: object.c:49
rsock_init_sockifaddr
void rsock_init_sockifaddr(void)
Definition: ifaddr.c:454
socket_s_getifaddrs
#define socket_s_getifaddrs
Definition: ifaddr.c:450
rb_ary_new2
#define rb_ary_new2
Definition: array.h:72
rb_str_cat2
#define rb_str_cat2
Definition: string.h:285
rb_str_append
VALUE rb_str_append(VALUE, VALUE)
Definition: string.c:3118
rb_str_new_cstr
#define rb_str_new_cstr(str)
Definition: string.h:219
rb_class_name
VALUE rb_class_name(VALUE)
Definition: variable.c:293
rb_undef_alloc_func
void rb_undef_alloc_func(VALUE)
Definition: vm_method.c:954
rb_cSocket
VALUE rb_cSocket
Definition: init.c:26
UINT2NUM
#define UINT2NUM
Definition: int.h:46
size
voidpf void uLong size
Definition: ioapi.h:138
INT2FIX
#define INT2FIX
Definition: long.h:48
rsock_inspect_sockaddr
VALUE rsock_inspect_sockaddr(struct sockaddr *sockaddr_arg, socklen_t socklen, VALUE ret)
Definition: raddrinfo.c:1148
NULL
#define NULL
Definition: regenc.h:69
RTYPEDDATA_DATA
#define RTYPEDDATA_DATA(v)
Definition: rtypeddata.h:47
TypedData_Wrap_Struct
#define TypedData_Wrap_Struct(klass, data_type, sval)
Definition: rtypeddata.h:101
rubysocket.h
rsock_sockaddr_obj
VALUE rsock_sockaddr_obj(struct sockaddr *addr, socklen_t len)
rsock_sockaddr_len
socklen_t rsock_sockaddr_len(struct sockaddr *addr)
Qnil
#define Qnil
Definition: special_consts.h:51
rb_str_catf
VALUE rb_str_catf(VALUE, const char *,...)
Definition: sprintf.c:1243
ifaddrs
Definition: win32.h:233
ifaddrs::ifa_flags
u_int ifa_flags
Definition: win32.h:236
ifaddrs::ifa_addr
struct sockaddr * ifa_addr
Definition: win32.h:237
ifaddrs::ifa_name
char * ifa_name
Definition: win32.h:235
ifaddrs::ifa_next
struct ifaddrs * ifa_next
Definition: win32.h:234
ifaddrs::ifa_data
void * ifa_data
Definition: win32.h:241
ifaddrs::ifa_dstaddr
struct sockaddr * ifa_dstaddr
Definition: win32.h:240
ifaddrs::ifa_broadaddr
struct sockaddr * ifa_broadaddr
Definition: win32.h:239
ifaddrs::ifa_netmask
struct sockaddr * ifa_netmask
Definition: win32.h:238
rb_data_type_struct
Definition: rtypeddata.h:70
VALUE
unsigned long VALUE
Definition: value.h:38
freeifaddrs
void freeifaddrs(struct ifaddrs *)
Definition: win32.c:4232
getifaddrs
int getifaddrs(struct ifaddrs **)
Definition: win32.c:4145
xfree
#define xfree
Definition: xmalloc.h:49
xmalloc
#define xmalloc
Definition: xmalloc.h:44