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"
32 /* predefined classes function prototypes */
33 static condition* _cnd_constr_timeout(condition*, va_list);
34 static void _cnd_destr_timeout(condition*);
35 static gboolean _cnd_eval_timeout(condition*, va_list);
36 static void _cnd_reset_timeout(condition*);
38 static condition* _cnd_constr_capturesize(condition*, va_list);
39 static void _cnd_destr_capturesize(condition*);
40 static gboolean _cnd_eval_capturesize(condition*, va_list);
41 static void _cnd_reset_capturesize(condition*);
43 void init_capture_stop_conditions(void){
44 cnd_register_class(CND_CLASS_TIMEOUT,
49 cnd_register_class(CND_CLASS_CAPTURESIZE,
50 _cnd_constr_capturesize,
51 _cnd_destr_capturesize,
52 _cnd_eval_capturesize,
53 _cnd_reset_capturesize);
54 } /* END init_capture_stop_conditions() */
56 void cleanup_capture_stop_conditions(void){
57 cnd_unregister_class(CND_CLASS_TIMEOUT);
58 cnd_unregister_class(CND_CLASS_CAPTURESIZE);
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 * Editor modelines - http://www.wireshark.org/tools/modelines.html
217 * indent-tabs-mode: nil
220 * ex: set shiftwidth=2 tabstop=8 expandtab:
221 * :indentSize=2:tabSize=8:noTabs=true: