getifaddrs.patch

Akira Tanaka, 05/04/2013 04:57 PM

Download (31.6 KB)

View differences:

ext/socket/extconf.rb
302 302
  netinet/tcp.h
303 303
  netinet/udp.h
304 304
  arpa/inet.h
305
  netpacket/packet.h
306
  net/ethernet.h
305 307
  sys/un.h
306 308
  ifaddrs.h
307 309
  sys/ioctl.h
......
310 312
  sys/param.h
311 313
  sys/ucred.h
312 314
  ucred.h
315
  net/if_dl.h
313 316
  arpa/nameser.h
314 317
  resolv.h
315 318
].each {|h|
......
325 328
  have_struct_member("struct sockaddr_un", "sun_len", headers) # 4.4BSD
326 329
end
327 330

  
331
have_type("struct sockaddr_dl", headers) # AF_LINK address.  4.4BSD since Net2
332

  
328 333
have_type("struct sockaddr_storage", headers)
329 334

  
330 335
have_type("struct addrinfo", headers)
......
550 555
    "unixserver.#{$OBJEXT}",
551 556
    "option.#{$OBJEXT}",
552 557
    "ancdata.#{$OBJEXT}",
553
    "raddrinfo.#{$OBJEXT}"
558
    "raddrinfo.#{$OBJEXT}",
559
    "ifaddr.#{$OBJEXT}"
554 560
  ]
555 561

  
556 562
  if getaddr_info_ok == :wide
ext/socket/ifaddr.c
1
#include "rubysocket.h"
2

  
3
VALUE rb_cSockIfaddr;
4

  
5
typedef struct rb_ifaddr_tag rb_ifaddr_t;
6
typedef struct rb_ifaddr_root_tag rb_ifaddr_root_t;
7

  
8
struct rb_ifaddr_tag {
9
    int ord;
10
    struct ifaddrs *ifaddr;
11
    rb_ifaddr_root_t *root;
12
};
13

  
14
struct rb_ifaddr_root_tag {
15
    int refcount;
16
    int numifaddrs;
17
    rb_ifaddr_t ary[1];
18
};
19

  
20
static rb_ifaddr_root_t *
21
get_root(const rb_ifaddr_t *ifaddr)
22
{
23
    return (rb_ifaddr_root_t *)((char *)&ifaddr[-ifaddr->ord] -
24
                                offsetof(rb_ifaddr_root_t, ary));
25
}
26

  
27
static void
28
ifaddr_mark(void *ptr)
29
{
30
}
31

  
32
static void
33
ifaddr_free(void *ptr)
34
{
35
    rb_ifaddr_t *ifaddr = ptr;
36
    rb_ifaddr_root_t *root = get_root(ifaddr);
37
    root->refcount--;
38
    if (root->refcount == 0) {
39
        freeifaddrs(root->ary[0].ifaddr);
40
        xfree(root);
41
    }
42
}
43

  
44
static size_t
45
ifaddr_memsize(const void *ptr)
46
{
47
    const rb_ifaddr_t *ifaddr;
48
    const rb_ifaddr_root_t *root;
49
    if (ptr == NULL)
50
        return 0;
51
    ifaddr = ptr;
52
    root = get_root(ifaddr);
53
    return sizeof(rb_ifaddr_root_t) + (root->numifaddrs - 1) * sizeof(rb_ifaddr_t);
54
}
55

  
56
static const rb_data_type_t ifaddr_type = {
57
    "socket/ifaddr",
58
    {ifaddr_mark, ifaddr_free, ifaddr_memsize,},
59
};
60

  
61
#define IS_IFADDRS(obj) rb_typeddata_is_kind_of((obj), &ifaddr_type)
62
static inline rb_ifaddr_t *
63
check_ifaddr(VALUE self)
64
{
65
      return rb_check_typeddata(self, &ifaddr_type);
66
}
67

  
68
static rb_ifaddr_t *
69
get_ifaddr(VALUE self)
70
{
71
    rb_ifaddr_t *rifaddr = check_ifaddr(self);
72

  
73
    if (!rifaddr) {
74
        rb_raise(rb_eTypeError, "uninitialized ifaddr");
75
    }
76
    return rifaddr;
77
}
78

  
79
static VALUE
80
rsock_getifaddrs(void)
81
{
82
    int ret;
83
    int numifaddrs, i;
84
    struct ifaddrs *ifaddrs, *ifa;
85
    rb_ifaddr_root_t *root;
86
    VALUE result;
87

  
88
    ret = getifaddrs(&ifaddrs);
89
    if (ret == -1)
90
        rb_sys_fail("getifaddrs");
91

  
92
    numifaddrs = 0;
93
    for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next)
94
        numifaddrs++;
95

  
96
    root = xmalloc(sizeof(rb_ifaddr_root_t) + (numifaddrs-1) * sizeof(rb_ifaddr_t));
97
    root->refcount = root->numifaddrs = numifaddrs;
98

  
99
    ifa = ifaddrs;
100
    for (i = 0; i < numifaddrs; i++) {
101
        root->ary[i].ord = i;
102
        root->ary[i].ifaddr = ifa;
103
        root->ary[i].root = root;
104
        ifa = ifa->ifa_next;
105
    }
106

  
107
    result = rb_ary_new2(numifaddrs);
108
    for (i = 0; i < numifaddrs; i++) {
109
        rb_ary_push(result, TypedData_Wrap_Struct(rb_cSockIfaddr, &ifaddr_type, &root->ary[i]));
110
    }
111

  
112
    return result;
