packetfu 1.1.8 → 1.1.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. data/README.rdoc +11 -12
  2. data/bench/octets.rb +9 -9
  3. data/examples/100kpackets.rb +13 -12
  4. data/examples/ackscan.rb +17 -16
  5. data/examples/arp.rb +36 -35
  6. data/examples/arphood.rb +37 -36
  7. data/examples/dissect_thinger.rb +7 -6
  8. data/examples/ethernet.rb +1 -0
  9. data/examples/examples.rb +1 -0
  10. data/examples/ifconfig.rb +1 -0
  11. data/examples/new-simple-stats.rb +24 -23
  12. data/examples/packetfu-shell.rb +26 -25
  13. data/examples/simple-sniffer.rb +10 -9
  14. data/examples/simple-stats.rb +24 -23
  15. data/examples/slammer.rb +4 -3
  16. data/lib/packetfu.rb +128 -127
  17. data/lib/packetfu/capture.rb +170 -169
  18. data/lib/packetfu/config.rb +53 -52
  19. data/lib/packetfu/inject.rb +57 -56
  20. data/lib/packetfu/packet.rb +529 -528
  21. data/lib/packetfu/pcap.rb +580 -579
  22. data/lib/packetfu/protos/arp.rb +91 -90
  23. data/lib/packetfu/protos/arp/header.rb +159 -158
  24. data/lib/packetfu/protos/arp/mixin.rb +37 -36
  25. data/lib/packetfu/protos/eth.rb +45 -44
  26. data/lib/packetfu/protos/eth/header.rb +244 -243
  27. data/lib/packetfu/protos/eth/mixin.rb +4 -3
  28. data/lib/packetfu/protos/hsrp.rb +70 -69
  29. data/lib/packetfu/protos/hsrp/header.rb +108 -107
  30. data/lib/packetfu/protos/hsrp/mixin.rb +30 -29
  31. data/lib/packetfu/protos/icmp.rb +72 -71
  32. data/lib/packetfu/protos/icmp/header.rb +83 -82
  33. data/lib/packetfu/protos/icmp/mixin.rb +15 -14
  34. data/lib/packetfu/protos/invalid.rb +50 -49
  35. data/lib/packetfu/protos/ip.rb +70 -69
  36. data/lib/packetfu/protos/ip/header.rb +292 -291
  37. data/lib/packetfu/protos/ip/mixin.rb +41 -40
  38. data/lib/packetfu/protos/ipv6.rb +51 -50
  39. data/lib/packetfu/protos/ipv6/header.rb +189 -188
  40. data/lib/packetfu/protos/ipv6/mixin.rb +30 -29
  41. data/lib/packetfu/protos/lldp.rb +3 -1
  42. data/lib/packetfu/protos/lldp/header.rb +1 -0
  43. data/lib/packetfu/protos/lldp/mixin.rb +1 -0
  44. data/lib/packetfu/protos/tcp.rb +177 -176
  45. data/lib/packetfu/protos/tcp/ecn.rb +36 -35
  46. data/lib/packetfu/protos/tcp/flags.rb +75 -74
  47. data/lib/packetfu/protos/tcp/header.rb +269 -268
  48. data/lib/packetfu/protos/tcp/hlen.rb +33 -32
  49. data/lib/packetfu/protos/tcp/mixin.rb +47 -46
  50. data/lib/packetfu/protos/tcp/option.rb +322 -321
  51. data/lib/packetfu/protos/tcp/options.rb +96 -95
  52. data/lib/packetfu/protos/tcp/reserved.rb +36 -35
  53. data/lib/packetfu/protos/udp.rb +117 -116
  54. data/lib/packetfu/protos/udp/header.rb +92 -91
  55. data/lib/packetfu/protos/udp/mixin.rb +4 -3
  56. data/lib/packetfu/structfu.rb +281 -280
  57. data/lib/packetfu/utils.rb +211 -208
  58. data/lib/packetfu/version.rb +42 -41
  59. data/packetfu.gemspec +1 -1
  60. data/spec/ethpacket_spec.rb +48 -48
  61. data/spec/packet_spec.rb +57 -57
  62. data/spec/packet_subclasses_spec.rb +8 -8
  63. data/spec/packetfu_spec.rb +59 -59
  64. data/spec/structfu_spec.rb +268 -268
  65. data/spec/tcp_spec.rb +75 -75
  66. data/test/all_tests.rb +13 -13
  67. data/test/func_lldp.rb +3 -3
  68. data/test/ptest.rb +2 -2
  69. data/test/test_arp.rb +116 -116
  70. data/test/test_capture.rb +45 -45
  71. data/test/test_eth.rb +68 -68
  72. data/test/test_hsrp.rb +9 -9
  73. data/test/test_icmp.rb +52 -52
  74. data/test/test_inject.rb +18 -18
  75. data/test/test_invalid.rb +16 -16
  76. data/test/test_ip.rb +36 -36
  77. data/test/test_ip6.rb +48 -48
  78. data/test/test_octets.rb +21 -21
  79. data/test/test_packet.rb +154 -154
  80. data/test/test_pcap.rb +170 -170
  81. data/test/test_structfu.rb +97 -97
  82. data/test/test_tcp.rb +320 -320
  83. data/test/test_udp.rb +76 -76
  84. metadata +2 -2
