live
NetAddress.hh
Go to the documentation of this file.
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// "groupsock"
17// Copyright (c) 1996-2024 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 // Note that the type "netAddressBits" is no longer defined; use "ipv4AddressBits" instead.
38typedef u_int32_t ipv4AddressBits;
39typedef u_int8_t ipv6AddressBits[16]; // 128 bits
40
42public:
43 NetAddress(u_int8_t const* data,
44 unsigned length = 4 /* default: 32 bits (for IPv4); use 16 (128 bits) for IPv6 */);
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
62struct sockaddr_storage const& nullAddress(int addressFamily = AF_INET);
63Boolean addressIsNull(sockaddr_storage const& address);
64
65SOCKLEN_T addressSize(sockaddr_storage const& address);
66
67void copyAddress(struct sockaddr_storage& to, NetAddress const* from);
68
69Boolean operator==(struct sockaddr_storage const& left, struct sockaddr_storage const& right);
70 // compares the family and address parts only; not the port number or anything else
71
73public:
74 NetAddressList(char const* hostname, int addressFamily = AF_UNSPEC);
77 virtual ~NetAddressList();
78
79 unsigned numAddresses() const { return fNumAddresses; }
80
81 NetAddress const* firstAddress() const;
82
83 // Used to iterate through the addresses in a list:
84 class Iterator {
85 public:
86 Iterator(NetAddressList const& addressList);
87 NetAddress const* nextAddress(); // NULL iff none
88 private:
90 unsigned fNextIndex;
91 };
92
93private:
94 void assign(unsigned numAddresses, NetAddress** addressArray);
95 void clean();
96
97 friend class Iterator;
98 unsigned fNumAddresses;
100};
101
102typedef u_int16_t portNumBits;
103
104class Port {
105public:
106 Port(portNumBits num /* in host byte order */);
107
108 portNumBits num() const { return fPortNum; } // in network byte order
109
110private:
111 portNumBits fPortNum; // stored in network byte order
112#ifdef IRIX
113 portNumBits filler; // hack to overcome a bug in IRIX C++ compiler
114#endif
115};
116
118
119
120// A generic table for looking up objects by (address1, address2, port)
122public:
125
126 void* Add(struct sockaddr_storage const& address1,
127 struct sockaddr_storage const& address2,
128 Port port,
129 void* value);
130 // Returns the old value if different, otherwise 0
131 void* Add(struct sockaddr_storage const& address1,
132 Port port,
133 void* value) {
134 return Add(address1, nullAddress(), port, value);
135 }
136
137 Boolean Remove(struct sockaddr_storage const& address1,
138 struct sockaddr_storage const& address2,
139 Port port);
140 Boolean Remove(struct sockaddr_storage const& address1,
141 Port port) {
142 return Remove(address1, nullAddress(), port);
143 }
144
145 void* Lookup(struct sockaddr_storage const& address1,
146 struct sockaddr_storage const& address2,
147 Port port);
148 // Returns 0 if not found
149 void* Lookup(struct sockaddr_storage const& address1,
150 Port port) {
151 return Lookup(address1, nullAddress(), port);
152 }
153
154 void* RemoveNext() { return fTable->RemoveNext(); }
155
156 // Used to iterate through the entries in the table
157 class Iterator {
158 public:
160 virtual ~Iterator();
161
162 void* next(); // NULL iff none
163
164 private:
166 };
167
168private:
169 friend class Iterator;
171};
172
173
174Boolean IsMulticastAddress(struct sockaddr_storage const& address);
175
176
177// A mechanism for displaying an IP (v4 or v6) address in ASCII.
178// (This encapsulates the "inet_ntop()" function.)
180public:
181 // IPv4 input:
182 AddressString(struct sockaddr_in const& addr);
183 AddressString(struct in_addr const& addr);
184 AddressString(ipv4AddressBits const& addr); // "addr" is assumed to be in network byte order
185
186 // IPv6 input:
187 AddressString(struct sockaddr_in6 const& addr);
188 AddressString(struct in6_addr const& addr);
190
191 // IPv4 or IPv6 input:
192 AddressString(struct sockaddr_storage const& addr);
193
194 virtual ~AddressString();
195
196 char const* val() const { return fVal; }
197
198private:
199 void init(ipv4AddressBits const& addr); // used to implement the IPv4 constructors
200 void init(ipv6AddressBits const& addr); // used to implement the IPv6 constructors
201
202private:
203 char* fVal; // The result ASCII string: allocated by the constructor; deleted by the destructor
204};
205
206portNumBits portNum(struct sockaddr_storage const& address);
207void setPortNum(struct sockaddr_storage& address, portNumBits portNum/*in network order*/);
208
209#endif
unsigned char Boolean
Definition: Boolean.hh:25
Boolean addressIsNull(sockaddr_storage const &address)
SOCKLEN_T addressSize(sockaddr_storage const &address)
portNumBits portNum(struct sockaddr_storage const &address)
UsageEnvironment & operator<<(UsageEnvironment &s, const Port &p)
void copyAddress(struct sockaddr_storage &to, NetAddress const *from)
u_int32_t ipv4AddressBits
Definition: NetAddress.hh:38
Boolean IsMulticastAddress(struct sockaddr_storage const &address)
void setPortNum(struct sockaddr_storage &address, portNumBits portNum)
struct sockaddr_storage const & nullAddress(int addressFamily=AF_INET)
u_int16_t portNumBits
Definition: NetAddress.hh:102
u_int8_t ipv6AddressBits[16]
Definition: NetAddress.hh:39
Boolean operator==(struct sockaddr_storage const &left, struct sockaddr_storage const &right)
#define SOCKLEN_T
Definition: NetCommon.h:123
HashTable::Iterator * fIter
Definition: NetAddress.hh:165
Iterator(AddressPortLookupTable &table)
void * Add(struct sockaddr_storage const &address1, Port port, void *value)
Definition: NetAddress.hh:131
void * Lookup(struct sockaddr_storage const &address1, Port port)
Definition: NetAddress.hh:149
void * Lookup(struct sockaddr_storage const &address1, struct sockaddr_storage const &address2, Port port)
Boolean Remove(struct sockaddr_storage const &address1, struct sockaddr_storage const &address2, Port port)
virtual ~AddressPortLookupTable()
void * Add(struct sockaddr_storage const &address1, struct sockaddr_storage const &address2, Port port, void *value)
Boolean Remove(struct sockaddr_storage const &address1, Port port)
Definition: NetAddress.hh:140
void init(ipv6AddressBits const &addr)
AddressString(struct sockaddr_storage const &addr)
virtual ~AddressString()
AddressString(ipv6AddressBits const &addr)
AddressString(ipv4AddressBits const &addr)
AddressString(struct in6_addr const &addr)
void init(ipv4AddressBits const &addr)
AddressString(struct sockaddr_in const &addr)
AddressString(struct sockaddr_in6 const &addr)
char const * val() const
Definition: NetAddress.hh:196
AddressString(struct in_addr const &addr)
void * RemoveNext()
Iterator(NetAddressList const &addressList)
NetAddress const * nextAddress()
NetAddressList const & fAddressList
Definition: NetAddress.hh:89
virtual ~NetAddressList()
NetAddressList(NetAddressList const &orig)
unsigned fNumAddresses
Definition: NetAddress.hh:98
NetAddress const * firstAddress() const
void assign(unsigned numAddresses, NetAddress **addressArray)
unsigned numAddresses() const
Definition: NetAddress.hh:79
NetAddress ** fAddressArray
Definition: NetAddress.hh:99
NetAddressList(char const *hostname, int addressFamily=AF_UNSPEC)
NetAddressList & operator=(NetAddressList const &rightSide)
NetAddress(unsigned length=4)
unsigned length() const
Definition: NetAddress.hh:50
NetAddress(u_int8_t const *data, unsigned length=4)
u_int8_t const * data() const
Definition: NetAddress.hh:51
void clean()
void assign(u_int8_t const *data, unsigned length)
NetAddress & operator=(NetAddress const &rightSide)
u_int8_t * fData
Definition: NetAddress.hh:59
NetAddress(NetAddress const &orig)
unsigned fLength
Definition: NetAddress.hh:58
virtual ~NetAddress()
portNumBits fPortNum
Definition: NetAddress.hh:111
portNumBits num() const
Definition: NetAddress.hh:108
Port(portNumBits num)