3 * $Id: file.c,v 1.26 1999/10/18 01:51:34 guy Exp $
6 * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
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.
35 #include "lanalyzer.h"
36 #include "ngsniffer.h"
46 /* The open_file_* routines should return:
50 * 1 if the file they're reading is one of the types it handles;
52 * 0 if the file they're reading isn't the type they're checking for.
54 * If the routine handles this type of file, it should set the "file_type"
55 * field in the "struct wtap" to the type of the file.
57 * XXX - I need to drag my damn ANSI C spec in to figure out how to
58 * declare a "const" array of pointers to functions; putting "const"
59 * right after "static" isn't the right answer, at least according
60 * to GCC, which whines if I do that.
63 static int (*open_routines[])(wtap *, int *) = {
76 int wtap_def_seek_read (FILE *fh, int seek_off, guint8 *pd, int len)
78 file_seek(fh, seek_off, SEEK_SET);
79 return file_read(pd, sizeof(guint8), len, fh);
82 #define N_FILE_TYPES (sizeof open_routines / sizeof open_routines[0])
84 /* Opens a file and prepares a wtap struct */
85 wtap* wtap_open_offline(const char *filename, int *err)
91 /* First, make sure the file is valid */
92 if (stat(filename, &statb) < 0) {
97 if (! S_ISREG(statb.st_mode) && ! S_ISFIFO(statb.st_mode)) {
98 *err = WTAP_ERR_NOT_REGULAR_FILE;
104 wth = (wtap*)malloc(sizeof(wtap));
111 errno = WTAP_ERR_CANT_OPEN;
112 if (!(wth->fd = open(filename, O_RDONLY))) {
117 if (!(wth->fh = filed_open(wth->fd, "rb"))) {
124 wth->file_encap = WTAP_ENCAP_UNKNOWN;
125 wth->data_offset = 0;
127 /* Try all file types */
128 for (i = 0; i < N_FILE_TYPES; i++) {
129 switch ((*open_routines[i])(wth, err)) {
132 /* I/O error - give up */
139 /* No I/O error, but not that type of file */
143 /* We found the file type */
148 /* Well, it's not one of the types of file we know about. */
151 *err = WTAP_ERR_FILE_UNKNOWN_FORMAT;
155 wth->frame_buffer = g_malloc(sizeof(struct Buffer));
156 buffer_init(wth->frame_buffer, 1500);
161 static wtap_dumper* wtap_dump_open_common(FILE *fh, int filetype,
162 int encap, int snaplen, int *err);
164 wtap_dumper* wtap_dump_open(const char *filename, int filetype, int encap,
165 int snaplen, int *err)
169 /* In case "fopen()" fails but doesn't set "errno", set "errno"
170 to a generic "the open failed" error. */
171 errno = WTAP_ERR_CANT_OPEN;
172 fh = fopen(filename, "w");
175 return NULL; /* can't create file */
177 return wtap_dump_open_common(fh, filetype, encap, snaplen, err);
180 wtap_dumper* wtap_dump_fdopen(int fd, int filetype, int encap, int snaplen,
185 /* In case "fopen()" fails but doesn't set "errno", set "errno"
186 to a generic "the open failed" error. */
187 errno = WTAP_ERR_CANT_OPEN;
188 fh = fdopen(fd, "w");
191 return NULL; /* can't create standard I/O stream */
193 return wtap_dump_open_common(fh, filetype, encap, snaplen, err);
196 static wtap_dumper* wtap_dump_open_common(FILE *fh, int filetype, int encap,
197 int snaplen, int *err)
201 wdh = malloc(sizeof (wtap_dumper));
204 /* NOTE: this means the FD handed to "wtap_dump_fdopen()"
205 will be closed if the malloc fails. */
210 wdh->file_type = filetype;
211 wdh->snaplen = snaplen;
217 if (!libpcap_dump_open(wdh, err))
222 /* We currently only support dumping "libpcap" files */
223 *err = WTAP_ERR_UNSUPPORTED_FILE_TYPE;
231 return NULL; /* XXX - provide a reason why we failed */
234 FILE* wtap_dump_file(wtap_dumper *wdh)
239 int wtap_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
240 const u_char *pd, int *err)
242 return (wdh->subtype_write)(wdh, phdr, pd, err);
245 int wtap_dump_close(wtap_dumper *wdh, int *err)
249 if (!(wdh->subtype_close)(wdh, err))
251 errno = WTAP_ERR_CANT_CLOSE;
252 if (fclose(wdh->fh) == EOF) {
254 /* The per-format close function succeeded,
255 but the fclose didn't. Save the reason
256 why, if our caller asked for it. */
267 * Routine to return a Wiretap error code (0 for no error, an errno
268 * for a file error, or a WTAP_ERR_ code for other errors) for an
277 gzerror(fh, &errnum);
280 case Z_OK: /* no error */
283 case Z_STREAM_END: /* EOF - not an error */
286 case Z_ERRNO: /* file I/O error */
290 return WTAP_ERR_ZLIB + errnum;
293 #else /* HAVE_LIBZ */
302 #endif /* HAVE_LIBZ */