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 class Queue;
00050
00052
00057 class SocketSource {
00058 private:
00059 struct qore_socketsource_private *priv;
00060
00062 DLLLOCAL SocketSource(const SocketSource&);
00063
00065 DLLLOCAL SocketSource& operator=(const SocketSource&);
00066
00067 public:
00069 DLLEXPORT SocketSource();
00070
00072 DLLEXPORT ~SocketSource();
00073
00075
00077 DLLEXPORT QoreStringNode *takeAddress();
00078
00080
00082 DLLEXPORT QoreStringNode *takeHostName();
00083
00085
00087 DLLEXPORT const char *getAddress() const;
00088
00090
00092 DLLEXPORT const char *getHostName() const;
00093
00094 DLLLOCAL void setAddress(QoreStringNode *addr);
00095 DLLLOCAL void setAddress(const char *addr);
00096 DLLLOCAL void setHostName(const char *host);
00097 DLLLOCAL void setHostName(QoreStringNode *host);
00098 DLLLOCAL void setAll(QoreObject *o, ExceptionSink *xsink);
00099 };
00100
00102
00112 class QoreSocket {
00113 private:
00115 struct qore_socket_private *priv;
00116
00118 DLLLOCAL QoreSocket(int s, int t, const QoreEncoding *csid);
00119
00121 DLLLOCAL int openINET();
00122
00124 DLLLOCAL int openUNIX();
00125
00126 DLLLOCAL void reuse(int opt);
00127 DLLLOCAL int recv(char *buf, qore_size_t bs, int flags, int timeout, bool do_event = true);
00128
00130 DLLLOCAL QoreStringNode *readHTTPData(int timeout, int *rc, int state = -1);
00131
00132 DLLLOCAL static void convertHeaderToHash(QoreHashNode *h, char *p);
00133
00135 DLLLOCAL QoreSocket(const QoreSocket&);
00136
00138 DLLLOCAL QoreSocket& operator=(const QoreSocket&);
00139
00140 public:
00142 DLLEXPORT QoreSocket();
00143
00145 DLLEXPORT ~QoreSocket();
00146
00148
00159 DLLEXPORT int connect(const char *name, ExceptionSink *xsink = 0);
00160
00162
00174 DLLEXPORT int connect(const char *name, int timeout_ms, ExceptionSink *xsink = 0);
00175
00177
00187 DLLEXPORT int connectINET(const char *host, int prt, ExceptionSink *xsink = 0);
00188
00190
00201 DLLEXPORT int connectINET(const char *host, int prt, int timeout_ms, ExceptionSink *xsink = 0);
00202
00204
00213 DLLEXPORT int connectUNIX(const char *p, ExceptionSink *xsink = 0);
00214
00216
00231 DLLEXPORT int connectSSL(const char *name, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
00232
00234
00250 DLLEXPORT int connectSSL(const char *name, int timeout_ms, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
00251
00253
00267 DLLEXPORT int connectINETSSL(const char *host, int prt, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
00268
00270
00285 DLLEXPORT int connectINETSSL(const char *host, int prt, int timeout_ms, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
00286
00288
00301 DLLEXPORT int connectUNIXSSL(const char *p, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
00302
00304
00312 DLLEXPORT int bind(const char *name, bool reuseaddr = false);
00313
00315
00320 DLLEXPORT int bind(int prt, bool reuseaddr);
00321
00323
00329 DLLEXPORT int bind(const char *interface, int prt, bool reuseaddr = false);
00330
00332
00337 DLLEXPORT int bind(const struct sockaddr *addr, int addr_size);
00338
00340 DLLEXPORT int getPort();
00341
00343
00351 DLLEXPORT QoreSocket *accept(SocketSource *source, ExceptionSink *xsink);
00352
00354
00364 DLLEXPORT QoreSocket *acceptSSL(SocketSource *source, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
00365
00367
00375 DLLEXPORT int acceptAndReplace(SocketSource *source);
00376
00378
00381 DLLEXPORT int listen();
00382
00384
00389 DLLEXPORT int send(const char *buf, qore_size_t size);
00390
00392
00397 DLLEXPORT int send(const QoreString *msg, ExceptionSink *xsink);
00398
00400
00404 DLLEXPORT int send(const BinaryNode *msg);
00405
00407
00412 DLLEXPORT int send(int fd, qore_offset_t size = -1);
00413
00415
00419 DLLEXPORT int sendi1(char i);
00420
00422
00427 DLLEXPORT int sendi2(short i);
00428
00430
00435 DLLEXPORT int sendi4(int i);
00436
00438
00443 DLLEXPORT int sendi8(int64 i);
00444
00446
00452 DLLEXPORT int sendi2LSB(short i);
00453
00455
00461 DLLEXPORT int sendi4LSB(int i);
00462
00464
00470 DLLEXPORT int sendi8LSB(int64 i);
00471
00473
00479 DLLEXPORT int recvi1(int timeout, char *val);
00480
00482
00490 DLLEXPORT int recvi2(int timeout, short *val);
00491
00493
00501 DLLEXPORT int recvi4(int timeout, int *val);
00502
00504
00512 DLLEXPORT int recvi8(int timeout, int64 *val);
00513
00515
00524 DLLEXPORT int recvi2LSB(int timeout, short *val);
00525
00527
00536 DLLEXPORT int recvi4LSB(int timeout, int *val);
00537
00539
00546 DLLEXPORT int recvi8LSB(int timeout, int64 *val);
00547
00549
00555 DLLEXPORT int recvu1(int timeout, unsigned char *val);
00556
00558
00566 DLLEXPORT int recvu2(int timeout, unsigned short *val);
00567
00569
00577 DLLEXPORT int recvu4(int timeout, unsigned int *val);
00578
00580
00589 DLLEXPORT int recvu2LSB(int timeout, unsigned short *val);
00590
00592
00601 DLLEXPORT int recvu4LSB(int timeout, unsigned int *val);
00602
00604
00611 DLLEXPORT QoreStringNode *recv(qore_offset_t bufsize, int timeout, int *prc);
00612
00614
00620 DLLEXPORT BinaryNode *recvBinary(qore_offset_t bufsize, int timeout, int *prc);
00621
00623
00632 DLLEXPORT QoreStringNode *recv(int timeout, int *prc);
00633
00635
00642 DLLEXPORT int recv(int fd, qore_offset_t size, int timeout);
00643
00645
00655 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);
00656
00658
00668 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);
00669
00671
00678 DLLEXPORT AbstractQoreNode *readHTTPHeader(int timeout, int *prc, int source = QORE_SOURCE_SOCKET);
00679
00681
00690 DLLEXPORT QoreHashNode *readHTTPChunkedBodyBinary(int timeout, ExceptionSink *xsink, int source = QORE_SOURCE_SOCKET);
00691
00693
00702 DLLEXPORT QoreHashNode *readHTTPChunkedBody(int timeout, ExceptionSink *xsink, int source = QORE_SOURCE_SOCKET);
00703
00705 DLLEXPORT int setSendTimeout(int ms);
00706
00708 DLLEXPORT int setRecvTimeout(int ms);
00709
00711 DLLEXPORT int getSendTimeout() const;
00712
00714 DLLEXPORT int getRecvTimeout() const;
00715
00717
00722 DLLEXPORT bool isDataAvailable(int timeout = 0) const;
00723
00725
00729 DLLEXPORT int close();
00730
00732
00738 DLLEXPORT int shutdown();
00739
00741
00745 DLLEXPORT int shutdownSSL(ExceptionSink *xsink);
00746
00748
00750 DLLEXPORT int getSocket() const;
00751
00753
00755 DLLEXPORT const QoreEncoding *getEncoding() const;
00756
00758
00760 DLLEXPORT void setEncoding(const QoreEncoding *id);
00761
00763
00765 DLLEXPORT bool isOpen() const;
00766
00768
00770 DLLEXPORT const char *getSSLCipherName() const;
00771
00773
00775 DLLEXPORT const char *getSSLCipherVersion() const;
00776
00778
00780 DLLEXPORT bool isSecure() const;
00781
00783 DLLEXPORT long verifyPeerCertificate() const;
00784
00786
00792 DLLEXPORT int upgradeClientToSSL(X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
00793
00795
00801 DLLEXPORT int upgradeServerToSSL(X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
00802
00804
00809 DLLEXPORT bool isWriteFinished(int timeout = 0) const;
00810
00811 DLLLOCAL static void doException(int rc, const char *meth, ExceptionSink *xsink);
00812
00814 DLLLOCAL void setEventQueue(Queue *cbq, ExceptionSink *xsink);
00815
00817 DLLLOCAL Queue *getQueue();
00818
00820 DLLLOCAL int64 getObjectIDForEvents() const;
00821
00823 DLLLOCAL void cleanup(ExceptionSink *xsink);
00824
00825 DLLLOCAL int setNoDelay(int nodelay);
00826 DLLLOCAL int getNoDelay() const;
00827 };
00828
00829 #endif // _QORE_QORESOCKET_H