@@ -1,42 +1,43 @@
1
+ # -*- coding: binary -*-
1
2
  module PacketFu
2
- # Implements the Explict Congestion Notification for TCPHeader.
3
- #
4
- # ==== Header Definition
5
- #
6
- #
7
- # Fixnum (1 bit) :n
8
- # Fixnum (1 bit) :c
9
- # Fixnum (1 bit) :e
10
- class TcpEcn < Struct.new(:n, :c, :e)
3
+ # Implements the Explict Congestion Notification for TCPHeader.
4
+ #
5
+ # ==== Header Definition
6
+ #
7
+ #
8
+ # Fixnum (1 bit) :n
9
+ # Fixnum (1 bit) :c
10
+ # Fixnum (1 bit) :e
11
+ class TcpEcn < Struct.new(:n, :c, :e)
11
12
 
12
- include StructFu
13
+ include StructFu
13
14
 
14
- def initialize(args={})
15
- super(args[:n], args[:c], args[:e]) if args
16
- end
15
+ def initialize(args={})
16
+ super(args[:n], args[:c], args[:e]) if args
17
+ end
17
18
 
18
- # Returns the TcpEcn field as an integer... even though it's going
19
- # to be split across a byte boundary.
20
- def to_i
21
- (n.to_i << 2) + (c.to_i << 1) + e.to_i
22
- end
19
+ # Returns the TcpEcn field as an integer... even though it's going
20
+ # to be split across a byte boundary.
21
+ def to_i
22
+ (n.to_i << 2) + (c.to_i << 1) + e.to_i
23
+ end
23
24
 
24
- # Reads a string to populate the object.
25
- def read(str)
26
- force_binary(str)
27
- return self if str.nil? || str.size < 2
28
- if 1.respond_to? :ord
29
- byte1 = str[0].ord
30
- byte2 = str[1].ord
31
- else
32
- byte1 = str[0]
33
- byte2 = str[1]
34
- end
35
- self[:n] = byte1 & 0b00000001 == 0b00000001 ? 1 : 0
36
- self[:c] = byte2 & 0b10000000 == 0b10000000 ? 1 : 0
37
- self[:e] = byte2 & 0b01000000 == 0b01000000 ? 1 : 0
38
- self
39
- end
25
+ # Reads a string to populate the object.
26
+ def read(str)
27
+ force_binary(str)
28
+ return self if str.nil? || str.size < 2
29
+ if 1.respond_to? :ord
30
+ byte1 = str[0].ord
31
+ byte2 = str[1].ord
32
+ else
33
+ byte1 = str[0]
34
+ byte2 = str[1]
35
+ end
36
+ self[:n] = byte1 & 0b00000001 == 0b00000001 ? 1 : 0
37
+ self[:c] = byte2 & 0b10000000 == 0b10000000 ? 1 : 0
38
+ self[:e] = byte2 & 0b01000000 == 0b01000000 ? 1 : 0
39
+ self
40
+ end
40
41
 
41
- end
42
+ end
42
43
  end
@@ -1,83 +1,84 @@
1
+ # -*- coding: binary -*-
1
2
  module PacketFu
2
- # Implements flags for TCPHeader.
3
- #
4
- # ==== Header Definition
5
- #
6
- # Fixnum (1 bit) :urg
7
- # Fixnum (1 bit) :ack
8
- # Fixnum (1 bit) :psh
9
- # Fixnum (1 bit) :rst
10
- # Fixnum (1 bit) :syn
11
- # Fixnum (1 bit) :fin
12
- #
13
- # Flags can typically be set by setting them either to 1 or 0, or to true or false.
14
- class TcpFlags < Struct.new(:urg, :ack, :psh, :rst, :syn, :fin)
3
+ # Implements flags for TCPHeader.
4
+ #
5
+ # ==== Header Definition
6
+ #
7
+ # Fixnum (1 bit) :urg
8
+ # Fixnum (1 bit) :ack
9
+ # Fixnum (1 bit) :psh
10
+ # Fixnum (1 bit) :rst
11
+ # Fixnum (1 bit) :syn
12
+ # Fixnum (1 bit) :fin
13
+ #
14
+ # Flags can typically be set by setting them either to 1 or 0, or to true or false.
15
+ class TcpFlags < Struct.new(:urg, :ack, :psh, :rst, :syn, :fin)
15
16
 
16
- include StructFu
17
+ include StructFu
17
18
 
