XRootD
Loading...
Searching...
No Matches
XrdNetConnect.cc
Go to the documentation of this file.
1/******************************************************************************/
2/* */
3/* X r d N e t C o n n e c t . c c */
4/* */
5/* (c) 2004 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 "errno.h"
32#include "fcntl.h"
33#ifndef WIN32
34#include "poll.h"
35#include "unistd.h"
36#include <sys/types.h>
37#include <sys/socket.h>
38#endif
39
42
43/******************************************************************************/
44/* C o n n e c t */
45/******************************************************************************/
46
48 const struct sockaddr *name,
49 int namelen,
50 int tsec)
51{
52 int old_flags, new_flags, myRC;
53 SOCKLEN_t myRClen = sizeof(myRC);
54
55// If no timeout wanted, do a plain connect() which will timeout after 3
56// minutes on most platforms.
57//
58 if (!tsec)
59 {if (connect(fd, name, namelen)) return errno;
60 return 0;
61 }
62
63// If a timeout is wanted, then we must convert this file descriptor to be
64// non-blocking so that if a connection is not made immediately we can get
65// control back and poll for completion for the specified amount of time.
66// Regardless of outcome we will restore the original fd settings.
67//
68 old_flags = fcntl(fd, F_GETFL, 0);
69 new_flags = old_flags | O_NDELAY | O_NONBLOCK;
70 fcntl(fd, F_SETFL, new_flags);
71 if (!connect(fd, name, namelen)) myRC = 0;
72 else if (EINPROGRESS != net_errno) myRC = net_errno;
73 else {struct pollfd polltab = {fd, POLLOUT|POLLWRNORM, 0};
74 do {myRC = poll(&polltab, 1, tsec*1000);}
75 while(myRC < 0 && errno == EINTR);
76 if (myRC != 1) myRC = ETIMEDOUT;
77 else getsockopt(fd,SOL_SOCKET,SO_ERROR,(Sokdata_t)&myRC,&myRClen);
78 }
79 fcntl(fd, F_SETFD, old_flags);
80 return myRC;
81}
#define net_errno
#define SOCKLEN_t
#define Sokdata_t
static int Connect(int fd, const struct sockaddr *name, int namelen, int tsec=-1)