1 /* capture_stop_conditions.c
2 * Implementation for 'stop condition handler'.
4 * $Id: capture_stop_conditions.c,v 1.4 2002/08/28 21:00:05 jmayer Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
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.
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*)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 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 = 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 long 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*)malloc(sizeof(cnd_capturesize_dat))) == NULL)
164 /* initialize user data */
165 data->max_capture_size = va_arg(ap, long);
166 cnd_set_user_data(cnd, (void*)data);
168 } /* END _cnd_constr_capturesize() */
171 * Destroys condition for capturesize check. This function is invoked by
172 * 'cnd_delete()' in order to perform class specific clean up.
174 * parameter: cnd - Pointer to condition passed by 'cnd_delete()'.
176 static void _cnd_destr_capturesize(condition* cnd){
178 free(cnd_get_user_data(cnd));
179 } /* END _cnd_destr_capturesize() */
182 * Condition handler for capturesize condition. This function is invoked by
183 * 'cnd_eval()' in order to perform class specific condition checks.
185 * parameter: cnd - The inititalized capturesize condition.
186 * ap - Pointer to user supplied arguments list for this
188 * returns: TRUE - Condition is true.
189 * FALSE - Condition is false.
191 static gboolean _cnd_eval_capturesize(condition* cnd, va_list ap){
192 cnd_capturesize_dat* data = (cnd_capturesize_dat*)cnd_get_user_data(cnd);
193 /* check capturesize here */
194 if(data->max_capture_size == 0) return FALSE; /* 0 == infinite */
195 if(va_arg(ap, long) >= data->max_capture_size){
199 } /* END _cnd_eval_capturesize() */
202 * Call this function to reset this condition to its initial state, i.e. the
203 * state it was in right after creation.
205 * parameter: cnd - Pointer to an initialized condition.
207 static void _cnd_reset_capturesize(condition *cnd _U_){
208 } /* END _cnd_reset_capturesize() */