Qore Programming Language - C/C++ Library  0.8.13
QoreSocketObject.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreSocketObject.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2003 - 2017 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 struct 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 QoreString *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  // send HTTP message
139  DLLEXPORT int sendHTTPMessage(ExceptionSink* xsink, QoreHashNode* info, const char* method, const char* path, const char* http_version, const QoreHashNode* headers, const void* ptr, int size, int source, int timeout_ms);
140  DLLEXPORT int sendHTTPMessageWithCallback(ExceptionSink* xsink, QoreHashNode *info, const char *method, const char *path, const char *http_version, const QoreHashNode *headers, const ResolvedCallReferenceNode& send_callback, int source, int timeout_ms);
141  DLLEXPORT int sendHTTPMessageWithCallback(ExceptionSink* xsink, QoreHashNode* info, const char* method, const char *path, const char *http_version, const QoreHashNode *headers, const ResolvedCallReferenceNode& send_callback, int source, int timeout_ms, bool* aborted);
142 
143  // send HTTP response
144  DLLEXPORT int sendHTTPResponse(ExceptionSink* xsink, int code, const char* desc, const char* http_version, const QoreHashNode* headers, const void* ptr, int size, int source, int timeout_ms);
145  DLLEXPORT int sendHTTPResponseWithCallback(ExceptionSink* xsink, int code, const char *desc, const char *http_version, const QoreHashNode *headers, const ResolvedCallReferenceNode& send_callback, int source, int timeout_ms);
146  DLLEXPORT int sendHTTPResponseWithCallback(ExceptionSink* xsink, int code, const char *desc, const char *http_version, const QoreHashNode *headers, const ResolvedCallReferenceNode& send_callback, int source, int timeout_ms, bool* aborted);
147 
148  // send data in HTTP chunked format
149  DLLEXPORT void sendHTTPChunkedBodyFromInputStream(InputStream *is, size_t max_chunked_size, const int timeout_ms, const ResolvedCallReferenceNode* trailer_callback, ExceptionSink* xsink);
150  DLLEXPORT void sendHTTPChunkedBodyTrailer(const QoreHashNode *headers, int timeout_ms, ExceptionSink* xsink);
151 
152  // read and parse HTTP header
153  DLLEXPORT AbstractQoreNode* readHTTPHeader(ExceptionSink* xsink, QoreHashNode* info, int timeout);
154  // receive a binary message in HTTP chunked format
155  DLLEXPORT QoreHashNode* readHTTPChunkedBodyBinary(int timeout, ExceptionSink* xsink);
156  // receive a binary message in HTTP chunked format
157  DLLEXPORT QoreHashNode* readHTTPChunkedBodyToOutputStream(OutputStream *os, int timeout_ms, ExceptionSink* xsink);
158  // receive a string message in HTTP chunked format
159  DLLEXPORT QoreHashNode* readHTTPChunkedBody(int timeout, ExceptionSink* xsink);
160 
161  // receive a binary message in HTTP chunked format
162  DLLEXPORT void readHTTPChunkedBodyBinaryWithCallback(const ResolvedCallReferenceNode& recv_callback, QoreObject* obj, int timeout_ms, ExceptionSink* xsink);
163  // receive a string message in HTTP chunked format
164  DLLEXPORT void readHTTPChunkedBodyWithCallback(const ResolvedCallReferenceNode& recv_callback, QoreObject* obj, int timeout_ms, ExceptionSink* xsink);
165 
166  DLLEXPORT QoreStringNode* readHTTPHeaderString(ExceptionSink* xsink, int timeout_ms);
167 
168  DLLEXPORT int setSendTimeout(int ms);
169  DLLEXPORT int setRecvTimeout(int ms);
170  DLLEXPORT int getSendTimeout();
171  DLLEXPORT int getRecvTimeout();
172  DLLEXPORT int close();
173  DLLEXPORT int shutdown();
174  DLLEXPORT int shutdownSSL(ExceptionSink* xsink) ;
175  DLLEXPORT const char* getSSLCipherName();
176  DLLEXPORT const char* getSSLCipherVersion();
177  DLLEXPORT bool isSecure();
178  DLLEXPORT long verifyPeerCertificate();
179  DLLEXPORT int getSocket();
180  DLLEXPORT void setEncoding(const QoreEncoding *id);
181  DLLEXPORT const QoreEncoding *getEncoding() const;
182  DLLEXPORT bool isDataAvailable(ExceptionSink* xsink, int timeout = 0);
183  DLLEXPORT bool isWriteFinished(ExceptionSink* xsink, int timeout = 0);
184  DLLEXPORT bool isOpen() const;
185  // c must be already referenced before this call
186  DLLEXPORT void setCertificate(QoreSSLCertificate* c);
187  // p must be already referenced before this call
188  DLLEXPORT void setPrivateKey(QoreSSLPrivateKey* p);
189  DLLEXPORT int setNoDelay(int nodelay);
190  DLLEXPORT int getNoDelay();
191  DLLEXPORT void setEventQueue(Queue *cbq, ExceptionSink* xsink);
192  DLLEXPORT QoreHashNode* getPeerInfo(ExceptionSink* xsink, bool host_lookup = true) const;
193  DLLEXPORT QoreHashNode* getSocketInfo(ExceptionSink* xsink, bool host_lookup = true) const;
194 
195  DLLEXPORT void upgradeClientToSSL(ExceptionSink* xsink);
196  DLLEXPORT void upgradeServerToSSL(ExceptionSink* xsink);
197 
198  DLLEXPORT void upgradeClientToSSL(int timeout_ms, ExceptionSink* xsink);
199  DLLEXPORT void upgradeServerToSSL(int timeout_ms, ExceptionSink* xsink);
200 
201  DLLEXPORT void clearWarningQueue(ExceptionSink* xsink);
202  DLLEXPORT void setWarningQueue(ExceptionSink* xsink, int64 warning_ms, int64 warning_bs, class Queue* wq, AbstractQoreNode* arg, int64 min_ms = 1000);
203  DLLEXPORT QoreHashNode* getUsageInfo() const;
204  DLLEXPORT void clearStats();
205  DLLEXPORT bool pendingHttpChunkedBody() const;
206  DLLEXPORT void setSslVerifyMode(int mode);
207  DLLEXPORT int getSslVerifyMode() const;
208  DLLEXPORT void acceptAllCertificates(bool accept_all = true);
209  DLLEXPORT bool getAcceptAllCertificates() const;
210 };
211 
212 #endif // _QORE_QORE_SOCKET_OBJECT_H
defines string encoding functions in Qore
Definition: QoreEncoding.h:85
a helper class for getting socket origination information
Definition: QoreSocket.h:73
represents an X509 certificate, reference-counted, dynamically-allocated only
Definition: QoreSSLCertificate.h:42
This is the hash or associative list container type in Qore, dynamically allocated only...
Definition: QoreHashNode.h:50
the base class for all data to be used as private data of Qore objects
Definition: AbstractPrivateData.h:44
The base class for all value and parse types in Qore expression trees.
Definition: AbstractQoreNode.h:54
provides access to a private key data structure for SSL connections
Definition: QoreSSLPrivateKey.h:40
Interface for private data of output streams.
Definition: OutputStream.h:44
Qore&#39;s string type supported by the QoreEncoding class.
Definition: QoreString.h:82
Qore&#39;s string value type, reference counted, dynamically-allocated only.
Definition: QoreStringNode.h:50
virtual DLLLOCAL void deref()
decrements the reference count of the object without the possibility of throwing a Qore-language exce...
Definition: AbstractPrivateData.h:67
provides access to sockets using Qore data structures
Definition: QoreSocket.h:126
the implementation of Qore&#39;s object data type, reference counted, dynamically-allocated only ...
Definition: QoreObject.h:62
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:47
Interface for private data of input streams.
Definition: InputStream.h:44
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:241
intptr_t qore_offset_t
used for offsets that could be negative
Definition: common.h:77
base class for resolved call references
Definition: CallReferenceNode.h:130
holds arbitrary binary data
Definition: BinaryNode.h:41