XRootD
XrdNetMsg Class Reference

#include <XrdNetMsg.hh>

+ Collaboration diagram for XrdNetMsg:

Public Member Functions

 XrdNetMsg (XrdSysError *erp, const char *dest=0, bool *aOK=0, bool refr=false)
 
 ~XrdNetMsg ()
 Destructor. More...
 
int Send (const char *buff, int blen=0, const char *dest=0, int tmo=-1)
 
int Send (const char *dest, const XrdNetSockAddr &netSA, const char *buff, int blen=0, int tmo=-1)
 
int Send (const struct iovec iov[], int iovcnt, const char *dest=0, int tmo=-1)
 

Protected Member Functions

int OK2Send (int timeout, const char *dest)
 
int retErr (int ecode, const char *theDest)
 
int retErr (int ecode, XrdNetAddr &theDest)
 

Protected Attributes

bool destOK = false
 
char * dfltDest
 
XrdSysErroreDest
 
int FD = -1
 
bool isRefr = false
 

Detailed Description

Definition at line 49 of file XrdNetMsg.hh.

Constructor & Destructor Documentation

◆ XrdNetMsg()

XrdNetMsg::XrdNetMsg ( XrdSysError erp,
const char *  dest = 0,
bool *  aOK = 0,
bool  refr = false 
)

Constructor

Parameters
erpThe error message object for routing error messages.
destThe endpint name which can be host:port or a named socket. This becomes the default endpoint. Any specified endpoint to send must be in the same family (e.g. UNIX). If not specified, then an endpoint must always be specified with send and is restricted to be in the INET family.
aOKIf supplied, set to true upon success; false otherwise.
refrWhen true, registers to socket for address refresh. This is done by periodically (as specified by the xrd.network directive) retranslating the dest host name to see if its IP address changed and if it did, updating the IP address. This option is ignored if dest is null (i.e. unspecified).

Definition at line 47 of file XrdNetMsg.cc.

48  : eDest(erp)
49 {
50  XrdNet myNet(erp);
51  bool aok = true;
52 
53 // Handle the case where no dest was specified. In this case we will always
54 // need the caller to specify a destination.
55 //
56  if (!dest)
57  {if ((FD = myNet.Relay(dest)) < 0)
58  {eDest->Emsg("NetMsg", "Unable to create UDP msg socket.");
59  aok = false;
60  }
61  if (aOK) *aOK = aok;
62  return;
63  }
64 
65 // Hande the common case where a dest is specified. We first make
66 // sure the dest is valid, eventhough that will occur again, so we van
67 // generate a resonable error message.
68 //
69  XrdNetAddr specDest;
70  const char *eText = specDest.Set(dest);
71  if (eText)
72  {eDest->Emsg("NetMsg", "Default", dest, "is unreachable");
73  if (aOK) *aOK = false;
74  return;
75  }
76 
77 // Obtain a file description for this socket and set the endpoint address
78 //
79  XrdNetPeer myPeer;
80 
81  if (!myNet.Relay(myPeer, dest, XRDNET_SENDONLY))
82  {eDest->Emsg("NetMsg", "Unable to create UDP msg socket.");
83  if (aOK) *aOK = false;
84  return;
85  }
86 
87 // Save the relevant information
88 //
89  dfltDest = strdup(myPeer.InetName ? myPeer.InetName : "Unknown!");
90  FD = myPeer.fd;
91  destOK = true;
92 
93 // If address refresh wanted, register this socket for refresh. This should
94 // never fail and if it does we return non-success.
95 //
96  if (refr && !XrdNetRefresh::Register(myPeer)) aok = false;
97 
98 // All done
99 //
100  if (aOK) *aOK = aok;
101 }
#define XRDNET_SENDONLY
Definition: XrdNetOpts.hh:43
const char * Set(const char *hSpec, int pNum=PortInSpec)
Definition: XrdNetAddr.cc:216
XrdSysError * eDest
Definition: XrdNetMsg.hh:145
bool destOK
Definition: XrdNetMsg.hh:148
char * dfltDest
Definition: XrdNetMsg.hh:146
char * InetName
Definition: XrdNetPeer.hh:44
static bool Register(XrdNetPeer &Peer)
Definition: XrdNet.hh:52
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
Definition: XrdSysError.cc:95

