Include config.h in preparation of moving the definition
[obnox/wireshark/wip.git] / capture_stop_conditions.c
1 /* capture_stop_conditions.c
2  * Implementation for 'stop condition handler'.
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <time.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <sys/stat.h>
33 #include <stdarg.h>
34 #include "conditions.h"
35 #include "capture_stop_conditions.h"
36
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*);
42
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*);
47
48 void init_capture_stop_conditions(void){
49   cnd_register_class(CND_CLASS_TIMEOUT,
50                      _cnd_constr_timeout,
51                      _cnd_destr_timeout,
52                      _cnd_eval_timeout,
53                      _cnd_reset_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() */
60
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() */
65
66 /*****************************************************************************/
67 /* Predefined condition 'timeout'.                                           */
68
69 /* class id */
70 const char* CND_CLASS_TIMEOUT = "cnd_class_timeout";
71
72 /* structure that contains user supplied data for this condition */
73 typedef struct _cnd_timeout_dat{
74   time_t start_time;
75   gint32 timeout_s;
76 }cnd_timeout_dat;
77
78 /*
79  * Constructs new condition for timeout check. This function is invoked by
80  * 'cnd_new()' in order to perform class specific initialization.
81  *
82  * parameter: cnd - Pointer to condition passed by 'cnd_new()'.
83  *            ap  - Pointer to user supplied arguments list for this
84  *                  constructor.
85  * returns:   Pointer to condition - Construction was successful.
86  *            NULL                 - Construction failed.
87  */
88 static condition* _cnd_constr_timeout(condition* cnd, va_list ap){
89   cnd_timeout_dat *data = NULL;
90   /* allocate memory */
91   if((data = (cnd_timeout_dat*)g_malloc(sizeof(cnd_timeout_dat))) == NULL)
92     return 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);
97   return cnd;
98 } /* END _cnd_constr_timeout() */
99
100 /*
101  * Destroys condition for timeout check. This function is invoked by
102  * 'cnd_delete()' in order to perform class specific clean up.
103  *
104  * parameter: cnd - Pointer to condition passed by 'cnd_delete()'.
105  */
106 static void _cnd_destr_timeout(condition* cnd){
107   /* free memory */
108   g_free(cnd_get_user_data(cnd));
109 } /* END _cnd_destr_timeout() */
110
111 /*
112  * Condition handler for timeout condition. This function is invoked by
113  * 'cnd_eval()' in order to perform class specific condition checks.
114  *
115  * parameter: cnd - The inititalized timeout condition.
116  *            ap  - Pointer to user supplied arguments list for this
117  *                  handler.
118  * returns:   TRUE  - Condition is true.
119  *            FALSE - Condition is false.
120  */
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);
123   gint32 elapsed_time;
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;
128   return FALSE;
129 } /* END _cnd_eval_timeout()*/
130
131 /*
132  * Call this function to reset this condition to its initial state, i.e. the
133  * state it was in right after creation.
134  *
135  * parameter: cnd - Pointer to an initialized condition.
136  */
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() */
140
141
142 /*****************************************************************************/
143 /* Predefined condition 'max. capturesize'.                                  */
144
145 /* class id */
146 const char* CND_CLASS_CAPTURESIZE = "cnd_class_capturesize";
147
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;
152
153 /*
154  * Constructs new condition for capturesize check. This function is invoked by
155  * 'cnd_new()' in order to perform class specific initialization.
156  *
157  * parameter: cnd - Pointer to condition passed by 'cnd_new()'.
158  *            ap  - Pointer to user supplied arguments list for this
159  *                  constructor.
160  * returns:   Pointer to condition - Construction was successful.
161  *            NULL                 - Construction failed.
162  */
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)
167     return NULL;
168   /* initialize user data */
169   data->max_capture_size = va_arg(ap, long);
170   cnd_set_user_data(cnd, (void*)data);
171   return cnd;
172 } /* END _cnd_constr_capturesize() */
173
174 /*
175  * Destroys condition for capturesize check. This function is invoked by
176  * 'cnd_delete()' in order to perform class specific clean up.
177  *
178  * parameter: cnd - Pointer to condition passed by 'cnd_delete()'.
179  */
180 static void _cnd_destr_capturesize(condition* cnd){
181   /* free memory */
182   g_free(cnd_get_user_data(cnd));
183 } /* END _cnd_destr_capturesize() */
184
185 /*
186  * Condition handler for capturesize condition. This function is invoked by
187  * 'cnd_eval()' in order to perform class specific condition checks.
188  *
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.
194  */
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){
200     return TRUE;
201   }
202   return FALSE;
203 } /* END _cnd_eval_capturesize() */
204
205 /*
206  * Call this function to reset this condition to its initial state, i.e. the
207  * state it was in right after creation.
208  *
209  * parameter: cnd - Pointer to an initialized condition.
210  */
211 static void _cnd_reset_capturesize(condition *cnd _U_){
212 } /* END _cnd_reset_capturesize() */