aboutsummaryrefslogtreecommitdiffstats
path: root/wiretap/nettl.c
blob: 60621913dd589a0965480c3dd94accb399c20d23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* nettl.c
 *
 * $Id: nettl.c,v 1.4 2000/01/20 17:13:42 oabad Exp $
 *
 * Wiretap Library
 * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include <errno.h>
#include <time.h>
#include "wtap.h"
#include "file_wrappers.h"
#include "buffer.h"
#include "nettl.h"

static char nettl_magic_hpux9[12] = {
    0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xD0, 0x00
};
static char nettl_magic_hpux10[12] = {
    0x54, 0x52, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80
};

/* HP nettl record header - The FCS is not included in the file. */
struct nettlrec_hdr {
    char	xxa[12];
    char	from_dce;
    char	xxb[55];
    guint16	length;
    guint16	length2;    /* don't know which one is captured length / real length */
    char	xxc[4];
    char	sec[4];
    char	usec[4];
    char	xxd[4];
};

/* header is followed by data and once again the total length (2 bytes) ! */

static int nettl_read(wtap *wth, int *err);

int nettl_open(wtap *wth, int *err)
{
    char magic[12];
    int bytes_read;

    /* Read in the string that should be at the start of a HP file */
    file_seek(wth->fh, 0, SEEK_SET);
    errno = WTAP_ERR_CANT_READ;
    bytes_read = file_read(magic, 1, 12, wth->fh);
    if (bytes_read != 12) {
    	*err = file_error(wth->fh);
	if (*err != 0)
	    return -1;
	return 0;
    }

    if (memcmp(magic, nettl_magic_hpux9, 12) &&
        memcmp(magic, nettl_magic_hpux10, 12)) {
	return 0;
    }

    file_seek(wth->fh, 0x80, SEEK_SET);
    wth->data_offset = 0x80;

    /* This is an nettl file */
    wth->file_type = WTAP_FILE_NETTL;
    wth->capture.nettl = g_malloc(sizeof(nettl_t));
    wth->subtype_read = nettl_read;
    wth->snapshot_length = 16384;	/* not available in header, only in frame */

    wth->capture.nettl->start = 0;

    wth->file_encap = WTAP_ENCAP_LAPB;

    return 1;
}

/* Read the next packet */
static int nettl_read(wtap *wth, int *err)
{
    int	bytes_read;
    struct nettlrec_hdr hdr;
    guint16 length;
    int	data_offset;

    /* Read record header. */
    errno = WTAP_ERR_CANT_READ;
    bytes_read = file_read(&hdr, 1, sizeof hdr, wth->fh);
    if (bytes_read != sizeof hdr) {
	*err = file_error(wth->fh);
	if (*err != 0)
	    return -1;
	if (bytes_read != 0) {
	    *err = WTAP_ERR_SHORT_READ;
	    return -1;
	}
	return 0;
    }
    wth->data_offset += sizeof hdr;
    length = pntohs(&hdr.length);
    if (length <= 0) return 0;

    wth->phdr.len = length;
    wth->phdr.caplen = length;

    wth->phdr.ts.tv_sec = pntohl(&hdr.sec);
    wth->phdr.ts.tv_usec = pntohl(&hdr.usec);
    if (wth->capture.nettl->start == 0)
	wth->capture.nettl->start = wth->phdr.ts.tv_sec;
    wth->phdr.pseudo_header.x25.flags = (hdr.from_dce & 0x20 ? 0x80 : 0x00);

    /*
     * Read the packet data.
     */
    buffer_assure_space(wth->frame_buffer, length);
    data_offset = wth->data_offset;
    errno = WTAP_ERR_CANT_READ;
    bytes_read = file_read(buffer_start_ptr(wth->frame_buffer), 1,
	    length, wth->fh);

    if (bytes_read != length) {
	*err = file_error(wth->fh);
	if (*err == 0)
	    *err = WTAP_ERR_SHORT_READ;
	return -1;
    }
    wth->data_offset += length;

    wth->phdr.pkt_encap = wth->file_encap;

    return data_offset;
}