Qore Programming Language  0.9.16
QoreSocketObject.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreSocketObject.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2003 - 2018 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/AbstractPrivateData.h>
40 #include <qore/QoreThreadLock.h>
41 
42 class QoreSSLCertificate;
43 class QoreSSLPrivateKey;
44 class Queue;
45 class my_socket_priv;
46 
47 class QoreSocketObject : public AbstractPrivateData {
48 private:
49  friend class my_socket_priv;
50  friend hashdecl qore_httpclient_priv;
51 
52  DLLLOCAL QoreSocketObject(QoreSocket* s, QoreSSLCertificate* cert = 0, QoreSSLPrivateKey* pk = 0);
53 
54 protected:
55  my_socket_priv* priv;
56 
57  DLLLOCAL virtual ~QoreSocketObject();
58 
59 public:
60  DLLEXPORT QoreSocketObject();
61 
62  DLLEXPORT virtual void deref(ExceptionSink* xsink);
63  DLLEXPORT virtual void deref();
64 
65  DLLEXPORT int connect(const char* name, int timeout_ms, ExceptionSink* xsink = NULL);
66  DLLEXPORT int connectINET(const char* host, int port, int timeout_ms, ExceptionSink* xsink = NULL);
67  DLLEXPORT int connectINET2(const char* host, const char* service, int family, int sock_type, int protocol, int timeout_ms = -1, ExceptionSink* xsink = NULL);
68  DLLEXPORT int connectUNIX(const char* p, int socktype, int protocol, ExceptionSink* xsink = NULL);
69  DLLEXPORT int connectSSL(const char* name, int timeout_ms, ExceptionSink* xsink);
70  DLLEXPORT int connectINETSSL(const char* host, int port, int timeout_ms, ExceptionSink* xsink);
71  DLLEXPORT int connectINET2SSL(const char* host, const char* service, int family, int sock_type, int protocol, int timeout_ms = -1, ExceptionSink* xsink = NULL);
72  DLLEXPORT int connectUNIXSSL(const char* p, int socktype, int protocol, ExceptionSink* xsink);
73  // to bind to either a UNIX socket or an INET interface:port
74  DLLEXPORT int bind(const char* name, bool reuseaddr = false);
75  // to bind to an INET tcp port on all interfaces
76  DLLEXPORT int bind(int port, bool reuseaddr = false);
77  // to bind an open socket to an INET tcp port on a specific interface
78  DLLEXPORT int bind(const char* iface, int port, bool reuseaddr = false);
79 
80  DLLEXPORT int bindUNIX(const char* name, int socktype, int protocol, ExceptionSink* xsink);
81  DLLEXPORT int bindINET(const char* name, const char* service, bool reuseaddr, int family, int socktype, int protocol, ExceptionSink* xsink);
82 
83  // get port number for INET sockets
84  DLLEXPORT int getPort();
85  DLLEXPORT QoreSocketObject *accept(SocketSource *source, ExceptionSink* xsink);
86  DLLEXPORT QoreSocketObject *acceptSSL(SocketSource *source, ExceptionSink* xsink);
87  DLLEXPORT QoreSocketObject *accept(int timeout_ms, ExceptionSink* xsink);
88  DLLEXPORT QoreSocketObject *acceptSSL(int timeout_ms, ExceptionSink* xsink);
89 
90  DLLEXPORT int listen(int backlog);
91  // send a buffer of a particular size
92  DLLEXPORT int send(const char* buf, int size);
93  DLLEXPORT int send(const char* buf, int size, int timeout_ms, ExceptionSink* xsink);
94  // send a null-terminated string
95  DLLEXPORT int send(const QoreStringNode& msg, int timeout_ms, ExceptionSink* xsink);
96  // send a binary object
97  DLLEXPORT int send(const BinaryNode* b);
98  DLLEXPORT int send(const BinaryNode* b, int timeout_ms, ExceptionSink* xsink);
99  // send a certain number of bytes (read from an InputStream)
100  DLLEXPORT void sendFromInputStream(InputStream *is, int64 size, int64 timeout_ms, ExceptionSink *xsink);
101 
102  // send from a file descriptor
103  DLLEXPORT int send(int fd, int size = -1);
104  // send bytes and convert to network order
105  DLLEXPORT int sendi1(char b, int timeout_ms, ExceptionSink* xsink);
106  DLLEXPORT int sendi2(short b, int timeout_ms, ExceptionSink* xsink);
107  DLLEXPORT int sendi4(int b, int timeout_ms, ExceptionSink* xsink);
108  DLLEXPORT int sendi8(int64 b, int timeout_ms, ExceptionSink* xsink);
109  DLLEXPORT int sendi2LSB(short b, int timeout_ms, ExceptionSink* xsink);
110  DLLEXPORT int sendi4LSB(int b, int timeout_ms, ExceptionSink* xsink);
111  DLLEXPORT int sendi8LSB(int64 b, int timeout_ms, ExceptionSink* xsink);
112  // receive a message
113  DLLEXPORT QoreStringNode* recv(int timeout, ExceptionSink* xsink);
114  // receive a certain number of bytes as a string
115  DLLEXPORT QoreStringNode* recv(qore_offset_t bufsize, int timeout_ms, ExceptionSink* xsink);
116  // receive a certain number of bytes as a binary object
117  DLLEXPORT BinaryNode* recvBinary(int bufsize, int timeout, ExceptionSink* xsink);
118  // receive a packet of bytes as a binary object
119  DLLEXPORT BinaryNode* recvBinary(int timeout, ExceptionSink* xsink);
120  // receive a certain number of bytes and write them to an OutputStream
121  DLLEXPORT void recvToOutputStream(OutputStream *os, int64 size, int64 timeout_ms, ExceptionSink *xsink);
122 
123  // receive and write data to a file descriptor
124  DLLEXPORT int recv(int fd, int size, int timeout);
125  // receive integers and convert from network byte order
126  DLLEXPORT int64 recvi1(int timeout, char* b, ExceptionSink* xsink);
127  DLLEXPORT int64 recvi2(int timeout, short *b, ExceptionSink* xsink);
128  DLLEXPORT int64 recvi4(int timeout, int *b, ExceptionSink* xsink);
129  DLLEXPORT int64 recvi8(int timeout, int64 *b, ExceptionSink* xsink);
130  DLLEXPORT int64 recvi2LSB(int timeout, short *b, ExceptionSink* xsink);
131  DLLEXPORT int64 recvi4LSB(int timeout, int *b, ExceptionSink* xsink);
132  DLLEXPORT int64 recvi8LSB(int timeout, int64 *b, ExceptionSink* xsink);
133  DLLEXPORT int64 recvu1(int timeout, unsigned char* b, ExceptionSink* xsink);
134  DLLEXPORT int64 recvu2(int timeout, unsigned short *b, ExceptionSink* xsink);
135  DLLEXPORT int64 recvu4(int timeout, unsigned int *b, ExceptionSink* xsink);
136  DLLEXPORT int64 recvu2LSB(int timeout, unsigned short *b, ExceptionSink* xsink);
137  DLLEXPORT int64 recvu4LSB(int timeout, unsigned int *b, ExceptionSink* xsink);
138 
139  // send HTTP message
140  DLLEXPORT int sendHTTPMessage(ExceptionSink* xsink, QoreHashNode* info, const char* method, const char* path,
141  const char* http_version, const QoreHashNode* headers, const void* ptr, int size, int source, int timeout_ms);
142  DLLEXPORT int sendHTTPMessage(ExceptionSink* xsink, QoreHashNode* info, const char* method, const char* path,
143  const char* http_version, const QoreHashNode* headers, const QoreStringNode& body, int source, int timeout_ms);
144  DLLEXPORT int sendHTTPMessageWithCallback(ExceptionSink* xsink, QoreHashNode* info, const char* method,
145  const char *path, const char *http_version, const QoreHashNode *headers,
146  const ResolvedCallReferenceNode& send_callback, int source, int timeout_ms, bool* aborted = nullptr);
147 
148  // send HTTP response
149  DLLEXPORT int sendHTTPResponse(ExceptionSink* xsink, QoreHashNode* info, int code, const char* desc,
150  const char* http_version, const QoreHashNode* headers, const void* data, size_t size, int source,
151  int timeout_ms);
152  DLLEXPORT int sendHTTPResponse(ExceptionSink* xsink, QoreHashNode* info, int code, const char* desc,
153  const char* http_version, const QoreHashNode* headers, const QoreStringNode& body, int source,
154  int timeout_ms);
155 
156  // send HTTP response from stream
157  DLLEXPORT int sendHTTPResponse(ExceptionSink* xsink, QoreHashNode* info, int code, const char* desc,
158  const char* http_version, const QoreHashNode* headers, InputStream *is, size_t max_chunked_size,
159  const ResolvedCallReferenceNode* trailer_callback, int source, int timeout_ms);
160 
161  // send HTTP response from callback
162  DLLEXPORT int sendHTTPResponseWithCallback(ExceptionSink* xsink, QoreHashNode* info, int code, const char *desc,
163  const char *http_version, const QoreHashNode *headers, const ResolvedCallReferenceNode& send_callback,
164  int source, int timeout_ms, bool* aborted = nullptr);
165 
166  // send data in HTTP chunked format
167  DLLEXPORT void sendHTTPChunkedBodyFromInputStream(InputStream *is, size_t max_chunked_size, const int timeout_ms,
168  const ResolvedCallReferenceNode* trailer_callback, ExceptionSink* xsink);
169  DLLEXPORT void sendHTTPChunkedBodyTrailer(const QoreHashNode *headers, int timeout_ms, ExceptionSink* xsink);
170 
171  // read and parse HTTP header
172  DLLEXPORT AbstractQoreNode* readHTTPHeader(ExceptionSink* xsink, QoreHashNode* info, int timeout);
173  // receive a binary message in HTTP chunked format
174  DLLEXPORT QoreHashNode* readHTTPChunkedBodyBinary(int timeout, ExceptionSink* xsink);
175  // receive a binary message in HTTP chunked format
176  DLLEXPORT QoreHashNode* readHTTPChunkedBodyToOutputStream(OutputStream *os, int timeout_ms, ExceptionSink* xsink);
177  // receive a string message in HTTP chunked format
178  DLLEXPORT QoreHashNode* readHTTPChunkedBody(int timeout, ExceptionSink* xsink);
179 
180  // receive a binary message in HTTP chunked format
181  DLLEXPORT void readHTTPChunkedBodyBinaryWithCallback(const ResolvedCallReferenceNode& recv_callback, QoreObject* obj, int timeout_ms, ExceptionSink* xsink);
182  // receive a string message in HTTP chunked format
183  DLLEXPORT void readHTTPChunkedBodyWithCallback(const ResolvedCallReferenceNode& recv_callback, QoreObject* obj, int timeout_ms, ExceptionSink* xsink);
184 
185  DLLEXPORT QoreStringNode* readHTTPHeaderString(ExceptionSink* xsink, int timeout_ms);
186 
187  DLLEXPORT int setSendTimeout(int ms);
188  DLLEXPORT int setRecvTimeout(int ms);
189  DLLEXPORT int getSendTimeout();
190  DLLEXPORT int getRecvTimeout();
191  DLLEXPORT int close();
192  DLLEXPORT int shutdown();
193  DLLEXPORT int shutdownSSL(ExceptionSink* xsink) ;
194  DLLEXPORT const char* getSSLCipherName();
195  DLLEXPORT const char* getSSLCipherVersion();
196  DLLEXPORT bool isSecure();
197  DLLEXPORT long verifyPeerCertificate();
198  DLLEXPORT int getSocket();
199  DLLEXPORT void setEncoding(const QoreEncoding *id);
200  DLLEXPORT const QoreEncoding *getEncoding() const;
201  DLLEXPORT bool isDataAvailable(ExceptionSink* xsink, int timeout = 0);
202  DLLEXPORT bool isWriteFinished(ExceptionSink* xsink, int timeout = 0);
203  DLLEXPORT bool isOpen() const;
204  // c must be already referenced before this call
205  DLLEXPORT void setCertificate(QoreSSLCertificate* c);
206  // p must be already referenced before this call
207  DLLEXPORT void setPrivateKey(QoreSSLPrivateKey* p);
208  DLLEXPORT int setNoDelay(int nodelay);
209  DLLEXPORT int getNoDelay();
210  DLLEXPORT void setEventQueue(ExceptionSink* xsink, Queue* q, QoreValue arg, bool with_data);
211  DLLEXPORT void setEventQueue(Queue* cbq, ExceptionSink* xsink);
212  DLLEXPORT QoreHashNode* getPeerInfo(ExceptionSink* xsink, bool host_lookup = true) const;
213  DLLEXPORT QoreHashNode* getSocketInfo(ExceptionSink* xsink, bool host_lookup = true) const;
214 
215  DLLEXPORT void upgradeClientToSSL(ExceptionSink* xsink);
216  DLLEXPORT void upgradeServerToSSL(ExceptionSink* xsink);
217 
218  DLLEXPORT void upgradeClientToSSL(int timeout_ms, ExceptionSink* xsink);
219  DLLEXPORT void upgradeServerToSSL(int timeout_ms, ExceptionSink* xsink);
220 
221  DLLEXPORT void clearWarningQueue(ExceptionSink* xsink);
222  DLLEXPORT void setWarningQueue(ExceptionSink* xsink, int64 warning_ms, int64 warning_bs, Queue* wq, QoreValue arg, int64 min_ms = 1000);
223  DLLEXPORT QoreHashNode* getUsageInfo() const;
224  DLLEXPORT void clearStats();
225  DLLEXPORT bool pendingHttpChunkedBody() const;
226  DLLEXPORT void setSslVerifyMode(int mode);
227  DLLEXPORT int getSslVerifyMode() const;
228  DLLEXPORT void acceptAllCertificates(bool accept_all = true);
229  DLLEXPORT bool getAcceptAllCertificates() const;
230  DLLEXPORT bool captureRemoteCertificates(bool set);
231  DLLEXPORT QoreObject* getRemoteCertificate() const;
232  DLLEXPORT int64 getConnectionId() const;
233 };
234 
235 #endif // _QORE_QORE_SOCKET_OBJECT_H
OutputStream
Interface for private data of output streams.
Definition: OutputStream.h:44
qore_offset_t
intptr_t qore_offset_t
used for offsets that could be negative
Definition: common.h:76
QoreValue
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:262
QoreHashNode
This is the hash or associative list container type in Qore, dynamically allocated only,...
Definition: QoreHashNode.h:50
QoreSocket
provides access to sockets using Qore data structures
Definition: QoreSocket.h:126
int64
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
QoreSSLCertificate
represents an X509 certificate, reference-counted, dynamically-allocated only
Definition: QoreSSLCertificate.h:42
SocketSource
a helper class for getting socket origination information
Definition: QoreSocket.h:73
InputStream
Interface for private data of input streams.
Definition: InputStream.h:44
QoreObject
the implementation of Qore's object data type, reference counted, dynamically-allocated only
Definition: QoreObject.h:61
ExceptionSink
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
AbstractPrivateData::deref
virtual DLLLOCAL void deref()
decrements the reference count of the object without the possibility of throwing a Qore-language exce...
Definition: AbstractPrivateData.h:67
QoreSSLPrivateKey
provides access to a private key data structure for SSL connections
Definition: QoreSSLPrivateKey.h:40
ResolvedCallReferenceNode
base class for resolved call references
Definition: CallReferenceNode.h:105
AbstractPrivateData
the base class for all data to be used as private data of Qore objects
Definition: AbstractPrivateData.h:44
QoreEncoding
defines string encoding functions in Qore
Definition: QoreEncoding.h:83
BinaryNode
holds arbitrary binary data
Definition: BinaryNode.h:41
QoreStringNode
Qore's string value type, reference counted, dynamically-allocated only.
Definition: QoreStringNode.h:50
AbstractQoreNode
The base class for all value and parse types in Qore expression trees.
Definition: AbstractQoreNode.h:54