113
}
114

  
115
/*
116
 * call-seq:
117
 *   getifaddr.name => string
118
 *
119
 * Returns the interface name of _getifaddr_.
120
 */
121

  
122
static VALUE
123
ifaddr_name(VALUE self)
124
{
125
    rb_ifaddr_t *rifaddr = get_ifaddr(self);
126
    struct ifaddrs *ifa = rifaddr->ifaddr;
127
    return rb_str_new_cstr(ifa->ifa_name);
128
}
129

  
130
/*
131
 * call-seq:
132
 *   getifaddr.ifindex => integer
133
 *
134
 * Returns the interface index of _getifaddr_.
135
 */
136

  
137
static VALUE
138
ifaddr_ifindex(VALUE self)
139
{
140
    rb_ifaddr_t *rifaddr = get_ifaddr(self);
141
    struct ifaddrs *ifa = rifaddr->ifaddr;
142
    unsigned int ifindex = if_nametoindex(ifa->ifa_name);
143
    if (ifindex == 0) {
144
        rb_raise(rb_eArgError, "invalid interface name: %s", ifa->ifa_name);
145
    }
146
    return UINT2NUM(ifindex);
147
}
148

  
149
/*
150
 * call-seq:
151
 *   getifaddr.flags => integer
152
 *
153
 * Returns the flags of _getifaddr_.
154
 */
155

  
156
static VALUE
157
ifaddr_flags(VALUE self)
158
{
159
    rb_ifaddr_t *rifaddr = get_ifaddr(self);
160
    struct ifaddrs *ifa = rifaddr->ifaddr;
161
    return UINT2NUM(ifa->ifa_flags);
162
}
163

  
164
/*
165
 * call-seq:
166
 *   getifaddr.addr => addrinfo
167
 *
168
 * Returns the address of _getifaddr_.
169
 * nil is returned if address is not available in _getifaddr_.
170
 */
171

  
172
static VALUE
173
ifaddr_addr(VALUE self)
174
{
175
    rb_ifaddr_t *rifaddr = get_ifaddr(self);
176
    struct ifaddrs *ifa = rifaddr->ifaddr;
177
    if (ifa->ifa_addr)
178
        return rsock_sockaddr_obj(ifa->ifa_addr, rsock_sockaddr_len(ifa->ifa_addr));
179
    return Qnil;
180
}
181

  
182
/*
183
 * call-seq:
184
 *   getifaddr.netmask => addrinfo
185
 *
186
 * Returns the netmask address of _getifaddr_.
187
 * nil is returned if netmask is not available in _getifaddr_.
188
 */
189

  
190
static VALUE
191
ifaddr_netmask(VALUE self)
192
{
193
    rb_ifaddr_t *rifaddr = get_ifaddr(self);
194
    struct ifaddrs *ifa = rifaddr->ifaddr;
195
    if (ifa->ifa_netmask)
196
        return rsock_sockaddr_obj(ifa->ifa_netmask, rsock_sockaddr_len(ifa->ifa_netmask));
197
    return Qnil;
198
}
199

  
200
/*
201
 * call-seq:
202
 *   getifaddr.broadaddr => addrinfo
203
 *
204
 * Returns the broadcast address of _getifaddr_.
205
 * nil is returned if the flags doesn't have IFF_BROADCAST.
206
 */
207

  
208
static VALUE
209
ifaddr_broadaddr(VALUE self)
210
{
211
    rb_ifaddr_t *rifaddr = get_ifaddr(self);
212
    struct ifaddrs *ifa = rifaddr->ifaddr;
213
    if ((ifa->ifa_flags & IFF_BROADCAST) && ifa->ifa_broadaddr)
214
        return rsock_sockaddr_obj(ifa->ifa_broadaddr, rsock_sockaddr_len(ifa->ifa_broadaddr));
215
    return Qnil;
216
}
217

  
218
/*
219
 * call-seq:
220
 *   getifaddr.dstaddr => addrinfo
221
 *
222
 * Returns the destination address of _getifaddr_.
223
 * nil is returned if the flags doesn't have IFF_POINTOPOINT.
224
 */
