Class: Lib::BOOTP::Packet

Inherits:
Object
  • Object
show all
Defined in:
lib/lib/bootp.rb,
lib/lib/bootp/packet/flags.rb,
lib/lib/bootp/packet/op_code.rb,
lib/lib/bootp/packet/seconds.rb,
lib/lib/bootp/packet/boot_file.rb,
lib/lib/bootp/packet/hop_count.rb,
lib/lib/bootp/packet/ip_address.rb,
lib/lib/bootp/packet/transaction_id.rb,
lib/lib/bootp/packet/server_host_name.rb,
lib/lib/bootp/packet/hardware_address_type.rb,
lib/lib/bootp/packet/client_hardware_address.rb,
lib/lib/bootp/packet/hardware_address_length.rb

Defined Under Namespace

Classes: BootFile, ClientHardwareAddress, Flags, HardwareAddressLength, HardwareAddressType, HopCount, IPAddress, OpCode, Seconds, ServerHostName, TransactionID

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(op: 1, htype: 1, hlen: 6, hops: 0, xid: nil, secs: 0, flags: 0, ciaddr: 0, yiaddr: 0, siaddr: 0, giaddr: 0, chaddr: nil, sname: '.', file: '.') {|_self| ... } ⇒ Packet

Returns a new instance of Packet.

Yields:

  • (_self)

Yield Parameters:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/lib/bootp.rb', line 38

def initialize(op: 1, htype: 1, hlen: 6, hops: 0, xid: nil, secs: 0, flags: 0, ciaddr: 0, yiaddr: 0, siaddr: 0, giaddr: 0, chaddr: nil, sname: '.', file: '.')
  packets[:op]     = (op.is_a? Lib::BOOTP::Packet::OpCode) ? op : Lib::BOOTP::Packet::OpCode.new(op)
  packets[:htype]  = (htype.is_a? Lib::BOOTP::Packet::HardwareAddressType) ? htype : Lib::BOOTP::Packet::HardwareAddressType.new(htype)
  packets[:hlen]   = (hlen.is_a? Lib::BOOTP::Packet::HardwareAddressLength) ? hlen : Lib::BOOTP::Packet::HardwareAddressLength.new(hlen)
  packets[:hops]   = (hops.is_a? Lib::BOOTP::Packet::HopCount) ? hops : Lib::BOOTP::Packet::HopCount.new(hops)
  packets[:xid]    = (xid.is_a? Lib::BOOTP::Packet::TransactionID) ? xid : Lib::BOOTP::Packet::TransactionID.new(xid)
  packets[:secs]   = (secs.is_a? Lib::BOOTP::Packet::Seconds) ? secs : Lib::BOOTP::Packet::Seconds.new(secs)
  packets[:flags]  = (flags.is_a? Lib::BOOTP::Packet::Flags) ? flags : Lib::BOOTP::Packet::Flags.new(flags)
  packets[:ciaddr] = (ciaddr.is_a? Lib::BOOTP::Packet::IPAddress) ? ciaddr : Lib::BOOTP::Packet::IPAddress.new(ciaddr)
  packets[:yiaddr] = (yiaddr.is_a? Lib::BOOTP::Packet::IPAddress) ? yiaddr : Lib::BOOTP::Packet::IPAddress.new(yiaddr)
  packets[:siaddr] = (siaddr.is_a? Lib::BOOTP::Packet::IPAddress) ? siaddr : Lib::BOOTP::Packet::IPAddress.new(siaddr)
  packets[:giaddr] = (giaddr.is_a? Lib::BOOTP::Packet::IPAddress) ? giaddr : Lib::BOOTP::Packet::IPAddress.new(giaddr)
  packets[:chaddr] = (chaddr.is_a? Lib::BOOTP::Packet::ClientHardwareAddress) ? chaddr : Lib::BOOTP::Packet::ClientHardwareAddress.new(chaddr)
  packets[:sname]  = (sname.is_a? Lib::BOOTP::Packet::ServerHostName) ? sname : Lib::BOOTP::Packet::ServerHostName.new(sname)
  packets[:file]   = (file.is_a? Lib::BOOTP::Packet::BootFile) ? file : Lib::BOOTP::Packet::BootFile.new(file)
  yield self if block_given?
