3 * $Id: file.c,v 1.24 1999/09/24 05:49:50 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"
45 /* The open_file_* routines should return:
49 * 1 if the file they're reading is one of the types it handles;
51 * 0 if the file they're reading isn't the type they're checking for.
53 * If the routine handles this type of file, it should set the "file_type"
54 * field in the "struct wtap" to the type of the file.
56 * XXX - I need to drag my damn ANSI C spec in to figure out how to
57 * declare a "const" array of pointers to functions; putting "const"
58 * right after "static" isn't the right answer, at least according
59 * to GCC, which whines if I do that.
62 static int (*open_routines[])(wtap *, int *) = {
74 int wtap_def_seek_read (FILE *fh, int seek_off, guint8 *pd, int len)
76 file_seek(fh, seek_off, SEEK_SET);
77 return file_read(pd, sizeof(guint8), len, fh);
80 #define N_FILE_TYPES (sizeof open_routines / sizeof open_routines[0])
82 /* Opens a file and prepares a wtap struct */
83 wtap* wtap_open_offline(const char *filename, int *err)
89 /* First, make sure the file is valid */
90 if (stat(filename, &statb) < 0) {
95 if (! S_ISREG(statb.st_mode) && ! S_ISFIFO(statb.st_mode)) {
96 *err = WTAP_ERR_NOT_REGULAR_FILE;
102 wth = (wtap*)malloc(sizeof(wtap));
109 errno = WTAP_ERR_CANT_OPEN;
110 if (!(wth->fd = open(filename, O_RDONLY))) {
115 if (!(wth->fh = filed_open(wth->fd, "rb"))) {
122 wth->file_encap = WTAP_ENCAP_UNKNOWN;
123 wth->data_offset = 0;
125 /* Try all file types */
126 for (i = 0; i < N_FILE_TYPES; i++) {
127 switch ((*open_routines[i])(wth, err)) {
130 /* I/O error - give up */
137 /* No I/O error, but not that type of file */
141 /* We found the file type */
146 /* Well, it's not one of the types of file we know about. */
149 *err = WTAP_ERR_FILE_UNKNOWN_FORMAT;
153 wth->frame_buffer = g_malloc(sizeof(struct Buffer));
154 buffer_init(wth->frame_buffer, 1500);
159 static wtap_dumper* wtap_dump_open_common(FILE *fh, int filetype,
160 int encap, int snaplen, int *err);
162 wtap_dumper* wtap_dump_open(const char *filename, int filetype, int encap,
163 int snaplen, int *err)
167 /* In case "fopen()" fails but doesn't set "errno", set "errno"
168 to a generic "the open failed" error. */
169 errno = WTAP_ERR_CANT_OPEN;
170 fh = fopen(filename, "w");
173 return NULL; /* can't create file */
175 return wtap_dump_open_common(fh, filetype, encap, snaplen, err);
178 wtap_dumper* wtap_dump_fdopen(int fd, int filetype, int encap, int snaplen,
183 /* In case "fopen()" fails but doesn't set "errno", set "errno"
184 to a generic "the open failed" error. */
185 errno = WTAP_ERR_CANT_OPEN;
186 fh = fdopen(fd, "w");
189 return NULL; /* can't create standard I/O stream */
191 return wtap_dump_open_common(fh, filetype, encap, snaplen, err);
194 static wtap_dumper* wtap_dump_open_common(FILE *fh, int filetype, int encap,
195 int snaplen, int *err)
199 wdh = malloc(sizeof (wtap_dumper));
202 /* NOTE: this means the FD handed to "wtap_dump_fdopen()"
203 will be closed if the malloc fails. */
208 wdh->file_type = filetype;
209 wdh->snaplen = snaplen;
215 if (!libpcap_dump_open(wdh, err))
220 /* We currently only support dumping "libpcap" files */
221 *err = WTAP_ERR_UNSUPPORTED_FILE_TYPE;
229 return NULL; /* XXX - provide a reason why we failed */
232 FILE* wtap_dump_file(wtap_dumper *wdh)
237 int wtap_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
238 const u_char *pd, int *err)
240 return (wdh->subtype_write)(wdh, phdr, pd, err);
243 int wtap_dump_close(wtap_dumper *wdh, int *err)
247 if (!(wdh->subtype_close)(wdh, err))
249 errno = WTAP_ERR_CANT_CLOSE;
250 if (fclose(wdh->fh) == EOF) {
252 /* The per-format close function succeeded,
253 but the fclose didn't. Save the reason
254 why, if our caller asked for it. */