Skip to content

Commit

Permalink
[NET] Accommodate K8s network namespaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
abh3 authored and gganis committed Nov 23, 2021
1 parent a1e8796 commit 208511b
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 47 deletions.
11 changes: 6 additions & 5 deletions src/XrdNet/XrdNetAddr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

#include "XrdNet/XrdNetAddr.hh"
#include "XrdNet/XrdNetCache.hh"
#include "XrdNet/XrdNetIdentity.hh"
#include "XrdNet/XrdNetUtils.hh"
#include "XrdSys/XrdSysE2T.hh"

Expand Down Expand Up @@ -90,15 +91,15 @@ bool XrdNetAddr::dynDNS = false;
/******************************************************************************/
/* C o n s t r u c t o r */
/******************************************************************************/

XrdNetAddr::XrdNetAddr(int port) : XrdNetAddrInfo()
{
char buff[1024];
const char *fqn = XrdNetIdentity::FQN();

// Get our host name and initialize this object with it
// Otherwise, we cannot initialize this object so force an error!
//
gethostname(buff, sizeof(buff));
Set(buff, port);
if (!fqn) fqn = "No_DNS_Name!";
Set(fqn, port);
}

/******************************************************************************/
Expand Down
47 changes: 22 additions & 25 deletions src/XrdNet/XrdNetIF.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

#include "XrdNet/XrdNetAddr.hh"
#include "XrdNet/XrdNetIF.hh"
#include "XrdNet/XrdNetIdentity.hh"
#include "XrdNet/XrdNetUtils.hh"
#include "XrdOuc/XrdOucTList.hh"
#include "XrdSys/XrdSysE2T.hh"
Expand Down Expand Up @@ -107,9 +108,9 @@ const char *sName[hasNum] = {"pub4 ", "prv4 ", "pub6 ", "prv6"};

XrdSysError *XrdNetIF::eDest = 0;

char *XrdNetIF::myDomain = XrdNetIF::SetDomain();
const char *XrdNetIF::myDomain = XrdNetIF::GetDomain();

char *XrdNetIF::ifCfg[2] = {0,0};
char *XrdNetIF::ifCfg[2] = {0,0}; // public, private

