Class: Datalink::Socket

Inherits:
Socket
  • Object
show all
Defined in:
lib/dl_socket_linux.rb,
lib/dl_socket_darwin.rb

Direct Known Subclasses

Ethernet::Socket

Constant Summary collapse

IFF_UP =
0x0001
IFF_BROADCAST =
0x0002
IFF_DEBUG =
0x0004
IFF_LOOPBACK =
0x0008
IFF_P2P =
0x0010
IFF_NOTRAILERS =
0x0020
IFF_RUNNING =
0x0040
IFF_NOARP =
0x0080
IFF_PROMISC =
0x0100
IFF_ALLMULTI =
0x0200
IFF_MASTER =
0x0400
IFF_SLAVE =
0x0800
IFF_MULTICAST =
0x1000
SIOCGIFINDEX =
0x8933
SIOCGIFFLAGS =
0x8913
SIOCSIFFLAGS =
0x8914
PF_PACKET =
17
AF_PACKET =
PF_PACKET
IFNAMSIZ =
16

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(if_name) ⇒ Socket

Returns a new instance of Socket.



48
49
50
51
52
53
54
# File 'lib/dl_socket_linux.rb', line 48

def initialize(if_name, proto=0x003)
  @if_name = if_name
  super(PF_PACKET, Socket::SOCK_RAW, proto)
  ifreq = [if_name.dup].pack 'a32'
  ioctl(SIOCGIFINDEX, ifreq)
  bind [AF_PACKET].pack('s') + [proto].pack('n') + ifreq[16..20]+ ("\x00" * 12)
end

Instance Attribute Details

#deviceObject (readonly)

Returns the value of attribute device.



32
33
34
# File 'lib/dl_socket_darwin.rb', line 32

def device
  @device
end

#device_nameObject (readonly)

Returns the value of attribute device_name.



32
33
34
# File 'lib/dl_socket_darwin.rb', line 32

def device_name
  @device_name
end

#if_nameObject

Returns the value of attribute if_name.



31
32
33
# File 'lib/dl_socket_darwin.rb', line 31

def if_name
  @if_name
end

Instance Method Details

#closeObject



73
74
75
# File 'lib/dl_socket_darwin.rb', line 73

def close
  @file_handle.close
end

#flagsObject



29
30
31
# File 'lib/dl_socket_linux.rb', line 29

def flags
  read_flags
end

#open(if_name = @if_name) ⇒ Object



70
71
# File 'lib/dl_socket_linux.rb', line 70

def open
end

#recvObject



56
57
58
59
60
# File 'lib/dl_socket_linux.rb', line 56

def recv
  data, from = recvfrom(2048)
  ether_type = data[12,2].unpack('n')[0]
  [data, ether_type, Time.now]
end

#send(obj) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/dl_socket_linux.rb', line 62

def send(obj)
  if obj.respond_to?(:encode)
    super(obj.encode,0)
  else
    super(obj,0)
  end
end