18
- def initialize(args={})
19
- # This technique attemts to ensure that flags are always 0 (off)
20
- # or 1 (on). Statements like nil and false shouldn't be lurking in here.
21
- if args.nil? || args.size.zero?
22
- super( 0, 0, 0, 0, 0, 0)
23
- else
24
- super(
25
- (args[:urg] ? 1 : 0),
26
- (args[:ack] ? 1 : 0),
27
- (args[:psh] ? 1 : 0),
28
- (args[:rst] ? 1 : 0),
29
- (args[:syn] ? 1 : 0),
30
- (args[:fin] ? 1 : 0)
31
- )
32
- end
33
- end
19
+ def initialize(args={})
20
+ # This technique attemts to ensure that flags are always 0 (off)
21
+ # or 1 (on). Statements like nil and false shouldn't be lurking in here.
22
+ if args.nil? || args.size.zero?
23
+ super( 0, 0, 0, 0, 0, 0)
24
+ else
25
+ super(
26
+ (args[:urg] ? 1 : 0),
27
+ (args[:ack] ? 1 : 0),
28
+ (args[:psh] ? 1 : 0),
29
+ (args[:rst] ? 1 : 0),
30
+ (args[:syn] ? 1 : 0),
31
+ (args[:fin] ? 1 : 0)
32
+ )
33
+ end
34
+ end
34
35
 
35
- # Returns the TcpFlags as an integer.
36
- # Also not a great candidate for to_s due to the short bitspace.
37
- def to_i
38
- (urg.to_i << 5) + (ack.to_i << 4) + (psh.to_i << 3) +
39
- (rst.to_i << 2) + (syn.to_i << 1) + fin.to_i
40
- end
36
+ # Returns the TcpFlags as an integer.
37
+ # Also not a great candidate for to_s due to the short bitspace.
38
+ def to_i
39
+ (urg.to_i << 5) + (ack.to_i << 4) + (psh.to_i << 3) +
40
+ (rst.to_i << 2) + (syn.to_i << 1) + fin.to_i
41
+ end
41
42
 
42
- # Helper to determine if this flag is a 1 or a 0.
43
- def zero_or_one(i=0)
44
- if i == 0 || i == false || i == nil
45
- 0
46
- else
47
- 1
48
- end
49
- end
43
+ # Helper to determine if this flag is a 1 or a 0.
44
+ def zero_or_one(i=0)
45
+ if i == 0 || i == false || i == nil
46
+ 0
47
+ else
48
+ 1
49
+ end
50
+ end
50
51
 
51
- # Setter for the Urgent flag.
52
- def urg=(i); self[:urg] = zero_or_one(i); end
53
- # Setter for the Acknowlege flag.
54
- def ack=(i); self[:ack] = zero_or_one(i); end
55
- # Setter for the Push flag.
56
- def psh=(i); self[:psh] = zero_or_one(i); end
57
- # Setter for the Reset flag.
58
- def rst=(i); self[:rst] = zero_or_one(i); end
59
- # Setter for the Synchronize flag.
60
- def syn=(i); self[:syn] = zero_or_one(i); end
61
- # Setter for the Finish flag.
62
- def fin=(i); self[:fin] = zero_or_one(i); end
52
+ # Setter for the Urgent flag.
53
+ def urg=(i); self[:urg] = zero_or_one(i); end
54
+ # Setter for the Acknowlege flag.
55
+ def ack=(i); self[:ack] = zero_or_one(i); end
56
+ # Setter for the Push flag.
57
+ def psh=(i); self[:psh] = zero_or_one(i); end
58
+ # Setter for the Reset flag.
59
+ def rst=(i); self[:rst] = zero_or_one(i); end
60
+ # Setter for the Synchronize flag.
61
+ def syn=(i); self[:syn] = zero_or_one(i); end
62
+ # Setter for the Finish flag.
63
+ def fin=(i); self[:fin] = zero_or_one(i); end
63
64
 
64
- # Reads a string to populate the object.
65
- def read(str)
66
- force_binary(str)
67
- return self if str.nil?
68
- if 1.respond_to? :ord
69
- byte = str[0].ord
70
- else
71
- byte = str[0]
72
- end
73
- self[:urg] = byte & 0b00100000 == 0b00100000 ? 1 : 0
74
- self[:ack] = byte & 0b00010000 == 0b00010000 ? 1 : 0
75
- self[:psh] = byte & 0b00001000 == 0b00001000 ? 1 : 0
76
- self[:rst] = byte & 0b00000100 == 0b00000100 ? 1 : 0
77
- self[:syn] = byte & 0b00000010 == 0b00000010 ? 1 : 0
78
- self[:fin] = byte & 0b00000001 == 0b00000001 ? 1 : 0
79
- self
80
- end
65
+ # Reads a string to populate the object.
66
+ def read(str)
67
+ force_binary(str)
68
+ return self if str.nil?
69
+ if 1.respond_to? :ord
70
+ byte = str[0].ord
71
+ else
72
+ byte = str[0]
73
+ end
74
+ self[:urg] = byte & 0b00100000 == 0b00100000 ? 1 : 0
75
+ self[:ack] = byte & 0b00010000 == 0b00010000 ? 1 : 0
76
+ self[:psh] = byte & 0b00001000 == 0b00001000 ? 1 : 0
77
+ self[:rst] = byte & 0b00000100 == 0b00000100 ? 1 : 0
78
+ self[:syn] = byte & 0b00000010 == 0b00000010 ? 1 : 0
79
+ self[:fin] = byte & 0b00000001 == 0b00000001 ? 1 : 0
80
+ self
81
+ end
81
82
 
