groupsock/include/NetAddress.hh

Go to the documentation of this file.
00001 /**********
00002 This library is free software; you can redistribute it and/or modify it under
00003 the terms of the GNU Lesser General Public License as published by the
00004 Free Software Foundation; either version 2.1 of the License, or (at your
00005 option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
00006 
00007 This library is distributed in the hope that it will be useful, but WITHOUT
00008 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00009 FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
00010 more details.
00011 
00012 You should have received a copy of the GNU Lesser General Public License
00013 along with this library; if not, write to the Free Software Foundation, Inc.,
00014 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
00015 **********/
00016 // "mTunnel" multicast access service
00017 // Copyright (c) 1996-2013 Live Networks, Inc.  All rights reserved.
00018 // Network Addresses
00019 // C++ header
00020 
00021 #ifndef _NET_ADDRESS_HH
00022 #define _NET_ADDRESS_HH
00023 
00024 #ifndef _HASH_TABLE_HH
00025 #include "HashTable.hh"
00026 #endif
00027 
00028 #ifndef _NET_COMMON_H
00029 #include "NetCommon.h"
00030 #endif
00031 
00032 #ifndef _USAGE_ENVIRONMENT_HH
00033 #include "UsageEnvironment.hh"
00034 #endif
00035 
00036 // Definition of a type representing a low-level network address.
00037 // At present, this is 32-bits, for IPv4.  Later, generalize it,
00038 // to allow for IPv6.
00039 typedef u_int32_t netAddressBits;
00040 
00041 class NetAddress {
00042     public:
00043         NetAddress(u_int8_t const* data,
00044                    unsigned length = 4 /* default: 32 bits */);
00045         NetAddress(unsigned length = 4); // sets address data to all-zeros
00046         NetAddress(NetAddress const& orig);
00047         NetAddress& operator=(NetAddress const& rightSide);
00048         virtual ~NetAddress();
00049 
00050         unsigned length() const { return fLength; }
00051         u_int8_t const* data() const // always in network byte order
00052                 { return fData; }
00053 
00054     private:
00055         void assign(u_int8_t const* data, unsigned length);
00056         void clean();
00057 
00058         unsigned fLength;
00059         u_int8_t* fData;
00060 };
00061 
00062 class NetAddressList {
00063     public:
00064         NetAddressList(char const* hostname);
00065         NetAddressList(NetAddressList const& orig);
00066         NetAddressList& operator=(NetAddressList const& rightSide);
00067         virtual ~NetAddressList();
00068 
00069         unsigned numAddresses() const { return fNumAddresses; }
00070 
00071         NetAddress const* firstAddress() const;
00072 
00073         // Used to iterate through the addresses in a list:
00074         class Iterator {
00075             public:
00076                 Iterator(NetAddressList const& addressList);
00077                 NetAddress const* nextAddress(); // NULL iff none
00078             private:
00079                 NetAddressList const& fAddressList;
00080                 unsigned fNextIndex;
00081         };
00082 
00083     private:
00084         void assign(netAddressBits numAddresses, NetAddress** addressArray);
00085         void clean();
00086 
00087         friend class Iterator;
00088         unsigned fNumAddresses;
00089         NetAddress** fAddressArray;
00090 };
00091 
00092 typedef u_int16_t portNumBits;
00093 
00094 class Port {
00095     public:
00096         Port(portNumBits num /* in host byte order */);
00097 
00098         portNumBits num() const // in network byte order
00099                 { return fPortNum; }
00100 
00101     private:
00102         portNumBits fPortNum; // stored in network byte order
00103 #ifdef IRIX
00104         portNumBits filler; // hack to overcome a bug in IRIX C++ compiler
00105 #endif
00106 };
00107 
00108 UsageEnvironment& operator<<(UsageEnvironment& s, const Port& p);
00109 
00110 
00111 // A generic table for looking up objects by (address1, address2, port)
00112 class AddressPortLookupTable {
00113     public:
00114         AddressPortLookupTable();
00115         virtual ~AddressPortLookupTable();
00116 
00117         void* Add(netAddressBits address1, netAddressBits address2,
00118                   Port port, void* value);
00119                 // Returns the old value if different, otherwise 0
00120         Boolean Remove(netAddressBits address1, netAddressBits address2,
00121                        Port port);
00122         void* Lookup(netAddressBits address1, netAddressBits address2,
00123                      Port port);
00124                 // Returns 0 if not found
00125 
00126         // Used to iterate through the entries in the table
00127         class Iterator {
00128             public:
00129                 Iterator(AddressPortLookupTable& table);
00130                 virtual ~Iterator();
00131 
00132                 void* next(); // NULL iff none
00133 
00134             private:
00135                 HashTable::Iterator* fIter;
00136         };
00137 
00138     private:
00139         friend class Iterator;
00140         HashTable* fTable;
00141 };
00142 
00143 
00144 Boolean IsMulticastAddress(netAddressBits address);
00145 
00146 
00147 // A mechanism for displaying an IPv4 address in ASCII.  This is intended to replace "inet_ntoa()", which is not thread-safe.
00148 class AddressString {
00149 public:
00150   AddressString(struct sockaddr_in const& addr);
00151   AddressString(struct in_addr const& addr);
00152   AddressString(netAddressBits addr); // "addr" is assumed to be in host byte order here
00153 
00154   virtual ~AddressString();
00155 
00156   char const* val() const { return fVal; }
00157 
00158 private:
00159   void init(netAddressBits addr); // used to implement each of the constructors
00160 
00161 private:
00162   char* fVal; // The result ASCII string: allocated by the constructor; deleted by the destructor
00163 };
00164 
00165 #endif

Generated on Tue Jun 18 13:16:50 2013 for live by  doxygen 1.5.2