diff options
| author | Paul Buetow <paul@buetow.org> | 2013-04-06 13:14:42 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2013-04-06 13:14:42 +0200 |
| commit | 658bfd183905576ed5f4dbe57a2ee82711b6ac93 (patch) | |
| tree | 08cf25d1a1fb09b643a65c5b237c52bce791f307 /src/sock/sock.cpp | |
| parent | 42b79aa5c591dde88e78922a519802f948d9ea60 (diff) | |
tagging ychat-0.7.9.0ychat-0.7.9.0
Diffstat (limited to 'src/sock/sock.cpp')
| -rwxr-xr-x | src/sock/sock.cpp | 321 |
1 files changed, 66 insertions, 255 deletions
diff --git a/src/sock/sock.cpp b/src/sock/sock.cpp index 8f66268..42daf24 100755 --- a/src/sock/sock.cpp +++ b/src/sock/sock.cpp @@ -5,8 +5,9 @@ #include <errno.h> #include <sys/types.h> #include <unistd.h> - #include "sock.h" +#include "../chat/chat.h" +#include "../chat/user.h" using namespace std; @@ -19,40 +20,21 @@ sock::sock() this->log_daemon = new logd( wrap::CONF->get_elem( "httpd.logging.accessfile" ), - wrap::CONF->get_elem( "httpd.logging.access_lines" ) ); + wrap::CONF->get_elem( "httpd.logging.accesslines" ) ); #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; -} +sock::~sock() +{} //<<* void -sock::chat_stream( socketcontainer *p_sock, user *p_user, map<string,string> &map_params ) +sock::chat_stream( int i_sock, user *p_user, map<string,string> &map_params ) { string s_msg( "\n" ); for ( int i = 0; i < PUSHSTR; i++ ) - _send(p_sock,s_msg.c_str(), s_msg.size()); + send( i_sock, s_msg.c_str(), s_msg.size(), 0 ); pthread_mutex_t mutex; pthread_mutex_init( &mutex, NULL ); @@ -62,7 +44,7 @@ sock::chat_stream( socketcontainer *p_sock, user *p_user, map<string,string> &ma { s_msg = p_user->get_mess( ); - if ( 0 > _send( p_sock, s_msg.c_str(), s_msg.size() ) ) + if ( 0 > send( i_sock, s_msg.c_str(), s_msg.size(), 0 ) ) { p_user->set_online( false ); break; @@ -78,7 +60,7 @@ sock::chat_stream( socketcontainer *p_sock, user *p_user, map<string,string> &ma s_msg = p_user->get_mess( ); if ( ! s_msg.empty() ) - _send( p_sock, s_msg.c_str(), s_msg.size()); + send( i_sock, s_msg.c_str(), s_msg.size(), 0 ); // remove the user from its room. string s_user( p_user->get_name() ); @@ -105,7 +87,7 @@ sock::chat_stream( socketcontainer *p_sock, user *p_user, map<string,string> &ma //*>> int -sock::_make_server_socket( int i_port ) +sock::make_server_socket( int i_port ) { size_t i_sock; struct sockaddr_in name; @@ -121,7 +103,7 @@ sock::_make_server_socket( int i_port ) wrap::system_message( SOCKERR ); - return _make_server_socket( i_port ); + return make_server_socket( i_port ); } // give the server socket a name. @@ -143,200 +125,26 @@ sock::_make_server_socket( int i_port ) wrap::system_message( string(SOCKERR) + tool::int2string(i_port) ); // Rerun recursive. - return _make_server_socket( i_port ); + 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; - } +#ifdef NCURSES - if(s_post_return.length() > i_buflen) - return -1; + mvprintw( NCUR_PORT_X,NCUR_PORT_Y, "Port: %d ", i_port); + refresh(); +#endif - memcpy(c_zbuf,s_post_return.c_str(),s_post_return.length()); - return s_post_return.length(); - } + return i_sock; } int -sock::read_write(socketcontainer* p_sock) +sock::read_write( int* p_sock ) { - int i_postpayloadoffset; - int i_sock = p_sock->i_sock; - + int i_sock = *p_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); + int i_bytes = read(i_sock, c_req, READSOCK); if (i_bytes <= 0) { @@ -352,17 +160,25 @@ sock::read_write(socketcontainer* p_sock) struct sockaddr_in client; size_t size = sizeof(client); - getpeername(i_sock, (struct sockaddr *)&client, &size); +#ifdef CYGWIN + getpeername( i_sock, (struct sockaddr *)&client, (int*)&size); +#else + getpeername( i_sock, (struct sockaddr *)&client, &size); +#endif + uint32_t &s_addr = client.sin_addr.s_addr; - if ( (map_params["REMOTE_ADDR"] = get_elem(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); + //map_params["REMOTE_ADDR"] = inet_ntoa_callback(&client.sin_addr); + //map_params["REMOTE_PORT"] = ntohs(client.sin_port); + + string s_rep = req_parser->parse(i_sock, string(c_req), map_params); #ifdef LOGGING @@ -370,31 +186,21 @@ sock::read_write(socketcontainer* p_sock) #endif // send s_rep to the client. - _send(p_sock, s_rep.c_str(), s_rep.size()); + send(i_sock, s_rep.c_str(), s_rep.size(), 0); // dont need those vals anymore. map_params.clear(); - _close(p_sock); + shutdown( i_sock, 2 ); + close ( i_sock ); + return 0; } - _close(p_sock); - return 1; -} - -void -sock::_main_loop_init() -{ - wrap::system_message(SOCKUNS); -} + shutdown( i_sock, 2 ); + close ( i_sock ); -socketcontainer* -sock::_create_container(int &i_sock) -{ - socketcontainer* p_sock = new socketcontainer; - p_sock->i_sock = i_sock; - return p_sock; + return 1; } int @@ -402,22 +208,22 @@ 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; + int i_sock, i; fd_set active_fd_set, read_fd_set; struct sockaddr_in clientname; size_t size; + + // create the server socket and set it up to accept connections. + i_sock = make_server_socket ( i_port ); + if (listen (i_sock, 1) < 0) { wrap::system_message( LISTERR ); @@ -430,8 +236,6 @@ sock::start() FD_ZERO (&active_fd_set); FD_SET (i_sock, &active_fd_set); - print_server_port(); - while( b_run ) { // block until input arrives on one or more active sockets. @@ -459,23 +263,36 @@ sock::start() int i_new_sock; size = sizeof(clientname); - i_new_sock = accept (i_sock, (struct sockaddr *) &clientname, &size); +#ifdef CYGWIN -#ifdef VERBOSE + i_new_sock = accept (i_sock, (struct sockaddr *) &clientname, (int*)&size); +#else - wrap::system_message(NEWREQU - + tool::int2string(i_req) + " " - + string(inet_ntoa( clientname.sin_addr )) + ":" - + tool::int2string(ntohs ( clientname.sin_port )) - ); + i_new_sock = accept (i_sock, (struct sockaddr *) &clientname, &size); #endif - FD_SET (i_new_sock, &active_fd_set); + if (i_new_sock < 0) + { + wrap::system_message( ACCPERR ); + close(i_new_sock); + } + else + { +#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); + int *p_sock = new int; + *p_sock = i; p_pool->run( (void*) p_sock ); FD_CLR( i, &active_fd_set ); } @@ -492,19 +309,13 @@ sock::clean_ipcache() if ( i_currentsize > 0 && (i_ipcachesize == 0 || i_ipcachesize <= i_currentsize) ) { wrap::system_message( - SOCKCA2+tool::int2string(i_currentsize)+","+tool::int2string(i_ipcachesize)+")"); + 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() ) |