References destOK, dfltDest, eDest, XrdSysError::Emsg(), FD, XrdNetPeer::fd, XrdNetPeer::InetName, XrdNetRefresh::Register(), XrdNet::Relay(), XrdNetAddr::Set(), and XRDNET_SENDONLY.

+ Here is the call graph for this function:

◆ ~XrdNetMsg()

XrdNetMsg::~XrdNetMsg ( )

Destructor.

Definition at line 107 of file XrdNetMsg.cc.

108 {
109 // If we are registered,deregister
110 //
112 
113 // Close the socket
114 //
115  if (close(FD) < 0)
116  eDest->Emsg("NetMsg", errno, "close socket for", dfltDest);
117 
118 // Free the poiinter to the default dest
119 //
120  free(dfltDest);
121 }
#define close(a)
Definition: XrdPosix.hh:48
bool isRefr
Definition: XrdNetMsg.hh:149
static void UnRegister(int fd)

References close, dfltDest, eDest, XrdSysError::Emsg(), FD, isRefr, and XrdNetRefresh::UnRegister().

+ Here is the call graph for this function:

Member Function Documentation

◆ OK2Send()

int XrdNetMsg::OK2Send ( int  timeout,
const char *  dest 
)
protected

Definition at line 241 of file XrdNetMsg.cc.

242 {
243  struct pollfd polltab = {FD, POLLOUT|POLLWRNORM, 0};
244  int retc;
245 
246  do {retc = poll(&polltab, 1, timeout);} while(retc < 0 && errno == EINTR);
247 
248  if (retc == 0 || !(polltab.revents & (POLLOUT | POLLWRNORM)))
249  eDest->Emsg("NetMsg", "UDP link to", dest, "is blocked.");
250  else if (retc < 0)
251  eDest->Emsg("NetMsg",errno,"poll", dest);
252  else return 1;
253  return 0;
254 }

References eDest, XrdSysError::Emsg(), and FD.

Referenced by Send().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ retErr() [1/2]

int XrdNetMsg::retErr ( int  ecode,
const char *  theDest 
)
protected

Definition at line 260 of file XrdNetMsg.cc.

261 {
262  if (!theDest)
263  {if (!destOK)
264  {eDest->Emsg("NetMsg", "Destination not specified."); return -1;}
265  theDest = dfltDest;
266  }
267  eDest->Emsg("NetMsg", ecode, "send to", theDest);
268  return (EWOULDBLOCK == ecode || EAGAIN == ecode ? 1 : -1);
269 }

References destOK, dfltDest, eDest, and XrdSysError::Emsg().

Referenced by retErr(), and Send().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ retErr() [2/2]

int XrdNetMsg::retErr ( int  ecode,
XrdNetAddr theDest 
)
protected

Definition at line 271 of file XrdNetMsg.cc.

272 {
273  return retErr(ecode, theDest.Name("unknown"));
274 }
const char * Name(const char *eName=0, const char **eText=0)
int retErr(int ecode, const char *theDest)
Definition: XrdNetMsg.cc:260

References XrdNetAddrInfo::Name(), and retErr().

+ Here is the call graph for this function:

◆ Send() [1/3]

int XrdNetMsg::Send ( const char *  buff,
int  blen = 0,
const char *  dest = 0,
int  tmo = -1 
)

Send a UDP message to an endpoint.

Parameters
buffThe data to send.
blenLength of the data in buff. If not specified, the length is computed as strlen(buff).
destThe endpint name which can be host:port or a named socket. If dest is zero, uses dest specified in the constructor.
tmomaximum seconds to wait for a idle socket. When negative, the default, no time limit applies.
Returns
<0 Message not sent due to error.
=0 Message send (well as defined by UDP)
>0 Message not sent, timeout occurred.