const char *XrdNetIF::ifTName[ifMax] = {"public IPv4", // 01
"private IPv4", // 02
Expand Down Expand Up @@ -428,6 +429,7 @@ int XrdNetIF::GetIF(XrdOucTList **ifList, const char **eText)
struct ifaddrs *ifBase, *ifP;
XrdOucTList *tLP, *tList = 0, *tLast = 0;
int n = 0;
bool anyIF = (ifCfg[0] == 0 && ifCfg[1] == 0);

if (getifaddrs(&ifBase) < 0)
{if (eText) *eText = XrdSysE2T(errno);
Expand All @@ -442,7 +444,7 @@ int XrdNetIF::GetIF(XrdOucTList **ifList, const char **eText)
ifP = ifBase;
while(ifP)
{if ((ifP->ifa_addr != 0)
&& (!ifList || IsOkName(ifP->ifa_name, ifIdx))
&& (!ifList || anyIF || IsOkName(ifP->ifa_name, ifIdx))
&& (ifP->ifa_flags & (IFF_UP))
&& (ifP->ifa_flags & (IFF_RUNNING))
&& !(ifP->ifa_flags & (IFF_LOOPBACK))
Expand All @@ -459,7 +461,7 @@ int XrdNetIF::GetIF(XrdOucTList **ifList, const char **eText)
{netAddr.Set(ifP->ifa_addr);
if ((iLen = netAddr.Format(ipBuff, sizeof(ipBuff),
XrdNetAddrInfo::fmtAddr,XrdNetAddrInfo::noPort)))
{sval[2] = ifIdx;
{sval[2] = (anyIF ? 3 : ifIdx);
sval[1] = (netAddr.isPrivate() ? 1 : 0);
sval[0] = iLen;
haveIF |= (sval[1] ? prvIF[ifT] : pubIF[ifT]);
Expand Down Expand Up @@ -519,9 +521,10 @@ int XrdNetIF::GetIF(XrdOucTList **ifList, const char **eText)
int XrdNetIF::GetIF(char *buff, int blen, const char **eText, bool show)
{
XrdOucTList *ifP, *ifN;
const char *ifName[3] = {ifCfg[0], ifCfg[1], "anon"};
char *bP = buff;
int n, bLeft = blen-8;
bool ifOK[2] = {false, false};
bool ifOK[3] = {false, false, false};

#ifndef HAVE_GETIFADDRS
// Display warning on how we are getting the interface addresses
Expand All @@ -544,7 +547,7 @@ int XrdNetIF::GetIF(char *buff, int blen, const char **eText, bool show)
ifOK[ifP->sval[2]] = true;
if (show && eDest)
{const char *kind = (ifP->sval[1] ? " private" : " public ");
eDest->Say("Config ", ifCfg[ifP->sval[2]], kind,
eDest->Say("Config ", ifName[ifP->sval[2]], kind,
" network interface: ", ifP->text);
}
ifN = ifP->next; delete ifP;
Expand Down Expand Up @@ -584,6 +587,19 @@ int XrdNetIF::GetIF(char *&ifline, const char **eText, bool show)
return n;
}

/******************************************************************************/
/* Private: G e t D o m a i n */
/******************************************************************************/

const char *XrdNetIF::GetDomain()
{
const char *dnP = XrdNetIdentity::Domain();

// Get our fully resilved name (this doesn't always work)
//
return (dnP ? dnP+1 : 0);
}

/******************************************************************************/
/* I n D o m a i n */
/******************************************************************************/
Expand Down Expand Up @@ -666,25 +682,6 @@ void XrdNetIF::Routing(XrdNetIF::netType nettype)
else ifMaskVec = ifMaskComm;
}

/******************************************************************************/
/* Private: S e t D o m a i n */
/******************************************************************************/

char *XrdNetIF::SetDomain()
{
XrdNetAddr myAddr((int)0);
const char *hnP;

// Get our fully resilved name (this doesn't always work)
//
if (!(hnP = myAddr.Name()) || !(hnP = index(hnP,'.')) || !(*(hnP+1)))
return 0;

// Return the components after the first as the domain name
//
return strdup(hnP+1);
}

/******************************************************************************/
/* S e t I F */
/******************************************************************************/
Expand Down
7 changes: 4 additions & 3 deletions src/XrdNet/XrdNetIF.hh
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,10 @@ struct ifAddrs
bool GenAddrs(ifAddrs &ifTab, XrdNetAddrInfo *src);
bool GenAddrs(ifAddrs &ifTab, const char *hName, bool wantV6);
bool GenIF(XrdNetAddrInfo **src, int srcnum, const char *xName=0);
static const
char *GetDomain();
static
bool IsOkName(const char *ifn, short &ifIdx);
static
char *SetDomain();
void SetIFPP();
bool SetIF64(bool retVal);
static
Expand Down Expand Up @@ -437,7 +437,8 @@ char ifAvail;

static
XrdSysError *eDest;
static char *myDomain;
static
const char *myDomain;
static char *ifCfg[2];
static
const char *ifTName[ifMax];
Expand Down
155 changes: 155 additions & 0 deletions src/XrdNet/XrdNetIdentity.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/******************************************************************************/
/* */
/* X r d N e t I d e n t i t y . h h */
/* */
/* (c) 2021 by the Board of Trustees of the Leland Stanford, Jr., University */
/* Produced by Andrew Hanushevsky for Stanford University under contract */
/* DE-AC02-76-SFO0515 with the Department of Energy */
/* */
/* This file is part of the XRootD software suite. */
/* */
/* XRootD is free software: you can redistribute it and/or modify it under */
/* the terms of the GNU Lesser General Public License as published by the */
/* Free Software Foundation, either version 3 of the License, or (at your */
/* option) any later version. */
/* */
/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
/* License for more details. */
/* */
/* You should have received a copy of the GNU Lesser General Public License */
/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
/* */
/* The copyright holder's institutional names and contributor's names may not */
/* be used to endorse or promote products derived from this software without */
/* specific prior written permission of the institution or contributor. */
/******************************************************************************/

#include <string.h>
#include <unistd.h>

#include "XrdNet/XrdNetAddr.hh"
#include "XrdNet/XrdNetIdentity.hh"
#include "XrdNet/XrdNetIF.hh"
#include "XrdOuc/XrdOucTList.hh"
#include "XrdSys/XrdSysE2T.hh"

/******************************************************************************/
/* O n e T i m e S t a t i c I n i t i a l i z a t i o n */
/******************************************************************************/

namespace
{
char *getMyFQN(const char *&myDom, const char *&myErr)
{
XrdNetAddr theAddr;
XrdOucTList *ifList, *ifNow;
const char *dnsName, *domP;
char *theName[2] = {0}, *theDom[2] = {0}, hName[256];
int hnLen;

// Obtain the host name, this is mandatory.
//
if (gethostname(hName, sizeof(hName)))
{myErr = XrdSysE2T(errno); myDom = 0; return 0;}
hnLen = strlen(hName);

// First step it to get all IP addresses configured on this machine
//
if (!XrdNetIF::GetIF(&ifList, &myErr))
{myDom = 0; return strdup(hName);}

// Run through the interfaces and try to get the hostname associated with
// this machine. Note that we may have public and private addresses and
// they may have different hostname attached. We only accept the hostname
// that matches what is returned by gethostname().
//
while((ifNow = ifList))
{int i = (ifNow->sval[1] ? 1 : 0); // Private | public

if (i >= 0 && theName[i] == 0 && !theAddr.Set(ifNow->text, 0)
&& (dnsName = theAddr.Name(0,&myErr)) && (domP = index(dnsName,'.')))
{int n = domP - dnsName;
if (n == hnLen && !strncmp(hName, dnsName, n))
{theName[i] = strdup(dnsName);
theDom[i] = theName[i] + n;
}
}
ifList = ifList->next;
delete ifNow;
}

// Fix up error pointer
//
if (myErr == 0) myErr = "no error";

// We prefer the public name should we have it
//
if (theName[0])
{if (theName[1]) free(theName[1]);
myDom = theDom[0];
return theName[0];
}

// Use the private name should we have it
//
if (theName[1])
{myDom = theDom[1];
return theName[1];
}

// Concote a name using old-style DNS resolution. This may not work if
// DNS namespaces are being used (e.g. k8s environments).
//
if ((myErr = theAddr.Set(hName,0))) dnsName = 0;
else dnsName = theAddr.Name(0, &myErr);

// Check if this worked
//
if (dnsName)
{theName[0] = strdup(dnsName);
if (!(myDom = index(theName[0], '.'))) myDom = "";
return theName[0];
}

// Fallback to using the simple unqualified hostname, this still mae OK
//
theName[0] = strdup(hName);
myDom = theName[0] + hnLen;
return theName[0];
}
}

/******************************************************************************/
/* S t a t i c M e m b e r s */
/******************************************************************************/

// Note that we are gauranteed that this will be fully initialzed prior
// to any method called that uses these values irrespective of static
// initialization order, even though statically initialized.

const char *XrdNetIdentity::DNS_Domain;
const char *XrdNetIdentity::DNS_Error;
const char *XrdNetIdentity::DNS_FQN = getMyFQN(DNS_Domain, DNS_Error);

/******************************************************************************/
/* D o m a i n */
/******************************************************************************/

const char *XrdNetIdentity::Domain(const char **eText)
{
if (eText) *eText = DNS_Error;
return DNS_Domain;
}

/******************************************************************************/
/* F Q N */
/******************************************************************************/

const char *XrdNetIdentity::FQN(const char **eText)
{
if (eText) *eText = DNS_Error;
return DNS_FQN;
}
49 changes: 49 additions & 0 deletions src/XrdNet/XrdNetIdentity.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#ifndef __NETIDENTITY__
#define __NETIDENTITY__
/******************************************************************************/
/* */
/* X r d N e t I d e n t i t y . h h */
/* */
/* (c) 2021 by the Board of Trustees of the Leland Stanford, Jr., University */
/* Produced by Andrew Hanushevsky for Stanford University under contract */
/* DE-AC02-76-SFO0515 with the Department of Energy */
/* */
/* This file is part of the XRootD software suite. */
/* */
/* XRootD is free software: you can redistribute it and/or modify it under */
/* the terms of the GNU Lesser General Public License as published by the */
/* Free Software Foundation, either version 3 of the License, or (at your */
/* option) any later version. */
/* */
/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
/* License for more details. */
/* */
/* You should have received a copy of the GNU Lesser General Public License */
/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
/* */
/* The copyright holder's institutional names and contributor's names may not */
/* be used to endorse or promote products derived from this software without */
/* specific prior written permission of the institution or contributor. */
/******************************************************************************/

class XrdNetIdentity
{
public:

static const char *Domain(const char **eText=0);

static const char *FQN(const char **etext=0);

XrdNetIdentity() {}
~XrdNetIdentity() {}

private:

static const char *DNS_FQN;
static const char *DNS_Domain; // Starts with leading dot!
static const char *DNS_Error; // Error indicator for debugging only
};
#endif

0 comments on commit 208511b

Please sign in to comment.