82
- end
83
+ end
83
84
  end
@@ -1,3 +1,4 @@
1
+ # -*- coding: binary -*-
1
2
  require 'packetfu/protos/tcp/reserved'
2
3
  require 'packetfu/protos/tcp/hlen'
3
4
  require 'packetfu/protos/tcp/ecn'
@@ -7,301 +8,301 @@ require 'packetfu/protos/tcp/options'
7
8
 
8
9
 
9
10
  module PacketFu
10
- # TCPHeader is a complete TCP struct, used in TCPPacket. Most IP traffic is TCP-based, by
11
- # volume.
12
- #
13
- # For more on TCP packets, see http://www.networksorcery.com/enp/protocol/tcp.htm
14
- #
15
- # ==== Header Definition
16
- #
17
- # Int16 :tcp_src Default: random
18
- # Int16 :tcp_dst
19
- # Int32 :tcp_seq Default: random
20
- # Int32 :tcp_ack
21
- # TcpHlen :tcp_hlen Default: 5 # Must recalc as options are set.
22
- # TcpReserved :tcp_reserved Default: 0
23
- # TcpEcn :tcp_ecn
24
- # TcpFlags :tcp_flags
25
- # Int16 :tcp_win, Default: 0 # WinXP's default syn packet
26
- # Int16 :tcp_sum, Default: calculated # Must set this upon generation.
27
- # Int16 :tcp_urg
28
- # TcpOptions :tcp_opts
29
- # String :body
30
- #
31
- # See also TcpHlen, TcpReserved, TcpEcn, TcpFlags, TcpOpts
32
- class TCPHeader < Struct.new(:tcp_src, :tcp_dst,
33
- :tcp_seq,
34
- :tcp_ack,
35
- :tcp_hlen, :tcp_reserved, :tcp_ecn, :tcp_flags, :tcp_win,
36
- :tcp_sum, :tcp_urg,
37
- :tcp_opts, :body)
38
- include StructFu
11
+ # TCPHeader is a complete TCP struct, used in TCPPacket. Most IP traffic is TCP-based, by
12
+ # volume.
13
+ #
14
+ # For more on TCP packets, see http://www.networksorcery.com/enp/protocol/tcp.htm
15
+ #
16
+ # ==== Header Definition
17
+ #
18
+ # Int16 :tcp_src Default: random
19
+ # Int16 :tcp_dst
20
+ # Int32 :tcp_seq Default: random
21
+ # Int32 :tcp_ack
22
+ # TcpHlen :tcp_hlen Default: 5 # Must recalc as options are set.
23
+ # TcpReserved :tcp_reserved Default: 0
24
+ # TcpEcn :tcp_ecn
25
+ # TcpFlags :tcp_flags
26
+ # Int16 :tcp_win, Default: 0 # WinXP's default syn packet
27
+ # Int16 :tcp_sum, Default: calculated # Must set this upon generation.
28
+ # Int16 :tcp_urg
29
+ # TcpOptions :tcp_opts
30
+ # String :body
31
+ #
32
+ # See also TcpHlen, TcpReserved, TcpEcn, TcpFlags, TcpOpts
33
+ class TCPHeader < Struct.new(:tcp_src, :tcp_dst,
34
+ :tcp_seq,
35
+ :tcp_ack,
36
+ :tcp_hlen, :tcp_reserved, :tcp_ecn, :tcp_flags, :tcp_win,
37
+ :tcp_sum, :tcp_urg,
38
+ :tcp_opts, :body)
39
+ include StructFu
39
40
 
