Module: Ionian::Extension::Socket

Defined in:
lib/ionian/extension/socket.rb

Overview

A mixin for Socket objects.

This module was designed to be extended by instantiated objects that implement the standard library Socket class. my_socket.extend Ionian::Socket

Extending this module also extends Ionian::IO.

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) extended(obj)

Called automaticallly when the object is extended with #extend.



17
18
19
20
# File 'lib/ionian/extension/socket.rb', line 17

def self.extended obj
  obj.extend Ionian::Extension::IO
  obj.initialize_ionian_socket
end

+ (Object) multicast(address)

Returns true if the given address is within the multicast range.



282
283
284
# File 'lib/ionian/extension/socket.rb', line 282

def multicast address
  address >= '224.0.0.0' and address <= '239.255.255.255' ? true : false
end

+ (Object) multicast?

Returns true if the given address is within the multicast range.



286
287
288
# File 'lib/ionian/extension/socket.rb', line 286

def multicast address
  address >= '224.0.0.0' and address <= '239.255.255.255' ? true : false
end

Instance Method Details

- (Object) broadcast Also known as: broadcast?

Returns true if sending broadcast datagrams is permitted. ( SO_BROADCAST )



29
30
31
32
33
# File 'lib/ionian/extension/socket.rb', line 29

def broadcast
  param = self.getsockopt(::Socket::SOL_SOCKET, ::Socket::SO_BROADCAST)
    .data.unpack('i').first
  param > 0 ? true : false
end

- (Object) broadcast=(value)

Permit sending broadcast datagrams if true. ( SO_BROADCAST )



37
38
39
40
# File 'lib/ionian/extension/socket.rb', line 37

def broadcast= value
  param = (!!value && value != 0) ? 1 : 0
  self.setsockopt ::Socket::SOL_SOCKET, ::Socket::SO_BROADCAST, [param].pack('i')
end

- (Object) cork Also known as: cork?

Returns true if multiple writes are buffered into a single segment. See #recork. Linux only. ( TCP_CORK )



124
125
126
127
128
# File 'lib/ionian/extension/socket.rb', line 124

def cork
  param = self.getsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_CORK)
    .data.unpack('i').first
  param > 0 ? true : false
end

- (Object) cork=(value)

Buffers multiple writes into a single segment if true. The segment is sent once the cork flag is disabled, the upper limit on the size of a segment is reached, the socket is closed, or 200ms elapses from the time the first corked byte is written. See #recork. Linux only. ( TCP_CORK )



140
141
142
143
# File 'lib/ionian/extension/socket.rb', line 140

def cork= value
  param = (!!value && value != 0) ? 1 : 0
  self.setsockopt ::Socket::IPPROTO_TCP, ::Socket::TCP_CORK, [param].pack('i')
end

- (Object) initialize_ionian_socket

Initialize the Ionian Socket variables. This is called automatically if #extend is called on an object.



24
25
# File 'lib/ionian/extension/socket.rb', line 24

def initialize_ionian_socket
end

- (Object) ip_add_membership(address = nil, interface = nil)

Join a multicast group. Address is the class D multicast address (uses remote address if not specified). Interface is the local network interface to receive the multicast traffic on (all interfaces if not specified). ( IP_ADD_MEMBERSHIP )



158
159
160
161
162
163
164
165
166
# File 'lib/ionian/extension/socket.rb', line 158

def ip_add_membership address = nil, interface = nil
  address   ||= self.remote_address.ip_address
  interface ||= '0.0.0.0'
  
  self.setsockopt \
      ::Socket::IPPROTO_IP,
      ::Socket::IP_ADD_MEMBERSHIP,
      IPAddr.new(address).hton + IPAddr.new(interface).hton
end

- (Object) ip_drop_membership(address = nil, interface = nil)

Leave a multicast group. Address is the class D multicast address (uses remote address if not specified). Interface is the local network interface the multicast traffic is received on (all interfaces if not specified). ( IP_DROP_MEMBERSHIP )



174
175
176
177
178
179
180
181
182
# File 'lib/ionian/extension/socket.rb', line 174

