packet-smb2: make smb2_set_dcerpc_file_id() more reliable
[metze/wireshark/wip.git] / ringbuffer.c
1 /* ringbuffer.c
2  * Routines for packet capture windows
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 /*
24  * <laurent.deniel@free.fr>
25  *
26  * Almost completely rewritten in order to:
27  *
28  * - be able to use a unlimited number of ringbuffer files
29  * - close the current file and open (truncating) the next file at switch
30  * - set the final file name once open (or reopen)
31  * - avoid the deletion of files that could not be truncated (can't arise now)
32  *   and do not erase empty files
33  *
34  * The idea behind that is to remove the limitation of the maximum # of
35  * ringbuffer files being less than the maximum # of open fd per process
36  * and to be able to reduce the amount of virtual memory usage (having only
37  * one file open at most) or the amount of file system usage (by truncating
38  * the files at switch and not the capture stop, and by closing them which
39  * makes possible their move or deletion after a switch).
40  *
41  */
42
43 #include <config.h>
44
45 #ifdef HAVE_LIBPCAP
46
47 #ifdef HAVE_FCNTL_H
48 #include <fcntl.h>
49 #endif
50
51 #ifdef HAVE_UNISTD_H
52 #include <unistd.h>
53 #endif
54
55 #include <stdio.h>
56 #include <string.h>
57 #include <time.h>
58 #include <errno.h>
59
60 #include <pcap.h>
61
62 #include <glib.h>
63
64 #include "ringbuffer.h"
65 #include <wsutil/file_util.h>
66
67
68 /* Ringbuffer file structure */
69 typedef struct _rb_file {
70   gchar         *name;
71 } rb_file;
72
73 /* Ringbuffer data structure */
74 typedef struct _ringbuf_data {
75   rb_file      *files;
76   guint         num_files;           /* Number of ringbuffer files (1 to ...) */
77   guint         curr_file_num;       /* Number of the current file (ever increasing) */
78   gchar        *fprefix;             /* Filename prefix */
79   gchar        *fsuffix;             /* Filename suffix */
80   gboolean      unlimited;           /* TRUE if unlimited number of files */
81
82   int           fd;                  /* Current ringbuffer file descriptor */
83   FILE         *pdh;
84   gboolean      group_read_access;   /* TRUE if files need to be opened with group read access */
85 } ringbuf_data;
86
87 static ringbuf_data rb_data;
88
89
90 /*
91  * create the next filename and open a new binary file with that name
92  */
93 static int ringbuf_open_file(rb_file *rfile, int *err)
94 {
95   char    filenum[5+1];
96   char    timestr[14+1];
97   time_t  current_time;
98
99   if (rfile->name != NULL) {
100     if (rb_data.unlimited == FALSE) {
101       /* remove old file (if any, so ignore error) */
102       ws_unlink(rfile->name);
103     }
104     g_free(rfile->name);
105   }
106
107 #ifdef _WIN32
108   _tzset();
109 #endif
110   current_time = time(NULL);
111
112   g_snprintf(filenum, sizeof(filenum), "%05u", (rb_data.curr_file_num + 1) % RINGBUFFER_MAX_NUM_FILES);
113   strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S", localtime(&current_time));
114   rfile->name = g_strconcat(rb_data.fprefix, "_", filenum, "_", timestr,
115                             rb_data.fsuffix, NULL);
116
117   if (rfile->name == NULL) {
118     if (err != NULL)
119       *err = ENOMEM;
120     return -1;
121   }
122
123   rb_data.fd = ws_open(rfile->name, O_RDWR|O_BINARY|O_TRUNC|O_CREAT,
124                             rb_data.group_read_access ? 0640 : 0600);
125
126   if (rb_data.fd == -1 && err != NULL) {
127     *err = errno;
128   }
129
130   return rb_data.fd;
131 }
132
133 /*
134  * Initialize the ringbuffer data structures
135  */
136 int
137 ringbuf_init(const char *capfile_name, guint num_files, gboolean group_read_access)
138 {
139   unsigned int i;
140   char        *pfx, *last_pathsep;
141   gchar       *save_file;
142
143   rb_data.files = NULL;
144   rb_data.curr_file_num = 0;
145   rb_data.fprefix = NULL;
146   rb_data.fsuffix = NULL;
147   rb_data.unlimited = FALSE;
148   rb_data.fd = -1;
149   rb_data.pdh = NULL;
150   rb_data.group_read_access = group_read_access;
151
152   /* just to be sure ... */
153   if (num_files <= RINGBUFFER_MAX_NUM_FILES) {
154     rb_data.num_files = num_files;
155   } else {
156     rb_data.num_files = RINGBUFFER_MAX_NUM_FILES;
157   }
158
159   /* Check file name */
160   if (capfile_name == NULL) {
161     /* ringbuffer does not work with temporary files! */
162     return -1;
163   }
164
165   /* set file name prefix/suffix */
166
167   save_file = g_strdup(capfile_name);
168   last_pathsep = strrchr(save_file, G_DIR_SEPARATOR);
169   pfx = strrchr(save_file,'.');
170   if (pfx != NULL && (last_pathsep == NULL || pfx > last_pathsep)) {
171     /* The pathname has a "." in it, and it's in the last component
172        of the pathname (because there is either only one component,
173        i.e. last_pathsep is null as there are no path separators,
174        or the "." is after the path separator before the last
175        component.
176
177        Treat it as a separator between the rest of the file name and
178        the file name suffix, and arrange that the names given to the
179        ring buffer files have the specified suffix, i.e. put the
180        changing part of the name *before* the suffix. */
181     pfx[0] = '\0';
182     rb_data.fprefix = g_strdup(save_file);
183     pfx[0] = '.'; /* restore capfile_name */
184     rb_data.fsuffix = g_strdup(pfx);
185   } else {
186     /* Either there's no "." in the pathname, or it's in a directory
187        component, so the last component has no suffix. */
188     rb_data.fprefix = g_strdup(save_file);
189     rb_data.fsuffix = NULL;
190   }
191   g_free(save_file);
192   save_file = NULL;
193
194   /* allocate rb_file structures (only one if unlimited since there is no
195      need to save all file names in that case) */
196
197   if (num_files == RINGBUFFER_UNLIMITED_FILES) {
198     rb_data.unlimited = TRUE;
199     rb_data.num_files = 1;
200   }
201
202   rb_data.files = (rb_file *)g_malloc(rb_data.num_files * sizeof(rb_file));
203   if (rb_data.files == NULL) {
204     return -1;
205   }
206
207   for (i=0; i < rb_data.num_files; i++) {
208     rb_data.files[i].name = NULL;
209   }
210
211   /* create the first file */
212   if (ringbuf_open_file(&rb_data.files[0], NULL) == -1) {
213     ringbuf_error_cleanup();
214     return -1;
215   }
216
217   return rb_data.fd;
218 }
219
220
221 const gchar *ringbuf_current_filename(void)
222 {
223   return rb_data.files[rb_data.curr_file_num % rb_data.num_files].name;
224 }
225
226 /*
227  * Calls ws_fdopen() for the current ringbuffer file
228  */
229 FILE *
230 ringbuf_init_libpcap_fdopen(int *err)
231 {
232   rb_data.pdh = ws_fdopen(rb_data.fd, "wb");
233   if (rb_data.pdh == NULL) {
234     if (err != NULL) {
235       *err = errno;
236     }
237   }
238   return rb_data.pdh;
239 }
240
241 /*
242  * Switches to the next ringbuffer file
243  */
244 gboolean
245 ringbuf_switch_file(FILE **pdh, gchar **save_file, int *save_file_fd, int *err)
246 {
247   int     next_file_index;
248   rb_file *next_rfile = NULL;
249
250   /* close current file */
251
252   if (fclose(rb_data.pdh) == EOF) {
253     if (err != NULL) {
254       *err = errno;
255     }
256     ws_close(rb_data.fd);  /* XXX - the above should have closed this already */
257     rb_data.pdh = NULL;    /* it's still closed, we just got an error while closing */
258     rb_data.fd = -1;
259     return FALSE;
260   }
261
262   rb_data.pdh = NULL;
263   rb_data.fd  = -1;
264
265   /* get the next file number and open it */
266
267   rb_data.curr_file_num++ /* = next_file_num*/;
268   next_file_index = (rb_data.curr_file_num) % rb_data.num_files;
269   next_rfile = &rb_data.files[next_file_index];
270
271   if (ringbuf_open_file(next_rfile, err) == -1) {
272     return FALSE;
273   }
274
275   if (ringbuf_init_libpcap_fdopen(err) == NULL) {
276     return FALSE;
277   }
278
279   /* switch to the new file */
280   *save_file = next_rfile->name;
281   *save_file_fd = rb_data.fd;
282   (*pdh) = rb_data.pdh;
283
284   return TRUE;
285 }
286
287 /*
288  * Calls fclose() for the current ringbuffer file
289  */
290 gboolean
291 ringbuf_libpcap_dump_close(gchar **save_file, int *err)
292 {
293   gboolean  ret_val = TRUE;
294
295   /* close current file, if it's open */
296   if (rb_data.pdh != NULL) {
297     if (fclose(rb_data.pdh) == EOF) {
298       if (err != NULL) {
299         *err = errno;
300       }
301       ws_close(rb_data.fd);
302       ret_val = FALSE;
303     }
304     rb_data.pdh = NULL;
305     rb_data.fd  = -1;
306   }
307
308   /* set the save file name to the current file */
309   *save_file = rb_data.files[rb_data.curr_file_num % rb_data.num_files].name;
310   return ret_val;
311 }
312
313 /*
314  * Frees all memory allocated by the ringbuffer
315  */
316 void
317 ringbuf_free(void)
318 {
319   unsigned int i;
320
321   if (rb_data.files != NULL) {
322     for (i=0; i < rb_data.num_files; i++) {
323       if (rb_data.files[i].name != NULL) {
324         g_free(rb_data.files[i].name);
325         rb_data.files[i].name = NULL;
326       }
327     }
328     g_free(rb_data.files);
329     rb_data.files = NULL;
330   }
331   if (rb_data.fprefix != NULL) {
332     g_free(rb_data.fprefix);
333     rb_data.fprefix = NULL;
334   }
335   if (rb_data.fsuffix != NULL) {
336     g_free(rb_data.fsuffix);
337     rb_data.fsuffix = NULL;
338   }
339 }
340
341 /*
342  * Frees all memory allocated by the ringbuffer
343  */
344 void
345 ringbuf_error_cleanup(void)
346 {
347   unsigned int i;
348
349   /* try to close via wtap */
350   if (rb_data.pdh != NULL) {
351     if (fclose(rb_data.pdh) == 0) {
352       rb_data.fd = -1;
353     }
354     rb_data.pdh = NULL;
355   }
356
357   /* close directly if still open */
358   if (rb_data.fd != -1) {
359     ws_close(rb_data.fd);
360     rb_data.fd = -1;
361   }
362
363   if (rb_data.files != NULL) {
364     for (i=0; i < rb_data.num_files; i++) {
365       if (rb_data.files[i].name != NULL) {
366         ws_unlink(rb_data.files[i].name);
367       }
368     }
369   }
370   /* free the memory */
371   ringbuf_free();
372 }
373
374 #endif /* HAVE_LIBPCAP */
375
376 /*
377  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
378  *
379  * Local Variables:
380  * c-basic-offset: 2
381  * tab-width: 8
382  * indent-tabs-mode: nil
383  * End:
384  *
385  * ex: set shiftwidth=2 tabstop=8 expandtab:
386  * :indentSize=2:tabSize=8:noTabs=true:
387  */