6 * Copyright (c) 1999 by Bert Driehuis <driehuis@playbeing.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include "file_wrappers.h"
31 #include "i4b_trace.h"
35 gboolean byte_swapped
;
38 static gboolean
i4btrace_read(wtap
*wth
, int *err
, gchar
**err_info
,
40 static gboolean
i4btrace_seek_read(wtap
*wth
, gint64 seek_off
,
41 struct wtap_pkthdr
*phdr
, Buffer
*buf
, int length
,
42 int *err
, gchar
**err_info
);
43 static int i4b_read_rec(wtap
*wth
, FILE_T fh
, struct wtap_pkthdr
*phdr
,
44 Buffer
*buf
, int *err
, gchar
**err_info
);
47 * Test some fields in the header to see if they make sense.
49 #define I4B_HDR_IS_OK(hdr) \
50 (!((unsigned)hdr.length < 3 || (unsigned)hdr.length > 16384 || \
51 (unsigned)hdr.unit > 4 || (unsigned)hdr.type > 4 || \
52 (unsigned)hdr.dir > 2 || (unsigned)hdr.trunc > 2048))
54 int i4btrace_open(wtap
*wth
, int *err
, gchar
**err_info
)
58 gboolean byte_swapped
= FALSE
;
61 /* I4B trace files have no magic in the header... Sigh */
62 errno
= WTAP_ERR_CANT_READ
;
63 bytes_read
= file_read(&hdr
, sizeof(hdr
), wth
->fh
);
64 if (bytes_read
!= sizeof(hdr
)) {
65 *err
= file_error(wth
->fh
, err_info
);
66 if (*err
!= 0 && *err
!= WTAP_ERR_SHORT_READ
)
71 /* Silly heuristic... */
72 if (!I4B_HDR_IS_OK(hdr
)) {
74 * OK, try byte-swapping the header fields.
76 hdr
.length
= BSWAP32(hdr
.length
);
77 hdr
.unit
= BSWAP32(hdr
.unit
);
78 hdr
.type
= BSWAP32(hdr
.type
);
79 hdr
.dir
= BSWAP32(hdr
.dir
);
80 hdr
.trunc
= BSWAP32(hdr
.trunc
);
81 if (!I4B_HDR_IS_OK(hdr
)) {
83 * It doesn't look valid in either byte order.
89 * It looks valid byte-swapped, so assume it's a
90 * trace written in the opposite byte order.
95 if (file_seek(wth
->fh
, 0, SEEK_SET
, err
) == -1)
98 /* Get capture start time */
100 wth
->file_type_subtype
= WTAP_FILE_TYPE_SUBTYPE_I4BTRACE
;
101 i4btrace
= (i4btrace_t
*)g_malloc(sizeof(i4btrace_t
));
102 wth
->priv
= (void *)i4btrace
;
103 wth
->subtype_read
= i4btrace_read
;
104 wth
->subtype_seek_read
= i4btrace_seek_read
;
105 wth
->snapshot_length
= 0; /* not known */
107 i4btrace
->byte_swapped
= byte_swapped
;
109 wth
->file_encap
= WTAP_ENCAP_ISDN
;
110 wth
->tsprecision
= WTAP_FILE_TSPREC_USEC
;
115 /* Read the next packet */
116 static gboolean
i4btrace_read(wtap
*wth
, int *err
, gchar
**err_info
,
119 *data_offset
= file_tell(wth
->fh
);
121 return i4b_read_rec(wth
, wth
->fh
, &wth
->phdr
, wth
->frame_buffer
,
126 i4btrace_seek_read(wtap
*wth
, gint64 seek_off
, struct wtap_pkthdr
*phdr
,
127 Buffer
*buf
, int length _U_
, int *err
, gchar
**err_info
)
129 if (file_seek(wth
->random_fh
, seek_off
, SEEK_SET
, err
) == -1)
132 if (!i4b_read_rec(wth
, wth
->random_fh
, phdr
, buf
, err
, err_info
)) {
133 /* Read error or EOF */
135 /* EOF means "short read" in random-access mode */
136 *err
= WTAP_ERR_SHORT_READ
;
144 i4b_read_rec(wtap
*wth
, FILE_T fh
, struct wtap_pkthdr
*phdr
, Buffer
*buf
,
145 int *err
, gchar
**err_info
)
147 i4btrace_t
*i4btrace
= (i4btrace_t
*)wth
->priv
;
152 errno
= WTAP_ERR_CANT_READ
;
153 bytes_read
= file_read(&hdr
, sizeof hdr
, fh
);
154 if (bytes_read
!= sizeof hdr
) {
155 *err
= file_error(fh
, err_info
);
156 if (*err
== 0 && bytes_read
!= 0) {
157 /* Read something, but not enough */
158 *err
= WTAP_ERR_SHORT_READ
;
163 if (i4btrace
->byte_swapped
) {
165 * Byte-swap the header.
167 hdr
.length
= BSWAP32(hdr
.length
);
168 hdr
.unit
= BSWAP32(hdr
.unit
);
169 hdr
.type
= BSWAP32(hdr
.type
);
170 hdr
.dir
= BSWAP32(hdr
.dir
);
171 hdr
.trunc
= BSWAP32(hdr
.trunc
);
172 hdr
.count
= BSWAP32(hdr
.count
);
173 hdr
.ts_sec
= BSWAP32(hdr
.ts_sec
);
174 hdr
.ts_usec
= BSWAP32(hdr
.ts_usec
);
177 if (hdr
.length
< sizeof(hdr
)) {
178 *err
= WTAP_ERR_BAD_FILE
; /* record length < header! */
179 *err_info
= g_strdup_printf("i4btrace: record length %u < header length %lu",
180 hdr
.length
, (unsigned long)sizeof(hdr
));
183 length
= hdr
.length
- (guint32
)sizeof(hdr
);
184 if (length
> WTAP_MAX_PACKET_SIZE
) {
186 * Probably a corrupt capture file; don't blow up trying
187 * to allocate space for an immensely-large packet.
189 *err
= WTAP_ERR_BAD_FILE
;
190 *err_info
= g_strdup_printf("i4btrace: File has %u-byte packet, bigger than maximum of %u",
191 length
, WTAP_MAX_PACKET_SIZE
);
195 phdr
->presence_flags
= WTAP_HAS_TS
;
198 phdr
->caplen
= length
;
200 phdr
->ts
.secs
= hdr
.ts_sec
;
201 phdr
->ts
.nsecs
= hdr
.ts_usec
* 1000;
207 * XXX - what is it? It's probably not WTAP_ENCAP_NULL,
208 * as that means it has a 4-byte AF_ type as the
209 * encapsulation header.
211 phdr
->pkt_encap
= WTAP_ENCAP_NULL
;
216 * D channel, so it's LAPD; set "p2p.sent".
218 phdr
->pkt_encap
= WTAP_ENCAP_ISDN
;
219 phdr
->pseudo_header
.isdn
.channel
= 0;
226 phdr
->pkt_encap
= WTAP_ENCAP_ISDN
;
227 phdr
->pseudo_header
.isdn
.channel
= 1;
234 phdr
->pkt_encap
= WTAP_ENCAP_ISDN
;
235 phdr
->pseudo_header
.isdn
.channel
= 2;
239 phdr
->pseudo_header
.isdn
.uton
= (hdr
.dir
== FROM_TE
);
242 * Read the packet data.
244 return wtap_read_packet_bytes(fh
, buf
, length
, err
, err_info
);