Bundle Age Extension Block: Type-20 decoding changes
[metze/wireshark/wip.git] / capture_stop_conditions.c
1 /* capture_stop_conditions.c
2  * Implementation for 'stop condition handler'.
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 #include <config.h>
24
25 #include <time.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include "conditions.h"
30 #include "capture_stop_conditions.h"
31 #include "ws_attributes.h"
32
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*);
38
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*);
43
44 static condition* _cnd_constr_interval(condition*, va_list);
45 static void _cnd_destr_interval(condition*);
46 static gboolean _cnd_eval_interval(condition*, va_list);
47 static void _cnd_reset_interval(condition*);
48
49 void init_capture_stop_conditions(void){
50   cnd_register_class(CND_CLASS_TIMEOUT,
51                      _cnd_constr_timeout,
52                      _cnd_destr_timeout,
53                      _cnd_eval_timeout,
54                      _cnd_reset_timeout);
55   cnd_register_class(CND_CLASS_CAPTURESIZE,
56                      _cnd_constr_capturesize,
57                      _cnd_destr_capturesize,
58                      _cnd_eval_capturesize,
59                      _cnd_reset_capturesize);
60   cnd_register_class(CND_CLASS_INTERVAL,
61                      _cnd_constr_interval,
62                      _cnd_destr_interval,
63                      _cnd_eval_interval,
64                      _cnd_reset_interval);
65 } /* END init_capture_stop_conditions() */
66
67 void cleanup_capture_stop_conditions(void){
68   cnd_unregister_class(CND_CLASS_TIMEOUT);
69   cnd_unregister_class(CND_CLASS_CAPTURESIZE);
70   cnd_unregister_class(CND_CLASS_INTERVAL);
71 } /* END cleanup_capture_stop_conditions() */
72
73 /*****************************************************************************/
74 /* Predefined condition 'timeout'.                                           */
75
76 /* class id */
77 const char* CND_CLASS_TIMEOUT = "cnd_class_timeout";
78
79 /* structure that contains user supplied data for this condition */
80 typedef struct _cnd_timeout_dat{
81   time_t start_time;
82   gint32 timeout_s;
83 }cnd_timeout_dat;
84
85 /*
86  * Constructs new condition for timeout check. This function is invoked by
87  * 'cnd_new()' in order to perform class specific initialization.
88  *
89  * parameter: cnd - Pointer to condition passed by 'cnd_new()'.
90  *            ap  - Pointer to user supplied arguments list for this
91  *                  constructor.
92  * returns:   Pointer to condition - Construction was successful.
93  *            NULL                 - Construction failed.
94  */
95 static condition* _cnd_constr_timeout(condition* cnd, va_list ap){
96   cnd_timeout_dat *data = NULL;
97   /* allocate memory */
98   if((data = (cnd_timeout_dat*)g_malloc(sizeof(cnd_timeout_dat))) == NULL)
99     return NULL;
100   /* initialize user data */
101   data->start_time = time(NULL);
102   data->timeout_s = va_arg(ap, gint32);
103   cnd_set_user_data(cnd, (void*)data);
104   return cnd;
105 } /* END _cnd_constr_timeout() */
106
107 /*
108  * Destroys condition for timeout check. This function is invoked by
109  * 'cnd_delete()' in order to perform class specific clean up.
110  *
111  * parameter: cnd - Pointer to condition passed by 'cnd_delete()'.
112  */
113 static void _cnd_destr_timeout(condition* cnd){
114   /* free memory */
115   g_free(cnd_get_user_data(cnd));
116 } /* END _cnd_destr_timeout() */
117
118 /*
119  * Condition handler for timeout condition. This function is invoked by
120  * 'cnd_eval()' in order to perform class specific condition checks.
121  *
122  * parameter: cnd - The inititalized timeout condition.
123  *            ap  - Pointer to user supplied arguments list for this
124  *                  handler.
125  * returns:   TRUE  - Condition is true.
126  *            FALSE - Condition is false.
127  */
128 static gboolean _cnd_eval_timeout(condition* cnd, va_list ap _U_){
129   cnd_timeout_dat* data = (cnd_timeout_dat*)cnd_get_user_data(cnd);
130   gint32 elapsed_time;
131   /* check timeout here */
132   if(data->timeout_s == 0) return FALSE; /* 0 == infinite */
133   elapsed_time = (gint32) (time(NULL) - data->start_time);
134   if(elapsed_time >= data->timeout_s) return TRUE;
135   return FALSE;
136 } /* END _cnd_eval_timeout()*/
137
138 /*
139  * Call this function to reset this condition to its initial state, i.e. the
140  * state it was in right after creation.
141  *
142  * parameter: cnd - Pointer to an initialized condition.
143  */
144 static void _cnd_reset_timeout(condition *cnd){
145   ((cnd_timeout_dat*)cnd_get_user_data(cnd))->start_time = time(NULL);
146 } /* END _cnd_reset_timeout() */
147
148
149 /*****************************************************************************/
150 /* Predefined condition 'max. capturesize'.                                  */
151
152 /* class id */
153 const char* CND_CLASS_CAPTURESIZE = "cnd_class_capturesize";
154
155 /* structure that contains user supplied data for this condition */
156 typedef struct _cnd_capturesize_dat{
157   guint64 max_capture_size;
158 }cnd_capturesize_dat;
159
160 /*
161  * Constructs new condition for capturesize check. This function is invoked by
162  * 'cnd_new()' in order to perform class specific initialization.
163  *
164  * parameter: cnd - Pointer to condition passed by 'cnd_new()'.
165  *            ap  - Pointer to user supplied arguments list for this
166  *                  constructor.
167  * returns:   Pointer to condition - Construction was successful.
168  *            NULL                 - Construction failed.
169  */
170 static condition* _cnd_constr_capturesize(condition* cnd, va_list ap){
171   cnd_capturesize_dat *data = NULL;
172   /* allocate memory */
173   if((data = (cnd_capturesize_dat*)g_malloc(sizeof(cnd_capturesize_dat))) == NULL)
174     return NULL;
175   /* initialize user data */
176   data->max_capture_size = va_arg(ap, guint64);
177   if (data->max_capture_size > ((guint64)INT_MAX + 1))
178     data->max_capture_size = (guint64)INT_MAX + 1;
179   cnd_set_user_data(cnd, (void*)data);
180   return cnd;
181 } /* END _cnd_constr_capturesize() */
182
183 /*
184  * Destroys condition for capturesize check. This function is invoked by
185  * 'cnd_delete()' in order to perform class specific clean up.
186  *
187  * parameter: cnd - Pointer to condition passed by 'cnd_delete()'.
188  */
189 static void _cnd_destr_capturesize(condition* cnd){
190   /* free memory */
191   g_free(cnd_get_user_data(cnd));
192 } /* END _cnd_destr_capturesize() */
193
194 /*
195  * Condition handler for capturesize condition. This function is invoked by
196  * 'cnd_eval()' in order to perform class specific condition checks.
197  *
198  * parameter: cnd - The inititalized capturesize condition.
199  *            ap  - Pointer to user supplied arguments list for this
200  *                  handler.
201  * returns:   TRUE  - Condition is true.
202  *            FALSE - Condition is false.
203  */
204 static gboolean _cnd_eval_capturesize(condition* cnd, va_list ap){
205   cnd_capturesize_dat* data = (cnd_capturesize_dat*)cnd_get_user_data(cnd);
206   /* check capturesize here */
207   if(data->max_capture_size == 0) return FALSE; /* 0 == infinite */
208   if(va_arg(ap, guint64) >= data->max_capture_size){
209     return TRUE;
210   }
211   return FALSE;
212 } /* END _cnd_eval_capturesize() */
213
214 /*
215  * Call this function to reset this condition to its initial state, i.e. the
216  * state it was in right after creation.
217  *
218  * parameter: cnd - Pointer to an initialized condition.
219  */
220 static void _cnd_reset_capturesize(condition *cnd _U_){
221 } /* END _cnd_reset_capturesize() */
222
223
224 /*****************************************************************************/
225 /* Predefined condition 'interval'.                                           */
226
227 /* class id */
228 const char* CND_CLASS_INTERVAL = "cnd_class_interval";
229
230 /* structure that contains user supplied data for this condition */
231 typedef struct _cnd_interval_dat{
232   time_t start_time;
233   gint32 interval_s;
234 }cnd_interval_dat;
235
236 /*
237  * Constructs new condition for interval check. This function is invoked by
238  * 'cnd_new()' in order to perform class specific initialization.
239  *
240  * parameter: cnd - Pointer to condition passed by 'cnd_new()'.
241  *            ap  - Pointer to user supplied arguments list for this
242  *                  constructor.
243  * returns:   Pointer to condition - Construction was successful.
244  *            NULL                 - Construction failed.
245  */
246 static condition* _cnd_constr_interval(condition* cnd, va_list ap){
247   cnd_interval_dat *data = NULL;
248   /* allocate memory */
249   if((data = (cnd_interval_dat*)g_malloc(sizeof(cnd_interval_dat))) == NULL)
250     return NULL;
251   /* initialize user data */
252   data->start_time = time(NULL);
253   data->interval_s = va_arg(ap, gint32);
254   data->start_time -= data->start_time % data->interval_s;
255   cnd_set_user_data(cnd, (void*)data);
256   return cnd;
257 } /* END _cnd_constr_interval() */
258
259 /*
260  * Destroys condition for interval check. This function is invoked by
261  * 'cnd_delete()' in order to perform class specific clean up.
262  *
263  * parameter: cnd - Pointer to condition passed by 'cnd_delete()'.
264  */
265 static void _cnd_destr_interval(condition* cnd){
266   /* free memory */
267   g_free(cnd_get_user_data(cnd));
268 } /* END _cnd_destr_interval() */
269
270 /*
271  * Condition handler for interval condition. This function is invoked by
272  * 'cnd_eval()' in order to perform class specific condition checks.
273  *
274  * parameter: cnd - The inititalized interval condition.
275  *            ap  - Pointer to user supplied arguments list for this
276  *                  handler.
277  * returns:   TRUE  - Condition is true.
278  *            FALSE - Condition is false.
279  */
280 static gboolean _cnd_eval_interval(condition* cnd, va_list ap _U_){
281   cnd_interval_dat* data = (cnd_interval_dat*)cnd_get_user_data(cnd);
282   gint32 elapsed_time;
283   /* check interval here */
284   if(data->interval_s == 0) return FALSE; /* 0 == infinite */
285   elapsed_time = (gint32) (time(NULL) - data->start_time);
286   if(elapsed_time >= data->interval_s) return TRUE;
287   return FALSE;
288 } /* END _cnd_eval_interval()*/
289
290 /*
291  * Call this function to reset this condition to its initial state, i.e. the
292  * state it was in right after creation.
293  *
294  * parameter: cnd - Pointer to an initialized condition.
295  */
296 static void _cnd_reset_interval(condition *cnd){
297   ((cnd_interval_dat*)cnd_get_user_data(cnd))->start_time = time(NULL);
298   ((cnd_interval_dat*)cnd_get_user_data(cnd))->start_time -=
299       ((cnd_interval_dat*)cnd_get_user_data(cnd))->start_time % ((cnd_interval_dat*)cnd_get_user_data(cnd))->interval_s;
300 } /* END _cnd_reset_interval() */
301
302
303
304 /*
305  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
306  *
307  * Local Variables:
308  * c-basic-offset: 2
309  * tab-width: 8
310  * indent-tabs-mode: nil
311  * End:
312  *
313  * ex: set shiftwidth=2 tabstop=8 expandtab:
314  * :indentSize=2:tabSize=8:noTabs=true:
315  */