groupsock/include/Groupsock.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-2014 Live Networks, Inc.  All rights reserved.
00018 // 'Group sockets'
00019 // C++ header
00020 
00021 #ifndef _GROUPSOCK_HH
00022 #define _GROUPSOCK_HH
00023 
00024 #ifndef _GROUPSOCK_VERSION_HH
00025 #include "groupsock_version.hh"
00026 #endif
00027 
00028 #ifndef _NET_INTERFACE_HH
00029 #include "NetInterface.hh"
00030 #endif
00031 
00032 #ifndef _GROUPEID_HH
00033 #include "GroupEId.hh"
00034 #endif
00035 
00036 // An "OutputSocket" is (by default) used only to send packets.
00037 // No packets are received on it (unless a subclass arranges this)
00038 
00039 class OutputSocket: public Socket {
00040 public:
00041   OutputSocket(UsageEnvironment& env);
00042   virtual ~OutputSocket();
00043 
00044   Boolean write(netAddressBits address, Port port, u_int8_t ttl,
00045                 unsigned char* buffer, unsigned bufferSize);
00046 
00047 protected:
00048   OutputSocket(UsageEnvironment& env, Port port);
00049 
00050   portNumBits sourcePortNum() const {return fSourcePort.num();}
00051 
00052 private: // redefined virtual function
00053   virtual Boolean handleRead(unsigned char* buffer, unsigned bufferMaxSize,
00054                              unsigned& bytesRead,
00055                              struct sockaddr_in& fromAddress);
00056 
00057 private:
00058   Port fSourcePort;
00059   unsigned fLastSentTTL;
00060 };
00061 
00062 class destRecord {
00063 public:
00064   destRecord(struct in_addr const& addr, Port const& port, u_int8_t ttl,
00065              destRecord* next);
00066   virtual ~destRecord();
00067 
00068 public:
00069   destRecord* fNext;
00070   GroupEId fGroupEId;
00071   Port fPort;
00072 };
00073 
00074 // A "Groupsock" is used to both send and receive packets.
00075 // As the name suggests, it was originally designed to send/receive
00076 // multicast, but it can send/receive unicast as well.
00077 
00078 class Groupsock: public OutputSocket {
00079 public:
00080   Groupsock(UsageEnvironment& env, struct in_addr const& groupAddr,
00081             Port port, u_int8_t ttl);
00082       // used for a 'source-independent multicast' group
00083   Groupsock(UsageEnvironment& env, struct in_addr const& groupAddr,
00084             struct in_addr const& sourceFilterAddr,
00085             Port port);
00086       // used for a 'source-specific multicast' group
00087   virtual ~Groupsock();
00088 
00089   void changeDestinationParameters(struct in_addr const& newDestAddr,
00090                                    Port newDestPort, int newDestTTL);
00091       // By default, the destination address, port and ttl for
00092       // outgoing packets are those that were specified in
00093       // the constructor.  This works OK for multicast sockets,
00094       // but for unicast we usually want the destination port
00095       // number, at least, to be different from the source port.
00096       // (If a parameter is 0 (or ~0 for ttl), then no change made.)
00097 
00098   // As a special case, we also allow multiple destinations (addresses & ports)
00099   // (This can be used to implement multi-unicast.)
00100   void addDestination(struct in_addr const& addr, Port const& port);
00101   void removeDestination(struct in_addr const& addr, Port const& port);
00102   void removeAllDestinations();
00103 
00104   struct in_addr const& groupAddress() const {
00105     return fIncomingGroupEId.groupAddress();
00106   }
00107   struct in_addr const& sourceFilterAddress() const {
00108     return fIncomingGroupEId.sourceFilterAddress();
00109   }
00110 
00111   Boolean isSSM() const {
00112     return fIncomingGroupEId.isSSM();
00113   }
00114 
00115   u_int8_t ttl() const { return fTTL; }
00116 
00117   void multicastSendOnly(); // send, but don't receive any multicast packets
00118 
00119   Boolean output(UsageEnvironment& env, u_int8_t ttl,
00120                  unsigned char* buffer, unsigned bufferSize,
00121                  DirectedNetInterface* interfaceNotToFwdBackTo = NULL);
00122 
00123   DirectedNetInterfaceSet& members() { return fMembers; }
00124 
00125   Boolean deleteIfNoMembers;
00126   Boolean isSlave; // for tunneling
00127 
00128   static NetInterfaceTrafficStats statsIncoming;
00129   static NetInterfaceTrafficStats statsOutgoing;
00130   static NetInterfaceTrafficStats statsRelayedIncoming;
00131   static NetInterfaceTrafficStats statsRelayedOutgoing;
00132   NetInterfaceTrafficStats statsGroupIncoming; // *not* static
00133   NetInterfaceTrafficStats statsGroupOutgoing; // *not* static
00134   NetInterfaceTrafficStats statsGroupRelayedIncoming; // *not* static
00135   NetInterfaceTrafficStats statsGroupRelayedOutgoing; // *not* static
00136 
00137   Boolean wasLoopedBackFromUs(UsageEnvironment& env,
00138                               struct sockaddr_in& fromAddress);
00139 
00140 public: // redefined virtual functions
00141   virtual Boolean handleRead(unsigned char* buffer, unsigned bufferMaxSize,
00142                              unsigned& bytesRead,
00143                              struct sockaddr_in& fromAddress);
00144 
00145 private:
00146   int outputToAllMembersExcept(DirectedNetInterface* exceptInterface,
00147                                u_int8_t ttlToFwd,
00148                                unsigned char* data, unsigned size,
00149                                netAddressBits sourceAddr);
00150 
00151 private:
00152   GroupEId fIncomingGroupEId;
00153   destRecord* fDests;
00154   u_int8_t fTTL;
00155   DirectedNetInterfaceSet fMembers;
00156 };
00157 
00158 UsageEnvironment& operator<<(UsageEnvironment& s, const Groupsock& g);
00159 
00160 // A data structure for looking up a 'groupsock'
00161 // by (multicast address, port), or by socket number
00162 class GroupsockLookupTable {
00163 public:
00164   Groupsock* Fetch(UsageEnvironment& env, netAddressBits groupAddress,
00165                    Port port, u_int8_t ttl, Boolean& isNew);
00166       // Creates a new Groupsock if none already exists
00167   Groupsock* Fetch(UsageEnvironment& env, netAddressBits groupAddress,
00168                    netAddressBits sourceFilterAddr,
00169                    Port port, Boolean& isNew);
00170       // Creates a new Groupsock if none already exists
00171   Groupsock* Lookup(netAddressBits groupAddress, Port port);
00172       // Returns NULL if none already exists
00173   Groupsock* Lookup(netAddressBits groupAddress,
00174                     netAddressBits sourceFilterAddr,
00175                     Port port);
00176       // Returns NULL if none already exists
00177   Groupsock* Lookup(UsageEnvironment& env, int sock);
00178       // Returns NULL if none already exists
00179   Boolean Remove(Groupsock const* groupsock);
00180 
00181   // Used to iterate through the groupsocks in the table
00182   class Iterator {
00183   public:
00184     Iterator(GroupsockLookupTable& groupsocks);
00185 
00186     Groupsock* next(); // NULL iff none
00187 
00188   private:
00189     AddressPortLookupTable::Iterator fIter;
00190   };
00191 
00192 private:
00193   Groupsock* AddNew(UsageEnvironment& env,
00194                     netAddressBits groupAddress,
00195                     netAddressBits sourceFilterAddress,
00196                     Port port, u_int8_t ttl);
00197 
00198 private:
00199   friend class Iterator;
00200   AddressPortLookupTable fTable;
00201 };
00202 
00203 #endif

Generated on Sun Jul 13 05:09:44 2014 for live by  doxygen 1.5.2