diff --git a/Makefile.in b/Makefile.in index 0740fa40f..af399ddea 100644 --- a/Makefile.in +++ b/Makefile.in @@ -103,6 +103,7 @@ OBJS = @OBJS@ \ src/capture_filter/mirror.o \ src/capture_filter/none.o \ src/capture_filter/split.o \ + src/compat/alarm.o \ src/compat/drand48.o \ src/compat/gettimeofday.o \ src/compat/platform_pipe.o \ diff --git a/src/compat/alarm.c b/src/compat/alarm.c new file mode 100644 index 000000000..417742a94 --- /dev/null +++ b/src/compat/alarm.c @@ -0,0 +1,64 @@ +/** + * @file compat/alarm.c + * @copydoc compat/alarm.h + */ +/* +* Copyright (c) 2019 CESNET, z. s. p. o. +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, is permitted provided that the following conditions +* are met: +* +* 1. Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* +* 2. Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* +* 3. Neither the name of CESNET nor the names of its contributors may be +* used to endorse or promote products derived from this software without +* specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, +* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY +* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO +* EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, +* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#include "config_unix.h" +#include "config_win32.h" +#endif // defined HAVE_CONFIG_H + +#ifdef WIN32 + +#include "compat/alarm.h" + +static void *abort_worker(void *arg) { + unsigned int *sec = arg; + sleep(*sec); + free(sec); + abort(); +} + +void alarm(unsigned int sec) +{ + pthread_t id; + unsigned int *val = malloc(sizeof(unsigned int)); + *val = sec; + pthread_create(&id, NULL, abort_worker, val); + pthread_detach(id); +} + +#endif // WIN32 diff --git a/src/compat/alarm.h b/src/compat/alarm.h new file mode 100644 index 000000000..25471f041 --- /dev/null +++ b/src/compat/alarm.h @@ -0,0 +1,50 @@ +/** + * @file compat/alarm.h + * @author Martin Pulec + * + * This emulates alarm on Windows without custom signal handler (i. e. + * using default SIGALRM action which is terminate). + */ +/* + * Copyright (c) 2019 CESNET, z. s. p. o. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, is permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of CESNET nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifdef WIN32 + +#ifndef COMPAT_ALARM_H +#define COMPAT_ALARM_H + +EXTERN_C void alarm(unsigned int sec); + +#endif // defined COMPAT_ALARM_H + +#endif /* WIN32 */ diff --git a/src/config_win32.h b/src/config_win32.h index 64b050ed6..997bf6315 100644 --- a/src/config_win32.h +++ b/src/config_win32.h @@ -114,6 +114,7 @@ typedef unsigned int in_addr_t; #define NEED_INET_ATON #include /* For clock_t */ +#include "compat/alarm.h" #include "compat/usleep.h" #include "crypto/random.h" diff --git a/src/utils/packet_counter.cpp b/src/utils/packet_counter.cpp index 450882946..80b965621 100644 --- a/src/utils/packet_counter.cpp +++ b/src/utils/packet_counter.cpp @@ -35,6 +35,12 @@ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#include "config_unix.h" +#include "config_win32.h" +#endif // defined HAVE_CONFIG_H + #include "utils/packet_counter.h" #include #include