|
||||
File indexing completed on 2025-01-18 10:15:40
0001 #ifndef __XRDNETADDR_HH__ 0002 #define __XRDNETADDR_HH__ 0003 /******************************************************************************/ 0004 /* */ 0005 /* X r d N e t A d d r . h h */ 0006 /* */ 0007 /* (c) 2013 by the Board of Trustees of the Leland Stanford, Jr., University */ 0008 /* All Rights Reserved */ 0009 /* Produced by Andrew Hanushevsky for Stanford University under contract */ 0010 /* DE-AC02-76-SFO0515 with the Department of Energy */ 0011 /* */ 0012 /* This file is part of the XRootD software suite. */ 0013 /* */ 0014 /* XRootD is free software: you can redistribute it and/or modify it under */ 0015 /* the terms of the GNU Lesser General Public License as published by the */ 0016 /* Free Software Foundation, either version 3 of the License, or (at your */ 0017 /* option) any later version. */ 0018 /* */ 0019 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */ 0020 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */ 0021 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */ 0022 /* License for more details. */ 0023 /* */ 0024 /* You should have received a copy of the GNU Lesser General Public License */ 0025 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */ 0026 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */ 0027 /* */ 0028 /* The copyright holder's institutional names and contributor's names may not */ 0029 /* be used to endorse or promote products derived from this software without */ 0030 /* specific prior written permission of the institution or contributor. */ 0031 /******************************************************************************/ 0032 0033 #include "XrdNet/XrdNetAddrInfo.hh" 0034 0035 //------------------------------------------------------------------------------ 0036 //! The XrdNetAddr class implements the manipulators for XrdNetAddrInfo. 0037 //------------------------------------------------------------------------------ 0038 0039 struct addrinfo; 0040 0041 class XrdNetAddr : public XrdNetAddrInfo 0042 { 0043 public: 0044 0045 //------------------------------------------------------------------------------ 0046 //! Determine if dynamic DNS has been set. 0047 //! 0048 //! @return True Dynamic DNS has been set. 0049 //! False Dynamic DNS has not been set. 0050 //------------------------------------------------------------------------------ 0051 0052 static bool DynDNS() {return dynDNS;} 0053 0054 //------------------------------------------------------------------------------ 0055 //! Determine if IPV4 mode has been set. 0056 //! 0057 //! @return True IPV4 mode has been set. 0058 //! False IPV4 mode has not been set. 0059 //------------------------------------------------------------------------------ 0060 0061 static bool IPV4Set() {return useIPV4;} 0062 0063 //------------------------------------------------------------------------------ 0064 //! Optionally set and also returns the port number for our address. 0065 //! 0066 //! @param pNum when negative it only returns the current port. Otherwise, 0067 //! it is taken as the value to be set. 0068 //! 0069 //! @return Success: The port number, which may be 0 if not set. 0070 //! Failure: -1 address is not an internet address or port is invalid. 0071 //------------------------------------------------------------------------------ 0072 0073 int Port(int pNum=-1); 0074 0075 //------------------------------------------------------------------------------ 0076 //! Register a host name with this IP address. This is not MT-safe! 0077 //! 0078 //! @param hName -> to a true host name which should be fully qualified. 0079 //! One of the IP addresses registered to this name must 0080 //! match the IP address associated with this object. 0081 //! 0082 //! @return True: Specified name is now associated with this address. 0083 //! False: Nothing changed, registration could not be verified. 0084 //------------------------------------------------------------------------------ 0085 0086 bool Register(const char *hName); 0087 0088 //------------------------------------------------------------------------------ 0089 //! Set the IP address and possibly the port number. 0090 //! 0091 //! @param hSpec 0 -> address is set to in6addr_any for binding via bind() 0092 //! (INADDR_ANY in IPV4 mode). 0093 //! !0 -> convert specification to an address. Valid formats: 0094 //! IPv4: nnn.nnn.nnn.nnn[:<port>] 0095 //! IPv6: [ipv6_addr][:<port>] **addr brackets required** 0096 //! IPvx: name[:port] x is determined by getaddrinfo() 0097 //! Unix: /<path> 0098 //! @param pNum >= 0 uses the value as the port number regardless of what 0099 //! is in hSpec, should it be supplied. However, if is 0100 //! present, it must be a valid port number or name. 0101 //! < 0 uses the positive value as the port number if the 0102 //! port number has not been specified in hSpec. 0103 //! **** When set to PortInSpec(the default, see below) the 0104 //! port number/name must be specified in hSpec. If it is 0105 //! not, an error is returned. 0106 //! 0107 //! @return Success: 0. 0108 //! Failure: Error message text describing the error. The message is in 0109 //! persistent storage and cannot be modified. 0110 //------------------------------------------------------------------------------ 0111 0112 static const int PortInSpec = (int)0x80000000; 0113 0114 const char *Set(const char *hSpec, int pNum=PortInSpec); 0115 0116 //------------------------------------------------------------------------------ 0117 //! Return multiple addresses. This form can only be used on the first element 0118 //! of this object that has been allocated as an array. This method is useful 0119 //! for getting all of the aliases assigned to a dns entry. 0120 //! The file descriptor association is set to a negative value. 0121 //! 0122 //! @param hSpec 0 -> address is set to in6addr_any for binding via bind() 0123 //! !0 -> convert specification to an address. Valid formats: 0124 //! IP.v4: nnn.nnn.nnn.nnn[:<port>] 0125 //! IP.v6: [ipv6_addr][:<port>] 0126 //! IP.xx: name[:port] xx is determined by getaddrinfo() 0127 //! @param maxIP number of elements in the array. 0128 //! @param numIP the number of IP addresses actually set (returned value). 0129 //! @param pNum >= 0 uses the value as the port number regardless of what 0130 //! is in hSpec, should it be supplied. However, if is 0131 //! present, it must be a valid port number or name. 0132 //! < 0 uses the positive value as the port number if the 0133 //! port number has not been specified in hSpec. 0134 //! **** When set to PortInSpec(the default, see below) the 0135 //! port number/name must be specified in hSpec. If it is 0136 //! not, an error is returned. 0137 //! @param forUDP when true addresses are usable for UDP connections. 0138 //! Otherwise, they are for TCP connections. 0139 //! 0140 //! @return Success: 0 with numIP set to the number of elements set. 0141 //! Failure: the error message text describing the error and 0142 //! numIP is set to zero. The message is in persistent 0143 //! storage and cannot be modified. 0144 //------------------------------------------------------------------------------ 0145 0146 const char *Set(const char *hSpec, int &numIP, int maxIP, 0147 int pNum=PortInSpec, bool forUDP=false); 0148 0149 //------------------------------------------------------------------------------ 0150 //! Set our address via a sockaddr structure. 0151 //! 0152 //! @param sockP a pointer to an initialized and valid sockaddr structure. 0153 //! @param sockFD the associated file descriptor and can be used to record 0154 //! the file descriptor returned by accept(). 0155 //! 0156 //! @return Success: Returns 0. 0157 //! Failure: Returns the error message text describing the error. The 0158 //! message is in persistent storage and cannot be modified. 0159 //------------------------------------------------------------------------------ 0160 0161 const char *Set(const struct sockaddr *sockP, int sockFD=-1); 0162 0163 //------------------------------------------------------------------------------ 0164 //! Set our address from the supplied socket file descriptor. 0165 //! 0166 //! @param sockFD a connected socket file descriptor. The value is also 0167 //! recorded as the associated file descriptor. 0168 //! @param peer When true the address is set from getpeername() 0169 //! When false the address is set from getsockname() 0170 //! 0171 //! @return Success: Returns 0. 0172 //! Failure: Returns the error message text describing the error. The 0173 //! message is in persistent storage and cannot be modified. 0174 //------------------------------------------------------------------------------ 0175 0176 const char *Set(int sockFD, bool peer=true); 0177 0178 //------------------------------------------------------------------------------ 0179 //! Set our address via and addrinfo structure and initialize the port. 0180 //! 0181 //! @param rP pointer to an addrinfo structure. 0182 //! @param port the port number to set. 0183 //! @param mapit when true maps IPv4 addresses to IPv6. Otherwise, does not. 0184 //! 0185 //! @return Success: Returns 0. 0186 //! Failure: Returns the error message text describing the error. The 0187 //! message is in persistent storage and cannot be modified. 0188 //------------------------------------------------------------------------------ 0189 0190 const char *Set(struct addrinfo *rP, int port, bool mapit=false); 0191 0192 //------------------------------------------------------------------------------ 0193 //! Set the cache time for address to name resolutions. This method should only 0194 //! be called during initialization time. The default is to not use the cache. 0195 //------------------------------------------------------------------------------ 0196 0197 static void SetCache(int keeptime); 0198 0199 //------------------------------------------------------------------------------ 0200 //! Set the dialect being spoken on this network link. 0201 //! 0202 //! @param dP Pointer to the dialect name. It must be permanently stable. 0203 //------------------------------------------------------------------------------ 0204 0205 void SetDialect(const char *dP) {protName = dP;} 0206 0207 //------------------------------------------------------------------------------ 0208 //! Indicate whether or not dynamic DNS is being used. This method should only 0209 //! be called during initialization time. The default is fixed DNS. 0210 //! 0211 //! @param onoff True if dynamic DNS is being used, false otherwise. 0212 //------------------------------------------------------------------------------ 0213 0214 static void SetDynDNS(bool onoff); 0215 0216 //------------------------------------------------------------------------------ 0217 //! Force this object to work in IPV4 mode only. This method permanently sets 0218 //! IPV4 mode which cannot be undone without a restart. It is meant to bypass 0219 //! broken IPV6 stacks on those unfortunate hosts that have one. It should be 0220 //! called before any other calls to this object (e.g. initialization time). 0221 //------------------------------------------------------------------------------ 0222 0223 static void SetIPV4(); 0224 0225 //------------------------------------------------------------------------------ 0226 //! Force this object to work in IPV6 mode using IPV6 or mapped IPV4 addresses. 0227 //! This method permanently sets IPV6 mode which cannot be undone without a 0228 //! restart. It is meant to disable the default mode which determines which 0229 //! address to use based on which address types are configured on the host 0230 //! (i.e. getaddrinfo() with hints AI_ADDRCONFIG|AI_V4MAPPED). 0231 //------------------------------------------------------------------------------ 0232 0233 static void SetIPV6(); 0234 0235 //------------------------------------------------------------------------------ 0236 //! Set the location for this address 0237 //! 0238 //! @param loc pointer to the structure that describes the location. See 0239 //! XrdnetAddrInfo for the definition of the stucture. 0240 //------------------------------------------------------------------------------ 0241 0242 void SetLocation(XrdNetAddrInfo::LocInfo &loc); 0243 0244 //------------------------------------------------------------------------------ 0245 //! Set the location's TLS state. 0246 //! 0247 //! @param val True if TLS is being used, false otherwise. 0248 //------------------------------------------------------------------------------ 0249 0250 void SetTLS(bool val); 0251 0252 //------------------------------------------------------------------------------ 0253 //! Assignment operator and copy constructor are inherited, no need to define 0254 //------------------------------------------------------------------------------ 0255 0256 //------------------------------------------------------------------------------ 0257 //! Constructor 0258 //! 0259 //! @param addr A pointer to an initialized and valid sockaddr or sockaddr 0260 //! compatible structure used to initialize the address. 0261 //! @param port Uses the address of the current host and the speoified 0262 //! port number to initilize the address. 0263 //------------------------------------------------------------------------------ 0264 0265 XrdNetAddr() : XrdNetAddrInfo() {} 0266 0267 XrdNetAddr(const XrdNetAddr *addr) : XrdNetAddrInfo(addr) {} 0268 0269 XrdNetAddr(const sockaddr *addr) : XrdNetAddrInfo() 0270 {Set(addr);} 0271 0272 XrdNetAddr(const sockaddr_in *addr) : XrdNetAddrInfo() 0273 {Set((sockaddr *)addr);} 0274 0275 XrdNetAddr(const sockaddr_in6 *addr) : XrdNetAddrInfo() 0276 {Set((sockaddr *)addr);} 0277 0278 XrdNetAddr(int port); 0279 0280 //------------------------------------------------------------------------------ 0281 //! Destructor 0282 //------------------------------------------------------------------------------ 0283 0284 ~XrdNetAddr() {} 0285 private: 0286 static struct addrinfo *Hints(int htype, int stype); 0287 bool Map64(); 0288 0289 static struct addrinfo *hostHints; 0290 static struct addrinfo *huntHintsTCP; 0291 static struct addrinfo *huntHintsUDP; 0292 static bool useIPV4; 0293 static bool dynDNS; 0294 }; 0295 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |