QUIC: fix null-ptr dereference in gQUIC version check
[metze/wireshark/wip.git] / capture_stop_conditions.c
1 /* capture_stop_conditions.c
2  * Implementation for 'stop condition handler'.
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 #include <config.h>
12
13 #include <time.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include <stdarg.h>
17 #include "conditions.h"
18 #include "capture_stop_conditions.h"
19 #include "ws_attributes.h"
20
21 /* predefined classes function prototypes */
22 static condition* _cnd_constr_timeout(condition*, va_list);
23 static void _cnd_destr_timeout(condition*);
24 static gboolean _cnd_eval_timeout(condition*, va_list);
25 static void _cnd_reset_timeout(condition*);
26
27 static condition* _cnd_constr_capturesize(condition*, va_list);
28 static void _cnd_destr_capturesize(condition*);
29 static gboolean _cnd_eval_capturesize(condition*, va_list);
30 static void _cnd_reset_capturesize(condition*);
31
32 static condition* _cnd_constr_interval(condition*, va_list);
33 static void _cnd_destr_interval(condition*);
34 static gboolean _cnd_eval_interval(condition*, va_list);
35 static void _cnd_reset_interval(condition*);
36
37 void init_capture_stop_conditions(void){
38   cnd_register_class(CND_CLASS_TIMEOUT,
39                      _cnd_constr_timeout,
40                      _cnd_destr_timeout,
41                      _cnd_eval_timeout,
42                      _cnd_reset_timeout);
43   cnd_register_class(CND_CLASS_CAPTURESIZE,
44                      _cnd_constr_capturesize,
45                      _cnd_destr_capturesize,
46                      _cnd_eval_capturesize,
47                      _cnd_reset_capturesize);
48   cnd_register_class(CND_CLASS_INTERVAL,
49                      _cnd_constr_interval,
50                      _cnd_destr_interval,
51                      _cnd_eval_interval,
52                      _cnd_reset_interval);
53 } /* END init_capture_stop_conditions() */
54
55 void cleanup_capture_stop_conditions(void){
56   cnd_unregister_class(CND_CLASS_TIMEOUT);
57   cnd_unregister_class(CND_CLASS_CAPTURESIZE);
58   cnd_unregister_class(CND_CLASS_INTERVAL);
59 } /* END cleanup_capture_stop_conditions() */
60
61 /*****************************************************************************/
62 /* Predefined condition 'timeout'.                                           */
63
64 /* class id */
65 const char* CND_CLASS_TIMEOUT = "cnd_class_timeout";
66
67 /* structure that contains user supplied data for this condition */
68 typedef struct _cnd_timeout_dat{
69   time_t start_time;
70   gint32 timeout_s;
71 }cnd_timeout_dat;
72
73 /*
74  * Constructs new condition for timeout check. This function is invoked by
75  * 'cnd_new()' in order to perform class specific initialization.
76  *
77  * parameter: cnd - Pointer to condition passed by 'cnd_new()'.
78  *            ap  - Pointer to user supplied arguments list for this
79  *                  constructor.
80  * returns:   Pointer to condition - Construction was successful.
81  *            NULL                 - Construction failed.
82  */
83 static condition* _cnd_constr_timeout(condition* cnd, va_list ap){
84   cnd_timeout_dat *data = NULL;
85   /* allocate memory */
86   if((data = (cnd_timeout_dat*)g_malloc(sizeof(cnd_timeout_dat))) == NULL)
87     return NULL;
88   /* initialize user data */
89   data->start_time = time(NULL);
90   data->timeout_s = va_arg(ap, gint32);
91   cnd_set_user_data(cnd, (void*)data);
92   return cnd;
93 } /* END _cnd_constr_timeout() */
94
95 /*
96  * Destroys condition for timeout check. This function is invoked by
97  * 'cnd_delete()' in order to perform class specific clean up.
98  *
99  * parameter: cnd - Pointer to condition passed by 'cnd_delete()'.
100  */
101 static void _cnd_destr_timeout(condition* cnd){
102   /* free memory */
103   g_free(cnd_get_user_data(cnd));
104 } /* END _cnd_destr_timeout() */
105
106 /*
107  * Condition handler for timeout condition. This function is invoked by
108  * 'cnd_eval()' in order to perform class specific condition checks.
109  *
110  * parameter: cnd - The inititalized timeout condition.
111  *            ap  - Pointer to user supplied arguments list for this
112  *                  handler.
113  * returns:   TRUE  - Condition is true.
114  *            FALSE - Condition is false.
115  */
116 static gboolean _cnd_eval_timeout(condition* cnd, va_list ap _U_){
117   cnd_timeout_dat* data = (cnd_timeout_dat*)cnd_get_user_data(cnd);
118   gint32 elapsed_time;
119   /* check timeout here */
120   if(data->timeout_s == 0) return FALSE; /* 0 == infinite */
121   elapsed_time = (gint32) (time(NULL) - data->start_time);
122   if(elapsed_time >= data->timeout_s) return TRUE;
123   return FALSE;
124 } /* END _cnd_eval_timeout()*/
125
126 /*
127  * Call this function to reset this condition to its initial state, i.e. the
128  * state it was in right after creation.
129  *
130  * parameter: cnd - Pointer to an initialized condition.
131  */
132 static void _cnd_reset_timeout(condition *cnd){
133   ((cnd_timeout_dat*)cnd_get_user_data(cnd))->start_time = time(NULL);
134 } /* END _cnd_reset_timeout() */
135
136
137 /*****************************************************************************/
138 /* Predefined condition 'max. capturesize'.                                  */
139
140 /* class id */
141 const char* CND_CLASS_CAPTURESIZE = "cnd_class_capturesize";
142
143 /* structure that contains user supplied data for this condition */
144 typedef struct _cnd_capturesize_dat{
145   guint64 max_capture_size;
146 }cnd_capturesize_dat;
147
148 /*
149  * Constructs new condition for capturesize check. This function is invoked by
150  * 'cnd_new()' in order to perform class specific initialization.
151  *
152  * parameter: cnd - Pointer to condition passed by 'cnd_new()'.
153  *            ap  - Pointer to user supplied arguments list for this
154  *                  constructor.
155  * returns:   Pointer to condition - Construction was successful.
156  *            NULL                 - Construction failed.
157  */
158 static condition* _cnd_constr_capturesize(condition* cnd, va_list ap){
159   cnd_capturesize_dat *data = NULL;
160   /* allocate memory */
161   if((data = (cnd_capturesize_dat*)g_malloc(sizeof(cnd_capturesize_dat))) == NULL)
162     return NULL;
163   /* initialize user data */
164   data->max_capture_size = va_arg(ap, guint64);
165   if (data->max_capture_size > ((guint64)INT_MAX + 1))
166     data->max_capture_size = (guint64)INT_MAX + 1;
167   cnd_set_user_data(cnd, (void*)data);
168   return cnd;
169 } /* END _cnd_constr_capturesize() */
170
171 /*
172  * Destroys condition for capturesize check. This function is invoked by
173  * 'cnd_delete()' in order to perform class specific clean up.
174  *
175  * parameter: cnd - Pointer to condition passed by 'cnd_delete()'.
176  */
177 static void _cnd_destr_capturesize(condition* cnd){
178   /* free memory */
179   g_free(cnd_get_user_data(cnd));
180 } /* END _cnd_destr_capturesize() */
181
182 /*
183  * Condition handler for capturesize condition. This function is invoked by
184  * 'cnd_eval()' in order to perform class specific condition checks.
185  *
186  * parameter: cnd - The inititalized capturesize condition.
187  *            ap  - Pointer to user supplied arguments list for this
188  *                  handler.
189  * returns:   TRUE  - Condition is true.
190  *            FALSE - Condition is false.
191  */
192 static gboolean _cnd_eval_capturesize(condition* cnd, va_list ap){
193   cnd_capturesize_dat* data = (cnd_capturesize_dat*)cnd_get_user_data(cnd);
194   /* check capturesize here */
195   if(data->max_capture_size == 0) return FALSE; /* 0 == infinite */
196   if(va_arg(ap, guint64) >= data->max_capture_size){
197     return TRUE;
198   }
199   return FALSE;
200 } /* END _cnd_eval_capturesize() */
201
202 /*
203  * Call this function to reset this condition to its initial state, i.e. the
204  * state it was in right after creation.
205  *
206  * parameter: cnd - Pointer to an initialized condition.
207  */
208 static void _cnd_reset_capturesize(condition *cnd _U_){
209 } /* END _cnd_reset_capturesize() */
210
211
212 /*****************************************************************************/
213 /* Predefined condition 'interval'.                                           */
214
215 /* class id */
216 const char* CND_CLASS_INTERVAL = "cnd_class_interval";
217
218 /* structure that contains user supplied data for this condition */
219 typedef struct _cnd_interval_dat{
220   time_t start_time;
221   gint32 interval_s;
222 }cnd_interval_dat;
223
224 /*
225  * Constructs new condition for interval check. This function is invoked by
226  * 'cnd_new()' in order to perform class specific initialization.
227  *
228  * parameter: cnd - Pointer to condition passed by 'cnd_new()'.
229  *            ap  - Pointer to user supplied arguments list for this
230  *                  constructor.
231  * returns:   Pointer to condition - Construction was successful.
232  *            NULL                 - Construction failed.
233  */
234 static condition* _cnd_constr_interval(condition* cnd, va_list ap){
235   cnd_interval_dat *data = NULL;
236   /* allocate memory */
237   if((data = (cnd_interval_dat*)g_malloc(sizeof(cnd_interval_dat))) == NULL)
238     return NULL;
239   /* initialize user data */
240   data->start_time = time(NULL);
241   data->interval_s = va_arg(ap, gint32);
242   data->start_time -= data->start_time % data->interval_s;
243   cnd_set_user_data(cnd, (void*)data);
244   return cnd;
245 } /* END _cnd_constr_interval() */
246
247 /*
248  * Destroys condition for interval check. This function is invoked by
249  * 'cnd_delete()' in order to perform class specific clean up.
250  *
251  * parameter: cnd - Pointer to condition passed by 'cnd_delete()'.
252  */
253 static void _cnd_destr_interval(condition* cnd){
254   /* free memory */
255   g_free(cnd_get_user_data(cnd));
256 } /* END _cnd_destr_interval() */
257
258 /*
259  * Condition handler for interval condition. This function is invoked by
260  * 'cnd_eval()' in order to perform class specific condition checks.
261  *
262  * parameter: cnd - The inititalized interval condition.
263  *            ap  - Pointer to user supplied arguments list for this
264  *                  handler.
265  * returns:   TRUE  - Condition is true.
266  *            FALSE - Condition is false.
267  */
268 static gboolean _cnd_eval_interval(condition* cnd, va_list ap _U_){
269   cnd_interval_dat* data = (cnd_interval_dat*)cnd_get_user_data(cnd);
270   gint32 elapsed_time;
271   /* check interval here */
272   if(data->interval_s == 0) return FALSE; /* 0 == infinite */
273   elapsed_time = (gint32) (time(NULL) - data->start_time);
274   if(elapsed_time >= data->interval_s) return TRUE;
275   return FALSE;
276 } /* END _cnd_eval_interval()*/
277
278 /*
279  * Call this function to reset this condition to its initial state, i.e. the
280  * state it was in right after creation.
281  *
282  * parameter: cnd - Pointer to an initialized condition.
283  */
284 static void _cnd_reset_interval(condition *cnd){
285   ((cnd_interval_dat*)cnd_get_user_data(cnd))->start_time = time(NULL);
286   ((cnd_interval_dat*)cnd_get_user_data(cnd))->start_time -=
287       ((cnd_interval_dat*)cnd_get_user_data(cnd))->start_time % ((cnd_interval_dat*)cnd_get_user_data(cnd))->interval_s;
288 } /* END _cnd_reset_interval() */
289
290
291
292 /*
293  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
294  *
295  * Local Variables:
296  * c-basic-offset: 2
297  * tab-width: 8
298  * indent-tabs-mode: nil
299  * End:
300  *
301  * ex: set shiftwidth=2 tabstop=8 expandtab:
302  * :indentSize=2:tabSize=8:noTabs=true:
303  */