"Fossies" - the Fresh Open Source Software Archive

Member "snort-2.9.17/src/util.h" (16 Oct 2020, 11750 Bytes) of package /linux/misc/snort-2.9.17.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "util.h" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.9.16.1_vs_2.9.17.

    1 /* $Id$ */
    2 /*
    3 ** Copyright (C) 2014-2020 Cisco and/or its affiliates. All rights reserved.
    4 ** Copyright (C) 2002-2013 Sourcefire, Inc.
    5 ** Copyright (C) 2002 Martin Roesch <roesch@sourcefire.com>
    6 **
    7 ** This program is free software; you can redistribute it and/or modify
    8 ** it under the terms of the GNU General Public License Version 2 as
    9 ** published by the Free Software Foundation.  You may not use, modify or
   10 ** distribute this program under any other version of the GNU General
   11 ** Public License.
   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 
   24 #ifndef __UTIL_H__
   25 #define __UTIL_H__
   26 
   27 #define TIMEBUF_SIZE 26
   28 
   29 #ifdef HAVE_CONFIG_H
   30 # include "config.h"
   31 #endif
   32 
   33 #ifndef WIN32
   34 # include <sys/time.h>
   35 # include <sys/types.h>
   36 # ifdef LINUX
   37 #  include <sys/syscall.h>
   38 # endif
   39 #endif
   40 #include <stdlib.h>
   41 #include <errno.h>
   42 #include <unistd.h>
   43 
   44 #ifdef HAVE_STRINGS_H
   45 #include <strings.h>
   46 #endif
   47 #include <string.h>
   48 
   49 #include "sf_types.h"
   50 #include "sflsq.h"
   51 #include "sfutil/sf_ipvar.h"
   52 #include "ipv6_port.h"
   53 #include "control/sfcontrol.h"
   54 
   55 /* Macros *********************************************************************/
   56 
   57 /* specifies that a function does not return
   58  * used for quieting Visual Studio warnings */
   59 #ifdef _MSC_VER
   60 # if _MSC_VER >= 1400
   61 #  define NORETURN __declspec(noreturn)
   62 # else
   63 #  define NORETURN
   64 # endif
   65 #else
   66 # define NORETURN
   67 #endif
   68 
   69 #if !defined(__GNUC__) || __GNUC__ < 2 || \
   70     (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
   71 #define __attribute__(x)    /* delete __attribute__ if non-gcc or gcc1 */
   72 #endif
   73 
   74 #define SNORT_SNPRINTF_SUCCESS 0
   75 #define SNORT_SNPRINTF_TRUNCATION 1
   76 #define SNORT_SNPRINTF_ERROR -1
   77 
   78 #define SNORT_STRNCPY_SUCCESS 0
   79 #define SNORT_STRNCPY_TRUNCATION 1
   80 #define SNORT_STRNCPY_ERROR -1
   81 
   82 #define SNORT_STRNLEN_ERROR -1
   83 
   84 #define SECONDS_PER_DAY  86400  /* number of seconds in a day  */
   85 #define SECONDS_PER_HOUR  3600  /* number of seconds in a hour */
   86 #define SECONDS_PER_MIN     60     /* number of seconds in a minute */
   87 
   88 #define STD_BUF  1024
   89 
   90 #define COPY4(x, y) \
   91     x[0] = y[0]; x[1] = y[1]; x[2] = y[2]; x[3] = y[3];
   92 
   93 #define COPY16(x,y) \
   94     x[0] = y[0]; x[1] = y[1]; x[2] = y[2]; x[3] = y[3]; \
   95     x[4] = y[4]; x[5] = y[5]; x[6] = y[6]; x[7] = y[7]; \
   96     x[8] = y[8]; x[9] = y[9]; x[10] = y[10]; x[11] = y[11]; \
   97     x[12] = y[12]; x[13] = y[13]; x[14] = y[14]; x[15] = y[15];
   98 
   99 
  100 /* Externs ********************************************************************/
  101 extern uint32_t *netmasks;
  102 
  103 
  104 /* Data types *****************************************************************/
  105 
  106 typedef struct _IntervalStats
  107 {
  108     uint64_t recv, recv_total;
  109     uint64_t drop, drop_total;
  110     uint64_t processed, processed_total;
  111     uint64_t tcp, tcp_total;
  112     uint64_t udp, udp_total;
  113     uint64_t icmp, icmp_total;
  114     uint64_t arp, arp_total;
  115     uint64_t ipx, ipx_total;
  116     uint64_t eapol, eapol_total;
  117     uint64_t ipv6, ipv6_total;
  118     uint64_t ethloopback, ethloopback_total;
  119     uint64_t other, other_total;
  120     uint64_t frags, frags_total;
  121     uint64_t discards, discards_total;
  122     uint64_t frag_trackers, frag_trackers_total;
  123     uint64_t frag_rebuilt, frag_rebuilt_total;
  124     uint64_t frag_element, frag_element_total;
  125     uint64_t frag_incomp, frag_incomp_total;
  126     uint64_t frag_timeout, frag_timeout_total;
  127     uint64_t frag_mem_faults, frag_mem_faults_total;
  128     uint64_t tcp_str_packets, tcp_str_packets_total;
  129     uint64_t tcp_str_trackers, tcp_str_trackers_total;
  130     uint64_t tcp_str_flushes, tcp_str_flushes_total;
  131     uint64_t tcp_str_segs_used, tcp_str_segs_used_total;
  132     uint64_t tcp_str_segs_queued, tcp_str_segs_queued_total;
  133     uint64_t tcp_str_mem_faults, tcp_str_mem_faults_total;
  134 
  135 #ifdef GRE
  136     uint64_t ip4ip4, ip4ip4_total;
  137     uint64_t ip4ip6, ip4ip6_total;
  138     uint64_t ip6ip4, ip6ip4_total;
  139     uint64_t ip6ip6, ip6ip6_total;
  140 
  141     uint64_t gre, gre_total;
  142     uint64_t gre_ip, gre_ip_total;
  143     uint64_t gre_eth, gre_eth_total;
  144     uint64_t gre_arp, gre_arp_total;
  145     uint64_t gre_ipv6, gre_ipv6_total;
  146     uint64_t gre_ipx, gre_ipx_total;
  147     uint64_t gre_loopback, gre_loopback_total;
  148     uint64_t gre_vlan, gre_vlan_total;
  149     uint64_t gre_ppp, gre_ppp_total;
  150 #endif
  151 
  152 #ifdef DLT_IEEE802_11
  153     uint64_t wifi_mgmt, wifi_mgmt_total;
  154     uint64_t wifi_control, wifi_control_total;
  155     uint64_t wifi_data, wifi_data_total;
  156 #endif
  157 
  158 } IntervalStats;
  159 
  160 
  161 /* Public function prototypes *************************************************/
  162 void StoreSnortInfoStrings(void);
  163 int DisplayBanner(void);
  164 int gmt2local(time_t);
  165 void ts_print(register const struct timeval *, char *);
  166 char *copy_argv(char **);
  167 void strip(char *);
  168 double CalcPct(uint64_t, uint64_t);
  169 void GoDaemon(void);
  170 void SignalWaitingParent(void);
  171 void CheckLogDir(void);
  172 char *read_infile(char *);
  173 void CleanupProtoNames(void);
  174 void CreatePidFile(const char *, pid_t);
  175 void ClosePidFile(void);
  176 void SetUidGid(int, int);
  177 void InitGroups(int, int);
  178 void SetChroot(char *, char **);
  179 void DropStats(int);
  180 /* Function For Displaying in SFR CLI */
  181 void DisplayActionStats (uint16_t type, void *old_context, struct _THREAD_ELEMENT *te, ControlDataSendFunc f);
  182 void TimeStart(void);
  183 void TimeStop(void);
  184 
  185 #ifndef __GNUC__
  186 #define __attribute__(x)  /*NOTHING*/
  187 #endif
  188 void LogMessage(const char *, ...) __attribute__((format (printf, 1, 2)));
  189 void WarningMessage(const char *, ...) __attribute__((format (printf, 1, 2)));
  190 void ErrorMessage(const char *, ...) __attribute__((format (printf, 1, 2)));
  191 typedef struct _ThrottleInfo
  192 {
  193     time_t lastUpdate;
  194     /*Within this duration (in seconds), maximal one distinct message is logged*/
  195     uint32_t duration_to_log;
  196     uint64_t count;
  197     /*Till the message count reaches to count_to_log, maximal one distinct message is logged*/
  198     uint64_t count_to_log;
  199 }ThrottleInfo;
  200 void ErrorMessageThrottled(ThrottleInfo*,const char *, ...) __attribute__((format (printf, 2, 3)));
  201 void LogThrottledByTimeCount(ThrottleInfo*,const char *, ...) __attribute__((format (printf, 2, 3)));
  202 
  203 NORETURN void FatalError(const char *, ...) __attribute__((format (printf, 1, 2)));
  204 NORETURN void SnortFatalExit(void);
  205 int SnortSnprintf(char *, size_t, const char *, ...) __attribute__((format (printf, 3, 4)));
  206 int SnortSnprintfAppend(char *, size_t, const char *, ...) __attribute__((format (printf, 3, 4)));
  207 
  208 char *SnortStrdup(const char *);
  209 int SnortStrncpy(char *, const char *, size_t);
  210 char *SnortStrndup(const char *, size_t);
  211 int SnortStrnlen(const char *, int);
  212 const char *SnortStrnPbrk(const char *s, int slen, const char *accept);
  213 const char *SnortStrnStr(const char *s, int slen, const char *searchstr);
  214 const char *SnortStrcasestr(const char *s, int slen, const char *substr);
  215 int CheckValueInRange(const char *value_str, char *option,
  216         unsigned long lo, unsigned long hi, unsigned long *value);
  217 
  218 void *SnortAlloc2(size_t, const char *, ...);
  219 char *CurrentWorkingDir(void);
  220 char *GetAbsolutePath(char *dir);
  221 char *StripPrefixDir(char *prefix, char *dir);
  222 void PrintPacketData(const uint8_t *, const uint32_t);
  223 
  224 char * ObfuscateIpToText(sfaddr_t *);
  225 
  226 #ifndef WIN32
  227 SF_LIST * SortDirectory(const char *);
  228 int GetFilesUnderDir(const char *, SF_QUEUE *, const char *);
  229 #endif
  230 
  231 /***********************************************************
  232  If you use any of the functions in this section, you need
  233  to call free() on the char * that is returned after you are
  234  done using it. Otherwise, you will have created a memory
  235  leak.
  236 ***********************************************************/
  237 char *hex(const u_char *, int);
  238 char *fasthex(const u_char *, int);
  239 int xatol(const char *, const char *);
  240 unsigned int xatou(const char *, const char *);
  241 unsigned int xatoup(const char *, const char *); // return > 0
  242 
  243 static inline void* SnortMalloc (unsigned long size)
  244 {
  245     void* pv = malloc(size);
  246 
  247     if ( pv )
  248         return pv;
  249 
  250     FatalError("Unable to allocate memory!  (%lu requested)\n", size);
  251 
  252     return NULL;
  253 }
  254 
  255 static inline void* SnortAlloc (unsigned long size)
  256 {
  257     void* pv = calloc(size, sizeof(char));
  258 
  259     if ( pv )
  260         return pv;
  261 
  262     FatalError("Unable to allocate memory!  (%lu requested)\n", size);
  263 
  264     return NULL;
  265 }
  266 
  267 static inline long SnortStrtol(const char *nptr, char **endptr, int base)
  268 {
  269     long iRet;
  270     errno = 0;
  271     iRet = strtol(nptr, endptr, base);
  272 
  273     return iRet;
  274 }
  275 
  276 static inline unsigned long SnortStrtoul(const char *nptr, char **endptr, int base)
  277 {
  278         unsigned long iRet;
  279         errno = 0;
  280         iRet = strtoul(nptr, endptr, base);
  281 
  282         return iRet;
  283 }
  284 
  285 // Checks to make sure we're not going to evaluate a negative number for which
  286 // strtoul() gladly accepts and parses returning an underflowed wrapped unsigned
  287 // long without error.
  288 //
  289 // Buffer passed in MUST be NULL terminated.
  290 //
  291 // Returns
  292 //  int
  293 //    -1 if buffer is nothing but spaces or first non-space character is a
  294 //       negative sign.  Also if errno is EINVAL (which may be due to a bad
  295 //       base) or there was nothing to convert.
  296 //     0 on success
  297 //
  298 // Populates pointer to uint32_t value passed in which should
  299 // only be used on a successful return from this function.
  300 //
  301 // Also will set errno to ERANGE on a value returned from strtoul that is
  302 // greater than UINT32_MAX, but still return success.
  303 //
  304 static inline int SnortStrToU32(const char *buffer, char **endptr,
  305         uint32_t *value, int base)
  306 {
  307     unsigned long int tmp;
  308 
  309     if ((buffer == NULL) || (endptr == NULL) || (value == NULL))
  310         return -1;
  311 
  312     // Only positive numbers should be processed and strtoul will
  313     // eat up white space and process '-' and '+' so move past
  314     // white space and check for a negative sign.
  315     while (isspace((int)*buffer))
  316         buffer++;
  317 
  318     // If all spaces or a negative sign is found, return error.
  319     // XXX May also want to exclude '+' as well.
  320     if ((*buffer == '\0') || (*buffer == '-'))
  321         return -1;
  322 
  323     tmp = SnortStrtoul(buffer, endptr, base);
  324 
  325     // The user of the function should check for ERANGE in errno since this
  326     // function can be used such that an ERANGE error is acceptable and
  327     // value gets truncated to UINT32_MAX.
  328     if ((errno == EINVAL) || (*endptr == buffer))
  329         return -1;
  330 
  331     // If value is greater than a UINT32_MAX set value to UINT32_MAX
  332     // and errno to ERANGE
  333     if (tmp > UINT32_MAX)
  334     {
  335         tmp = UINT32_MAX;
  336         errno = ERANGE;
  337     }
  338 
  339     *value = (uint32_t)tmp;
  340 
  341     return 0;
  342 }
  343 
  344 static inline long SnortStrtolRange(const char *nptr, char **endptr, int base, long lo, long hi)
  345 {
  346     long iRet = SnortStrtol(nptr, endptr, base);
  347     if ((iRet > hi) || (iRet < lo))
  348         *endptr = (char *)nptr;
  349 
  350     return iRet;
  351 }
  352 
  353 static inline unsigned long SnortStrtoulRange(const char *nptr, char **endptr, int base, unsigned long lo, unsigned long hi)
  354 {
  355     unsigned long iRet = SnortStrtoul(nptr, endptr, base);
  356     if ((iRet > hi) || (iRet < lo))
  357         *endptr = (char *)nptr;
  358 
  359     return iRet;
  360 }
  361 
  362 static inline int IsEmptyStr(const char *str)
  363 {
  364     const char *end;
  365 
  366     if (str == NULL)
  367         return 1;
  368 
  369     end = str + strlen(str);
  370 
  371     while ((str < end) && isspace((int)*str))
  372         str++;
  373 
  374     if (str == end)
  375         return 1;
  376 
  377     return 0;
  378 }
  379 
  380 #ifndef HAVE_GETTID
  381 static inline pid_t gettid(void)
  382 {
  383 #if defined(LINUX) && defined(SYS_gettid)
  384     return syscall(SYS_gettid);
  385 #else
  386     return getpid();
  387 #endif
  388 }
  389 #endif
  390 
  391 #endif /*__UTIL_H__*/