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 * 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.
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.
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.
29 #include "conditions.h"
30 #include "capture_stop_conditions.h"
31 #include "ws_attributes.h"
33 /* predefined classes function prototypes */
34 static condition* _cnd_constr_timeout(condition*, va_list);
35 static void _cnd_destr_timeout(condition*);
36 static gboolean _cnd_eval_timeout(condition*, va_list);
37 static void _cnd_reset_timeout(condition*);
39 static condition* _cnd_constr_capturesize(condition*, va_list);
40 static void _cnd_destr_capturesize(condition*);
41 static gboolean _cnd_eval_capturesize(condition*, va_list);
42 static void _cnd_reset_capturesize(condition*);
44 static condition* _cnd_constr_interval(condition*, va_list);
45 static void _cnd_destr_interval(condition*);
46 static gboolean _cnd_eval_interval(condition*, va_list);
47 static void _cnd_reset_interval(condition*);
49 void init_capture_stop_conditions(void){
50 cnd_register_class(CND_CLASS_TIMEOUT,
55 cnd_register_class(CND_CLASS_CAPTURESIZE,
56 _cnd_constr_capturesize,
57 _cnd_destr_capturesize,
58 _cnd_eval_capturesize,
59 _cnd_reset_capturesize);
60 cnd_register_class(CND_CLASS_INTERVAL,
65 } /* END init_capture_stop_conditions() */
67 void cleanup_capture_stop_conditions(void){
68 cnd_unregister_class(CND_CLASS_TIMEOUT);
69 cnd_unregister_class(CND_CLASS_CAPTURESIZE);
70 cnd_unregister_class(CND_CLASS_INTERVAL);
71 } /* END cleanup_capture_stop_conditions() */
73 /*****************************************************************************/
74 /* Predefined condition 'timeout'. */
77 const char* CND_CLASS_TIMEOUT = "cnd_class_timeout";
79 /* structure that contains user supplied data for this condition */
80 typedef struct _cnd_timeout_dat{
86 * Constructs new condition for timeout check. This function is invoked by
87 * 'cnd_new()' in order to perform class specific initialization.
89 * parameter: cnd - Pointer to condition passed by 'cnd_new()'.
90 * ap - Pointer to user supplied arguments list for this
92 * returns: Pointer to condition - Construction was successful.
93 * NULL - Construction failed.
95 static condition* _cnd_constr_timeout(condition* cnd, va_list ap){
96 cnd_timeout_dat *data = NULL;
98 if((data = (cnd_timeout_dat*)g_malloc(sizeof(cnd_timeout_dat))) == NULL)
100 /* initialize user data */
101 data->start_time = time(NULL);
102 data->timeout_s = va_arg(ap, gint32);
103 cnd_set_user_data(cnd, (void*)data);
105 } /* END _cnd_constr_timeout() */
108 * Destroys condition for timeout check. This function is invoked by
109 * 'cnd_delete()' in order to perform class specific clean up.
111 * parameter: cnd - Pointer to condition passed by 'cnd_delete()'.
113 static void _cnd_destr_timeout(condition* cnd){
115 g_free(cnd_get_user_data(cnd));
116 } /* END _cnd_destr_timeout() */
119 * Condition handler for timeout condition. This function is invoked by
120 * 'cnd_eval()' in order to perform class specific condition checks.
122 * parameter: cnd - The inititalized timeout condition.
123 * ap - Pointer to user supplied arguments list for this
125 * returns: TRUE - Condition is true.
126 * FALSE - Condition is false.
128 static gboolean _cnd_eval_timeout(condition* cnd, va_list ap _U_){
129 cnd_timeout_dat* data = (cnd_timeout_dat*)cnd_get_user_data(cnd);
131 /* check timeout here */
132 if(data->timeout_s == 0) return FALSE; /* 0 == infinite */
133 elapsed_time = (gint32) (time(NULL) - data->start_time);
134 if(elapsed_time >= data->timeout_s) return TRUE;
136 } /* END _cnd_eval_timeout()*/
139 * Call this function to reset this condition to its initial state, i.e. the
140 * state it was in right after creation.
142 * parameter: cnd - Pointer to an initialized condition.
144 static void _cnd_reset_timeout(condition *cnd){
145 ((cnd_timeout_dat*)cnd_get_user_data(cnd))->start_time = time(NULL);
146 } /* END _cnd_reset_timeout() */
149 /*****************************************************************************/
150 /* Predefined condition 'max. capturesize'. */
153 const char* CND_CLASS_CAPTURESIZE = "cnd_class_capturesize";
155 /* structure that contains user supplied data for this condition */
156 typedef struct _cnd_capturesize_dat{
157 guint64 max_capture_size;
158 }cnd_capturesize_dat;
161 * Constructs new condition for capturesize check. This function is invoked by
162 * 'cnd_new()' in order to perform class specific initialization.
164 * parameter: cnd - Pointer to condition passed by 'cnd_new()'.
165 * ap - Pointer to user supplied arguments list for this
167 * returns: Pointer to condition - Construction was successful.
168 * NULL - Construction failed.
170 static condition* _cnd_constr_capturesize(condition* cnd, va_list ap){
171 cnd_capturesize_dat *data = NULL;
172 /* allocate memory */
173 if((data = (cnd_capturesize_dat*)g_malloc(sizeof(cnd_capturesize_dat))) == NULL)
175 /* initialize user data */
176 data->max_capture_size = va_arg(ap, guint64);
177 if (data->max_capture_size > ((guint64)INT_MAX + 1))
178 data->max_capture_size = (guint64)INT_MAX + 1;
179 cnd_set_user_data(cnd, (void*)data);
181 } /* END _cnd_constr_capturesize() */
184 * Destroys condition for capturesize check. This function is invoked by
185 * 'cnd_delete()' in order to perform class specific clean up.
187 * parameter: cnd - Pointer to condition passed by 'cnd_delete()'.
189 static void _cnd_destr_capturesize(condition* cnd){
191 g_free(cnd_get_user_data(cnd));
192 } /* END _cnd_destr_capturesize() */
195 * Condition handler for capturesize condition. This function is invoked by
196 * 'cnd_eval()' in order to perform class specific condition checks.
198 * parameter: cnd - The inititalized capturesize condition.
199 * ap - Pointer to user supplied arguments list for this
201 * returns: TRUE - Condition is true.
202 * FALSE - Condition is false.
204 static gboolean _cnd_eval_capturesize(condition* cnd, va_list ap){
205 cnd_capturesize_dat* data = (cnd_capturesize_dat*)cnd_get_user_data(cnd);
206 /* check capturesize here */
207 if(data->max_capture_size == 0) return FALSE; /* 0 == infinite */
208 if(va_arg(ap, guint64) >= data->max_capture_size){
212 } /* END _cnd_eval_capturesize() */
215 * Call this function to reset this condition to its initial state, i.e. the
216 * state it was in right after creation.
218 * parameter: cnd - Pointer to an initialized condition.
220 static void _cnd_reset_capturesize(condition *cnd _U_){
221 } /* END _cnd_reset_capturesize() */
224 /*****************************************************************************/
225 /* Predefined condition 'interval'. */
228 const char* CND_CLASS_INTERVAL = "cnd_class_interval";
230 /* structure that contains user supplied data for this condition */
231 typedef struct _cnd_interval_dat{
237 * Constructs new condition for interval check. This function is invoked by
238 * 'cnd_new()' in order to perform class specific initialization.
240 * parameter: cnd - Pointer to condition passed by 'cnd_new()'.
241 * ap - Pointer to user supplied arguments list for this
243 * returns: Pointer to condition - Construction was successful.
244 * NULL - Construction failed.
246 static condition* _cnd_constr_interval(condition* cnd, va_list ap){
247 cnd_interval_dat *data = NULL;
248 /* allocate memory */
249 if((data = (cnd_interval_dat*)g_malloc(sizeof(cnd_interval_dat))) == NULL)
251 /* initialize user data */
252 data->start_time = time(NULL);
253 data->interval_s = va_arg(ap, gint32);
254 data->start_time -= data->start_time % data->interval_s;
255 cnd_set_user_data(cnd, (void*)data);
257 } /* END _cnd_constr_interval() */
260 * Destroys condition for interval check. This function is invoked by
261 * 'cnd_delete()' in order to perform class specific clean up.
263 * parameter: cnd - Pointer to condition passed by 'cnd_delete()'.
265 static void _cnd_destr_interval(condition* cnd){
267 g_free(cnd_get_user_data(cnd));
268 } /* END _cnd_destr_interval() */
271 * Condition handler for interval condition. This function is invoked by
272 * 'cnd_eval()' in order to perform class specific condition checks.
274 * parameter: cnd - The inititalized interval condition.
275 * ap - Pointer to user supplied arguments list for this
277 * returns: TRUE - Condition is true.
278 * FALSE - Condition is false.
280 static gboolean _cnd_eval_interval(condition* cnd, va_list ap _U_){
281 cnd_interval_dat* data = (cnd_interval_dat*)cnd_get_user_data(cnd);
283 /* check interval here */
284 if(data->interval_s == 0) return FALSE; /* 0 == infinite */
285 elapsed_time = (gint32) (time(NULL) - data->start_time);
286 if(elapsed_time >= data->interval_s) return TRUE;
288 } /* END _cnd_eval_interval()*/
291 * Call this function to reset this condition to its initial state, i.e. the
292 * state it was in right after creation.
294 * parameter: cnd - Pointer to an initialized condition.
296 static void _cnd_reset_interval(condition *cnd){
297 ((cnd_interval_dat*)cnd_get_user_data(cnd))->start_time = time(NULL);
298 ((cnd_interval_dat*)cnd_get_user_data(cnd))->start_time -=
299 ((cnd_interval_dat*)cnd_get_user_data(cnd))->start_time % ((cnd_interval_dat*)cnd_get_user_data(cnd))->interval_s;
300 } /* END _cnd_reset_interval() */
305 * Editor modelines - http://www.wireshark.org/tools/modelines.html
310 * indent-tabs-mode: nil
313 * ex: set shiftwidth=2 tabstop=8 expandtab:
314 * :indentSize=2:tabSize=8:noTabs=true: