aboutsummaryrefslogtreecommitdiffstats
path: root/capture_stop_conditions.c
blob: d1823fa174e401fe8a5dffb9d4e7e659feec7269 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* capture_stop_conditions.c
 * Implementation for 'stop condition handler'.
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include <config.h>

#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include "conditions.h"
#include "capture_stop_conditions.h"
#include "ws_attributes.h"

/* predefined classes function prototypes */
static condition* _cnd_constr_timeout(condition*, va_list);
static void _cnd_destr_timeout(condition*);
static gboolean _cnd_eval_timeout(condition*, va_list);
static void _cnd_reset_timeout(condition*);

static condition* _cnd_constr_capturesize(condition*, va_list);
static void _cnd_destr_capturesize(condition*);
static gboolean _cnd_eval_capturesize(condition*, va_list);
static void _cnd_reset_capturesize(condition*);

static condition* _cnd_constr_interval(condition*, va_list);
static void _cnd_destr_interval(condition*);
static gboolean _cnd_eval_interval(condition*, va_list);
static void _cnd_reset_interval(condition*);

void init_capture_stop_conditions(void){
  cnd_register_class(CND_CLASS_TIMEOUT,
                     _cnd_constr_timeout,
                     _cnd_destr_timeout,
                     _cnd_eval_timeout,
                     _cnd_reset_timeout);
  cnd_register_class(CND_CLASS_CAPTURESIZE,
                     _cnd_constr_capturesize,
                     _cnd_destr_capturesize,
                     _cnd_eval_capturesize,
                     _cnd_reset_capturesize);
  cnd_register_class(CND_CLASS_INTERVAL,
                     _cnd_constr_interval,
                     _cnd_destr_interval,
                     _cnd_eval_interval,
                     _cnd_reset_interval);
} /* END init_capture_stop_conditions() */

void cleanup_capture_stop_conditions(void){
  cnd_unregister_class(CND_CLASS_TIMEOUT);
  cnd_unregister_class(CND_CLASS_CAPTURESIZE);
  cnd_unregister_class(CND_CLASS_INTERVAL);
} /* END cleanup_capture_stop_conditions() */

/*****************************************************************************/
/* Predefined condition 'timeout'.                                           */

/* class id */
const char* CND_CLASS_TIMEOUT = "cnd_class_timeout";

/* structure that contains user supplied data for this condition */
typedef struct _cnd_timeout_dat{
  time_t start_time;
  gint32 timeout_s;
}cnd_timeout_dat;

/*
 * Constructs new condition for timeout check. This function is invoked by
 * 'cnd_new()' in order to perform class specific initialization.
 *
 * parameter: cnd - Pointer to condition passed by 'cnd_new()'.
 *            ap  - Pointer to user supplied arguments list for this
 *                  constructor.
 * returns:   Pointer to condition - Construction was successful.
 *            NULL                 - Construction failed.
 */
static condition* _cnd_constr_timeout(condition* cnd, va_list ap){
  cnd_timeout_dat *data = NULL;
  /* allocate memory */
  if((data = (cnd_timeout_dat*)g_malloc(sizeof(cnd_timeout_dat))) == NULL)
    return NULL;
  /* initialize user data */
  data->start_time = time(NULL);
  data->timeout_s = va_arg(ap, gint32);
  cnd_set_user_data(cnd, (void*)data);
  return cnd;
} /* END _cnd_constr_timeout() */

/*
 * Destroys condition for timeout check. This function is invoked by
 * 'cnd_delete()' in order to perform class specific clean up.
 *
 * parameter: cnd - Pointer to condition passed by 'cnd_delete()'.
 */
static void _cnd_destr_timeout(condition* cnd){
  /* free memory */
  g_free(cnd_get_user_data(cnd));
} /* END _cnd_destr_timeout() */

/*
 * Condition handler for timeout condition. This function is invoked by
 * 'cnd_eval()' in order to perform class specific condition checks.
 *
 * parameter: cnd - The inititalized timeout condition.
 *            ap  - Pointer to user supplied arguments list for this
 *                  handler.
 * returns:   TRUE  - Condition is true.
 *            FALSE - Condition is false.
 */
static gboolean _cnd_eval_timeout(condition* cnd, va_list ap _U_){
  cnd_timeout_dat* data = (cnd_timeout_dat*)cnd_get_user_data(cnd);
  gint32 elapsed_time;
  /* check timeout here */
  if(data->timeout_s == 0) return FALSE; /* 0 == infinite */
  elapsed_time = (gint32) (time(NULL) - data->start_time);
  if(elapsed_time >= data->timeout_s) return TRUE;
  return FALSE;
} /* END _cnd_eval_timeout()*/

/*
 * Call this function to reset this condition to its initial state, i.e. the
 * state it was in right after creation.
 *
 * parameter: cnd - Pointer to an initialized condition.
 */
static void _cnd_reset_timeout(condition *cnd){
  ((cnd_timeout_dat*)cnd_get_user_data(cnd))->start_time = time(NULL);
} /* END _cnd_reset_timeout() */


/*****************************************************************************/
/* Predefined condition 'max. capturesize'.                                  */

/* class id */
const char* CND_CLASS_CAPTURESIZE = "cnd_class_capturesize";

/* structure that contains user supplied data for this condition */
typedef struct _cnd_capturesize_dat{
  guint64 max_capture_size;
}cnd_capturesize_dat;

/*
 * Constructs new condition for capturesize check. This function is invoked by
 * 'cnd_new()' in order to perform class specific initialization.
 *
 * parameter: cnd - Pointer to condition passed by 'cnd_new()'.
 *            ap  - Pointer to user supplied arguments list for this
 *                  constructor.
 * returns:   Pointer to condition - Construction was successful.
 *            NULL                 - Construction failed.
 */
