CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/external/source/vncdll/winvnc/VSocket.h
Views: 11778
1
// Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
2
// Copyright (C) 2001 HorizonLive.com, Inc. All Rights Reserved.
3
//
4
// This file is part of the VNC system.
5
//
6
// The VNC system is free software; you can redistribute it and/or modify
7
// it under the terms of the GNU General Public License as published by
8
// the Free Software Foundation; either version 2 of the License, or
9
// (at your option) any later version.
10
//
11
// This program is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
// GNU General Public License for more details.
15
//
16
// You should have received a copy of the GNU General Public License
17
// along with this program; if not, write to the Free Software
18
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19
// USA.
20
//
21
// TightVNC distribution homepage on the Web: http://www.tightvnc.com/
22
//
23
// If the source code for the VNC system is not available from the place
24
// whence you received this file, check http://www.uk.research.att.com/vnc or contact
25
// the authors on [email protected] for information on obtaining it.
26
27
28
// VSocket.h
29
30
// RFB V3.0
31
32
// The VSocket class provides simple socket functionality,
33
// independent of platform. Hurrah.
34
35
class VSocket;
36
37
#if (!defined(_ATT_VSOCKET_DEFINED))
38
#define _ATT_VSOCKET_DEFINED
39
40
#include <omnithread.h>
41
#include "VTypes.h"
42
43
// This class is used as a part of output queue
44
class AIOBlock
45
{
46
public:
47
size_t data_size; // Data size in this block
48
char *data_ptr; // Beginning of the data buffer
49
AIOBlock *next; // Next block or NULL for the last block
50
51
AIOBlock(int size, const char *data = NULL) {
52
next = NULL;
53
data_size = size;
54
data_ptr = new char[size];
55
if (data_ptr && data)
56
memcpy(data_ptr, data, size);
57
}
58
~AIOBlock() {
59
if (data_ptr)
60
delete[] data_ptr;
61
}
62
};
63
64
////////////////////////////
65
// Socket implementation
66
67
// Create one or more VSocketSystem objects per application
68
class VSocketSystem
69
{
70
public:
71
VSocketSystem();
72
~VSocketSystem();
73
VBool Initialised() {return m_status;};
74
private:
75
VBool m_status;
76
};
77
78
// The main socket class
79
class VSocket
80
{
81
public:
82
// Constructor/Destructor
83
VSocket();
84
VSocket( WSAPROTOCOL_INFO * pSocketInfo, HANDLE hClose );
85
virtual ~VSocket();
86
87
////////////////////////////
88
// Socket implementation
89
90
// Create
91
// Create a socket and attach it to this VSocket object
92
VBool Create();
93
94
// Shutdown
95
// Shutdown the currently attached socket
96
VBool Shutdown();
97
98
// Close
99
// Close the currently attached socket
100
VBool Close();
101
102
// Bind
103
// Bind the attached socket to the specified port
104
// If localOnly is VTrue then the socket is bound only
105
// to the loopback adapter. If checkIfInUse is VTrue,
106
// then the socket won't be bound to an address which
107
// is already in use (i.e. accepts connections).
108
VBool Bind(const VCard port, const VBool localOnly = VFalse,
109
const VBool checkIfInUse = VFalse);
110
111
// Connect
112
// Make a stream socket connection to the specified port
113
// on the named machine.
114
VBool Connect(VStringConst address, const VCard port);
115
116
// Listen
117
// Set the attached socket to listen for connections
118
VBool Listen();
119
120
// Accept
121
// If the attached socket is set to listen then this
122
// call blocks waiting for an incoming connection, then
123
// returns a new socket object for the new connection
124
VSocket *Accept();
125
126
// TryAccept
127
// Non-blocking version of Accept. It waits for an
128
// incoming connection only for the specified number of
129
// milliseconds. It returns VFalse on error, otherwise stores
130
// either pointer to the new VSocket, or NULL on timeout
131
VBool TryAccept(VSocket **new_socket, long ms);
132
133
// GetPeerName
134
// If the socket is connected then this returns the name
135
// of the machine to which it is connected.
136
// This string MUST be copied before the next socket call...
137
VString GetPeerName();
138
139
// GetSockName
140
// If the socket exists then the name of the local machine
141
// is returned. This string MUST be copied before the next
142
// socket call!
143
VString GetSockName();
144
145
// Resolve
146
// Uses the Winsock API to resolve the supplied DNS name to
147
// an IP address and returns it as an Int32
148
static VCard32 Resolve(VStringConst name);
149
150
// SetTimeout
151
// Sets the socket timeout on reads and writes.
152
VBool SetTimeout(VCard32 secs);
153
154
// I/O routines
155
156
// Send and Read return the number of bytes sent or recieved.
157
VInt Send(const char *buff, const VCard bufflen);
158
VInt Read(char *buff, const VCard bufflen);
159
160
// SendExact and ReadExact attempt to send and recieve exactly
161
// the specified number of bytes.
162
VBool SendExact(const char *buff, const VCard bufflen);
163
VBool ReadExact(char *buff, const VCard bufflen);
164
165
// SendQueued sends as much data as possible immediately,
166
// and puts remaining bytes in a queue, to be sent later.
167
VBool SendQueued(const char *buff, const VCard bufflen);
168
169
////////////////////////////
170
// Internal structures
171
protected:
172
// The internal socket id
173
int sock;
174
HANDLE hCloseEvent;
175
176
// Output queue
177
size_t bytes_sent;
178
AIOBlock *out_queue;
179
omni_mutex queue_lock;
180
181
VBool SendFromQueue();
182
};
183
184
#endif // _ATT_VSOCKET_DEFINED
185
186