Update Travis to Trusty
[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 <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*)g_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   g_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 = (gint32) (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   guint64 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*)g_malloc(sizeof(cnd_capturesize_dat))) == NULL)
163     return NULL;
164   /* initialize user data */
165   data->max_capture_size = va_arg(ap, guint64);
166   if (data->max_capture_size > ((guint64)INT_MAX + 1))
167     data->max_capture_size = (guint64)INT_MAX + 1;
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, guint64) >= 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() */
211
212 /*
213  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
214  *
215  * Local Variables:
216  * c-basic-offset: 2
217  * tab-width: 8
218  * indent-tabs-mode: nil
219  * End:
220  *
221  * ex: set shiftwidth=2 tabstop=8 expandtab:
222  * :indentSize=2:tabSize=8:noTabs=true:
223  */