1/**********
2This library is free software; you can redistribute it and/or modify it under
3the terms of the GNU Lesser General Public License as published by the
4Free Software Foundation; either version 3 of the License, or (at your
5option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
6
7This library is distributed in the hope that it will be useful, but WITHOUT
8ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
10more details.
11
12You should have received a copy of the GNU Lesser General Public License
13along with this library; if not, write to the Free Software Foundation, Inc.,
1451 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 Addresses
19// C++ header
20
21#ifndef _NET_ADDRESS_HH
22#define _NET_ADDRESS_HH
23
24#ifndef _HASH_TABLE_HH
25#include "HashTable.hh"
26#endif
27
28#ifndef _NET_COMMON_H
29#include "NetCommon.h"
30#endif
31
32#ifndef _USAGE_ENVIRONMENT_HH
33#include "UsageEnvironment.hh"
34#endif
35
36// Definition of a type representing a low-level network address.
37// At present, this is 32-bits, for IPv4. Later, generalize it,
38// to allow for IPv6.
39typedef u_int32_t netAddressBits;
40
41class NetAddress {
42public:
43 NetAddress(u_int8_t const* data,
44 unsigned length = 4 /* default: 32 bits */);
45 NetAddress(unsigned length = 4); // sets address data to all-zeros
46 NetAddress(NetAddress const& orig);
47 NetAddress& operator=(NetAddress const& rightSide);
48 virtual ~NetAddress();
49
50 unsigned length() const { return fLength; }
51 u_int8_t const* data() const // always in network byte order
52 { return fData; }
53
54private:
55 void assign(u_int8_t const* data, unsigned length);
56 void clean();
57
58 unsigned fLength;
59 u_int8_t* fData;
60};
61
62class NetAddressList {
63public:
64 NetAddressList(char const* hostname);
65 NetAddressList(NetAddressList const& orig);
66 NetAddressList& operator=(NetAddressList const& rightSide);
67 virtual ~NetAddressList();
68
69 unsigned numAddresses() const { return fNumAddresses; }
70
71 NetAddress const* firstAddress() const;
72
73 // Used to iterate through the addresses in a list:
74 class Iterator {
75 public:
76 Iterator(NetAddressList const& addressList);
77 NetAddress const* nextAddress(); // NULL iff none
78 private:
79 NetAddressList const& fAddressList;
80 unsigned fNextIndex;
81 };
82
83private:
84 void assign(netAddressBits numAddresses, NetAddress** addressArray);
85 void clean();
86
87 friend class Iterator;
88 unsigned fNumAddresses;
89 NetAddress** fAddressArray;
90};
91
92typedef u_int16_t portNumBits;
93
94class Port {
95public:
96 Port(portNumBits num /* in host byte order */);
97
98 portNumBits num() const { return fPortNum; } // in network byte order
99
100private:
101 portNumBits fPortNum; // stored in network byte order
102#ifdef IRIX
103 portNumBits filler; // hack to overcome a bug in IRIX C++ compiler
104#endif
105};
106
107UsageEnvironment& operator<<(UsageEnvironment& s, const Port& p);
108
109
110// A generic table for looking up objects by (address1, address2, port)
111class AddressPortLookupTable {
112public:
113 AddressPortLookupTable();
114 virtual ~AddressPortLookupTable();
115
116 void* Add(netAddressBits address1, netAddressBits address2, Port port, void* value);
117 // Returns the old value if different, otherwise 0
118 Boolean Remove(netAddressBits address1, netAddressBits address2, Port port);
119 void* Lookup(netAddressBits address1, netAddressBits address2, Port port);
120 // Returns 0 if not found
121 void* RemoveNext() { return fTable->RemoveNext(); }
122
123 // Used to iterate through the entries in the table
124 class Iterator {
125 public:
126 Iterator(AddressPortLookupTable& table);
127 virtual ~Iterator();
128
129 void* next(); // NULL iff none
130
131 private:
132 HashTable::Iterator* fIter;
133 };
134
135private:
136 friend class Iterator;
137 HashTable* fTable;
138};
139
140
141Boolean IsMulticastAddress(netAddressBits address);
142
143
144// A mechanism for displaying an IPv4 address in ASCII. This is intended to replace "inet_ntoa()", which is not thread-safe.
145class AddressString {
146public:
147 AddressString(struct sockaddr_in const& addr);
148 AddressString(struct in_addr const& addr);
149 AddressString(netAddressBits addr); // "addr" is assumed to be in host byte order here
150
151 virtual ~AddressString();
152
153 char const* val() const { return fVal; }
154
155private:
156 void init(netAddressBits addr); // used to implement each of the constructors
157
158private:
159 char* fVal; // The result ASCII string: allocated by the constructor; deleted by the destructor
160};
161
162#endif
163