From: Shashidhar Bhandare
[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  * Ethereal - Network traffic analyzer
22  * By Gerald Combs <gerald@ethereal.com>
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 #include <stdlib.h>
48 #include <stdio.h>
49 #include <errno.h>
50
51 #include <pcap.h>
52
53 #include <glib.h>
54
55 #include "pcapio.h"
56
57 /* Magic numbers in "libpcap" files.
58
59    "libpcap" file records are written in the byte order of the host that
60    writes them, and the reader is expected to fix this up.
61
62    PCAP_MAGIC is the magic number, in host byte order; PCAP_SWAPPED_MAGIC
63    is a byte-swapped version of that.
64
65    PCAP_NSEC_MAGIC is for Ulf Lamping's modified "libpcap" format,
66    which uses the same common file format as PCAP_MAGIC, but the 
67    timestamps are saved in nanosecond resolution instead of microseconds.
68    PCAP_SWAPPED_NSEC_MAGIC is a byte-swapped version of that. */
69 #define PCAP_MAGIC                      0xa1b2c3d4
70 #define PCAP_SWAPPED_MAGIC              0xd4c3b2a1
71 #define PCAP_NSEC_MAGIC                 0xa1b23c4d
72 #define PCAP_SWAPPED_NSEC_MAGIC         0x4d3cb2a1
73
74 /* "libpcap" file header. */
75 struct pcap_hdr {
76         guint32 magic;          /* magic number */
77         guint16 version_major;  /* major version number */
78         guint16 version_minor;  /* minor version number */
79         gint32  thiszone;       /* GMT to local correction */
80         guint32 sigfigs;        /* accuracy of timestamps */
81         guint32 snaplen;        /* max length of captured packets, in octets */
82         guint32 network;        /* data link type */
83 };
84
85 /* "libpcap" record header. */
86 struct pcaprec_hdr {
87         guint32 ts_sec;         /* timestamp seconds */
88         guint32 ts_usec;        /* timestamp microseconds (nsecs for PCAP_NSEC_MAGIC) */
89         guint32 incl_len;       /* number of octets of packet saved in file */
90         guint32 orig_len;       /* actual length of packet */
91 };
92
93 /* Returns a FILE * to write to on success, NULL on failure; sets "*err" to
94    an error code, or 0 for a short write, on failure */
95 FILE *
96 libpcap_fdopen(int fd, int linktype, int snaplen, long *bytes_written,
97     int *err)
98 {
99         FILE *fp;
100         struct pcap_hdr file_hdr;
101         size_t nwritten;
102
103         fp = fdopen(fd, "wb");
104         if (fp == NULL) {
105                 *err = errno;
106                 return NULL;
107         }
108
109         file_hdr.magic = PCAP_MAGIC;
110         /* current "libpcap" format is 2.4 */
111         file_hdr.version_major = 2;
112         file_hdr.version_minor = 4;
113         file_hdr.thiszone = 0;  /* XXX - current offset? */
114         file_hdr.sigfigs = 0;   /* unknown, but also apparently unused */
115         file_hdr.snaplen = snaplen;
116         file_hdr.network = linktype;
117         nwritten = fwrite(&file_hdr, 1, sizeof file_hdr, fp);
118         if (nwritten != sizeof file_hdr) {
119                 if (nwritten == 0 && ferror(fp))
120                         *err = errno;
121                 else
122                         *err = 0;       /* short write */
123                 fclose(fp);
124                 return NULL;
125         }
126         *bytes_written = sizeof file_hdr;
127
128         return fp;
129 }
130
131 /* Write a record for a packet to a dump file.
132    Returns TRUE on success, FALSE on failure. */
133 gboolean
134 libpcap_write_packet(FILE *fp, const struct pcap_pkthdr *phdr, const u_char *pd,
135     long *bytes_written, int *err)
136 {
137         struct pcaprec_hdr rec_hdr;
138         size_t nwritten;
139
140         rec_hdr.ts_sec = phdr->ts.tv_sec;
141         rec_hdr.ts_usec = phdr->ts.tv_usec;
142         rec_hdr.incl_len = phdr->caplen;
143         rec_hdr.orig_len = phdr->len;
144         nwritten = fwrite(&rec_hdr, 1, sizeof rec_hdr, fp);
145         if (nwritten != sizeof rec_hdr) {
146                 if (nwritten == 0 && ferror(fp))
147                         *err = errno;
148                 else
149                         *err = 0;       /* short write */
150                 return FALSE;
151         }
152         *bytes_written += sizeof rec_hdr;
153
154         nwritten = fwrite(pd, 1, phdr->caplen, fp);
155         if (nwritten != phdr->caplen) {
156                 if (nwritten == 0 && ferror(fp))
157                         *err = errno;
158                 else
159                         *err = 0;       /* short write */
160                 return FALSE;
161         }
162         *bytes_written += phdr->caplen;
163         return TRUE;
164 }
165
166 gboolean
167 libpcap_dump_flush(FILE *pd, int *err)
168 {
169         if (fflush(pd) == EOF) {
170                 if (err != NULL)
171                         *err = errno;
172                 return FALSE;
173         }
174         return TRUE;
175 }
176
177 gboolean
178 libpcap_dump_close(FILE *pd, int *err)
179 {
180         if (fclose(pd) == EOF) {
181                 if (err != NULL)
182                         *err = errno;
183                 return FALSE;
184         }
185         return TRUE;
186 }