Initial checkin of the stats-tree tap API
[obnox/wireshark/wip.git] / capture_stop_conditions.c
1 /* capture_stop_conditions.c
2  * Implementation for 'stop condition handler'.
3  *
4  * $Id$
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
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 #include <time.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <sys/stat.h>
29 #include <stdarg.h>
30 #include "conditions.h"
31 #include "capture_stop_conditions.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 void init_capture_stop_conditions(void){
45   cnd_register_class(CND_CLASS_TIMEOUT,
46                      _cnd_constr_timeout,
47                      _cnd_destr_timeout,
48                      _cnd_eval_timeout,
49                      _cnd_reset_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() */
56
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() */
61
62 /*****************************************************************************/
63 /* Predefined condition 'timeout'.                                           */
64
65 /* class id */
66 const char* CND_CLASS_TIMEOUT = "cnd_class_timeout";
67
68 /* structure that contains user supplied data for this condition */
69 typedef struct _cnd_timeout_dat{
70   time_t start_time;
71   gint32 timeout_s;
72 }cnd_timeout_dat;
73
74 /*
75  * Constructs new condition for timeout check. This function is invoked by
76  * 'cnd_new()' in order to perform class specific initialization.
77  *
78  * parameter: cnd - Pointer to condition passed by 'cnd_new()'.
79  *            ap  - Pointer to user supplied arguments list for this
80  *                  constructor.
81  * returns:   Pointer to condition - Construction was successful.
82  *            NULL                 - Construction failed.
83  */
84 static condition* _cnd_constr_timeout(condition* cnd, va_list ap){
85   cnd_timeout_dat *data = NULL;
86   /* allocate memory */
87   if((data = (cnd_timeout_dat*)malloc(sizeof(cnd_timeout_dat))) == NULL)
88     return 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);
93   return cnd;
94 } /* END _cnd_constr_timeout() */
95
96 /*
97  * Destroys condition for timeout check. This function is invoked by
98  * 'cnd_delete()' in order to perform class specific clean up.
99  *
100  * parameter: cnd - Pointer to condition passed by 'cnd_delete()'.
101  */
102 static void _cnd_destr_timeout(condition* cnd){
103   /* free memory */
104   free(cnd_get_user_data(cnd));
105 } /* END _cnd_destr_timeout() */
106
107 /*
108  * Condition handler for timeout condition. This function is invoked by
109  * 'cnd_eval()' in order to perform class specific condition checks.
110  *
111  * parameter: cnd - The inititalized timeout condition.
112  *            ap  - Pointer to user supplied arguments list for this
113  *                  handler.
114  * returns:   TRUE  - Condition is true.
115  *            FALSE - Condition is false.
116  */
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);
119   gint32 elapsed_time;
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;
124   return FALSE;
125 } /* END _cnd_eval_timeout()*/
126
127 /*
128  * Call this function to reset this condition to its initial state, i.e. the
129  * state it was in right after creation.
130  *
131  * parameter: cnd - Pointer to an initialized condition.
132  */
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() */
136
137
138 /*****************************************************************************/
139 /* Predefined condition 'max. capturesize'.                                  */
140
141 /* class id */
142 const char* CND_CLASS_CAPTURESIZE = "cnd_class_capturesize";
143
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;
148
149 /*
150  * Constructs new condition for capturesize check. This function is invoked by
151  * 'cnd_new()' in order to perform class specific initialization.
152  *
153  * parameter: cnd - Pointer to condition passed by 'cnd_new()'.
154  *            ap  - Pointer to user supplied arguments list for this
155  *                  constructor.
156  * returns:   Pointer to condition - Construction was successful.
157  *            NULL                 - Construction failed.
158  */
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)
163     return NULL;
164   /* initialize user data */
165   data->max_capture_size = va_arg(ap, long);
166   cnd_set_user_data(cnd, (void*)data);
167   return cnd;
168 } /* END _cnd_constr_capturesize() */
169
170 /*
171  * Destroys condition for capturesize check. This function is invoked by
172  * 'cnd_delete()' in order to perform class specific clean up.
173  *
174  * parameter: cnd - Pointer to condition passed by 'cnd_delete()'.
175  */
176 static void _cnd_destr_capturesize(condition* cnd){
177   /* free memory */
178   free(cnd_get_user_data(cnd));
179 } /* END _cnd_destr_capturesize() */
180
181 /*
182  * Condition handler for capturesize condition. This function is invoked by
183  * 'cnd_eval()' in order to perform class specific condition checks.
184  *
185  * parameter: cnd - The inititalized capturesize condition.
186  *            ap  - Pointer to user supplied arguments list for this
187  *                  handler.
188  * returns:   TRUE  - Condition is true.
189  *            FALSE - Condition is false.
190  */
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){
196     return TRUE;
197   }
198   return FALSE;
199 } /* END _cnd_eval_capturesize() */
200
201 /*
202  * Call this function to reset this condition to its initial state, i.e. the
203  * state it was in right after creation.
204  *
205  * parameter: cnd - Pointer to an initialized condition.
206  */
207 static void _cnd_reset_capturesize(condition *cnd _U_){
208 } /* END _cnd_reset_capturesize() */