Source code: com/voytechs/jnetanalyzer/tcp/TCPSegment.java
1 /*
2 * File: TCPSegment.java
3 * Auth: Mark Bednarczyk
4 * Date: DATE
5 * Id: $Id: TCPSegment.java,v 1.1.1.1 2003/09/22 16:32:06 voytechs Exp $
6 ********************************************
7 Copyright (C) 2003 Mark Bednarczyk
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 ********************************************
23 * $Log: TCPSegment.java,v $
24 * Revision 1.1.1.1 2003/09/22 16:32:06 voytechs
25 * Initial import.
26 *
27 */
28 package com.voytechs.jnetanalyzer.tcp;
29
30 import com.voytechs.jnetanalyzer.message.*;
31
32 import com.voytechs.jnetstream.protocol.layer4.*;
33 import com.voytechs.jnetstream.protocol.layer3.*;
34 import com.voytechs.jnetstream.protocol.*;
35 import com.voytechs.jnetstream.codec.Packet;
36
37 import java.lang.*;
38 import java.util.*;
39
40 /**
41 *
42 */
43 public class TCPSegment
44 extends PacketMessageSegment {
45
46 /* Internal attributes */
47 private static final boolean debug = false;
48
49 // Convenience - these are referenced frequently.
50 private TCP tcp;
51 private IPv4 ip;
52
53 private int segmentLen = -1; // -1 means not initialized yet.
54
55 /**
56 * Initialize the TCP segment from packet information.
57 * @param
58 * @exception
59 */
60 public TCPSegment(Packet pkt) {
61 super(pkt);
62
63 this.tcp = (TCP)pkt.getHeader(TCP.NAME);
64 this.ip = (IPv4)pkt.getHeader(IPv4.NAME);
65
66 calculateTcpPayloadLength();
67
68 super.init(tcp.seq, segmentLen);
69 }
70 /**
71 *
72 * @param
73 * @exception
74 */
75 public TCPSegment(TCP tcp, Packet pkt, long seq, int len) {
76 super(pkt, seq, len);
77
78 this.tcp = tcp;
79 this.ip = (IPv4)pkt.getHeader(IPv4.NAME);
80 }
81
82 /**
83 * Returns the tcp header.
84 */
85 public TCP getTCPHeader() {
86 return(tcp);
87 }
88
89 /**
90 * Returns the tcp header.
91 */
92 public IPv4 getIPv4Header() {
93 return(ip);
94 }
95
96 /**
97 * Return the length of the tcp segment.
98 * A -1 means its not initialized yet.
99 */
100 public int getLength() {
101 return(segmentLen);
102 }
103
104 /**
105 * Return the acknoledgement.
106 */
107 public long getAck() {
108 return(tcp.ack);
109 }
110
111 /**
112 * Calculates the length of the TCP payload.
113 * Extracts ip.len field and subtracts ip and TCP header
114 * lengths.
115 */
116 private void calculateTcpPayloadLength() {
117
118 segmentLen = ip.len - ip.getHeaderLength() - tcp.getHeaderLength();
119 put("tcpLen", new Integer(segmentLen));
120
121 if(debug)
122 System.out.println("TCPSegment: len=" + segmentLen);
123 }
124
125 /**
126 * Test function for TCPSegment
127 * @param args command line arguments
128 */
129 public static void main(String [] args) {
130 }
131
132 } /* END OF: TCPSegment */