EHS Embedded HTTP Server  1.5.1.0
socket.h
1 /* $Id: socket.h 119 2012-04-05 21:49:58Z felfert $
2  *
3  * EHS is a library for embedding HTTP(S) support into a C++ application
4  *
5  * Copyright (C) 2004 Zachary J. Hansen
6  *
7  * Code cleanup, new features and bugfixes: Copyright (C) 2010 Fritz Elfert
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License version 2.1 as published by the Free Software Foundation;
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  * This can be found in the 'COPYING' file.
23  *
24  */
25 
26 #ifndef SOCKET_H
27 #define SOCKET_H
28 
29 #ifdef _MSC_VER
30 # pragma warning(disable : 4786)
31 #endif
32 
33 #ifdef HAVE_WINSOCK2_H
34 # include <winsock2.h>
35 #endif
36 #ifdef HAVE_WINDOWS_H
37 # include <windows.h>
38 #endif
39 #ifdef HAVE_TIME_H
40 # include <time.h>
41 #endif
42 #ifdef HAVE_NETINET_IN_H
43 # include <netinet/in.h>
44 #endif
45 #ifdef HAVE_SYS_TYPES_H
46 # include <sys/types.h>
47 #endif
48 #ifdef HAVE_SYS_SOCKET_H
49 # include <sys/socket.h>
50 #endif
51 #ifdef HAVE_SYS_IOCTL_H
52 # include <sys/ioctl.h>
53 #endif
54 #ifdef HAVE_UNISTD_H
55 # include <unistd.h>
56 #endif
57 #ifdef HAVE_ARPA_INET_H
58 # include <arpa/inet.h>
59 #endif
60 
61 #include <cstdio>
62 
63 #ifdef _WIN32
64 typedef unsigned long in_addr_t;
65 typedef size_t socklen_t;
66 # define sleep(seconds) (Sleep(seconds * 1000))
67 #endif
68 
69 extern const char *net_strerror();
70 #ifdef _WIN32
71 # define net_errno WSAGetLastError()
72 #else
73 # define INVALID_SOCKET -1
74 # define net_errno errno
75 #endif
76 
77 #include "networkabstraction.h"
78 
80 class Socket : public NetworkAbstraction {
81 
82  private:
83 
84  Socket(const Socket &);
85 
86  Socket & operator=(const Socket &);
87 
88  protected:
94  Socket(ehs_socket_t fd, sockaddr_in *peer);
95 
96  public:
97 
101  Socket();
102 
103  virtual void RegisterBindHelper(PrivilegedBindHelper *helper);
104 
105  virtual void Init(int port);
106 
107  virtual ~Socket();
108 
109  virtual void SetBindAddress(const char * bindAddress);
110 
111  virtual ehs_socket_t GetFd() const { return m_fd; }
112 
113  virtual int Read(void *buf, int bufsize);
114 
115  virtual int Send(const void *buf, size_t buflen, int flags = 0);
116 
117  virtual void Close();
118 
119  virtual NetworkAbstraction *Accept();
120 
123  virtual bool IsSecure() const { return false; }
124 
125  virtual void ThreadCleanup() { }
126 
127  protected:
128 
129  int GetLocalPort() const;
130 
131  int GetRemotePort() const;
132 
133  std::string GetLocalAddress() const;
134 
135  std::string GetRemoteAddress() const;
136 
137  std::string GetPeer() const;
138 
140  ehs_socket_t m_fd;
141 
143  sockaddr_in m_peer;
144 
146  sockaddr_in m_bindaddr;
147 
150 
151 };
152 
153 #endif // SOCKET_H
Socket::GetRemoteAddress
std::string GetRemoteAddress() const
Retrieves the peer address.
Socket::RegisterBindHelper
virtual void RegisterBindHelper(PrivilegedBindHelper *helper)
Registers a PrivilegedBindHelper for use by this instance.
Socket::GetRemotePort
int GetRemotePort() const
Retrieves the peer's port of a connection.
Socket::Init
virtual void Init(int port)
Initializes a listening socket.
Socket::Send
virtual int Send(const void *buf, size_t buflen, int flags=0)
Performs a send on the underlying socket.
Socket::Accept
virtual NetworkAbstraction * Accept()
Waits for an incoming connection.
Socket::SetBindAddress
virtual void SetBindAddress(const char *bindAddress)
Sets the bind address of the socket.
Socket::m_fd
ehs_socket_t m_fd
The file descriptor of the socket on which this connection came in.
Definition: socket.h:140
Socket::m_peer
sockaddr_in m_peer
Stores the peer address of the current connection.
Definition: socket.h:143
Socket::Close
virtual void Close()
Closes the underlying socket.
Socket::GetLocalAddress
std::string GetLocalAddress() const
Retrieves the peer address.
Socket
plain socket implementation of NetworkAbstraction
Definition: socket.h:80
Socket::IsSecure
virtual bool IsSecure() const
Determines, whether the underlying socket is secure.
Definition: socket.h:123
Socket::m_bindaddr
sockaddr_in m_bindaddr
Stores the bind address.
Definition: socket.h:146
Socket::Read
virtual int Read(void *buf, int bufsize)
Performs a read from the underlying socket.
PrivilegedBindHelper
Helper class for binding of sockets to privileged ports.
Definition: ehs.h:99
Socket::m_pBindHelper
PrivilegedBindHelper * m_pBindHelper
Our bind helper.
Definition: socket.h:149
NetworkAbstraction
Abstracts different socket types.
Definition: networkabstraction.h:49
Socket::GetPeer
std::string GetPeer() const
Combination of GetRemoteAddress and GetRemotePort.
Socket::GetFd
virtual ehs_socket_t GetFd() const
Retrieves the underlying file descriptor.
Definition: socket.h:111
Socket::ThreadCleanup
virtual void ThreadCleanup()
Handles thread specific clean up (used by OpenSSL).
Definition: socket.h:125
Socket::Socket
Socket()
Default constructor.
Socket::GetLocalPort
int GetLocalPort() const
Retrieves the peer's port of a connection.