34#ifndef _QORE_QORE_SOCKET_OBJECT_H
36#define _QORE_QORE_SOCKET_OBJECT_H
38#include <qore/QoreSocket.h>
39#include <qore/AbstractPollableIoObjectBase.h>
40#include <qore/QoreThreadLock.h>
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;
65 DLLEXPORT QoreSocketObject();
67 DLLEXPORT QoreSocketObject(QoreSocketObject& orig,
int descriptor);
69 DLLEXPORT
virtual void deref(ExceptionSink* xsink);
70 DLLEXPORT
virtual void deref();
77 DLLEXPORT
void invalidate(ExceptionSink* xsink);
87 DLLEXPORT AbstractPollState* startConnect(ExceptionSink* xsink,
const char* name);
96 DLLEXPORT AbstractPollState* startSslConnect(ExceptionSink* xsink);
108 DLLEXPORT AbstractPollState* startSend(ExceptionSink* xsink,
const char* data,
size_t size);
119 DLLEXPORT AbstractPollState* startAccept(ExceptionSink* xsink);
128 DLLEXPORT AbstractPollState* startSslAccept(ExceptionSink* xsink);
139 DLLEXPORT AbstractPollState* startRecv(ExceptionSink* xsink,
size_t size);
152 DLLEXPORT AbstractPollState* startRecvUntilBytes(ExceptionSink* xsink,
const char* pattern,
size_t size);
162 DLLEXPORT AbstractPollState* startRecvPacket(ExceptionSink* xsink);
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);
175 DLLEXPORT
int bind(
const char* name,
bool reuseaddr =
false);
177 DLLEXPORT
int bind(
int port,
bool reuseaddr =
false);
179 DLLEXPORT
int bind(
const char* iface,
int port,
bool reuseaddr =
false);
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);
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);
192 DLLEXPORT
int listen(
int backlog);
194 DLLEXPORT
int send(
const char* buf,
int size);
195 DLLEXPORT
int send(
const char* buf,
int size,
int timeout_ms, ExceptionSink* xsink);
197 DLLEXPORT
int send(
const QoreStringNode& msg,
int timeout_ms, ExceptionSink* xsink);
199 DLLEXPORT
int send(
const BinaryNode* b);
200 DLLEXPORT
int send(
const BinaryNode* b,
int timeout_ms, ExceptionSink* xsink);
202 DLLEXPORT
void sendFromInputStream(InputStream* is,
int64 size,
int64 timeout_ms, ExceptionSink *xsink);
205 DLLEXPORT
int send(
int fd,
int size = -1);
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);
215 DLLEXPORT QoreStringNode* recv(
int timeout, ExceptionSink* xsink);
217 DLLEXPORT QoreStringNode* recv(
qore_offset_t bufsize,
int timeout_ms, ExceptionSink* xsink);
219 DLLEXPORT BinaryNode* recvBinary(
int bufsize,
int timeout, ExceptionSink* xsink);
221 DLLEXPORT BinaryNode* recvBinary(
int timeout, ExceptionSink* xsink);
223 DLLEXPORT
void recvToOutputStream(OutputStream* os,
int64 size,
int64 timeout_ms, ExceptionSink *xsink);
226 DLLEXPORT
int recv(
int fd,
int size,
int timeout);
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);
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);
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,
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,
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);
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);
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);
274 DLLEXPORT AbstractQoreNode* readHTTPHeader(ExceptionSink* xsink, QoreHashNode* info,
int timeout);
276 DLLEXPORT QoreHashNode* readHTTPChunkedBodyBinary(
int timeout, ExceptionSink* xsink);
278 DLLEXPORT QoreHashNode* readHTTPChunkedBodyToOutputStream(OutputStream* os,
int timeout_ms, ExceptionSink* xsink);
280 DLLEXPORT QoreHashNode* readHTTPChunkedBody(
int timeout, ExceptionSink* xsink);
283 DLLEXPORT QoreHashNode* readHttpChunk(
int timeout, ExceptionSink* xsink);
286 DLLEXPORT
void readHTTPChunkedBodyBinaryWithCallback(
const ResolvedCallReferenceNode& recv_callback,
287 QoreObject* obj,
int timeout_ms, ExceptionSink* xsink);
289 DLLEXPORT
void readHTTPChunkedBodyWithCallback(
const ResolvedCallReferenceNode& recv_callback, QoreObject* obj,
290 int timeout_ms, ExceptionSink* xsink);
292 DLLEXPORT QoreStringNode* readHTTPHeaderString(ExceptionSink* xsink,
int timeout_ms);
294 DLLEXPORT QoreHashNode* readServerSentEvent(ExceptionSink* xsink,
const QoreStringNode* content_encoding,
302 DLLEXPORT
int getPollableDescriptor()
const;
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;
322 DLLEXPORT
void setCertificate(QoreSSLCertificate* c);
324 DLLEXPORT
void setPrivateKey(QoreSSLPrivateKey* p);
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;
334 DLLEXPORT
void upgradeClientToSSL(ExceptionSink* xsink,
int timeout_ms = -1);
335 DLLEXPORT
void upgradeServerToSSL(ExceptionSink* xsink,
int timeout_ms = -1);
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;
352 DLLEXPORT
int setNonBlock(ExceptionSink* xsink);
355 DLLEXPORT
void clearNonBlock();
358 DLLLOCAL QoreSocketObject(QoreSocket* s, QoreSSLCertificate* cert =
nullptr, QoreSSLPrivateKey* pk =
nullptr);
361 my_socket_priv* priv;
363 DLLLOCAL
virtual ~QoreSocketObject();
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
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