Move code to fill in the wtap_pkthdr into peektagged_process_header(),
[metze/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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 #include "config.h"
26
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"
34
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*);
40
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*);
45
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() */
58
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() */
63
64 /*****************************************************************************/
65 /* Predefined condition 'timeout'.                                           */
66
67 /* class id */
68 const char* CND_CLASS_TIMEOUT = "cnd_class_timeout";
69
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;
75
76 /*
77  * Constructs new condition for timeout check. This function is invoked by
78  * 'cnd_new()' in order to perform class specific initialization.
79  *
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.
85  */
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() */
97
98 /*
99  * Destroys condition for timeout check. This function is invoked by
100  * 'cnd_delete()' in order to perform class specific clean up.
101  *
102  * parameter: cnd - Pointer to condition passed by 'cnd_delete()'.
103  */
104 static void _cnd_destr_timeout(condition* cnd){
105   /* free memory */
106   g_free(cnd_get_user_data(cnd));
107 } /* END _cnd_destr_timeout() */
108
109 /*
110  * Condition handler for timeout condition. This function is invoked by
111  * 'cnd_eval()' in order to perform class specific condition checks.
112  *
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.
118  */
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()*/
128
129 /*
130  * Call this function to reset this condition to its initial state, i.e. the
131  * state it was in right after creation.
132  *
133  * parameter: cnd - Pointer to an initialized condition.
134  */
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() */
138
139
140 /*****************************************************************************/
141 /* Predefined condition 'max. capturesize'.                                  */
142
143 /* class id */
144 const char* CND_CLASS_CAPTURESIZE = "cnd_class_capturesize";
145
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;
150
151 /*
152  * Constructs new condition for capturesize check. This function is invoked by
153  * 'cnd_new()' in order to perform class specific initialization.
154  *
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.
160  */
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, long);
168   cnd_set_user_data(cnd, (void*)data);
169   return cnd;
170 } /* END _cnd_constr_capturesize() */
171
172 /*
173  * Destroys condition for capturesize check. This function is invoked by
174  * 'cnd_delete()' in order to perform class specific clean up.
175  *
176  * parameter: cnd - Pointer to condition passed by 'cnd_delete()'.
177  */
178 static void _cnd_destr_capturesize(condition* cnd){
179   /* free memory */
180   g_free(cnd_get_user_data(cnd));
181 } /* END _cnd_destr_capturesize() */
182
183 /*
184  * Condition handler for capturesize condition. This function is invoked by
185  * 'cnd_eval()' in order to perform class specific condition checks.
186  *
187  * parameter: cnd - The inititalized capturesize condition.
188  *            ap  - Pointer to user supplied arguments list for this
189  *                  handler.
190  * returns:   TRUE  - Condition is true.
191  *            FALSE - Condition is false.
192  */
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){
198     return TRUE;
199   }
200   return FALSE;
201 } /* END _cnd_eval_capturesize() */
202
203 /*
204  * Call this function to reset this condition to its initial state, i.e. the
205  * state it was in right after creation.
206  *
207  * parameter: cnd - Pointer to an initialized condition.
208  */
209 static void _cnd_reset_capturesize(condition *cnd _U_){
210 } /* END _cnd_reset_capturesize() */