10 typedef unsigned LONG_LONG ifa_flags_t;
11 #define PRIxIFAFLAGS PRI_LL_PREFIX"x" 12 #define IFAFLAGS2NUM(flags) ULL2NUM(flags) 14 typedef unsigned int ifa_flags_t;
15 #define PRIxIFAFLAGS "x" 16 #define IFAFLAGS2NUM(flags) UINT2NUM(flags) 21 typedef struct rb_ifaddr_tag rb_ifaddr_t;
22 typedef struct rb_ifaddr_root_tag rb_ifaddr_root_t;
24 struct rb_ifaddr_tag {
27 rb_ifaddr_root_t *root;
30 struct rb_ifaddr_root_tag {
36 static rb_ifaddr_root_t *
37 get_root(
const rb_ifaddr_t *ifaddr)
39 return (rb_ifaddr_root_t *)((
char *)&ifaddr[-ifaddr->ord] -
44 ifaddr_free(
void *ptr)
46 rb_ifaddr_t *ifaddr = ptr;
47 rb_ifaddr_root_t *root = get_root(ifaddr);
49 if (root->refcount == 0) {
56 ifaddr_memsize(
const void *ptr)
58 const rb_ifaddr_t *ifaddr;
59 const rb_ifaddr_root_t *root;
61 root = get_root(ifaddr);
62 return sizeof(rb_ifaddr_root_t) + (root->numifaddrs - 1) *
sizeof(rb_ifaddr_t);
67 {0, ifaddr_free, ifaddr_memsize,},
70 static inline rb_ifaddr_t *
71 check_ifaddr(
VALUE self)
77 get_ifaddr(
VALUE self)
79 rb_ifaddr_t *rifaddr = check_ifaddr(
self);
88 rsock_getifaddrs(
void)
93 rb_ifaddr_root_t *root;
109 root =
xmalloc(
sizeof(rb_ifaddr_root_t) + (numifaddrs-1) *
sizeof(rb_ifaddr_t));
111 root->numifaddrs = numifaddrs;
114 for (i = 0; i < numifaddrs; i++) {
115 root->ary[i].ord = i;
116 root->ary[i].ifaddr = ifa;
117 root->ary[i].root = root;
125 for (i = 1; i < numifaddrs; i++) {
142 ifaddr_name(
VALUE self)
144 rb_ifaddr_t *rifaddr = get_ifaddr(
self);
145 struct ifaddrs *ifa = rifaddr->ifaddr;
149 #ifdef HAVE_IF_NAMETOINDEX 158 ifaddr_ifindex(
VALUE self)
160 rb_ifaddr_t *rifaddr = get_ifaddr(
self);
161 struct ifaddrs *ifa = rifaddr->ifaddr;
162 unsigned int ifindex = if_nametoindex(ifa->
ifa_name);
169 #define ifaddr_ifindex rb_f_notimplement 180 ifaddr_flags(
VALUE self)
182 rb_ifaddr_t *rifaddr = get_ifaddr(
self);
183 struct ifaddrs *ifa = rifaddr->ifaddr;
196 ifaddr_addr(
VALUE self)
198 rb_ifaddr_t *rifaddr = get_ifaddr(
self);
199 struct ifaddrs *ifa = rifaddr->ifaddr;
214 ifaddr_netmask(
VALUE self)
216 rb_ifaddr_t *rifaddr = get_ifaddr(
self);
217 struct ifaddrs *ifa = rifaddr->ifaddr;
232 ifaddr_broadaddr(
VALUE self)
234 rb_ifaddr_t *rifaddr = get_ifaddr(
self);
235 struct ifaddrs *ifa = rifaddr->ifaddr;
250 ifaddr_dstaddr(
VALUE self)
252 rb_ifaddr_t *rifaddr = get_ifaddr(
self);
253 struct ifaddrs *ifa = rifaddr->ifaddr;
259 #ifdef HAVE_STRUCT_IF_DATA_IFI_VHID 269 ifaddr_vhid(
VALUE self)
271 rb_ifaddr_t *rifaddr = get_ifaddr(
self);
272 struct ifaddrs *ifa = rifaddr->ifaddr;
281 ifaddr_inspect_flags(ifa_flags_t flags,
VALUE result)
283 const char *sep =
" ";
284 #define INSPECT_BIT(bit, name) \ 285 if (flags & (bit)) { rb_str_catf(result, "%s" name, sep); flags &= ~(ifa_flags_t)(bit); sep = ","; } 287 INSPECT_BIT(IFF_UP,
"UP")
290 INSPECT_BIT(IFF_BROADCAST,
"BROADCAST")
293 INSPECT_BIT(IFF_DEBUG,
"DEBUG")
296 INSPECT_BIT(IFF_LOOPBACK,
"LOOPBACK")
298 #ifdef IFF_POINTOPOINT 299 INSPECT_BIT(IFF_POINTOPOINT,
"POINTOPOINT")
302 INSPECT_BIT(IFF_RUNNING,
"RUNNING")
305 INSPECT_BIT(IFF_NOARP,
"NOARP")
308 INSPECT_BIT(IFF_PROMISC,
"PROMISC")
310 #ifdef IFF_NOTRAILERS 311 INSPECT_BIT(IFF_NOTRAILERS,
"NOTRAILERS")
314 INSPECT_BIT(IFF_ALLMULTI,
"ALLMULTI")
317 INSPECT_BIT(IFF_SIMPLEX,
"SIMPLEX")
320 INSPECT_BIT(IFF_MASTER,
"MASTER")
323 INSPECT_BIT(IFF_SLAVE,
"SLAVE")
326 INSPECT_BIT(IFF_MULTICAST,
"MULTICAST")
329 INSPECT_BIT(IFF_PORTSEL,
"PORTSEL")
332 INSPECT_BIT(IFF_AUTOMEDIA,
"AUTOMEDIA")
335 INSPECT_BIT(IFF_DYNAMIC,
"DYNAMIC")
338 INSPECT_BIT(IFF_LOWER_UP,
"LOWER_UP")
341 INSPECT_BIT(IFF_DORMANT,
"DORMANT")
344 INSPECT_BIT(IFF_ECHO,
"ECHO")
348 rb_str_catf(result,
"%s%#"PRIxIFAFLAGS, sep, flags);
360 ifaddr_inspect(
VALUE self)
362 rb_ifaddr_t *rifaddr = get_ifaddr(
self);
366 ifa = rifaddr->ifaddr;
375 ifaddr_inspect_flags(ifa->
ifa_flags, result);
409 #ifdef HAVE_GETIFADDRS 453 return rsock_getifaddrs();
456 #define socket_s_getifaddrs rb_f_notimplement 462 #ifdef HAVE_GETIFADDRS 477 #ifdef HAVE_STRUCT_IF_DATA_IFI_VHID RUBY_EXTERN VALUE rb_cData
VALUE rsock_inspect_sockaddr(struct sockaddr *sockaddr_arg, socklen_t socklen, VALUE ret)
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
VALUE rsock_sockaddr_obj(struct sockaddr *addr, socklen_t len)
void rb_raise(VALUE exc, const char *fmt,...)
struct sockaddr * ifa_broadaddr
#define TypedData_Wrap_Struct(klass, data_type, sval)
VALUE rb_ary_push(VALUE ary, VALUE item)
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
void rsock_init_sockifaddr(void)
struct sockaddr * ifa_addr
#define socket_s_getifaddrs
void freeifaddrs(struct ifaddrs *)
socklen_t rsock_sockaddr_len(struct sockaddr *addr)
int getifaddrs(struct ifaddrs **)
VALUE rb_class_name(VALUE)
struct ifaddrs * ifa_next
VALUE rb_str_cat2(VALUE, const char *)
#define offsetof(p_type, field)
struct sockaddr * ifa_dstaddr
void rb_sys_fail(const char *mesg)
VALUE rb_str_new_cstr(const char *)
VALUE rb_str_catf(VALUE str, const char *format,...)
struct sockaddr * ifa_netmask
#define RTYPEDDATA_DATA(v)
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
VALUE rb_str_append(VALUE, VALUE)
void * rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type)