1 /*****************************************************************
5 | Copyright (c) 2002-2008, Axiomatic Systems, LLC.
8 | Redistribution and use in source and binary forms, with or without
9 | modification, are permitted provided that the following conditions are met:
10 | * Redistributions of source code must retain the above copyright
11 | notice, this list of conditions and the following disclaimer.
12 | * 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.
15 | * Neither the name of Axiomatic Systems nor the
16 | names of its contributors may be used to endorse or promote products
17 | derived from this software without specific prior written permission.
19 | THIS SOFTWARE IS PROVIDED BY AXIOMATIC SYSTEMS ''AS IS'' AND ANY
20 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | DISCLAIMED. IN NO EVENT SHALL AXIOMATIC SYSTEMS BE LIABLE FOR ANY
23 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 ****************************************************************/
32 #ifndef _NPT_NETWORK_H_
33 #define _NPT_NETWORK_H_
35 /*----------------------------------------------------------------------
37 +---------------------------------------------------------------------*/
39 #include "NptConstants.h"
40 #include "NptStrings.h"
43 /*----------------------------------------------------------------------
45 +---------------------------------------------------------------------*/
46 const unsigned int NPT_NETWORK_MAX_MAC_ADDRESS_LENGTH
= 8;
48 /*----------------------------------------------------------------------
50 +---------------------------------------------------------------------*/
51 #define NPT_NETWORK_INTERFACE_FLAG_LOOPBACK 0x01
52 #define NPT_NETWORK_INTERFACE_FLAG_PROMISCUOUS 0x02
53 #define NPT_NETWORK_INTERFACE_FLAG_BROADCAST 0x04
54 #define NPT_NETWORK_INTERFACE_FLAG_MULTICAST 0x08
55 #define NPT_NETWORK_INTERFACE_FLAG_POINT_TO_POINT 0x10
57 /*----------------------------------------------------------------------
59 +---------------------------------------------------------------------*/
66 /*----------------------------------------------------------------------
68 +---------------------------------------------------------------------*/
69 typedef unsigned int NPT_IpPort
;
71 /*----------------------------------------------------------------------
73 +---------------------------------------------------------------------*/
84 static const NPT_IpAddress Any
;
85 static const NPT_IpAddress Loopback
;
87 // constructors and destructor
89 NPT_IpAddress(Type type
);
90 NPT_IpAddress(unsigned long address
);
91 NPT_IpAddress(unsigned char a
, unsigned char b
, unsigned char c
, unsigned char d
);
92 NPT_IpAddress(Type type
, const unsigned char* address
, unsigned int size
, NPT_UInt32 scope_id
= 0);
95 Type
GetType() const { return m_Type
; }
96 NPT_UInt32
GetScopeId() const { return m_ScopeId
; }
99 NPT_Result
ResolveName(const char* name
,
100 NPT_Timeout timeout
= NPT_TIMEOUT_INFINITE
);
101 NPT_Result
Parse(const char* name
);
102 NPT_Result
Set(unsigned long address
);
103 NPT_Result
Set(const unsigned char bytes
[4]);
104 NPT_Result
Set(const unsigned char* bytes
, unsigned int size
, NPT_UInt32 scope_id
= 0);
105 const unsigned char* AsBytes() const;
106 unsigned long AsLong() const;
107 NPT_String
ToString() const;
108 NPT_String
ToUrlHost() const;
110 // address properties
111 bool IsUnspecified() const;
112 bool IsLooppack() const;
113 bool IsV4Compatible() const;
114 bool IsV4Mapped() const;
115 bool IsLinkLocal() const;
116 bool IsSiteLocal() const;
117 bool IsUniqueLocal() const;
118 bool IsMulticast() const;
121 bool operator==(const NPT_IpAddress
& other
) const;
124 NPT_String m_HostName
;
129 unsigned char m_Address
[16];
130 NPT_UInt32 m_ScopeId
; // IPv6 only
133 /*----------------------------------------------------------------------
135 +---------------------------------------------------------------------*/
148 // constructors and destructor
149 NPT_MacAddress() : m_Type(TYPE_UNKNOWN
), m_Length(0) {}
150 NPT_MacAddress(Type type
,
151 const unsigned char* addr
,
152 unsigned int length
);
155 void SetAddress(Type type
, const unsigned char* addr
,
156 unsigned int length
);
157 Type
GetType() const { return m_Type
; }
158 const unsigned char* GetAddress() const { return m_Address
; }
159 unsigned int GetLength() const { return m_Length
; }
160 NPT_String
ToString() const;
165 unsigned char m_Address
[NPT_NETWORK_MAX_MAC_ADDRESS_LENGTH
];
166 unsigned int m_Length
;
169 /*----------------------------------------------------------------------
170 | NPT_NetworkInterfaceAddress
171 +---------------------------------------------------------------------*/
172 class NPT_NetworkInterfaceAddress
175 // constructors and destructor
176 NPT_NetworkInterfaceAddress(const NPT_IpAddress
& primary
,
177 const NPT_IpAddress
& broadcast
,
178 const NPT_IpAddress
& destination
,
179 const NPT_IpAddress
& netmask
) :
180 m_PrimaryAddress(primary
),
181 m_BroadcastAddress(broadcast
),
182 m_DestinationAddress(destination
),
183 m_NetMask(netmask
) {}
186 const NPT_IpAddress
& GetPrimaryAddress() const {
187 return m_PrimaryAddress
;
189 const NPT_IpAddress
& GetBroadcastAddress() const {
190 return m_BroadcastAddress
;
192 const NPT_IpAddress
& GetDestinationAddress() const {
193 return m_DestinationAddress
;
195 const NPT_IpAddress
& GetNetMask() const {
199 bool IsAddressInNetwork(const NPT_IpAddress
& address
) {
200 if (m_PrimaryAddress
.AsLong() == address
.AsLong()) return true;
201 if (m_NetMask
.AsLong() == 0) return false;
202 return (m_PrimaryAddress
.AsLong() & m_NetMask
.AsLong()) == (address
.AsLong() & m_NetMask
.AsLong());
207 NPT_IpAddress m_PrimaryAddress
;
208 NPT_IpAddress m_BroadcastAddress
;
209 NPT_IpAddress m_DestinationAddress
;
210 NPT_IpAddress m_NetMask
;
213 /*----------------------------------------------------------------------
214 | NPT_NetworkInterface
215 +---------------------------------------------------------------------*/
216 class NPT_NetworkInterface
220 static NPT_Result
GetNetworkInterfaces(NPT_List
<NPT_NetworkInterface
*>& interfaces
);
222 // constructors and destructor
223 NPT_NetworkInterface(const char* name
,
224 const NPT_MacAddress
& mac
,
226 NPT_NetworkInterface(const char* name
,
228 ~NPT_NetworkInterface() {}
231 NPT_Result
AddAddress(const NPT_NetworkInterfaceAddress
& address
);
232 const NPT_String
& GetName() const {
235 const NPT_MacAddress
& GetMacAddress() const {
238 void SetMacAddress(NPT_MacAddress::Type type
,
239 const unsigned char* addr
,
240 unsigned int length
) {
241 m_MacAddress
.SetAddress(type
, addr
, length
);
243 NPT_Flags
GetFlags() const { return m_Flags
; }
244 const NPT_List
<NPT_NetworkInterfaceAddress
>& GetAddresses() const {
248 bool IsAddressInNetwork(const NPT_IpAddress
& address
) {
249 NPT_List
<NPT_NetworkInterfaceAddress
>::Iterator iter
= m_Addresses
.GetFirstItem();
251 if ((*iter
).IsAddressInNetwork(address
)) return true;
260 NPT_MacAddress m_MacAddress
;
262 NPT_List
<NPT_NetworkInterfaceAddress
> m_Addresses
;
265 /*----------------------------------------------------------------------
266 | NPT_NetworkNameResolver
267 +---------------------------------------------------------------------*/
268 class NPT_NetworkNameResolver
272 static NPT_Result
Resolve(const char* name
,
273 NPT_List
<NPT_IpAddress
>& addresses
,
274 NPT_Timeout timeout
= NPT_TIMEOUT_INFINITE
);
277 #endif // _NPT_NETWORK_H_