All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
QoreSocket.h
/* -*- mode: c++; indent-tabs-mode: nil -*- */
/*
QoreSocket.h
ipv4, ipv6, unix socket class with SSL support
Qore Programming Language
Copyright 2003 - 2013 David Nichols
will unlink (delete) UNIX domain socket files when closed
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _QORE_QORESOCKET_H
#define _QORE_QORESOCKET_H
#include <qore/Qore.h>
#include <sys/types.h>
#include <unistd.h>
#include <strings.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#define QSE_MISC_ERR 0
#define QSE_RECV_ERR -1
#define QSE_NOT_OPEN -2
#define QSE_TIMEOUT -3
#define QSE_SSL_ERR -4
class Queue;
class SocketSource {
friend struct qore_socket_private;
private:
struct qore_socketsource_private *priv; // private implementation
DLLLOCAL SocketSource(const SocketSource&);
DLLLOCAL SocketSource& operator=(const SocketSource&);
public:
DLLEXPORT SocketSource();
DLLEXPORT ~SocketSource();
DLLEXPORT QoreStringNode *takeAddress();
DLLEXPORT QoreStringNode *takeHostName();
DLLEXPORT const char *getAddress() const;
DLLEXPORT const char *getHostName() const;
DLLLOCAL void setAll(QoreObject *o, ExceptionSink *xsink);
};
class QoreSocket {
private:
struct qore_socket_private *priv;
DLLLOCAL QoreSocket(int n_sock, int n_sfamily, int n_stype, int s_prot, const QoreEncoding *csid);
DLLLOCAL static void convertHeaderToHash(QoreHashNode *h, char *p);
DLLLOCAL QoreSocket(const QoreSocket&);
DLLLOCAL QoreSocket& operator=(const QoreSocket&);
public:
DLLEXPORT QoreSocket();
DLLEXPORT ~QoreSocket();
DLLEXPORT int connect(const char *name, ExceptionSink *xsink = 0);
DLLEXPORT int connect(const char *name, int timeout_ms, ExceptionSink *xsink = 0);
DLLEXPORT int connectINET(const char *host, int prt, ExceptionSink *xsink = 0);
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);
DLLEXPORT int connectINET(const char *host, int prt, int timeout_ms, ExceptionSink *xsink = 0);
DLLEXPORT int connectUNIX(const char *p, ExceptionSink *xsink = 0);
DLLEXPORT int connectUNIX(const char *p, int socktype, int protocol = 0, ExceptionSink *xsink = 0);
DLLEXPORT int connectSSL(const char *name, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
DLLEXPORT int connectSSL(const char *name, int timeout_ms, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
DLLEXPORT int connectINETSSL(const char *host, int prt, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
DLLEXPORT int connectINETSSL(const char *host, int prt, int timeout_ms, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
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);
DLLEXPORT int connectUNIXSSL(const char *p, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
DLLEXPORT int connectUNIXSSL(const char *p, int socktype, int protocol, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
DLLEXPORT int bind(const char *name, bool reuseaddr = false);
DLLEXPORT int bind(int prt, bool reuseaddr);
DLLEXPORT int bind(const char *iface, int prt, bool reuseaddr = false);
DLLEXPORT int bind(const struct sockaddr *addr, int addr_size);
DLLEXPORT int bind(int family, const struct sockaddr *addr, int addr_size, int socktype = Q_SOCK_STREAM, int protocol = 0);
DLLEXPORT int bindUNIX(const char *name, ExceptionSink *xsink = 0);
DLLEXPORT int bindUNIX(const char *name, int socktype, int protocol = 0, ExceptionSink *xsink = 0);
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);
DLLEXPORT int getPort();
DLLEXPORT QoreSocket *accept(SocketSource *source, ExceptionSink *xsink);
DLLEXPORT QoreSocket *acceptSSL(SocketSource *source, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
DLLEXPORT int acceptAndReplace(SocketSource *source);
DLLEXPORT QoreSocket *accept(int timeout_ms, ExceptionSink *xsink);
DLLEXPORT QoreSocket *acceptSSL(int timeout_ms, X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
DLLEXPORT int acceptAndReplace(int timeout_ms, ExceptionSink *xsink);
DLLEXPORT int listen();
DLLEXPORT int send(const char *buf, qore_size_t size);
DLLEXPORT int send(const char *buf, qore_size_t size, ExceptionSink* xsink);
DLLEXPORT int send(const char *buf, qore_size_t size, int timeout_ms, ExceptionSink* xsink);
DLLEXPORT int send(const QoreString *msg, ExceptionSink *xsink);
DLLEXPORT int send(const QoreString *msg, int timeout_ms, ExceptionSink *xsink);
DLLEXPORT int send(const BinaryNode *msg);
DLLEXPORT int send(const BinaryNode *msg, ExceptionSink* xsink);
DLLEXPORT int send(const BinaryNode *msg, int timeout_ms, ExceptionSink* xsink);
DLLEXPORT int send(int fd, qore_offset_t size = -1);
DLLEXPORT int sendi1(char i);
DLLEXPORT int sendi2(short i);
DLLEXPORT int sendi4(int i);
DLLEXPORT int sendi8(int64 i);
DLLEXPORT int sendi2LSB(short i);
DLLEXPORT int sendi4LSB(int i);
DLLEXPORT int sendi8LSB(int64 i);
DLLEXPORT int sendi1(char i, int timeout_ms, ExceptionSink* xsink);
DLLEXPORT int sendi2(short i, int timeout_ms, ExceptionSink* xsink);
DLLEXPORT int sendi4(int i, int timeout_ms, ExceptionSink* xsink);
DLLEXPORT int sendi8(int64 i, int timeout_ms, ExceptionSink* xsink);
DLLEXPORT int sendi2LSB(short i, int timeout_ms, ExceptionSink* xsink);
DLLEXPORT int sendi4LSB(int i, int timeout_ms, ExceptionSink* xsink);
DLLEXPORT int sendi8LSB(int64 i, int timeout_ms, ExceptionSink* xsink);
DLLEXPORT int recvi1(int timeout_ms, char *val);
DLLEXPORT int recvi2(int timeout_ms, short *val);
DLLEXPORT int recvi4(int timeout_ms, int *val);
DLLEXPORT int recvi8(int timeout_ms, int64 *val);
DLLEXPORT int recvi2LSB(int timeout_ms, short *val);
DLLEXPORT int recvi4LSB(int timeout_ms, int *val);
DLLEXPORT int recvi8LSB(int timeout_ms, int64 *val);
DLLEXPORT int recvu1(int timeout_ms, unsigned char *val);
DLLEXPORT int recvu2(int timeout_ms, unsigned short *val);
DLLEXPORT int recvu4(int timeout_ms, unsigned int *val);
DLLEXPORT int recvu2LSB(int timeout_ms, unsigned short *val);
DLLEXPORT int recvu4LSB(int timeout_ms, unsigned int *val);
DLLEXPORT int64 recvi1(int timeout_ms, char *val, ExceptionSink* xsink);
DLLEXPORT int64 recvi2(int timeout_ms, short *val, ExceptionSink* xsink);
DLLEXPORT int64 recvi4(int timeout_ms, int *val, ExceptionSink* xsink);
DLLEXPORT int64 recvi8(int timeout_ms, int64 *val, ExceptionSink* xsink);
DLLEXPORT int64 recvi2LSB(int timeout_ms, short *val, ExceptionSink* xsink);
DLLEXPORT int64 recvi4LSB(int timeout_ms, int *val, ExceptionSink* xsink);
DLLEXPORT int64 recvi8LSB(int timeout_ms, int64 *val, ExceptionSink* xsink);
DLLEXPORT int64 recvu1(int timeout_ms, unsigned char *val, ExceptionSink* xsink);
DLLEXPORT int64 recvu2(int timeout_ms, unsigned short *val, ExceptionSink* xsink);
DLLEXPORT int64 recvu4(int timeout_ms, unsigned int *val, ExceptionSink* xsink);
DLLEXPORT int64 recvu2LSB(int timeout_ms, unsigned short *val, ExceptionSink* xsink);
DLLEXPORT int64 recvu4LSB(int timeout_ms, unsigned int *val, ExceptionSink* xsink);
DLLEXPORT QoreStringNode *recv(qore_offset_t bufsize, int timeout_ms, int *prc);
DLLEXPORT QoreStringNode* recv(qore_offset_t bufsize, int timeout_ms, ExceptionSink* xsink);
DLLEXPORT BinaryNode *recvBinary(qore_offset_t bufsize, int timeout_ms, int *prc);
DLLEXPORT BinaryNode* recvBinary(qore_offset_t bufsize, int timeout_ms, ExceptionSink* xsink);
DLLEXPORT QoreStringNode *recv(int timeout_ms, int *prc);
DLLEXPORT QoreStringNode* recv(int timeout_ms, ExceptionSink* xsink);
DLLEXPORT BinaryNode *recvBinary(int timeout_ms, int *prc);
DLLEXPORT BinaryNode* recvBinary(int timeout_ms, ExceptionSink* xsink);
DLLEXPORT int recv(int fd, qore_offset_t size, int timeout_ms);
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);
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);
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);
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);
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);
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);
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);
DLLEXPORT AbstractQoreNode *readHTTPHeader(int timeout_ms, int *prc, int source = QORE_SOURCE_SOCKET);
DLLEXPORT AbstractQoreNode *readHTTPHeader(QoreHashNode *info, int timeout_ms, int *prc, int source = QORE_SOURCE_SOCKET);
DLLEXPORT QoreHashNode *readHTTPHeader(ExceptionSink* xsink, QoreHashNode *info, int timeout_ms, int source = QORE_SOURCE_SOCKET);
DLLEXPORT QoreHashNode *readHTTPChunkedBodyBinary(int timeout_ms, ExceptionSink *xsink, int source = QORE_SOURCE_SOCKET);
DLLEXPORT QoreHashNode *readHTTPChunkedBody(int timeout_ms, ExceptionSink *xsink, int source = QORE_SOURCE_SOCKET);
DLLEXPORT int setSendTimeout(int ms);
DLLEXPORT int setRecvTimeout(int ms);
DLLEXPORT int getSendTimeout() const;
DLLEXPORT int getRecvTimeout() const;
DLLEXPORT bool isDataAvailable(int timeout_ms = 0) const;
DLLEXPORT int close();
DLLEXPORT int shutdown();
DLLEXPORT int shutdownSSL(ExceptionSink *xsink);
DLLEXPORT int getSocket() const;
DLLEXPORT const QoreEncoding *getEncoding() const;
DLLEXPORT void setEncoding(const QoreEncoding *id);
DLLEXPORT bool isOpen() const;
DLLEXPORT const char *getSSLCipherName() const;
DLLEXPORT const char *getSSLCipherVersion() const;
DLLEXPORT bool isSecure() const;
DLLEXPORT long verifyPeerCertificate() const;
DLLEXPORT int upgradeClientToSSL(X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
DLLEXPORT int upgradeServerToSSL(X509 *cert, EVP_PKEY *pkey, ExceptionSink *xsink);
DLLEXPORT bool isWriteFinished(int timeout_ms = 0) const;
DLLEXPORT QoreHashNode* getPeerInfo(ExceptionSink* xsink) const;
DLLEXPORT QoreHashNode* getSocketInfo(ExceptionSink* xsink) const;
DLLLOCAL static void doException(int rc, const char *meth, int timeout_ms, ExceptionSink *xsink);
DLLLOCAL void setEventQueue(Queue *cbq, ExceptionSink *xsink);
DLLLOCAL Queue *getQueue();
DLLLOCAL int64 getObjectIDForEvents() const;
DLLLOCAL void cleanup(ExceptionSink *xsink);
DLLLOCAL int setNoDelay(int nodelay);
DLLLOCAL int getNoDelay() const;
DLLLOCAL void setAccept(QoreObject *o);
};
#endif // _QORE_QORESOCKET_H