VR-Link API Documentation for DIS
vlInetDevice.h
Go to the documentation of this file.
00001 /*********************************************************************
00002 ** Copyright (c) 1992-2010 VT MAK
00003 ** All rights reserved.
00004 *********************************************************************/
00008 #pragma once
00009 
00010 #include <vlutil/vlInet.h>
00011 #include <vlutil/vlInetAddr.h>
00012 #include <vlutil/vlString.h>
00013 #include <vlutil/vlSmartPointers.h>
00014 #include <vector>
00015 #ifndef _WIN32
00016 #include <net/if.h> // for IFNAMSIZ
00017 #endif
00018 
00019 #ifdef DtUSE_UTILITIES_NAMESPACE
00020 namespace DtUSE_UTILITIES_NAMESPACE
00021 {
00022 #endif
00023 
00024 #ifdef _WIN32
00025 // Different physical network connection types have different physical address
00026 // structures. Allow for more than just an Ethernet MAC address.
00027 #define DtInet_MAX_PHYSADDR_LEN MAX_ADAPTER_ADDRESS_LENGTH
00028 #define DtMAX_DEVICE_NAME_LEN 128 // Windows is ???? so be safe
00029 #define DtInet_INITIAL_IFCONF_BUFF_SIZE sizeof(IP_ADAPTER_ADDRESSES)
00030 
00031 #define DtInet_IFSTATE_UP       IfOperStatusUp
00032 #define DtInet_IFSTATE_DOWN     IfOperStatusDown
00033 #define DtInet_IFSTATE_ACTIVE   IfOperStatusUp
00034 #define DtInet_IFSTATE_UNKNOWN  IfOperStatusUnknown
00035 
00036 #else
00037 // Different physical network connection types have different physical address
00038 // structures. Allow for more than just an Ethernet MAC address.
00039 #define DtInet_MAX_PHYSADDR_LEN 8
00040 #define DtMAX_DEVICE_NAME_LEN IFNAMSIZ // typically 16
00041 #define DtInet_INITIAL_IFCONF_BUFF_SIZE sizeof(struct ifreq)
00042 #ifdef __solaris__
00043 #define DtInet_INITIAL_LIFCONF_BUFF_SIZE sizeof(struct lifreq)
00044 #endif
00045 
00046 #define DtInet_IFSTATE_UP       IFF_UP
00047 #define DtInet_IFSTATE_DOWN     0
00048 #define DtInet_IFSTATE_ACTIVE   IFF_RUNNING
00049 #define DtInet_IFSTATE_UNKNOWN  0
00050 
00051 //#define DtInet_IF_ETHERNET ARPHRD_ETHER
00052 //#define DtInet_IF_FDDI     ARPHRD_FDDI
00053 //#define DtInet_IF_IEEE1394 ARPHRD_IEEE1394
00054 //#define DtInet_IF_WIFI     ARPHRD_IEEE80211
00055 //#define DtInet_IF_LOOPBACK ARPHRD_LOOPBACK
00056 //#define DtInet_IF_OTHER    ARPHRD_VOID
00057 
00058 #endif
00059 typedef DtU8 DtInetMacAddr[DtInet_MAX_PHYSADDR_LEN];
00060 
00061 // The standard ifreq structure does not support IPv6 addresses (sockaddr_in6)
00062 // so we have to define our own. In stark contrast to the standard ifreq struct
00063 // this one stores all the information in a single structure.
00064 struct ifinfo_in6
00065 {
00066    struct  sockaddr_in6 ifinfo_addr;
00067    struct  sockaddr_in6 ifinfo_dstaddr;
00068    struct  sockaddr_in6 ifinfo_broadaddr;
00069    unsigned long        ifinfo_flags;
00070    unsigned long        ifinfo_prefix;
00071    unsigned long        ifinfo_scope;
00072    u_short              ifinfo_site6;     // currently not used
00073    unsigned long        ifinfo_mtu;       // currently not used
00074 };
00075 
00076 struct ifreq_in6
00077 {
00078    char ifr6_name[DtMAX_DEVICE_NAME_LEN];
00079    union
00080    {
00081       struct ifinfo_in6   ifru_info_in6;
00082       caddr_t             ifru_data;
00083    } ifr6_ifru_in6;
00084 #define ifr6_info_in6   ifr6_ifru_in6.ifru_info_in6
00085 #define ifr6_data       ifr6_ifru_in6.ifru_data
00086 };
00087 
00088 enum DtInetIfLinkType
00089 {
00090    DtInet_IF_ETHERNET,
00091    DtInet_IF_FDDI,
00092    DtInet_IF_IEEE1394,
00093    DtInet_IF_WIFI,
00094    DtInet_IF_LOOPBACK,
00095    DtInet_IF_OTHER
00096 };
00097 
00098 class DtInetDevice;
00099 typedef DtBoost::shared_ptr<DtInetDevice> DtInetDeviceSP;
00100 
00101 class DT_DLL_VLUTIL DtInetDevice
00102 {
00103 public:
00104 
00108    DtInetDevice( const DtInetAddr& addr );
00109    DtInetDevice( const DtString& deviceName,
00110       DtInetUtils::DtInetAddrFamily fam = DtInetUtils::DtInetAddrFamily_IPv4 );
00111 
00114 #ifdef _WIN32
00115    DtInetDevice( PIP_ADAPTER_ADDRESSES devInfo,
00116       DtInetUtils::DtInetAddrFamily fam = DtInetUtils::DtInetAddrFamily_IPv4 );
00117 #else
00118 // DEBUG -- DAA what struct does SOLARIS use for IPv4 devices?
00119 #ifdef __solaris__
00120    DtInetDevice( int sock, struct lifreq* devInfo,
00121                  struct ifreq_in6* ipv6DevInfo,
00122       DtInetUtils::DtInetAddrFamily fam = DtInetUtils::DtInetAddrFamily_IPv4 );
00123 #else
00124    DtInetDevice( int sock, struct ifreq* devInfo,
00125                  struct ifreq_in6* ipv6DevInfo,
00126       DtInetUtils::DtInetAddrFamily fam = DtInetUtils::DtInetAddrFamily_IPv4 );
00127 #endif
00128 #endif
00129 
00131    DtInetDevice( const DtInetDevice& orig );
00132 
00134    DtInetDevice& operator=(const DtInetDevice & orig);
00135 
00137    virtual ~DtInetDevice();
00138 
00141    virtual const DtString& deviceName() const;
00142 
00145    virtual const DtInetAddr* addr() const;
00146 
00149    virtual const DtInetAddr* broadcastAddr() const;
00150 
00152    virtual const DtInetAddr* ipv4Addr() const;
00153 
00157    virtual const DtInetAddr* ipv4Netmask() const;
00158 
00163    virtual const DtInetAddr* ipv4BroadcastAddr() const;
00164 
00166    virtual const DtInetAddr* ipv6Addr() const;
00167 
00170    virtual const DtInetAddr* ipv6BroadcastAddr() const;
00171 
00174    virtual unsigned int macAddr(DtInetMacAddr& mac) const ;
00175 
00179    virtual const DtString& macAddrString() const;
00180 
00182    virtual DtInetIfLinkType type() const;
00183 
00185    virtual unsigned int mtu() const;
00186 
00188    virtual unsigned int linkState() const;
00189 
00192    virtual DtInetIpv6Scope linkScope() const;
00193 
00196    virtual bool isUp() const;     // UP means enabled, not necessarily active
00197    virtual bool isActive() const; // implies link is enabled (UP)
00198    virtual bool isBroadcastCapable() const;
00199    virtual bool isMulticastCapable() const;
00200    virtual bool hasIpv4Addr() const;
00201    virtual bool hasIpv6Addr() const;
00203 
00206    virtual unsigned int interfaceIndex() const;
00207 
00210    virtual bool isOk() const;
00211 
00213    virtual const DtString& getLastErrorString() const;
00214 
00216    friend DT_DLL_VLUTIL bool operator==(const DtInetDevice& dev1, const DtInetDevice& dev2);
00217 //   virtual bool operator==(const DtInetDevice &);
00218 
00219 protected:
00220    void scanDeviceByName( const DtString& deviceName );   
00221    void initMACAddrString();
00222 
00223 #ifdef _WIN32
00224    void init( PIP_ADAPTER_ADDRESSES devInfo );
00225 #else
00226 #ifdef __solaris__
00227    void init( int sockFd, struct lifreq* devInfo,
00228               struct ifreq_in6* ipv6DevInfo );
00229 #else
00230    void init( int sockFd, struct ifreq* devInfo,
00231               struct ifreq_in6* ipv6DevInfo );
00232 
00233 #endif
00234 #endif
00235 
00236 protected:
00237    DtString myDeviceName;
00238 
00239    DtInetMacAddr   myMACAddr;
00240    unsigned int    myMACAddrLen;
00241    DtString        myMACAddrString;
00242    DtInetAddr*     myIpv4Addr;
00243    DtInetAddr*     myIpv4Netmask;
00244    DtInetAddr*     myIpv6Addr;
00245    DtInetIpv6Scope myLinkScope;
00246    bool            myBroadcastEnabled;
00247    DtInetAddr*     myIpv4BroadcastAddr;
00248    DtInetAddr*     myIpv6BroadcastAddr;
00249 
00250    DtInetUtils::DtInetAddrFamily myDefaultAddrFam;
00251 
00252    bool                     myMulticastEnabled;
00253    std::vector<DtInetAddr*> myIpv4McastAddrList;
00254    std::vector<DtInetAddr*> myIpv6McastAddrList;
00255 
00256    unsigned int      myMtu;
00257    unsigned int      myLinkState;
00258    DtInetIfLinkType  myLinkHwType;
00259 
00260    // Interface indexes may change if the network is restarted (Linux) or an
00261    // interface changes state (Windows) so it is not to be used except during
00262    // initialization.
00263    unsigned int myIfIndex;
00264 
00265    bool     myDeviceError;
00266    DtString myLastErrorString;
00267 
00268    // These two members are used by the *nix version of fetchDeviceInfo to
00269    // cache the last values used during a successful scan of device interfaces
00270    // so the next time a scan is requested it can use the buffer size as a
00271    // starting point and adjust from there if necessary, rather than starting
00272    // each scan from scratch.
00273    unsigned int myIfconfBufferSize;
00274    unsigned int myIfconfResizeFactor;
00275 
00276 };
00277 // end of class DtInetDevice
00278 
00279 #ifdef DtUSE_UTILITIES_NAMESPACE
00280 } 
00281 #endif

Document ID: Generated on Mon May 14 08:06:18 EDT 2012 from SVN revision 114750
Copyright © 2005-2012 VT MÄK Inc. All Rights Reserved (www.mak.com)