summaryrefslogtreecommitdiff
path: root/yhttpd/src/sock
diff options
context:
space:
mode:
Diffstat (limited to 'yhttpd/src/sock')
-rw-r--r--yhttpd/src/sock/sock.cpp473
-rw-r--r--yhttpd/src/sock/sock.h91
-rw-r--r--yhttpd/src/sock/sslsock.cpp141
-rw-r--r--yhttpd/src/sock/sslsock.h41
4 files changed, 746 insertions, 0 deletions
diff --git a/yhttpd/src/sock/sock.cpp b/yhttpd/src/sock/sock.cpp
new file mode 100644
index 0000000..5ae0be1
--- /dev/null
+++ b/yhttpd/src/sock/sock.cpp
@@ -0,0 +1,473 @@
+#ifndef SOCK_CPP
+#define SOCK_CPP
+
+#include <arpa/inet.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include "sock.h"
+
+using namespace std;
+
+sock::sock()
+{
+ this->b_run = true;
+ this->i_req = 0;
+ this->req_parser = new reqp();
+#ifdef LOGGING
+
+ this->log_daemon = new logd( wrap::CONF->get_elem( "httpd.logging.accessfile" ),
+
+ wrap::CONF->get_elem( "httpd.logging.access_lines" ) );
+#endif
+}
+
+int
+sock::_send(socketcontainer *p_sock, const char *sz, int len)
+{
+
+ return send( p_sock->i_sock, sz, len, 0 );
+}
+
+int
+sock::_read(socketcontainer *p_sock, char *sz, int len)
+{
+
+ return read( p_sock->i_sock, sz, len );
+}
+
+int
+sock::_close(socketcontainer *p_sock)
+{
+ shutdown( p_sock->i_sock, 2 );
+ close ( p_sock->i_sock );
+ delete p_sock;
+}
+
+
+int
+sock::_make_server_socket( int i_port )
+{
+ size_t i_sock;
+ struct sockaddr_in name;
+
+ // create the server socket.
+ i_sock = socket (PF_INET, SOCK_STREAM, 0);
+ if (i_sock < 0)
+ {
+ wrap::system_message( SOCKERR );
+
+ if ( ++i_port > MAXPORT )
+ exit(1);
+
+ wrap::system_message( SOCKERR );
+
+ return _make_server_socket( i_port );
+ }
+
+ // give the server socket a name.
+ name.sin_family = AF_INET;
+ name.sin_port = htons(i_port);
+ name.sin_addr.s_addr = htonl(INADDR_ANY);
+ int i_optval = 1;
+
+ setsockopt( i_sock, SOL_SOCKET, SO_REUSEADDR, (char*)&i_optval, sizeof(int) );
+
+ if ( bind(i_sock, (struct sockaddr *) &name, sizeof (name)) < 0 )
+ {
+
+ wrap::system_message( BINDERR );
+
+ if ( ++i_port > MAXPORT )
+ exit(1);
+
+ wrap::system_message( string(SOCKERR) + tool::int2string(i_port) );
+
+ // Rerun recursive.
+ return _make_server_socket( i_port );
+ }
+
+ wrap::system_message( SOCKCRT + string("localhost:") + tool::int2string(i_port) );
+
+ i_server_port = i_port;
+ i_server_sock = i_sock;
+
+ return i_sock;
+}
+
+string
+sock::read_http_line(socketcontainer *p_sock)
+{
+ string s_line;
+ int i_total = 0;
+ int i_read = 0;
+ char ch;
+
+ do
+ {
+ i_read = _read(p_sock, &ch, sizeof(ch));
+
+ if(i_read <= 0)
+ return "";
+
+ s_line += ch;
+ i_total++;
+ }
+ while((ch != '\n') && i_total < MAXLENGTH);
+
+ if(ch != '\n')
+ /*
+ ** the games people play
+ */
+ return "";
+
+ return s_line;
+}
+int
+sock::read_http(socketcontainer *p_sock, char *c_zbuf, int i_buflen, int &i_postpayloadoffset)
+{
+ /*
+ ** 1) Read the first line
+ ** 2) If GET, handle as such
+ ** 3) If POST, handle as such
+ */
+ char ch;
+ int i_read;
+ int i_ret = -1;
+ int x,z;
+
+ string s_content_length;
+ string s_cl;
+ string s_post_return;
+ string s_line = read_http_line(p_sock);
+
+ i_postpayloadoffset = 0;
+ if(s_line.length() <= 0)
+ return -1;
+
+ /*
+ ** GET yada\r\n Followed by stuff we don't care about :) heh.
+ ** 01234
+ */
+ /*
+ ** POST yada\r\n
+ ** xxxxx
+ ** Content-Length: NNN\n
+ ** \n
+ */
+ if(s_line.substr(0,3) == "GET")
+ {
+ if(s_line.length() > i_buflen)
+ {
+ /*
+ ** Buffer overflow
+ */
+ return -1;
+ }
+ else
+ {
+ memcpy(c_zbuf,s_line.c_str(),s_line.length());
+ return s_line.length();
+ }
+ }
+
+ else
+ {
+ /*
+ ** POST yada
+ ** 01234
+ */
+ if(s_line.substr(0,4) != "POST")
+ return -1;
+
+ /*
+ ** Get us to the Content-Length:
+ */
+ s_post_return += s_line;
+ i_postpayloadoffset += s_line.length();
+
+ for(x=0 ;x < MAXLINES; x++)
+ {
+ s_line = read_http_line(p_sock);
+ s_post_return += s_line;
+ i_postpayloadoffset += s_line.length();
+
+ if (s_line.compare(0, 15, "Content-Length:"))
+ continue;
+
+ // Match found on Content-Length:... process, and then break out and get us to the promised land
+ s_content_length = s_line.substr( 16 /*strlen("Content-Length: ")*/,
+ s_line.length() - 16 /*strlen("Content-Length: ")*/);
+
+ /*
+ ** Content-Length: 333\n
+ ** 0123456789abcdefghijklmnopqrstuvwxyzAB
+ */
+
+ z = 0;
+
+ do
+ {
+ ch = s_content_length[z];
+ if(isdigit(ch))
+ s_cl += ch;
+
+ z++;
+
+ }
+ while(ch != '\n');
+
+ break;
+ }
+
+ if(s_cl.length() <= 0)
+ return -1;
+
+ z = atoi(s_cl.c_str());
+
+ /*
+ ** If we are going to overflow the buffer just by the payload, leave
+ ** of, if z did not convert correctly. (should have been ok by isdigit)
+ */
+ if(z > i_buflen || z < 0)
+ return -1;
+
+ /*
+ ** We have MAXLINES to get to the blank line separating POST data.
+ */
+ for(x=0 ;x < MAXLINES; x++)
+ {
+ s_line = read_http_line(p_sock);
+ s_post_return += s_line;
+
+ i_postpayloadoffset += s_line.length();
+ if(s_line == "\r\n")
+ break;
+ }
+
+ /*
+ ** funny business
+ */
+ if(x == MAXLINES)
+ return -1;
+
+ for(x=0; x < z; x++)
+ {
+ if(_read(p_sock,&ch,sizeof(ch)) != 1)
+ return -1;
+
+ s_post_return += ch;
+ }
+
+ if(s_post_return.length() > i_buflen)
+ return -1;
+
+ memcpy(c_zbuf,s_post_return.c_str(),s_post_return.length());
+ return s_post_return.length();
+ }
+}
+
+int
+sock::read_write(socketcontainer* p_sock)
+{
+ int i_postpayloadoffset;
+ int i_sock = p_sock->i_sock;
+
+ char c_req[READSOCK];
+
+ memset(c_req,0,sizeof(c_req));
+
+ int i_bytes = read_http(p_sock, c_req, READSOCK-1,i_postpayloadoffset);
+
+ if (i_bytes <= 0)
+ {
+ wrap::system_message( READERR );
+ }
+
+ else
+ {
+ // stores the request params.
+ map<string,string> map_params;
+
+ // get the s_rep ( s_html response which will be send imediatly to the client
+ struct sockaddr_in client;
+ size_t size = sizeof(client);
+
+ getpeername(i_sock, (struct sockaddr *)&client, &size);
+
+ uint32_t &s_addr = client.sin_addr.s_addr;
+ if ( (map_params["REMOTE_ADDR"] = get_elem(s_addr)) == "" )
+ {
+ map_params["REMOTE_ADDR"] = string(inet_ntoa(client.sin_addr));
+ set_elem(map_params["REMOTE_ADDR"], s_addr);
+ wrap::system_message(SOCKCAC+map_params["REMOTE_ADDR"]);
+ }
+
+ string s_rep = req_parser->parse(p_sock, string(c_req), map_params, i_postpayloadoffset);
+
+#ifdef LOGGING
+
+ log_daemon->log_access(map_params);
+#endif
+
+ // send s_rep to the client.
+ _send(p_sock, s_rep.c_str(), s_rep.size());
+
+ // dont need those vals anymore.
+ map_params.clear();
+
+ _close(p_sock);
+ return 0;
+ }
+
+ _close(p_sock);
+ return 1;
+}
+
+void
+sock::_main_loop_init()
+{
+ wrap::system_message(SOCKUNS);
+}
+
+#ifdef OPENSSL
+// This method is virtual, and is overloaded by sslsock!
+bool
+sock::_main_loop_do_ssl_stuff(int &i_new_sock)
+{
+ return 0;
+}
+#endif
+
+socketcontainer*
+sock::_create_container(int &i_sock)
+{
+ socketcontainer* p_sock = new socketcontainer;
+ p_sock->i_sock = i_sock;
+ return p_sock;
+}
+
+int
+sock::start()
+{
+ wrap::system_message( SOCKSRV );
+ pool* p_pool = wrap::POOL;
+ int i_sock = i_server_sock;
+
+#ifdef NCURSES
+
+ print_hits();
+ p_pool->print_pool_size();
+#endif
+
+ int i_port = tool::string2int( wrap::CONF->get_elem( "httpd.serverport" ) );
+ _main_loop_init();
+
+ int i;
+ fd_set active_fd_set, read_fd_set;
+ struct sockaddr_in clientname;
+ size_t size;
+
+ if (listen (i_sock, 1) < 0)
+ {
+ wrap::system_message( LISTERR );
+ exit( EXIT_FAILURE );
+ }
+
+ wrap::system_message( SOCKRDY );
+
+ // initialize the set of active sockets.
+ FD_ZERO (&active_fd_set);
+ FD_SET (i_sock, &active_fd_set);
+
+ while( b_run )
+ {
+ // block until input arrives on one or more active sockets.
+ read_fd_set = active_fd_set;
+ if (select (FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0)
+ {
+ wrap::system_message( SELCERR );
+
+ exit( EXIT_FAILURE );
+ }
+
+ // service all the sockets with input pending.
+ for ( i = 0; i < FD_SETSIZE; i++ )
+ if ( FD_ISSET (i, &read_fd_set) )
+ {
+ if ( i == i_sock )
+ {
+ // connection request on original socket.
+ ++i_req;
+
+#ifdef NCURSES
+
+ print_hits();
+#endif
+
+ int i_new_sock;
+ size = sizeof(clientname);
+ i_new_sock = accept (i_sock, (struct sockaddr *) &clientname, &size);
+
+#ifdef OPENSSL
+
+ if (_main_loop_do_ssl_stuff(i_new_sock))
+ continue;
+#endif
+
+#ifdef VERBOSE
+
+ wrap::system_message(NEWREQU
+ + tool::int2string(i_req) + " "
+ + string(inet_ntoa( clientname.sin_addr )) + ":"
+ + tool::int2string(ntohs ( clientname.sin_port ))
+ );
+#endif
+
+ FD_SET (i_new_sock, &active_fd_set);
+
+ }
+ else
+ {
+ socketcontainer *p_sock = _create_container(i);
+ p_pool->run( (void*) p_sock );
+ FD_CLR( i, &active_fd_set );
+ }
+ }
+ }
+}
+
+void
+sock::clean_ipcache()
+{
+ int i_ipcachesize = wrap::CONF->get_int("httpd.ipcachesize");
+ int i_currentsize = size();
+
+ if ( i_currentsize > 0 && (i_ipcachesize == 0 || i_ipcachesize <= i_currentsize) )
+ {
+ wrap::system_message(
+ SOCKCA2+tool::int2string(i_currentsize)+","+tool::int2string(i_ipcachesize)+")");
+ clear();
+ }
+}
+
+#ifdef NCURSES
+void
+sock::print_server_port() {
+ mvprintw( NCUR_PORT_X,NCUR_PORT_Y, "Port: %d ", i_server_port);
+ refresh();
+}
+
+void
+sock::print_hits()
+{
+ if ( wrap::NCUR->is_ready() )
+ {
+ mvprintw( NCUR_HITS_X,NCUR_HITS_Y, "Hits: %d ", i_req);
+ refresh();
+ }
+}
+#endif
+
+#endif
diff --git a/yhttpd/src/sock/sock.h b/yhttpd/src/sock/sock.h
new file mode 100644
index 0000000..72944e2
--- /dev/null
+++ b/yhttpd/src/sock/sock.h
@@ -0,0 +1,91 @@
+#include "../incl.h"
+
+#ifndef SOCK_H
+#define SOCK_H
+
+#include <queue>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+
+#include "../reqp.h"
+
+#include "../thrd/pool.h"
+#include "../maps/shashmap.h"
+
+#ifdef LOGGING
+#include "../logd.h"
+#endif
+
+using namespace std;
+
+class sock : public shashmap
+ < string, uint32_t, self_hash<uint32_t>, equals_allocator<uint32_t> >
+{
+protected:
+#ifdef LOGGING
+
+ logd *log_daemon; // the log daemon
+#endif
+
+ int i_server_sock;
+ int i_server_port;
+
+ // total number of server requests.
+ unsigned long long i_req;
+ bool b_run; // true while socket manager is running.
+ reqp *req_parser; // parses the http requests from clients.
+ char *c_buffer; // char buffer!
+ pthread_mutex_t mut_hits;
+
+ static string inet_ntoa_callback(void* p_void);
+
+public:
+ // creates a server socket.
+ int read_http(socketcontainer *p_sock, char *c_zbuf, int i_buflen, int &i_payloadoffset);
+ string read_http_line(socketcontainer *p_sock);
+
+ // small inline methods:
+ bool get_server_() const
+ {
+ return b_run;
+ }
+ // small inline methods:
+ bool get_run() const
+ {
+ return b_run;
+ }
+ bool set_run( bool b_run )
+ {
+ this->b_run = b_run;
+ }
+
+ sock();
+
+ int read_write( socketcontainer* p_sock );
+
+ int start();
+ void clean_ipcache();
+
+ // the chat stream there all the chat messages will sent through.
+ virtual int _send(socketcontainer *p_sock, const char *sz, int len);
+ virtual int _read(socketcontainer *p_sock, char *sz, int len);
+ virtual int _close(socketcontainer *p_sock);
+ virtual void _main_loop_init();
+#ifdef OPENSSL
+
+ virtual bool _main_loop_do_ssl_stuff(int& i_new_sock);
+#endif
+
+ virtual socketcontainer* _create_container(int& i_sock);
+ virtual int _make_server_socket(int i_port);
+
+#ifdef NCURSES
+ void print_server_port();
+ void print_hits();
+#endif
+
+};
+#endif
diff --git a/yhttpd/src/sock/sslsock.cpp b/yhttpd/src/sock/sslsock.cpp
new file mode 100644
index 0000000..32efc0f
--- /dev/null
+++ b/yhttpd/src/sock/sslsock.cpp
@@ -0,0 +1,141 @@
+#include "../incl.h"
+
+#ifdef OPENSSL
+#ifndef SSLSOCK_CPP
+#define SSLSOCK_CPP
+
+#include "sslsock.h"
+
+using namespace std;
+
+sslsock::sslsock() : sock()
+{
+ s_certificate_path = wrap::CONF->get_elem( "httpd.ssl.certificatepath" );
+ s_privatekey_path = wrap::CONF->get_elem( "httpd.ssl.privatekeypath" );
+ p_ctx = NULL;
+}
+
+int
+sslsock::_send(socketcontainer *p_sock, const char *sz, int len)
+{
+ return SSL_write((SSL*)p_sock->p_ssl_context,sz, len);
+}
+
+int
+sslsock::_read(socketcontainer *p_sock, char *sz, int len)
+{
+ return SSL_read((SSL*)p_sock->p_ssl_context,sz,len);
+}
+
+int
+sslsock::_close(socketcontainer *p_sock)
+{
+ SSL_free((SSL*)p_sock->p_ssl_context);
+ sock::_close(p_sock);
+}
+
+int
+sslsock::_make_server_socket(int i_port)
+{
+ SSL_METHOD *p_ssl_method;
+ unsigned long e;
+ char sz[1024];
+ string s_error;
+
+ int i_sock = sock::_make_server_socket(i_port);
+
+ if(i_sock <= 0)
+ {
+ wrap::system_message(SSLERR1);
+ return -1;
+ }
+
+ SSL_load_error_strings();
+ SSLeay_add_ssl_algorithms();
+
+ p_ssl_method = SSLv23_server_method();
+ p_ctx = SSL_CTX_new (p_ssl_method);
+ if (!p_ctx)
+ {
+ e = ERR_get_error();
+ ERR_error_string_n(e, sz, sizeof(sz) - 1);
+ s_error = sz;
+ wrap::system_message(SSLERR1);
+ return -1;
+ }
+
+ if (SSL_CTX_use_certificate_file(p_ctx, s_certificate_path.c_str(), SSL_FILETYPE_PEM) <= 0)
+ {
+ e = ERR_get_error();
+ ERR_error_string_n(e, sz, sizeof(sz) - 1);
+ s_error = sz;
+ wrap::system_message(SSLERR1);
+ return -1;
+ }
+
+ if (SSL_CTX_use_PrivateKey_file(p_ctx, s_privatekey_path.c_str(), SSL_FILETYPE_PEM) <= 0)
+ {
+ e = ERR_get_error();
+ ERR_error_string_n(e, sz, sizeof(sz) - 1);
+ s_error = sz;
+ wrap::system_message(SSLERR1);
+ return -1;
+ }
+
+ if (!SSL_CTX_check_private_key(p_ctx))
+ {
+ wrap::system_message(SSLERR2);
+ return -1;
+ }
+
+ return i_sock;
+}
+
+void
+sslsock::_main_loop_init()
+{
+ wrap::system_message(SOCKSEC);
+}
+
+bool
+sslsock::_main_loop_do_ssl_stuff(int& i_new_sock)
+{
+ SSL* p_ssl = SSL_new(p_ctx);
+
+ if( p_ssl == NULL || i_new_sock < 0)
+ {
+ wrap::system_message(SSLERR3);
+
+ close(i_new_sock);
+ if(p_ssl != NULL)
+ SSL_free(p_ssl);
+
+ return 1;
+ }
+
+ else
+ {
+ SSL_set_fd(p_ssl, i_new_sock);
+ if(SSL_accept(p_ssl) == -1)
+ {
+ wrap::system_message(SSLERR4);
+ close(i_new_sock);
+ return 1;
+ }
+
+ map_certs[i_new_sock] = p_ssl;
+ }
+
+ return 0;
+}
+
+socketcontainer*
+sslsock::_create_container(int &i_sock)
+{
+ socketcontainer* p_sock = sock::_create_container(i_sock);
+ p_sock->p_ssl_context = map_certs[i_sock];
+ return p_sock;
+}
+
+#endif
+#endif
diff --git a/yhttpd/src/sock/sslsock.h b/yhttpd/src/sock/sslsock.h
new file mode 100644
index 0000000..f5358cc
--- /dev/null
+++ b/yhttpd/src/sock/sslsock.h
@@ -0,0 +1,41 @@
+#include "../incl.h"
+
+#ifdef OPENSSL
+#ifndef SSLSOCK_H
+#define SSLSOCK_H
+
+#include "sock.h"
+
+#include <openssl/rsa.h>
+#include <openssl/crypto.h>
+#include <openssl/x509.h>
+#include <openssl/pem.h>
+#include <openssl/ssl.h>
+#include <openssl/err.h>
+
+using namespace std;
+
+class sslsock : public sock
+{
+private:
+ SSL_CTX* p_ctx;
+ string s_certificate_path;
+ string s_privatekey_path;
+ map<int,void*> map_certs;
+
+public:
+
+ sslsock( );
+ ~sslsock( );
+
+ int _send(socketcontainer *p_sock, const char *sz, int len);
+ int _read(socketcontainer *p_sock, char *sz, int len);
+ int _close(socketcontainer *p_sock);
+ void _main_loop_init();
+ bool _main_loop_do_ssl_stuff(int &i_new_sock);
+ socketcontainer* _create_container(int& i_sock);
+ int _make_server_socket(int i_port);
+};
+
+#endif
+#endif