Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-11 10:53:42

0001 #ifndef __XRDNETADDRINFO_HH__
0002 #define __XRDNETADDRINFO_HH__
0003 /******************************************************************************/
0004 /*                                                                            */
0005 /*                     X r d N e t A d d r I n f o . 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 <cinttypes>
0034 #include <cstdlib>
0035 #include <cstring>
0036 #include <netinet/in.h>
0037 #include <sys/socket.h>
0038 #include <sys/un.h>
0039 
0040 #include "XrdNet/XrdNetSockAddr.hh"
0041 #include "XrdSys/XrdSysPlatform.hh"
0042   
0043 //------------------------------------------------------------------------------
0044 //! The XrdNetAddrInfo class is meant to provide read/only access to members
0045 //! that only class XrdNetAddr can manipulate. This allows the generic use of
0046 //! the XrdNetAddr class but provides a means of exporting that information to
0047 //! other objects without allowing those objects to modify the information.
0048 //------------------------------------------------------------------------------
0049 
0050 struct addrinfo;
0051 class  XrdNetCache;
0052 
0053 class XrdNetAddrInfo
0054 {
0055 public:
0056 
0057 //------------------------------------------------------------------------------
0058 //! Provide the protocol languag being spoken (e.g. xroot, http, etc).
0059 //!
0060 //! @return Pointer to the protocol language being used. If unknown, nil ia
0061 //!         returned. Use the isUsingTLS() method to see if a secure version of
0062 //!         the protocol is being used.
0063 //------------------------------------------------------------------------------
0064 
0065 const char *Dialect() {return protName;}
0066 
0067 //------------------------------------------------------------------------------
0068 //! Provide our address family.
0069 //!
0070 //! @return Success: Returns AF_INET, AF_INET6, or AF_UNIX.
0071 //!         Failure: Returns 0, address is not valid.
0072 //------------------------------------------------------------------------------
0073 
0074 int         Family() const {return static_cast<int>(IP.Addr.sa_family);}
0075 
0076 //------------------------------------------------------------------------------
0077 //! Format our address into a supplied buffer with one of the following layouts
0078 //! (the ':<port>' or ':/path' can be omitted if desired, see fmtOpts param):
0079 //! IP.xx:   host_name:<port>
0080 //! IP.v4:   a.b.c.d:<port>
0081 //! IP.4to6: [::ffff:a.b.c.d]:<port>  | [::a.b.c.d]:<port>
0082 //! IP.v6:   [a:b:c:d:e:f:g:h]:<port>
0083 //! IP.Unix: localhost:/<path>
0084 //!
0085 //! @param  bAddr    address of buffer for result
0086 //! @param  bLen     length  of buffer
0087 //! @param  fmtType  specifies the type of format desired via fmtUse enum.
0088 //! @param  fmtOpts  additional formatting options (can be or'd):
0089 //!                  noPort    - do not append the port number to the address.
0090 //!                  noPortRaw - no port and no brackets for IPv6.
0091 //!                  old6Map4  - use deprecated IPV6 mapped format '[::x.x.x.x]'
0092 //!
0093 //! @return Success: The number of characters (less null) in Buff.
0094 //! @return Failure: 0 (buffer is too small or not a valid address). However,
0095 //!                  if bLen > 0 the buffer will contain a null terminated
0096 //!                  string of up to 8 question marks.
0097 //------------------------------------------------------------------------------
0098 
0099 enum fmtUse {fmtAuto=0, //!< Hostname if already resolved o/w use fmtAddr
0100              fmtName,   //!< Hostname if it is resolvable o/w use fmtAddr
0101              fmtAddr,   //!< Address using suitable ipv4 or ipv6 format
0102              fmtAdv6};  //!< Address only in ipv6 format
0103 
0104 static const int noPort    = 0x0000001; //!< Do not add port number
0105 static const int noPortRaw = 0x0000002; //!< Use raw address format (no port)
0106 static const int old6Map4  = 0x0000004; //!< Use deprecated IPV6 mapped format
0107 static const int prefipv4  = 0x0000008; //!< Use if mapped  IPV4 actual format
0108 
0109 int         Format(char *bAddr, int bLen, fmtUse fmtType=fmtAuto, int fmtOpts=0);
0110 
0111 //------------------------------------------------------------------------------
0112 //! Indicate whether or not our address is the loopback address. Use this
0113 //! method to gaurd against UDP packet spoofing.
0114 //!
0115 //! @return True:    This is     the loopback address.
0116 //! @return False:   This is not the loopback address.
0117 //------------------------------------------------------------------------------
0118 
0119 bool        isLoopback();
0120 
0121 //------------------------------------------------------------------------------
0122 //! Indicate whether or not a string is a possible hostname and not IP address.
0123 //! The return value does not aimply any kind of validity. For instance, a
0124 //! false return indicates this is not a valid hostname does not mean it is
0125 //! a valid IP address.
0126 //!
0127 //! @param name      The string to check.
0128 //!
0129 //! @return True:    This is     a possible hostname (i.e. not IP address).
0130 //! @return False:   This is not a possible hostname.
0131 //------------------------------------------------------------------------------
0132 
0133 static bool isHostName(const char *name);
0134 
0135 //------------------------------------------------------------------------------
0136 //! Indicate whether or not our address is if the desired type.
0137 //!
0138 //! @param ipType    The IP address version to test (see enum below).
0139 //!
0140 //! @return True:    This is     the address version of ipType.
0141 //! @return False:   This is not the address version of ipType.
0142 //------------------------------------------------------------------------------
0143 
0144 enum IPType {IPv4 = AF_INET, IPv6 = AF_INET6, IPuX = AF_UNIX};
0145 
0146 bool        isIPType(IPType ipType) const {return IP.Addr.sa_family == ipType;}
0147 
0148 //------------------------------------------------------------------------------
0149 //! Indicate whether or not our address is local.
0150 //!
0151 //! @return True:    This address is     local.
0152 //!         False:   This address is not local.
0153 //------------------------------------------------------------------------------
0154 
0155 bool        isLocal();
0156 
0157 //------------------------------------------------------------------------------
0158 //! Indicate whether or not our address is an IPv4 mapped to IPv6 address.
0159 //!
0160 //! @return True:  The address is     a mapped IPv4 address.
0161 //!         False: The address is not a mapped IPv4 address.
0162 //------------------------------------------------------------------------------
0163 
0164 bool        isMapped() const {return IP.Addr.sa_family == AF_INET6
0165                                   && IN6_IS_ADDR_V4MAPPED(&IP.v6.sin6_addr);
0166                              }
0167 
0168 //------------------------------------------------------------------------------
0169 //! Indicate whether or not our address is private.
0170 //!
0171 //! @return True:    This address is     private.
0172 //!         False:   This address is not private.
0173 //------------------------------------------------------------------------------
0174 
0175 bool        isPrivate();
0176 
0177 //------------------------------------------------------------------------------
0178 //! Indicate whether or not our address is registered in the DNS.
0179 //!
0180 //! @return True:    This address is     registered.
0181 //!         False:   This address is not registered.
0182 //------------------------------------------------------------------------------
0183 
0184 bool        isRegistered();
0185 
0186 //------------------------------------------------------------------------------
0187 //! Indicate whether or not the endpoint is using TLS for communications.
0188 //!
0189 //! @return True:    This address is     using TLS.
0190 //!         False:   This address is not using TLS.
0191 //------------------------------------------------------------------------------
0192 
0193 bool        isUsingTLS();
0194 
0195 //------------------------------------------------------------------------------
0196 //! Obtain the location of this address.
0197 //!
0198 //! @return !0       pointer to the unverified location information. Not all
0199 //!                  fields may be set.
0200 //! @return =0       location information is not available.
0201 //------------------------------------------------------------------------------
0202 
0203 struct LocInfo
0204       {unsigned char  Country[2]; //!< Two letter TLD country code
0205        unsigned char  Region;     //!< Region (may combine adjacent countries)
0206        unsigned char  Locale;     //!< Locale (may combine adjacent regions)
0207                 char  TimeZone;   //!< +/- hours from GMT (-128 if not set)
0208                 char  Flags;      //!< Flags
0209                 short Speed;      //!< I/F speed (Gb*1024/100)(not supported)
0210                 int   Latitude;   //!< Degrees +/- xx.xxxxxx  (not supported)
0211                 int   Longtitude; //!< Degrees +/- xx.xxxxxx  (not supported)
0212 
0213                 LocInfo() : Region(0), Locale(0), TimeZone(-128), Flags(0),
0214                             Speed(0), Latitude(0), Longtitude(0) {*Country = 0;}
0215 
0216        };
0217 
0218 const struct
0219 LocInfo    *Location() {return (addrLoc.Country[0] ? &addrLoc : 0);}
0220 
0221 //------------------------------------------------------------------------------
0222 //! Convert our IP address to the corresponding [host] name.
0223 //!
0224 //! @param  eName    value to return when the name cannot be determined.
0225 //! @param  eText    when not null, the reason for a failure is returned.
0226 //!
0227 //! @return Success: Pointer to the name or ip address with eText, if supplied,
0228 //!                  set to zero. The memory is owned by the object and is
0229 //!                  deleted when the object is deleted or Set() is called.
0230 //!         Failure: eName param and if eText is not zero, returns a pointer
0231 //!                  to a message describing the reason for the failure. The
0232 //!                  message is in persistent storage and cannot be modified.
0233 //------------------------------------------------------------------------------
0234 
0235 const char *Name(const char *eName=0, const char **eText=0);
0236 
0237 //------------------------------------------------------------------------------
0238 //! Provide a pointer to our socket address suitable for use in calls to methods
0239 //! that require our internal format of sock addr. A value is only returned for
0240 //! IPV6/4 addresses and is nill otherwise. The pointer refers to memory
0241 //! allocated by this object and becomes invalid should the object be deleted.
0242 //! Use SockSize() to get its logical length.
0243 //------------------------------------------------------------------------------
0244 
0245 const
0246 XrdNetSockAddr  *NetAddr() {return (sockAddr == (void *)&IP ? &IP : 0);}
0247 
0248 //------------------------------------------------------------------------------
0249 //! Return the port number for our address.
0250 //!
0251 //! @return Success: The port number, which may be 0 if not set.
0252 //!         Failure: -1 address is not an internet address or port is invalid.
0253 //------------------------------------------------------------------------------
0254 
0255 int         Port();
0256 
0257 //------------------------------------------------------------------------------
0258 //! Provide our protocol family.
0259 //!
0260 //! @return Success: Returns PF_INET, PF_INET6, or PF_UNIX.
0261 //!         Failure: Returns 0, address is not valid.
0262 //------------------------------------------------------------------------------
0263 
0264 int         Protocol() {return static_cast<int>(protType);}
0265 
0266 //------------------------------------------------------------------------------
0267 //! Check if the IP address in this object is the same as the one passed.
0268 //!
0269 //! @param ipAddr    points to the network address object to compare.
0270 //! @param plusPort  when true, port values must also match. In any case, both
0271 //!                  addresses must be of the same address family.
0272 //!
0273 //! @return Success: True  (addresses are     the same).
0274 //!         Failure: False (addresses are not the same).
0275 //!
0276 //! Note: implemented in terms of const version
0277 //------------------------------------------------------------------------------
0278 
0279 int         Same(const XrdNetAddrInfo *ipAddr, bool plusPort=false);
0280 
0281 //------------------------------------------------------------------------------
0282 //! Provide a pointer to our socket address suitable for use in calls to
0283 //! functions that require one (e.g. bind() etc). The pointer refers to memory
0284 //! allocated by this object and becomes invalid should the object be deleted
0285 //! or when Set() is called. Use SockSize() to get its length.
0286 //------------------------------------------------------------------------------
0287 
0288 const
0289 sockaddr   *SockAddr() {return sockAddr;}
0290 
0291 //------------------------------------------------------------------------------
0292 //! Provide the length of our socket adress. Useful for system calls needing it.
0293 //!
0294 //! @return Success: Returns the length of the address returned by SockAddr().
0295 //!         Failure: Returns 0, address is not valid.
0296 //------------------------------------------------------------------------------
0297 
0298 SOCKLEN_t   SockSize() {return static_cast<SOCKLEN_t>(addrSize);}
0299 
0300 //------------------------------------------------------------------------------
0301 //! Get the associated file descriptor.
0302 //!
0303 //! @return The associated file descriptor. If negative, no association exists.
0304 //------------------------------------------------------------------------------
0305 
0306 int         SockFD() {return (sockNum ? sockNum : -1);}
0307 
0308 //------------------------------------------------------------------------------
0309 //! Assignment operator
0310 //------------------------------------------------------------------------------
0311 
0312 XrdNetAddrInfo &operator=(XrdNetAddrInfo const &rhs)
0313                {if (&rhs != this)
0314                    {memmove(&IP, &rhs.IP, sizeof(IP));
0315                     addrSize = rhs.addrSize; sockNum = rhs.sockNum;
0316                     protType = rhs.protType;
0317                     protFlgs = rhs.protFlgs;
0318                     protName = rhs.protName;
0319                     if (hostName) free(hostName);
0320                     hostName = (rhs.hostName ? strdup(rhs.hostName):0);
0321                     addrLoc = rhs.addrLoc;
0322                     if (rhs.sockAddr != &rhs.IP.Addr)
0323                        {if (!unixPipe || sockAddr == &IP.Addr)
0324                            unixPipe = new sockaddr_un;
0325                         memcpy(unixPipe, rhs.unixPipe, sizeof(sockaddr_un));
0326                        } else sockAddr = &IP.Addr;
0327                    }
0328                 return *this;
0329                }
0330 
0331 //------------------------------------------------------------------------------
0332 //! Copy constructor
0333 //------------------------------------------------------------------------------
0334 
0335                XrdNetAddrInfo(XrdNetAddrInfo const &oP)
0336                              {hostName = 0;
0337                               unixPipe = 0;
0338                               *this = oP;
0339                              }
0340 
0341 //------------------------------------------------------------------------------
0342 //! Constructor
0343 //------------------------------------------------------------------------------
0344 
0345             XrdNetAddrInfo() : hostName(0), addrSize(0), protType(0),
0346                                protFlgs(0), sockNum(0), protName(0)
0347                            {IP.Addr.sa_family = 0;
0348                             sockAddr = &IP.Addr;
0349                            }
0350 
0351             XrdNetAddrInfo(const XrdNetAddrInfo *addr) : hostName(0) {*this = *addr;}
0352 
0353 //------------------------------------------------------------------------------
0354 //! Destructor
0355 //------------------------------------------------------------------------------
0356 
0357            ~XrdNetAddrInfo() {if (hostName) free(hostName);
0358                           if (sockAddr != &IP.Addr) delete unixPipe;
0359                          }
0360 
0361 protected:
0362        char               *LowCase(char *str);
0363        int                 QFill(char *bAddr, int bLen);
0364        int                 Resolve();
0365 
0366 static XrdNetCache        *dnsCache;
0367 
0368 XrdNetSockAddr             IP;
0369 union {struct sockaddr    *sockAddr;
0370        struct sockaddr_un *unixPipe;
0371       };
0372 char                      *hostName;
0373 LocInfo                    addrLoc;
0374 unsigned short             addrSize;
0375 unsigned char              protType;
0376 unsigned char              protFlgs;
0377 int                        sockNum;
0378 const char                *protName;
0379 
0380 // Flag settings in protFlgs
0381 //
0382 static const char          isTLS = 0x01; //!< Location using TLS
0383 };
0384 #endif