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.
30 #include "conditions.h"
31 #include "capture_stop_conditions.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 void init_capture_stop_conditions(void){
45 cnd_register_class(CND_CLASS_TIMEOUT,
50 cnd_register_class(CND_CLASS_CAPTURESIZE,
51 _cnd_constr_capturesize,
52 _cnd_destr_capturesize,
53 _cnd_eval_capturesize,
54 _cnd_reset_capturesize);
55 } /* END init_capture_stop_conditions() */
57 void cleanup_capture_stop_conditions(void){
58 cnd_unregister_class(CND_CLASS_TIMEOUT);
59 cnd_unregister_class(CND_CLASS_CAPTURESIZE);
60 } /* END cleanup_capture_stop_conditions() */
62 /*****************************************************************************/
63 /* Predefined condition 'timeout'. */
66 const char* CND_CLASS_TIMEOUT = "cnd_class_timeout";
68 /* structure that contains user supplied data for this condition */
69 typedef struct _cnd_timeout_dat{
75 * Constructs new condition for timeout check. This function is invoked by
76 * 'cnd_new()' in order to perform class specific initialization.
78 * parameter: cnd - Pointer to condition passed by 'cnd_new()'.
79 * ap - Pointer to user supplied arguments list for this
81 * returns: Pointer to condition - Construction was successful.
82 * NULL - Construction failed.
84 static condition* _cnd_constr_timeout(condition* cnd, va_list ap){
85 cnd_timeout_dat *data = NULL;
87 if((data = (cnd_timeout_dat*)g_malloc(sizeof(cnd_timeout_dat))) == NULL)
89 /* initialize user data */
90 data->start_time = time(NULL);
91 data->timeout_s = va_arg(ap, gint32);
92 cnd_set_user_data(cnd, (void*)data);
94 } /* END _cnd_constr_timeout() */
97 * Destroys condition for timeout check. This function is invoked by
98 * 'cnd_delete()' in order to perform class specific clean up.
100 * parameter: cnd - Pointer to condition passed by 'cnd_delete()'.
102 static void _cnd_destr_timeout(condition* cnd){
104 g_free(cnd_get_user_data(cnd));
105 } /* END _cnd_destr_timeout() */
108 * Condition handler for timeout condition. This function is invoked by
109 * 'cnd_eval()' in order to perform class specific condition checks.
111 * parameter: cnd - The inititalized timeout condition.
112 * ap - Pointer to user supplied arguments list for this
114 * returns: TRUE - Condition is true.
115 * FALSE - Condition is false.
117 static gboolean _cnd_eval_timeout(condition* cnd, va_list ap _U_){
118 cnd_timeout_dat* data = (cnd_timeout_dat*)cnd_get_user_data(cnd);
120 /* check timeout here */
121 if(data->timeout_s == 0) return FALSE; /* 0 == infinite */
122 elapsed_time = (gint32) (time(NULL) - data->start_time);
123 if(elapsed_time >= data->timeout_s) return TRUE;
125 } /* END _cnd_eval_timeout()*/
128 * Call this function to reset this condition to its initial state, i.e. the
129 * state it was in right after creation.
131 * parameter: cnd - Pointer to an initialized condition.
133 static void _cnd_reset_timeout(condition *cnd){
134 ((cnd_timeout_dat*)cnd_get_user_data(cnd))->start_time = time(NULL);
135 } /* END _cnd_reset_timeout() */
138 /*****************************************************************************/
139 /* Predefined condition 'max. capturesize'. */
142 const char* CND_CLASS_CAPTURESIZE = "cnd_class_capturesize";
144 /* structure that contains user supplied data for this condition */
145 typedef struct _cnd_capturesize_dat{
146 guint64 max_capture_size;
147 }cnd_capturesize_dat;
150 * Constructs new condition for capturesize check. This function is invoked by
151 * 'cnd_new()' in order to perform class specific initialization.
153 * parameter: cnd - Pointer to condition passed by 'cnd_new()'.
154 * ap - Pointer to user supplied arguments list for this
156 * returns: Pointer to condition - Construction was successful.
157 * NULL - Construction failed.
159 static condition* _cnd_constr_capturesize(condition* cnd, va_list ap){
160 cnd_capturesize_dat *data = NULL;
161 /* allocate memory */
162 if((data = (cnd_capturesize_dat*)g_malloc(sizeof(cnd_capturesize_dat))) == NULL)
164 /* initialize user data */
165 data->max_capture_size = va_arg(ap, guint64);
166 if (data->max_capture_size > ((guint64)INT_MAX + 1))
167 data->max_capture_size = (guint64)INT_MAX + 1;
168 cnd_set_user_data(cnd, (void*)data);
170 } /* END _cnd_constr_capturesize() */
173 * Destroys condition for capturesize check. This function is invoked by
174 * 'cnd_delete()' in order to perform class specific clean up.
176 * parameter: cnd - Pointer to condition passed by 'cnd_delete()'.
178 static void _cnd_destr_capturesize(condition* cnd){
180 g_free(cnd_get_user_data(cnd));
181 } /* END _cnd_destr_capturesize() */
184 * Condition handler for capturesize condition. This function is invoked by
185 * 'cnd_eval()' in order to perform class specific condition checks.
187 * parameter: cnd - The inititalized capturesize condition.
188 * ap - Pointer to user supplied arguments list for this
190 * returns: TRUE - Condition is true.
191 * FALSE - Condition is false.
193 static gboolean _cnd_eval_capturesize(condition* cnd, va_list ap){
194 cnd_capturesize_dat* data = (cnd_capturesize_dat*)cnd_get_user_data(cnd);
195 /* check capturesize here */
196 if(data->max_capture_size == 0) return FALSE; /* 0 == infinite */
197 if(va_arg(ap, guint64) >= data->max_capture_size){
201 } /* END _cnd_eval_capturesize() */
204 * Call this function to reset this condition to its initial state, i.e. the
205 * state it was in right after creation.
207 * parameter: cnd - Pointer to an initialized condition.
209 static void _cnd_reset_capturesize(condition *cnd _U_){
210 } /* END _cnd_reset_capturesize() */
213 * Editor modelines - http://www.wireshark.org/tools/modelines.html
218 * indent-tabs-mode: nil
221 * ex: set shiftwidth=2 tabstop=8 expandtab:
222 * :indentSize=2:tabSize=8:noTabs=true: