Skip to content

Commit

Permalink
Declare the "packet_info" structure "pi" in "packet.h", rather than in a
Browse files Browse the repository at this point in the history
bunch of source files.

Replace the "payload" field of a "packet_info" structure with "len" and
"captured_len" fields, which contain the total packet length and total
captured packet length (including all headers) at the current protocol
layer (i.e., if a given layer has a length field, and that length field
says its shorter than the length we got from the capture, reduce the
"pi.len" and "pi.captured_len" values appropriately).  Those fields can
be used in the future if we add checks to make sure a field we're
extracting from a packet doesn't go past the end of the packet, or past
the captured part of the packet.

Get rid of the additional payload argument to some dissection functions;
use "pi.captured_len - offset" instead.

Have the END_OF_FRAME macro use "pi.captured_len" rather than
"fd->cap_len", so that "dissect the rest of the frame" becomes "dissect
the rest of the packet", and doesn't dissect end-of-frame padding such
as padding added to make an Ethernet frame 60 or more octets long.  (We
might want to rename it END_OF_PACKET; if we ever want to label the
end-of-frame padding for the benefit of people curious what that extra
gunk is, we could have a separate END_OF_FRAME macro that uses
"fd->cap_len".)

svn path=/trunk/; revision=506
  • Loading branch information
guyharris committed Aug 18, 1999
1 parent d4331d4 commit ac4f872
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 78 deletions.
2 changes: 0 additions & 2 deletions packet-atalk.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
# include <netinet/in.h>
#endif

extern packet_info pi;

static int proto_ddp = -1;

/* P = Padding, H = Hops, L = Len */
Expand Down
29 changes: 20 additions & 9 deletions packet-eth.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* packet-eth.c
* Routines for ethernet packet disassembly
*
* $Id: packet-eth.c,v 1.14 1999/08/01 04:28:08 gram Exp $
* $Id: packet-eth.c,v 1.15 1999/08/18 00:57:50 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org>
Expand Down Expand Up @@ -47,6 +47,8 @@ static int hf_eth_type = -1;

#define IEEE_802_3_MAX_LEN 1500

#define ETH_HEADER_SIZE 14

/* These are the Netware-ish names for the different Ethernet frame types.
EthernetII: The ethernet with a Type field instead of a length field
Ethernet802.2: An 802.3 header followed by an 802.3 header
Expand All @@ -64,8 +66,8 @@ static int hf_eth_type = -1;
void
capture_eth(const u_char *pd, guint32 cap_len, packet_counts *ld) {
guint16 etype;
int offset = 14;
int ethhdr_type; /* the type of ethernet frame */
int offset = ETH_HEADER_SIZE;
int ethhdr_type; /* the type of ethernet frame */

etype = (pd[12] << 8) | pd[13];

