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
00028 #ifndef _QORE_QORESOCKET_H
00029
00030 #define _QORE_QORESOCKET_H
00031
00032 #include <qore/Qore.h>
00033
00034 #include <sys/types.h>
00035 #include <sys/socket.h>
00036 #include <sys/un.h>
00037 #include <netinet/in.h>
00038 #include <netdb.h>
00039 #include <unistd.h>
00040 #include <strings.h>
00041 #include <string.h>
00042 #include <stdlib.h>
00043 #include <ctype.h>
00044 #include <unistd.h>
00045 #include <arpa/inet.h>
00046
00047 #include <openssl/ssl.h>
00048 #include <openssl/err.h>
00049
00050 #define QSE_MISC_ERR 0
00051 #define QSE_RECV_ERR -1
00052 #define QSE_NOT_OPEN -2
00053 #define QSE_TIMEOUT -3
00054
00055 class Queue;
00056
00058
00063 class SocketSource {
00064 private:
00065 struct qore_socketsource_private *priv;
00066
00068 DLLLOCAL SocketSource(const SocketSource&);
00069
00071 DLLLOCAL SocketSource& operator=(const SocketSource&);
00072
00073 public:
00075 DLLEXPORT SocketSource();
00076
00078 DLLEXPORT ~SocketSource();
00079
00081
00083 DLLEXPORT QoreStringNode *takeAddress();
00084
00086
00088 DLLEXPORT QoreStringNode *takeHostName();
00089
00091
00093 DLLEXPORT const char *getAddress() const;
00094
00096
00098 DLLEXPORT const char *getHostName() const;
00099
00100 DLLLOCAL void setAddress(QoreStringNode *addr);
00101 DLLLOCAL void setAddress(const char *addr);
00102 DLLLOCAL void setHostName(const char *host);
00103 DLLLOCAL void setHostName(QoreStringNode *host);
00104 DLLLOCAL void setAll(QoreObject *o, ExceptionSink *xsink);
00105 };
00106
00108
00118 class QoreSocket {
00119 private:
00121 struct qore_socket_private *priv;
00122
00124 DLLLOCAL QoreSocket(int s, int t, const QoreEncoding *csid);
00125
00127 DLLLOCAL int openINET();
00128
00130 DLLLOCAL int openUNIX();
00131
00132 DLLLOCAL void reuse(int opt);
00133 DLLLOCAL int recv(char *buf, qore_size_t bs, int flags, int timeout, bool do_event = true);
00134
00136 DLLLOCAL QoreStringNode *readHTTPData(int timeout, int *rc, int state = -1);
00137
00138 DLLLOCAL static void convertHeaderToHash(QoreHashNode *h, char *p);
00139
00141 DLLLOCAL QoreSocket(const QoreSocket&);
00142
00144 DLLLOCAL QoreSocket& operator=(const QoreSocket&);
00145
00146 public:
00148 DLLEXPORT QoreSocket();
00149
00151 DLLEXPORT ~QoreSocket();
00152
00154
00165 DLLEXPORT int connect(const char *name, ExceptionSink *xsink = 0);
00166
00168
00180 DLLEXPORT int connect(const char *name, int timeout_ms, ExceptionSink *xsink = 0);
00181
00183
00193 DLLEXPORT int connectINET(const char *host, int prt, ExceptionSink *xsink = 0);
00194
00196
00207 DLLEXPORT int connectINET(const char *host, int prt, int timeout_ms, ExceptionSink *xsink = 0);
00208
00210
00219 DLLEXPORT int connectUNIX(const char *p, ExceptionSink *xsink = 0);
00220
00222
00237 DLLEXPORT int connectSSL(const char *name, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
00238
00240
00256 DLLEXPORT int connectSSL(const char *name, int timeout_ms, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
00257
00259
00273 DLLEXPORT int connectINETSSL(const char *host, int prt, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
00274
00276
00291 DLLEXPORT int connectINETSSL(const char *host, int prt, int timeout_ms, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
00292
00294
00307 DLLEXPORT int connectUNIXSSL(const char *p, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
00308
00310
00318 DLLEXPORT int bind(const char *name, bool reuseaddr = false);
00319
00321
00326 DLLEXPORT int bind(int prt, bool reuseaddr);
00327
00329
00335 DLLEXPORT int bind(const char *interface, int prt, bool reuseaddr = false);
00336
00338
00343 DLLEXPORT int bind(const struct sockaddr *addr, int addr_size);
00344
00346 DLLEXPORT int getPort();
00347
00349
00357 DLLEXPORT QoreSocket *accept(SocketSource *source, ExceptionSink *xsink);
00358
00360
00370 DLLEXPORT QoreSocket *acceptSSL(SocketSource *source, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
00371
00373
00381 DLLEXPORT int acceptAndReplace(SocketSource *source);
00382
00384
00387 DLLEXPORT int listen();
00388
00390
00395 DLLEXPORT int send(const char *buf, qore_size_t size);
00396
00398
00403 DLLEXPORT int send(const QoreString *msg, ExceptionSink *xsink);
00404
00406
00410 DLLEXPORT int send(const BinaryNode *msg);
00411
00413
00418 DLLEXPORT int send(int fd, qore_offset_t size = -1);
00419
00421
00425 DLLEXPORT int sendi1(char i);
00426
00428
00433 DLLEXPORT int sendi2(short i);
00434
00436
00441 DLLEXPORT int sendi4(int i);
00442
00444
00449 DLLEXPORT int sendi8(int64 i);
00450
00452
00458 DLLEXPORT int sendi2LSB(short i);
00459
00461
00467 DLLEXPORT int sendi4LSB(int i);
00468
00470
00476 DLLEXPORT int sendi8LSB(int64 i);
00477
00479
00485 DLLEXPORT int recvi1(int timeout, char *val);
00486
00488
00496 DLLEXPORT int recvi2(int timeout, short *val);
00497
00499
00507 DLLEXPORT int recvi4(int timeout, int *val);
00508
00510
00518 DLLEXPORT int recvi8(int timeout, int64 *val);
00519
00521
00530 DLLEXPORT int recvi2LSB(int timeout, short *val);
00531
00533
00542 DLLEXPORT int recvi4LSB(int timeout, int *val);
00543
00545
00552 DLLEXPORT int recvi8LSB(int timeout, int64 *val);
00553
00555
00561 DLLEXPORT int recvu1(int timeout, unsigned char *val);
00562
00564
00572 DLLEXPORT int recvu2(int timeout, unsigned short *val);
00573
00575
00583 DLLEXPORT int recvu4(int timeout, unsigned int *val);
00584
00586
00595 DLLEXPORT int recvu2LSB(int timeout, unsigned short *val);
00596
00598
00607 DLLEXPORT int recvu4LSB(int timeout, unsigned int *val);
00608
00610
00617 DLLEXPORT QoreStringNode *recv(qore_offset_t bufsize, int timeout, int *prc);
00618
00620
00626 DLLEXPORT BinaryNode *recvBinary(qore_offset_t bufsize, int timeout, int *prc);
00627
00629
00638 DLLEXPORT QoreStringNode *recv(int timeout, int *prc);
00639
00641
00649 DLLEXPORT BinaryNode *recvBinary(int timeout, int *prc);
00650
00652
00659 DLLEXPORT int recv(int fd, qore_offset_t size, int timeout);
00660
00662
00672 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);
00673
00675
00686 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);
00687
00689
00699 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);
00700
00702
00709 DLLEXPORT AbstractQoreNode *readHTTPHeader(int timeout, int *prc, int source = QORE_SOURCE_SOCKET);
00710
00712
00720 DLLEXPORT AbstractQoreNode *readHTTPHeader(QoreHashNode *info, int timeout, int *prc, int source = QORE_SOURCE_SOCKET);
00721
00723
00732 DLLEXPORT QoreHashNode *readHTTPChunkedBodyBinary(int timeout, ExceptionSink *xsink, int source = QORE_SOURCE_SOCKET);
00733
00735
00744 DLLEXPORT QoreHashNode *readHTTPChunkedBody(int timeout, ExceptionSink *xsink, int source = QORE_SOURCE_SOCKET);
00745
00747 DLLEXPORT int setSendTimeout(int ms);
00748
00750 DLLEXPORT int setRecvTimeout(int ms);
00751
00753 DLLEXPORT int getSendTimeout() const;
00754
00756 DLLEXPORT int getRecvTimeout() const;
00757
00759
00764 DLLEXPORT bool isDataAvailable(int timeout = 0) const;
00765
00767
00771 DLLEXPORT int close();
00772
00774
00780 DLLEXPORT int shutdown();
00781
00783
00787 DLLEXPORT int shutdownSSL(ExceptionSink *xsink);
00788
00790
00792 DLLEXPORT int getSocket() const;
00793
00795
00797 DLLEXPORT const QoreEncoding *getEncoding() const;
00798
00800
00802 DLLEXPORT void setEncoding(const QoreEncoding *id);
00803
00805
00807 DLLEXPORT bool isOpen() const;
00808
00810
00812 DLLEXPORT const char *getSSLCipherName() const;
00813
00815
00817 DLLEXPORT const char *getSSLCipherVersion() const;
00818
00820
00822 DLLEXPORT bool isSecure() const;
00823
00825 DLLEXPORT long verifyPeerCertificate() const;
00826
00828
00834 DLLEXPORT int upgradeClientToSSL(X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
00835
00837
00843 DLLEXPORT int upgradeServerToSSL(X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
00844
00846
00851 DLLEXPORT bool isWriteFinished(int timeout = 0) const;
00852
00853 DLLLOCAL static void doException(int rc, const char *meth, ExceptionSink *xsink);
00854
00856 DLLLOCAL void setEventQueue(Queue *cbq, ExceptionSink *xsink);
00857
00859 DLLLOCAL Queue *getQueue();
00860
00862 DLLLOCAL int64 getObjectIDForEvents() const;
00863
00865 DLLLOCAL void cleanup(ExceptionSink *xsink);
00866
00867 DLLLOCAL int setNoDelay(int nodelay);
00868 DLLLOCAL int getNoDelay() const;
00869 };
00870
00871 #endif // _QORE_QORESOCKET_H