225

  
226
static VALUE
227
ifaddr_dstaddr(VALUE self)
228
{
229
    rb_ifaddr_t *rifaddr = get_ifaddr(self);
230
    struct ifaddrs *ifa = rifaddr->ifaddr;
231
    if ((ifa->ifa_flags & IFF_POINTOPOINT) && ifa->ifa_dstaddr)
232
        return rsock_sockaddr_obj(ifa->ifa_dstaddr, rsock_sockaddr_len(ifa->ifa_dstaddr));
233
    return Qnil;
234
}
235

  
236
static void
237
ifaddr_inspect_flags(unsigned int flags, VALUE result)
238
{
239
    char *sep = " ";
240
#define INSPECT_BIT(bit, name) \
241
    if (flags & (bit)) { rb_str_catf(result, "%s" name, sep); flags &= ~(bit); sep = ","; }
242
#ifdef IFF_UP
243
    INSPECT_BIT(IFF_UP, "UP")
244
#endif
245
#ifdef IFF_BROADCAST
246
    INSPECT_BIT(IFF_BROADCAST, "BROADCAST")
247
#endif
248
#ifdef IFF_DEBUG
249
    INSPECT_BIT(IFF_DEBUG, "DEBUG")
250
#endif
251
#ifdef IFF_LOOPBACK
252
    INSPECT_BIT(IFF_LOOPBACK, "LOOPBACK")
253
#endif
254
#ifdef IFF_POINTOPOINT
255
    INSPECT_BIT(IFF_POINTOPOINT, "POINTOPOINT")
256
#endif
257
#ifdef IFF_RUNNING
258
    INSPECT_BIT(IFF_RUNNING, "RUNNING")
259
#endif
260
#ifdef IFF_NOARP
261
    INSPECT_BIT(IFF_NOARP, "NOARP")
262
#endif
263
#ifdef IFF_PROMISC
264
    INSPECT_BIT(IFF_PROMISC, "PROMISC")
265
#endif
266
#ifdef IFF_NOTRAILERS
267
    INSPECT_BIT(IFF_NOTRAILERS, "NOTRAILERS")
268
#endif
269
#ifdef IFF_ALLMULTI
270
    INSPECT_BIT(IFF_ALLMULTI, "ALLMULTI")
271
#endif
272
#ifdef IFF_MASTER
273
    INSPECT_BIT(IFF_MASTER, "MASTER")
274
#endif
275
#ifdef IFF_SLAVE
276
    INSPECT_BIT(IFF_SLAVE, "SLAVE")
277
#endif
278
#ifdef IFF_MULTICAST
279
    INSPECT_BIT(IFF_MULTICAST, "MULTICAST")
280
#endif
281
#ifdef IFF_PORTSEL
282
    INSPECT_BIT(IFF_PORTSEL, "PORTSEL")
283
#endif
284
#ifdef IFF_AUTOMEDIA
285
    INSPECT_BIT(IFF_AUTOMEDIA, "AUTOMEDIA")
286
#endif
287
#ifdef IFF_DYNAMIC
288
    INSPECT_BIT(IFF_DYNAMIC, "DYNAMIC")
289
#endif
290
#ifdef IFF_LOWER_UP
291
    INSPECT_BIT(IFF_LOWER_UP, "LOWER_UP")
292
#endif
293
#ifdef IFF_DORMANT
294
    INSPECT_BIT(IFF_DORMANT, "DORMANT")
295
#endif
296
#ifdef IFF_ECHO
297
    INSPECT_BIT(IFF_ECHO, "ECHO")
298
#endif
299
#undef INSPECT_BIT
300
    if (flags) {
301
        rb_str_catf(result, "%s%#x", sep, flags);
302
    }
303
}
304

  
305
/*
306
 * call-seq:
307
 *   getifaddr.inspect => string
308
 *
309
 * Returns a string to show contents of _getifaddr_.
310
 */
311

  
312
static VALUE
313
ifaddr_inspect(VALUE self)
314
{
315
    rb_ifaddr_t *rifaddr = get_ifaddr(self);
316
    struct ifaddrs *ifa;
317
    VALUE result;
318

  
319
    ifa = rifaddr->ifaddr;
320

  
321
    result = rb_str_new_cstr("#<");
322

  
323
    rb_str_append(result, rb_class_name(CLASS_OF(self)));
324
    rb_str_cat2(result, " ");
325
    rb_str_cat2(result, ifa->ifa_name);
326

  
327
    if (ifa->ifa_flags)
328
        ifaddr_inspect_flags(ifa->ifa_flags, result);
329

  
330
    if (ifa->ifa_addr) {
331
      rb_str_cat2(result, " [");
332
      rsock_inspect_sockaddr(ifa->ifa_addr,
333
          rsock_sockaddr_len(ifa->ifa_addr),
334
          result);
335
      rb_str_cat2(result, "]");
336
    }
337
    if (ifa->ifa_netmask) {
338
      rb_str_cat2(result, " netmask:[");
339
      rsock_inspect_sockaddr(ifa->ifa_netmask,
340
          rsock_sockaddr_len(ifa->ifa_netmask),
341
          result);
342
      rb_str_cat2(result, "]");
343
    }
344

  
345
    if ((ifa->ifa_flags & IFF_BROADCAST) && ifa->ifa_broadaddr) {
346
      rb_str_cat2(result, " broadcast:[");
347
      rsock_inspect_sockaddr(ifa->ifa_broadaddr,
348
          rsock_sockaddr_len(ifa->ifa_broadaddr),
349
          result);
350
      rb_str_cat2(result, "]");
351
    }
352

  
353
    if ((ifa->ifa_flags & IFF_POINTOPOINT) && ifa->ifa_dstaddr) {
354
      rb_str_cat2(result, " dstaddr:[");
355
      rsock_inspect_sockaddr(ifa->ifa_dstaddr,
356
          rsock_sockaddr_len(ifa->ifa_dstaddr),
357
          result);
358
      rb_str_cat2(result, "]");
359
    }
360

  
361
    rb_str_cat2(result, ">");
362
    return result;
363
}
364

  
365
/*
366
 * call-seq:
367
 *   Socket.getifaddrs => [ifaddr1, ...]
368
 *
369
 * Returns an array of interface addresses.
370
 * An element of the array is an instance of Socket::Ifaddr.
371
 *
372
 * This method can be used to find multicast-enabled interfaces:
373
 *
374
 *   pp Socket.getifaddrs.reject {|ifaddr|
375
 *     !ifaddr.addr.ip? || (ifaddr.flags & Socket::IFF_MULTICAST == 0)
376
 *   }.map {|ifaddr| [ifaddr.name, ifaddr.ifindex, ifaddr.addr] }
377
 *   #=> [["eth0", 2, #<Addrinfo: 221.186.184.67>],
378
 *   #    ["eth0", 2, #<Addrinfo: fe80::216:3eff:fe95:88bb%eth0>]]
379
 *
380
 * Example result on GNU/Linux:
381
 *   pp Socket.getifaddrs
382
 *   #=> [#<Socket::Ifaddr lo UP,LOOPBACK,RUNNING,0x10000 [PACKET protocol:0 lo hatype:772 HOST hwaddr:00:00:00:00:00:00]>,
383
 *   #    #<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]>,
384
 *   #    #<Socket::Ifaddr sit0 NOARP [PACKET protocol:0 sit0 hatype:776 HOST hwaddr:00:00:00:00]>,
385
 *   #    #<Socket::Ifaddr lo UP,LOOPBACK,RUNNING,0x10000 [127.0.0.1] netmask:[255.0.0.0]>,
386
 *   #    #<Socket::Ifaddr eth0 UP,BROADCAST,RUNNING,MULTICAST,0x10000 [221.186.184.67] netmask:[255.255.255.240] broadcast:[221.186.184.79]>,
387
 *   #    #<Socket::Ifaddr lo UP,LOOPBACK,RUNNING,0x10000 [::1] netmask:[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]>,
388
 *   #    #<Socket::Ifaddr eth0 UP,BROADCAST,RUNNING,MULTICAST,0x10000 [fe80::216:3eff:fe95:88bb%eth0] netmask:[ffff:ffff:ffff:ffff::]>]
389
 *
390
 * Example result on FreeBSD:
391
 *   pp Socket.getifaddrs'
392
 *   #=> [#<Socket::Ifaddr usbus0 UP,0x10000 [LINK usbus0]>,
393
 *   #    #<Socket::Ifaddr re0 UP,BROADCAST,RUNNING,MULTICAST,0x800 [LINK re0 3a:d0:40:9a:fe:e8]>,
394
 *   #    #<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]>,
395
 *   #    #<Socket::Ifaddr re0 UP,BROADCAST,RUNNING,MULTICAST,0x800 [fe80:2::38d0:40ff:fe9a:fee8] netmask:[ffff:ffff:ffff:ffff::]>,
396
 *   #    #<Socket::Ifaddr re0 UP,BROADCAST,RUNNING,MULTICAST,0x800 [2001:2e8:408:10::12] netmask:[UNSPEC]>,
397
 *   #    #<Socket::Ifaddr plip0 POINTOPOINT,MULTICAST,0x800 [LINK plip0]>,
398
 *   #    #<Socket::Ifaddr lo0 UP,LOOPBACK,RUNNING,MULTICAST [LINK lo0]>,
399
 *   #    #<Socket::Ifaddr lo0 UP,LOOPBACK,RUNNING,MULTICAST [::1] netmask:[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]>,
400
 *   #    #<Socket::Ifaddr lo0 UP,LOOPBACK,RUNNING,MULTICAST [fe80:4::1] netmask:[ffff:ffff:ffff:ffff::]>,
401
 *   #    #<Socket::Ifaddr lo0 UP,LOOPBACK,RUNNING,MULTICAST [127.0.0.1] netmask:[255.?.?.? (5 bytes for 16 bytes sockaddr_in)]>]
402
 */