static condition* _cnd_constr_capturesize(condition* cnd, va_list ap){
  cnd_capturesize_dat *data = NULL;
  /* allocate memory */
  if((data = (cnd_capturesize_dat*)g_malloc(sizeof(cnd_capturesize_dat))) == NULL)
    return NULL;
  /* initialize user data */
  data->max_capture_size = va_arg(ap, guint64);
  if (data->max_capture_size > ((guint64)INT_MAX + 1))
    data->max_capture_size = (guint64)INT_MAX + 1;
  cnd_set_user_data(cnd, (void*)data);
  return cnd;
} /* END _cnd_constr_capturesize() */

/*
 * Destroys condition for capturesize check. This function is invoked by
 * 'cnd_delete()' in order to perform class specific clean up.
 *
 * parameter: cnd - Pointer to condition passed by 'cnd_delete()'.
 */
static void _cnd_destr_capturesize(condition* cnd){
  /* free memory */
  g_free(cnd_get_user_data(cnd));
} /* END _cnd_destr_capturesize() */

/*
 * Condition handler for capturesize condition. This function is invoked by
 * 'cnd_eval()' in order to perform class specific condition checks.
 *
 * parameter: cnd - The inititalized capturesize condition.
 *            ap  - Pointer to user supplied arguments list for this
 *                  handler.
 * returns:   TRUE  - Condition is true.
 *            FALSE - Condition is false.
 */
static gboolean _cnd_eval_capturesize(condition* cnd, va_list ap){
  cnd_capturesize_dat* data = (cnd_capturesize_dat*)cnd_get_user_data(cnd);
  /* check capturesize here */
  if(data->max_capture_size == 0) return FALSE; /* 0 == infinite */
  if(va_arg(ap, guint64) >= data->max_capture_size){
    return TRUE;
  }
  return FALSE;
} /* END _cnd_eval_capturesize() */

/*
 * Call this function to reset this condition to its initial state, i.e. the
 * state it was in right after creation.
 *
 * parameter: cnd - Pointer to an initialized condition.
 */
static void _cnd_reset_capturesize(condition *cnd _U_){
} /* END _cnd_reset_capturesize() */


/*****************************************************************************/
/* Predefined condition 'interval'.                                           */

/* class id */
const char* CND_CLASS_INTERVAL = "cnd_class_interval";

/* structure that contains user supplied data for this condition */
typedef struct _cnd_interval_dat{
  time_t start_time;
  gint32 interval_s;
}cnd_interval_dat;

/*
 * Constructs new condition for interval check. This function is invoked by
 * 'cnd_new()' in order to perform class specific initialization.
 *
 * parameter: cnd - Pointer to condition passed by 'cnd_new()'.
 *            ap  - Pointer to user supplied arguments list for this
 *                  constructor.
 * returns:   Pointer to condition - Construction was successful.
 *            NULL                 - Construction failed.
 */
static condition* _cnd_constr_interval(condition* cnd, va_list ap){
  cnd_interval_dat *data = NULL;
  /* allocate memory */
  if((data = (cnd_interval_dat*)g_malloc(sizeof(cnd_interval_dat))) == NULL)
    return NULL;
  /* initialize user data */
  data->start_time = time(NULL);
  data->interval_s = va_arg(ap, gint32);
  data->start_time -= data->start_time % data->interval_s;
  cnd_set_user_data(cnd, (void*)data);
  return cnd;
} /* END _cnd_constr_interval() */

/*
 * Destroys condition for interval check. This function is invoked by
 * 'cnd_delete()' in order to perform class specific clean up.
 *
 * parameter: cnd - Pointer to condition passed by 'cnd_delete()'.
 */
static void _cnd_destr_interval(condition* cnd){
  /* free memory */
  g_free(cnd_get_user_data(cnd));
} /* END _cnd_destr_interval() */

/*
 * Condition handler for interval condition. This function is invoked by
 * 'cnd_eval()' in order to perform class specific condition checks.
 *
 * parameter: cnd - The inititalized interval condition.
 *            ap  - Pointer to user supplied arguments list for this
 *                  handler.
 * returns:   TRUE  - Condition is true.
 *            FALSE - Condition is false.
 */
static gboolean _cnd_eval_interval(condition* cnd, va_list ap _U_){
  cnd_interval_dat* data = (cnd_interval_dat*)cnd_get_user_data(cnd);
  gint32 elapsed_time;
  /* check interval here */
  if(data->interval_s == 0) return FALSE; /* 0 == infinite */
  elapsed_time = (gint32) (time(NULL) - data->start_time);
  if(elapsed_time >= data->interval_s) return TRUE;
  return FALSE;
} /* END _cnd_eval_interval()*/

/*
 * Call this function to reset this condition to its initial state, i.e. the
 * state it was in right after creation.
 *
 * parameter: cnd - Pointer to an initialized condition.
 */
static void _cnd_reset_interval(condition *cnd){
  ((cnd_interval_dat*)cnd_get_user_data(cnd))->start_time = time(NULL);
  ((cnd_interval_dat*)cnd_get_user_data(cnd))->start_time -=
      ((cnd_interval_dat*)cnd_get_user_data(cnd))->start_time % ((cnd_interval_dat*)cnd_get_user_data(cnd))->interval_s;
} /* END _cnd_reset_interval() */



/*
 * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
 *
 * Local Variables:
 * c-basic-offset: 2
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * ex: set shiftwidth=2 tabstop=8 expandtab:
 * :indentSize=2:tabSize=8:noTabs=true:
 */