Class: Scriptroute::UDP

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

Direct Known Subclasses

NTP

Constant Summary

Constants inherited from IPv4

IPv4::IPPROTO_ICMP, IPv4::IPPROTO_TCP, IPv4::IPPROTO_UDP

Instance Attribute Summary collapse

Attributes inherited from IPv4

#ip_dst, #ip_hl, #ip_id, #ip_len, #ip_off, #ip_options, #ip_p, #ip_src, #ip_sum, #ip_tos, #ip_ttl, #ip_v

Instance Method Summary collapse

Methods inherited from IPv4

#add_option, #calculate_header_len, #calculate_packet_len, creator, #ipv4_unmarshal

Constructor Details

#initialize(paylen_or_str = 0) ⇒ UDP

Create a new UDP packet from a payload size or from contents.

Parameters:

  • paylen_or_str (Integer, String) (defaults to: 0)

    size or contents



424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'lib/scriptroute/packets.rb', line 424

def initialize(paylen_or_str = 0)
  if(paylen_or_str.is_a?(Fixnum)) then
    if( paylen_or_str < 0) then raise "payload length must be >= 0" end
    @uh_ulen = paylen_or_str + 8
    if(@uh_ulen > 1480) then
      raise "desired packet too big"
    end
    @uh_sport = 32945
    @uh_dport = 33434
    @uh_sum = 0
    super( IPPROTO_UDP )
  else
    @uh_sport, @uh_dport, @uh_ulen, @uh_sum = paylen_or_str.unpack("nnnn")
  end
end

Instance Attribute Details

#uh_dportFixnum

Returns:

  • (Fixnum)


403
404
405
# File 'lib/scriptroute/packets.rb', line 403

def uh_dport
  @uh_dport
end

#uh_sportFixnum (readonly)

Returns:

  • (Fixnum)


403
404
405
# File 'lib/scriptroute/packets.rb', line 403

def uh_sport
  @uh_sport
end

#uh_sumFixnum

Returns:

  • (Fixnum)


403
404
405
# File 'lib/scriptroute/packets.rb', line 403

def uh_sum
  @uh_sum
end

#uh_ulenFixnum (readonly)

Returns:

  • (Fixnum)


403
404
405
# File 'lib/scriptroute/packets.rb', line 403

def uh_ulen
  @uh_ulen
end

Instance Method Details

#ip_payload_lenFixnum

Returns the udp data plus header length, uh_ulen.

Returns:

  • (Fixnum)

    the udp data plus header length, uh_ulen



418
419
420
# File 'lib/scriptroute/packets.rb', line 418

def ip_payload_len 
  @uh_ulen
end

#marshalString

Returns header and payload of this UDP datagram.

Returns:

  • (String)

    header and payload of this UDP datagram.



441
442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'lib/scriptroute/packets.rb', line 441

def marshal
  #    payload = "a%d"% (@payload_len)
  #    puts payload
  if(@uh_ulen < 8) then warn "uh_ulen should be at least 8" end
  array_of_elements = [ @uh_sport, @uh_dport, @uh_ulen, @uh_sum ]
  raise "a UDP header field was unset" if array_of_elements.include?(nil)
  super + [ @uh_sport, @uh_dport, @uh_ulen, @uh_sum ].pack("nnnn")  + 
    if ( self.class == UDP ) then
      "\0" * ( @uh_ulen - 8 )
    else
      "" # the subclass will take care of it
    end
  
end

#to_sString

Returns:

  • (String)


465
466
467
# File 'lib/scriptroute/packets.rb', line 465

def to_s
  super + " UDP %d > %d len %d" % [ @uh_sport, @uh_dport, @uh_ulen ] 
end

#udp_unmarshal(str) ⇒ void

This method returns an undefined value.

Used for subclasses to set the UDP header fields.

Parameters:

  • str (String)

    the udp header to parse into ports, length, and checksum.



460
461
462
# File 'lib/scriptroute/packets.rb', line 460

def udp_unmarshal(str)
  @uh_sport, @uh_dport, @uh_ulen, @uh_sum = str.unpack("nnnn")
end