Clean up PortManager class, mark PortVal ctors deprecated.

Moved PortVal ctors back to public API, but deprecated, just in
case it helps give any external code a chance to adapt.
This commit is contained in:
Jon Siwek 2017-12-12 11:35:55 -06:00
parent 054c4a67c4
commit d6d7f33f5c
2 changed files with 36 additions and 14 deletions

View file

@ -768,7 +768,7 @@ PortManager::PortManager()
auto port_type = (TransportProto)i; auto port_type = (TransportProto)i;
for ( auto j = 0u; j < arr.size(); ++j ) for ( auto j = 0u; j < arr.size(); ++j )
arr[j] = new PortVal(j, port_type); arr[j] = new PortVal(Mask(j, port_type), true);
} }
} }
@ -807,34 +807,45 @@ PortVal* PortManager::Get(uint32 port_num, TransportProto port_type) const
return rval; return rval;
} }
PortVal::PortVal(uint32 p, TransportProto port_type) : Val(TYPE_PORT) uint32 PortManager::Mask(uint32 port_num, TransportProto port_type) const
{ {
// Note, for ICMP one-way connections: // Note, for ICMP one-way connections:
// src_port = icmp_type, dst_port = icmp_code. // src_port = icmp_type, dst_port = icmp_code.
if ( p >= 65536 ) if ( port_num >= 65536 )
{ {
InternalWarning("bad port number"); reporter->Warning("bad port number %d", port_num);
p = 0; port_num = 0;
} }
switch ( port_type ) { switch ( port_type ) {
case TRANSPORT_TCP: case TRANSPORT_TCP:
p |= TCP_PORT_MASK; port_num |= TCP_PORT_MASK;
break; break;
case TRANSPORT_UDP: case TRANSPORT_UDP:
p |= UDP_PORT_MASK; port_num |= UDP_PORT_MASK;
break; break;
case TRANSPORT_ICMP: case TRANSPORT_ICMP:
p |= ICMP_PORT_MASK; port_num |= ICMP_PORT_MASK;
break; break;
default: default:
break; // "other" break; // "unknown/other"
} }
return port_num;
}
PortVal::PortVal(uint32 p, TransportProto port_type) : Val(TYPE_PORT)
{
auto port_num = port_mgr->Mask(p, port_type);
val.uint_val = static_cast<bro_uint_t>(port_num);
}
PortVal::PortVal(uint32 p, bool unused) : Val(TYPE_PORT)
{
val.uint_val = static_cast<bro_uint_t>(p); val.uint_val = static_cast<bro_uint_t>(p);
} }

View file

@ -510,9 +510,15 @@ public:
~PortManager(); ~PortManager();
// Port number given in host order. // Port number given in host order.
PortVal* Get(uint32 port_num) const;
PortVal* Get(uint32 port_num, TransportProto port_type) const; PortVal* Get(uint32 port_num, TransportProto port_type) const;
// Host-order port number already masked with port space protocol mask.
PortVal* Get(uint32 port_num) const;
// Returns a masked port number
uint32 Mask(uint32 port_num, TransportProto port_type) const;
private:
std::array<std::array<PortVal*, 65536>, NUM_PORT_SPACES> ports; std::array<std::array<PortVal*, 65536>, NUM_PORT_SPACES> ports;
}; };
@ -520,6 +526,14 @@ extern PortManager* port_mgr;
class PortVal : public Val { class PortVal : public Val {
public: public:
// Port number given in host order.
BRO_DEPRECATED("use port_mgr->Get() instead")
PortVal(uint32 p, TransportProto port_type);
// Host-order port number already masked with port space protocol mask.
BRO_DEPRECATED("use port_mgr->Get() instead")
PortVal(uint32 p);
Val* SizeVal() const override { return new Val(val.uint_val, TYPE_INT); } Val* SizeVal() const override { return new Val(val.uint_val, TYPE_INT); }
// Returns the port number in host order (not including the mask). // Returns the port number in host order (not including the mask).
@ -546,10 +560,7 @@ protected:
friend class Val; friend class Val;
friend class PortManager; friend class PortManager;
PortVal() {} PortVal() {}
// Constructors - both take the port number in host order. PortVal(uint32 p, bool unused);
PortVal(uint32 p, TransportProto port_type);
PortVal(uint32 p); // used for already-massaged port value.
void ValDescribe(ODesc* d) const override; void ValDescribe(ODesc* d) const override;