Class: RubySL::Socket::Foreign::Ifaddrs

Inherits:
Rubinius::FFI::Struct
  • Object
show all
Defined in:
lib/rubysl/socket/foreign/ifaddrs.rb

Overview

Class representing an “ifaddrs” C structure.

The memory used by this structure should be free’d using ‘RubySL::Socket::Foreign.freeifaddrs` only as following:

RubySL::Socket::Foreign.freeifaddrs(initial_ifaddrs_struct)

To ensure Rubinius doesn’t accidentally free invalid pointers all pointers (including the pointer of “self”) stored in this class have the “autorelease” option set to false.

Constant Summary collapse

POINTERS =
[
  :address, :next, :broadcast_address, :destination_address,
  :netmask_address
]

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Ifaddrs

Returns a new instance of Ifaddrs.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rubysl/socket/foreign/ifaddrs.rb', line 23

def initialize(*args)
  super

  POINTERS.each do |name|
    pointer = __send__(name)

    pointer.autorelease = false if pointer
  end

  pointer.autorelease = false
end

Instance Method Details

#addressObject



51
52
53
# File 'lib/rubysl/socket/foreign/ifaddrs.rb', line 51

def address
  @address ||= self[:ifa_addr]
end

#address_to_addrinfoObject



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rubysl/socket/foreign/ifaddrs.rb', line 87

def address_to_addrinfo
  return unless address

  sockaddr = Sockaddr.new(address)

  if sockaddr.family == ::Socket::AF_INET
    ::Addrinfo.new(SockaddrIn.new(address).to_s)
  elsif sockaddr.family == ::Socket::AF_INET6
    ::Addrinfo.new(SockaddrIn6.new(address).to_s)
  else
    nil
  end
end

#broadcast?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/rubysl/socket/foreign/ifaddrs.rb', line 79

def broadcast?
  flags & ::Socket::IFF_BROADCAST > 0
end

#broadcast_addressObject



55
56
57
# File 'lib/rubysl/socket/foreign/ifaddrs.rb', line 55

def broadcast_address
  @broadcast_address ||= self[:ifa_broadaddr]
end

#broadcast_to_addrinfoObject



101
102
103
104
105
# File 'lib/rubysl/socket/foreign/ifaddrs.rb', line 101

def broadcast_to_addrinfo
  return if !broadcast? || !broadcast_address

  ::Addrinfo.raw_with_family(Sockaddr.new(broadcast_address).family)
end

#dataObject



43
44
45
# File 'lib/rubysl/socket/foreign/ifaddrs.rb', line 43

def data
  @data ||= self[:ifa_data]
end

#destination_addressObject



59
60
61
# File 'lib/rubysl/socket/foreign/ifaddrs.rb', line 59

def destination_address
  @destination_address ||= self[:ifa_dstaddr]
end

#destination_to_addrinfoObject



107
108
109
110
111
# File 'lib/rubysl/socket/foreign/ifaddrs.rb', line 107

def destination_to_addrinfo
  return if !point_to_point? || !destination_address

  ::Addrinfo.raw_with_family(Sockaddr.new(destination_address).family)
end

#each_addressObject



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rubysl/socket/foreign/ifaddrs.rb', line 67

def each_address
  next_pointer = self.next

  while next_pointer
    struct = self.class.new(next_pointer)

    yield struct

    next_pointer = struct.next
  end
end

#flagsObject



39
40
41
# File 'lib/rubysl/socket/foreign/ifaddrs.rb', line 39

def flags
  self[:ifa_flags]
end

#nameObject



35
36
37
# File 'lib/rubysl/socket/foreign/ifaddrs.rb', line 35

def name
  self[:ifa_name]
end

#netmask_addressObject



63
64
65
# File 'lib/rubysl/socket/foreign/ifaddrs.rb', line 63

def netmask_address
  @netmask_address ||= self[:ifa_netmask]
end

#netmask_to_addrinfoObject



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rubysl/socket/foreign/ifaddrs.rb', line 113

def netmask_to_addrinfo
  return unless netmask_address

  sockaddr = Sockaddr.new(netmask_address)

  if sockaddr.family == ::Socket::AF_INET
    ::Addrinfo.new(SockaddrIn.new(netmask_address).to_s)
  elsif sockaddr.family == ::Socket::AF_INET6
    ::Addrinfo.new(SockaddrIn6.new(netmask_address).to_s)
  else
    ::Addrinfo.raw_with_family(sockaddr.family)
  end
end

#nextObject



47
48
49
# File 'lib/rubysl/socket/foreign/ifaddrs.rb', line 47

def next
  @next ||= self[:ifa_next]
end

#point_to_point?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/rubysl/socket/foreign/ifaddrs.rb', line 83

def point_to_point?
  flags & ::Socket::IFF_POINTOPOINT > 0
end