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