40
- def initialize(args={})
41
- @random_seq = rand(0xffffffff)
42
- @random_src = rand_port
43
- super(
44
- Int16.new(args[:tcp_src] || tcp_calc_src),
45
- Int16.new(args[:tcp_dst]),
46
- Int32.new(args[:tcp_seq] || tcp_calc_seq),
47
- Int32.new(args[:tcp_ack]),
48
- TcpHlen.new(:hlen => (args[:tcp_hlen] || 5)),
49
- TcpReserved.new(args[:tcp_reserved] || 0),
50
- TcpEcn.new(args[:tcp_ecn]),
51
- TcpFlags.new(args[:tcp_flags]),
52
- Int16.new(args[:tcp_win] || 0x4000),
53
- Int16.new(args[:tcp_sum] || 0),
54
- Int16.new(args[:tcp_urg]),
55
- TcpOptions.new.read(args[:tcp_opts]),
56
- StructFu::String.new.read(args[:body])
57
- )
58
- end
41
+ def initialize(args={})
42
+ @random_seq = rand(0xffffffff)
43
+ @random_src = rand_port
44
+ super(
45
+ Int16.new(args[:tcp_src] || tcp_calc_src),
46
+ Int16.new(args[:tcp_dst]),
47
+ Int32.new(args[:tcp_seq] || tcp_calc_seq),
48
+ Int32.new(args[:tcp_ack]),
49
+ TcpHlen.new(:hlen => (args[:tcp_hlen] || 5)),
50
+ TcpReserved.new(args[:tcp_reserved] || 0),
51
+ TcpEcn.new(args[:tcp_ecn]),
52
+ TcpFlags.new(args[:tcp_flags]),
53
+ Int16.new(args[:tcp_win] || 0x4000),
54
+ Int16.new(args[:tcp_sum] || 0),
55
+ Int16.new(args[:tcp_urg]),
56
+ TcpOptions.new.read(args[:tcp_opts]),
57
+ StructFu::String.new.read(args[:body])
58
+ )
59
+ end
59
60
 
60
- attr_accessor :flavor
61
+ attr_accessor :flavor
61
62
 
62
- # Helper function to create the string for Hlen, Reserved, ECN, and Flags.
63
- def bits_to_s
64
- bytes = []
65
- bytes[0] = (self[:tcp_hlen].to_i << 4) +
66
- (self[:tcp_reserved].to_i << 1) +
67
- self[:tcp_ecn].n.to_i
68
- bytes[1] = (self[:tcp_ecn].c.to_i << 7) +
69
- (self[:tcp_ecn].e.to_i << 6) +
70
- self[:tcp_flags].to_i
71
- bytes.pack("CC")
72
- end
63
+ # Helper function to create the string for Hlen, Reserved, ECN, and Flags.
64
+ def bits_to_s
65
+ bytes = []
66
+ bytes[0] = (self[:tcp_hlen].to_i << 4) +
67
+ (self[:tcp_reserved].to_i << 1) +
68
+ self[:tcp_ecn].n.to_i
69
+ bytes[1] = (self[:tcp_ecn].c.to_i << 7) +
70
+ (self[:tcp_ecn].e.to_i << 6) +
71
+ self[:tcp_flags].to_i
72
+ bytes.pack("CC")
73
+ end
73
74
 
74
- # Returns the object in string form.
75
- def to_s
76
- hdr = self.to_a.map do |x|
77
- if x.kind_of? TcpHlen
78
- bits_to_s
79
- elsif x.kind_of? TcpReserved
80
- next
81
- elsif x.kind_of? TcpEcn
82
- next
83
- elsif x.kind_of? TcpFlags
84
- next
85
- else
86
- x.to_s
87
- end
88
- end
89
- hdr.flatten.join
90
- end
75
+ # Returns the object in string form.
76
+ def to_s
77
+ hdr = self.to_a.map do |x|
78
+ if x.kind_of? TcpHlen
79
+ bits_to_s
80
+ elsif x.kind_of? TcpReserved
81
+ next
82
+ elsif x.kind_of? TcpEcn
83
+ next
84
+ elsif x.kind_of? TcpFlags
85
+ next
86
+ else
87
+ x.to_s
88
+ end
89
+ end
90
+ hdr.flatten.join
91
+ end
91
92
 
92
- # Reads a string to populate the object.
93
- def read(str)
94
- force_binary(str)
95
- return self if str.nil?
96
- self[:tcp_src].read(str[0,2])
97
- self[:tcp_dst].read(str[2,2])
98
- self[:tcp_seq].read(str[4,4])
99
- self[:tcp_ack].read(str[8,4])
100
- self[:tcp_hlen].read(str[12,1])
101
- self[:tcp_reserved].read(str[12,1])
102
- self[:tcp_ecn].read(str[12,2])
103
- self[:tcp_flags].read(str[13,1])
104
- self[:tcp_win].read(str[14,2])
105
- self[:tcp_sum].read(str[16,2])
106
- self[:tcp_urg].read(str[18,2])
107
- self[:tcp_opts].read(str[20,((self[:tcp_hlen].to_i * 4) - 20)])
108
- self[:body].read(str[(self[:tcp_hlen].to_i * 4),str.size])
109
- self
110
- end
93
+ # Reads a string to populate the object.
94
+ def read(str)
95
+ force_binary(str)
96
+ return self if str.nil?
97
+ self[:tcp_src].read(str[0,2])
98
+ self[:tcp_dst].read(str[2,2])
99
+ self[:tcp_seq].read(str[4,4])
100
+ self[:tcp_ack].read(str[8,4])
101
+ self[:tcp_hlen].read(str[12,1])
102
+ self[:tcp_reserved].read(str[12,1])
103
+ self[:tcp_ecn].read(str[12,2])
104
+ self[:tcp_flags].read(str[13,1])
105
+ self[:tcp_win].read(str[14,2])
106
+ self[:tcp_sum].read(str[16,2])
107
+ self[:tcp_urg].read(str[18,2])
108
+ self[:tcp_opts].read(str[20,((self[:tcp_hlen].to_i * 4) - 20)])
109
+ self[:body].read(str[(self[:tcp_hlen].to_i * 4),str.size])
110
+ self
111
+ end
111
112
 
