Qore Programming Language - C/C++ Library 2.1.1
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 - 2024 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 struct qore_httpclient_priv;
50 friend class SocketPollSocketOperationBase;
51 friend class SocketConnectPollOperation;
52 friend class SocketAcceptPollSocketOperationBase;
53 friend class SocketAcceptPollOperation;
54 friend class SocketSendPollOperation;
55 friend class SocketRecvPollOperationBase;
56 friend class SocketRecvPollOperation;
57 friend class SocketRecvDataPollOperation;
58 friend class SocketReadHttpHeaderPollOperation;
59 friend class SocketRecvUntilBytesPollOperation;
60 friend class SocketUpgradeClientSslPollOperation;
61 friend class SocketUpgradeServerSslPollOperation;
62 friend class HttpClientConnectPollOperation;
63
64public:
65 DLLEXPORT QoreSocketObject();
66
67 DLLEXPORT QoreSocketObject(QoreSocketObject& orig, int descriptor);
68
69 DLLEXPORT virtual void deref(ExceptionSink* xsink);
70 DLLEXPORT virtual void deref();
71
73
77 DLLEXPORT void invalidate(ExceptionSink* xsink);
78
80
87 DLLEXPORT AbstractPollState* startConnect(ExceptionSink* xsink, const char* name);
88
90
96 DLLEXPORT AbstractPollState* startSslConnect(ExceptionSink* xsink);
97
99
108 DLLEXPORT AbstractPollState* startSend(ExceptionSink* xsink, const char* data, size_t size);
109
111
119 DLLEXPORT AbstractPollState* startAccept(ExceptionSink* xsink);
120
122
128 DLLEXPORT AbstractPollState* startSslAccept(ExceptionSink* xsink);
129
131
139 DLLEXPORT AbstractPollState* startRecv(ExceptionSink* xsink, size_t size);
140
142
152 DLLEXPORT AbstractPollState* startRecvUntilBytes(ExceptionSink* xsink, const char* pattern, size_t size);
153
155
162 DLLEXPORT AbstractPollState* startRecvPacket(ExceptionSink* xsink);
163
164 DLLEXPORT int connect(const char* name, int timeout_ms, ExceptionSink* xsink = nullptr);
165 DLLEXPORT int connectINET(const char* host, int port, int timeout_ms, ExceptionSink* xsink = nullptr);
166 DLLEXPORT int connectINET2(const char* host, const char* service, int family, int sock_type, int protocol,
167 int timeout_ms = -1, ExceptionSink* xsink = nullptr);
168 DLLEXPORT int connectUNIX(const char* p, int socktype, int protocol, ExceptionSink* xsink = nullptr);
169 DLLEXPORT int connectSSL(ExceptionSink* xsink, const char* name, int timeout_ms);
170 DLLEXPORT int connectINETSSL(ExceptionSink* xsink, const char* host, int port, int timeout_ms);
171 DLLEXPORT int connectINET2SSL(ExceptionSink* xsink, const char* host, const char* service, int family,
172 int sock_type, int protocol, int timeout_ms = -1);
173 DLLEXPORT int connectUNIXSSL(ExceptionSink* xsink, const char* p, int socktype, int protocol);
174 // to bind to either a UNIX socket or an INET interface:port
175 DLLEXPORT int bind(const char* name, bool reuseaddr = false);
176 // to bind to an INET tcp port on all interfaces
177 DLLEXPORT int bind(int port, bool reuseaddr = false);
178 // to bind an open socket to an INET tcp port on a specific interface
179 DLLEXPORT int bind(const char* iface, int port, bool reuseaddr = false);
180
181 DLLEXPORT int bindUNIX(const char* name, int socktype, int protocol, ExceptionSink* xsink);
182 DLLEXPORT int bindINET(const char* name, const char* service, bool reuseaddr, int family, int socktype,
183 int protocol, ExceptionSink* xsink);
184
185 // get port number for INET sockets
186 DLLEXPORT int getPort();
187 DLLEXPORT QoreSocketObject* accept(SocketSource* source, ExceptionSink* xsink);
188 DLLEXPORT QoreSocketObject* acceptSSL(ExceptionSink* xsink, SocketSource* source);
189 DLLEXPORT QoreSocketObject* accept(int timeout_ms, ExceptionSink* xsink);
190 DLLEXPORT QoreSocketObject* acceptSSL(ExceptionSink* xsink, int timeout_ms);
191
192 DLLEXPORT int listen(int backlog);
193 // send a buffer of a particular size
194 DLLEXPORT int send(const char* buf, int size);
195 DLLEXPORT int send(const char* buf, int size, int timeout_ms, ExceptionSink* xsink);
196 // send a null-terminated string
197 DLLEXPORT int send(const QoreStringNode& msg, int timeout_ms, ExceptionSink* xsink);
198 // send a binary object
199 DLLEXPORT int send(const BinaryNode* b);
200 DLLEXPORT int send(const BinaryNode* b, int timeout_ms, ExceptionSink* xsink);
201 // send a certain number of bytes (read from an InputStream)
202 DLLEXPORT void sendFromInputStream(InputStream* is, int64 size, int64 timeout_ms, ExceptionSink *xsink);
203
204 // send from a file descriptor
205 DLLEXPORT int send(int fd, int size = -1);
206 // send bytes and convert to network order
207 DLLEXPORT int sendi1(char b, int timeout_ms, ExceptionSink* xsink);
208 DLLEXPORT int sendi2(short b, int timeout_ms, ExceptionSink* xsink);
209 DLLEXPORT int sendi4(int b, int timeout_ms, ExceptionSink* xsink);
210 DLLEXPORT int sendi8(int64 b, int timeout_ms, ExceptionSink* xsink);
211 DLLEXPORT int sendi2LSB(short b, int timeout_ms, ExceptionSink* xsink);
212 DLLEXPORT int sendi4LSB(int b, int timeout_ms, ExceptionSink* xsink);
213 DLLEXPORT int sendi8LSB(int64 b, int timeout_ms, ExceptionSink* xsink);
214 // receive a message
215 DLLEXPORT QoreStringNode* recv(int timeout, ExceptionSink* xsink);
216 // receive a certain number of bytes as a string
217 DLLEXPORT QoreStringNode* recv(qore_offset_t bufsize, int timeout_ms, ExceptionSink* xsink);
218 // receive a certain number of bytes as a binary object
219 DLLEXPORT BinaryNode* recvBinary(int bufsize, int timeout, ExceptionSink* xsink);
220 // receive a packet of bytes as a binary object
221 DLLEXPORT BinaryNode* recvBinary(int timeout, ExceptionSink* xsink);
222 // receive a certain number of bytes and write them to an OutputStream
223 DLLEXPORT void recvToOutputStream(OutputStream* os, int64 size, int64 timeout_ms, ExceptionSink *xsink);
224
225 // receive and write data to a file descriptor
226 DLLEXPORT int recv(int fd, int size, int timeout);
227 // receive integers and convert from network byte order
228 DLLEXPORT int64 recvi1(int timeout, char* b, ExceptionSink* xsink);
229 DLLEXPORT int64 recvi2(int timeout, short* b, ExceptionSink* xsink);
230 DLLEXPORT int64 recvi4(int timeout, int* b, ExceptionSink* xsink);
231 DLLEXPORT int64 recvi8(int timeout, int64* b, ExceptionSink* xsink);
232 DLLEXPORT int64 recvi2LSB(int timeout, short* b, ExceptionSink* xsink);
233 DLLEXPORT int64 recvi4LSB(int timeout, int* b, ExceptionSink* xsink);
234 DLLEXPORT int64 recvi8LSB(int timeout, int64* b, ExceptionSink* xsink);
235 DLLEXPORT int64 recvu1(int timeout, unsigned char* b, ExceptionSink* xsink);
236 DLLEXPORT int64 recvu2(int timeout, unsigned short* b, ExceptionSink* xsink);
237 DLLEXPORT int64 recvu4(int timeout, unsigned int* b, ExceptionSink* xsink);
238 DLLEXPORT int64 recvu2LSB(int timeout, unsigned short* b, ExceptionSink* xsink);
239 DLLEXPORT int64 recvu4LSB(int timeout, unsigned int* b, ExceptionSink* xsink);
240
241 // send HTTP message
242 DLLEXPORT int sendHTTPMessage(ExceptionSink* xsink, QoreHashNode* info, const char* method, const char* path,
243 const char* http_version, const QoreHashNode* headers, const void* ptr, int size, int source, int timeout_ms);
244 DLLEXPORT int sendHTTPMessage(ExceptionSink* xsink, QoreHashNode* info, const char* method, const char* path,
245 const char* http_version, const QoreHashNode* headers, const QoreStringNode& body, int source, int timeout_ms);
246 DLLEXPORT int sendHTTPMessageWithCallback(ExceptionSink* xsink, QoreHashNode* info, const char* method,
247 const char *path, const char *http_version, const QoreHashNode* headers,
248 const ResolvedCallReferenceNode& send_callback, int source, int timeout_ms, bool* aborted = nullptr);
249
250 // send HTTP response
251 DLLEXPORT int sendHTTPResponse(ExceptionSink* xsink, QoreHashNode* info, int code, const char* desc,
252 const char* http_version, const QoreHashNode* headers, const void* data, size_t size, int source,
253 int timeout_ms);
254 DLLEXPORT int sendHTTPResponse(ExceptionSink* xsink, QoreHashNode* info, int code, const char* desc,
255 const char* http_version, const QoreHashNode* headers, const QoreStringNode& body, int source,
256 int timeout_ms);
257
258 // send HTTP response from stream
259 DLLEXPORT int sendHTTPResponse(ExceptionSink* xsink, QoreHashNode* info, int code, const char* desc,
260 const char* http_version, const QoreHashNode* headers, InputStream* is, size_t max_chunked_size,
261 const ResolvedCallReferenceNode* trailer_callback, int source, int timeout_ms);
262
263 // send HTTP response from callback
264 DLLEXPORT int sendHTTPResponseWithCallback(ExceptionSink* xsink, QoreHashNode* info, int code, const char *desc,
265 const char *http_version, const QoreHashNode* headers, const ResolvedCallReferenceNode& send_callback,
266 int source, int timeout_ms, bool* aborted = nullptr);
267
268 // send data in HTTP chunked format
269 DLLEXPORT void sendHTTPChunkedBodyFromInputStream(InputStream* is, size_t max_chunked_size, const int timeout_ms,
270 const ResolvedCallReferenceNode* trailer_callback, ExceptionSink* xsink);
271 DLLEXPORT void sendHTTPChunkedBodyTrailer(const QoreHashNode* headers, int timeout_ms, ExceptionSink* xsink);
272
273 // read and parse HTTP header
274 DLLEXPORT AbstractQoreNode* readHTTPHeader(ExceptionSink* xsink, QoreHashNode* info, int timeout);
275 // receive a binary message in HTTP chunked format
276 DLLEXPORT QoreHashNode* readHTTPChunkedBodyBinary(int timeout, ExceptionSink* xsink);
277 // receive a binary message in HTTP chunked format
278 DLLEXPORT QoreHashNode* readHTTPChunkedBodyToOutputStream(OutputStream* os, int timeout_ms, ExceptionSink* xsink);
279 // receive a string message in HTTP chunked format
280 DLLEXPORT QoreHashNode* readHTTPChunkedBody(int timeout, ExceptionSink* xsink);
281
282 // receive a single HTTP chunk
283 DLLEXPORT QoreHashNode* readHttpChunk(int timeout, ExceptionSink* xsink);
284
285 // receive a binary message in HTTP chunked format
286 DLLEXPORT void readHTTPChunkedBodyBinaryWithCallback(const ResolvedCallReferenceNode& recv_callback,
287 QoreObject* obj, int timeout_ms, ExceptionSink* xsink);
288 // receive a string message in HTTP chunked format
289 DLLEXPORT void readHTTPChunkedBodyWithCallback(const ResolvedCallReferenceNode& recv_callback, QoreObject* obj,
290 int timeout_ms, ExceptionSink* xsink);
291
292 DLLEXPORT QoreStringNode* readHTTPHeaderString(ExceptionSink* xsink, int timeout_ms);
293
294 DLLEXPORT QoreHashNode* readServerSentEvent(ExceptionSink* xsink, const QoreStringNode* content_encoding,
295 int timeout_ms);
296
298
302 DLLEXPORT int getPollableDescriptor() const;
303
304 DLLEXPORT int setSendTimeout(int ms);
305 DLLEXPORT int setRecvTimeout(int ms);
306 DLLEXPORT int getSendTimeout();
307 DLLEXPORT int getRecvTimeout();
308 DLLEXPORT int close();
309 DLLEXPORT int shutdown();
310 DLLEXPORT int shutdownSSL(ExceptionSink* xsink) ;
311 DLLEXPORT const char* getSSLCipherName();
312 DLLEXPORT const char* getSSLCipherVersion();
313 DLLEXPORT bool isSecure();
314 DLLEXPORT long verifyPeerCertificate();
315 DLLEXPORT int getSocket();
316 DLLEXPORT void setEncoding(const QoreEncoding* id);
317 DLLEXPORT const QoreEncoding* getEncoding() const;
318 DLLEXPORT bool isDataAvailable(ExceptionSink* xsink, int timeout = 0);
319 DLLEXPORT bool isWriteFinished(ExceptionSink* xsink, int timeout = 0);
320 DLLEXPORT bool isOpen() const;
321 // c must be already referenced before this call
322 DLLEXPORT void setCertificate(QoreSSLCertificate* c);
323 // p must be already referenced before this call
324 DLLEXPORT void setPrivateKey(QoreSSLPrivateKey* p);
325 // c and p must be already referenced before this call
326 DLLEXPORT void setCertificateAndPrivateKey(QoreSSLCertificate* c, QoreSSLPrivateKey* p);
327 DLLEXPORT int setNoDelay(int nodelay);
328 DLLEXPORT int getNoDelay();
329 DLLEXPORT void setEventQueue(ExceptionSink* xsink, Queue* q, QoreValue arg, bool with_data);
330 DLLEXPORT void setEventQueue(Queue* cbq, ExceptionSink* xsink);
331 DLLEXPORT QoreHashNode* getPeerInfo(ExceptionSink* xsink, bool host_lookup = true) const;
332 DLLEXPORT QoreHashNode* getSocketInfo(ExceptionSink* xsink, bool host_lookup = true) const;
333
334 DLLEXPORT void upgradeClientToSSL(ExceptionSink* xsink, int timeout_ms = -1);
335 DLLEXPORT void upgradeServerToSSL(ExceptionSink* xsink, int timeout_ms = -1);
336
337 DLLEXPORT void clearWarningQueue(ExceptionSink* xsink);
338 DLLEXPORT void setWarningQueue(ExceptionSink* xsink, int64 warning_ms, int64 warning_bs, Queue* wq, QoreValue arg,
339 int64 min_ms = 1000);
340 DLLEXPORT QoreHashNode* getUsageInfo() const;
341 DLLEXPORT void clearStats();
342 DLLEXPORT bool pendingHttpChunkedBody() const;
343 DLLEXPORT void setSslVerifyMode(int mode);
344 DLLEXPORT int getSslVerifyMode() const;
345 DLLEXPORT void acceptAllCertificates(bool accept_all = true);
346 DLLEXPORT bool getAcceptAllCertificates() const;
347 DLLEXPORT bool captureRemoteCertificates(bool set);
348 DLLEXPORT QoreObject* getRemoteCertificate() const;
349 DLLEXPORT int64 getConnectionId() const;
350
352 DLLEXPORT int setNonBlock(ExceptionSink* xsink);
353
355 DLLEXPORT void clearNonBlock();
356
357private:
358 DLLLOCAL QoreSocketObject(QoreSocket* s, QoreSSLCertificate* cert = nullptr, QoreSSLPrivateKey* pk = nullptr);
359
360protected:
361 my_socket_priv* priv;
362
363 DLLLOCAL virtual ~QoreSocketObject();
364};
365
366#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:51
the implementation of Qore's object data type, reference counted, dynamically-allocated only
Definition QoreObject.h:61
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:129
Qore's string value type, reference counted, dynamically-allocated only.
Definition QoreStringNode.h:50
base class for resolved call references
Definition CallReferenceNode.h:115
a helper class for getting socket origination information
Definition QoreSocket.h:76
intptr_t qore_offset_t
used for offsets that could be negative
Definition common.h:82
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:266
The main value class in Qore, designed to be passed by value.
Definition QoreValue.h:279