00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 const char *XrdInetCVSID = "$Id: XrdInet.cc 25932 2008-10-23 10:58:11Z ganis $";
00014
00015 #include <ctype.h>
00016 #include <errno.h>
00017 #include <netdb.h>
00018 #include <stdio.h>
00019 #include <unistd.h>
00020 #include <sys/types.h>
00021 #include <sys/socket.h>
00022
00023 #include "XrdSys/XrdSysError.hh"
00024
00025 #include "Xrd/XrdInet.hh"
00026 #include "Xrd/XrdLink.hh"
00027 #include "Xrd/XrdTrace.hh"
00028
00029 #include "XrdNet/XrdNetOpts.hh"
00030 #include "XrdNet/XrdNetPeer.hh"
00031
00032
00033
00034
00035
00036 extern XrdOucTrace XrdTrace;
00037
00038 const char *XrdInet::TraceID = "Inet";
00039
00040
00041
00042
00043
00044 XrdLink *XrdInet::Accept(int opts, int timeout)
00045 {
00046 XrdNetPeer myPeer;
00047 XrdLink *lp;
00048 int ismyfd, lnkopts = (opts & XRDNET_MULTREAD ? XRDLINK_RDLOCK : 0);
00049
00050
00051
00052 if (!XrdNet::Accept(myPeer, opts | (netOpts & XRDNET_NORLKUP), timeout))
00053 return (XrdLink *)0;
00054 if ((ismyfd = (myPeer.fd == iofd))) lnkopts |= XRDLINK_NOCLOSE;
00055
00056
00057
00058 if (!(lp = XrdLink::Alloc(myPeer, lnkopts)))
00059 {eDest->Emsg("Accept",ENOMEM,"allocate new link for",myPeer.InetName);
00060 if (!ismyfd) close(myPeer.fd);
00061 } else {
00062 myPeer.InetBuff = 0;
00063 TRACE(NET, "Accepted connection from " <<myPeer.fd <<'@' <<myPeer.InetName);
00064 }
00065
00066
00067
00068 return lp;
00069 }
00070
00071
00072
00073
00074
00075 XrdLink *XrdInet::Connect(const char *host, int port, int opts, int tmo)
00076 {
00077 XrdNetPeer myPeer;
00078 XrdLink *lp;
00079 int ismyfd, lnkopts = (opts & XRDNET_MULTREAD ? XRDLINK_RDLOCK : 0);
00080
00081
00082
00083 if (!XrdNet::Connect(myPeer, host, port, opts, tmo)) return (XrdLink *)0;
00084 if ((ismyfd = (myPeer.fd == iofd))) lnkopts |= XRDLINK_NOCLOSE;
00085
00086
00087
00088 if (!(lp = XrdLink::Alloc(myPeer, lnkopts)))
00089 {eDest->Emsg("Connect",ENOMEM,"allocate new link to",myPeer.InetName);
00090 if (!ismyfd) close(myPeer.fd);
00091 } else {
00092 myPeer.InetBuff = 0;
00093 TRACE(NET, "Connected to " <<myPeer.InetName <<':' <<port);
00094 }
00095
00096
00097
00098 return lp;
00099 }