Definition at line 127 of file XrdNetMsg.cc.

128 {
129  int retc;
130 
131 // Get the buffer length of not specified
132 //
133  if (!Blen && !(Blen = strlen(Buff))) return 0;
134 
135 // Handle the case where we are sendingto he dest setup at construction. This
136 // is the most common case.
137 //
138  if (!dest)
139  {if (!destOK)
140  {eDest->Emsg("NetMsg", "Destination not specified."); return -1;}
141 
142  if (tmo >= 0 && !OK2Send(tmo, dfltDest)) return 1;
143 
144  do {retc = send(FD, (Sokdata_t)Buff, Blen, 0);
145  } while (retc < 0 && errno == EINTR);
146 
147  return (retc < 0 ? retErr(errno, dfltDest) : 0);
148  }
149 
150 // Caller want to send to a specific destination other than the default
151 //
152  XrdNetAddr specDest;
153 
154  if (specDest.Set(dest))
155  {eDest->Emsg("NetMsg", dest, "is unreachable"); return -1;}
156 
157  if (tmo >= 0 && !OK2Send(tmo, dest)) return 1;
158 
159  do {retc = sendto(FD, (Sokdata_t)Buff, Blen, 0,
160  specDest.SockAddr(), specDest.SockSize());}
161  while (retc < 0 && errno == EINTR);
162 
163  return (retc < 0 ? retErr(errno, specDest) : 0);
164 }
#define Sokdata_t
const sockaddr * SockAddr()
SOCKLEN_t SockSize()
int OK2Send(int timeout, const char *dest)
Definition: XrdNetMsg.cc:241

References destOK, dfltDest, eDest, XrdSysError::Emsg(), FD, OK2Send(), retErr(), XrdNetAddr::Set(), XrdNetAddrInfo::SockAddr(), XrdNetAddrInfo::SockSize(), and Sokdata_t.

Referenced by XrdXrootdGSReal::GetDictID(), XrdXrootdGSReal::Ident(), XrdCmsPrepare::Inform(), XrdFrcReqAgent::Ping(), XrdStats::Report(), and XrdXrootdMonitor::Send().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Send() [2/3]

int XrdNetMsg::Send ( const char *  dest,
const XrdNetSockAddr netSA,
const char *  buff,
int  blen = 0,
int  tmo = -1 
)

Send a UDP message to an endpoint.

Parameters
buffThe data to send.
blenLength of the data in buff. If not specified, the length is computed as strlen(buff).
destThe endpoint in the form as in "host:port". This is strictly used for error messages.
netSAThe endpoint address. This overrides the constructor. The family must be AF_INET or AF_INET6.
tmomaximum seconds to wait for a idle socket. When negative, the default, no time limit applies.
Returns
<0 Message not sent due to error.
=0 Message send (well as defined by UDP)
>0 Message not sent, timeout occurred.

Definition at line 168 of file XrdNetMsg.cc.

170 {
171  int aSize, retc;
172 
173  if (!Blen && !(Blen = strlen(Buff))) return 0;
174 
175  if (netSA.Addr.sa_family == AF_INET) aSize = sizeof(netSA.v4);
176  else if (netSA.Addr.sa_family == AF_INET6) aSize = sizeof(netSA.v6);
177  else return retErr(EAFNOSUPPORT, (dest ? dest : "Unknown!"));
178 
179  if (tmo >= 0 && !OK2Send(tmo, dest)) return 1;
180 
181  do {retc = sendto(FD, (Sokdata_t)Buff, Blen, 0, &netSA.Addr, aSize);}
182  while (retc < 0 && errno == EINTR);
183 
184  if (retc >= 0) return 0;
185  return retErr(errno, (dest ? dest : "Unknown!"));
186 }
struct sockaddr_in6 v6
struct sockaddr Addr
struct sockaddr_in v4

