3 * $Id: nettl.c,v 1.8 2000/03/01 10:25:14 oabad 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 static gboolean is_hpux_11;
44 /* HP nettl record header for the SX25L2 subsystem - The FCS is not included in the file. */
45 struct nettlrec_sx25l2_hdr {
50 guint8 length2[2]; /* don't know which one is captured length / real length */
57 /* HP nettl record header for the NS_LS_IP subsystem */
58 struct nettlrec_ns_ls_ip_hdr {
61 guint8 length2[4]; /* don't know which one is captured length / real length */
67 /* header is followed by data and once again the total length (2 bytes) ! */
69 static int nettl_read(wtap *wth, int *err);
71 int nettl_open(wtap *wth, int *err)
73 char magic[12], os_vers[2];
76 /* Read in the string that should be at the start of a HP file */
77 file_seek(wth->fh, 0, SEEK_SET);
78 errno = WTAP_ERR_CANT_READ;
79 bytes_read = file_read(magic, 1, 12, wth->fh);
80 if (bytes_read != 12) {
81 *err = file_error(wth->fh);
87 if (memcmp(magic, nettl_magic_hpux9, 12) &&
88 memcmp(magic, nettl_magic_hpux10, 12)) {
92 file_seek(wth->fh, 0x63, SEEK_SET);
93 wth->data_offset = 0x63;
94 bytes_read = file_read(os_vers, 1, 2, wth->fh);
95 if (bytes_read != 2) {
96 *err = file_error(wth->fh);
101 if (os_vers[0] == '1' && os_vers[1] == '1')
106 file_seek(wth->fh, 0x80, SEEK_SET);
107 wth->data_offset = 0x80;
109 /* This is an nettl file */
110 wth->file_type = WTAP_FILE_NETTL;
111 wth->capture.nettl = g_malloc(sizeof(nettl_t));
112 wth->subtype_read = nettl_read;
113 wth->snapshot_length = 16384; /* not available in header, only in frame */
115 wth->capture.nettl->start = 0;
120 /* Read the next packet */
121 static int nettl_read(wtap *wth, int *err)
124 struct nettlrec_sx25l2_hdr lapb_hdr;
125 struct nettlrec_ns_ls_ip_hdr ip_hdr;
131 /* Read record header. */
132 errno = WTAP_ERR_CANT_READ;
133 bytes_read = file_read(encap, 1, 4, wth->fh);
134 if (bytes_read != 4) {
135 *err = file_error(wth->fh);
138 if (bytes_read != 0) {
139 *err = WTAP_ERR_SHORT_READ;
144 wth->data_offset += 4;
146 case NETTL_SUBSYS_NS_LS_IP :
147 wth->phdr.pkt_encap = WTAP_ENCAP_RAW_IP;
148 bytes_read = file_read(&ip_hdr, 1, sizeof ip_hdr, wth->fh);
149 if (bytes_read != sizeof ip_hdr) {
150 *err = file_error(wth->fh);
153 if (bytes_read != 0) {
154 *err = WTAP_ERR_SHORT_READ;
159 wth->data_offset += sizeof ip_hdr;
161 /* The packet header in HP-UX 11 nettl traces is 4 octets longer than
164 bytes_read = file_read(dummy, 1, 4, wth->fh);
165 if (bytes_read != 4) {
166 *err = file_error(wth->fh);
169 if (bytes_read != 0) {
170 *err = WTAP_ERR_SHORT_READ;
175 wth->data_offset += 4;
178 length = pntohl(&ip_hdr.length);
179 if (length <= 0) return 0;
180 wth->phdr.len = length;
181 wth->phdr.caplen = length;
183 wth->phdr.ts.tv_sec = pntohl(&ip_hdr.sec);
184 wth->phdr.ts.tv_usec = pntohl(&ip_hdr.usec);
185 if (wth->capture.nettl->start == 0)
186 wth->capture.nettl->start = wth->phdr.ts.tv_sec;
189 * Read the packet data.
191 buffer_assure_space(wth->frame_buffer, length);
192 data_offset = wth->data_offset;
193 errno = WTAP_ERR_CANT_READ;
194 bytes_read = file_read(buffer_start_ptr(wth->frame_buffer), 1,
197 if (bytes_read != length) {
198 *err = file_error(wth->fh);
200 *err = WTAP_ERR_SHORT_READ;
203 wth->data_offset += length;
205 case NETTL_SUBSYS_SX25L2 :
206 wth->phdr.pkt_encap = WTAP_ENCAP_LAPB;
207 bytes_read = file_read(&lapb_hdr, 1, sizeof lapb_hdr, wth->fh);
208 if (bytes_read != sizeof lapb_hdr) {
209 *err = file_error(wth->fh);
212 if (bytes_read != 0) {
213 *err = WTAP_ERR_SHORT_READ;
218 wth->data_offset += sizeof lapb_hdr;
221 bytes_read = file_read(dummy, 1, 4, wth->fh);
222 if (bytes_read != 4) {
223 *err = file_error(wth->fh);
226 if (bytes_read != 0) {
227 *err = WTAP_ERR_SHORT_READ;
232 wth->data_offset += 4;
235 length = pntohs(&lapb_hdr.length);
236 if (length <= 0) return 0;
237 wth->phdr.len = length;
238 wth->phdr.caplen = length;
240 wth->phdr.ts.tv_sec = pntohl(&lapb_hdr.sec);
241 wth->phdr.ts.tv_usec = pntohl(&lapb_hdr.usec);
242 if (wth->capture.nettl->start == 0)
243 wth->capture.nettl->start = wth->phdr.ts.tv_sec;
244 wth->phdr.pseudo_header.x25.flags = (lapb_hdr.from_dce & 0x20 ? 0x80 : 0x00);
247 * Read the packet data.
249 buffer_assure_space(wth->frame_buffer, length);
250 data_offset = wth->data_offset;
251 errno = WTAP_ERR_CANT_READ;
252 bytes_read = file_read(buffer_start_ptr(wth->frame_buffer), 1,
255 if (bytes_read != length) {
256 *err = file_error(wth->fh);
258 *err = WTAP_ERR_SHORT_READ;
261 wth->data_offset += length;
264 g_message("nettl: network type %u unknown or unsupported",
266 *err = WTAP_ERR_UNSUPPORTED_ENCAP;