1 /* capture_stop_conditions.c
2 * Implementation for 'stop condition handler'.
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * SPDX-License-Identifier: GPL-2.0+
17 #include "conditions.h"
18 #include "capture_stop_conditions.h"
19 #include "ws_attributes.h"
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*);
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*);
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*);
37 void init_capture_stop_conditions(void){
38 cnd_register_class(CND_CLASS_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,
53 } /* END init_capture_stop_conditions() */
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() */
61 /*****************************************************************************/
62 /* Predefined condition 'timeout'. */
65 const char* CND_CLASS_TIMEOUT = "cnd_class_timeout";
67 /* structure that contains user supplied data for this condition */
68 typedef struct _cnd_timeout_dat{
74 * Constructs new condition for timeout check. This function is invoked by
75 * 'cnd_new()' in order to perform class specific initialization.
77 * parameter: cnd - Pointer to condition passed by 'cnd_new()'.
78 * ap - Pointer to user supplied arguments list for this
80 * returns: Pointer to condition - Construction was successful.
81 * NULL - Construction failed.
83 static condition* _cnd_constr_timeout(condition* cnd, va_list ap){
84 cnd_timeout_dat *data = NULL;
86 if((data = (cnd_timeout_dat*)g_malloc(sizeof(cnd_timeout_dat))) == 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);
93 } /* END _cnd_constr_timeout() */
96 * Destroys condition for timeout check. This function is invoked by
97 * 'cnd_delete()' in order to perform class specific clean up.
99 * parameter: cnd - Pointer to condition passed by 'cnd_delete()'.
101 static void _cnd_destr_timeout(condition* cnd){
103 g_free(cnd_get_user_data(cnd));
104 } /* END _cnd_destr_timeout() */
107 * Condition handler for timeout condition. This function is invoked by
108 * 'cnd_eval()' in order to perform class specific condition checks.
110 * parameter: cnd - The inititalized timeout condition.
111 * ap - Pointer to user supplied arguments list for this
113 * returns: TRUE - Condition is true.
114 * FALSE - Condition is false.
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);
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;
124 } /* END _cnd_eval_timeout()*/
127 * Call this function to reset this condition to its initial state, i.e. the
128 * state it was in right after creation.
130 * parameter: cnd - Pointer to an initialized condition.
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() */
137 /*****************************************************************************/
138 /* Predefined condition 'max. capturesize'. */
141 const char* CND_CLASS_CAPTURESIZE = "cnd_class_capturesize";
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;
149 * Constructs new condition for capturesize check. This function is invoked by
150 * 'cnd_new()' in order to perform class specific initialization.
152 * parameter: cnd - Pointer to condition passed by 'cnd_new()'.
153 * ap - Pointer to user supplied arguments list for this
155 * returns: Pointer to condition - Construction was successful.
156 * NULL - Construction failed.
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)
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);
169 } /* END _cnd_constr_capturesize() */
172 * Destroys condition for capturesize check. This function is invoked by
173 * 'cnd_delete()' in order to perform class specific clean up.
175 * parameter: cnd - Pointer to condition passed by 'cnd_delete()'.
177 static void _cnd_destr_capturesize(condition* cnd){
179 g_free(cnd_get_user_data(cnd));
180 } /* END _cnd_destr_capturesize() */
183 * Condition handler for capturesize condition. This function is invoked by
184 * 'cnd_eval()' in order to perform class specific condition checks.
186 * parameter: cnd - The inititalized capturesize condition.
187 * ap - Pointer to user supplied arguments list for this
189 * returns: TRUE - Condition is true.
190 * FALSE - Condition is false.
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){
200 } /* END _cnd_eval_capturesize() */
203 * Call this function to reset this condition to its initial state, i.e. the
204 * state it was in right after creation.
206 * parameter: cnd - Pointer to an initialized condition.
208 static void _cnd_reset_capturesize(condition *cnd _U_){
209 } /* END _cnd_reset_capturesize() */
212 /*****************************************************************************/
213 /* Predefined condition 'interval'. */
216 const char* CND_CLASS_INTERVAL = "cnd_class_interval";
218 /* structure that contains user supplied data for this condition */
219 typedef struct _cnd_interval_dat{
225 * Constructs new condition for interval check. This function is invoked by
226 * 'cnd_new()' in order to perform class specific initialization.
228 * parameter: cnd - Pointer to condition passed by 'cnd_new()'.
229 * ap - Pointer to user supplied arguments list for this
231 * returns: Pointer to condition - Construction was successful.
232 * NULL - Construction failed.
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)
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);
245 } /* END _cnd_constr_interval() */
248 * Destroys condition for interval check. This function is invoked by
249 * 'cnd_delete()' in order to perform class specific clean up.
251 * parameter: cnd - Pointer to condition passed by 'cnd_delete()'.
253 static void _cnd_destr_interval(condition* cnd){
255 g_free(cnd_get_user_data(cnd));
256 } /* END _cnd_destr_interval() */
259 * Condition handler for interval condition. This function is invoked by
260 * 'cnd_eval()' in order to perform class specific condition checks.
262 * parameter: cnd - The inititalized interval condition.
263 * ap - Pointer to user supplied arguments list for this
265 * returns: TRUE - Condition is true.
266 * FALSE - Condition is false.
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);
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;
276 } /* END _cnd_eval_interval()*/
279 * Call this function to reset this condition to its initial state, i.e. the
280 * state it was in right after creation.
282 * parameter: cnd - Pointer to an initialized condition.
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() */
293 * Editor modelines - http://www.wireshark.org/tools/modelines.html
298 * indent-tabs-mode: nil
301 * ex: set shiftwidth=2 tabstop=8 expandtab:
302 * :indentSize=2:tabSize=8:noTabs=true: