Module: Scriptroute::UDPgeneric

Included in:
UDP, UDP6
Defined in:
lib/scriptroute/packets.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#uh_dportFixnum

Returns:

  • (Fixnum)


541
542
543
# File 'lib/scriptroute/packets.rb', line 541

def uh_dport
  @uh_dport
end

#uh_sportFixnum (readonly)

Returns:

  • (Fixnum)


541
542
543
# File 'lib/scriptroute/packets.rb', line 541

def uh_sport
  @uh_sport
end

#uh_sumFixnum

Returns:

  • (Fixnum)


541
542
543
# File 'lib/scriptroute/packets.rb', line 541

def uh_sum
  @uh_sum
end

#uh_ulenFixnum (readonly)

Returns:

  • (Fixnum)


541
542
543
# File 'lib/scriptroute/packets.rb', line 541

def uh_ulen
  @uh_ulen
end

Instance Method Details

#initialize(paylen_or_str = 0) ⇒ Object

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

Parameters:

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

    size or contents



552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# File 'lib/scriptroute/packets.rb', line 552

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( Scriptroute::IP::IPPROTO_UDP )
  else
    @uh_sport, @uh_dport, @uh_ulen, @uh_sum = paylen_or_str.unpack("nnnn")
  end
end

#ip_payload_lenFixnum

Returns the udp data plus header length, uh_ulen.

Returns:

  • (Fixnum)

    the udp data plus header length, uh_ulen



545
546
547
548
# File 'lib/scriptroute/packets.rb', line 545

def ip_payload_len 
  raise "ip_payload_len was nil when asked" unless @uh_ulen
  @uh_ulen
end

#marshalString

Returns header and payload of this UDP datagram.

Returns:

  • (String)

    header and payload of this UDP datagram.



569
570
571
572
573
574
575
576
577
578
579
580
581
# File 'lib/scriptroute/packets.rb', line 569

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 or self.class == UDP6 ) then
      "\0" * ( @uh_ulen - 8 )
    else
      "" # the subclass will take care of it
    end
end

#to_sString

Returns:

  • (String)


592
593
594
# File 'lib/scriptroute/packets.rb', line 592

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.



587
588
589
# File 'lib/scriptroute/packets.rb', line 587

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