1 | /********** |
2 | This library is free software; you can redistribute it and/or modify it under |
3 | the terms of the GNU Lesser General Public License as published by the |
4 | Free Software Foundation; either version 3 of the License, or (at your |
5 | option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.) |
6 | |
7 | This library is distributed in the hope that it will be useful, but WITHOUT |
8 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
9 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for |
10 | more details. |
11 | |
12 | You should have received a copy of the GNU Lesser General Public License |
13 | along with this library; if not, write to the Free Software Foundation, Inc., |
14 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
15 | **********/ |
16 | // "mTunnel" multicast access service |
17 | // Copyright (c) 1996-2020 Live Networks, Inc. All rights reserved. |
18 | // Network Interfaces |
19 | // C++ header |
20 | |
21 | #ifndef _NET_INTERFACE_HH |
22 | #define _NET_INTERFACE_HH |
23 | |
24 | #ifndef _NET_ADDRESS_HH |
25 | #include "NetAddress.hh" |
26 | #endif |
27 | |
28 | class NetInterface { |
29 | public: |
30 | virtual ~NetInterface(); |
31 | |
32 | static UsageEnvironment* DefaultUsageEnvironment; |
33 | // if non-NULL, used for each new interfaces |
34 | |
35 | protected: |
36 | NetInterface(); // virtual base class |
37 | }; |
38 | |
39 | class DirectedNetInterface: public NetInterface { |
40 | public: |
41 | virtual ~DirectedNetInterface(); |
42 | |
43 | virtual Boolean write(unsigned char* data, unsigned numBytes) = 0; |
44 | |
45 | virtual Boolean SourceAddrOKForRelaying(UsageEnvironment& env, |
46 | unsigned addr) = 0; |
47 | |
48 | protected: |
49 | DirectedNetInterface(); // virtual base class |
50 | }; |
51 | |
52 | class DirectedNetInterfaceSet { |
53 | public: |
54 | DirectedNetInterfaceSet(); |
55 | virtual ~DirectedNetInterfaceSet(); |
56 | |
57 | DirectedNetInterface* Add(DirectedNetInterface const* interf); |
58 | // Returns the old value if different, otherwise 0 |
59 | Boolean Remove(DirectedNetInterface const* interf); |
60 | |
61 | Boolean IsEmpty() { return fTable->IsEmpty(); } |
62 | |
63 | // Used to iterate through the interfaces in the set |
64 | class Iterator { |
65 | public: |
66 | Iterator(DirectedNetInterfaceSet& interfaces); |
67 | virtual ~Iterator(); |
68 | |
69 | DirectedNetInterface* next(); // NULL iff none |
70 | |
71 | private: |
72 | HashTable::Iterator* fIter; |
73 | }; |
74 | |
75 | private: |
76 | friend class Iterator; |
77 | HashTable* fTable; |
78 | }; |
79 | |
80 | class Socket: public NetInterface { |
81 | public: |
82 | virtual ~Socket(); |
83 | void reset(); // closes the socket, and sets "fSocketNum" to -1 |
84 | |
85 | virtual Boolean handleRead(unsigned char* buffer, unsigned bufferMaxSize, |
86 | unsigned& bytesRead, |
87 | struct sockaddr_in& fromAddress) = 0; |
88 | // Returns False on error; resultData == NULL if data ignored |
89 | |
90 | int socketNum() const { return fSocketNum; } |
91 | |
92 | Port port() const { |
93 | return fPort; |
94 | } |
95 | |
96 | UsageEnvironment& env() const { return fEnv; } |
97 | |
98 | static int DebugLevel; |
99 | |
100 | protected: |
101 | Socket(UsageEnvironment& env, Port port); // virtual base class |
102 | |
103 | Boolean changePort(Port newPort); // will also cause socketNum() to change |
104 | |
105 | private: |
106 | int fSocketNum; |
107 | UsageEnvironment& fEnv; |
108 | Port fPort; |
109 | }; |
110 | |
111 | UsageEnvironment& operator<<(UsageEnvironment& s, const Socket& sock); |
112 | |
113 | // A data structure for looking up a Socket by port: |
114 | |
115 | class SocketLookupTable { |
116 | public: |
117 | virtual ~SocketLookupTable(); |
118 | |
119 | Socket* Fetch(UsageEnvironment& env, Port port, Boolean& isNew); |
120 | // Creates a new Socket if none already exists |
121 | Boolean Remove(Socket const* sock); |
122 | |
123 | protected: |
124 | SocketLookupTable(); // abstract base class |
125 | virtual Socket* CreateNew(UsageEnvironment& env, Port port) = 0; |
126 | |
127 | private: |
128 | HashTable* fTable; |
129 | }; |
130 | |
131 | // A data structure for counting traffic: |
132 | |
133 | class NetInterfaceTrafficStats { |
134 | public: |
135 | NetInterfaceTrafficStats(); |
136 | |
137 | void countPacket(unsigned packetSize); |
138 | |
139 | float totNumPackets() const {return fTotNumPackets;} |
140 | float totNumBytes() const {return fTotNumBytes;} |
141 | |
142 | Boolean haveSeenTraffic() const; |
143 | |
144 | private: |
145 | float fTotNumPackets; |
146 | float fTotNumBytes; |
147 | }; |
148 | |
149 | #endif |
150 | |