00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 const char *XrdNetWorkCVSID = "$Id: XrdNetWork.cc 22437 2008-03-04 14:35:16Z rdm $";
00014
00015 #include <errno.h>
00016 #include <fcntl.h>
00017 #ifndef WIN32
00018 #include <poll.h>
00019 #include <stdio.h>
00020 #include <string.h>
00021 #include <unistd.h>
00022 #include <sys/types.h>
00023 #include <sys/socket.h>
00024 #else
00025 #include <stdio.h>
00026 #include <string.h>
00027 #include <Winsock2.h>
00028 #include <io.h>
00029 #endif
00030
00031 #include "XrdNet/XrdNet.hh"
00032 #include "XrdNet/XrdNetLink.hh"
00033 #include "XrdNet/XrdNetOpts.hh"
00034 #include "XrdNet/XrdNetPeer.hh"
00035 #include "XrdNet/XrdNetWork.hh"
00036
00037 #include "XrdSys/XrdSysError.hh"
00038
00039
00040
00041
00042
00043 XrdNetLink *XrdNetWork::Accept(int opts, int timeout)
00044 {
00045 XrdNetPeer myPeer;
00046 XrdNetLink *lp;
00047 int ismyfd, lnkopts;
00048
00049
00050
00051 if (!XrdNet::Accept(myPeer, opts, timeout)) return (XrdNetLink *)0;
00052 if ((ismyfd = (myPeer.fd == iofd))) lnkopts = XRDNETLINK_NOCLOSE;
00053 else lnkopts = 0;
00054
00055
00056
00057 if (!(lp = XrdNetLink::Alloc(eDest,(XrdNet *)this,myPeer,BuffQ,lnkopts)))
00058 {if (!ismyfd) close(myPeer.fd);
00059 if (!(opts & XRDNET_NOEMSG))
00060 eDest->Emsg("Connect",ENOMEM,"accept connection from",myPeer.InetName);
00061 } else myPeer.InetBuff = 0;
00062 return lp;
00063 }
00064
00065
00066
00067
00068
00069 XrdNetLink *XrdNetWork::Connect(const char *host, int port, int opts, int tmo)
00070 {
00071 XrdNetPeer myPeer;
00072 XrdNetLink *lp;
00073
00074
00075
00076 if (!XrdNet::Connect(myPeer, host, port, opts, tmo)) return (XrdNetLink *)0;
00077
00078
00079
00080 if (!(lp = XrdNetLink::Alloc(eDest, (XrdNet *)this, myPeer, BuffQ)))
00081 {close(myPeer.fd);
00082 if (!(opts & XRDNET_NOEMSG))
00083 eDest->Emsg("Connect", ENOMEM, "connect to", host);
00084 }
00085 return lp;
00086 }
00087
00088
00089
00090
00091
00092 XrdNetLink *XrdNetWork::Relay(const char *dest, int opts)
00093 {
00094 XrdNetPeer myPeer;
00095 XrdNetLink *lp;
00096 int lnkopts;
00097
00098
00099
00100 if (!XrdNet::Connect(myPeer, dest, -1, opts | XRDNET_UDPSOCKET))
00101 return (XrdNetLink *)0;
00102
00103
00104
00105 lnkopts = (myPeer.fd == iofd ? XRDNETLINK_NOCLOSE : 0)
00106 | (opts & XRDNET_SENDONLY ? XRDNETLINK_NOSTREAM : 0);
00107
00108
00109
00110 if (!(lp = XrdNetLink::Alloc(eDest, (XrdNet *)this, myPeer, BuffQ, lnkopts)))
00111 {close(myPeer.fd);
00112 if (!(opts & XRDNET_NOEMSG))
00113 eDest->Emsg("Connect", ENOMEM, "allocate relay to",
00114 (dest ? dest : "network"));
00115 }
00116 return lp;
00117 }