112
- # Setter for the TCP source port.
113
- def tcp_src=(i); typecast i; end
114
- # Getter for the TCP source port.
115
- def tcp_src; self[:tcp_src].to_i; end
116
- # Setter for the TCP destination port.
117
- def tcp_dst=(i); typecast i; end
118
- # Getter for the TCP destination port.
119
- def tcp_dst; self[:tcp_dst].to_i; end
120
- # Setter for the TCP sequence number.
121
- def tcp_seq=(i); typecast i; end
122
- # Getter for the TCP sequence number.
123
- def tcp_seq; self[:tcp_seq].to_i; end
124
- # Setter for the TCP ackowlegement number.
125
- def tcp_ack=(i); typecast i; end
126
- # Getter for the TCP ackowlegement number.
127
- def tcp_ack; self[:tcp_ack].to_i; end
128
- # Setter for the TCP window size number.
129
- def tcp_win=(i); typecast i; end
130
- # Getter for the TCP window size number.
131
- def tcp_win; self[:tcp_win].to_i; end
132
- # Setter for the TCP checksum.
133
- def tcp_sum=(i); typecast i; end
134
- # Getter for the TCP checksum.
135
- def tcp_sum; self[:tcp_sum].to_i; end
136
- # Setter for the TCP urgent field.
137
- def tcp_urg=(i); typecast i; end
138
- # Getter for the TCP urgent field.
139
- def tcp_urg; self[:tcp_urg].to_i; end
113
+ # Setter for the TCP source port.
114
+ def tcp_src=(i); typecast i; end
115
+ # Getter for the TCP source port.
116
+ def tcp_src; self[:tcp_src].to_i; end
117
+ # Setter for the TCP destination port.
118
+ def tcp_dst=(i); typecast i; end
119
+ # Getter for the TCP destination port.
120
+ def tcp_dst; self[:tcp_dst].to_i; end
121
+ # Setter for the TCP sequence number.
122
+ def tcp_seq=(i); typecast i; end
123
+ # Getter for the TCP sequence number.
124
+ def tcp_seq; self[:tcp_seq].to_i; end
125
+ # Setter for the TCP ackowlegement number.
126
+ def tcp_ack=(i); typecast i; end
127
+ # Getter for the TCP ackowlegement number.
128
+ def tcp_ack; self[:tcp_ack].to_i; end
129
+ # Setter for the TCP window size number.
130
+ def tcp_win=(i); typecast i; end
131
+ # Getter for the TCP window size number.
132
+ def tcp_win; self[:tcp_win].to_i; end
133
+ # Setter for the TCP checksum.
134
+ def tcp_sum=(i); typecast i; end
135
+ # Getter for the TCP checksum.
136
+ def tcp_sum; self[:tcp_sum].to_i; end
137
+ # Setter for the TCP urgent field.
138
+ def tcp_urg=(i); typecast i; end
139
+ # Getter for the TCP urgent field.
140
+ def tcp_urg; self[:tcp_urg].to_i; end
140
141
 
141
- # Getter for the TCP Header Length value.
142
- def tcp_hlen; self[:tcp_hlen].to_i; end
143
- # Setter for the TCP Header Length value. Can take
144
- # either a string or an integer. Note that if it's
145
- # a string, the top four bits are used.
146
- def tcp_hlen=(i)
147
- case i
148
- when PacketFu::TcpHlen
149
- self[:tcp_hlen] = i
150
- when Numeric
151
- self[:tcp_hlen] = TcpHlen.new(:hlen => i.to_i)
152
- else
153
- self[:tcp_hlen].read(i)
154
- end
155
- end
142
+ # Getter for the TCP Header Length value.
143
+ def tcp_hlen; self[:tcp_hlen].to_i; end
144
+ # Setter for the TCP Header Length value. Can take
145
+ # either a string or an integer. Note that if it's
146
+ # a string, the top four bits are used.
147
+ def tcp_hlen=(i)
148
+ case i
149
+ when PacketFu::TcpHlen
150
+ self[:tcp_hlen] = i
151
+ when Numeric
152
+ self[:tcp_hlen] = TcpHlen.new(:hlen => i.to_i)
153
+ else
154
+ self[:tcp_hlen].read(i)
155
+ end
156
+ end
156
157
 
