Qore Programming Language - C/C++ Library  0.8.12.3
QoreSocketObject.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreSocketObject.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2003 - 2015 David Nichols
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 from a file descriptor
100  DLLEXPORT int send(int fd, int size = -1);
101  // send bytes and convert to network order
102  DLLEXPORT int sendi1(char b, int timeout_ms, ExceptionSink* xsink);
103  DLLEXPORT int sendi2(short b, int timeout_ms, ExceptionSink* xsink);
104  DLLEXPORT int sendi4(int b, int timeout_ms, ExceptionSink* xsink);
105  DLLEXPORT int sendi8(int64 b, int timeout_ms, ExceptionSink* xsink);
106  DLLEXPORT int sendi2LSB(short b, int timeout_ms, ExceptionSink* xsink);
107  DLLEXPORT int sendi4LSB(int b, int timeout_ms, ExceptionSink* xsink);
108  DLLEXPORT int sendi8LSB(int64 b, int timeout_ms, ExceptionSink* xsink);
109  // receive a message
110  DLLEXPORT QoreStringNode* recv(int timeout, ExceptionSink* xsink);
111  // receive a certain number of bytes as a string
112  DLLEXPORT QoreStringNode* recv(qore_offset_t bufsize, int timeout_ms, ExceptionSink* xsink);
113  // receive a certain number of bytes as a binary object
114  DLLEXPORT BinaryNode* recvBinary(int bufsize, int timeout, ExceptionSink* xsink);
115  // receive a packet of bytes as a binary object
116  DLLEXPORT BinaryNode* recvBinary(int timeout, ExceptionSink* xsink);
117  // receive and write data to a file descriptor
118  DLLEXPORT int recv(int fd, int size, int timeout);
119  // receive integers and convert from network byte order
120  DLLEXPORT int64 recvi1(int timeout, char* b, ExceptionSink* xsink);
121  DLLEXPORT int64 recvi2(int timeout, short *b, ExceptionSink* xsink);
122  DLLEXPORT int64 recvi4(int timeout, int *b, ExceptionSink* xsink);
123  DLLEXPORT int64 recvi8(int timeout, int64 *b, ExceptionSink* xsink);
124  DLLEXPORT int64 recvi2LSB(int timeout, short *b, ExceptionSink* xsink);
125  DLLEXPORT int64 recvi4LSB(int timeout, int *b, ExceptionSink* xsink);
126  DLLEXPORT int64 recvi8LSB(int timeout, int64 *b, ExceptionSink* xsink);
127  DLLEXPORT int64 recvu1(int timeout, unsigned char* b, ExceptionSink* xsink);
128  DLLEXPORT int64 recvu2(int timeout, unsigned short *b, ExceptionSink* xsink);
129  DLLEXPORT int64 recvu4(int timeout, unsigned int *b, ExceptionSink* xsink);
130  DLLEXPORT int64 recvu2LSB(int timeout, unsigned short *b, ExceptionSink* xsink);
131  DLLEXPORT int64 recvu4LSB(int timeout, unsigned int *b, ExceptionSink* xsink);
132  // send HTTP message
133  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);
134  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);
135  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);
136 
137  // send HTTP response
138  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);
139  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);
140  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);
141 
142  // read and parse HTTP header
143  DLLEXPORT AbstractQoreNode* readHTTPHeader(ExceptionSink* xsink, QoreHashNode* info, int timeout);
144  // receive a binary message in HTTP chunked format
145  DLLEXPORT QoreHashNode* readHTTPChunkedBodyBinary(int timeout, ExceptionSink* xsink);
146  // receive a string message in HTTP chunked format
147  DLLEXPORT QoreHashNode* readHTTPChunkedBody(int timeout, ExceptionSink* xsink);
148 
149  // receive a binary message in HTTP chunked format
150  DLLEXPORT void readHTTPChunkedBodyBinaryWithCallback(const ResolvedCallReferenceNode& recv_callback, QoreObject* obj, int timeout_ms, ExceptionSink* xsink);
151  // receive a string message in HTTP chunked format
152  DLLEXPORT void readHTTPChunkedBodyWithCallback(const ResolvedCallReferenceNode& recv_callback, QoreObject* obj, int timeout_ms, ExceptionSink* xsink);
153 
154  DLLEXPORT QoreStringNode* readHTTPHeaderString(ExceptionSink* xsink, int timeout_ms);
155 
156  DLLEXPORT int setSendTimeout(int ms);
157  DLLEXPORT int setRecvTimeout(int ms);
158  DLLEXPORT int getSendTimeout();
159  DLLEXPORT int getRecvTimeout();
160  DLLEXPORT int close();
161  DLLEXPORT int shutdown();
162  DLLEXPORT int shutdownSSL(ExceptionSink* xsink) ;
163  DLLEXPORT const char* getSSLCipherName();
164  DLLEXPORT const char* getSSLCipherVersion();
165  DLLEXPORT bool isSecure();
166  DLLEXPORT long verifyPeerCertificate();
167  DLLEXPORT int getSocket();
168  DLLEXPORT void setEncoding(const QoreEncoding *id);
169  DLLEXPORT const QoreEncoding *getEncoding() const;
170  DLLEXPORT bool isDataAvailable(ExceptionSink* xsink, int timeout = 0);
171  DLLEXPORT bool isWriteFinished(ExceptionSink* xsink, int timeout = 0);
172  DLLEXPORT bool isOpen() const;
173  // c must be already referenced before this call
174  DLLEXPORT void setCertificate(QoreSSLCertificate* c);
175  // p must be already referenced before this call
176  DLLEXPORT void setPrivateKey(QoreSSLPrivateKey* p);
177  DLLEXPORT int setNoDelay(int nodelay);
178  DLLEXPORT int getNoDelay();
179  DLLEXPORT void setEventQueue(Queue *cbq, ExceptionSink* xsink);
180  DLLEXPORT QoreHashNode* getPeerInfo(ExceptionSink* xsink, bool host_lookup = true) const;
181  DLLEXPORT QoreHashNode* getSocketInfo(ExceptionSink* xsink, bool host_lookup = true) const;
182 
183  DLLEXPORT void upgradeClientToSSL(ExceptionSink* xsink);
184  DLLEXPORT void upgradeServerToSSL(ExceptionSink* xsink);
185 
186  DLLEXPORT void upgradeClientToSSL(int timeout_ms, ExceptionSink* xsink);
187  DLLEXPORT void upgradeServerToSSL(int timeout_ms, ExceptionSink* xsink);
188 
189  DLLEXPORT void clearWarningQueue(ExceptionSink* xsink);
190  DLLEXPORT void setWarningQueue(ExceptionSink* xsink, int64 warning_ms, int64 warning_bs, class Queue* wq, AbstractQoreNode* arg, int64 min_ms = 1000);
191  DLLEXPORT QoreHashNode* getUsageInfo() const;
192  DLLEXPORT void clearStats();
193  DLLEXPORT bool pendingHttpChunkedBody() const;
194 };
195 
196 #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:71
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:49
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:55
provides access to a private key data structure for SSL connections
Definition: QoreSSLPrivateKey.h:40
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:82
Qore'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:124
the implementation of Qore's object data type, reference counted, dynamically-allocated only ...
Definition: QoreObject.h:64
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:43
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:228
intptr_t qore_offset_t
used for offsets that could be negative
Definition: common.h:74
base class for resolved call references
Definition: CallReferenceNode.h:130
holds arbitrary binary data
Definition: BinaryNode.h:41