Class: Socket::Ifaddr

Inherits:
Data
  • Object
show all
Defined in:
ifaddr.c,
ifaddr.c

Overview

Socket::Ifaddr represents a result of getifaddrs() function.

Instance Method Summary collapse

Instance Method Details

#addrObject

Returns the address of ifaddr. nil is returned if address is not available in ifaddr.



195
196
197
198
199
200
201
202
203
# File 'ifaddr.c', line 195

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;
}

#broadaddrObject

Returns the broadcast address of ifaddr. nil is returned if the flags doesn’t have IFF_BROADCAST.



231
232
233
234
235
236
237
238
239
# File 'ifaddr.c', line 231

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;
}

#dstaddrObject

Returns the destination address of ifaddr. nil is returned if the flags doesn’t have IFF_POINTOPOINT.



249
250
251
252
253
254
255
256
257
# File 'ifaddr.c', line 249

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;
}

#flagsInteger

Returns the flags of ifaddr.

Returns:

  • (Integer)


179
180
181
182
183
184
185
# File 'ifaddr.c', line 179

static VALUE
ifaddr_flags(VALUE self)
{
    rb_ifaddr_t *rifaddr = get_ifaddr(self);
    struct ifaddrs *ifa = rifaddr->ifaddr;
    return IFAFLAGS2NUM(ifa->ifa_flags);
}

#ifindexInteger

Returns the interface index of ifaddr.

Returns:

  • (Integer)


157
158
159
160
161
162
163
164
165
166
167
# File 'ifaddr.c', line 157

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);
}

#inspectString

Returns a string to show contents of ifaddr.

Returns:

  • (String)


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
# File 'ifaddr.c', line 338

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;
}

#nameString

Returns the interface name of ifaddr.

Returns:

  • (String)


141
142
143
144
145
146
147
# File 'ifaddr.c', line 141

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);
}

#netmaskObject

Returns the netmask address of ifaddr. nil is returned if netmask is not available in ifaddr.



213
214
215
216
217
218
219
220
221
# File 'ifaddr.c', line 213

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;
}