157
- # Getter for the TCP Reserved field.
158
- def tcp_reserved; self[:tcp_reserved].to_i; end
159
- # Setter for the TCP Reserved field.
160
- def tcp_reserved=(i)
161
- case i
162
- when PacketFu::TcpReserved
163
- self[:tcp_reserved]=i
164
- when Numeric
165
- args = {}
166
- args[:r1] = (i & 0b100) >> 2
167
- args[:r2] = (i & 0b010) >> 1
168
- args[:r3] = (i & 0b001)
169
- self[:tcp_reserved] = TcpReserved.new(args)
170
- else
171
- self[:tcp_reserved].read(i)
172
- end
173
- end
158
+ # Getter for the TCP Reserved field.
159
+ def tcp_reserved; self[:tcp_reserved].to_i; end
160
+ # Setter for the TCP Reserved field.
161
+ def tcp_reserved=(i)
162
+ case i
163
+ when PacketFu::TcpReserved
164
+ self[:tcp_reserved]=i
165
+ when Numeric
166
+ args = {}
167
+ args[:r1] = (i & 0b100) >> 2
168
+ args[:r2] = (i & 0b010) >> 1
169
+ args[:r3] = (i & 0b001)
170
+ self[:tcp_reserved] = TcpReserved.new(args)
171
+ else
172
+ self[:tcp_reserved].read(i)
173
+ end
174
+ end
174
175
 
175
- # Getter for the ECN bits.
176
- def tcp_ecn; self[:tcp_ecn].to_i; end
177
- # Setter for the ECN bits.
178
- def tcp_ecn=(i)
179
- case i
180
- when PacketFu::TcpEcn
181
- self[:tcp_ecn]=i
182
- when Numeric
183
- args = {}
184
- args[:n] = (i & 0b100) >> 2
185
- args[:c] = (i & 0b010) >> 1
186
- args[:e] = (i & 0b001)
187
- self[:tcp_ecn] = TcpEcn.new(args)
188
- else
189
- self[:tcp_ecn].read(i)
190
- end
191
- end
176
+ # Getter for the ECN bits.
177
+ def tcp_ecn; self[:tcp_ecn].to_i; end
178
+ # Setter for the ECN bits.
179
+ def tcp_ecn=(i)
180
+ case i
181
+ when PacketFu::TcpEcn
182
+ self[:tcp_ecn]=i
183
+ when Numeric
184
+ args = {}
185
+ args[:n] = (i & 0b100) >> 2
186
+ args[:c] = (i & 0b010) >> 1
187
+ args[:e] = (i & 0b001)
188
+ self[:tcp_ecn] = TcpEcn.new(args)
189
+ else
190
+ self[:tcp_ecn].read(i)
191
+ end
192
+ end
192
193
 
193
- # Getter for TCP Options.
194
- def tcp_opts; self[:tcp_opts].to_s; end
195
- # Setter for TCP Options.
196
- def tcp_opts=(i)
197
- case i
198
- when PacketFu::TcpOptions
199
- self[:tcp_opts]=i
200
- else
201
- self[:tcp_opts].read(i)
202
- end
203
- end
194
+ # Getter for TCP Options.
195
+ def tcp_opts; self[:tcp_opts].to_s; end
196
+ # Setter for TCP Options.
197
+ def tcp_opts=(i)
198
+ case i
199
+ when PacketFu::TcpOptions
200
+ self[:tcp_opts]=i
201
+ else
202
+ self[:tcp_opts].read(i)
203
+ end
204
+ end
204
205
 
205
- # Resets the sequence number to a new random number.
206
- def tcp_calc_seq; @random_seq; end
207
- # Resets the source port to a new random number.
208
- def tcp_calc_src; @random_src; end
206
+ # Resets the sequence number to a new random number.
207
+ def tcp_calc_seq; @random_seq; end
208
+ # Resets the source port to a new random number.
209
+ def tcp_calc_src; @random_src; end
209
210
 
210
- # Returns the actual length of the TCP options.
211
- def tcp_opts_len
212
- self[:tcp_opts].to_s.size
213
- end
211
+ # Returns the actual length of the TCP options.
212
+ def tcp_opts_len
213
+ self[:tcp_opts].to_s.size
214
+ end
214
215
 
215
- # Sets and returns the true length of the TCP Header.
216
- # TODO: Think about making all the option stuff safer.
217
- def tcp_calc_hlen
218
- self[:tcp_hlen] = TcpHlen.new(:hlen => ((20 + tcp_opts_len) / 4))
219
- end
216
+ # Sets and returns the true length of the TCP Header.
217
+ # TODO: Think about making all the option stuff safer.
218
+ def tcp_calc_hlen
219
+ self[:tcp_hlen] = TcpHlen.new(:hlen => ((20 + tcp_opts_len) / 4))
220
+ end
220
221
 
221
- # Generates a random high port. This is affected by packet flavor.
222
- def rand_port
223
- rand(0xffff - 1025) + 1025
224
- end
222
+ # Generates a random high port. This is affected by packet flavor.
223
+ def rand_port
224
+ rand(0xffff - 1025) + 1025
225
+ end
225
226
 
