blob: 382b2702d38ea227bec98ad053a6c86e8e9e1c4f [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19// -=- Socket.h - abstract base-class for any kind of network stream/socket
20
21#ifndef __NETWORK_SOCKET_H__
22#define __NETWORK_SOCKET_H__
23
Pierre Ossman574dc642016-10-05 13:39:11 +020024#include <list>
25
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000026#include <limits.h>
27#include <rdr/FdInStream.h>
28#include <rdr/FdOutStream.h>
29#include <rdr/Exception.h>
30
31namespace network {
32
33 class Socket {
34 public:
35 Socket(int fd)
36 : instream(new rdr::FdInStream(fd)),
37 outstream(new rdr::FdOutStream(fd)),
38 ownStreams(true), isShutdown_(false),
39 queryConnection(false) {}
40 virtual ~Socket() {
41 if (ownStreams) {
42 delete instream;
43 delete outstream;
44 }
45 }
46 rdr::FdInStream &inStream() {return *instream;}
47 rdr::FdOutStream &outStream() {return *outstream;}
48 int getFd() {return outstream->getFd();}
49
50 // if shutdown() is overridden then the override MUST call on to here
51 virtual void shutdown() {isShutdown_ = true;}
52 bool isShutdown() const {return isShutdown_;}
Peter Åstrand (astrand)01dc1a62017-10-10 12:56:04 +020053 virtual bool cork(bool enable) = 0;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000054
55 // information about this end of the socket
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000056 virtual int getMyPort() = 0;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000057
58 // information about the remote end of the socket
59 virtual char* getPeerAddress() = 0; // a string e.g. "192.168.0.1"
60 virtual int getPeerPort() = 0;
61 virtual char* getPeerEndpoint() = 0; // <address>::<port>
62
63 // Is the remote end on the same machine?
64 virtual bool sameMachine() = 0;
65
66 // Was there a "?" in the ConnectionFilter used to accept this Socket?
67 void setRequiresQuery() {queryConnection = true;}
68 bool requiresQuery() const {return queryConnection;}
69
70 protected:
71 Socket() : instream(0), outstream(0), ownStreams(false),
72 isShutdown_(false), queryConnection(false) {}
73 Socket(rdr::FdInStream* i, rdr::FdOutStream* o, bool own)
74 : instream(i), outstream(o), ownStreams(own),
75 isShutdown_(false), queryConnection(false) {}
76 rdr::FdInStream* instream;
77 rdr::FdOutStream* outstream;
78 bool ownStreams;
79 bool isShutdown_;
80 bool queryConnection;
81 };
82
83 class ConnectionFilter {
84 public:
85 virtual bool verifyConnection(Socket* s) = 0;
Steve Kondika6424622017-07-08 01:49:14 -070086 virtual ~ConnectionFilter() {}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000087 };
88
89 class SocketListener {
90 public:
91 SocketListener() : fd(0), filter(0) {}
92 virtual ~SocketListener() {}
93
94 // shutdown() stops the socket from accepting further connections
95 virtual void shutdown() = 0;
96
97 // accept() returns a new Socket object if there is a connection
98 // attempt in progress AND if the connection passes the filter
99 // if one is installed. Otherwise, returns 0.
100 virtual Socket* accept() = 0;
101
Pierre Ossmane3a2be62018-05-03 14:03:55 +0200102 virtual int getMyPort() = 0;
103
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000104 // setFilter() applies the specified filter to all new connections
105 void setFilter(ConnectionFilter* f) {filter = f;}
106 int getFd() {return fd;}
107 protected:
108 int fd;
109 ConnectionFilter* filter;
110 };
111
112 struct SocketException : public rdr::SystemException {
113 SocketException(const char* text, int err_) : rdr::SystemException(text, err_) {}
114 };
115
116 class SocketServer {
117 public:
118 virtual ~SocketServer() {}
119
120 // addSocket() tells the server to serve the Socket. The caller
121 // retains ownership of the Socket - the only way for the server
122 // to discard a Socket is by calling shutdown() on it.
123 // outgoing is set to true if the socket was created by connecting out
124 // to another host, or false if the socket was created by accept()ing
125 // an incoming connection.
126 virtual void addSocket(network::Socket* sock, bool outgoing=false) = 0;
127
128 // removeSocket() tells the server to stop serving the Socket. The
129 // caller retains ownership of the Socket - the server must NOT
130 // delete the Socket! This call is used mainly to cause per-Socket
131 // resources to be freed.
132 virtual void removeSocket(network::Socket* sock) = 0;
133
Pierre Ossman574dc642016-10-05 13:39:11 +0200134 // getSockets() gets a list of sockets. This can be used to generate an
135 // fd_set for calling select().
136 virtual void getSockets(std::list<network::Socket*>* sockets) = 0;
137
Pierre Ossmand408ca52016-04-29 14:26:05 +0200138 // processSocketReadEvent() tells the server there is a Socket read event.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000139 // The implementation can indicate that the Socket is no longer active
140 // by calling shutdown() on it. The caller will then call removeSocket()
141 // soon after processSocketEvent returns, to allow any pre-Socket
142 // resources to be tidied up.
Pierre Ossmand408ca52016-04-29 14:26:05 +0200143 virtual void processSocketReadEvent(network::Socket* sock) = 0;
144
145 // processSocketReadEvent() tells the server there is a Socket write event.
146 // This is only necessary if the Socket has been put in non-blocking
147 // mode and needs this callback to flush the buffer.
148 virtual void processSocketWriteEvent(network::Socket* sock) = 0;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000149
150 // checkTimeouts() allows the server to check socket timeouts, etc. The
151 // return value is the number of milliseconds to wait before
152 // checkTimeouts() should be called again. If this number is zero then
153 // there is no timeout and checkTimeouts() should be called the next time
154 // an event occurs.
155 virtual int checkTimeouts() = 0;
156
157 virtual bool getDisable() {return false;};
158 };
159
160}
161
162#endif // __NETWORK_SOCKET_H__