XRootD
Loading...
Searching...
No Matches
XrdNetIF.cc
Go to the documentation of this file.
1/******************************************************************************/
2/* */
3/* X r d N e t I F . c c */
4/* */
5/* (c) 2011 by the Board of Trustees of the Leland Stanford, Jr., University */
6/* All Rights Reserved */
7/* Produced by Andrew Hanushevsky for Stanford University under contract */
8/* DE-AC02-76-SFO0515 with the Department of Energy */
9/* */
10/* This file is part of the XRootD software suite. */
11/* */
12/* XRootD is free software: you can redistribute it and/or modify it under */
13/* the terms of the GNU Lesser General Public License as published by the */
14/* Free Software Foundation, either version 3 of the License, or (at your */
15/* option) any later version. */
16/* */
17/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
18/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
19/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
20/* License for more details. */
21/* */
22/* You should have received a copy of the GNU Lesser General Public License */
23/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
24/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
25/* */
26/* The copyright holder's institutional names and contributor's names may not */
27/* be used to endorse or promote products derived from this software without */
28/* specific prior written permission of the institution or contributor. */
29/******************************************************************************/
30
31#include <cctype>
32#include <netdb.h>
33#include <cstdio>
34#include <sys/types.h>
35
36#ifdef HAVE_GETIFADDRS
37#include <net/if.h>
38#include <ifaddrs.h>
39#endif
40
41#include "XrdNet/XrdNetAddr.hh"
42#include "XrdNet/XrdNetIF.hh"
44#include "XrdNet/XrdNetUtils.hh"
45#include "XrdOuc/XrdOucTList.hh"
46#include "XrdSys/XrdSysE2T.hh"
47#include "XrdSys/XrdSysError.hh"
48
49#include <iostream>
50
51/******************************************************************************/
52/* L o c a l S t a t i c s */
53/******************************************************************************/
54
55namespace
56{
57// Selection mask values
58//
59const char hasPub4 = 0x01;
60const char hasPrv4 = 0x02;
61const char hasPub6 = 0x04;
62const char hasPrv6 = 0x08;
63const char hasAny4 = hasPub4 | hasPrv4;
64const char hasAny6 = hasPub6 | hasPrv6;
65const int hasNum = 4;
66
67// Name translation table
68//
69const char sMask[hasNum] = {hasPub4, hasPrv4, hasPub6, hasPrv6};
70const char *sName[hasNum] = {"pub4 ", "prv4 ", "pub6 ", "prv6"};
71
72// common -> Only private addresses may select any node. Public must select
73// a node that has a public address.
74//
75 const char ifMaskComm [XrdNetIF::ifMax] = {hasPub4, hasAny4,
76 hasPub6, hasAny6,
77 hasPub4 | hasPub6,
78 hasAny6 | hasAny4,
79 hasPub6 | hasPub4,
80 hasAny4 | hasAny6
81 };
82
83// local -> Public-private address is immaterial for node selection.
84//
85 const char ifMaskLocal[XrdNetIF::ifMax] = {hasAny4, hasAny4,
86 hasAny6, hasAny6,
87 hasAny4 | hasAny6,
88 hasAny4 | hasAny6,
89 hasAny6 | hasAny4,
90 hasAny6 | hasAny4
91 };
92
93// split -> Address type may only select node with that address type.
94//
95 const char ifMaskSplit[XrdNetIF::ifMax] = {hasPub4, hasPrv4,
96 hasPub6, hasPrv6,
97 hasPub4 | hasPub6,
98 hasPrv4 | hasPrv6,
99 hasPub6 | hasPub4,
100 hasPrv6 | hasPrv4
101 };
102}
103
104/******************************************************************************/
105/* S t a t i c M e m b e r s */
106/******************************************************************************/
107
108XrdSysError *XrdNetIF::eDest = 0;
109
110const char *XrdNetIF::myDomain = XrdNetIF::GetDomain();
111
112char *XrdNetIF::ifCfg[2] = {0,0}; // public, private
113
114const char *XrdNetIF::ifTName[ifMax] = {"public IPv4", // 01
115 "private IPv4", // 02
116 "public IPv6", // 04
117 "private IPv6", // 08
118 "public",
119 "private",
120 "public",
121 "private"
122 };
123
124
125// The following vector is suitable only for local routing. It is reset
126// to the appropriate selection bits when Routing() is called.
127//
128const char *XrdNetIF::ifMaskVec = ifMaskLocal;
129
130XrdNetIF::netType XrdNetIF::netRoutes = XrdNetIF::netLocal;
131
132int XrdNetIF::dfPort = 1094;
133
134XrdNetIF::ifData XrdNetIF::ifNull;
135
136bool XrdNetIF::rPIPA = false;
137
138/******************************************************************************/
139/* D i s p l a y */
140/******************************************************************************/
141
142void XrdNetIF::Display(const char *pfx)
143{
144 static const char *ifN[] = {"pub4", "prv4", "pub6", "prv6"};
145 static const char *ifT[] = {"all4", 0, "all6", 0};
146 static const char *nNM[] = {"local", "split", "common", "local"};
147 const char *iHX[hasNum] = {"", "", "", ""};
148 const char *ifRType, *hName = "";
149 char buff[256];
150 bool nameOK = false;
151
152// If we have no error routing object, just return
153//
154 if (!eDest) return;
155
156// Get a hostname
157//
158 for (int i = 0; i < (int)ifNum; i++)
159 {if (ifName[i] != &ifNull)
160 {hName = ifName[i]->iVal;
161 if (ifxDNS[i]) {nameOK = true; break;}
162 }
163 }
164
165// Compute selection mask
166//
167 for (int i = 0; i < hasNum; i++)
168 if (ifMask & sMask[i]) iHX[i] = sName[i];
169
170// Print results
171//
172 sprintf(buff, ": %s %s%s%s%s", nNM[ifRoute], iHX[0],iHX[1],iHX[2],iHX[3]);
173 eDest->Say(pfx, "Routing for ", hName, buff);
174
175 for (int i = 0; i < (int)ifNum; i++)
176 {if (ifName[i] != &ifNull)
177 {if (ifT[i] && ifDest[i] == ifDest[i+1]) {ifRType = ifT[i]; i++;}
178 else ifRType = ifN[i];
179 sprintf(buff, "Route %s: ", ifRType);
180 eDest->Say(pfx, buff, (nameOK ? hName : ifName[i]->iVal),
181 " Dest=", ifDest[i]->iVal, portSfx.val);
182 }
183 }
184}
185
186/******************************************************************************/
187/* Private: G e n A d d r s */
188/******************************************************************************/
189
190bool XrdNetIF::GenAddrs(ifAddrs &ifTab, XrdNetAddrInfo *src)
191{
192 static const int noPort = XrdNetAddr::noPort;
193 static const int old6M4 = XrdNetAddr::noPort | XrdNetAddr::old6Map4;
194 int n;
195
196// If this is an IPV4 address, then format as it
197//
198 ifTab.ipV6 = false;
200 {if (!(ifTab.hALen = src->Format(ifTab.hAddr, sizeof(ifTab.hAddr),
201 XrdNetAddr::fmtAddr, noPort))
202 || !(ifTab.hDLen = src->Format(ifTab.hDest, sizeof(ifTab.hDest),
203 XrdNetAddr::fmtAdv6, old6M4))) return false;
204 return true;
205 }
206
207// If this is a mapped address then we can easily generate the IPV4 version
208// and the locate destination address is the deprecated IPV6 address.
209//
210 if (src->isMapped())
211 {char *Colon;
212 if (!src->Format(ifTab.hAddr, sizeof(ifTab.hAddr),
213 XrdNetAddr::fmtAdv6, noPort)) return false;
214 if (!(Colon = rindex(ifTab.hAddr, ':'))) return false;
215 n = strlen(Colon+1);
216 memmove(ifTab.hAddr,Colon+1,n); ifTab.hAddr[n-1] = 0; ifTab.hALen = n-1;
217 if (!(ifTab.hDLen = src->Format(ifTab.hDest, sizeof(ifTab.hDest),
218 XrdNetAddr::fmtAdv6, old6M4))) return false;
219 return true;
220 }
221
222// There is no IPV4 address so use pure IPV6.
223//
224 ifTab.ipV6 = true;
225 if (!(ifTab.hALen = src->Format(ifTab.hAddr, sizeof(ifTab.hAddr),
226 XrdNetAddr::fmtAdv6, noPort))
227 || !(ifTab.hDLen = src->Format(ifTab.hDest, sizeof(ifTab.hDest),
228 XrdNetAddr::fmtAdv6, noPort))) return false;
229 return true;
230}
231
232/******************************************************************************/
233
234bool XrdNetIF::GenAddrs(ifAddrs &ifTab, const char *hName, bool wantV6)
235{
236 XrdNetAddr *iP;
239 int i, iN, iPVT = -1;
240 bool aOK = false;
241
242// Find alternate addresses in the desired protocol family for this host.
243//
244 if (!XrdNetUtils::GetAddrs(hName, &iP, iN, aOpts, 0) && iN)
245 {for (i = 0; i < iN; i++)
246 {if (iP[i].isPrivate()) iPVT = i;
247 else break;
248 }
249 if (i < iN) ifTab.prvt = false;
250 else if (iPVT >= 0) {i = iPVT; ifTab.prvt = true;}
251 if (i > iN) aOK = GenAddrs(ifTab, &iP[i]);
252 delete [] iP;
253 }
254
255// All done
256//
257 return aOK;
258}
259
260/******************************************************************************/
261/* Private: G e n I F */
262/******************************************************************************/
263
264#define ADDSLOT(xdst, xstr, xlen) {strcpy(ifBP->iVal,xstr);ifBP->iLen=xlen; \
265 xdst=ifBP; bP += (6 + xlen + (xlen & 0x01));ifBP = (ifData *)bP;}
266
267#define RLOSLOT(xdst) xdst = (ifData *)(ifBuff+((char *)xdst-buff))
268
269bool XrdNetIF::GenIF(XrdNetAddrInfo **src, int srcnum, const char *xName)
270{
271 ifAddrs ifTab;
272 const char *hName;
273 char buff[4096], *bP = buff;
274 ifData *ifBP = (ifData *)buff;
275 ifType ifT;
276 int i, n;
277 bool isPrivate;
278
279// Initialize all of the vectors and free the buffer if we allocated it
280//
281 for (i = 0; i < (int)ifMax; i++)
282 {ifName[i] = ifDest[i] = &ifNull;
283 ifxDNS[i] = false;
284 }
285 if (ifBuff) {free(ifBuff); ifBuff = 0;}
286
287for (i = 0; i < srcnum; i++)
288{
289
290// Generate interface addresses. Failure here is almost impossible.
291//
292 if (!src[i]) continue;
293 isPrivate = src[i]->isPrivate();
294 if (!GenAddrs(ifTab, src[i]))
295 {if (eDest) eDest->Emsg("SetIF", "Unable to validate net interfaces!");
296 return false;
297 }
298
299// Determine interface type
300//
301 if (isPrivate) ifT = (ifTab.ipV6 ? PrivateV6 : PrivateV4);
302 else ifT = (ifTab.ipV6 ? PublicV6 : PublicV4);
303
304// We can now check if we have a duplicate interface here
305//
306 if (ifDest[ifT] != &ifNull && eDest)
307 {char eBuff[64];
308 sprintf(eBuff, "Skipping duplicate %s interface",
309 (isPrivate ? "private" : "public"));
310 eDest->Emsg("SetIF", eBuff, ifTab.hDest);
311 continue;
312 }
313
314// Set the locate destination, always an address
315//
316 ADDSLOT(ifDest[ifT], ifTab.hDest, ifTab.hDLen);
317
318// If this is a private interface, then set private pointers to actual addresses
319// since, technically, private addresses should not be registered. Otherwise,
320// fill in the public interface information. We also set unregistered public
321// addresses (what a pain). Of course, that is a configurable detail.
322//
323 if (!rPIPA && isPrivate)
324 {ADDSLOT(ifName[ifT], ifTab.hAddr, ifTab.hALen);
325 } else {
326 hName = src[i]->Name();
327 if (!hName || !(src[i]->isRegistered())) hName = xName;
328 if (hName)
329 {ADDSLOT(ifName[ifT], hName, strlen(hName));
330 ifxDNS[ifT] = true;
331 } else ifName[ifT] = ifDest[ifT];
332 }
333}
334
335// At this point we have set all of the advertised interfaces. If this is a
336// registered host then we know we have the name and nest information but not
337// necessarily the locate destination for each protocol. So, we will try to
338// find them via DNS. If the host does not have an IPv6 address then we will
339// use the mapped IPv4 address and hope that the client is dual stacked.
340//
341 if (ifDest[PublicV4] == &ifNull && ifxDNS[PublicV6]
342 && GenAddrs(ifTab, ifName[PublicV6]->iVal, false))
343 {if (!ifTab.prvt)
344 {ADDSLOT(ifDest[PublicV4], ifTab.hDest, ifTab.hDLen);
345 ifName[PublicV4] = ifName[PublicV6];
346 ifxDNS[PublicV4] = ifxDNS[PublicV6];
347 } else if (ifDest[PrivateV4] == &ifNull)
348 {ADDSLOT(ifDest[PrivateV4], ifTab.hDest, ifTab.hDLen);
349 ifName[PrivateV4] = ifName[PublicV6];
350 ifxDNS[PrivateV4] = ifxDNS[PublicV6];
351 }
352 }
353
354 if (ifDest[PublicV6] == &ifNull && ifxDNS[PublicV4]
355 && GenAddrs(ifTab, ifName[PublicV4]->iVal, true))
356 {if (!ifTab.prvt)
357 {ADDSLOT(ifDest[PublicV6], ifTab.hDest, ifTab.hDLen);
358 ifName[PublicV6] = ifName[PublicV4];
359 ifxDNS[PublicV6] = ifxDNS[PublicV4];
360 } else if (ifDest[PrivateV6] == &ifNull)
361 {ADDSLOT(ifDest[PrivateV6], ifTab.hDest, ifTab.hDLen);
362 ifName[PrivateV6] = ifName[PublicV4];
363 ifxDNS[PrivateV6] = ifxDNS[PublicV4];
364 }
365 }
366
367// Allocate/Replace string storage area
368//
369 n = (char *)ifBP - buff;
370 if (!(ifBuff = (char *)malloc(n))) return false;
371 memcpy(ifBuff, buff, n);
372
373// Now relocate all the pointers in the name and dest vectors
374//
375 for (n = 0; n < (int)ifNum; n++)
376 {if (ifName[n] != &ifNull) RLOSLOT(ifName[n]);
377 if (ifDest[n] != &ifNull) RLOSLOT(ifDest[n]);
378 }
379
380// All done
381//
382 return true;
383}
384
385/******************************************************************************/
386/* G e t D e s t */
387/******************************************************************************/
388
389int XrdNetIF::GetDest(char *dest, int dlen, ifType ifT, bool prefn)
390{
391 ifType ifX = (ifT >= ifAny ? static_cast<ifType>(ifAvail) : ifT);
392 ifData *ifP = (prefn && ifxDNS[ifX] ? ifName[ifX] : ifDest[ifX]);
393 int n;
394
395// Compute length and make sure we don't overflow
396//
397 n = ifP->iLen + portSfx.len;
398 if (!(ifP->iLen) || n >= dlen) return 0;
399
400// Return result with port appended
401//
402 strcpy(dest, ifP->iVal);
403 strcpy(dest +ifP->iLen, portSfx.val);
404 return n;
405}
406
407/******************************************************************************/
408/* G e t I F */
409/******************************************************************************/
410
411#define prtaddr(x) std::cerr <<"Addr!!! " << *x <<std::endl;
412
413int XrdNetIF::GetIF(XrdOucTList **ifList, const char **eText)
414{
415 static const int prvIF[] = {havePrv4, havePrv6};
416 static const int pubIF[] = {havePub4, havePub6};
417
418 char ipBuff[256];
419 short ifIdx = 0, sval[4] = {0, 0, 0, 0};
420 short iLen;
421 int ifT, haveIF = 0;
422
423#ifdef HAVE_GETIFADDRS
424
425// Obtain the list of interfaces
426//
427 XrdNetAddr netAddr;
428 struct ifaddrs *ifBase, *ifP;
429 XrdOucTList *tLP, *tList = 0, *tLast = 0;
430 int n = 0;
431 bool anyIF = (ifCfg[0] == 0 && ifCfg[1] == 0);
432
433 if (getifaddrs(&ifBase) < 0)
434 {if (eText) *eText = XrdSysE2T(errno);
435 if (ifList) *ifList = 0;
436 if (eDest) eDest->Emsg("GetIF", errno, "get interface addresses.");
437 return 0;
438 }
439
440// Report only those interfaces that are up and are not loop-back devices and
441// have been specified by actual name
442//
443 ifP = ifBase;
444 while(ifP)
445 {if ((ifP->ifa_addr != 0)
446 && (!ifList || anyIF || IsOkName(ifP->ifa_name, ifIdx))
447 && (ifP->ifa_flags & (IFF_UP))
448 && (ifP->ifa_flags & (IFF_RUNNING))
449 && !(ifP->ifa_flags & (IFF_LOOPBACK))
450 && ((ifP->ifa_addr->sa_family == AF_INET &&
451 !V4LinkLocal(ifP->ifa_addr))
452 ||
453 (ifP->ifa_addr->sa_family == AF_INET6 &&
454 !(IN6_IS_ADDR_LINKLOCAL(&((sockaddr_in6 *)(ifP->ifa_addr))->sin6_addr)))
455 )
456 )
457 {if (ifP->ifa_addr->sa_family == AF_INET){haveIF |= haveIPv4;ifT=0;}
458 else {haveIF |= haveIPv6; ifT = 1;}
459 if (ifList)
460 {netAddr.Set(ifP->ifa_addr);
461 if ((iLen = netAddr.Format(ipBuff, sizeof(ipBuff),
463 {sval[2] = (anyIF ? 3 : ifIdx);
464 sval[1] = (netAddr.isPrivate() ? 1 : 0);
465 sval[0] = iLen;
466 haveIF |= (sval[1] ? prvIF[ifT] : pubIF[ifT]);
467 tLP = new XrdOucTList(ipBuff, sval);
468 if (tList) tLast->next = tLP;
469 else tList = tLP;
470 tLast = tLP;
471 n++;
472 }
473 } else {
474 netAddr.Set(ifP->ifa_addr);
475 haveIF |= (netAddr.isPrivate() ? prvIF[ifT] : pubIF[ifT]);
476 }
477 }
478 ifP = ifP->ifa_next;
479 }
480
481// All done
482//
483 if (ifBase) freeifaddrs(ifBase);
484 if (eText) *eText = 0;
485 if (!ifList) return haveIF;
486 *ifList = tList;
487 return n;
488
489#else
490
491// If we just need to provide the interface type, indicate we cannot
492//
493 if (!ifList) return haveNoGI;
494
495// For platforms that don't support getifaddrs() use our address
496//
497 XrdNetAddr netAddr((int)0);
498
499// Simply return our formatted address as the interface address
500//
501 if ((iLen = netAddr.Format(ipBuff, sizeof(ipBuff),
503 {if (eText) *eText = 0;
504 sval[0] = iLen;
505 *ifList = new XrdOucTList(ipBuff, sval);
506 return 1;
507 }
508
509// Something bad happened and it shouldn't have
510//
511 if (eText) *eText = "unknown error";
512 if (eDest) eDest->Emsg("GetIF", "Unable to get interface address; "
513 "check if IPV6 enabled!");
514 return 0;
515#endif
516}
517
518/******************************************************************************/
519
520int XrdNetIF::GetIF(char *buff, int blen, const char **eText, bool show)
521{
522 XrdOucTList *ifP, *ifN;
523 const char *ifName[3] = {ifCfg[0], ifCfg[1], "anon"};
524 char *bP = buff;
525 int n, bLeft = blen-8;
526 bool ifOK[3] = {false, false, false};
527
528#ifndef HAVE_GETIFADDRS
529// Display warning on how we are getting the interface addresses
530//
531 if (eDest && show)
532 eDest->Say("Config Warning: using DNS registered address as interface!");
533#endif
534
535// Create the interface list here
536//
537 *buff = 0;
538 if (GetIF(&ifN, eText))
539 {while((ifP = ifN))
540 {n = ifP->sval[0];
541 if (bLeft > n+2)
542 {if (bP != buff) {*bP++ = ' '; bLeft--;}
543 strcpy(bP, ifP->text);
544 bP += n; bLeft -= (n+1);
545 }
546 ifOK[ifP->sval[2]] = true;
547 if (show && eDest)
548 {const char *kind = (ifP->sval[1] ? " private" : " public ");
549 eDest->Say("Config ", ifName[ifP->sval[2]], kind,
550 " network interface: ", ifP->text);
551 }
552 ifN = ifP->next; delete ifP;
553 }
554 }
555
556// Warn about missing interfaces
557//
558 if (show && eDest)
559 {for (n = 0; n < 2; n++)
560 {if (!ifOK[n] && ifCfg[n])
561 eDest->Say("Config ", ifCfg[n],
562 " interface not found or is not usable.");
563 }
564 }
565
566// Return result
567//
568 return bP-buff;
569}
570
571/******************************************************************************/
572
573int XrdNetIF::GetIF(char *&ifline, const char **eText, bool show)
574{
575 char buff[4096];
576 int n;
577
578 if ((n = GetIF(buff, sizeof(buff), eText, show))) ifline = strdup(buff);
579 else ifline = 0;
580
581// Warn about no interfaces
582//
583 if (!ifline && show && eDest)
584 eDest->Say("Config ", "No usable interfaces; using DNS registered "
585 "address as the interface.");
586 return n;
587}
588
589/******************************************************************************/
590/* Private: G e t D o m a i n */
591/******************************************************************************/
592
593const char *XrdNetIF::GetDomain()
594{
595 const char *dnP = XrdNetIdentity::Domain();
596
597// Get our fully resilved name (this doesn't always work)
598//
599 return (dnP ? dnP+1 : 0);
600}
601
602/******************************************************************************/
603/* I n D o m a i n */
604/******************************************************************************/
605
607{
608 const char *hnP;
609
610// Do not attempt to resolve private addresses as they are always in the domain.
611//
612 if (epaddr->isPrivate()) return true;
613
614// Checkout the domain
615//
616 if (!myDomain || !(hnP = epaddr->Name(0)) || !(hnP = index(hnP, '.')))
617 return false;
618
619// Match the domain and returnthe result
620//
621 return strcmp(myDomain, hnP+1) == 0;
622}
623
624/******************************************************************************/
625/* I s O k N a m e */
626/******************************************************************************/
627
628bool XrdNetIF::IsOkName(const char *ifn, short &ifIdx)
629{
630 if (!ifn) return false;
631 if (ifCfg[0] && !strcmp(ifn, ifCfg[0])) ifIdx = 0;
632 else if (ifCfg[1] && !strcmp(ifn, ifCfg[1])) ifIdx = 1;
633 else return false;
634 return true;
635}
636
637/******************************************************************************/
638/* P o r t */
639/******************************************************************************/
640
641int XrdNetIF::Port(int pnum)
642{
643 int prevport = ifPort;
644
645// Check if anything is really changing
646//
647 pnum &= 0x0000ffff;
648 if (pnum == prevport) return prevport;
649
650// Format the port number (can't be more than 5 characters)
651//
652 portSfx.len = sprintf(portSfx.val, ":%d", pnum);
653 ifPort = pnum;
654
655// All done
656//
657 return prevport;
658}
659
660/******************************************************************************/
661/* P o r t D e f a u l t */
662/******************************************************************************/
663
664void XrdNetIF::PortDefault(int pnum) {dfPort = pnum;}
665
666/******************************************************************************/
667/* R o u t i n g */
668/******************************************************************************/
669
671{
672
673// Set the routing type
674//
675 netRoutes = (nettype == netDefault ? netLocal : nettype);
676
677// Based on the routing we need to set the appropriate selection mask vector
678//
679 if (netRoutes == netLocal) ifMaskVec = ifMaskLocal;
680 else if (netRoutes == netSplit) ifMaskVec = ifMaskSplit;
681 else ifMaskVec = ifMaskComm;
682}
683
684/******************************************************************************/
685/* S e t I F */
686/******************************************************************************/
687
688bool XrdNetIF::SetIF(XrdNetAddrInfo *src, const char *ifList, int port,
689 netType nettype, const char *xName)
690{
691 XrdNetAddrInfo *netIF[4] = {0,0,0,0}; //pub 0:v4, prv 1:v4 pub 2:v6 prv 3:v6
692 XrdNetAddr netAdr[4];
693 const char *ifErr = 0, *ifBeg = ifList, *ifEnd, *ifAdr, *ifBad = 0;
694 int i, n, ifCnt = 1;
695 char abuff[64];
696
697// Setup the port number (this sets ifPort)
698//
699 if (port >= 0) Port((port ? port : dfPort));
700
701// Set routing mode for this interface
702//
703 ifRoute = static_cast<short>(nettype == netDefault ? netRoutes : nettype);
704
705// If no list is supplied then fill out based on the source address
706//
707 if (!ifList || !(*ifList))
708 {XrdNetAddrInfo *ifVec[8];
709 XrdNetAddr *iP;
710 const char *hName = src->Name();
711 if (!(src->isRegistered()) && xName) hName = xName;
712 ifCnt = 0;
713 if (!hName
714 || XrdNetUtils::GetAddrs(hName,&iP,ifCnt,XrdNetUtils::allIPv64,ifPort)
715 || !ifCnt) return SetIF64(GenIF(&src, 1));
716 if (ifCnt > 8) ifCnt = 8;
717 for (i = 0; i < ifCnt; i++) ifVec[i] = &iP[i];
718 bool aOK = GenIF(ifVec, ifCnt, hName);
719 delete [] iP;
720 return SetIF64(aOK);
721 }
722
723// Prefrentially use the connect address as the primary interface. This
724// avoids using reported interfaces that may have strange routing.
725//
726 i = (src->isIPType(XrdNetAddrInfo::IPv4) || src->isMapped() ? 0 : 2);
727 if (src->isPrivate()) i |= 1;
728 netIF[i] = src;
729
730// Process the iflist (up to four interfaces)
731//
732 if (ifList && *ifList)
733 do {while (*ifBeg && *ifBeg == ' ') ifBeg++;
734 if ( !(*ifBeg)) break;
735 if (!(ifEnd = index(ifBeg, ' '))) {ifAdr = ifBeg; ifBeg = "";}
736 else {n = ifEnd - ifBeg;
737 if (n >= (int)sizeof(abuff))
738 {ifAdr = 0; ifBad = ifBeg; ifErr = "invalid";}
739 else {strncpy(abuff, ifBeg, n); abuff[n] = 0; ifAdr = abuff;}
740 ifBeg = ifEnd+1;
741 }
742 if (!ifAdr || (ifErr = netAdr[ifCnt].Set(ifAdr, ifPort)))
743 {if (eDest)
744 {if (!ifAdr) ifAdr = ifBad;
745 eDest->Emsg("SetIF", "Unable to use interface", ifAdr, ifErr);
746 }
747 continue;
748 }
749 i = (netAdr[ifCnt].isIPType(XrdNetAddrInfo::IPv4) ||
750 netAdr[ifCnt].isMapped() ? 0 : 2);
751 if (netAdr[ifCnt].isPrivate()) i |= 1;
752 if (!netIF[i]) netIF[i] = &netAdr[ifCnt--];
753 } while(ifCnt >= 0);
754
755// Set the interface data
756//
757 return SetIF64(GenIF(netIF, 4));
758}
759
760/******************************************************************************/
761/* S e t I F N a m e s */
762/******************************************************************************/
763
764bool XrdNetIF::SetIFNames(char *ifnames)
765{
766 char *comma;
767
768// Parse the interface names
769//
770 if ((comma = index(ifnames, ',')))
771 {if (comma == ifnames || !(*(comma+1)))
772 {if (eDest) eDest->Say("Config","Invalid interface name - ",ifnames);
773 return false;
774 }
775 }
776
777// Free old names, if any
778//
779 if (ifCfg[0]) free(ifCfg[0]);
780 if (ifCfg[1]) free(ifCfg[1]);
781
782// Copy the new names
783//
784 if (comma)
785 {*comma = 0;
786 ifCfg[1] = (strcmp(ifnames, comma+1) ? strdup(comma+1) : 0);
787 *comma = ',';
788 } else ifCfg[1] = 0;
789 ifCfg[0] = strdup(ifnames);
790 return true;
791}
792
793/******************************************************************************/
794/* Private: S e t I F P P */
795/******************************************************************************/
796
797void XrdNetIF::SetIFPP()
798{
799 int i, j;
800
801// For split network we use what we have
802//
803 if (netSplit == (netType)ifRoute) return;
804
805// Now set all undefined private interfaces for common and local network routing
806//
807 i = (int)PrivateV4; j = PublicV4;
808 do {if (ifName[i] == &ifNull) {ifName[i]=ifName[j]; ifxDNS[i]=ifxDNS[j];}
809 if (ifDest[i] == &ifNull) ifDest[i]=ifDest[j];
810 if (i == (int)PrivateV6) break;
811 i = (int)PrivateV6; j = (int)PublicV6;
812 } while(true);
813
814// If this is a common network then we are done
815//
816 if (netCommon == (netType)ifRoute) return;
817
818// Now set all undefined public interfaces for local network routing
819//
820 i = (int)PublicV4; j = PrivateV4;
821 do {if (ifName[i] == &ifNull) {ifName[i]=ifName[j]; ifxDNS[i]=ifxDNS[j];}
822 if (ifDest[i] == &ifNull) ifDest[i]=ifDest[j];
823 if (i == (int)PublicV6) break;
824 i = (int)PublicV6; j = (int)PrivateV6;
825 } while(true);
826}
827
828/******************************************************************************/
829/* Private: S e t I F 6 4 */
830/******************************************************************************/
831
832bool XrdNetIF::SetIF64(bool retVal)
833{
834 static const int ifN46= 4;
835 static ifType ifSet[ifN46] = {Public46, Private46, Public64, Private64};
836 static ifType ifChk[ifN46] = {PublicV4, PrivateV4, PublicV6, PrivateV6};
837 static ifType eqSel[ifN46] = {PublicV6, PrivateV6, PublicV4, PrivateV4};
838 static ifType neSel[ifN46] = {PublicV4, PrivateV4, PublicV6, PrivateV6};
839
840// Readjust routing tables if this is not a split network
841//
842 if (netSplit != (netType)ifRoute) SetIFPP();
843
844// Fill out the 4/6 6/4 tables and compute the selection mask
845//
846 ifMask = 0;
847 for (int i = 0; i < ifN46; i++)
848 {ifName[ifSet[i]] = (ifName[ifChk[i]] == &ifNull ? ifName[eqSel[i]]
849 : ifName[neSel[i]]);
850 ifDest[ifSet[i]] = (ifDest[ifChk[i]] == &ifNull ? ifDest[eqSel[i]]
851 : ifDest[neSel[i]]);
852 ifxDNS[ifSet[i]] = ifName[ifSet[i]] != &ifNull &&
853 isalpha(*(ifName[ifSet[i]]->iVal));
854 if (ifDest[ifChk[i]] != &ifNull) ifMask |= sMask[i];
855 }
856
857// Record the one that is actually present
858//
859 if (ifName[Public64] != &ifNull) ifAvail = static_cast<char>(Public64);
860 else ifAvail = static_cast<char>(Private64);
861
862// Return wha the caller wants us to return
863//
864 return retVal;
865}
866
867/******************************************************************************/
868/* S e t M s g s */
869/******************************************************************************/
870
872
873/******************************************************************************/
874/* S e t R P I P A */
875/******************************************************************************/
876
877void XrdNetIF::SetRPIPA(bool rval) {rPIPA = rval;}
878
879/******************************************************************************/
880/* Private: V 4 L i n k L o c a l */
881/******************************************************************************/
882
883bool XrdNetIF::V4LinkLocal(struct sockaddr *saP)
884{
885 unsigned char *ipV4;
886
887 ipV4 = (unsigned char *)&((sockaddr_in *)(saP))->sin_addr.s_addr;
888 return ipV4[0] == 169 && ipV4[1] == 254;
889}
static XrdSysError eDest(0,"crypto_")
#define RLOSLOT(xdst)
Definition XrdNetIF.cc:267
#define ADDSLOT(xdst, xstr, xlen)
Definition XrdNetIF.cc:264
const char * XrdSysE2T(int errcode)
Definition XrdSysE2T.cc:104
static const int noPort
Do not add port number.
static const int old6Map4
Use deprecated IPV6 mapped format.
bool isMapped() const
bool isIPType(IPType ipType) const
int Format(char *bAddr, int bLen, fmtUse fmtType=fmtAuto, int fmtOpts=0)
@ fmtAddr
Address using suitable ipv4 or ipv6 format.
const char * Name(const char *eName=0, const char **eText=0)
const char * Set(const char *hSpec, int pNum=PortInSpec)
static const int haveNoGI
ifList == 0 && getifaddrs() is not supported
Definition XrdNetIF.hh:148
bool SetIF(XrdNetAddrInfo *src, const char *ifList, int port=0, netType nettype=netDefault, const char *xName=0)
Definition XrdNetIF.cc:688
static const int haveIPv4
ifList == 0 && non-local ipv4 i/f found (or'd)
Definition XrdNetIF.hh:150
static void SetRPIPA(bool rval)
Definition XrdNetIF.cc:877
static bool InDomain(XrdNetAddrInfo *epaddr)
Definition XrdNetIF.cc:606
void Display(const char *pfx="=====> ")
Definition XrdNetIF.cc:142
static int GetIF(XrdOucTList **ifList, const char **eText=0)
Definition XrdNetIF.cc:413
static void Routing(netType nettype)
Definition XrdNetIF.cc:670
int Port()
Definition XrdNetIF.hh:276
static void SetMsgs(XrdSysError *erp)
Definition XrdNetIF.cc:871
static void PortDefault(int pnum=1094)
Definition XrdNetIF.cc:664
static const int havePub6
ifList == 0 && public ipv6 i/f found (or'd)
Definition XrdNetIF.hh:160
static bool SetIFNames(char *ifnames)
Definition XrdNetIF.cc:764
static const int havePub4
ifList == 0 && public ipv4 i/f found (or'd)
Definition XrdNetIF.hh:158
int GetDest(char *dest, int dlen, ifType ifT=PublicV6, bool prefn=false)
Definition XrdNetIF.cc:389
static const int haveIPv6
ifList == 0 && non-local ipv6 i/f found (or'd)
Definition XrdNetIF.hh:152
ifType
The enum that is used to index into ifData to get appropriate interface.
Definition XrdNetIF.hh:64
@ PrivateV4
Definition XrdNetIF.hh:65
@ Private64
Definition XrdNetIF.hh:73
@ Private46
Definition XrdNetIF.hh:71
@ PrivateV6
Definition XrdNetIF.hh:67
static const int havePrv6
ifList == 0 && private ipv6 i/f found (or'd)
Definition XrdNetIF.hh:156
static const int havePrv4
ifList == 0 && private ipv4 i/f found (or'd)
Definition XrdNetIF.hh:154
static const char * Domain(const char **eText=0)
static const char * GetAddrs(const char *hSpec, XrdNetAddr *aListP[], int &aListN, AddrOpts opts=allIPMap, int pNum=PortInSpec)
XrdOucTList * next
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
void Say(const char *text1, const char *text2=0, const char *txt3=0, const char *text4=0, const char *text5=0, const char *txt6=0)