Working on merging the v6-addr branch. This is checkpoint, tests don't

pass yet.

Changes:

- Gave IPAddress/IPPrefix methods AsString() so that one doesn't need
  to cast to get a string represenation.

- Val::AsAddr()/AsSubnet() return references rather than pointers. I
  find that more intuitive.

- ODesc/Serializer/SerializationFormat get methods to support
  IPAddress/IPPrefix directly.

- Reformatted the comments in IPAddr.h from /// to /** style.

- Given IPPrefix a Contains() method.

- A bit of cleanup.
This commit is contained in:
Robin Sommer 2012-02-16 18:23:26 -08:00
parent 7458ebf385
commit 94b9644da7
20 changed files with 160 additions and 129 deletions

View file

@ -1,3 +1,4 @@
// Main analyzer interface.
#ifndef ANALYZER_H #ifndef ANALYZER_H
#define ANALYZER_H #define ANALYZER_H

View file

@ -109,7 +109,7 @@ char* CompositeHash::SingleValHash(int type_check, char* kp0,
{ {
uint32* kp = AlignAndPadType<uint32>(kp0); uint32* kp = AlignAndPadType<uint32>(kp0);
uint32 bytes[4]; uint32 bytes[4];
v->AsAddr()->CopyIPv6(bytes); v->AsAddr().CopyIPv6(bytes);
kp[0] = bytes[0]; kp[0] = bytes[0];
kp[1] = bytes[1]; kp[1] = bytes[1];
kp[2] = bytes[2]; kp[2] = bytes[2];
@ -122,12 +122,12 @@ char* CompositeHash::SingleValHash(int type_check, char* kp0,
{ {
uint32* kp = AlignAndPadType<uint32>(kp0); uint32* kp = AlignAndPadType<uint32>(kp0);
uint32 bytes[4]; uint32 bytes[4];
v->AsSubNet()->Prefix().CopyIPv6(bytes); v->AsSubNet().Prefix().CopyIPv6(bytes);
kp[0] = bytes[0]; kp[0] = bytes[0];
kp[1] = bytes[1]; kp[1] = bytes[1];
kp[2] = bytes[2]; kp[2] = bytes[2];
kp[3] = bytes[3]; kp[3] = bytes[3];
kp[4] = v->AsSubNet()->Length(); kp[4] = v->AsSubNet().Length();
kp1 = reinterpret_cast<char*>(kp+5); kp1 = reinterpret_cast<char*>(kp+5);
} }
break; break;
@ -354,15 +354,15 @@ HashKey* CompositeHash::ComputeSingletonHash(const Val* v, int type_check) const
case TYPE_INTERNAL_ADDR: case TYPE_INTERNAL_ADDR:
{ {
uint32 bytes[4]; uint32 bytes[4];
v->AsAddr()->CopyIPv6(bytes); v->AsAddr().CopyIPv6(bytes);
return new HashKey((void*)bytes, 4 * sizeof(uint32)); return new HashKey((void*)bytes, 4 * sizeof(uint32));
} }
case TYPE_INTERNAL_SUBNET: case TYPE_INTERNAL_SUBNET:
{ {
uint32 bytes[5]; uint32 bytes[5];
v->AsSubNet()->Prefix().CopyIPv6(bytes); v->AsSubNet().Prefix().CopyIPv6(bytes);
bytes[4] = v->AsSubNet()->Length(); bytes[4] = v->AsSubNet().Length();
return new HashKey((void*)bytes, 5 * sizeof(uint32)); return new HashKey((void*)bytes, 5 * sizeof(uint32));
} }

View file

@ -660,8 +660,8 @@ const IP_Hdr* ConnCompressor::PendingConnToPacket(const PendingConn* c)
IPAddr ip1(IPAddr::IPv6, c->key.ip1, IPAddr::Network); IPAddr ip1(IPAddr::IPv6, c->key.ip1, IPAddr::Network);
IPAddr ip2(IPAddr::IPv6, c->key.ip2, IPAddr::Network); IPAddr ip2(IPAddr::IPv6, c->key.ip2, IPAddr::Network);
if ( ip1.family() == IPAddr::IPv6 || if ( ip1.GetFamily() == IPAddr::IPv6 ||
ip2.family() == IPAddr::IPv6 ) ip2.GetFamily() == IPAddr::IPv6 )
reporter->InternalError("IPv6 snuck into connection compressor"); reporter->InternalError("IPv6 snuck into connection compressor");
else else
{ {

View file

@ -137,7 +137,8 @@ static bool is_mapped_dce_rpc_endpoint(const dce_rpc_endpoint_addr& addr)
bool is_mapped_dce_rpc_endpoint(const ConnID* id, TransportProto proto) bool is_mapped_dce_rpc_endpoint(const ConnID* id, TransportProto proto)
{ {
if ( id->dst_addr.family() == IPAddr::IPv6 ) if ( id->dst_addr.GetFamily() == IPAddr::IPv6 )
// TODO: Does the protocol support v6 addresses? #773
return false; return false;
dce_rpc_endpoint_addr addr; dce_rpc_endpoint_addr addr;

View file

@ -65,7 +65,7 @@ struct dce_rpc_endpoint_addr {
{ {
static char buf[128]; static char buf[128];
snprintf(buf, sizeof(buf), "%s/%d/%s", snprintf(buf, sizeof(buf), "%s/%d/%s",
addr->AsString().c_str(), port, addr.AsString().c_str(), port,
proto == TRANSPORT_TCP ? "tcp" : proto == TRANSPORT_TCP ? "tcp" :
(proto == TRANSPORT_UDP ? "udp" : "?")); (proto == TRANSPORT_UDP ? "udp" : "?"));

View file

@ -347,13 +347,13 @@ void DNS_Mapping::Save(FILE* f) const
{ {
fprintf(f, "%.0f %d %s %d %s %d\n", creation_time, req_host != 0, fprintf(f, "%.0f %d %s %d %s %d\n", creation_time, req_host != 0,
req_host ? req_host : req_host ? req_host :
IPAddr(IPAddr::IPv4, &req_addr, IPAddr::Network)->AsString()->c_str(), IPAddr(IPAddr::IPv4, &req_addr, IPAddr::Network).AsString().c_str(),
failed, (names && names[0]) ? names[0] : "*", failed, (names && names[0]) ? names[0] : "*",
num_addrs); num_addrs);
for ( int i = 0; i < num_addrs; ++i ) for ( int i = 0; i < num_addrs; ++i )
fprintf(f, "%s\n", fprintf(f, "%s\n",
IPAddr(IPAddr::IPv4, &addrs[i], IPAddr::Network)->AsString().c_str()); IPAddr(IPAddr::IPv4, &addrs[i], IPAddr::Network).AsString().c_str());
} }
@ -518,7 +518,7 @@ Val* DNS_Mgr::LookupAddr(uint32 addr)
case DNS_FORCE: case DNS_FORCE:
reporter->FatalError("can't find DNS entry for %s in cache", reporter->FatalError("can't find DNS entry for %s in cache",
IPAddr(IPAddr::IPv4, &addr, IPAddr::Network)->AsString().c_str()); IPAddr(IPAddr::IPv4, &addr, IPAddr::Network).AsString().c_str());
return 0; return 0;
case DNS_DEFAULT: case DNS_DEFAULT:
@ -787,13 +787,13 @@ ListVal* DNS_Mgr::AddrListDelta(ListVal* al1, ListVal* al2)
for ( int i = 0; i < al1->Length(); ++i ) for ( int i = 0; i < al1->Length(); ++i )
{ {
const IPAddr* al1_i = al1->Index(i)->AsAddr(); const IPAddr& al1_i = al1->Index(i)->AsAddr();
int j; int j;
for ( j = 0; j < al2->Length(); ++j ) for ( j = 0; j < al2->Length(); ++j )
{ {
const IPAddr* al2_j = al2->Index(j)->AsAddr(); const IPAddr& al2_j = al2->Index(j)->AsAddr();
if ( *al1_i == *al2_j ) if ( al1_i == al2_j )
break; break;
} }
@ -809,8 +809,8 @@ void DNS_Mgr::DumpAddrList(FILE* f, ListVal* al)
{ {
for ( int i = 0; i < al->Length(); ++i ) for ( int i = 0; i < al->Length(); ++i )
{ {
const IPAddr* al_i = al->Index(i)->AsAddr(); const IPAddr& al_i = al->Index(i)->AsAddr();
fprintf(f, "%s%s", i > 0 ? "," : "", al_i->AsString().c_str()); fprintf(f, "%s%s", i > 0 ? "," : "", al_i.AsString().c_str());
} }
} }

View file

@ -157,18 +157,6 @@ void ODesc::Add(double d)
} }
} }
void ODesc::Add(const IPAddr& addr)
{
string s = addr->AsString();
AddBytes(s.c_str());
}
void ODesc::Add(const IPPrefix& prefix)
{
string s = prefix->AsString();
AddBytes(s.c_str());
}
void ODesc::AddCS(const char* s) void ODesc::AddCS(const char* s)
{ {
int n = strlen(s); int n = strlen(s);

View file

@ -6,7 +6,9 @@
#include <stdio.h> #include <stdio.h>
#include <list> #include <list>
#include <utility> #include <utility>
#include "BroString.h" #include "BroString.h"
#include "IPAddr.h"
typedef enum { typedef enum {
DESC_READABLE, DESC_READABLE,
@ -68,13 +70,14 @@ public:
void Add(const char* s, int do_indent=1); void Add(const char* s, int do_indent=1);
void AddN(const char* s, int len) { AddBytes(s, len); } void AddN(const char* s, int len) { AddBytes(s, len); }
void Add(const string& s) { AddBytes(s.data(), s.size()); }
void Add(int i); void Add(int i);
void Add(uint32 u); void Add(uint32 u);
void Add(int64 i); void Add(int64 i);
void Add(uint64 u); void Add(uint64 u);
void Add(double d); void Add(double d);
void Add(const IPAddr& addr); void Add(const IPAddr& addr) { Add(addr.AsString()); }
void Add(const IPPrefix& prefix); void Add(const IPPrefix& prefix) { Add(prefix.AsString()); }
// Add s as a counted string. // Add s as a counted string.
void AddCS(const char* s); void AddCS(const char* s);

View file

@ -4493,7 +4493,7 @@ Val* InExpr::Fold(Val* v1, Val* v2) const
if ( v1->Type()->Tag() == TYPE_ADDR && if ( v1->Type()->Tag() == TYPE_ADDR &&
v2->Type()->Tag() == TYPE_SUBNET ) v2->Type()->Tag() == TYPE_SUBNET )
return new Val(v2->AsSubNetVal().Contains(v1->AsAddr()), TYPE_BOOL); return new Val(v2->AsSubNetVal()->Contains(v1->AsAddr()), TYPE_BOOL);
TableVal* vt = v2->AsTableVal(); TableVal* vt = v2->AsTableVal();
if ( vt->Lookup(v1, false) ) if ( vt->Lookup(v1, false) )

View file

@ -90,7 +90,7 @@ void IPAddr::Init(const std::string& s)
string IPAddr::AsString() const string IPAddr::AsString() const
{ {
if ( family() == IPv4 ) if ( GetFamily() == IPv4 )
{ {
char s[INET_ADDRSTRLEN]; char s[INET_ADDRSTRLEN];
@ -131,7 +131,7 @@ IPPrefix::IPPrefix(const in6_addr& in6, uint8_t length)
IPPrefix::IPPrefix(const IPAddr& addr, uint8_t length) IPPrefix::IPPrefix(const IPAddr& addr, uint8_t length)
: prefix(addr) : prefix(addr)
{ {
if ( prefix.family() == IPAddr::IPv4 ) if ( prefix.GetFamily() == IPAddr::IPv4 )
{ {
if ( length > 32 ) if ( length > 32 )
reporter->InternalError("Bad IPAddr(v4) IPPrefix length : %d", reporter->InternalError("Bad IPAddr(v4) IPPrefix length : %d",
@ -152,27 +152,15 @@ IPPrefix::IPPrefix(const IPAddr& addr, uint8_t length)
prefix.Mask(this->length); prefix.Mask(this->length);
} }
IPPrefix::IPPrefix(const std::string& s, uint8_t length)
: prefix(s), length(length)
{
if ( prefix.family() == IPAddr::IPv4 && length > 32 )
reporter->InternalError("Bad string IPPrefix length : %d", length);
else if ( prefix.family() == IPAddr::IPv6 && length > 128 )
reporter->InternalError("Bad string IPPrefix length : %d", length);
prefix.Mask(this->length);
}
string IPPrefix::AsString() const string IPPrefix::AsString() const
{ {
char l[16]; char l[16];
if ( prefix.family() == IPAddr::IPv4 ) if ( prefix.GetFamily() == IPAddr::IPv4 )
modp_uitoa10(length - 96, l); modp_uitoa10(length - 96, l);
else else
modp_uitoa10(length, l); modp_uitoa10(length, l);
return prefix->AsString() +"/" + l; return prefix.AsString() +"/" + l;
} }

View file

@ -114,7 +114,7 @@ public:
/** /**
* Returns the address' family. * Returns the address' family.
*/ */
Family family() const Family GetFamily() const
{ {
if ( memcmp(in6.s6_addr, v4_mapped_prefix, 12) == 0 ) if ( memcmp(in6.s6_addr, v4_mapped_prefix, 12) == 0 )
return IPv4; return IPv4;
@ -132,7 +132,7 @@ public:
*/ */
bool IsMulticast() const bool IsMulticast() const
{ {
if ( family() == IPv4 ) if ( GetFamily() == IPv4 )
return in6.s6_addr[12] == 224; return in6.s6_addr[12] == 224;
else else
return in6.s6_addr[0] == 0xff; return in6.s6_addr[0] == 0xff;
@ -143,7 +143,7 @@ public:
*/ */
bool IsBroadcast() const bool IsBroadcast() const
{ {
if ( family() == IPv4 ) if ( GetFamily() == IPv4 )
return ((in6.s6_addr[12] == 0xff) && (in6.s6_addr[13] == 0xff) return ((in6.s6_addr[12] == 0xff) && (in6.s6_addr[13] == 0xff)
&& (in6.s6_addr[14] == 0xff) && (in6.s6_addr[15] == 0xff)); && (in6.s6_addr[14] == 0xff) && (in6.s6_addr[15] == 0xff));
else else
@ -162,9 +162,9 @@ public:
* @return The number of 32-bit words the raw representation uses. This * @return The number of 32-bit words the raw representation uses. This
* will be 1 for an IPv4 address and 4 for an IPv6 address. * will be 1 for an IPv4 address and 4 for an IPv6 address.
*/ */
int GetBytes(const uint32_t* const * bytes) const int GetBytes(const uint32_t** bytes) const
{ {
if ( family() == IPv4 ) if ( GetFamily() == IPv4 )
{ {
*bytes = (uint32_t*) &in6.s6_addr[12]; *bytes = (uint32_t*) &in6.s6_addr[12];
return 1; return 1;
@ -306,7 +306,7 @@ inline IPAddr::IPAddr(Family family, const uint32_t* bytes, ByteOrder order)
inline bool IPAddr::IsLoopback() const inline bool IPAddr::IsLoopback() const
{ {
if ( family() == IPv4 ) if ( GetFamily() == IPv4 )
return in6.s6_addr[12] == 127; return in6.s6_addr[12] == 127;
else else
@ -327,6 +327,12 @@ inline bool IPAddr::IsLoopback() const
class IPPrefix class IPPrefix
{ {
public: public:
/**
* Constructs a prefix 0/0.
*/
IPPrefix() : length(0) {}
/** /**
* Constructs a prefix instance from an IPv4 address and a prefix * Constructs a prefix instance from an IPv4 address and a prefix
* length. * length.
@ -356,16 +362,6 @@ public:
*/ */
IPPrefix(const IPAddr& addr, uint8_t length); IPPrefix(const IPAddr& addr, uint8_t length);
/**
* Constructs a prefix instance from IP string representation and length.
*
* @param s String containing an IP address as either a dotted IPv4
* address or a hex IPv6 address.
*
* @param length The prefix length in the range from 0 to 128
*/
IPPrefix(const std::string& s, uint8_t length);
/** /**
* Copy constructor. * Copy constructor.
*/ */
@ -389,7 +385,7 @@ public:
*/ */
uint8_t Length() const uint8_t Length() const
{ {
return prefix.family() == IPAddr::IPv4 ? length - 96 : length; return prefix.GetFamily() == IPAddr::IPv4 ? length - 96 : length;
} }
/** /**
@ -398,6 +394,16 @@ public:
*/ */
uint8_t LengthIPv6() const { return length; } uint8_t LengthIPv6() const { return length; }
/** Returns true if the given address is part of the prefix.
*
* @param addr The address to test.
*/
bool Contains(const IPAddr& addr) const
{
IPAddr p(addr);
p.Mask(length);
return p == prefix;
}
/** /**
* Assignment operator. * Assignment operator.
*/ */

View file

@ -200,7 +200,7 @@ bool LogVal::Read(SerializationFormat* fmt)
case TYPE_SUBNET: case TYPE_SUBNET:
{ {
IPPrefix prefix; IPPrefix prefix;
if ( ! fmt->Read(&prefix), "subnet" ) if ( ! fmt->Read(&prefix, "subnet") )
return false; return false;
val.subnet_val = new IPPrefix(prefix); val.subnet_val = new IPPrefix(prefix);
@ -210,10 +210,10 @@ bool LogVal::Read(SerializationFormat* fmt)
case TYPE_ADDR: case TYPE_ADDR:
{ {
IPAddr addr; IPAddr addr;
if ( ! fmt->Read(&addr), "net" ) if ( ! fmt->Read(&addr, "net") )
return false; return false;
val.addr_val = new IPAddr(prefix); val.addr_val = new IPAddr(addr);
return true; return true;
} }

View file

@ -680,7 +680,7 @@ RemoteSerializer::PeerID RemoteSerializer::Connect(const IPAddr& ip,
if ( ! initialized ) if ( ! initialized )
reporter->InternalError("remote serializer not initialized"); reporter->InternalError("remote serializer not initialized");
if ( ip.family() == IPAddr::IPv6 ) if ( ip.GetFamily() == IPAddr::IPv6 )
Error("inter-Bro communication not supported over IPv6"); Error("inter-Bro communication not supported over IPv6");
const uint32* bytes; const uint32* bytes;
@ -1237,7 +1237,7 @@ bool RemoteSerializer::Listen(const IPAddr& ip, uint16 port, bool expect_ssl)
if ( ! initialized ) if ( ! initialized )
reporter->InternalError("remote serializer not initialized"); reporter->InternalError("remote serializer not initialized");
if ( ip.family() == IPAddr::IPv6 ) if ( ip.GetFamily() == IPAddr::IPv6 )
Error("inter-Bro communication not supported over IPv6"); Error("inter-Bro communication not supported over IPv6");
const uint32* bytes; const uint32* bytes;

View file

@ -1071,7 +1071,7 @@ static bool val_to_maskedval(Val* v, maskedvalue_list* append_to)
const uint32* n; const uint32* n;
uint32 m[4]; uint32 m[4];
v->AsSubNet().Prefix().GetBytes(&n); v->AsSubNet().Prefix().GetBytes(&n);
v->AsSubNetVal().Mask().CopyIPv6(m); v->AsSubNetVal()->Mask().CopyIPv6(m);
for ( unsigned int i = 0; i < 4; ++i ) for ( unsigned int i = 0; i < 4; ++i )
m[i] = ntohl(m[i]); m[i] = ntohl(m[i]);
@ -1079,7 +1079,7 @@ static bool val_to_maskedval(Val* v, maskedvalue_list* append_to)
bool is_v4_mask = m[0] == 0xffffffff && bool is_v4_mask = m[0] == 0xffffffff &&
m[1] == m[0] && m[2] == m[0]; m[1] == m[0] && m[2] == m[0];
if ( v->AsSubNet().Prefix().family() == IPAddr::IPv4 && if ( v->AsSubNet().Prefix().GetFamily() == IPAddr::IPv4 &&
is_v4_mask ) is_v4_mask )
{ {
mval->val = ntohl(*n); mval->val = ntohl(*n);

View file

@ -232,11 +232,29 @@ bool BinarySerializationFormat::Read(string* v, const char* tag)
bool BinarySerializationFormat::Read(IPAddr* addr, const char* tag) bool BinarySerializationFormat::Read(IPAddr* addr, const char* tag)
{ {
string s; int n = 0;
if ( ! Read(&s, tag) ) if ( ! Read(&n, "addr-len") )
return false; return false;
*addr = IPAddr(s); if ( n != 1 && n != 4 )
return false;
uint32_t raw[4];
for ( int i = 0; i < n; ++i )
{
uint32_t i = 0;
if ( ! Read(&i, "addr-part") )
return false;
raw[n] = htonl(i);
}
if ( n == 1 )
*addr = IPAddr(IPAddr::IPv4, raw, IPAddr::Network);
else
*addr = IPAddr(IPAddr::IPv6, raw, IPAddr::Network);
return true; return true;
} }
@ -323,12 +341,26 @@ bool BinarySerializationFormat::Write(const string& s, const char* tag)
bool BinarySerializationFormat::Write(const IPAddr& addr, const char* tag) bool BinarySerializationFormat::Write(const IPAddr& addr, const char* tag)
{ {
return Write(addr.AsString()); const uint32_t* raw;
int n = addr.GetBytes(&raw);
assert(n == 1 || n == 4);
if ( ! Write(n, "addr-len") )
return false;
for ( int i = 0; i < n; ++i )
{
if ( ! Write(ntohl(raw[i]), "addr-part") )
return false;
}
return true;
} }
bool BinarySerializationFormat::Write(const IPPrefix& prefix, const char* tag) bool BinarySerializationFormat::Write(const IPPrefix& prefix, const char* tag)
{ {
return Write(addr.AsString(), tag) && Write(prefix->Length(), tag); return Write(prefix.Prefix(), "prefix") && Write(prefix.Length(), "width");
} }
bool BinarySerializationFormat::WriteOpenTag(const char* tag) bool BinarySerializationFormat::WriteOpenTag(const char* tag)
@ -421,6 +453,18 @@ bool XMLSerializationFormat::Read(string* s, const char* tag)
return false; return false;
} }
bool XMLSerializationFormat::Read(IPAddr* addr, const char* tag)
{
reporter->InternalError("no reading of xml");
return false;
}
bool XMLSerializationFormat::Read(IPPrefix* prefix, const char* tag)
{
reporter->InternalError("no reading of xml");
return false;
}
bool XMLSerializationFormat::Write(char v, const char* tag) bool XMLSerializationFormat::Write(char v, const char* tag)
{ {
return WriteElem(tag, "char", &v, 1); return WriteElem(tag, "char", &v, 1);
@ -501,6 +545,18 @@ bool XMLSerializationFormat::Write(const char* buf, int len, const char* tag)
return WriteElem(tag, "string", buf, len); return WriteElem(tag, "string", buf, len);
} }
bool XMLSerializationFormat::Write(const IPAddr& addr, const char* tag)
{
reporter->InternalError("XML output of addresses not implemented");
return false;
}
bool XMLSerializationFormat::Write(const IPPrefix& prefix, const char* tag)
{
reporter->InternalError("XML output of prefixes not implemented");
return false;
}
bool XMLSerializationFormat::WriteEncodedString(const char* s, int len) bool XMLSerializationFormat::WriteEncodedString(const char* s, int len)
{ {
while ( len-- ) while ( len-- )

View file

@ -94,6 +94,8 @@ public:
virtual bool Read(double* d, const char* tag); virtual bool Read(double* d, const char* tag);
virtual bool Read(char** str, int* len, const char* tag); virtual bool Read(char** str, int* len, const char* tag);
virtual bool Read(string* s, const char* tag); virtual bool Read(string* s, const char* tag);
virtual bool Read(IPAddr* addr, const char* tag);
virtual bool Read(IPPrefix* prefix, const char* tag);
virtual bool Write(int v, const char* tag); virtual bool Write(int v, const char* tag);
virtual bool Write(uint16 v, const char* tag); virtual bool Write(uint16 v, const char* tag);
virtual bool Write(uint32 v, const char* tag); virtual bool Write(uint32 v, const char* tag);
@ -105,6 +107,8 @@ public:
virtual bool Write(const char* s, const char* tag); virtual bool Write(const char* s, const char* tag);
virtual bool Write(const char* buf, int len, const char* tag); virtual bool Write(const char* buf, int len, const char* tag);
virtual bool Write(const string& s, const char* tag); virtual bool Write(const string& s, const char* tag);
virtual bool Write(const IPAddr& addr, const char* tag);
virtual bool Write(const IPPrefix& prefix, const char* tag);
virtual bool WriteOpenTag(const char* tag); virtual bool WriteOpenTag(const char* tag);
virtual bool WriteCloseTag(const char* tag); virtual bool WriteCloseTag(const char* tag);
virtual bool WriteSeparator(); virtual bool WriteSeparator();
@ -127,6 +131,8 @@ public:
virtual bool Write(const char* s, const char* tag); virtual bool Write(const char* s, const char* tag);
virtual bool Write(const char* buf, int len, const char* tag); virtual bool Write(const char* buf, int len, const char* tag);
virtual bool Write(const string& s, const char* tag); virtual bool Write(const string& s, const char* tag);
virtual bool Write(const IPAddr& addr, const char* tag);
virtual bool Write(const IPPrefix& prefix, const char* tag);
virtual bool WriteOpenTag(const char* tag); virtual bool WriteOpenTag(const char* tag);
virtual bool WriteCloseTag(const char* tag); virtual bool WriteCloseTag(const char* tag);
virtual bool WriteSeparator(); virtual bool WriteSeparator();
@ -142,6 +148,8 @@ public:
virtual bool Read(double* d, const char* tag); virtual bool Read(double* d, const char* tag);
virtual bool Read(char** str, int* len, const char* tag); virtual bool Read(char** str, int* len, const char* tag);
virtual bool Read(string* s, const char* tag); virtual bool Read(string* s, const char* tag);
virtual bool Read(IPAddr* addr, const char* tag);
virtual bool Read(IPPrefix* prefix, const char* tag);
private: private:
// Encodes non-printable characters. // Encodes non-printable characters.

View file

@ -69,6 +69,8 @@ public:
{ return format->Read(const_cast<char**>(str), len, tag); } { return format->Read(const_cast<char**>(str), len, tag); }
bool Read(string* s, const char* tag); bool Read(string* s, const char* tag);
bool Read(IPAddr* a, const char* tag) { return format->Read(a, tag); }
bool Read(IPPrefix* p, const char* tag) { return format->Read(p, tag); }
bool Write(const char* s, const char* tag) bool Write(const char* s, const char* tag)
{ return format->Write(s, tag); } { return format->Write(s, tag); }
@ -76,6 +78,8 @@ public:
{ return format->Write(buf, len, tag); } { return format->Write(buf, len, tag); }
bool Write(const string& s, const char* tag) bool Write(const string& s, const char* tag)
{ return format->Write(s.data(), s.size(), tag); } { return format->Write(s.data(), s.size(), tag); }
bool Write(const IPAddr& a, const char* tag) { return format->Write(a, tag); }
bool Write(const IPPrefix& p, const char* tag) { return format->Write(p, tag); }
bool WriteOpenTag(const char* tag) bool WriteOpenTag(const char* tag)
{ return format->WriteOpenTag(tag); } { return format->WriteOpenTag(tag); }

View file

@ -861,18 +861,16 @@ AddrVal::AddrVal(const char* text) : Val(TYPE_ADDR)
val.addr_val = new IPAddr(text); val.addr_val = new IPAddr(text);
} }
#if 0
AddrVal::AddrVal(uint32 addr) : Val(TYPE_ADDR) AddrVal::AddrVal(uint32 addr) : Val(TYPE_ADDR)
{ {
// ### perhaps do gethostbyaddr here? // ### perhaps do gethostbyaddr here?
val.addr_val = new IPAddr(IPAddr::IPv4, &addr, IPAddr::Network); val.addr_val = new IPAddr(IPAddr::IPv4, &addr, IPAddr::Network);
} }
AddrVal::AddrVal(const uint32* addr) : Val(TYPE_ADDR) AddrVal::AddrVal(const uint32 addr[4]) : Val(TYPE_ADDR)
{ {
val.addr_val = new IPAddr(IPAddr::IPv6, addr, IPAddr::Network); val.addr_val = new IPAddr(IPAddr::IPv6, addr, IPAddr::Network);
} }
#endif
AddrVal::AddrVal(const IPAddr& addr) : Val(TYPE_ADDR) AddrVal::AddrVal(const IPAddr& addr) : Val(TYPE_ADDR)
{ {
@ -891,7 +889,7 @@ unsigned int AddrVal::MemoryAllocation() const
Val* AddrVal::SizeVal() const Val* AddrVal::SizeVal() const
{ {
if ( val.addr_val->family() == IPAddr::IPv4 ) if ( val.addr_val->GetFamily() == IPAddr::IPv4 )
return new Val(32, TYPE_COUNT); return new Val(32, TYPE_COUNT);
else else
return new Val(128, TYPE_COUNT); return new Val(128, TYPE_COUNT);
@ -965,7 +963,6 @@ void SubNetVal::ValDescribe(ODesc* d) const
d->Add(string(*val.subnet_val).c_str()); d->Add(string(*val.subnet_val).c_str());
} }
#if 0
IPAddr SubNetVal::Mask() const IPAddr SubNetVal::Mask() const
{ {
if ( val.subnet_val->Length() == 0 ) if ( val.subnet_val->Length() == 0 )
@ -995,19 +992,6 @@ IPAddr SubNetVal::Mask() const
return rval; return rval;
} }
bool SubNetVal::Contains(const uint32 addr) const
{
IPAddr a(IPAddr::IPv4, &addr, IPAddr::Network);
return val.subnet_val->Contains(a);
}
bool SubNetVal::Contains(const uint32* addr) const
{
IPAddr a(IPAddr::IPv6, addr, IPAddr::Network);
return val.subnet_val->Contains(a);
}
#endif
bool SubNetVal::Contains(const IPAddr& addr) const bool SubNetVal::Contains(const IPAddr& addr) const
{ {
IPAddr a(addr); IPAddr a(addr);

View file

@ -261,17 +261,17 @@ public:
ACCESSOR(TYPE_PATTERN, RE_Matcher*, re_val, AsPattern) ACCESSOR(TYPE_PATTERN, RE_Matcher*, re_val, AsPattern)
ACCESSOR(TYPE_VECTOR, vector<Val*>*, vector_val, AsVector) ACCESSOR(TYPE_VECTOR, vector<Val*>*, vector_val, AsVector)
IPPrefix* AsSubNet() const IPPrefix& AsSubNet()
{ {
CHECK_TAG(type->Tag(), TYPE_SUBNET, "Val::SubNet", type_name) CHECK_TAG(type->Tag(), TYPE_SUBNET, "Val::SubNet", type_name)
return val.subnet_val; return *val.subnet_val;
} }
IPAddr* AsAddr() const IPAddr& AsAddr()
{ {
if ( type->Tag() != TYPE_ADDR ) if ( type->Tag() != TYPE_ADDR )
BadTag("Val::AsAddr", type_name(type->Tag())); BadTag("Val::AsAddr", type_name(type->Tag()));
return val.addr_val; return *val.addr_val;
} }
// Gives fast access to the bits of something that is one of // Gives fast access to the bits of something that is one of
@ -562,10 +562,8 @@ public:
Val* SizeVal() const; Val* SizeVal() const;
// Constructor for address already in network order. // Constructor for address already in network order.
#if 0 AddrVal(uint32 addr); // IPv4.
AddrVal(uint32 addr); AddrVal(const uint32 addr[4]); // IPv6.
AddrVal(const uint32* addr);
#endif
AddrVal(const IPAddr& addr); AddrVal(const IPAddr& addr);
unsigned int MemoryAllocation() const; unsigned int MemoryAllocation() const;
@ -583,10 +581,8 @@ class SubNetVal : public Val {
public: public:
SubNetVal(const char* text); SubNetVal(const char* text);
SubNetVal(const char* text, int width); SubNetVal(const char* text, int width);
#if 0 SubNetVal(uint32 addr, int width); // IPv4.
SubNetVal(uint32 addr, int width); SubNetVal(const uint32 addr[4], int width); // IPv6.
SubNetVal(const uint32* addr, int width);
#endif
SubNetVal(const IPAddr& addr, int width); SubNetVal(const IPAddr& addr, int width);
~SubNetVal(); ~SubNetVal();
@ -594,11 +590,7 @@ public:
const IPAddr& Prefix() const { return val.subnet_val->Prefix(); } const IPAddr& Prefix() const { return val.subnet_val->Prefix(); }
int Width() const { return val.subnet_val->Length(); } int Width() const { return val.subnet_val->Length(); }
#if 0
IPAddr Mask() const; IPAddr Mask() const;
bool Contains(const uint32 addr) const;
bool Contains(const uint32* addr) const;
#endif
bool Contains(const IPAddr& addr) const; bool Contains(const IPAddr& addr) const;

View file

@ -181,7 +181,7 @@ static void do_fmt(const char*& fmt, Val* v, ODesc* d)
const IPAddr& u = v->AsAddr(); const IPAddr& u = v->AsAddr();
const uint32* net_order_u; const uint32* net_order_u;
int len = u->GetBytes(&net_order_u); int len = u.GetBytes(&net_order_u);
if ( len == 4 ) if ( len == 4 )
{ {
@ -1963,7 +1963,7 @@ function do_profiling%(%) : any
## Returns: True if *ip* belongs to a local interface. ## Returns: True if *ip* belongs to a local interface.
function is_local_interface%(ip: addr%) : bool function is_local_interface%(ip: addr%) : bool
%{ %{
if ( ip->AsAddr()->IsLoopback() ) if ( ip->AsAddr().IsLoopback() )
return new Val(1, TYPE_BOOL); return new Val(1, TYPE_BOOL);
list<IPAddr> addrs; list<IPAddr> addrs;
@ -2054,7 +2054,7 @@ function gethostname%(%) : string
## Returns: true if *a* is an IPv4 address, else false. ## Returns: true if *a* is an IPv4 address, else false.
function is_v4_addr%(a: addr%): bool function is_v4_addr%(a: addr%): bool
%{ %{
if ( a->AsAddr()->family() == IPAddr::IPv4 ) if ( a->AsAddr().GetFamily() == IPAddr::IPv4 )
return new Val(1, TYPE_BOOL); return new Val(1, TYPE_BOOL);
else else
return new Val(0, TYPE_BOOL); return new Val(0, TYPE_BOOL);
@ -2067,7 +2067,7 @@ function is_v4_addr%(a: addr%): bool
## Returns: true if *a* is an IPv6 address, else false. ## Returns: true if *a* is an IPv6 address, else false.
function is_v6_addr%(a: addr%): bool function is_v6_addr%(a: addr%): bool
%{ %{
if ( a->AsAddr().Family() == IPAddr::IPv6 ) if ( a->AsAddr().GetFamily() == IPAddr::IPv6 )
return new Val(1, TYPE_BOOL); return new Val(1, TYPE_BOOL);
else else
return new Val(0, TYPE_BOOL); return new Val(0, TYPE_BOOL);
@ -2091,7 +2091,7 @@ function addr_to_counts%(a: addr%): index_vec
%{ %{
VectorVal* rval = new VectorVal(new VectorType(base_type(TYPE_COUNT))); VectorVal* rval = new VectorVal(new VectorType(base_type(TYPE_COUNT)));
const uint32* bytes; const uint32* bytes;
int len = a->AsAddr()->GetBytes(&bytes); int len = a->AsAddr().GetBytes(&bytes);
for ( int i = 0; i < len; ++i ) for ( int i = 0; i < len; ++i )
rval->Assign(i, new Val(ntohl(bytes[i]), TYPE_COUNT), 0); rval->Assign(i, new Val(ntohl(bytes[i]), TYPE_COUNT), 0);
@ -2446,7 +2446,7 @@ function ptr_name_to_addr%(s: string%): addr
function addr_to_ptr_name%(a: addr%): string function addr_to_ptr_name%(a: addr%): string
%{ %{
const uint32* addr; const uint32* addr;
int len = a->AsAddr()->GetBytes(&addr); int len = a->AsAddr().GetBytes(&addr);
if ( len == 1 ) if ( len == 1 )
{ {
@ -2564,7 +2564,7 @@ static Val* parse_eftp(const char* line)
{ {
string s(line); string s(line);
IPAddr tmp(s); IPAddr tmp(s);
uint32* bytes; const uint32* bytes;
tmp.GetBytes(&bytes); tmp.GetBytes(&bytes);
addr = *bytes; addr = *bytes;
if ( addr == 0 ) if ( addr == 0 )
@ -2672,7 +2672,7 @@ function parse_ftp_epsv%(str: string%): ftp_port
function fmt_ftp_port%(a: addr, p: port%): string function fmt_ftp_port%(a: addr, p: port%): string
%{ %{
const uint32* addr; const uint32* addr;
int len = a->AsAddr()->GetBytes(&addr); int len = a->AsAddr().GetBytes(&addr);
if ( len == 1 ) if ( len == 1 )
{ {
uint32 a = ntohl(addr[0]); uint32 a = ntohl(addr[0]);
@ -3441,7 +3441,7 @@ function lookup_addr%(host: addr%) : string
frame->SetDelayed(); frame->SetDelayed();
trigger->Hold(); trigger->Hold();
if ( host->AsAddr().Family() != IPAddr::IPv4 ) if ( host->AsAddr().GetFamily() != IPAddr::IPv4 )
{ {
// FIXME: This is a temporary work-around until we get this // FIXME: This is a temporary work-around until we get this
// fixed. We warn the user once, and always trigger a timeout. // fixed. We warn the user once, and always trigger a timeout.
@ -3567,7 +3567,7 @@ function lookup_location%(a: addr%) : geo_location
} }
#ifdef HAVE_GEOIP_COUNTRY_EDITION_V6 #ifdef HAVE_GEOIP_COUNTRY_EDITION_V6
if ( geoip_v6 && a->AsAddr().Family() == IPAddr::IPv6 ) if ( geoip_v6 && a->AsAddr().GetFamily() == IPAddr::IPv6 )
{ {
const uint32* bytes; const uint32* bytes;
a->AsAddr().GetBytes(&bytes); a->AsAddr().GetBytes(&bytes);
@ -3581,7 +3581,7 @@ function lookup_location%(a: addr%) : geo_location
else else
#endif #endif
if ( geoip && a->AsAddr().Family() == IPAddr::IPv4 ) if ( geoip && a->AsAddr().GetFamily() == IPAddr::IPv4 )
{ {
const uint32* bytes; const uint32* bytes;
a->AsAddr().GetBytes(&bytes); a->AsAddr().GetBytes(&bytes);
@ -3664,7 +3664,7 @@ function lookup_asn%(a: addr%) : count
{ {
// IPv6 support showed up in 1.4.5. // IPv6 support showed up in 1.4.5.
#ifdef HAVE_GEOIP_COUNTRY_EDITION_V6 #ifdef HAVE_GEOIP_COUNTRY_EDITION_V6
if ( a->AsAddr()->family() == IPAddr::IPv6 ) if ( a->AsAddr().GetFamily() == IPAddr::IPv6 )
{ {
const uint32* bytes; const uint32* bytes;
a->AsAddr().GetBytes(&bytes); a->AsAddr().GetBytes(&bytes);
@ -3675,7 +3675,7 @@ function lookup_asn%(a: addr%) : count
else else
#endif #endif
if ( a->AsAddr().Family() == IPAddr::IPv4 ) if ( a->AsAddr().GetFamily() == IPAddr::IPv4 )
{ {
const uint32* bytes; const uint32* bytes;
a->AsAddr().GetBytes(&bytes); a->AsAddr().GetBytes(&bytes);
@ -5402,7 +5402,7 @@ function preserve_prefix%(a: addr, width: count%): any
AnonymizeIPAddr* ip_anon = ip_anonymizer[PREFIX_PRESERVING_A50]; AnonymizeIPAddr* ip_anon = ip_anonymizer[PREFIX_PRESERVING_A50];
if ( ip_anon ) if ( ip_anon )
{ {
if ( a->AsAddr().Family() == IPAddr::IPv6 ) if ( a->AsAddr().GetFamily() == IPAddr::IPv6 )
builtin_error("preserve_prefix() not supported for IPv6 addresses"); builtin_error("preserve_prefix() not supported for IPv6 addresses");
else else
{ {
@ -5431,13 +5431,13 @@ function preserve_subnet%(a: subnet%): any
AnonymizeIPAddr* ip_anon = ip_anonymizer[PREFIX_PRESERVING_A50]; AnonymizeIPAddr* ip_anon = ip_anonymizer[PREFIX_PRESERVING_A50];
if ( ip_anon ) if ( ip_anon )
{ {
if ( a->AsSubNet()->Prefix().Family() == IPAddr::IPv6 ) if ( a->AsSubNet().Prefix().GetFamily() == IPAddr::IPv6 )
builtin_error("preserve_subnet() not supported for IPv6 addresses"); builtin_error("preserve_subnet() not supported for IPv6 addresses");
else else
{ {
const uint32* bytes; const uint32* bytes;
a->AsSubNet().Prefix().GetBytes(&bytes); a->AsSubNet().Prefix().GetBytes(&bytes);
ip_anon->PreservePrefix(*bytes, a->AsSubNet()->Length()); ip_anon->PreservePrefix(*bytes, a->AsSubNet().Length());
} }
} }
@ -5467,7 +5467,7 @@ function anonymize_addr%(a: addr, cl: IPAddrAnonymizationClass%): addr
if ( anon_class < 0 || anon_class >= NUM_ADDR_ANONYMIZATION_CLASSES ) if ( anon_class < 0 || anon_class >= NUM_ADDR_ANONYMIZATION_CLASSES )
builtin_error("anonymize_addr(): invalid ip addr anonymization class"); builtin_error("anonymize_addr(): invalid ip addr anonymization class");
if ( a->AsAddr().Family() == IPAddr::IPv6 ) if ( a->AsAddr().GetFamily() == IPAddr::IPv6 )
{ {
builtin_error("anonymize_addr() not supported for IPv6 addresses"); builtin_error("anonymize_addr() not supported for IPv6 addresses");
return 0; return 0;