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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #include "conditions.h"
33 #include "capture_stop_conditions.h"
35 /* predefined classes function prototypes */
36 static condition* _cnd_constr_timeout(condition*, va_list);
37 static void _cnd_destr_timeout(condition*);
38 static gboolean _cnd_eval_timeout(condition*, va_list);
39 static void _cnd_reset_timeout(condition*);
41 static condition* _cnd_constr_capturesize(condition*, va_list);
42 static void _cnd_destr_capturesize(condition*);
43 static gboolean _cnd_eval_capturesize(condition*, va_list);
44 static void _cnd_reset_capturesize(condition*);
46 void init_capture_stop_conditions(void){
47 cnd_register_class(CND_CLASS_TIMEOUT,
52 cnd_register_class(CND_CLASS_CAPTURESIZE,
53 _cnd_constr_capturesize,
54 _cnd_destr_capturesize,
55 _cnd_eval_capturesize,
56 _cnd_reset_capturesize);
57 } /* END init_capture_stop_conditions() */
59 void cleanup_capture_stop_conditions(void){
60 cnd_unregister_class(CND_CLASS_TIMEOUT);
61 cnd_unregister_class(CND_CLASS_CAPTURESIZE);
62 } /* END cleanup_capture_stop_conditions() */
64 /*****************************************************************************/
65 /* Predefined condition 'timeout'. */
68 const char* CND_CLASS_TIMEOUT = "cnd_class_timeout";
70 /* structure that contains user supplied data for this condition */
71 typedef struct _cnd_timeout_dat{
77 * Constructs new condition for timeout check. This function is invoked by
78 * 'cnd_new()' in order to perform class specific initialization.
80 * parameter: cnd - Pointer to condition passed by 'cnd_new()'.
81 * ap - Pointer to user supplied arguments list for this
83 * returns: Pointer to condition - Construction was successful.
84 * NULL - Construction failed.
86 static condition* _cnd_constr_timeout(condition* cnd, va_list ap){
87 cnd_timeout_dat *data = NULL;
89 if((data = (cnd_timeout_dat*)g_malloc(sizeof(cnd_timeout_dat))) == NULL)
91 /* initialize user data */
92 data->start_time = time(NULL);
93 data->timeout_s = va_arg(ap, gint32);
94 cnd_set_user_data(cnd, (void*)data);
96 } /* END _cnd_constr_timeout() */
99 * Destroys condition for timeout check. This function is invoked by
100 * 'cnd_delete()' in order to perform class specific clean up.
102 * parameter: cnd - Pointer to condition passed by 'cnd_delete()'.
104 static void _cnd_destr_timeout(condition* cnd){
106 g_free(cnd_get_user_data(cnd));
107 } /* END _cnd_destr_timeout() */
110 * Condition handler for timeout condition. This function is invoked by
111 * 'cnd_eval()' in order to perform class specific condition checks.
113 * parameter: cnd - The inititalized timeout condition.
114 * ap - Pointer to user supplied arguments list for this
116 * returns: TRUE - Condition is true.
117 * FALSE - Condition is false.
119 static gboolean _cnd_eval_timeout(condition* cnd, va_list ap _U_){
120 cnd_timeout_dat* data = (cnd_timeout_dat*)cnd_get_user_data(cnd);
122 /* check timeout here */
123 if(data->timeout_s == 0) return FALSE; /* 0 == infinite */
124 elapsed_time = (gint32) (time(NULL) - data->start_time);
125 if(elapsed_time >= data->timeout_s) return TRUE;
127 } /* END _cnd_eval_timeout()*/
130 * Call this function to reset this condition to its initial state, i.e. the
131 * state it was in right after creation.
133 * parameter: cnd - Pointer to an initialized condition.
135 static void _cnd_reset_timeout(condition *cnd){
136 ((cnd_timeout_dat*)cnd_get_user_data(cnd))->start_time = time(NULL);
137 } /* END _cnd_reset_timeout() */
140 /*****************************************************************************/
141 /* Predefined condition 'max. capturesize'. */
144 const char* CND_CLASS_CAPTURESIZE = "cnd_class_capturesize";
146 /* structure that contains user supplied data for this condition */
147 typedef struct _cnd_capturesize_dat{
148 long max_capture_size;
149 }cnd_capturesize_dat;
152 * Constructs new condition for capturesize check. This function is invoked by
153 * 'cnd_new()' in order to perform class specific initialization.
155 * parameter: cnd - Pointer to condition passed by 'cnd_new()'.
156 * ap - Pointer to user supplied arguments list for this
158 * returns: Pointer to condition - Construction was successful.
159 * NULL - Construction failed.
161 static condition* _cnd_constr_capturesize(condition* cnd, va_list ap){
162 cnd_capturesize_dat *data = NULL;
163 /* allocate memory */
164 if((data = (cnd_capturesize_dat*)g_malloc(sizeof(cnd_capturesize_dat))) == NULL)
166 /* initialize user data */
167 data->max_capture_size = va_arg(ap, long);
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, long) >= 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() */