403

  
404
static VALUE
405
socket_s_getifaddrs(VALUE self)
406
{
407
    return rsock_getifaddrs();
408
}
409

  
410
void
411
rsock_init_sockifaddr(void)
412
{
413
    /*
414
     * Document-class: Socket::Ifaddr
415
     *
416
     * Socket::Ifaddr represents a result of getifaddrs() function.
417
     */
418
    rb_cSockIfaddr = rb_define_class_under(rb_cSocket, "Ifaddr", rb_cData);
419
    rb_define_method(rb_cSockIfaddr, "inspect", ifaddr_inspect, 0);
420
    rb_define_method(rb_cSockIfaddr, "name", ifaddr_name, 0);
421
    rb_define_method(rb_cSockIfaddr, "ifindex", ifaddr_ifindex, 0);
422
    rb_define_method(rb_cSockIfaddr, "flags", ifaddr_flags, 0);
423
    rb_define_method(rb_cSockIfaddr, "addr", ifaddr_addr, 0);
424
    rb_define_method(rb_cSockIfaddr, "netmask", ifaddr_netmask, 0);
425
    rb_define_method(rb_cSockIfaddr, "broadaddr", ifaddr_broadaddr, 0);
426
    rb_define_method(rb_cSockIfaddr, "dstaddr", ifaddr_dstaddr, 0);
427

  
428
    rb_define_singleton_method(rb_cSocket, "getifaddrs", socket_s_getifaddrs, 0);
429
}
ext/socket/init.c
627 627
    rsock_init_sockopt();
628 628
    rsock_init_ancdata();
629 629
    rsock_init_addrinfo();
630
    rsock_init_sockifaddr();
630 631
    rsock_init_socket_constants();