def ip_drop_membership address = nil, interface = nil
  address   ||= self.remote_address.ip_address
  interface ||= '0.0.0.0'
  
  self.setsockopt \
      ::Socket::IPPROTO_IP,
      ::Socket::IP_DROP_MEMBERSHIP,
      IPAddr.new(address).hton + IPAddr.new(interface).hton
end

- (Object) ip_multicast_if

Returns the default interface for outgoing multicasts. ( IP_MULTICAST_IF )



186
187
188
189
# File 'lib/ionian/extension/socket.rb', line 186

def ip_multicast_if
  self.getsockopt(::Socket::IPPROTO_IP, ::Socket::IP_MULTICAST_IF)
    .data.unpack('CCCC').join('.')
end

- (Object) ip_multicast_if=(interface = nil)

Specify default interface for outgoing multicasts. ( IP_MULTICAST_IF )



193
194
195
196
197
198
199
200
# File 'lib/ionian/extension/socket.rb', line 193

def ip_multicast_if= interface = nil
  interface ||= '0.0.0.0'
  
  self.setsockopt \
    ::Socket::IPPROTO_IP,
    ::Socket::IP_MULTICAST_IF,
    IPAddr.new(interface).hton
end

- (Object) ip_multicast_loop Also known as: ip_multicast_loop?

Returns true if loopback of outgoing multicasts is enabled. ( IP_MULTICAST_LOOP )



217
218
219
220
221
# File 'lib/ionian/extension/socket.rb', line 217

def ip_multicast_loop
  param = self.getsockopt(::Socket::IPPROTO_IP, ::Socket::IP_MULTICAST_LOOP)
    .data.unpack('C').first
  param > 0 ? true : false
end

- (Object) ip_multicast_loop=(value)

Enables loopback of outgoing multicasts if true. ( IP_MULTICAST_LOOP )



227
228
229
230
# File 'lib/ionian/extension/socket.rb', line 227

def ip_multicast_loop= value
  param = (!!value && value != 0) ? 1 : 0
  self.setsockopt ::Socket::IPPROTO_IP, ::Socket::IP_MULTICAST_LOOP, [param].pack('C')
end

- (Object) ip_multicast_ttl

Returns the time to live (hop limit) for outgoing multicasts. ( IP_MULTICAST_TTL )



204
205
206
207
# File 'lib/ionian/extension/socket.rb', line 204

def ip_multicast_ttl
  self.getsockopt(::Socket::IPPROTO_IP, ::Socket::IP_MULTICAST_TTL)
    .data.unpack('C').first
end

- (Object) ip_multicast_ttl=(value)

Set the time to live (hop limit) for outgoing multicasts. ( IP_MULTICAST_TTL )



211
212
213
# File 'lib/ionian/extension/socket.rb', line 211

def ip_multicast_ttl= value
  self.setsockopt ::Socket::IPPROTO_IP, ::Socket::IP_MULTICAST_TTL, [value].pack('C')
end

- (Object) ipv6_add_membership

Not yet implemented.



233
234
235
236
# File 'lib/ionian/extension/socket.rb', line 233

def ipv6_add_membership
  # TODO: Implement
  false
end

- (Object) ipv6_drop_membership

Not yet implemented.



239
240
241
242
# File 'lib/ionian/extension/socket.rb', line 239

def ipv6_drop_membership
  # TODO: Implement
  false
end

- (Object) ipv6_multicast_hops

Not yet implemented.



256
257
258
259
# File 'lib/ionian/extension/socket.rb', line 256

def ipv6_multicast_hops
  # TODO: Implement
  false
end

- (Object) ipv6_multicast_hops=(value)

Not yet implemented.



262
263
264
# File 'lib/ionian/extension/socket.rb', line 262

def ipv6_multicast_hops= value
  # TODO: Implement
end

- (Object) ipv6_multicast_if

Not yet implemented.



245
246
247
248
# File 'lib/ionian/extension/socket.rb', line 245

def ipv6_multicast_if
  # TODO: Implement
  false
end

- (Object) ipv6_multicast_if=(value)

Not yet implemented.



251
252
253
# File 'lib/ionian/extension/socket.rb', line 251

def ipv6_multicast_if= value
  # TODO: Implement
end

- (Object) ipv6_multicast_loop Also known as: ipv6_multicast_loop?

Not yet implemented.



267
268
269
270
# File 'lib/ionian/extension/socket.rb', line 267

