mirror of
https://github.com/zeek/zeek.git
synced 2025-10-03 07:08:19 +00:00
Reformat Zeek in Spicy style
This largely copies over Spicy's `.clang-format` configuration file. The one place where we deviate is header include order since Zeek depends on headers being included in a certain order.
This commit is contained in:
parent
7b8e7ed72c
commit
f5a76c1aed
786 changed files with 131714 additions and 153609 deletions
377
src/net_util.h
377
src/net_util.h
|
@ -5,21 +5,16 @@
|
|||
#include "zeek/zeek-config.h"
|
||||
|
||||
// Define first.
|
||||
enum TransportProto
|
||||
{
|
||||
TRANSPORT_UNKNOWN,
|
||||
TRANSPORT_TCP,
|
||||
TRANSPORT_UDP,
|
||||
TRANSPORT_ICMP,
|
||||
};
|
||||
enum TransportProto {
|
||||
TRANSPORT_UNKNOWN,
|
||||
TRANSPORT_TCP,
|
||||
TRANSPORT_UDP,
|
||||
TRANSPORT_ICMP,
|
||||
};
|
||||
|
||||
extern const char* transport_proto_string(TransportProto proto);
|
||||
|
||||
enum IPFamily
|
||||
{
|
||||
IPv4,
|
||||
IPv6
|
||||
};
|
||||
enum IPFamily { IPv4, IPv6 };
|
||||
|
||||
// Force these files to stay in this order. Normally, clang-format
|
||||
// wants to move sys/types.h to the end of this block, but that
|
||||
|
@ -45,39 +40,35 @@ enum IPFamily
|
|||
#include <netinet/ip6.h>
|
||||
|
||||
#ifndef HAVE_IP6_OPT
|
||||
struct ip6_opt
|
||||
{
|
||||
uint8_t ip6o_type;
|
||||
uint8_t ip6o_len;
|
||||
};
|
||||
struct ip6_opt {
|
||||
uint8_t ip6o_type;
|
||||
uint8_t ip6o_len;
|
||||
};
|
||||
#endif // HAVE_IP6_OPT
|
||||
|
||||
#ifndef HAVE_IP6_EXT
|
||||
struct ip6_ext
|
||||
{
|
||||
uint8_t ip6e_nxt;
|
||||
uint8_t ip6e_len;
|
||||
};
|
||||
struct ip6_ext {
|
||||
uint8_t ip6e_nxt;
|
||||
uint8_t ip6e_len;
|
||||
};
|
||||
#endif // HAVE_IP6_EXT
|
||||
|
||||
#else
|
||||
|
||||
struct ip6_hdr
|
||||
{
|
||||
union {
|
||||
struct ip6_hdrctl
|
||||
{
|
||||
uint32_t ip6_un1_flow; /* 4 bits version, 8 bits TC, 20 bits
|
||||
flow-ID */
|
||||
uint16_t ip6_un1_plen; /* payload length */
|
||||
uint8_t ip6_un1_nxt; /* next header */
|
||||
uint8_t ip6_un1_hlim; /* hop limit */
|
||||
} ip6_un1;
|
||||
uint8_t ip6_un2_vfc; /* 4 bits version, top 4 bits tclass */
|
||||
} ip6_ctlun;
|
||||
struct in6_addr ip6_src; /* source address */
|
||||
struct in6_addr ip6_dst; /* destination address */
|
||||
};
|
||||
struct ip6_hdr {
|
||||
union {
|
||||
struct ip6_hdrctl {
|
||||
uint32_t ip6_un1_flow; /* 4 bits version, 8 bits TC, 20 bits
|
||||
flow-ID */
|
||||
uint16_t ip6_un1_plen; /* payload length */
|
||||
uint8_t ip6_un1_nxt; /* next header */
|
||||
uint8_t ip6_un1_hlim; /* hop limit */
|
||||
} ip6_un1;
|
||||
uint8_t ip6_un2_vfc; /* 4 bits version, top 4 bits tclass */
|
||||
} ip6_ctlun;
|
||||
struct in6_addr ip6_src; /* source address */
|
||||
struct in6_addr ip6_dst; /* destination address */
|
||||
};
|
||||
|
||||
#define ip6_vfc ip6_ctlun.ip6_un2_vfc
|
||||
#define ip6_flow ip6_ctlun.ip6_un1.ip6_un1_flow
|
||||
|
@ -86,48 +77,42 @@ struct ip6_hdr
|
|||
#define ip6_hlim ip6_ctlun.ip6_un1.ip6_un1_hlim
|
||||
#define ip6_hops ip6_ctlun.ip6_un1.ip6_un1_hlim
|
||||
|
||||
struct ip6_opt
|
||||
{
|
||||
uint8_t ip6o_type;
|
||||
uint8_t ip6o_len;
|
||||
};
|
||||
struct ip6_opt {
|
||||
uint8_t ip6o_type;
|
||||
uint8_t ip6o_len;
|
||||
};
|
||||
|
||||
struct ip6_ext
|
||||
{
|
||||
uint8_t ip6e_nxt;
|
||||
uint8_t ip6e_len;
|
||||
};
|
||||
struct ip6_ext {
|
||||
uint8_t ip6e_nxt;
|
||||
uint8_t ip6e_len;
|
||||
};
|
||||
|
||||
struct ip6_frag
|
||||
{
|
||||
uint8_t ip6f_nxt; /* next header */
|
||||
uint8_t ip6f_reserved; /* reserved field */
|
||||
uint16_t ip6f_offlg; /* offset, reserved, and flag */
|
||||
uint32_t ip6f_ident; /* identification */
|
||||
};
|
||||
struct ip6_frag {
|
||||
uint8_t ip6f_nxt; /* next header */
|
||||
uint8_t ip6f_reserved; /* reserved field */
|
||||
uint16_t ip6f_offlg; /* offset, reserved, and flag */
|
||||
uint32_t ip6f_ident; /* identification */
|
||||
};
|
||||
|
||||
struct ip6_hbh
|
||||
{
|
||||
uint8_t ip6h_nxt; /* next header */
|
||||
uint8_t ip6h_len; /* length in units of 8 octets */
|
||||
/* followed by options */
|
||||
};
|
||||
struct ip6_hbh {
|
||||
uint8_t ip6h_nxt; /* next header */
|
||||
uint8_t ip6h_len; /* length in units of 8 octets */
|
||||
/* followed by options */
|
||||
};
|
||||
|
||||
struct ip6_dest
|
||||
{
|
||||
uint8_t ip6d_nxt; /* next header */
|
||||
uint8_t ip6d_len; /* length in units of 8 octets */
|
||||
/* followed by options */
|
||||
};
|
||||
struct ip6_dest {
|
||||
uint8_t ip6d_nxt; /* next header */
|
||||
uint8_t ip6d_len; /* length in units of 8 octets */
|
||||
/* followed by options */
|
||||
};
|
||||
|
||||
struct ip6_rthdr
|
||||
{
|
||||
uint8_t ip6r_nxt; /* next header */
|
||||
uint8_t ip6r_len; /* length in units of 8 octets */
|
||||
uint8_t ip6r_type; /* routing type */
|
||||
uint8_t ip6r_segleft; /* segments left */
|
||||
/* followed by routing type specific data */
|
||||
};
|
||||
struct ip6_rthdr {
|
||||
uint8_t ip6r_nxt; /* next header */
|
||||
uint8_t ip6r_len; /* length in units of 8 octets */
|
||||
uint8_t ip6r_type; /* routing type */
|
||||
uint8_t ip6r_segleft; /* segments left */
|
||||
/* followed by routing type specific data */
|
||||
};
|
||||
#endif // HAVE_NETINET_IP6_H
|
||||
|
||||
// For Solaris.
|
||||
|
@ -139,62 +124,53 @@ struct ip6_rthdr
|
|||
#define TCPOPT_TIMESTAMP TCPOPT_TSTAMP
|
||||
#endif
|
||||
|
||||
namespace zeek
|
||||
{
|
||||
namespace zeek {
|
||||
|
||||
class IPAddr;
|
||||
class IP_Hdr;
|
||||
|
||||
namespace detail
|
||||
{
|
||||
namespace detail {
|
||||
|
||||
struct checksum_block
|
||||
{
|
||||
const uint8_t* block;
|
||||
int len;
|
||||
};
|
||||
struct checksum_block {
|
||||
const uint8_t* block;
|
||||
int len;
|
||||
};
|
||||
|
||||
struct ipv4_pseudo_hdr
|
||||
{
|
||||
in_addr src;
|
||||
in_addr dst;
|
||||
uint8_t zero;
|
||||
uint8_t next_proto;
|
||||
uint16_t len;
|
||||
};
|
||||
struct ipv4_pseudo_hdr {
|
||||
in_addr src;
|
||||
in_addr dst;
|
||||
uint8_t zero;
|
||||
uint8_t next_proto;
|
||||
uint16_t len;
|
||||
};
|
||||
|
||||
struct ipv6_pseudo_hdr
|
||||
{
|
||||
in6_addr src;
|
||||
in6_addr dst;
|
||||
uint32_t len;
|
||||
uint8_t zero[3];
|
||||
uint8_t next_proto;
|
||||
};
|
||||
struct ipv6_pseudo_hdr {
|
||||
in6_addr src;
|
||||
in6_addr dst;
|
||||
uint32_t len;
|
||||
uint8_t zero[3];
|
||||
uint8_t next_proto;
|
||||
};
|
||||
|
||||
extern uint16_t in_cksum(const checksum_block* blocks, int num_blocks);
|
||||
|
||||
inline uint16_t in_cksum(const uint8_t* data, int len)
|
||||
{
|
||||
checksum_block cb{data, len};
|
||||
return in_cksum(&cb, 1);
|
||||
}
|
||||
inline uint16_t in_cksum(const uint8_t* data, int len) {
|
||||
checksum_block cb{data, len};
|
||||
return in_cksum(&cb, 1);
|
||||
}
|
||||
|
||||
extern uint16_t ip4_in_cksum(const IPAddr& src, const IPAddr& dst, uint8_t next_proto,
|
||||
const uint8_t* data, int len);
|
||||
extern uint16_t ip4_in_cksum(const IPAddr& src, const IPAddr& dst, uint8_t next_proto, const uint8_t* data, int len);
|
||||
|
||||
extern uint16_t ip6_in_cksum(const IPAddr& src, const IPAddr& dst, uint8_t next_proto,
|
||||
const uint8_t* data, int len);
|
||||
extern uint16_t ip6_in_cksum(const IPAddr& src, const IPAddr& dst, uint8_t next_proto, const uint8_t* data, int len);
|
||||
|
||||
inline uint16_t ip_in_cksum(bool is_ipv4, const IPAddr& src, const IPAddr& dst, uint8_t next_proto,
|
||||
const uint8_t* data, int len)
|
||||
{
|
||||
if ( is_ipv4 )
|
||||
return ip4_in_cksum(src, dst, next_proto, data, len);
|
||||
return ip6_in_cksum(src, dst, next_proto, data, len);
|
||||
}
|
||||
inline uint16_t ip_in_cksum(bool is_ipv4, const IPAddr& src, const IPAddr& dst, uint8_t next_proto, const uint8_t* data,
|
||||
int len) {
|
||||
if ( is_ipv4 )
|
||||
return ip4_in_cksum(src, dst, next_proto, data, len);
|
||||
return ip6_in_cksum(src, dst, next_proto, data, len);
|
||||
}
|
||||
|
||||
} // namespace zeek::detail
|
||||
} // namespace detail
|
||||
|
||||
// Returns the ones-complement checksum of a chunk of 'b' bytes.
|
||||
extern int ones_complement_checksum(const void* p, int b, uint32_t sum);
|
||||
|
@ -208,27 +184,22 @@ extern int mobility_header_checksum(const IP_Hdr* ip);
|
|||
|
||||
// True if sequence # a is between b and c (b <= a <= c). It must be true
|
||||
// that b <= c in the sequence space.
|
||||
inline bool seq_between(uint32_t a, uint32_t b, uint32_t c)
|
||||
{
|
||||
if ( b <= c )
|
||||
return a >= b && a <= c;
|
||||
else
|
||||
return a >= b || a <= c;
|
||||
}
|
||||
inline bool seq_between(uint32_t a, uint32_t b, uint32_t c) {
|
||||
if ( b <= c )
|
||||
return a >= b && a <= c;
|
||||
else
|
||||
return a >= b || a <= c;
|
||||
}
|
||||
|
||||
// Returns a - b, adjusted for sequence wraparound.
|
||||
inline int32_t seq_delta(uint32_t a, uint32_t b)
|
||||
{
|
||||
return a - b;
|
||||
}
|
||||
inline int32_t seq_delta(uint32_t a, uint32_t b) { return a - b; }
|
||||
|
||||
// Returns 'A', 'B', 'C' or 'D'
|
||||
extern char addr_to_class(uint32_t addr);
|
||||
|
||||
extern const char* fmt_conn_id(const IPAddr& src_addr, uint32_t src_port, const IPAddr& dst_addr,
|
||||
extern const char* fmt_conn_id(const IPAddr& src_addr, uint32_t src_port, const IPAddr& dst_addr, uint32_t dst_port);
|
||||
extern const char* fmt_conn_id(const uint32_t* src_addr, uint32_t src_port, const uint32_t* dst_addr,
|
||||
uint32_t dst_port);
|
||||
extern const char* fmt_conn_id(const uint32_t* src_addr, uint32_t src_port,
|
||||
const uint32_t* dst_addr, uint32_t dst_port);
|
||||
|
||||
/**
|
||||
* Given a MAC address, formats it in hex as 00:de:ad:be:ef.
|
||||
|
@ -252,114 +223,84 @@ extern uint32_t extract_uint32(const u_char* data);
|
|||
|
||||
#ifdef WORDS_BIGENDIAN
|
||||
|
||||
inline double ntohd(double d)
|
||||
{
|
||||
return d;
|
||||
}
|
||||
inline double htond(double d)
|
||||
{
|
||||
return d;
|
||||
}
|
||||
inline double ntohd(double d) { return d; }
|
||||
inline double htond(double d) { return d; }
|
||||
|
||||
inline float ntohf(float f)
|
||||
{
|
||||
return f;
|
||||
}
|
||||
inline float htonf(float f)
|
||||
{
|
||||
return f;
|
||||
}
|
||||
inline float ntohf(float f) { return f; }
|
||||
inline float htonf(float f) { return f; }
|
||||
|
||||
#ifndef HAVE_BYTEORDER_64
|
||||
inline uint64_t ntohll(uint64_t i)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
inline uint64_t htonll(uint64_t i)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
inline uint64_t ntohll(uint64_t i) { return i; }
|
||||
inline uint64_t htonll(uint64_t i) { return i; }
|
||||
#endif
|
||||
|
||||
#else
|
||||
|
||||
inline double ntohd(double d)
|
||||
{
|
||||
assert(sizeof(d) == 8);
|
||||
inline double ntohd(double d) {
|
||||
assert(sizeof(d) == 8);
|
||||
|
||||
double tmp;
|
||||
char* src = (char*)&d;
|
||||
char* dst = (char*)&tmp;
|
||||
double tmp;
|
||||
char* src = (char*)&d;
|
||||
char* dst = (char*)&tmp;
|
||||
|
||||
dst[0] = src[7];
|
||||
dst[1] = src[6];
|
||||
dst[2] = src[5];
|
||||
dst[3] = src[4];
|
||||
dst[4] = src[3];
|
||||
dst[5] = src[2];
|
||||
dst[6] = src[1];
|
||||
dst[7] = src[0];
|
||||
dst[0] = src[7];
|
||||
dst[1] = src[6];
|
||||
dst[2] = src[5];
|
||||
dst[3] = src[4];
|
||||
dst[4] = src[3];
|
||||
dst[5] = src[2];
|
||||
dst[6] = src[1];
|
||||
dst[7] = src[0];
|
||||
|
||||
return tmp;
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
|
||||
inline double htond(double d)
|
||||
{
|
||||
return ntohd(d);
|
||||
}
|
||||
inline double htond(double d) { return ntohd(d); }
|
||||
|
||||
inline float ntohf(float f)
|
||||
{
|
||||
assert(sizeof(f) == 4);
|
||||
inline float ntohf(float f) {
|
||||
assert(sizeof(f) == 4);
|
||||
|
||||
float tmp;
|
||||
char* src = (char*)&f;
|
||||
char* dst = (char*)&tmp;
|
||||
float tmp;
|
||||
char* src = (char*)&f;
|
||||
char* dst = (char*)&tmp;
|
||||
|
||||
dst[0] = src[3];
|
||||
dst[1] = src[2];
|
||||
dst[2] = src[1];
|
||||
dst[3] = src[0];
|
||||
dst[0] = src[3];
|
||||
dst[1] = src[2];
|
||||
dst[2] = src[1];
|
||||
dst[3] = src[0];
|
||||
|
||||
return tmp;
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
|
||||
inline float htonf(float f)
|
||||
{
|
||||
return ntohf(f);
|
||||
}
|
||||
inline float htonf(float f) { return ntohf(f); }
|
||||
|
||||
#ifndef HAVE_BYTEORDER_64
|
||||
inline uint64_t ntohll(uint64_t i)
|
||||
{
|
||||
u_char c;
|
||||
union {
|
||||
uint64_t i;
|
||||
u_char c[8];
|
||||
} x;
|
||||
inline uint64_t ntohll(uint64_t i) {
|
||||
u_char c;
|
||||
union {
|
||||
uint64_t i;
|
||||
u_char c[8];
|
||||
} x;
|
||||
|
||||
x.i = i;
|
||||
c = x.c[0];
|
||||
x.c[0] = x.c[7];
|
||||
x.c[7] = c;
|
||||
c = x.c[1];
|
||||
x.c[1] = x.c[6];
|
||||
x.c[6] = c;
|
||||
c = x.c[2];
|
||||
x.c[2] = x.c[5];
|
||||
x.c[5] = c;
|
||||
c = x.c[3];
|
||||
x.c[3] = x.c[4];
|
||||
x.c[4] = c;
|
||||
return x.i;
|
||||
}
|
||||
x.i = i;
|
||||
c = x.c[0];
|
||||
x.c[0] = x.c[7];
|
||||
x.c[7] = c;
|
||||
c = x.c[1];
|
||||
x.c[1] = x.c[6];
|
||||
x.c[6] = c;
|
||||
c = x.c[2];
|
||||
x.c[2] = x.c[5];
|
||||
x.c[5] = c;
|
||||
c = x.c[3];
|
||||
x.c[3] = x.c[4];
|
||||
x.c[4] = c;
|
||||
return x.i;
|
||||
}
|
||||
|
||||
inline uint64_t htonll(uint64_t i)
|
||||
{
|
||||
return ntohll(i);
|
||||
}
|
||||
inline uint64_t htonll(uint64_t i) { return ntohll(i); }
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace zeek
|
||||
} // namespace zeek
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue