5a13761002fbc061225df163d494f6813406b788
[obnox/wireshark/wip.git] / wiretap / file.c
1 /* file.c
2  *
3  * $Id: file.c,v 1.9 1999/03/01 18:57:04 gram Exp $
4  *
5  * Wiretap Library
6  * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
7  * 
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.
12  * 
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.
17  * 
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.
21  *
22  */
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include "wtap.h"
28 #include "buffer.h"
29 #include "lanalyzer.h"
30 #include "ngsniffer.h"
31 #include "libpcap.h"
32 #include "snoop.h"
33 #include "iptrace.h"
34 #include "netmon.h"
35 #include "netxray.h"
36
37 /* The open_file_* routines should return the WTAP_FILE_* type
38  * that they are checking for if the file is successfully recognized
39  * as such. If the file is not of that type, the routine should return
40  * WTAP_FILE_UNKNOWN */
41
42 /* Opens a file and prepares a wtap struct */
43 wtap* wtap_open_offline(char *filename)
44 {
45         wtap    *wth;
46
47         wth = (wtap*)malloc(sizeof(wtap));
48
49         /* Open the file */
50         if (!(wth->fh = fopen(filename, "rb"))) {
51                 return NULL;
52         }
53
54         /* initialization */
55         wth->file_encap = WTAP_ENCAP_NONE;
56         wth->filter.offline = NULL;
57         wth->filter_type = WTAP_FILTER_NONE;
58         wth->filter_length = 0;
59         wth->offline_filter_lengths = NULL;
60
61         /* Try all file types */
62
63         /* WTAP_FILE_PCAP */
64         if ((wth->file_type = libpcap_open(wth)) != WTAP_FILE_UNKNOWN) {
65                 goto success;
66         }
67         /* WTAP_FILE_NGSNIFFER */
68         if ((wth->file_type = ngsniffer_open(wth)) != WTAP_FILE_UNKNOWN) {
69                 goto success;
70         }
71         /* WTAP_FILE_LANALYZER */
72         if ((wth->file_type = lanalyzer_open(wth)) != WTAP_FILE_UNKNOWN) {
73                 goto success;
74         }
75         /* WTAP_FILE_SNOOP */
76         if ((wth->file_type = snoop_open(wth)) != WTAP_FILE_UNKNOWN) {
77                 goto success;
78         }
79         /* WTAP_FILE_IPTRACE */
80         if ((wth->file_type = iptrace_open(wth)) != WTAP_FILE_UNKNOWN) {
81                 goto success;
82         }
83         /* WTAP_FILE_NETMON */
84         if ((wth->file_type = netmon_open(wth)) != WTAP_FILE_UNKNOWN) {
85                 goto success;
86         }
87         /* WTAP_FILE_NETXRAY */
88         if ((wth->file_type = netxray_open(wth)) != WTAP_FILE_UNKNOWN) {
89                 goto success;
90         }
91
92
93 /* failure: */
94         fclose(wth->fh);
95         free(wth);
96         wth = NULL;
97         return wth;
98
99 success:
100         wth->frame_buffer = g_malloc(sizeof(struct Buffer));
101         buffer_init(wth->frame_buffer, 1500);
102         return wth;
103 }