Witness: set col_info for interfaceInfo_state
[wireshark-wip.git] / capture_stop_conditions.c
blob386bf809bbfc1221a3656e7569b96838b87c3149
1 /* capture_stop_conditions.c
2 * Implementation for 'stop condition handler'.
4 * $Id$
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.
25 #include "config.h"
27 #include <time.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <sys/stat.h>
31 #include <stdarg.h>
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,
48 _cnd_constr_timeout,
49 _cnd_destr_timeout,
50 _cnd_eval_timeout,
51 _cnd_reset_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'. */
67 /* class id */
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{
72 time_t start_time;
73 gint32 timeout_s;
74 }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
82 * constructor.
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;
88 /* allocate memory */
89 if((data = (cnd_timeout_dat*)g_malloc(sizeof(cnd_timeout_dat))) == NULL)
90 return 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);
95 return cnd;
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){
105 /* free memory */
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
115 * handler.
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);
121 gint32 elapsed_time;
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;
126 return FALSE;
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'. */
143 /* class id */
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 guint64 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
157 * constructor.
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)
165 return NULL;
166 /* initialize user data */
167 data->max_capture_size = va_arg(ap, guint64);
168 if (data->max_capture_size > ((guint64)INT_MAX + 1))
169 data->max_capture_size = (guint64)INT_MAX + 1;
170 cnd_set_user_data(cnd, (void*)data);
171 return cnd;
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){
181 /* free memory */
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
191 * handler.
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, guint64) >= data->max_capture_size){
200 return TRUE;
202 return FALSE;
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() */