00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef _QORE_QORESOCKET_H
00028
00029 #define _QORE_QORESOCKET_H
00030
00031 #include <qore/Qore.h>
00032
00033 #include <sys/types.h>
00034 #include <sys/socket.h>
00035 #include <sys/un.h>
00036 #include <netinet/in.h>
00037 #include <netdb.h>
00038 #include <unistd.h>
00039 #include <strings.h>
00040 #include <string.h>
00041 #include <stdlib.h>
00042 #include <ctype.h>
00043 #include <unistd.h>
00044 #include <arpa/inet.h>
00045
00046 #include <openssl/ssl.h>
00047 #include <openssl/err.h>
00048
00049 #define QSE_MISC_ERR 0
00050 #define QSE_RECV_ERR -1
00051 #define QSE_NOT_OPEN -2
00052 #define QSE_TIMEOUT -3
00053
00054 class Queue;
00055
00057
00062 class SocketSource {
00063 private:
00064 struct qore_socketsource_private *priv;
00065
00067 DLLLOCAL SocketSource(const SocketSource&);
00068
00070 DLLLOCAL SocketSource& operator=(const SocketSource&);
00071
00072 public:
00074 DLLEXPORT SocketSource();
00075
00077 DLLEXPORT ~SocketSource();
00078
00080
00082 DLLEXPORT QoreStringNode *takeAddress();
00083
00085
00087 DLLEXPORT QoreStringNode *takeHostName();
00088
00090
00092 DLLEXPORT const char *getAddress() const;
00093
00095
00097 DLLEXPORT const char *getHostName() const;
00098
00099 DLLLOCAL void setAddress(QoreStringNode *addr);
00100 DLLLOCAL void setAddress(const char *addr);
00101 DLLLOCAL void setHostName(const char *host);
00102 DLLLOCAL void setHostName(QoreStringNode *host);
00103 DLLLOCAL void setAll(QoreObject *o, ExceptionSink *xsink);
00104 };
00105
00107
00117 class QoreSocket {
00118 private:
00120 struct qore_socket_private *priv;
00121
00123 DLLLOCAL QoreSocket(int s, int t, const QoreEncoding *csid);
00124
00126 DLLLOCAL int openINET();
00127
00129 DLLLOCAL int openUNIX();
00130
00131 DLLLOCAL void reuse(int opt);
00132 DLLLOCAL int recv(char *buf, qore_size_t bs, int flags, int timeout, bool do_event = true);
00133
00135 DLLLOCAL QoreStringNode *readHTTPData(int timeout, int *rc, int state = -1);
00136
00137 DLLLOCAL static void convertHeaderToHash(QoreHashNode *h, char *p);
00138
00140 DLLLOCAL QoreSocket(const QoreSocket&);
00141
00143 DLLLOCAL QoreSocket& operator=(const QoreSocket&);
00144
00145 public:
00147 DLLEXPORT QoreSocket();
00148
00150 DLLEXPORT ~QoreSocket();
00151
00153
00164 DLLEXPORT int connect(const char *name, ExceptionSink *xsink = 0);
00165
00167
00179 DLLEXPORT int connect(const char *name, int timeout_ms, ExceptionSink *xsink = 0);
00180
00182
00192 DLLEXPORT int connectINET(const char *host, int prt, ExceptionSink *xsink = 0);
00193
00195
00206 DLLEXPORT int connectINET(const char *host, int prt, int timeout_ms, ExceptionSink *xsink = 0);
00207
00209
00218 DLLEXPORT int connectUNIX(const char *p, ExceptionSink *xsink = 0);
00219
00221
00236 DLLEXPORT int connectSSL(const char *name, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
00237
00239
00255 DLLEXPORT int connectSSL(const char *name, int timeout_ms, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
00256
00258
00272 DLLEXPORT int connectINETSSL(const char *host, int prt, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
00273
00275
00290 DLLEXPORT int connectINETSSL(const char *host, int prt, int timeout_ms, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
00291
00293
00306 DLLEXPORT int connectUNIXSSL(const char *p, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
00307
00309
00317 DLLEXPORT int bind(const char *name, bool reuseaddr = false);
00318
00320
00325 DLLEXPORT int bind(int prt, bool reuseaddr);
00326
00328
00334 DLLEXPORT int bind(const char *interface, int prt, bool reuseaddr = false);
00335
00337
00342 DLLEXPORT int bind(const struct sockaddr *addr, int addr_size);
00343
00345 DLLEXPORT int getPort();
00346
00348
00356 DLLEXPORT QoreSocket *accept(SocketSource *source, ExceptionSink *xsink);
00357
00359
00369 DLLEXPORT QoreSocket *acceptSSL(SocketSource *source, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
00370
00372
00380 DLLEXPORT int acceptAndReplace(SocketSource *source);
00381
00383
00386 DLLEXPORT int listen();
00387
00389
00394 DLLEXPORT int send(const char *buf, qore_size_t size);
00395
00397
00402 DLLEXPORT int send(const QoreString *msg, ExceptionSink *xsink);
00403
00405
00409 DLLEXPORT int send(const BinaryNode *msg);
00410
00412
00417 DLLEXPORT int send(int fd, qore_offset_t size = -1);
00418
00420
00424 DLLEXPORT int sendi1(char i);
00425
00427
00432 DLLEXPORT int sendi2(short i);
00433
00435
00440 DLLEXPORT int sendi4(int i);
00441
00443
00448 DLLEXPORT int sendi8(int64 i);
00449
00451
00457 DLLEXPORT int sendi2LSB(short i);
00458
00460
00466 DLLEXPORT int sendi4LSB(int i);
00467
00469
00475 DLLEXPORT int sendi8LSB(int64 i);
00476
00478
00484 DLLEXPORT int recvi1(int timeout, char *val);
00485
00487
00495 DLLEXPORT int recvi2(int timeout, short *val);
00496
00498
00506 DLLEXPORT int recvi4(int timeout, int *val);
00507
00509
00517 DLLEXPORT int recvi8(int timeout, int64 *val);
00518
00520
00529 DLLEXPORT int recvi2LSB(int timeout, short *val);
00530
00532
00541 DLLEXPORT int recvi4LSB(int timeout, int *val);
00542
00544
00551 DLLEXPORT int recvi8LSB(int timeout, int64 *val);
00552
00554
00560 DLLEXPORT int recvu1(int timeout, unsigned char *val);
00561
00563
00571 DLLEXPORT int recvu2(int timeout, unsigned short *val);
00572
00574
00582 DLLEXPORT int recvu4(int timeout, unsigned int *val);
00583
00585
00594 DLLEXPORT int recvu2LSB(int timeout, unsigned short *val);
00595
00597
00606 DLLEXPORT int recvu4LSB(int timeout, unsigned int *val);
00607
00609
00616 DLLEXPORT QoreStringNode *recv(qore_offset_t bufsize, int timeout, int *prc);
00617
00619
00625 DLLEXPORT BinaryNode *recvBinary(qore_offset_t bufsize, int timeout, int *prc);
00626
00628
00636 DLLEXPORT QoreStringNode *recv(int timeout, int *prc);
00637
00639
00647 DLLEXPORT BinaryNode *recvBinary(int timeout, int *prc);
00648
00650
00657 DLLEXPORT int recv(int fd, qore_offset_t size, int timeout);
00658
00660
00670 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);
00671
00673
00683 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);
00684
00686
00693 DLLEXPORT AbstractQoreNode *readHTTPHeader(int timeout, int *prc, int source = QORE_SOURCE_SOCKET);
00694
00696
00705 DLLEXPORT QoreHashNode *readHTTPChunkedBodyBinary(int timeout, ExceptionSink *xsink, int source = QORE_SOURCE_SOCKET);
00706
00708
00717 DLLEXPORT QoreHashNode *readHTTPChunkedBody(int timeout, ExceptionSink *xsink, int source = QORE_SOURCE_SOCKET);
00718
00720 DLLEXPORT int setSendTimeout(int ms);
00721
00723 DLLEXPORT int setRecvTimeout(int ms);
00724
00726 DLLEXPORT int getSendTimeout() const;
00727
00729 DLLEXPORT int getRecvTimeout() const;
00730
00732
00737 DLLEXPORT bool isDataAvailable(int timeout = 0) const;
00738
00740
00744 DLLEXPORT int close();
00745
00747
00753 DLLEXPORT int shutdown();
00754
00756
00760 DLLEXPORT int shutdownSSL(ExceptionSink *xsink);
00761
00763
00765 DLLEXPORT int getSocket() const;
00766
00768
00770 DLLEXPORT const QoreEncoding *getEncoding() const;
00771
00773
00775 DLLEXPORT void setEncoding(const QoreEncoding *id);
00776
00778
00780 DLLEXPORT bool isOpen() const;
00781
00783
00785 DLLEXPORT const char *getSSLCipherName() const;
00786
00788
00790 DLLEXPORT const char *getSSLCipherVersion() const;
00791
00793
00795 DLLEXPORT bool isSecure() const;
00796
00798 DLLEXPORT long verifyPeerCertificate() const;
00799
00801
00807 DLLEXPORT int upgradeClientToSSL(X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
00808
00810
00816 DLLEXPORT int upgradeServerToSSL(X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
00817
00819
00824 DLLEXPORT bool isWriteFinished(int timeout = 0) const;
00825
00826 DLLLOCAL static void doException(int rc, const char *meth, ExceptionSink *xsink);
00827
00829 DLLLOCAL void setEventQueue(Queue *cbq, ExceptionSink *xsink);
00830
00832 DLLLOCAL Queue *getQueue();
00833
00835 DLLLOCAL int64 getObjectIDForEvents() const;
00836
00838 DLLLOCAL void cleanup(ExceptionSink *xsink);
00839
00840 DLLLOCAL int setNoDelay(int nodelay);
00841 DLLLOCAL int getNoDelay() const;
00842 };
00843
00844 #endif // _QORE_QORESOCKET_H