kodi
NptNetwork.h
1 /*****************************************************************
2 |
3 | Neptune - Network
4 |
5 | Copyright (c) 2002-2008, Axiomatic Systems, LLC.
6 | All rights reserved.
7 |
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.
18 |
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.
29 |
30  ****************************************************************/
31 
32 #ifndef _NPT_NETWORK_H_
33 #define _NPT_NETWORK_H_
34 
35 /*----------------------------------------------------------------------
36 | includes
37 +---------------------------------------------------------------------*/
38 #include "NptTypes.h"
39 #include "NptConstants.h"
40 #include "NptStrings.h"
41 #include "NptList.h"
42 
43 /*----------------------------------------------------------------------
44 | constants
45 +---------------------------------------------------------------------*/
46 const unsigned int NPT_NETWORK_MAX_MAC_ADDRESS_LENGTH = 8;
47 
48 /*----------------------------------------------------------------------
49 | flags
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
56 
57 /*----------------------------------------------------------------------
58 | workarounds
59 +---------------------------------------------------------------------*/
60 #if defined(_WIN32)
61 #if defined(SetPort)
62 #undef SetPort
63 #endif
64 #endif
65 
66 /*----------------------------------------------------------------------
67 | types
68 +---------------------------------------------------------------------*/
69 typedef unsigned int NPT_IpPort;
70 
71 /*----------------------------------------------------------------------
72 | NPT_IpAddress
73 +---------------------------------------------------------------------*/
75 {
76 public:
77  // constants
78  typedef enum {
79  IPV4,
80  IPV6
81  } Type;
82 
83  // class members
84  static const NPT_IpAddress Any;
85  static const NPT_IpAddress Loopback;
86 
87  // constructors and destructor
88  NPT_IpAddress();
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);
93 
94  // accessors
95  Type GetType() const { return m_Type; }
96  NPT_UInt32 GetScopeId() const { return m_ScopeId; }
97 
98  // methods
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;
109 
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;
119 
120  // operators
121  bool operator==(const NPT_IpAddress& other) const;
122 
123  // FIXME: temporary
124  NPT_String m_HostName;
125 
126 private:
127  // members
128  Type m_Type;
129  unsigned char m_Address[16];
130  NPT_UInt32 m_ScopeId; // IPv6 only
131 };
132 
133 /*----------------------------------------------------------------------
134 | NPT_MacAddress
135 +---------------------------------------------------------------------*/
137 {
138 public:
139  // typedef enum
140  typedef enum {
141  TYPE_UNKNOWN,
142  TYPE_LOOPBACK,
143  TYPE_ETHERNET,
144  TYPE_PPP,
145  TYPE_IEEE_802_11
146  } Type;
147 
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);
153 
154  // methods
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;
161 
162 private:
163  // members
164  Type m_Type;
165  unsigned char m_Address[NPT_NETWORK_MAX_MAC_ADDRESS_LENGTH];
166  unsigned int m_Length;
167 };
168 
169 /*----------------------------------------------------------------------
170 | NPT_NetworkInterfaceAddress
171 +---------------------------------------------------------------------*/
173 {
174 public:
175  // constructors and destructor
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) {}
184 
185  // methods
186  const NPT_IpAddress& GetPrimaryAddress() const {
187  return m_PrimaryAddress;
188  }
189  const NPT_IpAddress& GetBroadcastAddress() const {
190  return m_BroadcastAddress;
191  }
192  const NPT_IpAddress& GetDestinationAddress() const {
193  return m_DestinationAddress;
194  }
195  const NPT_IpAddress& GetNetMask() const {
196  return m_NetMask;
197  }
198 
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());
203  }
204 
205 private:
206  // members
207  NPT_IpAddress m_PrimaryAddress;
208  NPT_IpAddress m_BroadcastAddress;
209  NPT_IpAddress m_DestinationAddress;
210  NPT_IpAddress m_NetMask;
211 };
212 
213 /*----------------------------------------------------------------------
214 | NPT_NetworkInterface
215 +---------------------------------------------------------------------*/
217 {
218 public:
219  // class methods
220  static NPT_Result GetNetworkInterfaces(NPT_List<NPT_NetworkInterface*>& interfaces);
221 
222  // constructors and destructor
223  NPT_NetworkInterface(const char* name,
224  const NPT_MacAddress& mac,
225  NPT_Flags flags);
226  NPT_NetworkInterface(const char* name,
227  NPT_Flags flags);
229 
230  // methods
231  NPT_Result AddAddress(const NPT_NetworkInterfaceAddress& address);
232  const NPT_String& GetName() const {
233  return m_Name;
234  }
235  const NPT_MacAddress& GetMacAddress() const {
236  return m_MacAddress;
237  }
238  void SetMacAddress(NPT_MacAddress::Type type,
239  const unsigned char* addr,
240  unsigned int length) {
241  m_MacAddress.SetAddress(type, addr, length);
242  }
243  NPT_Flags GetFlags() const { return m_Flags; }
244  const NPT_List<NPT_NetworkInterfaceAddress>& GetAddresses() const {
245  return m_Addresses;
246  }
247 
248  bool IsAddressInNetwork(const NPT_IpAddress& address) {
249  NPT_List<NPT_NetworkInterfaceAddress>::Iterator iter = m_Addresses.GetFirstItem();
250  while (iter) {
251  if ((*iter).IsAddressInNetwork(address)) return true;
252  ++iter;
253  }
254  return false;
255  }
256 
257 private:
258  // members
259  NPT_String m_Name;
260  NPT_MacAddress m_MacAddress;
261  NPT_Flags m_Flags;
263 };
264 
265 /*----------------------------------------------------------------------
266 | NPT_NetworkNameResolver
267 +---------------------------------------------------------------------*/
269 {
270 public:
271  // class methods
272  static NPT_Result Resolve(const char* name,
273  NPT_List<NPT_IpAddress>& addresses,
274  NPT_Timeout timeout = NPT_TIMEOUT_INFINITE);
275 };
276 
277 #endif // _NPT_NETWORK_H_
Definition: NptNetwork.h:216
Definition: NptNetwork.h:136
Definition: NptNetwork.h:74
Definition: NptNetwork.h:268
Definition: NptNetwork.h:172
Definition: NptStrings.h:57
Definition: NptList.h:54