end

Class Method Details

.from_json(json) ⇒ Object



175
176
177
178
179
180
181
182
# File 'lib/lib/bootp.rb', line 175

def self.from_json(json)
  json = json.is_a?(Hash) ? JSON.parse(json.to_json, symbolize_names: true) : JSON.parse(json, symbolize_names: true)
  self.new do |p|
    json.each_pair do |k,v|
      p.send("#{k}=".to_sym, v) unless k == :options
    end
  end
end

.unpack(packet) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/lib/bootp.rb', line 151

def self.unpack(packet)
  op, htype, hlen, hops, xid, secs, flags, ciaddr, yiaddr, siaddr, giaddr, chaddr, sname, file = packet.unpack('C4NnnN4a16a64a128')
  op = Lib::BOOTP::Packet::OpCode.new op
  htype = Lib::BOOTP::Packet::HardwareAddressType.new htype
  hlen = Lib::BOOTP::Packet::HardwareAddressLength.new hlen
  hops = Lib::BOOTP::Packet::HopCount.new hops
  xid = Lib::BOOTP::Packet::TransactionID.new xid
  secs = Lib::BOOTP::Packet::Seconds.new secs
  flags = Lib::BOOTP::Packet::Flags.new flags
  ciaddr = Lib::BOOTP::Packet::IPAddress.new ciaddr
  yiaddr = Lib::BOOTP::Packet::IPAddress.new yiaddr
  siaddr = Lib::BOOTP::Packet::IPAddress.new siaddr
  giaddr = Lib::BOOTP::Packet::IPAddress.new giaddr
  chaddr = Lib::BOOTP::Packet::ClientHardwareAddress.unpack chaddr
  sname = Lib::BOOTP::Packet::ServerHostName.unpack sname
  file = Lib::BOOTP::Packet::BootFile.unpack file

  self.new(op:op, htype:htype, hlen:hlen, hops:hops, xid:xid, secs:secs, flags:flags, ciaddr:ciaddr, yiaddr:yiaddr, siaddr:siaddr, giaddr:giaddr, chaddr:chaddr, sname:sname, file:file)
end

Instance Method Details

#chaddr=(chaddr) ⇒ Object



100
101
102
# File 'lib/lib/bootp.rb', line 100

def chaddr=(chaddr)
  packets[:chaddr] = (chaddr.is_a? Lib::BOOTP::Packet::ClientHardwareAddress) ? chaddr : Lib::BOOTP::Packet::ClientHardwareAddress.new(chaddr)
end

#ciaddr=(ciaddr) ⇒ Object



84
85
86
# File 'lib/lib/bootp.rb', line 84

def ciaddr=(ciaddr)
  packets[:ciaddr] = (ciaddr.is_a? Lib::BOOTP::Packet::IPAddress) ? ciaddr : Lib::BOOTP::Packet::IPAddress.new(ciaddr)
end

#file=(file) ⇒ Object



108
109
110
# File 'lib/lib/bootp.rb', line 108

def file=(file)
  packets[:file] = (file.is_a? Lib::BOOTP::Packet::BootFile) ? file : Lib::BOOTP::Packet::BootFile.new(file)
end

#flags=(flags) ⇒ Object



80
81
82
# File 'lib/lib/bootp.rb', line 80

def flags=(flags)
  packets[:flags] = (flags.is_a? Lib::BOOTP::Packet::Flags) ? flags : Lib::BOOTP::Packet::Flags.new(flags)
end

#giaddr=(giaddr) ⇒ Object



96
97
98
# File 'lib/lib/bootp.rb', line 96

def giaddr=(giaddr)
  packets[:giaddr] = (giaddr.is_a? Lib::BOOTP::Packet::IPAddress) ? giaddr : Lib::BOOTP::Packet::IPAddress.new(giaddr)
end

#hlen=(hlen) ⇒ Object



64
65
66
# File 'lib/lib/bootp.rb', line 64

def hlen=(hlen)
  packets[:hlen] = (hlen.is_a? Lib::BOOTP::Packet::HardwareAddressLength) ? hlen : Lib::BOOTP::Packet::HardwareAddressLength.new(hlen)
