Qore Programming Language  0.8.7
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
QoreSocket.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreSocket.h
4 
5  ipv4, ipv6, unix socket class with SSL support
6 
7  Qore Programming Language
8 
9  Copyright 2003 - 2013 David Nichols
10 
11  will unlink (delete) UNIX domain socket files when closed
12 
13  This library is free software; you can redistribute it and/or
14  modify it under the terms of the GNU Lesser General Public
15  License as published by the Free Software Foundation; either
16  version 2.1 of the License, or (at your option) any later version.
17 
18  This library is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  Lesser General Public License for more details.
22 
23  You should have received a copy of the GNU Lesser General Public
24  License along with this library; if not, write to the Free Software
25  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 */
27 
28 #ifndef _QORE_QORESOCKET_H
29 
30 #define _QORE_QORESOCKET_H
31 
32 #include <qore/Qore.h>
33 
34 #include <sys/types.h>
35 #include <unistd.h>
36 #include <strings.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <ctype.h>
40 #include <unistd.h>
41 
42 #include <openssl/ssl.h>
43 #include <openssl/err.h>
44 
45 #define QSE_MISC_ERR 0
46 #define QSE_RECV_ERR -1
47 #define QSE_NOT_OPEN -2
48 #define QSE_TIMEOUT -3
49 #define QSE_SSL_ERR -4
50 
51 class Queue;
52 
54 
61 class SocketSource {
62  friend struct qore_socket_private;
63 private:
64  struct qore_socketsource_private *priv; // private implementation
65 
67  DLLLOCAL SocketSource(const SocketSource&);
68 
70  DLLLOCAL SocketSource& operator=(const SocketSource&);
71 
72 public:
74  DLLEXPORT SocketSource();
75 
77  DLLEXPORT ~SocketSource();
78 
80 
82  DLLEXPORT QoreStringNode *takeAddress();
83 
85 
87  DLLEXPORT QoreStringNode *takeHostName();
88 
90 
92  DLLEXPORT const char *getAddress() const;
93 
95 
97  DLLEXPORT const char *getHostName() const;
98 
99  DLLLOCAL void setAll(QoreObject *o, ExceptionSink *xsink);
100 };
101 
103 
113 class QoreSocket {
114 private:
116  struct qore_socket_private *priv;
117 
119  DLLLOCAL QoreSocket(int n_sock, int n_sfamily, int n_stype, int s_prot, const QoreEncoding *csid);
120 
121  DLLLOCAL static void convertHeaderToHash(QoreHashNode *h, char *p);
122 
124  DLLLOCAL QoreSocket(const QoreSocket&);
125 
127  DLLLOCAL QoreSocket& operator=(const QoreSocket&);
128 
129 public:
131  DLLEXPORT QoreSocket();
132 
134  DLLEXPORT ~QoreSocket();
135 
137 
148  DLLEXPORT int connect(const char *name, ExceptionSink *xsink = 0);
149 
151 
163  DLLEXPORT int connect(const char *name, int timeout_ms, ExceptionSink *xsink = 0);
164 
166 
176  DLLEXPORT int connectINET(const char *host, int prt, ExceptionSink *xsink = 0);
177 
179 
193  DLLEXPORT int connectINET2(const char *name, const char *service, int family = Q_AF_UNSPEC, int sock_type = Q_SOCK_STREAM, int protocol = 0, int timeout_ms = -1, ExceptionSink *xsink = 0);
194 
196 
207  DLLEXPORT int connectINET(const char *host, int prt, int timeout_ms, ExceptionSink *xsink = 0);
208 
210 
219  DLLEXPORT int connectUNIX(const char *p, ExceptionSink *xsink = 0);
220 
222 
233  DLLEXPORT int connectUNIX(const char *p, int socktype, int protocol = 0, ExceptionSink *xsink = 0);
234 
236 
251  DLLEXPORT int connectSSL(const char *name, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
252 
254 
270  DLLEXPORT int connectSSL(const char *name, int timeout_ms, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
271 
273 
287  DLLEXPORT int connectINETSSL(const char *host, int prt, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
288 
290 
305  DLLEXPORT int connectINETSSL(const char *host, int prt, int timeout_ms, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
306 
308 
324  DLLEXPORT int connectINET2SSL(const char *name, const char *service, int family, int sock_type, int protocol, int timeout_ms, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink = 0);
325 
327 
340  DLLEXPORT int connectUNIXSSL(const char *p, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
341 
343 
358  DLLEXPORT int connectUNIXSSL(const char *p, int socktype, int protocol, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
359 
361 
367  DLLEXPORT int bind(const char *name, bool reuseaddr = false);
368 
370 
375  DLLEXPORT int bind(int prt, bool reuseaddr);
376 
378 
384  DLLEXPORT int bind(const char *iface, int prt, bool reuseaddr = false);
385 
387 
394  DLLEXPORT int bind(const struct sockaddr *addr, int addr_size);
395 
397 
405  DLLEXPORT int bind(int family, const struct sockaddr *addr, int addr_size, int socktype = Q_SOCK_STREAM, int protocol = 0);
406 
408 
416  DLLEXPORT int bindUNIX(const char *name, ExceptionSink *xsink = 0);
417 
419 
429  DLLEXPORT int bindUNIX(const char *name, int socktype, int protocol = 0, ExceptionSink *xsink = 0);
430 
432 
444  DLLEXPORT int bindINET(const char *name, const char *service, bool reuseaddr = true, int family = Q_AF_UNSPEC, int socktype = Q_SOCK_STREAM, int protocol = 0, ExceptionSink *xsink = 0);
445 
447  DLLEXPORT int getPort();
448 
450 
462  DLLEXPORT QoreSocket *accept(SocketSource *source, ExceptionSink *xsink);
463 
465 
480  DLLEXPORT QoreSocket *acceptSSL(SocketSource *source, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
481 
483 
495  DLLEXPORT int acceptAndReplace(SocketSource *source);
496 
498 
509  DLLEXPORT QoreSocket *accept(int timeout_ms, ExceptionSink *xsink);
510 
512 
524  DLLEXPORT QoreSocket *acceptSSL(int timeout_ms, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
525 
527 
538  DLLEXPORT int acceptAndReplace(int timeout_ms, ExceptionSink *xsink);
539 
541 
544  DLLEXPORT int listen();
545 
547 
553  DLLEXPORT int send(const char *buf, qore_size_t size);
554 
556 
563  DLLEXPORT int send(const char *buf, qore_size_t size, ExceptionSink* xsink);
564 
566 
574  DLLEXPORT int send(const char *buf, qore_size_t size, int timeout_ms, ExceptionSink* xsink);
575 
577 
583  DLLEXPORT int send(const QoreString *msg, ExceptionSink *xsink);
584 
586 
593  DLLEXPORT int send(const QoreString *msg, int timeout_ms, ExceptionSink *xsink);
594 
596 
601  DLLEXPORT int send(const BinaryNode *msg);
602 
604 
610  DLLEXPORT int send(const BinaryNode *msg, ExceptionSink* xsink);
611 
613 
620  DLLEXPORT int send(const BinaryNode *msg, int timeout_ms, ExceptionSink* xsink);
621 
623 
629  DLLEXPORT int send(int fd, qore_offset_t size = -1);
630 
632 
636  DLLEXPORT int sendi1(char i);
637 
639 
644  DLLEXPORT int sendi2(short i);
645 
647 
652  DLLEXPORT int sendi4(int i);
653 
655 
660  DLLEXPORT int sendi8(int64 i);
661 
663 
669  DLLEXPORT int sendi2LSB(short i);
670 
672 
678  DLLEXPORT int sendi4LSB(int i);
679 
681 
687  DLLEXPORT int sendi8LSB(int64 i);
688 
690 
698  DLLEXPORT int sendi1(char i, int timeout_ms, ExceptionSink* xsink);
699 
701 
710  DLLEXPORT int sendi2(short i, int timeout_ms, ExceptionSink* xsink);
711 
713 
722  DLLEXPORT int sendi4(int i, int timeout_ms, ExceptionSink* xsink);
723 
725 
734  DLLEXPORT int sendi8(int64 i, int timeout_ms, ExceptionSink* xsink);
735 
737 
747  DLLEXPORT int sendi2LSB(short i, int timeout_ms, ExceptionSink* xsink);
748 
750 
760  DLLEXPORT int sendi4LSB(int i, int timeout_ms, ExceptionSink* xsink);
761 
763 
773  DLLEXPORT int sendi8LSB(int64 i, int timeout_ms, ExceptionSink* xsink);
774 
776 
785  DLLEXPORT int recvi1(int timeout_ms, char *val);
786 
788 
796  DLLEXPORT int recvi2(int timeout_ms, short *val);
797 
799 
807  DLLEXPORT int recvi4(int timeout_ms, int *val);
808 
810 
818  DLLEXPORT int recvi8(int timeout_ms, int64 *val);
819 
821 
830  DLLEXPORT int recvi2LSB(int timeout_ms, short *val);
831 
833 
842  DLLEXPORT int recvi4LSB(int timeout_ms, int *val);
843 
845 
852  DLLEXPORT int recvi8LSB(int timeout_ms, int64 *val);
853 
855 
861  DLLEXPORT int recvu1(int timeout_ms, unsigned char *val);
862 
864 
872  DLLEXPORT int recvu2(int timeout_ms, unsigned short *val);
873 
875 
883  DLLEXPORT int recvu4(int timeout_ms, unsigned int *val);
884 
886 
895  DLLEXPORT int recvu2LSB(int timeout_ms, unsigned short *val);
896 
898 
907  DLLEXPORT int recvu4LSB(int timeout_ms, unsigned int *val);
908 
910 
920  DLLEXPORT int64 recvi1(int timeout_ms, char *val, ExceptionSink* xsink);
921 
923 
934  DLLEXPORT int64 recvi2(int timeout_ms, short *val, ExceptionSink* xsink);
935 
937 
949  DLLEXPORT int64 recvi4(int timeout_ms, int *val, ExceptionSink* xsink);
950 
952 
963  DLLEXPORT int64 recvi8(int timeout_ms, int64 *val, ExceptionSink* xsink);
964 
966 
978  DLLEXPORT int64 recvi2LSB(int timeout_ms, short *val, ExceptionSink* xsink);
979 
981 
993  DLLEXPORT int64 recvi4LSB(int timeout_ms, int *val, ExceptionSink* xsink);
994 
996 
1007  DLLEXPORT int64 recvi8LSB(int timeout_ms, int64 *val, ExceptionSink* xsink);
1008 
1010 
1020  DLLEXPORT int64 recvu1(int timeout_ms, unsigned char *val, ExceptionSink* xsink);
1021 
1023 
1035  DLLEXPORT int64 recvu2(int timeout_ms, unsigned short *val, ExceptionSink* xsink);
1036 
1038 
1050  DLLEXPORT int64 recvu4(int timeout_ms, unsigned int *val, ExceptionSink* xsink);
1051 
1053 
1067  DLLEXPORT int64 recvu2LSB(int timeout_ms, unsigned short *val, ExceptionSink* xsink);
1068 
1070 
1084  DLLEXPORT int64 recvu4LSB(int timeout_ms, unsigned int *val, ExceptionSink* xsink);
1085 
1087 
1094  DLLEXPORT QoreStringNode *recv(qore_offset_t bufsize, int timeout_ms, int *prc);
1095 
1097 
1104  DLLEXPORT QoreStringNode* recv(qore_offset_t bufsize, int timeout_ms, ExceptionSink* xsink);
1105 
1107 
1113  DLLEXPORT BinaryNode *recvBinary(qore_offset_t bufsize, int timeout_ms, int *prc);
1114 
1116 
1122  DLLEXPORT BinaryNode* recvBinary(qore_offset_t bufsize, int timeout_ms, ExceptionSink* xsink);
1123 
1125 
1134  DLLEXPORT QoreStringNode *recv(int timeout_ms, int *prc);
1135 
1137 
1146  DLLEXPORT QoreStringNode* recv(int timeout_ms, ExceptionSink* xsink);
1147 
1149 
1157  DLLEXPORT BinaryNode *recvBinary(int timeout_ms, int *prc);
1158 
1160 
1168  DLLEXPORT BinaryNode* recvBinary(int timeout_ms, ExceptionSink* xsink);
1169 
1171 
1178  DLLEXPORT int recv(int fd, qore_offset_t size, int timeout_ms);
1179 
1181 
1191  DLLEXPORT int sendHTTPMessage(const char *method, const char *path, const char *http_version, const QoreHashNode *headers, const void *data, qore_size_t size, int source = QORE_SOURCE_SOCKET);
1192 
1194 
1205  DLLEXPORT int sendHTTPMessage(QoreHashNode *info, const char *method, const char *path, const char *http_version, const QoreHashNode *headers, const void *data, qore_size_t size, int source = QORE_SOURCE_SOCKET);
1206 
1208 
1222  DLLEXPORT int sendHTTPMessage(ExceptionSink* xsink, QoreHashNode *info, const char *method, const char *path, const char *http_version, const QoreHashNode *headers, const void *data, qore_size_t size, int source = QORE_SOURCE_SOCKET);
1223 
1225 
1240  DLLEXPORT int sendHTTPMessage(ExceptionSink* xsink, QoreHashNode *info, const char *method, const char *path, const char *http_version, const QoreHashNode *headers, const void *data, qore_size_t size, int source, int timeout_ms);
1241 
1243 
1255  DLLEXPORT int sendHTTPResponse(int code, const char *desc, const char *http_version, const QoreHashNode *headers, const void *data, qore_size_t size, int source = QORE_SOURCE_SOCKET);
1256 
1258 
1271  DLLEXPORT int sendHTTPResponse(ExceptionSink* xsink, int code, const char *desc, const char *http_version, const QoreHashNode *headers, const void *data, qore_size_t size, int source = QORE_SOURCE_SOCKET);
1272 
1274 
1288  DLLEXPORT int sendHTTPResponse(ExceptionSink* xsink, int code, const char *desc, const char *http_version, const QoreHashNode *headers, const void *data, qore_size_t size, int source, int timeout_ms);
1289 
1291 
1298  DLLEXPORT AbstractQoreNode *readHTTPHeader(int timeout_ms, int *prc, int source = QORE_SOURCE_SOCKET);
1299 
1301 
1309  DLLEXPORT AbstractQoreNode *readHTTPHeader(QoreHashNode *info, int timeout_ms, int *prc, int source = QORE_SOURCE_SOCKET);
1310 
1312 
1323  DLLEXPORT QoreHashNode *readHTTPHeader(ExceptionSink* xsink, QoreHashNode *info, int timeout_ms, int source = QORE_SOURCE_SOCKET);
1324 
1326 
1335  DLLEXPORT QoreHashNode *readHTTPChunkedBodyBinary(int timeout_ms, ExceptionSink *xsink, int source = QORE_SOURCE_SOCKET);
1336 
1338 
1347  DLLEXPORT QoreHashNode *readHTTPChunkedBody(int timeout_ms, ExceptionSink *xsink, int source = QORE_SOURCE_SOCKET);
1348 
1350  DLLEXPORT int setSendTimeout(int ms);
1351 
1353  DLLEXPORT int setRecvTimeout(int ms);
1354 
1356  DLLEXPORT int getSendTimeout() const;
1357 
1359  DLLEXPORT int getRecvTimeout() const;
1360 
1362 
1367  DLLEXPORT bool isDataAvailable(int timeout_ms = 0) const;
1368 
1370 
1374  DLLEXPORT int close();
1375 
1377 
1383  DLLEXPORT int shutdown();
1384 
1386 
1390  DLLEXPORT int shutdownSSL(ExceptionSink *xsink);
1391 
1393 
1395  DLLEXPORT int getSocket() const;
1396 
1398 
1400  DLLEXPORT const QoreEncoding *getEncoding() const;
1401 
1403 
1405  DLLEXPORT void setEncoding(const QoreEncoding *id);
1406 
1408 
1410  DLLEXPORT bool isOpen() const;
1411 
1413 
1415  DLLEXPORT const char *getSSLCipherName() const;
1416 
1418 
1420  DLLEXPORT const char *getSSLCipherVersion() const;
1421 
1423 
1425  DLLEXPORT bool isSecure() const;
1426 
1428  DLLEXPORT long verifyPeerCertificate() const;
1429 
1431 
1439  DLLEXPORT int upgradeClientToSSL(X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
1440 
1442 
1450  DLLEXPORT int upgradeServerToSSL(X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
1451 
1453 
1458  DLLEXPORT bool isWriteFinished(int timeout_ms = 0) const;
1459 
1461 
1472  DLLEXPORT QoreHashNode* getPeerInfo(ExceptionSink* xsink) const;
1473 
1475 
1486  DLLEXPORT QoreHashNode* getSocketInfo(ExceptionSink* xsink) const;
1487 
1488  DLLLOCAL static void doException(int rc, const char *meth, int timeout_ms, ExceptionSink *xsink);
1489 
1491  DLLLOCAL void setEventQueue(Queue *cbq, ExceptionSink *xsink);
1492 
1494  DLLLOCAL Queue *getQueue();
1495 
1497  DLLLOCAL int64 getObjectIDForEvents() const;
1498 
1500  DLLLOCAL void cleanup(ExceptionSink *xsink);
1501 
1502  DLLLOCAL int setNoDelay(int nodelay);
1503  DLLLOCAL int getNoDelay() const;
1504 
1506  DLLLOCAL void setAccept(QoreObject *o);
1507 };
1508 
1509 #endif // _QORE_QORESOCKET_H