lib/packetgen/header/tcp.rb in packetgen-2.4.0 vs lib/packetgen/header/tcp.rb in packetgen-2.5.0

- old
+ new

@@ -1,15 +1,22 @@ +# This file is part of PacketGen +# See https://github.com/sdaubert/packetgen for more informations +# Copyright (C) 2016 Sylvain Daubert <sylvain.daubert@laposte.net> +# This program is published under MIT license. + +# frozen_string_literal: true + module PacketGen module Header # A TCP header consists of: # * a source port ({#sport}, {Types::Int16} type), # * a destination port ({#dport}, +Int16+ type), # * a sequence number ({#seqnum}, {Types::Int32} type), # * an acknownledge number ({#acknum}, +Int32+ type), # * a 16-bit field ({#u16}, +Int16+ type) composed of: - # * a 4-bit {#data_offset} value, + # * a 4-bit {#data_offset} self[attr], # * a 3-bit {#reserved} field, # * a 9-bit {#flags} field, # * a {#window} field (+Int16+ type), # * a {#checksum} field (+Int16+ type), # * a urgent pointer ({#urg_pointer}, +Int16+ type), @@ -42,11 +49,11 @@ # # == Options # {#options} TCP attribute is a {Options}. {Option} may added to it: # tcph.options << PacketGen::Header::TCP::MSS.new(1250) # or: - # tcph.options << { opt: 'MSS', value: 1250 } + # tcph.options << { opt: 'MSS', self[attr]: 1250 } # @author Sylvain Daubert class TCP < Base end end end @@ -186,19 +193,12 @@ # @return [Integer] def calc_checksum sum = ip_header(self).pseudo_header_checksum sum += IP_PROTOCOL sum += self.sz - str = self.to_s - str << "\x00" if str.length % 2 == 1 - sum += str.unpack('n*').reduce(:+) - - while sum > 0xffff do - sum = (sum & 0xffff) + (sum >> 16) - end - sum = ~sum & 0xffff - self[:checksum].value = (sum == 0) ? 0xffff : sum + sum += IP.sum16(self) + self.checksum = IP.reduce_checksum(sum) end # Compute header length and set +data_offset+ field # @return [Integer] def calc_length @@ -210,17 +210,17 @@ # @return [String] def inspect str = Inspect.dashed_line(self.class, 2) shift = Inspect.shift_level(2) - to_h.each do |attr, value| + fields.each do |attr| next if attr == :body - str << Inspect.inspect_attribute(attr, value, 2) + str << Inspect.inspect_attribute(attr, self[attr], 2) if attr == :u16 doff = Inspect.int_dec_hex(data_offset, 1) str << shift + Inspect::FMT_ATTR % ['', 'data_offset', doff] str << shift + Inspect::FMT_ATTR % ['', 'reserved', reserved] - flags = '' + flags = ''.dup %w(ns cwr ece urg ack psh rst syn fin).each do |fl| flags << (send("flag_#{fl}?") ? fl[0].upcase : '.') end str << shift + Inspect::FMT_ATTR % ['', 'flags', flags] end