3 * $Id: nettl.c,v 1.13 2000/05/19 23:06:57 gram Exp $
6 * Copyright (c) 1998 by Gilbert Ramirez <gram@xiexie.org>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 #include "file_wrappers.h"
35 static char nettl_magic_hpux9[12] = {
36 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xD0, 0x00
38 static char nettl_magic_hpux10[12] = {
39 0x54, 0x52, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80
42 /* HP nettl record header for the SX25L2 subsystem - The FCS is not included in the file. */
43 struct nettlrec_sx25l2_hdr {
48 guint8 length2[2]; /* don't know which one is captured length / real length */
55 /* HP nettl record header for the NS_LS_IP subsystem */
56 struct nettlrec_ns_ls_ip_hdr {
59 guint8 length2[4]; /* don't know which one is captured length / real length */
65 /* header is followed by data and once again the total length (2 bytes) ! */
67 static int nettl_read(wtap *wth, int *err);
68 static int nettl_seek_read(wtap *wth, int seek_off,
69 union wtap_pseudo_header *pseudo_header, u_char *pd, int length);
70 static int nettl_read_rec_header(wtap *wth, FILE_T fh,
71 struct wtap_pkthdr *phdr, union wtap_pseudo_header *pseudo_header,
73 static int nettl_read_rec_data(FILE_T fh, char *pd, int length, int *err);
74 static void nettl_close(wtap *wth);
76 int nettl_open(wtap *wth, int *err)
78 char magic[12], os_vers[2];
81 /* Read in the string that should be at the start of a HP file */
82 file_seek(wth->fh, 0, SEEK_SET);
83 errno = WTAP_ERR_CANT_READ;
84 bytes_read = file_read(magic, 1, 12, wth->fh);
85 if (bytes_read != 12) {
86 *err = file_error(wth->fh);
92 if (memcmp(magic, nettl_magic_hpux9, 12) &&
93 memcmp(magic, nettl_magic_hpux10, 12)) {
97 file_seek(wth->fh, 0x63, SEEK_SET);
98 wth->data_offset = 0x63;
99 bytes_read = file_read(os_vers, 1, 2, wth->fh);
100 if (bytes_read != 2) {
101 *err = file_error(wth->fh);
107 file_seek(wth->fh, 0x80, SEEK_SET);
108 wth->data_offset = 0x80;
110 /* This is an nettl file */
111 wth->file_type = WTAP_FILE_NETTL;
112 wth->capture.nettl = g_malloc(sizeof(nettl_t));
113 if (os_vers[0] == '1' && os_vers[1] == '1')
114 wth->capture.nettl->is_hpux_11 = TRUE;
116 wth->capture.nettl->is_hpux_11 = FALSE;
117 wth->subtype_read = nettl_read;
118 wth->subtype_seek_read = nettl_seek_read;
119 wth->subtype_close = nettl_close;
120 wth->snapshot_length = 16384; /* not available in header, only in frame */
125 /* Read the next packet */
126 static int nettl_read(wtap *wth, int *err)
131 /* Read record header. */
132 record_offset = wth->data_offset;
133 ret = nettl_read_rec_header(wth, wth->fh, &wth->phdr, &wth->pseudo_header,
136 /* Read error or EOF */
139 wth->data_offset += ret;
142 * Read the packet data.
144 buffer_assure_space(wth->frame_buffer, wth->phdr.caplen);
145 if (nettl_read_rec_data(wth->fh, buffer_start_ptr(wth->frame_buffer),
146 wth->phdr.caplen, err) < 0)
147 return -1; /* Read error */
148 wth->data_offset += wth->phdr.caplen;
149 return record_offset;
153 nettl_seek_read(wtap *wth, int seek_off,
154 union wtap_pseudo_header *pseudo_header, u_char *pd, int length)
157 int err; /* XXX - return this */
158 struct wtap_pkthdr phdr;
160 file_seek(wth->random_fh, seek_off, SEEK_SET);
162 /* Read record header. */
163 ret = nettl_read_rec_header(wth, wth->random_fh, &phdr, pseudo_header,
166 /* Read error or EOF */
171 * Read the packet data.
173 return nettl_read_rec_data(wth->random_fh, pd, length, &err);
177 nettl_read_rec_header(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
178 union wtap_pseudo_header *pseudo_header, int *err)
181 struct nettlrec_sx25l2_hdr lapb_hdr;
182 struct nettlrec_ns_ls_ip_hdr ip_hdr;
188 errno = WTAP_ERR_CANT_READ;
189 bytes_read = file_read(encap, 1, 4, fh);
190 if (bytes_read != 4) {
191 *err = file_error(fh);
194 if (bytes_read != 0) {
195 *err = WTAP_ERR_SHORT_READ;
203 case NETTL_SUBSYS_NS_LS_IP :
204 phdr->pkt_encap = WTAP_ENCAP_RAW_IP;
205 bytes_read = file_read(&ip_hdr, 1, sizeof ip_hdr, fh);
206 if (bytes_read != sizeof ip_hdr) {
207 *err = file_error(fh);
210 if (bytes_read != 0) {
211 *err = WTAP_ERR_SHORT_READ;
216 offset += sizeof ip_hdr;
218 /* The packet header in HP-UX 11 nettl traces is 4 octets longer than
220 if (wth->capture.nettl->is_hpux_11) {
221 bytes_read = file_read(dummy, 1, 4, fh);
222 if (bytes_read != 4) {
223 *err = file_error(fh);
226 if (bytes_read != 0) {
227 *err = WTAP_ERR_SHORT_READ;
235 length = pntohl(&ip_hdr.length);
236 if (length <= 0) return 0;
238 phdr->caplen = length;
240 phdr->ts.tv_sec = pntohl(&ip_hdr.sec);
241 phdr->ts.tv_usec = pntohl(&ip_hdr.usec);
243 case NETTL_SUBSYS_SX25L2 :
244 phdr->pkt_encap = WTAP_ENCAP_LAPB;
245 bytes_read = file_read(&lapb_hdr, 1, sizeof lapb_hdr, fh);
246 if (bytes_read != sizeof lapb_hdr) {
247 *err = file_error(fh);
250 if (bytes_read != 0) {
251 *err = WTAP_ERR_SHORT_READ;
256 offset += sizeof lapb_hdr;
258 if (wth->capture.nettl->is_hpux_11) {
259 bytes_read = file_read(dummy, 1, 4, fh);
260 if (bytes_read != 4) {
261 *err = file_error(fh);
264 if (bytes_read != 0) {
265 *err = WTAP_ERR_SHORT_READ;
273 length = pntohs(&lapb_hdr.length);
274 if (length <= 0) return 0;
276 phdr->caplen = length;
278 phdr->ts.tv_sec = pntohl(&lapb_hdr.sec);
279 phdr->ts.tv_usec = pntohl(&lapb_hdr.usec);
280 pseudo_header->x25.flags = (lapb_hdr.from_dce & 0x20 ? 0x80 : 0x00);
283 g_message("nettl: network type %u unknown or unsupported",
285 *err = WTAP_ERR_UNSUPPORTED_ENCAP;
292 nettl_read_rec_data(FILE_T fh, char *pd, int length, int *err)
296 bytes_read = file_read(pd, 1, length, fh);
298 if (bytes_read != length) {
299 *err = file_error(fh);
301 *err = WTAP_ERR_SHORT_READ;
307 static void nettl_close(wtap *wth)
309 g_free(wth->capture.nettl);