tests: add regression tests for Follow TCP Stream
[metze/wireshark/wip.git] / capture_stop_conditions.c
index debf9f9247850f4113481ea90731cd161ca645c1..d1823fa174e401fe8a5dffb9d4e7e659feec7269 100644 (file)
@@ -1,36 +1,22 @@
 /* capture_stop_conditions.c
  * Implementation for 'stop condition handler'.
  *
- * $Id$
- *
  * Wireshark - Network traffic analyzer
  * By Gerald Combs <gerald@wireshark.org>
  * Copyright 1998 Gerald Combs
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * SPDX-License-Identifier: GPL-2.0-or-later
  */
 
-#include "config.h"
+#include <config.h>
 
 #include <time.h>
 #include <string.h>
 #include <stdlib.h>
-#include <sys/stat.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);
@@ -43,6 +29,11 @@ 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,
@@ -54,11 +45,17 @@ void init_capture_stop_conditions(void){
                      _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() */
 
 /*****************************************************************************/
@@ -145,7 +142,7 @@ const char* CND_CLASS_CAPTURESIZE = "cnd_class_capturesize";
 
 /* structure that contains user supplied data for this condition */
 typedef struct _cnd_capturesize_dat{
-  long max_capture_size;
+  guint64 max_capture_size;
 }cnd_capturesize_dat;
 
 /*
@@ -164,7 +161,9 @@ static condition* _cnd_constr_capturesize(condition* cnd, va_list ap){
   if((data = (cnd_capturesize_dat*)g_malloc(sizeof(cnd_capturesize_dat))) == NULL)
     return NULL;
   /* initialize user data */
-  data->max_capture_size = va_arg(ap, long);
+  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() */
@@ -194,7 +193,7 @@ 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, long) >= data->max_capture_size){
+  if(va_arg(ap, guint64) >= data->max_capture_size){
     return TRUE;
   }
   return FALSE;
@@ -208,3 +207,97 @@ static gboolean _cnd_eval_capturesize(condition* cnd, va_list ap){
  */
 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:
+ */