EHS Embedded HTTP Server  1.5.0.132
socket.h
00001 /* $Id: socket.h 119 2012-04-05 21:49:58Z felfert $
00002  *
00003  * EHS is a library for embedding HTTP(S) support into a C++ application
00004  *
00005  * Copyright (C) 2004 Zachary J. Hansen
00006  *
00007  * Code cleanup, new features and bugfixes: Copyright (C) 2010 Fritz Elfert
00008  *
00009  *    This library is free software; you can redistribute it and/or
00010  *    modify it under the terms of the GNU Lesser General Public
00011  *    License version 2.1 as published by the Free Software Foundation;
00012  *
00013  *    This library is distributed in the hope that it will be useful,
00014  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  *    Lesser General Public License for more details.
00017  *
00018  *    You should have received a copy of the GNU Lesser General Public
00019  *    License along with this library; if not, write to the Free Software
00020  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  *    This can be found in the 'COPYING' file.
00023  *
00024  */
00025 
00026 #ifndef SOCKET_H
00027 #define SOCKET_H
00028 
00029 #ifdef _MSC_VER
00030 # pragma warning(disable : 4786)
00031 #endif
00032 
00033 #ifdef HAVE_WINSOCK2_H
00034 # include <winsock2.h>
00035 #endif
00036 #ifdef HAVE_WINDOWS_H
00037 # include <windows.h>
00038 #endif
00039 #ifdef HAVE_TIME_H
00040 # include <time.h>
00041 #endif
00042 #ifdef HAVE_NETINET_IN_H
00043 # include <netinet/in.h>
00044 #endif
00045 #ifdef HAVE_SYS_TYPES_H
00046 # include <sys/types.h>
00047 #endif
00048 #ifdef HAVE_SYS_SOCKET_H
00049 # include <sys/socket.h>
00050 #endif
00051 #ifdef HAVE_SYS_IOCTL_H
00052 # include <sys/ioctl.h>
00053 #endif
00054 #ifdef HAVE_UNISTD_H
00055 # include <unistd.h>
00056 #endif
00057 #ifdef HAVE_ARPA_INET_H
00058 # include <arpa/inet.h>
00059 #endif
00060 
00061 #include <cstdio>
00062 
00063 #ifdef _WIN32
00064 typedef unsigned long in_addr_t;
00065 typedef size_t socklen_t;
00066 # define sleep(seconds) (Sleep(seconds * 1000))
00067 #endif
00068 
00069 extern const char *net_strerror();
00070 #ifdef _WIN32
00071 # define net_errno WSAGetLastError()
00072 #else
00073 # define INVALID_SOCKET -1
00074 # define net_errno errno
00075 #endif
00076 
00077 #include "networkabstraction.h"
00078 
00080 class Socket : public NetworkAbstraction {
00081 
00082     private:
00083 
00084         Socket(const Socket &);
00085 
00086         Socket & operator=(const Socket &);
00087 
00088     protected:
00094         Socket(ehs_socket_t fd, sockaddr_in *peer);
00095 
00096     public:
00097 
00101         Socket();
00102 
00103         virtual void RegisterBindHelper(PrivilegedBindHelper *helper);
00104 
00105         virtual void Init(int port);
00106 
00107         virtual ~Socket();
00108 
00109         virtual void SetBindAddress(const char * bindAddress);
00110 
00111         virtual ehs_socket_t GetFd() const { return m_fd; }
00112 
00113         virtual int Read(void *buf, int bufsize);
00114 
00115         virtual int Send(const void *buf, size_t buflen, int flags = 0);
00116 
00117         virtual void Close();
00118 
00119         virtual NetworkAbstraction *Accept();
00120 
00123         virtual bool IsSecure() const { return false; }
00124 
00125         virtual void ThreadCleanup() { }
00126 
00127     protected:
00128 
00129         int GetLocalPort() const;
00130 
00131         int GetRemotePort() const;
00132 
00133         std::string GetLocalAddress() const;
00134 
00135         std::string GetRemoteAddress() const;
00136 
00137         std::string GetPeer() const;
00138 
00140         ehs_socket_t m_fd;
00141 
00143         sockaddr_in m_peer;
00144 
00146         sockaddr_in m_bindaddr;
00147 
00149         PrivilegedBindHelper *m_pBindHelper;
00150 
00151 };
00152 
00153 #endif // SOCKET_H