#ifndef SOCKET_H #define SOCKET_H using namespace std; #include <sys/socket.h> #include <netinet/in.h> #include <string> #include "config.h" struct hostent; class InAddr { public: virtual operator sockaddr_storage() const = 0; virtual string tostring() const = 0; virtual int af() const = 0; virtual sockaddr *addr() const = 0; virtual socklen_t addr_len() const = 0; virtual ~InAddr(); virtual int get_port() const = 0; virtual void set_port(int) = 0; virtual bool is6() const { return 0; } virtual hostent *gethost() = 0; static InAddr *create(const sockaddr *addr); static InAddr *create(const sockaddr_storage *addr) { return create((sockaddr*)addr); } static InAddr *getpeername(int fd); static InAddr *getsockname(int fd); static InAddr *listener(int af, int port); }; //! the representation of a network connection class Socket { //! the file descriptor int fd; //! is this a listener bool listener; InAddr *that_addr; InAddr *this_addr; //! is this dead? bool dead; string buffer; public: //! the local port int port; //! create an empty socket Socket(); //! create a listener on port Socket(int port); int connect(const char *, int); //! connect this socket to fd int connect(int fd); //! read data from this socket int read(char *, int); //! write data to this socket int write(const char *, int); //! is this socket dead? bool get_dead() { return dead; } //! get the address InAddr *get_addr(); //! get the address InAddr *get_local_addr(); //! accept a socket from this one Socket *accept(); string read_data(); virtual ~Socket(); int getfd() { return fd; } //! Is this an IPv6 native connection? bool is6() { return get_addr()->is6(); } }; #endif