def ipv6_multicast_loop
  # TODO: Implement
  false
end

- (Object) ipv6_multicast_loop=(value)

Not yet implemented.



275
276
277
# File 'lib/ionian/extension/socket.rb', line 275

def ipv6_multicast_loop= value
  # TODO: Implement
end

- (Object) linger Also known as: linger?

For connection-oriented protocols, prevent #close from returning immediately and try to deliver any data in the send buffer if value is true. ( SO_LINGER )



48
49
50
51
52
# File 'lib/ionian/extension/socket.rb', line 48

def linger
  param = self.getsockopt(::Socket::SOL_SOCKET, ::Socket::SO_LINGER)
    .data.unpack('i').first
  param > 0 ? true : false
end

- (Object) linger=(enable, time: 60)

For connection-oriented protocols, prevent #close from returning immediately and try to deliver any data in the send buffer if value is true.

Args:

Time: Time in seconds to remain open before discarding data and
      sending a RST packet.

( SO_LINGER )



62
63
64
65
66
67
# File 'lib/ionian/extension/socket.rb', line 62

def linger= enable, time: 60
  # TODO: Passing a kwarg doesn't work here because of the
  #       assignment operator. Causes parser error.
  param = (!!enable && enable != 0) ? 1 : 0
  self.setsockopt ::Socket::SOL_SOCKET, ::Socket::SO_LINGER, [param, time.to_i].pack('ii')
end

- (Object) multicast Also known as: multicast?

Returns true if the socket's address is in the multicast range.



290
291
292
# File 'lib/ionian/extension/socket.rb', line 290

def multicast
  Ionian::Extension::Socket.multicast self.remote_address.ip_address
end

- (Object) no_delay Also known as: no_delay?

Returns true if the Nagle algorithm is disabled. ( TCP_NODELAY )



105
106
107
108
109
# File 'lib/ionian/extension/socket.rb', line 105

def no_delay
  param = self.getsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY)
    .data.unpack('i').first
  param > 0 ? true : false
end

- (Object) no_delay=(value)

Disables the Nagle algorithm if true. ( TCP_NODELAY )



115
116
117
118
# File 'lib/ionian/extension/socket.rb', line 115

def no_delay= value
  param = (!!value && value != 0) ? 1 : 0
  self.setsockopt ::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY, [param].pack('i')
end

- (Object) recork

Unsets cork to transmit data, then reapplies cork. ( TCP_CORK )



147
148
149
150
# File 'lib/ionian/extension/socket.rb', line 147

def recork
  self.setsockopt ::Socket::IPPROTO_TCP, ::Socket::TCP_CORK, [0].pack('i')
  self.setsockopt ::Socket::IPPROTO_TCP, ::Socket::TCP_CORK, [1].pack('i')
end

- (Object) reuse_addr Also known as: reuse_addr?

Returns true if local address reuse is allowed. ( SO_REUSEADDR )



73
74
75
76
77
# File 'lib/ionian/extension/socket.rb', line 73

def reuse_addr
  param = self.getsockopt(::Socket::SOL_SOCKET, ::Socket::SO_REUSEADDR)
    .data.unpack('i').first
  param > 0 ? true : false
end

- (Object) reuse_addr=(value)

Allows local address reuse if true. ( SO_REUSEADDR )



83
84
85
86
# File 'lib/ionian/extension/socket.rb', line 83

def reuse_addr= value
  param = (!!value && value != 0) ? 1 : 0
  self.setsockopt ::Socket::SOL_SOCKET, ::Socket::SO_REUSEADDR, [param].pack('i')
end

- (Object) ttl Also known as: ttl?

Returns the time to live (hop limit). ( IP_TTL )



90
91
92
93
# File 'lib/ionian/extension/socket.rb', line 90

def ttl
  self.getsockopt(::Socket::IPPROTO_IP, ::Socket::IP_TTL)
    .data.unpack('i').first
end

- (Object) ttl=(value)

Sets the time to live (hop limit). ( IP_TTL )



99
100
101
# File 'lib/ionian/extension/socket.rb', line 99

def ttl= value
  self.setsockopt ::Socket::IPPROTO_IP, ::Socket::IP_TTL, [value].pack('i')
end