226
- # Gets a more readable option list.
227
- def tcp_options
228
- self[:tcp_opts].decode
229
- end
227
+ # Gets a more readable option list.
228
+ def tcp_options
229
+ self[:tcp_opts].decode
230
+ end
230
231
 
231
- # Gets a more readable flags list
232
- def tcp_flags_dotmap
233
- dotmap = tcp_flags.members.map do |flag|
234
- status = self.tcp_flags.send flag
235
- status == 0 ? "." : flag.to_s.upcase[0].chr
236
- end
237
- dotmap.join
238
- end
232
+ # Gets a more readable flags list
233
+ def tcp_flags_dotmap
234
+ dotmap = tcp_flags.members.map do |flag|
235
+ status = self.tcp_flags.send flag
236
+ status == 0 ? "." : flag.to_s.upcase[0].chr
237
+ end
238
+ dotmap.join
239
+ end
239
240
 
240
- # Sets a more readable option list.
241
- def tcp_options=(arg)
242
- self[:tcp_opts].encode arg
243
- end
241
+ # Sets a more readable option list.
242
+ def tcp_options=(arg)
243
+ self[:tcp_opts].encode arg
244
+ end
244
245
 
245
- # Equivalent to tcp_src.
246
- def tcp_sport
247
- self.tcp_src.to_i
248
- end
246
+ # Equivalent to tcp_src.
247
+ def tcp_sport
248
+ self.tcp_src.to_i
249
+ end
249
250
 
250
- # Equivalent to tcp_src=.
251
- def tcp_sport=(arg)
252
- self.tcp_src=(arg)
253
- end
251
+ # Equivalent to tcp_src=.
252
+ def tcp_sport=(arg)
253
+ self.tcp_src=(arg)
254
+ end
254
255
 
255
- # Equivalent to tcp_dst.
256
- def tcp_dport
257
- self.tcp_dst.to_i
258
- end
259
-
260
- # Equivalent to tcp_dst=.
261
- def tcp_dport=(arg)
262
- self.tcp_dst=(arg)
263
- end
256
+ # Equivalent to tcp_dst.
257
+ def tcp_dport
258
+ self.tcp_dst.to_i
259
+ end
260
+
261
+ # Equivalent to tcp_dst=.
262
+ def tcp_dport=(arg)
263
+ self.tcp_dst=(arg)
264
+ end
264
265
 
265
- # Recalculates calculated fields for TCP (except checksum which is at the Packet level).
266
- def tcp_recalc(arg=:all)
267
- case arg
268
- when :tcp_hlen
269
- tcp_calc_hlen
270
- when :tcp_src
271
- @random_tcp_src = rand_port
272
- when :tcp_sport
273
- @random_tcp_src = rand_port
274
- when :tcp_seq
275
- @random_tcp_seq = rand(0xffffffff)
276
- when :all
277
- tcp_calc_hlen
278
- @random_tcp_src = rand_port
279
- @random_tcp_seq = rand(0xffffffff)
280
- else
281
- raise ArgumentError, "No such field `#{arg}'"
282
- end
283
- end
266
+ # Recalculates calculated fields for TCP (except checksum which is at the Packet level).
267
+ def tcp_recalc(arg=:all)
268
+ case arg
269
+ when :tcp_hlen
270
+ tcp_calc_hlen
271
+ when :tcp_src
272
+ @random_tcp_src = rand_port
273
+ when :tcp_sport
274
+ @random_tcp_src = rand_port
275
+ when :tcp_seq
276
+ @random_tcp_seq = rand(0xffffffff)
277
+ when :all
278
+ tcp_calc_hlen
279
+ @random_tcp_src = rand_port
280
+ @random_tcp_seq = rand(0xffffffff)
281
+ else
282
+ raise ArgumentError, "No such field `#{arg}'"
283
+ end
284
+ end
284
285
 
285
- # Readability aliases
286
+ # Readability aliases
286
287
 
287
- alias :tcp_flags_readable :tcp_flags_dotmap
288
+ alias :tcp_flags_readable :tcp_flags_dotmap
288
289
 
289
- def tcp_ack_readable
290
- "0x%08x" % tcp_ack
291
- end
290
+ def tcp_ack_readable
291
+ "0x%08x" % tcp_ack
292
+ end
292
293
 
293
- def tcp_seq_readable
294
- "0x%08x" % tcp_seq
295
- end
294
+ def tcp_seq_readable
295
+ "0x%08x" % tcp_seq
296
+ end
296
297
 
297
- def tcp_sum_readable
298
- "0x%04x" % tcp_sum
299
- end
298
+ def tcp_sum_readable
299
+ "0x%04x" % tcp_sum
300
+ end
300
301
 
301
- def tcp_opts_readable
302
- tcp_options
303
- end
302
+ def tcp_opts_readable
303
+ tcp_options
304
+ end
304
305
 
305
- end
306
+ end
306
307
 
307
308
  end