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
 
XrdSysError * eDest
 
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 66 of file XrdNetMsg.cc.

67  : eDest(erp)
68 {
69  XrdNet myNet(erp);
70  bool aok = true;
71 
72 // Handle the case where no dest was specified. In this case we will always
73 // need the caller to specify a destination.
74 //
75  if (!dest)
76  {if ((FD = myNet.Relay(dest)) < 0)
77  {eDest->Emsg("NetMsg", "Unable to create UDP msg socket.");
78  aok = false;
79  } else FitDatagram(FD);
80  if (aOK) *aOK = aok;
81  return;
82  }
83 
84 // Hande the common case where a dest is specified. We first make
85 // sure the dest is valid, eventhough that will occur again, so we van
86 // generate a resonable error message.
87 //
88  XrdNetAddr specDest;
89  const char *eText = specDest.Set(dest);
90  if (eText)
91  {eDest->Emsg("NetMsg", "Default", dest, "is unreachable");
92  if (aOK) *aOK = false;
93  return;
94  }
95 
96 // Obtain a file description for this socket and set the endpoint address
97 //
98  XrdNetPeer myPeer;
99 
100  if (!myNet.Relay(myPeer, dest, XRDNET_SENDONLY))
101  {eDest->Emsg("NetMsg", "Unable to create UDP msg socket.");
102  if (aOK) *aOK = false;
103  return;
104  }
105 
106 // Save the relevant information
107 //
108  dfltDest = strdup(myPeer.InetName ? myPeer.InetName : "Unknown!");
109  FD = myPeer.fd;
110  destOK = true;
111  FitDatagram(FD);
112 
113 // If address refresh wanted, register this socket for refresh. This should
114 // never fail and if it does we return non-success.
115 //
116  if (refr && !XrdNetRefresh::Register(myPeer)) aok = false;
117 
118 // All done
119 //
120  if (aOK) *aOK = aok;
121 }
#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:116

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 127 of file XrdNetMsg.cc.

128 {
129 // If we are registered,deregister
130 //
132 
133 // Close the socket
134 //
135  if (close(FD) < 0)
136  eDest->Emsg("NetMsg", errno, "close socket for", dfltDest);
137 
138 // Free the poiinter to the default dest
139 //
140  free(dfltDest);
141 }
#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 261 of file XrdNetMsg.cc.

262 {
263  struct pollfd polltab = {FD, POLLOUT|POLLWRNORM, 0};
264  int retc;
265 
266  do {retc = poll(&polltab, 1, timeout);} while(retc < 0 && errno == EINTR);
267 
268  if (retc == 0 || !(polltab.revents & (POLLOUT | POLLWRNORM)))
269  eDest->Emsg("NetMsg", "UDP link to", dest, "is blocked.");
270  else if (retc < 0)
271  eDest->Emsg("NetMsg",errno,"poll", dest);
272  else return 1;
273  return 0;
274 }

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 280 of file XrdNetMsg.cc.

281 {
282  if (!theDest)
283  {if (!destOK)
284  {eDest->Emsg("NetMsg", "Destination not specified."); return -1;}
285  theDest = dfltDest;
286  }
287  eDest->Emsg("NetMsg", ecode, "send to", theDest);
288  return (EWOULDBLOCK == ecode || EAGAIN == ecode ? 1 : -1);
289 }

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 291 of file XrdNetMsg.cc.

292 {
293  return retErr(ecode, theDest.Name("unknown"));
294 }
const char * Name(const char *eName=0, const char **eText=0)
int retErr(int ecode, const char *theDest)
Definition: XrdNetMsg.cc:280

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 147 of file XrdNetMsg.cc.

148 {
149  int retc;
150 
151 // Get the buffer length of not specified
152 //
153  if (!Blen && !(Blen = strlen(Buff))) return 0;
154 
155 // Handle the case where we are sendingto he dest setup at construction. This
156 // is the most common case.
157 //
158  if (!dest)
159  {if (!destOK)
160  {eDest->Emsg("NetMsg", "Destination not specified."); return -1;}
161 
162  if (tmo >= 0 && !OK2Send(tmo, dfltDest)) return 1;
163 
164  do {retc = send(FD, (Sokdata_t)Buff, Blen, 0);
165  } while (retc < 0 && errno == EINTR);
166 
167  return (retc < 0 ? retErr(errno, dfltDest) : 0);
168  }
169 
170 // Caller want to send to a specific destination other than the default
171 //
172  XrdNetAddr specDest;
173 
174  if (specDest.Set(dest))
175  {eDest->Emsg("NetMsg", dest, "is unreachable"); return -1;}
176 
177  if (tmo >= 0 && !OK2Send(tmo, dest)) return 1;
178 
179  do {retc = sendto(FD, (Sokdata_t)Buff, Blen, 0,
180  specDest.SockAddr(), specDest.SockSize());}
181  while (retc < 0 && errno == EINTR);
182 
183  return (retc < 0 ? retErr(errno, specDest) : 0);
184 }
#define Sokdata_t
const sockaddr * SockAddr()
SOCKLEN_t SockSize()
int OK2Send(int timeout, const char *dest)
Definition: XrdNetMsg.cc:261

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 188 of file XrdNetMsg.cc.

190 {
191  int aSize, retc;
192 
193  if (!Blen && !(Blen = strlen(Buff))) return 0;
194 
195  if (netSA.Addr.sa_family == AF_INET) aSize = sizeof(netSA.v4);
196  else if (netSA.Addr.sa_family == AF_INET6) aSize = sizeof(netSA.v6);
197  else return retErr(EAFNOSUPPORT, (dest ? dest : "Unknown!"));
198 
199  if (tmo >= 0 && !OK2Send(tmo, dest)) return 1;
200 
201  do {retc = sendto(FD, (Sokdata_t)Buff, Blen, 0, &netSA.Addr, aSize);}
202  while (retc < 0 && errno == EINTR);
203 
204  if (retc >= 0) return 0;
205  return retErr(errno, (dest ? dest : "Unknown!"));
206 }
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 210 of file XrdNetMsg.cc.

212 {
213 
214 // Handle the common case of sendingto the cobbected address
215 //
216  if (!dest)
217  {if (!destOK)
218  {eDest->Emsg("NetMsg", "Destination not specified."); return -1;}
219  if (tmo >= 0 && !OK2Send(tmo, dfltDest)) return 1;
220  if (writev(FD, iov, iovcnt) >= 0) return 0;
221  return retErr(errno, dfltDest);
222  }
223 
224 // Caller want to send to a specific destination other than the default
225 //
226  XrdNetAddr specDest;
227  int retc;
228 
229  if (specDest.Set(dest))
230  {eDest->Emsg("NetMsg", dest, "is unreachable"); return -1;}
231 
232 // Create the message via the msghdr
233 //
234  struct msghdr mHdr{};
235 
236  mHdr.msg_name = (void*)specDest.SockAddr();
237  mHdr.msg_namelen = specDest.SockSize();
238  mHdr.msg_iov = const_cast<struct iovec*>(iov);
239  mHdr.msg_iovlen = iovcnt;
240 
241 // Handle timeout if need be
242 //
243  if (tmo >= 0 && !OK2Send(tmo, dest)) return 1;
244 
245 // Send the message
246 //
247  do {retc = sendmsg(FD, &mHdr, 0);} while (retc < 0 && errno == EINTR);
248 
249 // All done
250 //
251  return (retc < 0 ? retErr(errno, specDest) : 0);
252 }
#define writev(a, b, c)
Definition: XrdPosix.hh:123

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: