Qore Programming Language 1.19.2
Loading...
Searching...
No Matches
QoreSocketObject.h
1/* -*- mode: c++; indent-tabs-mode: nil -*- */
2/*
3 QoreSocketObject.h
4
5 Qore Programming Language
6
7 Copyright (C) 2003 - 2023 Qore Technologies, s.r.o.
8
9 provides a thread-safe interface to the QoreSocket object
10
11 Permission is hereby granted, free of charge, to any person obtaining a
12 copy of this software and associated documentation files (the "Software"),
13 to deal in the Software without restriction, including without limitation
14 the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 and/or sell copies of the Software, and to permit persons to whom the
16 Software is furnished to do so, subject to the following conditions:
17
18 The above copyright notice and this permission notice shall be included in
19 all copies or substantial portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 DEALINGS IN THE SOFTWARE.
28
29 Note that the Qore library is released under a choice of three open-source
30 licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
31 information.
32*/
33
34#ifndef _QORE_QORE_SOCKET_OBJECT_H
35
36#define _QORE_QORE_SOCKET_OBJECT_H
37
38#include <qore/QoreSocket.h>
39#include <qore/AbstractPollableIoObjectBase.h>
40#include <qore/QoreThreadLock.h>
41
44class Queue;
45class my_socket_priv;
46
47class QoreSocketObject : public AbstractPollableIoObjectBase {
48 friend class my_socket_priv;
49 friend hashdecl qore_httpclient_priv;
50 friend class SocketConnectPollOperation;
51 friend class SocketSendPollOperation;
52 friend class SocketRecvPollOperationBase;
53 friend class SocketRecvPollOperation;
54 friend class SocketRecvUntilBytesPollOperation;
55 friend class SocketUpgradeClientSslPollOperation;
56 friend class HttpClientConnectPollOperation;
57
58public:
59 DLLEXPORT QoreSocketObject();
60
61 DLLEXPORT virtual void deref(ExceptionSink* xsink);
62 DLLEXPORT virtual void deref();
63
65
69 DLLEXPORT void invalidate(ExceptionSink* xsink);
70
72
79 DLLEXPORT AbstractPollState* startConnect(ExceptionSink* xsink, const char* name);
80
82
90 DLLEXPORT AbstractPollState* startSslConnect(ExceptionSink* xsink, X509* cert = nullptr,
91 EVP_PKEY* pkey = nullptr);
92
94
103 DLLEXPORT AbstractPollState* startSend(ExceptionSink* xsink, const char* data, size_t size);
104
106
114 DLLEXPORT AbstractPollState* startRecv(ExceptionSink* xsink, size_t size);
115
117
127 DLLEXPORT AbstractPollState* startRecvUntilBytes(ExceptionSink* xsink, const char* pattern, size_t size);
128
129 DLLEXPORT int connect(const char* name, int timeout_ms, ExceptionSink* xsink = nullptr);
130 DLLEXPORT int connectINET(const char* host, int port, int timeout_ms, ExceptionSink* xsink = nullptr);
131 DLLEXPORT int connectINET2(const char* host, const char* service, int family, int sock_type, int protocol,
132 int timeout_ms = -1, ExceptionSink* xsink = nullptr);
133 DLLEXPORT int connectUNIX(const char* p, int socktype, int protocol, ExceptionSink* xsink = nullptr);
134 DLLEXPORT int connectSSL(const char* name, int timeout_ms, ExceptionSink* xsink);
135 DLLEXPORT int connectINETSSL(const char* host, int port, int timeout_ms, ExceptionSink* xsink);
136 DLLEXPORT int connectINET2SSL(const char* host, const char* service, int family, int sock_type, int protocol,
137 int timeout_ms = -1, ExceptionSink* xsink = nullptr);
138 DLLEXPORT int connectUNIXSSL(const char* p, int socktype, int protocol, ExceptionSink* xsink);
139 // to bind to either a UNIX socket or an INET interface:port
140 DLLEXPORT int bind(const char* name, bool reuseaddr = false);
141 // to bind to an INET tcp port on all interfaces
142 DLLEXPORT int bind(int port, bool reuseaddr = false);
143 // to bind an open socket to an INET tcp port on a specific interface
144 DLLEXPORT int bind(const char* iface, int port, bool reuseaddr = false);
145
146 DLLEXPORT int bindUNIX(const char* name, int socktype, int protocol, ExceptionSink* xsink);
147 DLLEXPORT int bindINET(const char* name, const char* service, bool reuseaddr, int family, int socktype,
148 int protocol, ExceptionSink* xsink);
149
150 // get port number for INET sockets
151 DLLEXPORT int getPort();
152 DLLEXPORT QoreSocketObject* accept(SocketSource* source, ExceptionSink* xsink);
153 DLLEXPORT QoreSocketObject* acceptSSL(SocketSource* source, ExceptionSink* xsink);
154 DLLEXPORT QoreSocketObject* accept(int timeout_ms, ExceptionSink* xsink);
155 DLLEXPORT QoreSocketObject* acceptSSL(int timeout_ms, ExceptionSink* xsink);
156
157 DLLEXPORT int listen(int backlog);
158 // send a buffer of a particular size
159 DLLEXPORT int send(const char* buf, int size);
160 DLLEXPORT int send(const char* buf, int size, int timeout_ms, ExceptionSink* xsink);
161 // send a null-terminated string
162 DLLEXPORT int send(const QoreStringNode& msg, int timeout_ms, ExceptionSink* xsink);
163 // send a binary object
164 DLLEXPORT int send(const BinaryNode* b);
165 DLLEXPORT int send(const BinaryNode* b, int timeout_ms, ExceptionSink* xsink);
166 // send a certain number of bytes (read from an InputStream)
167 DLLEXPORT void sendFromInputStream(InputStream* is, int64 size, int64 timeout_ms, ExceptionSink *xsink);
168
169 // send from a file descriptor
170 DLLEXPORT int send(int fd, int size = -1);
171 // send bytes and convert to network order
172 DLLEXPORT int sendi1(char b, int timeout_ms, ExceptionSink* xsink);
173 DLLEXPORT int sendi2(short b, int timeout_ms, ExceptionSink* xsink);
174 DLLEXPORT int sendi4(int b, int timeout_ms, ExceptionSink* xsink);
175 DLLEXPORT int sendi8(int64 b, int timeout_ms, ExceptionSink* xsink);
176 DLLEXPORT int sendi2LSB(short b, int timeout_ms, ExceptionSink* xsink);
177 DLLEXPORT int sendi4LSB(int b, int timeout_ms, ExceptionSink* xsink);
178 DLLEXPORT int sendi8LSB(int64 b, int timeout_ms, ExceptionSink* xsink);
179 // receive a message
180 DLLEXPORT QoreStringNode* recv(int timeout, ExceptionSink* xsink);
181 // receive a certain number of bytes as a string
182 DLLEXPORT QoreStringNode* recv(qore_offset_t bufsize, int timeout_ms, ExceptionSink* xsink);
183 // receive a certain number of bytes as a binary object
184 DLLEXPORT BinaryNode* recvBinary(int bufsize, int timeout, ExceptionSink* xsink);
185 // receive a packet of bytes as a binary object
186 DLLEXPORT BinaryNode* recvBinary(int timeout, ExceptionSink* xsink);
187 // receive a certain number of bytes and write them to an OutputStream
188 DLLEXPORT void recvToOutputStream(OutputStream* os, int64 size, int64 timeout_ms, ExceptionSink *xsink);
189
190 // receive and write data to a file descriptor
191 DLLEXPORT int recv(int fd, int size, int timeout);
192 // receive integers and convert from network byte order
193 DLLEXPORT int64 recvi1(int timeout, char* b, ExceptionSink* xsink);
194 DLLEXPORT int64 recvi2(int timeout, short* b, ExceptionSink* xsink);
195 DLLEXPORT int64 recvi4(int timeout, int* b, ExceptionSink* xsink);
196 DLLEXPORT int64 recvi8(int timeout, int64* b, ExceptionSink* xsink);
197 DLLEXPORT int64 recvi2LSB(int timeout, short* b, ExceptionSink* xsink);
198 DLLEXPORT int64 recvi4LSB(int timeout, int* b, ExceptionSink* xsink);
199 DLLEXPORT int64 recvi8LSB(int timeout, int64* b, ExceptionSink* xsink);
200 DLLEXPORT int64 recvu1(int timeout, unsigned char* b, ExceptionSink* xsink);
201 DLLEXPORT int64 recvu2(int timeout, unsigned short* b, ExceptionSink* xsink);
202 DLLEXPORT int64 recvu4(int timeout, unsigned int* b, ExceptionSink* xsink);
203 DLLEXPORT int64 recvu2LSB(int timeout, unsigned short* b, ExceptionSink* xsink);
204 DLLEXPORT int64 recvu4LSB(int timeout, unsigned int* b, ExceptionSink* xsink);
205
206 // send HTTP message
207 DLLEXPORT int sendHTTPMessage(ExceptionSink* xsink, QoreHashNode* info, const char* method, const char* path,
208 const char* http_version, const QoreHashNode* headers, const void* ptr, int size, int source, int timeout_ms);
209 DLLEXPORT int sendHTTPMessage(ExceptionSink* xsink, QoreHashNode* info, const char* method, const char* path,
210 const char* http_version, const QoreHashNode* headers, const QoreStringNode& body, int source, int timeout_ms);
211 DLLEXPORT int sendHTTPMessageWithCallback(ExceptionSink* xsink, QoreHashNode* info, const char* method,
212 const char *path, const char *http_version, const QoreHashNode* headers,
213 const ResolvedCallReferenceNode& send_callback, int source, int timeout_ms, bool* aborted = nullptr);
214
215 // send HTTP response
216 DLLEXPORT int sendHTTPResponse(ExceptionSink* xsink, QoreHashNode* info, int code, const char* desc,
217 const char* http_version, const QoreHashNode* headers, const void* data, size_t size, int source,
218 int timeout_ms);
219 DLLEXPORT int sendHTTPResponse(ExceptionSink* xsink, QoreHashNode* info, int code, const char* desc,
220 const char* http_version, const QoreHashNode* headers, const QoreStringNode& body, int source,
221 int timeout_ms);
222
223 // send HTTP response from stream
224 DLLEXPORT int sendHTTPResponse(ExceptionSink* xsink, QoreHashNode* info, int code, const char* desc,
225 const char* http_version, const QoreHashNode* headers, InputStream* is, size_t max_chunked_size,
226 const ResolvedCallReferenceNode* trailer_callback, int source, int timeout_ms);
227
228 // send HTTP response from callback
229 DLLEXPORT int sendHTTPResponseWithCallback(ExceptionSink* xsink, QoreHashNode* info, int code, const char *desc,
230 const char *http_version, const QoreHashNode* headers, const ResolvedCallReferenceNode& send_callback,
231 int source, int timeout_ms, bool* aborted = nullptr);
232
233 // send data in HTTP chunked format
234 DLLEXPORT void sendHTTPChunkedBodyFromInputStream(InputStream* is, size_t max_chunked_size, const int timeout_ms,
235 const ResolvedCallReferenceNode* trailer_callback, ExceptionSink* xsink);
236 DLLEXPORT void sendHTTPChunkedBodyTrailer(const QoreHashNode* headers, int timeout_ms, ExceptionSink* xsink);
237
238 // read and parse HTTP header
239 DLLEXPORT AbstractQoreNode* readHTTPHeader(ExceptionSink* xsink, QoreHashNode* info, int timeout);
240 // receive a binary message in HTTP chunked format
241 DLLEXPORT QoreHashNode* readHTTPChunkedBodyBinary(int timeout, ExceptionSink* xsink);
242 // receive a binary message in HTTP chunked format
243 DLLEXPORT QoreHashNode* readHTTPChunkedBodyToOutputStream(OutputStream* os, int timeout_ms, ExceptionSink* xsink);
244 // receive a string message in HTTP chunked format
245 DLLEXPORT QoreHashNode* readHTTPChunkedBody(int timeout, ExceptionSink* xsink);
246
247 // receive a binary message in HTTP chunked format
248 DLLEXPORT void readHTTPChunkedBodyBinaryWithCallback(const ResolvedCallReferenceNode& recv_callback,
249 QoreObject* obj, int timeout_ms, ExceptionSink* xsink);
250 // receive a string message in HTTP chunked format
251 DLLEXPORT void readHTTPChunkedBodyWithCallback(const ResolvedCallReferenceNode& recv_callback, QoreObject* obj,
252 int timeout_ms, ExceptionSink* xsink);
253
254 DLLEXPORT QoreStringNode* readHTTPHeaderString(ExceptionSink* xsink, int timeout_ms);
255
257
261 DLLEXPORT int getPollableDescriptor() const;
262
263 DLLEXPORT int setSendTimeout(int ms);
264 DLLEXPORT int setRecvTimeout(int ms);
265 DLLEXPORT int getSendTimeout();
266 DLLEXPORT int getRecvTimeout();
267 DLLEXPORT int close();
268 DLLEXPORT int shutdown();
269 DLLEXPORT int shutdownSSL(ExceptionSink* xsink) ;
270 DLLEXPORT const char* getSSLCipherName();
271 DLLEXPORT const char* getSSLCipherVersion();
272 DLLEXPORT bool isSecure();
273 DLLEXPORT long verifyPeerCertificate();
274 DLLEXPORT int getSocket();
275 DLLEXPORT void setEncoding(const QoreEncoding* id);
276 DLLEXPORT const QoreEncoding* getEncoding() const;
277 DLLEXPORT bool isDataAvailable(ExceptionSink* xsink, int timeout = 0);
278 DLLEXPORT bool isWriteFinished(ExceptionSink* xsink, int timeout = 0);
279 DLLEXPORT bool isOpen() const;
280 // c must be already referenced before this call
281 DLLEXPORT void setCertificate(QoreSSLCertificate* c);
282 // p must be already referenced before this call
283 DLLEXPORT void setPrivateKey(QoreSSLPrivateKey* p);
284 // c and p must be already referenced before this call
285 DLLEXPORT void setCertificateAndPrivateKey(QoreSSLCertificate* c, QoreSSLPrivateKey* p);
286 DLLEXPORT int setNoDelay(int nodelay);
287 DLLEXPORT int getNoDelay();
288 DLLEXPORT void setEventQueue(ExceptionSink* xsink, Queue* q, QoreValue arg, bool with_data);
289 DLLEXPORT void setEventQueue(Queue* cbq, ExceptionSink* xsink);
290 DLLEXPORT QoreHashNode* getPeerInfo(ExceptionSink* xsink, bool host_lookup = true) const;
291 DLLEXPORT QoreHashNode* getSocketInfo(ExceptionSink* xsink, bool host_lookup = true) const;
292
293 DLLEXPORT void upgradeClientToSSL(ExceptionSink* xsink);
294 DLLEXPORT void upgradeServerToSSL(ExceptionSink* xsink);
295
296 DLLEXPORT void upgradeClientToSSL(int timeout_ms, ExceptionSink* xsink);
297 DLLEXPORT void upgradeServerToSSL(int timeout_ms, ExceptionSink* xsink);
298
299 DLLEXPORT void clearWarningQueue(ExceptionSink* xsink);
300 DLLEXPORT void setWarningQueue(ExceptionSink* xsink, int64 warning_ms, int64 warning_bs, Queue* wq, QoreValue arg,
301 int64 min_ms = 1000);
302 DLLEXPORT QoreHashNode* getUsageInfo() const;
303 DLLEXPORT void clearStats();
304 DLLEXPORT bool pendingHttpChunkedBody() const;
305 DLLEXPORT void setSslVerifyMode(int mode);
306 DLLEXPORT int getSslVerifyMode() const;
307 DLLEXPORT void acceptAllCertificates(bool accept_all = true);
308 DLLEXPORT bool getAcceptAllCertificates() const;
309 DLLEXPORT bool captureRemoteCertificates(bool set);
310 DLLEXPORT QoreObject* getRemoteCertificate() const;
311 DLLEXPORT int64 getConnectionId() const;
312
314 DLLEXPORT int setNonBlock(ExceptionSink* xsink);
315
317 DLLEXPORT void clearNonBlock();
318
319private:
320 DLLLOCAL QoreSocketObject(QoreSocket* s, QoreSSLCertificate* cert = nullptr, QoreSSLPrivateKey* pk = nullptr);
321
322protected:
323 my_socket_priv* priv;
324
325 DLLLOCAL virtual ~QoreSocketObject();
326};
327
328#endif // _QORE_QORE_SOCKET_OBJECT_H
The base class for all value and parse types in Qore expression trees.
Definition: AbstractQoreNode.h:57
holds arbitrary binary data
Definition: BinaryNode.h:41
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:50
Interface for private data of input streams.
Definition: InputStream.h:44
Interface for private data of output streams.
Definition: OutputStream.h:44
defines string encoding functions in Qore
Definition: QoreEncoding.h:83
This is the hash or associative list container type in Qore, dynamically allocated only,...
Definition: QoreHashNode.h:50
the implementation of Qore's object data type, reference counted, dynamically-allocated only
Definition: QoreObject.h:60
represents an X509 certificate, reference-counted, dynamically-allocated only
Definition: QoreSSLCertificate.h:42
provides access to a private key data structure for SSL connections
Definition: QoreSSLPrivateKey.h:40
provides access to sockets using Qore data structures
Definition: QoreSocket.h:127
Qore's string value type, reference counted, dynamically-allocated only.
Definition: QoreStringNode.h:50
base class for resolved call references
Definition: CallReferenceNode.h:109
a helper class for getting socket origination information
Definition: QoreSocket.h:74
intptr_t qore_offset_t
used for offsets that could be negative
Definition: common.h:76
long long int64
64bit integer type, cannot use int64_t here since it breaks the API on some 64-bit systems due to equ...
Definition: common.h:260
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:276