xrootd
Loading...
Searching...
No Matches
XrdNetSocket.hh
Go to the documentation of this file.
1#ifndef __NETSOCKET__
2#define __NETSOCKET__
3/******************************************************************************/
4/* */
5/* X r d N e t S o c k e t . h h */
6/* */
7/* (C) 2004 by the Board of Trustees of the Leland Stanford, Jr., University */
8/* All Rights Reserved */
9/* Produced by Andrew Hanushevsky for Stanford University under contract */
10/* DE-AC02-76-SFO0515 with the Deprtment of Energy */
11/* */
12/* This file is part of the XRootD software suite. */
13/* */
14/* XRootD is free software: you can redistribute it and/or modify it under */
15/* the terms of the GNU Lesser General Public License as published by the */
16/* Free Software Foundation, either version 3 of the License, or (at your */
17/* option) any later version. */
18/* */
19/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
20/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
21/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
22/* License for more details. */
23/* */
24/* You should have received a copy of the GNU Lesser General Public License */
25/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
26/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
27/* */
28/* The copyright holder's institutional names and contributor's names may not */
29/* be used to endorse or promote products derived from this software without */
30/* specific prior written permission of the institution or contributor. */
31/******************************************************************************/
32
33#ifndef WIN32
34#include <sys/socket.h>
35#else
36#include <Winsock2.h>
37#endif
38
39#include "XrdNet/XrdNetAddr.hh"
40
41/******************************************************************************/
42/* C l a s s D e f i n i t i o n */
43/******************************************************************************/
44
45class XrdSysError;
46
48{
49public:
50
51// When creating a socket object, you may pass an optional error routing object.
52// If you do so, error messages will be writen via the error object. Otherwise,
53// errors will be returned quietly. Addionally, you can attach a file descriptor
54// to the socket object. This is useful when creating an object for accepted
55// connections, e.g., ClientSock = new XrdNetSocket("", ServSock.Accept()).
56//
57 XrdNetSocket(XrdSysError *erobj=0, int SockFileDesc=-1);
58
60
61// Create a named socket. Returns a NetSocket object that can be used for the
62// given path. A udp or tcp socket can be created on the path with the given
63// file name. The access permission mode must also be supplied. Upon failure,
64// a null pointer is returned.
65//
66static XrdNetSocket *Create(XrdSysError *Say, const char *path,
67 const char *fn, mode_t mode, int isudp=0);
68
69// Open a socket. Returns socket number upon success otherwise a -1. Use
70// LastError() to find out the reason for failure. Only one socket at a time
71// may be created. Use Close() to close the socket of Detach() to remove
72// the socket association before creating a new one.
73
74// |<-------- C l i e n t -------->| |<-------- S e r v e r -------->|
75// Unix Socket Internet Socket Unix Socket Internet Socket
76// path = Filname hostname. filename 0 or ""
77// port = -1 port number -1 port number
78// flags = ~XRDNET_SERVER ~XRDNET_SERVER XRDNET_SERVER XRDNET_SERVER
79
80// If the client path does not start with a slash and the port number is -1
81// then hostname must be of the form hostname:port. Open() will always set
82// the REUSEADDR option when binding to a port number.
83//
84 int Open(const char *path, int port=-1, int flags=0, int sockbuffsz=0);
85
86// Issue accept on the created socket. Upon success return socket FD, upon
87// failure return -1. Use LastError() to obtain reason for failure. Note that
88// Accept() is valid only for Server Sockets. An optional millisecond
89// timeout may be specified. If no new connection is attempted within the
90// millisecond time limit, a return is made with -1 and an error code of 0.
91// Accept() always sets the "close on exec" flag for the new fd.
92//
93 int Accept(int ms=-1);
94
95// Close a socket.
96//
97 void Close();
98
99// Detach the socket filedescriptor without closing it. Useful when you
100// will be attaching the descriptor to a stream. Returns the descriptor so
101// you can do something like Stream.Attach(Socket.Detach()).
102//
103 int Detach();
104
105// Return last errno.
106//
107inline int LastError() {return ErrCode;}
108
109// Obtain the name of the host on the other side of a socket. Upon success,
110// a pointer to the hostname is returned. Otherwise null is returned. An
111// optional address for holding the vided to obtain the hostname for it.
112// The string is strdup'd and is deleted when the socket object is deleted.
113//
114const char *Peername(const struct sockaddr **InetAddr=0, int *InetSize=0);
115
116// Set socket options (see definitions in XrdNetOpts.hh). The defaults
117// defaults are such that each option must be set to override the default
118// behaviour. The method is static so it can be used in any context.
119// An optional error routing object may be specified if error messages are
120// wanted. Only when all option settings succeed is 0 is returned.
121//
122static int setOpts(int fd, int options, XrdSysError *eDest=0);
123
124// Set socket recv/send buffer sizes. The method is static so it can be used in
125// any context. An optional error routing object may be specified if error
126// messages are wanted. Only when all option settings succeed is 0 is returned.
127//
128static int setWindow(int fd, int Windowsz, XrdSysError *eDest=0);
129
130static int getWindow(int fd, int &Windowsz, XrdSysError *eDest=0);
131
132// Return the name of the socket into the provided buffer. Returns 0
133// upon success or errno upon failure.
134//
135int SockName(char *buff, int blen);
136
137// Return socket file descriptor number (useful when attaching to a stream).
138//
139inline int SockNum() {return SockFD;}
140
141// Create a path to a named socket returning the actual name of the socket.
142// This method does not actually create the socket, only the path to the
143// socket. If the full path exists then it must be a named socket. Upon
144// success, it returns a pointer to the buffer holding the name (supplied by
145// the caller). Otherwise, it returns a null pointer.
146//
147static char *socketPath(XrdSysError *Say, char *inbuff,
148 const char *path, const char *fn,
149 mode_t mode);
150
151/******************************************************************************/
152
153private:
158};
159#endif
Definition XrdNetAddr.hh:42
Definition XrdNetSocket.hh:48
static char * socketPath(XrdSysError *Say, char *inbuff, const char *path, const char *fn, mode_t mode)
int LastError()
Definition XrdNetSocket.hh:107
int SockName(char *buff, int blen)
XrdNetSocket(XrdSysError *erobj=0, int SockFileDesc=-1)
int Open(const char *path, int port=-1, int flags=0, int sockbuffsz=0)
static XrdNetSocket * Create(XrdSysError *Say, const char *path, const char *fn, mode_t mode, int isudp=0)
int SockFD
Definition XrdNetSocket.hh:156
int ErrCode
Definition XrdNetSocket.hh:157
const char * Peername(const struct sockaddr **InetAddr=0, int *InetSize=0)
static int setOpts(int fd, int options, XrdSysError *eDest=0)
int SockNum()
Definition XrdNetSocket.hh:139
XrdSysError * eroute
Definition XrdNetSocket.hh:155
XrdNetAddr SockInfo
Definition XrdNetSocket.hh:154
static int setWindow(int fd, int Windowsz, XrdSysError *eDest=0)
int Accept(int ms=-1)
~XrdNetSocket()
Definition XrdNetSocket.hh:59
static int getWindow(int fd, int &Windowsz, XrdSysError *eDest=0)
Definition XrdSysError.hh:90