631 632
}
ext/socket/mkconstants.rb
703 703
LOCAL_PEERCRED	nil	Retrieve peer credentials
704 704
LOCAL_CREDS	nil	Pass credentials to receiver
705 705
LOCAL_CONNWAIT	nil	Connect blocks until accepted
706

  
707
IFF_802_1Q_VLAN      nil 802.1Q VLAN device
708
IFF_ALLMULTI         nil receive all multicast packets
709
IFF_ALTPHYS          nil use alternate physical connection
710
IFF_AUTOMEDIA        nil auto media select active
711
IFF_BONDING          nil bonding master or slave
712
IFF_BRIDGE_PORT      nil device used as bridge port
713
IFF_BROADCAST        nil broadcast address valid
714
IFF_CANTCONFIG       nil unconfigurable using ioctl(2)
715
IFF_DEBUG            nil turn on debugging
716
IFF_DISABLE_NETPOLL  nil disable netpoll at run-time
717
IFF_DONT_BRIDGE      nil disallow bridging this ether dev
718
IFF_DORMANT          nil driver signals dormant
719
IFF_DRV_OACTIVE      nil tx hardware queue is full
720
IFF_DRV_RUNNING      nil resources allocated
721
IFF_DYING            nil interface is winding down
722
IFF_DYNAMIC          nil dialup device with changing addresses
723
IFF_EBRIDGE          nil ethernet bridging device
724
IFF_ECHO             nil echo sent packets
725
IFF_ISATAP           nil ISATAP interface (RFC4214)
726
IFF_LINK0            nil per link layer defined bit 0
727
IFF_LINK1            nil per link layer defined bit 1
728
IFF_LINK2            nil per link layer defined bit 2
729
IFF_LIVE_ADDR_CHANGE nil hardware address change when it's running
730
IFF_LOOPBACK         nil loopback net
731
IFF_LOWER_UP         nil driver signals L1 up
732
IFF_MACVLAN_PORT     nil device used as macvlan port
733
IFF_MASTER           nil master of a load balancer
734
IFF_MASTER_8023AD    nil bonding master, 802.3ad.
735
IFF_MASTER_ALB       nil bonding master, balance-alb.
736
IFF_MASTER_ARPMON    nil bonding master, ARP mon in use
737
IFF_MONITOR          nil user-requested monitor mode
738
IFF_MULTICAST        nil supports multicast
739
IFF_NOARP            nil no address resolution protocol
740
IFF_NOTRAILERS       nil avoid use of trailers
741
IFF_OACTIVE          nil transmission in progress
742
IFF_OVS_DATAPATH     nil device used as Open vSwitch datapath port
743
IFF_POINTOPOINT      nil point-to-point link
744
IFF_PORTSEL          nil can set media type
745
IFF_PPROMISC         nil user-requested promisc mode
746
IFF_PROMISC          nil receive all packets
747
IFF_RENAMING         nil interface is being renamed
748
IFF_ROUTE            nil routing entry installed
749
IFF_RUNNING          nil resources allocated
750
IFF_SIMPLEX          nil can't hear own transmissions
751
IFF_SLAVE            nil slave of a load balancer
752
IFF_SLAVE_INACTIVE   nil bonding slave not the curr. active
753
IFF_SLAVE_NEEDARP    nil need ARPs for validation
754
IFF_SMART            nil interface manages own routes
755
IFF_STATICARP        nil static ARP
756
IFF_SUPP_NOFCS       nil sending custom FCS
757
IFF_TEAM_PORT        nil used as team port
758
IFF_TX_SKB_SHARING   nil sharing skbs on transmit
759
IFF_UNICAST_FLT      nil unicast filtering
760
IFF_UP               nil interface is up
761
IFF_WAN_HDLC         nil WAN HDLC device
762
IFF_XMIT_DST_RELEASE nil dev_hard_start_xmit() is allowed to release skb->dst
763
IFF_VOLATILE         nil volatile flags
764
IFF_CANTCHANGE       nil flags not changeable
ext/socket/raddrinfo.c
953 953
inspect_sockaddr(VALUE addrinfo, VALUE ret)
954 954
{
955 955
    rb_addrinfo_t *rai = get_addrinfo(addrinfo);
956
    union_sockaddr *sockaddr = &rai->addr;
957
    socklen_t socklen = rai->sockaddr_len;
958
    return rsock_inspect_sockaddr((struct sockaddr *)sockaddr, socklen, ret);
959
}
956 960

  
957
    if (rai->sockaddr_len == 0) {
961
VALUE
962
rsock_inspect_sockaddr(struct sockaddr *sockaddr_arg, socklen_t socklen, VALUE ret)
963
{
964
    union_sockaddr *sockaddr = (union_sockaddr *)sockaddr_arg;
965
    if (socklen == 0) {
958 966
        rb_str_cat2(ret, "empty-sockaddr");
959 967
    }
960
    else if ((long)rai->sockaddr_len < ((char*)&rai->addr.addr.sa_family + sizeof(rai->addr.addr.sa_family)) - (char*)&rai->addr)
968
    else if ((long)socklen < ((char*)&sockaddr->addr.sa_family + sizeof(sockaddr->addr.sa_family)) - (char*)sockaddr)
961 969
        rb_str_cat2(ret, "too-short-sockaddr");
962 970
    else {
963
        switch (rai->addr.addr.sa_family) {
971
        switch (sockaddr->addr.sa_family) {
972
          case AF_UNSPEC:
973
	  {
974
	    rb_str_cat2(ret, "UNSPEC");
975
            break;
976
	  }
977

  
964 978
          case AF_INET:
965 979
          {
966 980
            struct sockaddr_in *addr;
967 981
            int port;
968
            if (rai->sockaddr_len < (socklen_t)sizeof(struct sockaddr_in)) {
969
                rb_str_cat2(ret, "too-short-AF_INET-sockaddr");
970
            }
971
            else {
972
                addr = &rai->addr.in;
973
                rb_str_catf(ret, "%d.%d.%d.%d",
974
                            ((unsigned char*)&addr->sin_addr)[0],
975
                            ((unsigned char*)&addr->sin_addr)[1],
976
                            ((unsigned char*)&addr->sin_addr)[2],
977
                            ((unsigned char*)&addr->sin_addr)[3]);
978
                port = ntohs(addr->sin_port);
979
                if (port)
980
                    rb_str_catf(ret, ":%d", port);
981
                if ((socklen_t)sizeof(struct sockaddr_in) < rai->sockaddr_len)
982
                    rb_str_catf(ret, "(sockaddr %d bytes too long)", (int)(rai->sockaddr_len - sizeof(struct sockaddr_in)));
983
            }
982
	    addr = &sockaddr->in;
983
	    if (((char*)&addr->sin_addr)-(char*)addr+0+1 <= socklen)
984
		rb_str_catf(ret, "%d", ((unsigned char*)&addr->sin_addr)[0]);
985
	    else
986
		rb_str_cat2(ret, "?");
987
	    if (((char*)&addr->sin_addr)-(char*)addr+1+1 <= socklen)
988
		rb_str_catf(ret, ".%d", ((unsigned char*)&addr->sin_addr)[1]);
989
	    else
990
		rb_str_cat2(ret, ".?");
991
	    if (((char*)&addr->sin_addr)-(char*)addr+2+1 <= socklen)
992
		rb_str_catf(ret, ".%d", ((unsigned char*)&addr->sin_addr)[2]);
993
	    else
994
		rb_str_cat2(ret, ".?");
995
	    if (((char*)&addr->sin_addr)-(char*)addr+3+1 <= socklen)
996
		rb_str_catf(ret, ".%d", ((unsigned char*)&addr->sin_addr)[3]);
997
	    else
998
		rb_str_cat2(ret, ".?");
999

  
1000
	    if (((char*)&addr->sin_port)-(char*)addr+sizeof(addr->sin_port) < socklen) {
1001
		port = ntohs(addr->sin_port);
1002
		if (port)
1003
		    rb_str_catf(ret, ":%d", port);
1004
	    }
1005
	    else {
1006
		rb_str_cat2(ret, ":?");
1007
	    }
1008
	    if ((socklen_t)sizeof(struct sockaddr_in) != socklen)
1009
		rb_str_catf(ret, " (%d bytes for %d bytes sockaddr_in)",
1010
		  (int)socklen,
1011
		  (int)sizeof(struct sockaddr_in));
984 1012
            break;
985 1013
          }
986 1014

  
......
991 1019
            char hbuf[1024];
992 1020
            int port;
993 1021
            int error;
994
            if (rai->sockaddr_len < (socklen_t)sizeof(struct sockaddr_in6)) {
995
                rb_str_cat2(ret, "too-short-AF_INET6-sockaddr");
1022
            if (socklen < (socklen_t)sizeof(struct sockaddr_in6)) {
1023
                rb_str_catf(ret, "too-short-AF_INET6-sockaddr %d bytes", (int)socklen);
996 1024
            }
997 1025
            else {
998
                addr = &rai->addr.in6;
1026
                addr = &sockaddr->in6;
999 1027
                /* use getnameinfo for scope_id.
1000 1028
                 * RFC 4007: IPv6 Scoped Address Architecture
1001 1029
                 * draft-ietf-ipv6-scope-api-00.txt: Scoped Address Extensions to the IPv6 Basic Socket API
1002 1030
                 */
1003
                error = getnameinfo(&rai->addr.addr, rai->sockaddr_len,
1031
                error = getnameinfo(&sockaddr->addr, socklen,
1004 1032
                                    hbuf, (socklen_t)sizeof(hbuf), NULL, 0,
1005 1033
                                    NI_NUMERICHOST|NI_NUMERICSERV);
1006 1034
                if (error) {
......
1013 1041
                    port = ntohs(addr->sin6_port);
1014 1042
                    rb_str_catf(ret, "[%s]:%d", hbuf, port);
1015 1043
                }
1016
                if ((socklen_t)sizeof(struct sockaddr_in6) < rai->sockaddr_len)
1017
                    rb_str_catf(ret, "(sockaddr %d bytes too long)", (int)(rai->sockaddr_len - sizeof(struct sockaddr_in6)));
1044
                if ((socklen_t)sizeof(struct sockaddr_in6) < socklen)
1045
                    rb_str_catf(ret, "(sockaddr %d bytes too long)", (int)(socklen - sizeof(struct sockaddr_in6)));
1018 1046
            }
1019 1047
            break;
1020 1048
          }
......
1023 1051
#ifdef HAVE_SYS_UN_H
1024 1052
          case AF_UNIX:
1025 1053
          {
1026
            struct sockaddr_un *addr = &rai->addr.un;
1054
            struct sockaddr_un *addr = &sockaddr->un;
1027 1055
            char *p, *s, *e;
1028 1056
            s = addr->sun_path;
1029
            e = (char*)addr + rai->sockaddr_len;
1057
            e = (char*)addr + socklen;
1030 1058
            while (s < e && *(e-1) == '\0')
1031 1059
                e--;
1032 1060
            if (e < s)
......
1042 1070
                }
1043 1071
                if (printable_only) { /* only printable, no space */
1044 1072
                    if (s[0] != '/') /* relative path */
1045
                        rb_str_cat2(ret, "AF_UNIX ");
1073
                        rb_str_cat2(ret, "UNIX ");
1046 1074
                    rb_str_cat(ret, s, p - s);
1047 1075
                }
1048 1076
                else {
1049
                    rb_str_cat2(ret, "AF_UNIX");
1077
                    rb_str_cat2(ret, "UNIX");
1050 1078
                    while (s < e)
1051 1079
                        rb_str_catf(ret, ":%02x", (unsigned char)*s++);
1052 1080
                }
......
1055 1083
          }
1056 1084
#endif
1057 1085

  
1086
#ifdef AF_PACKET
1087
          /* GNU/Linux */
1088
          case AF_PACKET:
1089
          {
1090
            struct sockaddr_ll *addr;
1091
            addr = (struct sockaddr_ll *)sockaddr;
1092
            rb_str_cat2(ret, "PACKET");
1093

  
1094
            if (offsetof(struct sockaddr_ll, sll_protocol) + sizeof(addr->sll_protocol) <= socklen) {
1095
                rb_str_catf(ret, " protocol:%d", ntohs(addr->sll_protocol));
1096
            }
1097
            if (offsetof(struct sockaddr_ll, sll_ifindex) + sizeof(addr->sll_ifindex) <= socklen) {
1098
                char buf[IFNAMSIZ];
1099
                if (if_indextoname(addr->sll_ifindex, buf) == NULL)
1100
                    rb_str_catf(ret, " ifindex:%d", addr->sll_ifindex);
1101
                else
1102
                    rb_str_catf(ret, " %s", buf);
1103
            }
1104
            if (offsetof(struct sockaddr_ll, sll_hatype) + sizeof(addr->sll_hatype) <= socklen) {
1105
                rb_str_catf(ret, " hatype:%d", addr->sll_hatype);
1106
            }
1107
            if (offsetof(struct sockaddr_ll, sll_pkttype) + sizeof(addr->sll_pkttype) <= socklen) {
1108
                if (addr->sll_pkttype == PACKET_HOST)
1109
                    rb_str_cat2(ret, " HOST");
1110
                else if (addr->sll_pkttype == PACKET_BROADCAST)
1111
                    rb_str_cat2(ret, " BROADCAST");
1112
                else if (addr->sll_pkttype == PACKET_MULTICAST)
1113
                    rb_str_cat2(ret, " MULTICAST");
1114
                else if (addr->sll_pkttype == PACKET_OTHERHOST)
1115
                    rb_str_cat2(ret, " OTHERHOST");
1116
                else if (addr->sll_pkttype == PACKET_OUTGOING)
1117
                    rb_str_cat2(ret, " OUTGOING");
1118
                else
1119
                    rb_str_catf(ret, " pkttype:%d", addr->sll_pkttype);
1120
            }
1121
            if (socklen != (socklen_t)(offsetof(struct sockaddr_ll, sll_addr) + addr->sll_halen)) {
1122
                if (offsetof(struct sockaddr_ll, sll_halen) + sizeof(addr->sll_halen) <= socklen) {
1123
                    rb_str_catf(ret, " halen:%d", addr->sll_halen);
1124
                }
1125
            }
1126
            if (offsetof(struct sockaddr_ll, sll_addr) < socklen) {
1127
                socklen_t len, i;
1128
                rb_str_cat2(ret, " hwaddr");
1129
                len = addr->sll_halen;
1130
                if (socklen < offsetof(struct sockaddr_ll, sll_addr) + len)
1131
                    len = socklen - offsetof(struct sockaddr_ll, sll_addr);
1132
                for (i = 0; i < len; i++) {
1133
                    rb_str_catf(ret, ":%02x", addr->sll_addr[i]);
1134
                }
1135
            }
1136

  
1137
            if (socklen < (socklen_t)(offsetof(struct sockaddr_ll, sll_halen) + sizeof(addr->sll_halen)) ||
1138
                (socklen_t)(offsetof(struct sockaddr_ll, sll_addr) + addr->sll_halen) != socklen)
1139
                rb_str_catf(ret, " (%d bytes for %d bytes sockaddr_ll)",
1140
                    (int)socklen, (int)sizeof(struct sockaddr_ll));
1141

  
1142
            break;
1143
          }
1144
#endif
1145

  
1146
#ifdef AF_LINK
1147
	  /* AF_LINK is defined in 4.4BSD derivations since Net2.
1148
	     link_ntoa is also defined at Net2.
1149
             However Debian GNU/kFreeBSD defines AF_LINK but
1150
             don't have link_ntoa.  */
1151
          case AF_LINK:
1152
	  {
1153
	    /*
1154
	     * Simple implementation using link_ntoa():
1155
	     * This doesn't work on Debian GNU/kFreeBSD 6.0.7 (squeeze).
1156
             * Also, the format is bit different.
1157
	     *
1158
	     * rb_str_catf(ret, "LINK %s", link_ntoa(&sockaddr->dl));
1159
	     * break;
1160
	     */
1161
            struct sockaddr_dl *addr = &sockaddr->dl;
1162
            char *np = NULL, *ap = NULL, *endp;
1163
            int nlen = 0, alen = 0;
1164
            int i, off;
1165

  
1166
            endp = ((char *)addr) + socklen;
1167

  
1168
            rb_str_cat2(ret, "LINK");
1169

  
1170
            if (offsetof(struct sockaddr_dl, sdl_data) < socklen) {
1171
                np = addr->sdl_data;
1172
                nlen = addr->sdl_nlen;
1173
                if (endp - np < nlen)
1174
                    nlen = endp - np;
1175
            }
1176
            off = addr->sdl_nlen;
1177

  
1178
            if (offsetof(struct sockaddr_dl, sdl_data) + off < socklen) {
1179
                ap = addr->sdl_data + off;
1180
                alen = addr->sdl_alen;
1181
                if (endp - ap < alen)
1182
                    alen = endp - ap;
1183
            }
1184

  
1185
            if (np)
1186
                rb_str_catf(ret, " %.*s", nlen, np);
1187
            else
1188
                rb_str_cat2(ret, " ?");
1189

  
1190
            if (ap) {
1191
                for (i = 0; i < alen; i++)
1192
                    rb_str_catf(ret, "%s%02x", i == 0 ? " " : ":", (unsigned char)ap[i]);
1193
            }
1194

  
1195
            if (socklen < (socklen_t)(offsetof(struct sockaddr_dl, sdl_nlen) + sizeof(addr->sdl_nlen)) ||
1196
                socklen < (socklen_t)(offsetof(struct sockaddr_dl, sdl_alen) + sizeof(addr->sdl_alen)) ||
1197
                socklen < (socklen_t)(offsetof(struct sockaddr_dl, sdl_slen) + sizeof(addr->sdl_slen)) ||
1198
                /* longer length is possible behavior because struct sockaddr_dl has "minimum work area, can be larger" as the last field.
1199
                 * cf. Net2:/usr/src/sys/net/if_dl.h. */
1200
                socklen < (socklen_t)(offsetof(struct sockaddr_dl, sdl_data) + addr->sdl_nlen + addr->sdl_alen + addr->sdl_slen))
1201
                rb_str_catf(ret, " (%d bytes for %d bytes sockaddr_dl)",
1202
                    (int)socklen, (int)sizeof(struct sockaddr_dl));
1203
            break;
1204
          }
1205
#endif
1206

  
1058 1207
          default:
1059 1208
          {
1060
            ID id = rsock_intern_family(rai->addr.addr.sa_family);
1209
            ID id = rsock_intern_family(sockaddr->addr.sa_family);
1061 1210
            if (id == 0)
1062
                rb_str_catf(ret, "unknown address family %d", rai->addr.addr.sa_family);
1211
                rb_str_catf(ret, "unknown address family %d", sockaddr->addr.sa_family);
1063 1212
            else
1064 1213
                rb_str_catf(ret, "%s address format unknown", rb_id2name(id));
1065 1214
            break;
ext/socket/rubysocket.h
44 44
#  include <netdb.h>
45 45
#endif
46 46

  
47
#ifdef HAVE_NETPACKET_PACKET_H
48
#  include <netpacket/packet.h>
49
#endif
50
#ifdef HAVE_NET_ETHERNET_H
51
#  include <net/ethernet.h>
52
#endif
53

  
47 54
#include <errno.h>
48 55

  
49 56
#ifdef HAVE_SYS_UN_H
......
87 94
#ifdef HAVE_UCRED_H
88 95
#  include <ucred.h>
89 96
#endif
97
#ifdef HAVE_NET_IF_DL_H
98
#  include <net/if_dl.h>
99
#endif
90 100

  
91 101
#ifndef HAVE_TYPE_SOCKLEN_T
92 102
typedef int socklen_t;
......
169 179
#ifdef HAVE_TYPE_STRUCT_SOCKADDR_UN
170 180
  struct sockaddr_un un;
171 181
#endif
182
#ifdef HAVE_TYPE_STRUCT_SOCKADDR_DL
183
  struct sockaddr_dl dl; /* AF_LINK */
184
#endif
172 185
  struct sockaddr_storage storage;
173 186
  char place_holder[2048]; /* sockaddr_storage is not enough for Unix domain sockets on SunOS and Darwin. */
174 187
} union_sockaddr;
......
267 280
VALUE rsock_make_ipaddr(struct sockaddr *addr, socklen_t addrlen);
268 281
VALUE rsock_ipaddr(struct sockaddr *sockaddr, socklen_t sockaddrlen, int norevlookup);
269 282
VALUE rsock_make_hostent(VALUE host, struct addrinfo *addr, VALUE (*ipaddr)(struct sockaddr *, socklen_t));
283
VALUE rsock_inspect_sockaddr(struct sockaddr *addr, socklen_t socklen, VALUE ret);
284
socklen_t rsock_sockaddr_len(struct sockaddr *addr);
285
VALUE rsock_sockaddr_obj(struct sockaddr *addr, socklen_t len);
270 286

  
271 287
int rsock_revlookup_flag(VALUE revlookup, int *norevlookup);
272 288

  
......
344 360
void rsock_init_ancdata(void);
345 361
void rsock_init_addrinfo(void);
346 362
void rsock_init_sockopt(void);
363
void rsock_init_sockifaddr(void);
347 364
void rsock_init_socket_init(void);
348 365

  
349 366
NORETURN(void rsock_sys_fail_host_port(const char *, VALUE, VALUE));
ext/socket/socket.c
1584 1584
        return (socklen_t)sizeof(struct sockaddr_un);
1585 1585
#endif
1586 1586

  
1587
#ifdef AF_PACKET
1588
      case AF_PACKET:
1589
        return (socklen_t)(offsetof(struct sockaddr_ll, sll_addr) + ((struct sockaddr_ll *)addr)->sll_halen);
1590
#endif
1591

  
1587 1592
      default:
1588 1593
        return (socklen_t)(offsetof(struct sockaddr, sa_family) + sizeof(addr->sa_family));
1589 1594
    }
1590 1595
}
1591 1596

  
1597
socklen_t
1598
rsock_sockaddr_len(struct sockaddr *addr)
1599
{
1600
    return sockaddr_len(addr);
1601
}
1602

  
1592 1603
static VALUE
1593 1604
sockaddr_obj(struct sockaddr *addr, socklen_t len)
1594 1605
{
......
1622 1633
    return rsock_addrinfo_new(addr, len, addr->sa_family, 0, 0, Qnil, Qnil);
1623 1634
}
1624 1635

  
1636
VALUE
1637
rsock_sockaddr_obj(struct sockaddr *addr, socklen_t len)
1638
{
1639
    return sockaddr_obj(addr, len);
1640
}
1641

  
1625 1642
#endif
1626 1643

  
1627 1644
#if defined(HAVE_GETIFADDRS) || (defined(SIOCGLIFCONF) && defined(SIOCGLIFNUM) && !defined(__hpux)) || defined(SIOCGIFCONF) ||  defined(_WIN32)