NptNetwork.h

00001 /*****************************************************************
00002 |
00003 |   Neptune - Network
00004 |
00005 |   (c) 2001-2006 Gilles Boccon-Gibod
00006 |   Author: Gilles Boccon-Gibod (bok@bok.net)
00007 |
00008  ****************************************************************/
00009 
00010 #ifndef _NPT_NETWORK_H_
00011 #define _NPT_NETWORK_H_
00012 
00013 /*----------------------------------------------------------------------
00014 |   includes
00015 +---------------------------------------------------------------------*/
00016 #include "NptTypes.h"
00017 #include "NptConstants.h"
00018 #include "NptStrings.h"
00019 #include "NptList.h"
00020 
00021 /*----------------------------------------------------------------------
00022 |   constants
00023 +---------------------------------------------------------------------*/
00024 const unsigned int NPT_NETWORK_MAX_MAC_ADDRESS_LENGTH  = 8;
00025 
00026 /*----------------------------------------------------------------------
00027 |   flags
00028 +---------------------------------------------------------------------*/
00029 #define NPT_NETWORK_INTERFACE_FLAG_LOOPBACK       0x01
00030 #define NPT_NETWORK_INTERFACE_FLAG_PROMISCUOUS    0x02
00031 #define NPT_NETWORK_INTERFACE_FLAG_BROADCAST      0x04
00032 #define NPT_NETWORK_INTERFACE_FLAG_MULTICAST      0x08
00033 #define NPT_NETWORK_INTERFACE_FLAG_POINT_TO_POINT 0x10
00034 
00035 /*----------------------------------------------------------------------
00036 |   workarounds
00037 +---------------------------------------------------------------------*/
00038 #if defined(_WIN32)
00039 #if defined(SetPort)
00040 #undef SetPort
00041 #endif
00042 #endif
00043 
00044 /*----------------------------------------------------------------------
00045 |   types
00046 +---------------------------------------------------------------------*/
00047 typedef unsigned int NPT_IpPort;
00048 
00049 /*----------------------------------------------------------------------
00050 |   NPT_IpAddress
00051 +---------------------------------------------------------------------*/
00052 class NPT_IpAddress
00053 {
00054 public:
00055     // class members
00056     static NPT_IpAddress Any;
00057 
00058     // constructors and destructor
00059     NPT_IpAddress();
00060     NPT_IpAddress(unsigned long address);
00061 
00062     // methods
00063     NPT_Result       ResolveName(const char* name, 
00064                                  NPT_Timeout timeout = NPT_TIMEOUT_INFINITE);
00065     NPT_Result       Parse(const char* name);
00066     NPT_Result       Set(unsigned long address);
00067     NPT_Result       Set(const unsigned char bytes[4]);
00068     const unsigned char* AsBytes() const;
00069     unsigned long    AsLong() const;
00070     NPT_String       ToString() const;
00071     
00072 private:
00073     // members
00074     unsigned char m_Address[4];
00075 };
00076 
00077 /*----------------------------------------------------------------------
00078 |   NPT_MacAddress
00079 +---------------------------------------------------------------------*/
00080 class NPT_MacAddress
00081 {
00082 public:
00083     // typedef enum
00084     typedef enum {
00085         TYPE_UNKNOWN,
00086         TYPE_LOOPBACK,
00087         TYPE_ETHERNET,
00088         TYPE_PPP,
00089         TYPE_IEEE_802_11
00090     } Type;
00091     
00092     // constructors and destructor
00093     NPT_MacAddress() : m_Type(TYPE_UNKNOWN), m_Length(0) {}
00094     NPT_MacAddress(Type           type,
00095                    const unsigned char* addr, 
00096                    unsigned int   length);
00097     
00098     // methods
00099     void                 SetAddress(Type type, const unsigned char* addr,
00100                                     unsigned int length);
00101     Type                 GetType() const    { return m_Type; }
00102     const unsigned char* GetAddress() const { return m_Address; }
00103     unsigned int         GetLength() const  { return m_Length; }
00104     NPT_String           ToString() const;
00105     
00106 private:
00107     // members
00108     Type          m_Type;
00109     unsigned char m_Address[NPT_NETWORK_MAX_MAC_ADDRESS_LENGTH];
00110     unsigned int  m_Length;
00111 };
00112 
00113 /*----------------------------------------------------------------------
00114 |   NPT_NetworkInterfaceAddress
00115 +---------------------------------------------------------------------*/
00116 class NPT_NetworkInterfaceAddress
00117 {
00118 public:
00119     // constructors and destructor
00120     NPT_NetworkInterfaceAddress(const NPT_IpAddress& primary,
00121                                 const NPT_IpAddress& broadcast,
00122                                 const NPT_IpAddress& destination,
00123                                 const NPT_IpAddress& netmask) :
00124         m_PrimaryAddress(primary),
00125         m_BroadcastAddress(broadcast),
00126         m_DestinationAddress(destination),
00127         m_NetMask(netmask) {}
00128 
00129     // methods
00130     const NPT_IpAddress& GetPrimaryAddress() const {
00131         return m_PrimaryAddress;
00132     }
00133     const NPT_IpAddress& GetBroadcastAddress() const {
00134         return m_BroadcastAddress;
00135     }
00136     const NPT_IpAddress& GetDestinationAddress() const {
00137         return m_DestinationAddress;
00138     }
00139     const NPT_IpAddress& GetNetMask() const {
00140         return m_NetMask;
00141     }
00142     
00143 private:
00144     // members
00145     NPT_IpAddress m_PrimaryAddress;
00146     NPT_IpAddress m_BroadcastAddress;
00147     NPT_IpAddress m_DestinationAddress;
00148     NPT_IpAddress m_NetMask;
00149 };
00150 
00151 /*----------------------------------------------------------------------
00152 |   NPT_NetworkInterface
00153 +---------------------------------------------------------------------*/
00154 class NPT_NetworkInterface
00155 {
00156 public:
00157     // class methods
00158     static NPT_Result GetNetworkInterfaces(NPT_List<NPT_NetworkInterface*>& interfaces);
00159 
00160     // constructors and destructor
00161     NPT_NetworkInterface(const char*           name,
00162                          const NPT_MacAddress& mac,
00163                          NPT_Flags             flags);
00164    ~NPT_NetworkInterface() {}
00165 
00166     // methods
00167     NPT_Result AddAddress(const NPT_NetworkInterfaceAddress& address);
00168     const NPT_String& GetName() const {
00169         return m_Name;
00170     }
00171     const NPT_MacAddress& GetMacAddress() const {
00172         return m_MacAddress;
00173     }
00174     NPT_Flags GetFlags() const { return m_Flags; }
00175     const NPT_List<NPT_NetworkInterfaceAddress>& GetAddresses() const {
00176         return m_Addresses;
00177     }    
00178     
00179 private:
00180     // members
00181     NPT_String                            m_Name;
00182     NPT_MacAddress                        m_MacAddress;
00183     NPT_Flags                             m_Flags;
00184     NPT_List<NPT_NetworkInterfaceAddress> m_Addresses;
00185 };
00186 
00187 
00188 #endif // _NPT_NETWORK_H_