1 /* capture_stop_conditions.c
2 * Implementation for 'stop condition handler'.
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 #include "conditions.h"
35 #include "capture_stop_conditions.h"
37 /* predefined classes function prototypes */
38 static condition* _cnd_constr_timeout(condition*, va_list);
39 static void _cnd_destr_timeout(condition*);
40 static gboolean _cnd_eval_timeout(condition*, va_list);
41 static void _cnd_reset_timeout(condition*);
43 static condition* _cnd_constr_capturesize(condition*, va_list);
44 static void _cnd_destr_capturesize(condition*);
45 static gboolean _cnd_eval_capturesize(condition*, va_list);
46 static void _cnd_reset_capturesize(condition*);
48 void init_capture_stop_conditions(void){
49 cnd_register_class(CND_CLASS_TIMEOUT,
54 cnd_register_class(CND_CLASS_CAPTURESIZE,
55 _cnd_constr_capturesize,
56 _cnd_destr_capturesize,
57 _cnd_eval_capturesize,
58 _cnd_reset_capturesize);
59 } /* END init_capture_stop_conditions() */
61 void cleanup_capture_stop_conditions(void){
62 cnd_unregister_class(CND_CLASS_TIMEOUT);
63 cnd_unregister_class(CND_CLASS_CAPTURESIZE);
64 } /* END cleanup_capture_stop_conditions() */
66 /*****************************************************************************/
67 /* Predefined condition 'timeout'. */
70 const char* CND_CLASS_TIMEOUT = "cnd_class_timeout";
72 /* structure that contains user supplied data for this condition */
73 typedef struct _cnd_timeout_dat{
79 * Constructs new condition for timeout check. This function is invoked by
80 * 'cnd_new()' in order to perform class specific initialization.
82 * parameter: cnd - Pointer to condition passed by 'cnd_new()'.
83 * ap - Pointer to user supplied arguments list for this
85 * returns: Pointer to condition - Construction was successful.
86 * NULL - Construction failed.
88 static condition* _cnd_constr_timeout(condition* cnd, va_list ap){
89 cnd_timeout_dat *data = NULL;
91 if((data = (cnd_timeout_dat*)g_malloc(sizeof(cnd_timeout_dat))) == NULL)
93 /* initialize user data */
94 data->start_time = time(NULL);
95 data->timeout_s = va_arg(ap, gint32);
96 cnd_set_user_data(cnd, (void*)data);
98 } /* END _cnd_constr_timeout() */
101 * Destroys condition for timeout check. This function is invoked by
102 * 'cnd_delete()' in order to perform class specific clean up.
104 * parameter: cnd - Pointer to condition passed by 'cnd_delete()'.
106 static void _cnd_destr_timeout(condition* cnd){
108 g_free(cnd_get_user_data(cnd));
109 } /* END _cnd_destr_timeout() */
112 * Condition handler for timeout condition. This function is invoked by
113 * 'cnd_eval()' in order to perform class specific condition checks.
115 * parameter: cnd - The inititalized timeout condition.
116 * ap - Pointer to user supplied arguments list for this
118 * returns: TRUE - Condition is true.
119 * FALSE - Condition is false.
121 static gboolean _cnd_eval_timeout(condition* cnd, va_list ap _U_){
122 cnd_timeout_dat* data = (cnd_timeout_dat*)cnd_get_user_data(cnd);
124 /* check timeout here */
125 if(data->timeout_s == 0) return FALSE; /* 0 == infinite */
126 elapsed_time = (gint32) (time(NULL) - data->start_time);
127 if(elapsed_time >= data->timeout_s) return TRUE;
129 } /* END _cnd_eval_timeout()*/
132 * Call this function to reset this condition to its initial state, i.e. the
133 * state it was in right after creation.
135 * parameter: cnd - Pointer to an initialized condition.
137 static void _cnd_reset_timeout(condition *cnd){
138 ((cnd_timeout_dat*)cnd_get_user_data(cnd))->start_time = time(NULL);
139 } /* END _cnd_reset_timeout() */
142 /*****************************************************************************/
143 /* Predefined condition 'max. capturesize'. */
146 const char* CND_CLASS_CAPTURESIZE = "cnd_class_capturesize";
148 /* structure that contains user supplied data for this condition */
149 typedef struct _cnd_capturesize_dat{
150 long max_capture_size;
151 }cnd_capturesize_dat;
154 * Constructs new condition for capturesize check. This function is invoked by
155 * 'cnd_new()' in order to perform class specific initialization.
157 * parameter: cnd - Pointer to condition passed by 'cnd_new()'.
158 * ap - Pointer to user supplied arguments list for this
160 * returns: Pointer to condition - Construction was successful.
161 * NULL - Construction failed.
163 static condition* _cnd_constr_capturesize(condition* cnd, va_list ap){
164 cnd_capturesize_dat *data = NULL;
165 /* allocate memory */
166 if((data = (cnd_capturesize_dat*)g_malloc(sizeof(cnd_capturesize_dat))) == NULL)
168 /* initialize user data */
169 data->max_capture_size = va_arg(ap, long);
170 cnd_set_user_data(cnd, (void*)data);
172 } /* END _cnd_constr_capturesize() */
175 * Destroys condition for capturesize check. This function is invoked by
176 * 'cnd_delete()' in order to perform class specific clean up.
178 * parameter: cnd - Pointer to condition passed by 'cnd_delete()'.
180 static void _cnd_destr_capturesize(condition* cnd){
182 g_free(cnd_get_user_data(cnd));
183 } /* END _cnd_destr_capturesize() */
186 * Condition handler for capturesize condition. This function is invoked by
187 * 'cnd_eval()' in order to perform class specific condition checks.
189 * parameter: cnd - The inititalized capturesize condition.
190 * ap - Pointer to user supplied arguments list for this
192 * returns: TRUE - Condition is true.
193 * FALSE - Condition is false.
195 static gboolean _cnd_eval_capturesize(condition* cnd, va_list ap){
196 cnd_capturesize_dat* data = (cnd_capturesize_dat*)cnd_get_user_data(cnd);
197 /* check capturesize here */
198 if(data->max_capture_size == 0) return FALSE; /* 0 == infinite */
199 if(va_arg(ap, long) >= data->max_capture_size){
203 } /* END _cnd_eval_capturesize() */
206 * Call this function to reset this condition to its initial state, i.e. the
207 * state it was in right after creation.
209 * parameter: cnd - Pointer to an initialized condition.
211 static void _cnd_reset_capturesize(condition *cnd _U_){
212 } /* END _cnd_reset_capturesize() */