4 * Copyright (c) 1999 by Bert Driehuis <driehuis@playbeing.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "file_wrappers.h"
28 #include <wsutil/buffer.h>
29 #include "i4b_trace.h"
33 gboolean byte_swapped;
36 static gboolean i4btrace_read(wtap *wth, int *err, gchar **err_info,
38 static gboolean i4btrace_seek_read(wtap *wth, gint64 seek_off,
39 struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info);
40 static int i4b_read_rec(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
41 Buffer *buf, int *err, gchar **err_info);
44 * Test some fields in the header to see if they make sense.
46 #define I4B_HDR_IS_OK(hdr) \
47 (!((unsigned int)hdr.length < 3 || (unsigned int)hdr.length > 16384 || \
48 (unsigned int)hdr.unit > 4 || (unsigned int)hdr.type > 4 || \
49 (unsigned int)hdr.dir > 2 || (unsigned int)hdr.trunc > 2048))
51 int i4btrace_open(wtap *wth, int *err, gchar **err_info)
54 gboolean byte_swapped = FALSE;
57 /* I4B trace files have no magic in the header... Sigh */
58 errno = WTAP_ERR_CANT_READ;
59 if (!wtap_read_bytes(wth->fh, &hdr, sizeof(hdr), err, err_info)) {
60 if (*err != WTAP_ERR_SHORT_READ)
65 /* Silly heuristic... */
66 if (!I4B_HDR_IS_OK(hdr)) {
68 * OK, try byte-swapping the header fields.
70 hdr.length = GUINT32_SWAP_LE_BE(hdr.length);
71 hdr.unit = GUINT32_SWAP_LE_BE(hdr.unit);
72 hdr.type = GUINT32_SWAP_LE_BE(hdr.type);
73 hdr.dir = GUINT32_SWAP_LE_BE(hdr.dir);
74 hdr.trunc = GUINT32_SWAP_LE_BE(hdr.trunc);
75 if (!I4B_HDR_IS_OK(hdr)) {
77 * It doesn't look valid in either byte order.
83 * It looks valid byte-swapped, so assume it's a
84 * trace written in the opposite byte order.
89 if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
92 /* Get capture start time */
94 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_I4BTRACE;
95 i4btrace = (i4btrace_t *)g_malloc(sizeof(i4btrace_t));
96 wth->priv = (void *)i4btrace;
97 wth->subtype_read = i4btrace_read;
98 wth->subtype_seek_read = i4btrace_seek_read;
99 wth->snapshot_length = 0; /* not known */
101 i4btrace->byte_swapped = byte_swapped;
103 wth->file_encap = WTAP_ENCAP_ISDN;
104 wth->file_tsprec = WTAP_TSPREC_USEC;
109 /* Read the next packet */
110 static gboolean i4btrace_read(wtap *wth, int *err, gchar **err_info,
113 *data_offset = file_tell(wth->fh);
115 return i4b_read_rec(wth, wth->fh, &wth->phdr, wth->frame_buffer,
120 i4btrace_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr,
121 Buffer *buf, int *err, gchar **err_info)
123 if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
126 if (!i4b_read_rec(wth, wth->random_fh, phdr, buf, err, err_info)) {
127 /* Read error or EOF */
129 /* EOF means "short read" in random-access mode */
130 *err = WTAP_ERR_SHORT_READ;
138 i4b_read_rec(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
139 int *err, gchar **err_info)
141 i4btrace_t *i4btrace = (i4btrace_t *)wth->priv;
145 errno = WTAP_ERR_CANT_READ;
146 if (!wtap_read_bytes_or_eof(fh, &hdr, sizeof hdr, err, err_info))
149 if (i4btrace->byte_swapped) {
151 * Byte-swap the header.
153 hdr.length = GUINT32_SWAP_LE_BE(hdr.length);
154 hdr.unit = GUINT32_SWAP_LE_BE(hdr.unit);
155 hdr.type = GUINT32_SWAP_LE_BE(hdr.type);
156 hdr.dir = GUINT32_SWAP_LE_BE(hdr.dir);
157 hdr.trunc = GUINT32_SWAP_LE_BE(hdr.trunc);
158 hdr.count = GUINT32_SWAP_LE_BE(hdr.count);
159 hdr.ts_sec = GUINT32_SWAP_LE_BE(hdr.ts_sec);
160 hdr.ts_usec = GUINT32_SWAP_LE_BE(hdr.ts_usec);
163 if (hdr.length < sizeof(hdr)) {
164 *err = WTAP_ERR_BAD_FILE; /* record length < header! */
165 *err_info = g_strdup_printf("i4btrace: record length %u < header length %lu",
166 hdr.length, (unsigned long)sizeof(hdr));
169 length = hdr.length - (guint32)sizeof(hdr);
170 if (length > WTAP_MAX_PACKET_SIZE) {
172 * Probably a corrupt capture file; don't blow up trying
173 * to allocate space for an immensely-large packet.
175 *err = WTAP_ERR_BAD_FILE;
176 *err_info = g_strdup_printf("i4btrace: File has %u-byte packet, bigger than maximum of %u",
177 length, WTAP_MAX_PACKET_SIZE);
181 phdr->rec_type = REC_TYPE_PACKET;
182 phdr->presence_flags = WTAP_HAS_TS;
185 phdr->caplen = length;
187 phdr->ts.secs = hdr.ts_sec;
188 phdr->ts.nsecs = hdr.ts_usec * 1000;
194 * XXX - what is it? It's probably not WTAP_ENCAP_NULL,
195 * as that means it has a 4-byte AF_ type as the
196 * encapsulation header.
198 phdr->pkt_encap = WTAP_ENCAP_NULL;
203 * D channel, so it's LAPD; set "p2p.sent".
205 phdr->pkt_encap = WTAP_ENCAP_ISDN;
206 phdr->pseudo_header.isdn.channel = 0;
213 phdr->pkt_encap = WTAP_ENCAP_ISDN;
214 phdr->pseudo_header.isdn.channel = 1;
221 phdr->pkt_encap = WTAP_ENCAP_ISDN;
222 phdr->pseudo_header.isdn.channel = 2;
226 phdr->pseudo_header.isdn.uton = (hdr.dir == FROM_TE);
229 * Read the packet data.
231 return wtap_read_packet_bytes(fh, buf, length, err, err_info);