end

#hops=(hops) ⇒ Object



68
69
70
# File 'lib/lib/bootp.rb', line 68

def hops=(hops)
  packets[:hops] = (hops.is_a? Lib::BOOTP::Packet::HopCount) ? hops : Lib::BOOTP::Packet::HopCount.new(hops)
end

#htype=(htype) ⇒ Object



60
61
62
# File 'lib/lib/bootp.rb', line 60

def htype=(htype)
  packets[:htype] = (htype.is_a? Lib::BOOTP::Packet::HardwareAddressType) ? htype : Lib::BOOTP::Packet::HardwareAddressType.new(htype)
end

#op=(op) ⇒ Object



56
57
58
# File 'lib/lib/bootp.rb', line 56

def op=(op)
  packets[:op] = (op.is_a? Lib::BOOTP::Packet::OpCode) ? op : Lib::BOOTP::Packet::OpCode.new(op)
end

#packObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/lib/bootp.rb', line 134

def pack
  self.op.pack +
      self.htype.pack +
      self.hlen.pack +
      self.hops.pack +
      self.xid.pack +
      self.secs.pack +
      self.flags.pack +
      self.ciaddr.pack +
      self.yiaddr.pack +
      self.siaddr.pack +
      self.giaddr.pack +
      self.chaddr.pack +
      self.sname.pack +
      self.file.pack
end

#secs=(secs) ⇒ Object



76
77
78
# File 'lib/lib/bootp.rb', line 76

def secs=(secs)
  packets[:secs] = (secs.is_a? Lib::BOOTP::Packet::Seconds) ? secs : Lib::BOOTP::Packet::Seconds.new(secs)
end

#siaddr=(siaddr) ⇒ Object



92
93
94
# File 'lib/lib/bootp.rb', line 92

def siaddr=(siaddr)
  packets[:siaddr] = (siaddr.is_a? Lib::BOOTP::Packet::IPAddress) ? siaddr : Lib::BOOTP::Packet::IPAddress.new(siaddr)
end

#sname=(sname) ⇒ Object



104
105
106
# File 'lib/lib/bootp.rb', line 104

def sname=(sname)
  packets[:sname] = (sname.is_a? Lib::BOOTP::Packet::ServerHostName) ? sname : Lib::BOOTP::Packet::ServerHostName.new(sname)
end

#to_hObject



113
114
115
# File 'lib/lib/bootp.rb', line 113

def to_h
  packets
end

#to_jsonObject



171
172
173
# File 'lib/lib/bootp.rb', line 171

def to_json
  to_h.to_json
end

#to_sObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/lib/bootp.rb', line 117

def to_s
  "OP:        #{self.op}"+
      "\nHTYPE:     #{self.htype}"+
      "\nHLEN:      #{self.hlen}"+
      "\nHOPS:      #{self.hops}"+
      "\nXID:       #{self.xid}"+
      "\nSECS:      #{self.secs}"+
      "\nFLAGS:     #{self.flags}"+
      "\nCIADDR:    #{self.ciaddr}"+
      "\nYIADDR:    #{self.yiaddr}"+
      "\nSIADDR:    #{self.siaddr}"+
      "\nGIADDR:    #{self.giaddr}"+
      "\nCHADDR:    #{self.chaddr}"+
      "\nSNAME:     #{self.sname}"+
      "\nFILE:      #{self.file}"
end

#xid=(xid) ⇒ Object



72
73
74
# File 'lib/lib/bootp.rb', line 72

def xid=(xid)
  packets[:xid] = (xid.is_a? Lib::BOOTP::Packet::TransactionID) ? xid : Lib::BOOTP::Packet::TransactionID.new(xid)
end

#yiaddr=(yiaddr) ⇒ Object



88
89
90
# File 'lib/lib/bootp.rb', line 88

def yiaddr=(yiaddr)
  packets[:yiaddr] = (yiaddr.is_a? Lib::BOOTP::Packet::IPAddress) ? yiaddr : Lib::BOOTP::Packet::IPAddress.new(yiaddr)
end