1 #ifndef IP_PORT_CONNECTION_H_7C9BC74A_523B_11E2_A779_2F696D9D5D2F_
2 #define IP_PORT_CONNECTION_H_7C9BC74A_523B_11E2_A779_2F696D9D5D2F_
4 // Copyright (c) 2012, Miriam Ruiz <miriam@debian.org>. All rights reserved.
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
9 // 1. Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
12 // 2. Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS", AND ANY EXPRESS
17 // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
19 // NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <netinet/in.h>
34 // IPv4 would be: IpPort<in_addr_t,u_int16_t>
35 template <typename NETID
, typename PORT
>
37 NETID addr
; // IP address
43 IpPort(const NETID
&a
, const PORT
&p
) : addr(a
), port(p
) {
46 void set(const NETID
&a
, const PORT
&p
) {
50 bool operator< (const IpPort
<NETID
, PORT
> &other
) const {
51 if ( port
< other
.port
) return true; // Check Port First
52 if ( port
> other
.port
) return false;
53 if ( addr
< other
.addr
) return true; // If Ports are equal, then Check IPs
54 if ( addr
> other
.addr
) return false;
58 inline bool operator>= (const IpPort
<NETID
, PORT
> &other
) const {
59 return ! operator< (other
);
62 bool operator<= (const IpPort
<NETID
, PORT
> &other
) const {
63 if ( port
< other
.port
) return true; // Check Port First
64 if ( port
> other
.port
) return false;
65 if ( addr
< other
.addr
) return true; // If Ports are equal, then Check IPs
66 if ( addr
> other
.addr
) return false;
70 inline bool operator> (const IpPort
<NETID
, PORT
> &other
) const {
71 return ! operator<= (other
);
74 bool operator== (const IpPort
<NETID
, PORT
> &other
) const {
75 if ( addr
!= other
.addr
) return false;
76 if ( port
!= other
.port
) return false;
80 inline bool operator!= (const IpPort
<NETID
, PORT
> &other
) const {
81 return ! operator== (other
);
86 // IPv4 would be: IpPortConnection<in_addr_t,u_int16_t>
87 template <typename NETID
, typename PORT
>
88 struct IpPortConnection
{
89 IpPort
<NETID
, PORT
> low
;
90 IpPort
<NETID
, PORT
> high
;
92 IpPortConnection (const NETID
&saddr
, const PORT
&sport
, const NETID
&daddr
, const PORT
&dport
);
94 bool less (const IpPortConnection
<NETID
, PORT
> &other
, bool equal
) const {
95 if ( low
< other
.low
) return true; // Check Lover ConnID First
96 if ( low
> other
.low
) return false;
97 if ( high
< other
.high
) return true; // If Low ConnIDs are equal, then check High ConnID
98 if ( high
> other
.high
) return false;
102 bool equal (const IpPortConnection
<NETID
, PORT
> &other
) const {
103 if ( low
!= other
.low
) return false;
104 if ( high
!= other
.high
) return false;
108 inline bool operator< (const IpPortConnection
<NETID
, PORT
> &other
) const {
109 return less (other
, false);
112 bool operator<= (const IpPortConnection
<NETID
, PORT
> &other
) const {
113 return less (other
, true);
116 bool operator> (const IpPortConnection
<NETID
, PORT
> &other
) const {
117 return ! less (other
, true);
120 bool operator>= (const IpPortConnection
<NETID
, PORT
> &other
) const {
121 return ! less (other
, false);
124 bool operator== (const IpPortConnection
<NETID
, PORT
> &other
) const {
125 return equal (other
);
128 bool operator!= (const IpPortConnection
<NETID
, PORT
> &other
) const {
129 return ! equal (other
);
132 bool lower_than(const NETID
&saddr
, const PORT
&sport
, const NETID
&daddr
, const PORT
&dport
) const;
135 template <typename NETID
, typename PORT
>
136 IpPortConnection
<NETID
,PORT
>::IpPortConnection(const NETID
&saddr
, const PORT
&sport
, const NETID
&daddr
, const PORT
&dport
) {
137 if (sport
< dport
) { // Source port is lower
138 low
.set(saddr
, sport
); high
.set(daddr
, dport
);
139 } else if (sport
> dport
) { // Dest port is higher
140 high
.set(saddr
, sport
); low
.set(daddr
, dport
);
141 } else if (saddr
< daddr
) { // Source IP is lower
142 low
.set(saddr
, sport
); high
.set(daddr
, dport
);
143 } else if (saddr
> daddr
) { // Dest IP is higher
144 high
.set(saddr
, sport
); low
.set(daddr
, dport
);
145 } else { // They are equal
146 high
.set(saddr
, sport
); low
.set(daddr
, dport
);
150 template <typename NETID
, typename PORT
>
151 bool IpPortConnection
<NETID
,PORT
>::lower_than(const NETID
&saddr
, const PORT
&sport
, const NETID
&daddr
, const PORT
&dport
) const {
152 IpPort
<NETID
,PORT
> src(saddr
, sport
);
153 IpPort
<NETID
,PORT
> dst(daddr
, dport
);
155 const IpPort
<NETID
,PORT
> *other_low
= &src
;
156 const IpPort
<NETID
,PORT
> *other_high
= &dst
;
157 if (*other_low
> *other_high
) {
162 if ( low
< *other_low
) return true;
163 if ( low
> *other_low
) return false;
164 if ( high
< *other_high
) return true;
165 if ( high
> *other_high
) return false;
169 template <typename NETID
, typename PORT
>
170 inline std::ostream
& operator<< (std::ostream
& out
, const IpPortConnection
<NETID
,PORT
> & a
) {
171 out
<< a
.low
.addr
<< ":" << a
.low
.port
<< " <-> " << a
.high
.addr
<< ":" << a
.high
.port
;
175 } // namespace filter
177 #endif // IP_PORT_CONNECTION_H_7C9BC74A_523B_11E2_A779_2F696D9D5D2F_