Class: Scriptroute::IPv4

Inherits:
Object
  • Object
show all
Defined in:
lib/scriptroute/packets.rb,
lib/scriptroute/packets.rb

Overview

as strings to hand off to the interpreter.

Direct Known Subclasses

ICMP, TCP, UDP

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

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ip_dstIPaddress

Returns:



152
153
154
# File 'lib/scriptroute/packets.rb', line 152

def ip_dst
  @ip_dst
end

#ip_hlFixnum (readonly)

Returns:

  • (Fixnum)


149
150
151
# File 'lib/scriptroute/packets.rb', line 149

def ip_hl
  @ip_hl
end

#ip_idFixnum

Returns:

  • (Fixnum)


149
150
151
# File 'lib/scriptroute/packets.rb', line 149

def ip_id
  @ip_id
end

#ip_lenFixnum (readonly)

Returns:

  • (Fixnum)


149
150
151
# File 'lib/scriptroute/packets.rb', line 149

def ip_len
  @ip_len
end

#ip_offFixnum

Returns:

  • (Fixnum)


149
150
151
# File 'lib/scriptroute/packets.rb', line 149

def ip_off
  @ip_off
end

#ip_optionsArray<IPv4option>

Returns:



154
155
156
# File 'lib/scriptroute/packets.rb', line 154

def ip_options
  @ip_options
end

#ip_pFixnum (readonly)

Returns:

  • (Fixnum)


149
150
151
# File 'lib/scriptroute/packets.rb', line 149

def ip_p
  @ip_p
end

#ip_srcIPaddress (readonly)

Returns:



152
153
154
# File 'lib/scriptroute/packets.rb', line 152

def ip_src
  @ip_src
end

#ip_sumFixnum

Returns:

  • (Fixnum)


149
150
151
# File 'lib/scriptroute/packets.rb', line 149

def ip_sum
  @ip_sum
end

#ip_tosFixnum

Returns:

  • (Fixnum)


149
150
151
# File 'lib/scriptroute/packets.rb', line 149

def ip_tos
  @ip_tos
end

#ip_ttlFixnum

Returns:

  • (Fixnum)


149
150
151
# File 'lib/scriptroute/packets.rb', line 149

def ip_ttl
  @ip_ttl
end

#ip_vFixnum (readonly)

Returns:

  • (Fixnum)


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.

Parameters:

  • str (String)

    the bytes of the packet to unpack.



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.

Parameters:

  • opt (IPv4option)

    add a record route or timestamp option.



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_lenvoid

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 + ((ip_options.map { |o| o.ipt_len }.sum)/4.0).ceil, 15 ].min # at most 15
end

#calculate_packet_lenvoid

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_lenFixnum

Returns the length of the payload, intended to be implemented by subclasses.

Returns:

  • (Fixnum)

    the length of the payload, intended to be implemented by subclasses.

Raises:

  • (RuntimeError)

    if invoked directly instead of being overloaded.



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.

Parameters:

  • str (String)

    The string from which to unpack.



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_sString

Returns:

  • (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