From Pascal Quantin:
[obnox/wireshark/wip.git] / pcapio.c
1 /* pcapio.c
2  * Our own private code for writing libpcap files when capturing.
3  *
4  * We have these because we want a way to open a stream for output given
5  * only a file descriptor.  libpcap 0.9[.x] has "pcap_dump_fopen()", which
6  * provides that, but
7  *
8  *      1) earlier versions of libpcap doesn't have it
9  *
10  * and
11  *
12  *      2) WinPcap doesn't have it, because a file descriptor opened
13  *         by code built for one version of the MSVC++ C library
14  *         can't be used by library routines built for another version
15  *         (e.g., threaded vs. unthreaded).
16  *
17  * Libpcap's pcap_dump() also doesn't return any error indications.
18  *
19  * $Id$
20  *
21  * Wireshark - Network traffic analyzer
22  * By Gerald Combs <gerald@wireshark.org>
23  * Copyright 1998 Gerald Combs
24  *
25  * Derived from code in the Wiretap Library
26  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
27  *
28  * This program is free software; you can redistribute it and/or
29  * modify it under the terms of the GNU General Public License
30  * as published by the Free Software Foundation; either version 2
31  * of the License, or (at your option) any later version.
32  *
33  * This program is distributed in the hope that it will be useful,
34  * but WITHOUT ANY WARRANTY; without even the implied warranty of
35  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36  * GNU General Public License for more details.
37  *
38  * You should have received a copy of the GNU General Public License
39  * along with this program; if not, write to the Free Software
40  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
41  */
42
43 #ifdef HAVE_CONFIG_H
44 #include "config.h"
45 #endif
46
47 #ifdef HAVE_LIBPCAP
48
49 #include <stdlib.h>
50 #include <stdio.h>
51 #include <errno.h>
52
53 #include <pcap.h>
54
55 #include <glib.h>
56
57 #include "pcapio.h"
58
59 /* Magic numbers in "libpcap" files.
60
61    "libpcap" file records are written in the byte order of the host that
62    writes them, and the reader is expected to fix this up.
63
64    PCAP_MAGIC is the magic number, in host byte order; PCAP_SWAPPED_MAGIC
65    is a byte-swapped version of that.
66
67    PCAP_NSEC_MAGIC is for Ulf Lamping's modified "libpcap" format,
68    which uses the same common file format as PCAP_MAGIC, but the 
69    timestamps are saved in nanosecond resolution instead of microseconds.
70    PCAP_SWAPPED_NSEC_MAGIC is a byte-swapped version of that. */
71 #define PCAP_MAGIC                      0xa1b2c3d4
72 #define PCAP_SWAPPED_MAGIC              0xd4c3b2a1
73 #define PCAP_NSEC_MAGIC                 0xa1b23c4d
74 #define PCAP_SWAPPED_NSEC_MAGIC         0x4d3cb2a1
75
76 /* "libpcap" file header. */
77 struct pcap_hdr {
78         guint32 magic;          /* magic number */
79         guint16 version_major;  /* major version number */
80         guint16 version_minor;  /* minor version number */
81         gint32  thiszone;       /* GMT to local correction */
82         guint32 sigfigs;        /* accuracy of timestamps */
83         guint32 snaplen;        /* max length of captured packets, in octets */
84         guint32 network;        /* data link type */
85 };
86
87 /* "libpcap" record header. */
88 struct pcaprec_hdr {
89         guint32 ts_sec;         /* timestamp seconds */
90         guint32 ts_usec;        /* timestamp microseconds (nsecs for PCAP_NSEC_MAGIC) */
91         guint32 incl_len;       /* number of octets of packet saved in file */
92         guint32 orig_len;       /* actual length of packet */
93 };
94
95 /* Returns a FILE * to write to on success, NULL on failure; sets "*err" to
96    an error code, or 0 for a short write, on failure */
97 FILE *
98 libpcap_fdopen(int fd, int linktype, int snaplen, long *bytes_written,
99     int *err)
100 {
101         FILE *fp;
102         struct pcap_hdr file_hdr;
103         size_t nwritten;
104
105         fp = fdopen(fd, "wb");
106         if (fp == NULL) {
107                 *err = errno;
108                 return NULL;
109         }
110
111         file_hdr.magic = PCAP_MAGIC;
112         /* current "libpcap" format is 2.4 */
113         file_hdr.version_major = 2;
114         file_hdr.version_minor = 4;
115         file_hdr.thiszone = 0;  /* XXX - current offset? */
116         file_hdr.sigfigs = 0;   /* unknown, but also apparently unused */
117         file_hdr.snaplen = snaplen;
118         file_hdr.network = linktype;
119         nwritten = fwrite(&file_hdr, 1, sizeof file_hdr, fp);
120         if (nwritten != sizeof file_hdr) {
121                 if (nwritten == 0 && ferror(fp))
122                         *err = errno;
123                 else
124                         *err = 0;       /* short write */
125                 fclose(fp);
126                 return NULL;
127         }
128         *bytes_written = sizeof file_hdr;
129
130         return fp;
131 }
132
133 /* Write a record for a packet to a dump file.
134    Returns TRUE on success, FALSE on failure. */
135 gboolean
136 libpcap_write_packet(FILE *fp, const struct pcap_pkthdr *phdr, const u_char *pd,
137     long *bytes_written, int *err)
138 {
139         struct pcaprec_hdr rec_hdr;
140         size_t nwritten;
141
142         rec_hdr.ts_sec = phdr->ts.tv_sec;
143         rec_hdr.ts_usec = phdr->ts.tv_usec;
144         rec_hdr.incl_len = phdr->caplen;
145         rec_hdr.orig_len = phdr->len;
146         nwritten = fwrite(&rec_hdr, 1, sizeof rec_hdr, fp);
147         if (nwritten != sizeof rec_hdr) {
148                 if (nwritten == 0 && ferror(fp))
149                         *err = errno;
150                 else
151                         *err = 0;       /* short write */
152                 return FALSE;
153         }
154         *bytes_written += sizeof rec_hdr;
155
156         nwritten = fwrite(pd, 1, phdr->caplen, fp);
157         if (nwritten != phdr->caplen) {
158                 if (nwritten == 0 && ferror(fp))
159                         *err = errno;
160                 else
161                         *err = 0;       /* short write */
162                 return FALSE;
163         }
164         *bytes_written += phdr->caplen;
165         return TRUE;
166 }
167
168 gboolean
169 libpcap_dump_flush(FILE *pd, int *err)
170 {
171         if (fflush(pd) == EOF) {
172                 if (err != NULL)
173                         *err = errno;
174                 return FALSE;
175         }
176         return TRUE;
177 }
178
179 gboolean
180 libpcap_dump_close(FILE *pd, int *err)
181 {
182         if (fclose(pd) == EOF) {
183                 if (err != NULL)
184                         *err = errno;
185                 return FALSE;
186         }
187         return TRUE;
188 }
189
190 #endif /* HAVE_LIBPCAP */