frame: Fix Dereference of null pointer found by Clang analyzer
[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
32 /* predefined classes function prototypes */
33 static condition* _cnd_constr_timeout(condition*, va_list);
34 static void _cnd_destr_timeout(condition*);
35 static gboolean _cnd_eval_timeout(condition*, va_list);
36 static void _cnd_reset_timeout(condition*);
37
38 static condition* _cnd_constr_capturesize(condition*, va_list);
39 static void _cnd_destr_capturesize(condition*);
40 static gboolean _cnd_eval_capturesize(condition*, va_list);
41 static void _cnd_reset_capturesize(condition*);
42
43 void init_capture_stop_conditions(void){
44   cnd_register_class(CND_CLASS_TIMEOUT,
45                      _cnd_constr_timeout,
46                      _cnd_destr_timeout,
47                      _cnd_eval_timeout,
48                      _cnd_reset_timeout);
49   cnd_register_class(CND_CLASS_CAPTURESIZE,
50                      _cnd_constr_capturesize,
51                      _cnd_destr_capturesize,
52                      _cnd_eval_capturesize,
53                      _cnd_reset_capturesize);
54 } /* END init_capture_stop_conditions() */
55
56 void cleanup_capture_stop_conditions(void){
57   cnd_unregister_class(CND_CLASS_TIMEOUT);
58   cnd_unregister_class(CND_CLASS_CAPTURESIZE);
59 } /* END cleanup_capture_stop_conditions() */
60
61 /*****************************************************************************/
62 /* Predefined condition 'timeout'.                                           */
63
64 /* class id */
65 const char* CND_CLASS_TIMEOUT = "cnd_class_timeout";
66
67 /* structure that contains user supplied data for this condition */
68 typedef struct _cnd_timeout_dat{
69   time_t start_time;
70   gint32 timeout_s;
71 }cnd_timeout_dat;
72
73 /*
74  * Constructs new condition for timeout check. This function is invoked by
75  * 'cnd_new()' in order to perform class specific initialization.
76  *
77  * parameter: cnd - Pointer to condition passed by 'cnd_new()'.
78  *            ap  - Pointer to user supplied arguments list for this
79  *                  constructor.
80  * returns:   Pointer to condition - Construction was successful.
81  *            NULL                 - Construction failed.
82  */
83 static condition* _cnd_constr_timeout(condition* cnd, va_list ap){
84   cnd_timeout_dat *data = NULL;
85   /* allocate memory */
86   if((data = (cnd_timeout_dat*)g_malloc(sizeof(cnd_timeout_dat))) == NULL)
87     return NULL;
88   /* initialize user data */
89   data->start_time = time(NULL);
90   data->timeout_s = va_arg(ap, gint32);
91   cnd_set_user_data(cnd, (void*)data);
92   return cnd;
93 } /* END _cnd_constr_timeout() */
94
95 /*
96  * Destroys condition for timeout check. This function is invoked by
97  * 'cnd_delete()' in order to perform class specific clean up.
98  *
99  * parameter: cnd - Pointer to condition passed by 'cnd_delete()'.
100  */
101 static void _cnd_destr_timeout(condition* cnd){
102   /* free memory */
103   g_free(cnd_get_user_data(cnd));
104 } /* END _cnd_destr_timeout() */
105
106 /*
107  * Condition handler for timeout condition. This function is invoked by
108  * 'cnd_eval()' in order to perform class specific condition checks.
109  *
110  * parameter: cnd - The inititalized timeout condition.
111  *            ap  - Pointer to user supplied arguments list for this
112  *                  handler.
113  * returns:   TRUE  - Condition is true.
114  *            FALSE - Condition is false.
115  */
116 static gboolean _cnd_eval_timeout(condition* cnd, va_list ap _U_){
117   cnd_timeout_dat* data = (cnd_timeout_dat*)cnd_get_user_data(cnd);
118   gint32 elapsed_time;
119   /* check timeout here */
120   if(data->timeout_s == 0) return FALSE; /* 0 == infinite */
121   elapsed_time = (gint32) (time(NULL) - data->start_time);
122   if(elapsed_time >= data->timeout_s) return TRUE;
123   return FALSE;
124 } /* END _cnd_eval_timeout()*/
125
126 /*
127  * Call this function to reset this condition to its initial state, i.e. the
128  * state it was in right after creation.
129  *
130  * parameter: cnd - Pointer to an initialized condition.
131  */
132 static void _cnd_reset_timeout(condition *cnd){
133   ((cnd_timeout_dat*)cnd_get_user_data(cnd))->start_time = time(NULL);
134 } /* END _cnd_reset_timeout() */
135
136
137 /*****************************************************************************/
138 /* Predefined condition 'max. capturesize'.                                  */
139
140 /* class id */
141 const char* CND_CLASS_CAPTURESIZE = "cnd_class_capturesize";
142
143 /* structure that contains user supplied data for this condition */
144 typedef struct _cnd_capturesize_dat{
145   guint64 max_capture_size;
146 }cnd_capturesize_dat;
147
148 /*
149  * Constructs new condition for capturesize check. This function is invoked by
150  * 'cnd_new()' in order to perform class specific initialization.
151  *
152  * parameter: cnd - Pointer to condition passed by 'cnd_new()'.
153  *            ap  - Pointer to user supplied arguments list for this
154  *                  constructor.
155  * returns:   Pointer to condition - Construction was successful.
156  *            NULL                 - Construction failed.
157  */
158 static condition* _cnd_constr_capturesize(condition* cnd, va_list ap){
159   cnd_capturesize_dat *data = NULL;
160   /* allocate memory */
161   if((data = (cnd_capturesize_dat*)g_malloc(sizeof(cnd_capturesize_dat))) == NULL)
162     return NULL;
163   /* initialize user data */
164   data->max_capture_size = va_arg(ap, guint64);
165   if (data->max_capture_size > ((guint64)INT_MAX + 1))
166     data->max_capture_size = (guint64)INT_MAX + 1;
167   cnd_set_user_data(cnd, (void*)data);
168   return cnd;
169 } /* END _cnd_constr_capturesize() */
170
171 /*
172  * Destroys condition for capturesize check. This function is invoked by
173  * 'cnd_delete()' in order to perform class specific clean up.
174  *
175  * parameter: cnd - Pointer to condition passed by 'cnd_delete()'.
176  */
177 static void _cnd_destr_capturesize(condition* cnd){
178   /* free memory */
179   g_free(cnd_get_user_data(cnd));
180 } /* END _cnd_destr_capturesize() */
181
182 /*
183  * Condition handler for capturesize condition. This function is invoked by
184  * 'cnd_eval()' in order to perform class specific condition checks.
185  *
186  * parameter: cnd - The inititalized capturesize condition.
187  *            ap  - Pointer to user supplied arguments list for this
188  *                  handler.
189  * returns:   TRUE  - Condition is true.
190  *            FALSE - Condition is false.
191  */
192 static gboolean _cnd_eval_capturesize(condition* cnd, va_list ap){
193   cnd_capturesize_dat* data = (cnd_capturesize_dat*)cnd_get_user_data(cnd);
194   /* check capturesize here */
195   if(data->max_capture_size == 0) return FALSE; /* 0 == infinite */
196   if(va_arg(ap, guint64) >= data->max_capture_size){
197     return TRUE;
198   }
199   return FALSE;
200 } /* END _cnd_eval_capturesize() */
201
202 /*
203  * Call this function to reset this condition to its initial state, i.e. the
204  * state it was in right after creation.
205  *
206  * parameter: cnd - Pointer to an initialized condition.
207  */
208 static void _cnd_reset_capturesize(condition *cnd _U_){
209 } /* END _cnd_reset_capturesize() */
210
211 /*
212  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
213  *
214  * Local Variables:
215  * c-basic-offset: 2
216  * tab-width: 8
217  * indent-tabs-mode: nil
218  * End:
219  *
220  * ex: set shiftwidth=2 tabstop=8 expandtab:
221  * :indentSize=2:tabSize=8:noTabs=true:
222  */