Expand Down Expand Up @@ -104,10 +106,10 @@ capture_eth(const u_char *pd, guint32 cap_len, packet_counts *ld) {
void
dissect_eth(const u_char *pd, frame_data *fd, proto_tree *tree) {
guint16 etype, length;
int offset = 14;
int offset = ETH_HEADER_SIZE;
proto_tree *fh_tree = NULL;
proto_item *ti;
int ethhdr_type; /* the type of ethernet frame */
int ethhdr_type; /* the type of ethernet frame */

if (check_col(fd, COL_RES_DL_DST))
col_add_str(fd, COL_RES_DL_DST, get_ether_name((u_char *)&pd[0]));
Expand Down Expand Up @@ -145,22 +147,32 @@ dissect_eth(const u_char *pd, frame_data *fd, proto_tree *tree) {
col_add_str(fd, COL_INFO, "802.3");
if (tree) {

ti = proto_tree_add_item_format(tree, proto_eth, 0, offset,
ti = proto_tree_add_item_format(tree, proto_eth, 0, ETH_HEADER_SIZE,
NULL, "IEEE 802.3 %s", (ethhdr_type == ETHERNET_802_3 ? "Raw " : ""));

fh_tree = proto_item_add_subtree(ti, ETT_IEEE8023);

proto_tree_add_item(fh_tree, hf_eth_dst, 0, 6, &pd[0]);
proto_tree_add_item(fh_tree, hf_eth_src, 6, 6, &pd[6]);
proto_tree_add_item(fh_tree, hf_eth_len, 12, 2, length);

/* Convert the LLC length from the 802.3 header to a total
length, by adding in the Ethernet header size, and set
the payload and captured-payload lengths to the minima
of the total length and the frame lengths. */
length += ETH_HEADER_SIZE;
if (pi.len > length)
pi.len = length;
if (pi.captured_len > length)
pi.captured_len = length;
}

} else {
ethhdr_type = ETHERNET_II;
if (tree) {

ti = proto_tree_add_item_format(tree, proto_eth, 0, 14, NULL,
"Ethernet II");
ti = proto_tree_add_item_format(tree, proto_eth, 0, ETH_HEADER_SIZE,
NULL, "Ethernet II");

fh_tree = proto_item_add_subtree(ti, ETT_ETHER2);

Expand All @@ -171,7 +183,6 @@ dissect_eth(const u_char *pd, frame_data *fd, proto_tree *tree) {
proto_tree_add_item_format(fh_tree, hf_eth_src, 6, 6, &pd[6],
"Source: %s (%s)", ether_to_str((guint8 *) &pd[6]),
get_ether_name((u_char *) &pd[6]));

}
}

Expand Down
9 changes: 4 additions & 5 deletions packet-ftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Routines for ftp packet dissection
* Copyright 1999, Richard Sharpe <rsharpe@ns.aus.com>
*
* $Id: packet-ftp.c,v 1.5 1999/07/29 05:46:54 gram Exp $
* $Id: packet-ftp.c,v 1.6 1999/08/18 00:57:50 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@unicom.net>
Expand Down Expand Up @@ -44,17 +44,16 @@
#include "packet.h"
#include "etypes.h"

extern packet_info pi;

static int proto_ftp = -1;

void
dissect_ftp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree, int max_data)
dissect_ftp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
{
proto_tree *ftp_tree, *ti;
gchar rr[50], rd[1500];
int i1 = (u_char *)strchr(pd + offset, ' ') - (pd + offset); /* Where is that space */
int i2;
int max_data = pi.captured_len - offset;

memset(rr, '\0', sizeof(rr));
memset(rd, '\0', sizeof(rd));
Expand Down Expand Up @@ -115,7 +114,7 @@ dissect_ftp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree, int
}

void
dissect_ftpdata(const u_char *pd, int offset, frame_data *fd, proto_tree *tree, int max_data)
dissect_ftpdata(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
{
proto_tree *ti;

Expand Down
23 changes: 16 additions & 7 deletions packet-ip.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* packet-ip.c
* Routines for IP and miscellaneous IP protocol packet disassembly
*
* $Id: packet-ip.c,v 1.37 1999/08/17 03:09:39 gram Exp $
* $Id: packet-ip.c,v 1.38 1999/08/18 00:57:50 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org>
Expand Down Expand Up @@ -46,8 +46,6 @@
#include "packet-ip.h"
#endif

extern packet_info pi;

static int proto_ip = -1;
static int hf_ip_version = -1;
static int hf_ip_hdr_len = -1;
Expand Down Expand Up @@ -627,10 +625,10 @@ dissect_ip(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
proto_tree *ip_tree, *field_tree;
proto_item *ti, *tf;
gchar tos_str[32];
guint hlen, optlen;
guint hlen, optlen, len;
guint16 flags;
int advance;
guint8 nxt;
int advance;
guint8 nxt;

/* To do: check for runts, errs, etc. */
/* Avoids alignment problems on many architectures. */
Expand All @@ -640,6 +638,17 @@ dissect_ip(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
iph.ip_off = ntohs(iph.ip_off);
iph.ip_sum = ntohs(iph.ip_sum);

/* Length of IP datagram plus headers above it. */
len = iph.ip_len + offset;

/* Set the payload and captured-payload lengths to the minima of (the
IP length plus the length of the headers above it) and the frame
lengths. */
if (pi.len > len)
pi.len = len;
if (pi.captured_len > len)
pi.captured_len = len;

hlen = lo_nibble(iph.ip_v_hl) * 4; /* IP header length, in bytes */

switch (iph.ip_p) {
Expand Down Expand Up @@ -768,8 +777,8 @@ dissect_ip(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
pi.iphdrlen = lo_nibble(iph.ip_v_hl);
pi.ip_src = iph.ip_src;
pi.ip_dst = iph.ip_dst;
pi.payload = pi.iplen - hlen;

/* Skip over header + options */
offset += hlen;
nxt = iph.ip_p;
if (iph.ip_off & IP_OFFSET) {
Expand Down
18 changes: 10 additions & 8 deletions packet-nbns.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Gilbert Ramirez <gram@verdict.uthscsa.edu>
* Much stuff added by Guy Harris <guy@netapp.com>
*
* $Id: packet-nbns.c,v 1.23 1999/07/29 05:46:58 gram Exp $
* $Id: packet-nbns.c,v 1.24 1999/08/18 00:57:51 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org>
Expand Down Expand Up @@ -977,14 +977,14 @@ struct nbdgm_header {
};

void
dissect_nbdgm(const u_char *pd, int offset, frame_data *fd, proto_tree *tree,
int max_data)
dissect_nbdgm(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
{
proto_tree *nbdgm_tree = NULL;
proto_item *ti;
struct nbdgm_header header;
int flags;
int message_index;
int max_data = pi.captured_len - offset;

char *message[] = {
"Unknown",
Expand Down Expand Up @@ -1263,12 +1263,13 @@ dissect_nbss_packet(const u_char *pd, int offset, frame_data *fd, proto_tree *tr
}

void
dissect_nbss(const u_char *pd, int offset, frame_data *fd, proto_tree *tree, int max_data)
dissect_nbss(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
{
guint8 msg_type;
guint8 flags;
guint16 length;
int len;
int max_data;

msg_type = pd[offset];
flags = pd[offset + 1];
Expand All @@ -1283,10 +1284,11 @@ dissect_nbss(const u_char *pd, int offset, frame_data *fd, proto_tree *tree, int
val_to_str(msg_type, message_types, "Unknown (%x)"));
}

while (max_data > 0) {
len = dissect_nbss_packet(pd, offset, fd, tree, max_data);
offset += len;
max_data -= len;
max_data = pi.captured_len - offset;
while (max_data > 0) {
len = dissect_nbss_packet(pd, offset, fd, tree, max_data);
offset += len;
max_data -= len;
}

}
Expand Down
7 changes: 3 additions & 4 deletions packet-nntp.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Routines for nntp packet dissection
* Copyright 1999, Richard Sharpe <rsharpe@ns.aus.com>
*
* $Id: packet-nntp.c,v 1.3 1999/07/29 05:47:00 gram Exp $
* $Id: packet-nntp.c,v 1.4 1999/08/18 00:57:52 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@unicom.net>
Expand Down Expand Up @@ -41,18 +41,17 @@
#include <glib.h>
#include "packet.h"

extern packet_info pi;

static int proto_nntp = -1;

void
dissect_nntp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree, int max_data)
dissect_nntp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
{
gchar *type;
proto_tree *nntp_tree, *ti;
const u_char *data, *dataend;
const u_char *lineend, *eol;
int linelen;
int max_data = pi.captured_len - offset;

data = &pd[offset];
dataend = data + END_OF_FRAME;
Expand Down
7 changes: 3 additions & 4 deletions packet-pop.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Routines for pop packet dissection
* Copyright 1999, Richard Sharpe <rsharpe@ns.aus.com>
*
* $Id: packet-pop.c,v 1.5 1999/07/29 05:47:01 gram Exp $
* $Id: packet-pop.c,v 1.6 1999/08/18 00:57:52 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@unicom.net>
Expand Down Expand Up @@ -44,17 +44,16 @@
#include "packet.h"
#include "etypes.h"

extern packet_info pi;

static int proto_pop = -1;

void
dissect_pop(const u_char *pd, int offset, frame_data *fd, proto_tree *tree, int max_data)
dissect_pop(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
{
proto_tree *pop_tree, *ti;
gchar rr[50], rd[1500];
int i1 = (u_char *)strchr(pd + offset, ' ') - (pd + offset); /* Where is that space */
int i2;
int max_data = pi.captured_len - offset;

memset(rr, '\0', sizeof(rr));
memset(rd, '\0', sizeof(rd));
Expand Down
4 changes: 1 addition & 3 deletions packet-smb.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Routines for smb packet dissection
* Copyright 1999, Richard Sharpe <rsharpe@ns.aus.com>
*
* $Id: packet-smb.c,v 1.21 1999/07/29 05:47:04 gram Exp $
* $Id: packet-smb.c,v 1.22 1999/08/18 00:57:52 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@unicom.net>
Expand Down Expand Up @@ -46,8 +46,6 @@
#include "smb.h"
#include "alignment.h"

extern packet_info pi;

static int proto_smb = -1;

char *decode_smb_name(unsigned char);
Expand Down
20 changes: 8 additions & 12 deletions packet-tcp.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* packet-tcp.c
* Routines for TCP packet disassembly
*
* $Id: packet-tcp.c,v 1.29 1999/07/31 13:55:16 deniel Exp $
* $Id: packet-tcp.c,v 1.30 1999/08/18 00:57:53 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org>
Expand Down Expand Up @@ -56,7 +56,6 @@
#endif

extern FILE* data_out_file;
extern packet_info pi;

static gchar info_str[COL_MAX_LEN];
static int info_len;
Expand Down Expand Up @@ -335,8 +334,7 @@ dissect_tcp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
guint bpos;
guint hlen;
guint optlen;
guint packet_max = pi.payload + offset;
guint payload;
guint packet_max = pi.len;

/* To do: Check for {cap len,pkt len} < struct len */
/* Avoids alignment problems on many architectures. */
Expand Down Expand Up @@ -368,8 +366,6 @@ dissect_tcp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {

hlen = hi_nibble(th.th_off_x2) * 4; /* TCP header length, in bytes */

payload = pi.payload - hlen;

if (check_col(fd, COL_RES_SRC_PORT))
col_add_str(fd, COL_RES_SRC_PORT, get_tcp_port(th.th_sport));
if (check_col(fd, COL_UNRES_SRC_PORT))
Expand Down Expand Up @@ -465,27 +461,27 @@ dissect_tcp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
dissect_lpd(pd, offset, fd, tree);
else if (PORT_IS(TCP_PORT_TELNET)) {
pi.match_port = TCP_PORT_TELNET;
dissect_telnet(pd, offset, fd, tree, payload);
dissect_telnet(pd, offset, fd, tree);
} else if (PORT_IS(TCP_PORT_FTPDATA)) {
pi.match_port = TCP_PORT_FTPDATA;
dissect_ftpdata(pd, offset, fd, tree, payload);
dissect_ftpdata(pd, offset, fd, tree);
} else if (PORT_IS(TCP_PORT_FTP)) {
pi.match_port = TCP_PORT_FTP;
dissect_ftp(pd, offset, fd, tree, payload);
dissect_ftp(pd, offset, fd, tree);
} else if (PORT_IS(TCP_PORT_POP)) {
pi.match_port = TCP_PORT_POP;
dissect_pop(pd, offset, fd, tree, payload);
dissect_pop(pd, offset, fd, tree);
} else if (PORT_IS(TCP_PORT_NNTP)) {
pi.match_port = TCP_PORT_NNTP;
dissect_nntp(pd, offset, fd, tree, payload);
dissect_nntp(pd, offset, fd, tree);
} else if (PORT_IS(TCP_PORT_PPTP)) {
pi.match_port = TCP_PORT_PPTP;
dissect_pptp(pd, offset, fd, tree);
} else if (PORT_IS(TCP_PORT_HTTP) || PORT_IS(TCP_ALT_PORT_HTTP))
dissect_http(pd, offset, fd, tree);
else if (PORT_IS(TCP_PORT_NBSS)) {
pi.match_port = TCP_PORT_NBSS;
dissect_nbss(pd, offset, fd, tree, payload);
dissect_nbss(pd, offset, fd, tree);
} else if (PORT_IS(TCP_PORT_RTSP))
dissect_rtsp(pd, offset, fd, tree);
else {
Expand Down
Loading

0 comments on commit ac4f872

Please sign in to comment.