#include "config.h"#include <assert.h>#include <sys/types.h>#include <netinet/in.h>#include <netinet/in_systm.h>#include <netinet/ip.h>#include <netinet/tcp.h>#include <netinet/udp.h>#include <netinet/ip_icmp.h>#include "util.h"Include dependency graph for net_util.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.
Classes | |
| struct | ip6_hdr |
| struct | subnet_type |
Defines | |
| #define | NUM_ADDR_WORDS 1 |
Typedefs | |
| typedef uint32 | addr_type |
| typedef const uint32 | const_addr_type |
Functions | |
| int | seq_between (uint32 a, uint32 b, uint32 c) |
| int | seq_delta (uint32 a, uint32 b) |
| int | ones_complement_checksum (const void *p, int b, uint32 sum) |
| int | tcp_checksum (const struct ip *ip, const struct tcphdr *tp, int len) |
| int | udp_checksum (const struct ip *ip, const struct udphdr *up, int len) |
| int | icmp_checksum (const struct icmp *icmpp, int len) |
| uint32 | addr_to_net (uint32 addr) |
| char | addr_to_class (uint32 addr) |
| const char * | dotted_addr (uint32 addr, int alternative=0) |
| const char * | dotted_addr (const uint32 *addr, int alternative=0) |
| const char * | dotted_net (uint32 addr) |
| const char * | dotted_net6 (const uint32 *addr) |
| uint32 | dotted_to_addr (const char *addr_text) |
| uint32 * | dotted_to_addr6 (const char *addr_text) |
| int | is_v4_addr (const uint32 addr[4]) |
| uint32 | to_v4_addr (const uint32 *addr) |
| uint32 | mask_addr (uint32 a, uint32 top_bits_to_keep) |
| const uint32 * | mask_addr (const uint32 *a, uint32 top_bits_to_keep) |
| void | copy_addr (const uint32 *src_a, uint32 *dst_a) |
| int | addr_eq (const uint32 *a1, const uint32 *a2) |
| int | subnet_eq (const subnet_type *s1, const subnet_type *s2) |
| uint32 | extract_uint32 (const u_char *data) |
| double | ntohd (double d) |
| double | htond (double d) |
|
|
Definition at line 73 of file net_util.h. Referenced by PacketFilter::AddDst(), PacketFilter::AddSrc(), PrefixTable::Insert(), PrefixTable::Lookup(), PacketFilter::Match(), TCP_Connection::NextPacket(), PrefixTable::Remove(), PacketFilter::RemoveDst(), and PacketFilter::RemoveSrc(). |
|
|
|
Definition at line 72 of file net_util.h. Referenced by PrefixTable::Insert(), PrefixTable::Lookup(), and PrefixTable::Remove(). |
|
||||||||||||
|
Definition at line 152 of file net_util.h. References uint32. Referenced by DNS_Mgr::AddrListDelta(), NetSessions::DoNextPacket(), RuleMatcher::InitEndpoint(), ConnCompressor::Instantiate(), and same_atomic_val().
00153 {
00154 #ifdef BROv6
00155 return a1[0] == a2[0] &&
00156 a1[1] == a2[1] &&
00157 a1[2] == a2[2] &&
00158 a1[3] == a2[3];
00159 #else
00160 return a1[0] == a2[0];
00161 #endif
00162 }
|
|
|
Definition at line 121 of file net_util.cc. References addr, CHECK_CLASS, CLASS_B, CLASS_C, CLASS_D, CLASS_E, and uint32. Referenced by AnonymizeIPAddr::PreserveNet().
00122 {
00123 if ( CHECK_CLASS(addr, CLASS_E) )
00124 return 'E';
00125 else if ( CHECK_CLASS(addr, CLASS_D) )
00126 return 'D';
00127 else if ( CHECK_CLASS(addr, CLASS_C) )
00128 return 'C';
00129 else if ( CHECK_CLASS(addr, CLASS_B) )
00130 return 'B';
00131 else
00132 return 'A';
00133 }
|
|
|
Definition at line 135 of file net_util.cc. References addr, CHECK_CLASS, CLASS_B, CLASS_C, CLASS_D, and uint32. Referenced by NetVal::NetVal().
00136 {
00137 if ( CHECK_CLASS(addr, CLASS_D) )
00138 ; // class D's are left alone ###
00139 else if ( CHECK_CLASS(addr, CLASS_C) )
00140 addr = addr & 0xffffff00;
00141 else if ( CHECK_CLASS(addr, CLASS_B) )
00142 addr = addr & 0xffff0000;
00143 else
00144 addr = addr & 0xff000000;
00145
00146 return addr;
00147 }
|
|
||||||||||||
|
Definition at line 140 of file net_util.h. References uint32. Referenced by ConnID::BuildConnKey(), Connection::Connection(), TCP_Connection::NextPacket(), and ConnCompressor::PendingConnToPacket().
00141 {
00142 #ifdef BROv6
00143 dst_a[0] = src_a[0];
00144 dst_a[1] = src_a[1];
00145 dst_a[2] = src_a[2];
00146 dst_a[3] = src_a[3];
00147 #else
00148 dst_a[0] = src_a[0];
00149 #endif
00150 }
|
|
||||||||||||
|
Definition at line 162 of file net_util.cc. References addr, is_v4_addr(), to_v4_addr(), and uint32. Referenced by MutableVal::Bind(), UDP_Connection::Describe(), TCP_Connection::Describe(), Packet::Describe(), ICMP_Connection::Describe(), dotted_net6(), DNS_Mgr::DumpAddrList(), DNS_Mgr::LookupAddr(), AnonymizeIPAddr_A50::PreservePrefix(), DNS_Mapping::ReqStr(), DNS_Mapping::Save(), dce_rpc_endpoint_addr::to_string(), SubNetVal::ValDescribe(), and Val::ValDescribe().
00163 {
00164 #ifdef BROv6
00165 if ( is_v4_addr(addr) )
00166 return dotted_addr(addr[3], alternative);
00167
00168 static char buf[256];
00169
00170 if ( inet_ntop(AF_INET6, addr, buf, sizeof buf) == NULL )
00171 return "<bad IPv6 address conversion>";
00172
00173 return buf;
00174
00175 #else
00176 return dotted_addr(to_v4_addr(addr), alternative);
00177 #endif
00178 }
|
|
||||||||||||
|
Definition at line 149 of file net_util.cc. References addr, fmt, snprintf(), and uint32.
|
|
|
Definition at line 180 of file net_util.cc. References addr, CHECK_CLASS, CLASS_C, CLASS_D, and uint32. Referenced by NetVal::ValDescribe().
00181 {
00182 addr = ntohl(addr);
00183
00184 static char buf[32];
00185
00186 if ( CHECK_CLASS(addr, CLASS_D) )
00187 sprintf(buf, "%d.%d.%d.%d",
00188 addr >> 24, (addr >> 16) & 0xff,
00189 (addr >> 8) & 0xff, addr & 0xff);
00190
00191 else if ( CHECK_CLASS(addr, CLASS_C) )
00192 sprintf(buf, "%d.%d.%d",
00193 addr >> 24, (addr >> 16) & 0xff, (addr >> 8) & 0xff);
00194
00195 else
00196 // Same for class A's and B's.
00197 sprintf(buf, "%d.%d", addr >> 24, (addr >> 16) & 0xff);
00198
00199 return buf;
00200 }
|
|
|
Definition at line 202 of file net_util.cc. References addr, dotted_addr(), and uint32. Referenced by NetVal::ValDescribe().
00203 {
00204 // ### this isn't right, but net's should go away eventually ...
00205 return dotted_addr(addr);
00206 }
|
|
|
Definition at line 208 of file net_util.cc. References addr, error(), sscanf(), and uint32. Referenced by AddrVal::AddrVal(), and DNS_Mapping::DNS_Mapping().
00209 {
00210 int addr[4];
00211
00212 if ( sscanf(addr_text,
00213 "%d.%d.%d.%d", addr+0, addr+1, addr+2, addr+3) != 4 )
00214 {
00215 error("bad dotted address:", addr_text );
00216 return 0;
00217 }
00218
00219 if ( addr[0] < 0 || addr[1] < 0 || addr[2] < 0 || addr[3] < 0 ||
00220 addr[0] > 255 || addr[1] > 255 || addr[2] > 255 || addr[3] > 255 )
00221 {
00222 error("bad dotted address:", addr_text);
00223 return 0;
00224 }
00225
00226 uint32 a = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3];
00227
00228 // ### perhaps do gethostbyaddr here?
00229
00230 return uint32(htonl(a));
00231 }
|
|
|
Referenced by AddrVal::AddrVal(). |
|
|
Definition at line 334 of file net_util.cc. Referenced by TCP_Connection::BuildOSVal().
|
|
|
Definition at line 212 of file net_util.h. References ntohd(). Referenced by SocketComm::ProcessParentMessage(), SocketComm::ProcessRemoteMessage(), RemoteSerializer::SendPing(), and BinarySerializationFormat::Write().
00212 { return ntohd(d); }
|
|
||||||||||||
|
Definition at line 98 of file net_util.cc. References len, ones_complement_checksum(), and uint32. Referenced by ICMP_Connection::NextPacket().
00099 {
00100 uint32 sum;
00101
00102 if ( len % 2 == 1 )
00103 // Add in pad byte.
00104 sum = htons(((const u_char*) icmpp)[len - 1] << 8);
00105 else
00106 sum = 0;
00107
00108 sum = ones_complement_checksum((void*) icmpp, len, sum);
00109
00110 return sum;
00111 }
|
|
|
Referenced by RemoteSerializer::Connect(), dotted_addr(), SubNetVal::Init(), RemoteSerializer::Listen(), mask_addr(), to_v4_addr(), and SubNetVal::ValDescribe(). |
|
||||||||||||
|
Definition at line 289 of file net_util.cc. References addr, error(), is_v4_addr(), and uint32. Referenced by SubNetVal::Init().
00290 {
00291 #ifdef BROv6
00292 static uint32 addr[4];
00293
00294 addr[0] = a[0];
00295 addr[1] = a[1];
00296 addr[2] = a[2];
00297 addr[3] = a[3];
00298
00299 // This is a bit dicey: if it's a v4 address, then we interpret
00300 // the mask as being with respect to 32 bits total, even though
00301 // strictly speaking, the v4 address comprises the least-significant
00302 // bits out of 128, rather than the most significant. However,
00303 // we only do this if the mask itself is consistent for a 32-bit
00304 // address.
00305 uint32 max_bits = (is_v4_addr(a) && top_bits_to_keep <= 32) ? 32 : 128;
00306
00307 if ( top_bits_to_keep == 0 || top_bits_to_keep > max_bits )
00308 {
00309 error("bad address mask value", top_bits_to_keep);
00310 return addr;
00311 }
00312
00313 int word = 3; // start zeroing out with word #3
00314 int bits_to_chop = max_bits - top_bits_to_keep; // bits to discard
00315 while ( bits_to_chop >= 32 )
00316 { // there's an entire word to discard
00317 addr[word] = 0;
00318 --word; // move on to next, more significant word
00319 bits_to_chop -= 32; // we just go rid of 32 bits
00320 }
00321
00322 // All that's left to work with now is the word pointed to by "word".
00323 uint32 addr32 = ntohl(addr[word]);
00324 addr32 >>= bits_to_chop;
00325 addr32 <<= bits_to_chop;
00326 addr[word] = htonl(addr32);
00327
00328 return addr;
00329 #else
00330 return a;
00331 #endif
00332 }
|
|
||||||||||||
|
Definition at line 266 of file net_util.cc. References addr, error(), and uint32.
00267 {
00268 if ( top_bits_to_keep > 32 )
00269 {
00270 error("bad address mask value", top_bits_to_keep);
00271 return a;
00272 }
00273
00274 if ( top_bits_to_keep == 0 )
00275 // The shifts below don't have any effect with 0, i.e.,
00276 // 1 << 32 does not yield 0; either due to compiler
00277 // misoptimization or language semantics.
00278 return 0;
00279
00280 uint32 addr = ntohl(a);
00281
00282 int shift = 32 - top_bits_to_keep;
00283 addr >>= shift;
00284 addr <<= shift;
00285
00286 return htonl(addr);
00287 }
|
|
|
Definition at line 192 of file net_util.h. Referenced by htond(), SocketComm::ProcessParentMessage(), RemoteSerializer::ProcessPongMsg(), SocketComm::ProcessRemoteMessage(), and BinarySerializationFormat::Read().
00193 {
00194 assert(sizeof(d) == 8);
00195
00196 double tmp;
00197 char* src = (char*) &d;
00198 char* dst = (char*) &tmp;
00199
00200 dst[0] = src[7];
00201 dst[1] = src[6];
00202 dst[2] = src[5];
00203 dst[3] = src[4];
00204 dst[4] = src[3];
00205 dst[5] = src[2];
00206 dst[6] = src[1];
00207 dst[7] = src[0];
00208
00209 return tmp;
00210 }
|
|
||||||||||||||||
|
Definition at line 37 of file net_util.cc. Referenced by NetSessions::DoNextPacket(), ICMP_Connection::ExtractICMPContext(), TCP_TracePacket::Finish(), icmp_checksum(), tcp_checksum(), TCP_Endpoint::TCP_Endpoint(), udp_checksum(), and TCP_Endpoint::ValidChecksum().
00038 {
00039 const u_short* sp = (u_short*) p; // better be aligned!
00040
00041 b /= 2; // convert to count of short's
00042
00043 /* No need for endian conversions. */
00044 while ( --b >= 0 )
00045 sum += *sp++;
00046
00047 while ( sum > 0xffff )
00048 sum = (sum & 0xffff) + (sum >> 16);
00049
00050 return sum;
00051 }
|
|
||||||||||||||||
|
Definition at line 93 of file net_util.h. References uint32. Referenced by TCP_Connection::NextPacket().
00094 {
00095 if ( b <= c )
00096 return a >= b && a <= c;
00097 else
00098 return a >= b || a <= c;
00099 }
|
|
||||||||||||
|
Definition at line 102 of file net_util.h. Referenced by TCP_EndpointStats::DataSent(), TCP_Connection::NextPacket(), and TCP_Endpoint::Size().
00103 {
00104 return int(a-b);
00105 }
|
|
||||||||||||
|
Definition at line 164 of file net_util.h. References subnet_type::net, and subnet_type::width. Referenced by same_atomic_val().
|
|
||||||||||||||||
|
Definition at line 53 of file net_util.cc. References ip::ip_dst, len, ones_complement_checksum(), tcphdr::th_off, and uint32. Referenced by TCP_TracePacket::Finish().
00054 {
00055 int tcp_len = tp->th_off * 4 + len;
00056 uint32 sum;
00057
00058 if ( len % 2 == 1 )
00059 // Add in pad byte.
00060 sum = htons(((const u_char*) tp)[tcp_len - 1] << 8);
00061 else
00062 sum = 0;
00063
00064 sum = ones_complement_checksum((void*) &ip->ip_src.s_addr, 4, sum);
00065 sum = ones_complement_checksum((void*) &ip->ip_dst.s_addr, 4, sum);
00066
00067 uint32 addl_pseudo =
00068 (htons(IPPROTO_TCP) << 16) | htons((unsigned short) tcp_len);
00069
00070 sum = ones_complement_checksum((void*) &addl_pseudo, 4, sum);
00071 sum = ones_complement_checksum((void*) tp, tcp_len, sum);
00072
00073 return sum;
00074 }
|
|
|
Definition at line 255 of file net_util.cc. References addr, internal_error(), is_v4_addr(), and uint32. Referenced by RemoteSerializer::Connect(), dotted_addr(), is_mapped_dce_rpc_endpoint(), TCP_Rewriter::LeaveAddrInTheClear(), RemoteSerializer::Listen(), and TCP_Rewriter::TCP_Rewriter().
00256 {
00257 #ifdef BROv6
00258 if ( ! is_v4_addr(addr) )
00259 internal_error("conversion of non-IPv4 address to IPv4 address");
00260 return addr[3];
00261 #else
00262 return addr[0];
00263 #endif
00264 }
|
|
||||||||||||||||
|
Definition at line 76 of file net_util.cc. References ip::ip_dst, len, ones_complement_checksum(), and uint32. Referenced by UDP_Connection::NextPacket().
00077 {
00078 uint32 sum;
00079
00080 if ( len % 2 == 1 )
00081 // Add in pad byte.
00082 sum = htons(((const u_char*) up)[len - 1] << 8);
00083 else
00084 sum = 0;
00085
00086 sum = ones_complement_checksum((void*) &ip->ip_src.s_addr, 4, sum);
00087 sum = ones_complement_checksum((void*) &ip->ip_dst.s_addr, 4, sum);
00088
00089 uint32 addl_pseudo =
00090 (htons(IPPROTO_UDP) << 16) | htons((unsigned short) len);
00091
00092 sum = ones_complement_checksum((void*) &addl_pseudo, 4, sum);
00093 sum = ones_complement_checksum((void*) up, len, sum);
00094
00095 return sum;
00096 }
|
1.3.5