Class: Scriptroute::IPv4
- Inherits:
-
Object
- Object
- Scriptroute::IPv4
- Defined in:
- lib/scriptroute/packets.rb,
lib/scriptroute/packets.rb
Overview
as strings to hand off to the interpreter.
Constant Summary collapse
- IPPROTO_ICMP =
1
- IPPROTO_TCP =
6
- IPPROTO_UDP =
17
- @@creators =
a by-ip_p table of packet instantiators.
Hash.new
Instance Attribute Summary collapse
- #ip_dst ⇒ IPaddress
- #ip_hl ⇒ Fixnum readonly
- #ip_id ⇒ Fixnum
- #ip_len ⇒ Fixnum readonly
- #ip_off ⇒ Fixnum
- #ip_options ⇒ Array<IPv4option>
- #ip_p ⇒ Fixnum readonly
- #ip_src ⇒ IPaddress readonly
- #ip_sum ⇒ Fixnum
- #ip_tos ⇒ Fixnum
- #ip_ttl ⇒ Fixnum
- #ip_v ⇒ Fixnum readonly
Class Method Summary collapse
-
.creator(str) ⇒ Object
invoke a factory(?) to create a TCP/UDP/ICMP as appropriate, then unmarshal the IPv4 header contents.
Instance Method Summary collapse
- #add_option(opt) ⇒ void
-
#calculate_header_len ⇒ void
sets ip_hl, should not be necessary to call this explicitly.
- #calculate_packet_len ⇒ void
-
#ip_payload_len ⇒ Fixnum
The length of the payload, intended to be implemented by subclasses.
-
#ipv4_unmarshal(str) ⇒ void
unpack the ipv4 component of the packet, even if we’re unmarshaling an instance of a subclass.
- #to_s ⇒ String
Instance Attribute Details
#ip_dst ⇒ IPaddress
152 153 154 |
# File 'lib/scriptroute/packets.rb', line 152 def ip_dst @ip_dst end |
#ip_hl ⇒ Fixnum (readonly)
149 150 151 |
# File 'lib/scriptroute/packets.rb', line 149 def ip_hl @ip_hl end |
#ip_id ⇒ Fixnum
149 150 151 |
# File 'lib/scriptroute/packets.rb', line 149 def ip_id @ip_id end |
#ip_len ⇒ Fixnum (readonly)
149 150 151 |
# File 'lib/scriptroute/packets.rb', line 149 def ip_len @ip_len end |
#ip_off ⇒ Fixnum
149 150 151 |
# File 'lib/scriptroute/packets.rb', line 149 def ip_off @ip_off end |
#ip_options ⇒ Array<IPv4option>
154 155 156 |
# File 'lib/scriptroute/packets.rb', line 154 def @ip_options end |
#ip_p ⇒ Fixnum (readonly)
149 150 151 |
# File 'lib/scriptroute/packets.rb', line 149 def ip_p @ip_p end |
#ip_src ⇒ IPaddress (readonly)
152 153 154 |
# File 'lib/scriptroute/packets.rb', line 152 def ip_src @ip_src end |
#ip_sum ⇒ Fixnum
149 150 151 |
# File 'lib/scriptroute/packets.rb', line 149 def ip_sum @ip_sum end |
#ip_tos ⇒ Fixnum
149 150 151 |
# File 'lib/scriptroute/packets.rb', line 149 def ip_tos @ip_tos end |
#ip_ttl ⇒ Fixnum
149 150 151 |
# File 'lib/scriptroute/packets.rb', line 149 def ip_ttl @ip_ttl end |
#ip_v ⇒ Fixnum (readonly)
149 150 151 |
# File 'lib/scriptroute/packets.rb', line 149 def ip_v @ip_v end |
Class Method Details
.creator(str) ⇒ Object
invoke a factory(?) to create a TCP/UDP/ICMP as appropriate, then unmarshal the IPv4 header contents.
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/scriptroute/packets.rb', line 225 def IPv4.creator(str) ip_vhl, ip_tos, ip_len, ip_id, ip_off, ip_ttl, ip_p, ip_sum, ip_src, ip_dst = str.unpack("ccn" + "nn" + "ccn" + "N" + "N"); ip_hl = ip_vhl & 0xf; if(@@creators[ip_p]) then pkt = (@@creators[ip_p]).call(str[(ip_hl * 4) .. ip_len]) pkt.ipv4_unmarshal(str) pkt else raise "unknown protocol #%d in %s" % [ ip_p, str.unpack("C*").map { |c| "%x" % c }.join(' ') ] end end |
Instance Method Details
#add_option(opt) ⇒ void
This method returns an undefined value.
194 195 196 197 198 |
# File 'lib/scriptroute/packets.rb', line 194 def add_option(opt) opt.is_a?(IPv4option) or raise "can add only IPv4options" @ip_options.push(opt) calculate_header_len end |
#calculate_header_len ⇒ void
This method returns an undefined value.
sets ip_hl, should not be necessary to call this explicitly.
183 184 185 |
# File 'lib/scriptroute/packets.rb', line 183 def calculate_header_len @ip_hl = [ 5 + ((.map { |o| o.ipt_len }.sum)/4.0).ceil, 15 ].min # at most 15 end |
#calculate_packet_len ⇒ void
This method returns an undefined value.
187 188 189 190 191 |
# File 'lib/scriptroute/packets.rb', line 187 def calculate_packet_len calculate_header_len # ensures @ip_hl is set properly raise "ip_payload_len not calculated" unless ip_payload_len @ip_len = ip_payload_len + (@ip_hl * 4) end |
#ip_payload_len ⇒ Fixnum
Returns the length of the payload, intended to be implemented by subclasses.
178 179 180 |
# File 'lib/scriptroute/packets.rb', line 178 def ip_payload_len raise "ip_payload_len is a pure virtual function in IPv4" end |
#ipv4_unmarshal(str) ⇒ void
This method returns an undefined value.
unpack the ipv4 component of the packet, even if we’re unmarshaling an instance of a subclass.
245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/scriptroute/packets.rb', line 245 def ipv4_unmarshal(str) ip_vhl, @ip_tos, @ip_len, @ip_id, @ip_off, @ip_ttl, @ip_p, @ip_sum, ip_src, ip_dst = str.unpack("CCn" + "nn" + "CCn" + "N" + "N"); @ip_src, @ip_dst = [ip_src, ip_dst].map { |addr| IPaddress.new(addr) } @ip_hl = ip_vhl & 0xf; @ip_v = (ip_vhl & 0xf0) >> 4; @ip_options = Array.new if(@ip_hl > 5) then add_option(IPv4option.creator(str[20 .. (@ip_hl*4)])) end end |
#to_s ⇒ String
260 261 262 263 |
# File 'lib/scriptroute/packets.rb', line 260 def to_s "%s > %s ttl%d" % [ @ip_src, @ip_dst, @ip_ttl ] + @ip_options.map { |o| o.to_s }.join(", ") end |