References XrdNetSockAddr::Addr, FD, OK2Send(), retErr(), Sokdata_t, XrdNetSockAddr::v4, and XrdNetSockAddr::v6.

+ Here is the call graph for this function:

◆ Send() [3/3]

int XrdNetMsg::Send ( const struct iovec  iov[],
int  iovcnt,
const char *  dest = 0,
int  tmo = -1 
)

Send a UDP message to an endpoint using an I/O vector.

Parameters
iovThe vector of data to send. Total amount be <= 4096 bytes.
iovcntThe number of elements in the vector.
destThe endpint name which can be host:port or a named socket. If dest is zero, uses dest specified in the constructor.
tmomaximum seconds to wait for a idle socket. When negative, the default, no time limit applies.
Returns
<0 Message not sent due to error.
=0 Message send (well as defined by UDP)
>0 Message not sent, timeout occurred.

Definition at line 190 of file XrdNetMsg.cc.

192 {
193 
194 // Handle the common case of sendingto the cobbected address
195 //
196  if (!dest)
197  {if (!destOK)
198  {eDest->Emsg("NetMsg", "Destination not specified."); return -1;}
199  if (tmo >= 0 && !OK2Send(tmo, dfltDest)) return 1;
200  if (writev(FD, iov, iovcnt) >= 0) return 0;
201  return retErr(errno, dfltDest);
202  }
203 
204 // Caller want to send to a specific destination other than the default
205 //
206  XrdNetAddr specDest;
207  int retc;
208 
209  if (specDest.Set(dest))
210  {eDest->Emsg("NetMsg", dest, "is unreachable"); return -1;}
211 
212 // Create the message via the msghdr
213 //
214  struct msghdr mHdr{};
215 
216  mHdr.msg_name = (void*)specDest.SockAddr();
217  mHdr.msg_namelen = specDest.SockSize();
218  mHdr.msg_iov = const_cast<struct iovec*>(iov);
219  mHdr.msg_iovlen = iovcnt;
220 
221 // Handle timeout if need be
222 //
223  if (tmo >= 0 && !OK2Send(tmo, dest)) return 1;
224 
225 // Send the message
226 //
227  do {retc = sendmsg(FD, &mHdr, 0);} while (retc < 0 && errno == EINTR);
228 
229 // All done
230 //
231  return (retc < 0 ? retErr(errno, specDest) : 0);
232 }
ssize_t writev(int fildes, const struct iovec *iov, int iovcnt)

References destOK, dfltDest, eDest, XrdSysError::Emsg(), FD, OK2Send(), retErr(), XrdNetAddr::Set(), XrdNetAddrInfo::SockAddr(), XrdNetAddrInfo::SockSize(), and writev().

+ Here is the call graph for this function:

Member Data Documentation

◆ destOK

bool XrdNetMsg::destOK = false
protected

Definition at line 148 of file XrdNetMsg.hh.

Referenced by XrdNetMsg(), retErr(), and Send().

◆ dfltDest

char* XrdNetMsg::dfltDest
protected

Definition at line 146 of file XrdNetMsg.hh.

Referenced by XrdNetMsg(), ~XrdNetMsg(), retErr(), and Send().

◆ eDest

XrdSysError* XrdNetMsg::eDest
protected

Definition at line 145 of file XrdNetMsg.hh.

Referenced by XrdNetMsg(), ~XrdNetMsg(), OK2Send(), retErr(), and Send().

◆ FD

int XrdNetMsg::FD = -1
protected

Definition at line 147 of file XrdNetMsg.hh.

Referenced by XrdNetMsg(), ~XrdNetMsg(), OK2Send(), and Send().

◆ isRefr

bool XrdNetMsg::isRefr = false
protected

Definition at line 149 of file XrdNetMsg.hh.

Referenced by ~XrdNetMsg().


The documentation for this class was generated from the following files: