LCOV - code coverage report
Current view: top level - src - net_util.h (source / functions) Hit Total Coverage
Test: app.info Lines: 24 26 92.3 %
Date: 2010-12-13 Functions: 6 7 85.7 %
Branches: 5 14 35.7 %

           Branch data     Line data    Source code
       1                 :            : // $Id: net_util.h 6219 2008-10-01 05:39:07Z vern $
       2                 :            : //
       3                 :            : // See the file "COPYING" in the main distribution directory for copyright.
       4                 :            : 
       5                 :            : #ifndef netutil_h
       6                 :            : #define netutil_h
       7                 :            : 
       8                 :            : #include "config.h"
       9                 :            : 
      10                 :            : #include <assert.h>
      11                 :            : #include <sys/types.h>
      12                 :            : 
      13                 :            : #include <netinet/in.h>
      14                 :            : #include <netinet/in_systm.h>
      15                 :            : #include <arpa/inet.h>
      16                 :            : 
      17                 :            : #include <netinet/ip.h>
      18                 :            : #ifdef HAVE_LINUX
      19                 :            : #define __FAVOR_BSD
      20                 :            : #endif
      21                 :            : #include <netinet/tcp.h>
      22                 :            : #include <netinet/udp.h>
      23                 :            : #include <netinet/ip_icmp.h>
      24                 :            : 
      25                 :            : #include "util.h"
      26                 :            : 
      27                 :            : #ifdef HAVE_NETINET_IP6_H
      28                 :            : #include <netinet/ip6.h>
      29                 :            : #else
      30                 :            : struct ip6_hdr {
      31                 :            :         uint16          ip6_plen;
      32                 :            :         uint8           ip6_nxt;
      33                 :            :         uint8           ip6_hlim;
      34                 :            : };
      35                 :            : #endif
      36                 :            : 
      37                 :            : #include "util.h"
      38                 :            : 
      39                 :            : #ifdef BROv6
      40                 :            : typedef uint32* addr_type;      // a pointer to 4 uint32's
      41                 :            : typedef const uint32* const_addr_type;
      42                 :            : #define NUM_ADDR_WORDS 4
      43                 :            : 
      44                 :            : typedef struct {
      45                 :            :         uint32 net[4];
      46                 :            :         uint32 width;
      47                 :            : } subnet_type;
      48                 :            : 
      49                 :            : #else
      50                 :            : typedef uint32 addr_type;
      51                 :            : typedef const uint32 const_addr_type;
      52                 :            : #define NUM_ADDR_WORDS 1
      53                 :            : 
      54                 :            : typedef struct {
      55                 :            :         uint32 net;
      56                 :            :         uint32 width;
      57                 :            : } subnet_type;
      58                 :            : 
      59                 :            : #endif
      60                 :            : 
      61                 :            : // For Solaris.
      62                 :            : #if !defined(TCPOPT_WINDOW) && defined(TCPOPT_WSCALE)
      63                 :            : #define TCPOPT_WINDOW TCPOPT_WSCALE
      64                 :            : #endif
      65                 :            : 
      66                 :            : #if !defined(TCPOPT_TIMESTAMP) && defined(TCPOPT_TSTAMP)
      67                 :            : #define TCPOPT_TIMESTAMP TCPOPT_TSTAMP
      68                 :            : #endif
      69                 :            : 
      70                 :            : // True if sequence # a is between b and c (b <= a <= c).  It must be true
      71                 :            : // that b <= c in the sequence space.
      72                 :        448 : inline int seq_between(uint32 a, uint32 b, uint32 c)
      73                 :            :         {
      74         [ +  - ]:        448 :         if ( b <= c )
      75 [ +  + ][ +  + ]:        448 :                 return a >= b && a <= c;
      76                 :            :         else
      77 [ #  # ][ #  # ]:        448 :                 return a >= b || a <= c;
      78                 :            :         }
      79                 :            : 
      80                 :            : // Returns a - b, adjusted for sequence wraparound.
      81                 :     170930 : inline int seq_delta(uint32 a, uint32 b)
      82                 :            :         {
      83                 :     170930 :         return int(a-b);
      84                 :            :         }
      85                 :            : 
      86                 :            : // Returns the ones-complement checksum of a chunk of b short-aligned bytes.
      87                 :            : extern int ones_complement_checksum(const void* p, int b, uint32 sum);
      88                 :            : 
      89                 :            : extern int tcp_checksum(const struct ip* ip, const struct tcphdr* tp, int len);
      90                 :            : extern int udp_checksum(const struct ip* ip, const struct udphdr* up, int len);
      91                 :            : #ifdef BROv6
      92                 :            : extern int udp6_checksum(const struct ip6_hdr* ip, const struct udphdr* up,
      93                 :            :                                 int len);
      94                 :            : #endif
      95                 :            : extern int icmp_checksum(const struct icmp* icmpp, int len);
      96                 :            : 
      97                 :            : // Given an address in host order, returns its "classical network prefix",
      98                 :            : // also in host order.
      99                 :            : extern uint32 addr_to_net(uint32 addr);
     100                 :            : // Returns 'A', 'B', 'C' or 'D'
     101                 :            : extern char addr_to_class(uint32 addr);
     102                 :            : 
     103                 :            : // Returns a pointer to static storage giving the ASCII dotted representation
     104                 :            : // of the given address, which should be passed in network order.
     105                 :            : extern const char* dotted_addr(uint32 addr, int alternative=0);
     106                 :            : extern const char* dotted_addr(const uint32* addr, int alternative=0);
     107                 :            : 
     108                 :            : // Same, but for the network prefix.
     109                 :            : extern const char* dotted_net(uint32 addr);
     110                 :            : extern const char* dotted_net6(const uint32* addr);
     111                 :            : 
     112                 :            : // Given an ASCII dotted representation, returns the corresponding address
     113                 :            : // in network order.
     114                 :            : extern uint32 dotted_to_addr(const char* addr_text);
     115                 :            : extern uint32* dotted_to_addr6(const char* addr_text);
     116                 :            : 
     117                 :            : extern int is_v4_addr(const uint32 addr[4]);
     118                 :            : extern uint32 to_v4_addr(const uint32* addr);
     119                 :            : 
     120                 :            : extern uint32 mask_addr(uint32 a, uint32 top_bits_to_keep);
     121                 :            : extern const uint32* mask_addr(const uint32* a, uint32 top_bits_to_keep);
     122                 :            : 
     123                 :            : extern const char* fmt_conn_id(const uint32* src_addr, uint32 src_port,
     124                 :            :                                 const uint32* dst_addr, uint32 dst_port);
     125                 :            : 
     126                 :      43810 : inline void copy_addr(const uint32* src_a, uint32* dst_a)
     127                 :            :         {
     128                 :            : #ifdef BROv6
     129                 :            :         dst_a[0] = src_a[0];
     130                 :            :         dst_a[1] = src_a[1];
     131                 :            :         dst_a[2] = src_a[2];
     132                 :            :         dst_a[3] = src_a[3];
     133                 :            : #else
     134                 :      43810 :         dst_a[0] = src_a[0];
     135                 :            : #endif
     136                 :      43810 :         }
     137                 :            : 
     138                 :      21318 : inline int addr_eq(const uint32* a1, const uint32* a2)
     139                 :            :         {
     140                 :            : #ifdef BROv6
     141                 :            :         return a1[0] == a2[0] &&
     142                 :            :                 a1[1] == a2[1] &&
     143                 :            :                 a1[2] == a2[2] &&
     144                 :            :                 a1[3] == a2[3];
     145                 :            : #else
     146                 :      21318 :         return a1[0] == a2[0];
     147                 :            : #endif
     148                 :            :         }
     149                 :            : 
     150                 :          0 : inline int subnet_eq(const subnet_type* s1, const subnet_type* s2)
     151                 :            :         {
     152                 :            : #ifdef BROv6
     153                 :            :         return s1->net[0] == s2->net[0] &&
     154                 :            :                 s1->net[1] == s2->net[1] &&
     155                 :            :                 s1->net[2] == s2->net[2] &&
     156                 :            :                 s1->net[3] == s2->net[3] &&
     157                 :            :                 s1->width == s2->width;
     158                 :            : #else
     159 [ #  # ][ #  # ]:          0 :         return s1->net == s2->net && s1->width == s2->width;
     160                 :            : #endif
     161                 :            :         }
     162                 :            : 
     163                 :            : // Read 4 bytes from data and return in network order.
     164                 :            : extern uint32 extract_uint32(const u_char* data);
     165                 :            : 
     166                 :            : // Endian conversions for double.
     167                 :            : // This is certainly not a very clean solution but should work on the
     168                 :            : // major platforms. Alternativly, we could use a string format or the
     169                 :            : // XDR library.
     170                 :            : 
     171                 :            : #ifdef WORDS_BIGENDIAN
     172                 :            : 
     173                 :            : inline double ntohd(double d)   { return d; }
     174                 :            : inline double htond(double d)   { return d; }
     175                 :            : 
     176                 :            : #else
     177                 :            : 
     178                 :          4 : inline double ntohd(double d)
     179                 :            :         {
     180                 :            :         assert(sizeof(d) == 8);
     181                 :            : 
     182                 :            :         double tmp;
     183                 :          4 :         char* src = (char*) &d;
     184                 :          4 :         char* dst = (char*) &tmp;
     185                 :            : 
     186                 :          4 :         dst[0] = src[7];
     187                 :          4 :         dst[1] = src[6];
     188                 :          4 :         dst[2] = src[5];
     189                 :          4 :         dst[3] = src[4];
     190                 :          4 :         dst[4] = src[3];
     191                 :          4 :         dst[5] = src[2];
     192                 :          4 :         dst[6] = src[1];
     193                 :          4 :         dst[7] = src[0];
     194                 :            : 
     195                 :          4 :         return tmp;
     196                 :            :         }
     197                 :            : 
     198                 :          4 : inline double htond(double d) { return ntohd(d); }
     199                 :            : 
     200                 :            : #endif
     201                 :            : 
     202                 :            : #endif

Generated by: LCOV version 1.8