Binding do privileged ports.This example shows, how to use a BindHelper in order to bind to privileged ports. Before trying this, the bindhelper binary must be changed setuid-root.
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <ehs.h>
#include <sstream>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cerrno>
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
#ifdef HAVE_SYS_WAIT_H
# include <sys/wait.h>
#endif
#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#include "common.h"
using namespace std;
class TestHarness :
public EHS
{
{
ostringstream oss;
oss
<< "<html><head><title>TestHarness</title></head><body><table><tr>"
<<
"<tr><td>request-method:</td><td>" << request->
Method() <<
"</td></tr>" << endl
<<
"<tr><td>uri:</td><td>" << request->
Uri() <<
"</td></tr>" << endl
<<
"<tr><td>http-version:</td><td>" << request->
HttpVersion() <<
"</td></tr>" << endl
<<
"<tr><td>body-length:</td><td>" << request->
Body().length ( ) <<
"</td></tr>" << endl
<<
"<tr><td>number-request-headers:</td><td>" << request->
Headers().size ( ) <<
"</td></tr>" << endl
<<
"<tr><td>number-form-value-maps:</td><td>" << request->
FormValues().size ( ) <<
"</td></tr>" << endl
<<
"<tr><td>client-address:</td><td>" << request->
RemoteAddress ( ) <<
"</td></tr>" << endl
<<
"<tr><td>client-port:</td><td>" << request->
RemotePort ( ) <<
"</td></tr>" << endl;
for ( StringMap::iterator i = request->
Headers().begin ( );
i != request->
Headers().end ( ); ++i ) {
oss << "<tr><td>Request Header:</td><td>" << i->first << " => " << i->second << "</td></tr>" << endl;
}
for ( CookieMap::iterator i = request->
Cookies().begin ( );
i != request->
Cookies().end ( ); ++i ) {
oss << "<tr><td>Cookie:</td><td>" << i->first << " => " << i->second << "</td></tr>" << endl;
}
oss << "</table></body></html>" << endl;
response->
SetBody ( oss.str().c_str(), oss.str().length() );
return HTTPRESPONSECODE_200_OK;
}
};
#ifndef _WIN32
{
public:
MyHelper() : mutex(pthread_mutex_t())
{
pthread_mutex_init(&mutex, NULL);
}
virtual bool BindPrivilegedPort(int socket, const char *addr, const unsigned short port)
{
bool ret = false;
pid_t pid;
int status;
char buf[32];
pthread_mutex_lock(&mutex);
switch (pid = fork()) {
case 0:
sprintf(buf, "%08x%08x%04x", socket, inet_addr(addr), port);
execl("bindhelper", buf, ((void *)NULL));
exit(errno);
break;
case -1:
break;
default:
if (waitpid(pid, &status, 0) != -1) {
ret = (0 == status);
if (0 != status)
cerr << "bind: " << strerror(WEXITSTATUS(status)) << endl;
}
break;
}
pthread_mutex_unlock(&mutex);
return ret;
}
private:
pthread_mutex_t mutex;
};
#endif
int main ( int argc, char ** argv )
{
cout << getEHSconfig() << endl;
if (argc != 2) {
cerr << "Usage: " << basename(argv[0]) << " [port]" << endl;
return 0;
}
cerr << "binding to " << atoi(argv[1]) << endl;
TestHarness srv;
#ifndef _WIN32
MyHelper h;
srv.SetBindHelper(&h);
#endif
EHSServerParameters oSP;
oSP["port"] = argv[1];
oSP["mode"] = "threadpool";
try {
srv.StartServer(oSP);
kbdio kbd;
cout << "Press q to terminate ..." << endl;
while (!(srv.ShouldTerminate() || kbd.qpressed())) {
usleep(300000);
}
srv.StopServer();
} catch (exception &e) {
cerr << "ERROR: " << e.what() << endl;
}
return 0;
}
EHS provides HTTP server functionality to a child class.
This class represents a clients HTTP request.
const std::string & Body() const
Retrieves this request's body.
FormValueMap & FormValues()
Retrieves form values.
std::string RemoteAddress()
Retrieves the peer's IP address.
const std::string & Uri() const
Retrieves this request's URI.
CookieMap & Cookies()
Retrieves cookies.
const std::string & HttpVersion() const
Retrieves the HTTP version.
int RemotePort()
Retrieves the peer's port.
RequestMethod Method() const
Retrieves the request method.
StringCaseMap & Headers()
Retrieves HTTP headers.
This class represents what is sent back to the client.
void SetBody(const char *ipsBody, size_t inBodyLength)
Sets the body of this instance.
Helper class for binding of sockets to privileged ports.