diff --git a/epoll.cxx b/epoll.cxx index 1bfbce1..31e4f3b 100644 --- a/epoll.cxx +++ b/epoll.cxx @@ -7,6 +7,7 @@ */ #include "epoll.hxx" +#include "xerxes.hxx" namespace xerxes { @@ -15,7 +16,10 @@ namespace xerxes if(fd < 0) { throw EpollCreateErr(); - //throw std::runtime_error("could not create epoll descriptor."); + } + if(be_debug) + { + std::cout << "epoll fd created" << std::endl; } } @@ -25,6 +29,10 @@ namespace xerxes { close(fd); } + if(be_debug) + { + std::cout << "epoll closed" << std::endl; + } } void @@ -43,6 +51,10 @@ namespace xerxes throw EpollAddErr(EPOLL_ADD_ERR_SOURCE); if(0 != epoll_ctl(fd, EPOLL_CTL_ADD, target.fd, events[target.fd].get())) throw EpollAddErr(EPOLL_ADD_ERR_TARGET); + if(be_debug) + { + std::cout << "fd " << target.fd << " and fd " << source.fd << " added to epoll" << std::endl; + } } void @@ -54,6 +66,10 @@ namespace xerxes if(0 != epoll_ctl(fd, EPOLL_CTL_ADD, socket.fd, events[socket.fd].get())) throw EpollAddErr(); + if(be_debug) + { + std::cout << "fd " << socket.fd << " added to epoll" << std::endl; + } } void @@ -62,6 +78,10 @@ namespace xerxes if(0 != epoll_ctl(fd, EPOLL_CTL_DEL, socket.fd, 0)) throw EpollAddErr(); events.erase(socket.fd); + if(be_debug) + { + std::cout << "fd " << socket.fd << " deleted from epoll" << std::endl; + } } EpollErr::EpollErr() @@ -71,8 +91,11 @@ namespace xerxes EpollErr::EpollErr(std::string err) { - std::cerr << "Epoll Error: " << err << std::endl; - perror("ERRNO"); + if(!be_quiet) + { + std::cerr << "Epoll Error: " << err << std::endl; + perror("ERRNO"); + } } EpollAddErr::EpollAddErr() { diff --git a/socket.cxx b/socket.cxx index eee6ca3..e676f0a 100644 --- a/socket.cxx +++ b/socket.cxx @@ -12,6 +12,7 @@ #include #include #include "socket.hxx" +#include "xerxes.hxx" namespace xerxes { @@ -24,6 +25,10 @@ namespace xerxes { throw ConnCreateErr(); } + if(be_debug) + { + std::cout << "socket with fd " << fd << " created" << std::endl; + } } Socket::Socket(int new_fd) @@ -40,6 +45,10 @@ namespace xerxes if(fd) { close(fd); + if(be_debug) + { + std::cout << "socket with fd " << fd << " closed" << std::endl; + } } } @@ -100,6 +109,10 @@ namespace xerxes MysqlData makeData(int len) { + if(be_debug) + { + std::cout << "Init transfer Buffer" << std::endl; + } return MysqlData(buffer_t(new char[len]), len); } @@ -111,6 +124,10 @@ namespace xerxes { throw ConnListenErr(); } + if(be_debug) + { + std::cout << "listen on socket" << std::endl; + } return ret; } @@ -124,6 +141,10 @@ namespace xerxes { throw ConnAcceptErr(); } + if(be_debug) + { + std::cout << "connection accepted" << std::endl; + } return boost::shared_ptr(new Socket(new_fd)); } @@ -138,6 +159,10 @@ namespace xerxes { throw ConnConnectErr(); } + if(be_debug) + { + std::cout << "socket connected" << std::endl; + } return ret; } @@ -145,6 +170,10 @@ namespace xerxes connect_inet(Socket& socket, SocketOption& opt) { + if(be_debug) + { + std::cout << "connect inet to: " << opt.hostname << ":" << opt.port << std::endl; + } addrinfo hints; addrinfo* res; @@ -164,8 +193,11 @@ namespace xerxes connect_unix(Socket& socket, SocketOption& opt) { + if(be_debug) + { + std::cout << "connect unix to: " << opt.file << std::endl; + } struct sockaddr_un adr; - memset(&adr, 0, sizeof(adr)); adr.sun_family = AF_UNIX; strncpy(adr.sun_path, opt.file.c_str(), sizeof(adr.sun_path)); @@ -186,6 +218,11 @@ namespace xerxes { throw ConDataErr(); } + if(be_debug) + { + std::cout << "recived " << ret << " bytes from fd " << socket.fd << std::endl; + } + return ret; } @@ -196,7 +233,7 @@ namespace xerxes int flags) { int ret = ::send(socket.fd, data.first.get(), len, flags); - if(ret == 0 && len != 0) + if((ret == 0 && len != 0) || len != ret) { throw ConResetErr(); } @@ -204,6 +241,11 @@ namespace xerxes { throw ConDataErr(); } + + if(be_debug) + { + std::cout << "sended " << ret << " of " << len << " bytes to " << socket.fd << std::endl; + } return ret; } @@ -217,6 +259,12 @@ namespace xerxes { throw ConnBindErr(); } + + if(be_debug) + { + std::cout << "socket bound" << std::endl; + } + return ret; } @@ -224,6 +272,10 @@ namespace xerxes bind_inet(Socket& socket, SocketOption& opt) { + if(be_debug) + { + std::cout << "bind inet socket to: " << opt.hostname << ":" << opt.port << std::endl; + } addrinfo hints; addrinfo* res; @@ -250,10 +302,14 @@ namespace xerxes bind_unix(Socket& socket, SocketOption& opt) { + if(be_debug) + { + std::cout << "bind unix socket to: " << opt.file << std::endl; + } + unlink(opt.file.c_str()); struct sockaddr_un adr; - memset(&adr, 0, sizeof(adr)); adr.sun_family = AF_UNIX; strncpy(adr.sun_path, opt.file.c_str(), sizeof(adr.sun_path)); @@ -264,7 +320,10 @@ namespace xerxes SocketErr("unknown"); } SocketErr::SocketErr(std::string err){ - std::cerr << "Socket Exception: " << err << std::endl; - perror("ERRNO"); + if(!be_quiet) + { + std::cerr << "Socket Exception: " << err << std::endl; + perror("ERRNO"); + } } } diff --git a/xerxes.cxx b/xerxes.cxx index 8045059..3beb04f 100644 --- a/xerxes.cxx +++ b/xerxes.cxx @@ -7,8 +7,7 @@ */ /** - * TODO : Unix Socket testing - * TODO : Do some Error handling + * TODO : Closing of dockets does not work korrect */ #include @@ -22,7 +21,8 @@ #include - +int be_quiet = 0; +int be_debug = 0; int main(int argc, char* argv[]) @@ -36,6 +36,8 @@ main(int argc, char* argv[]) po::options_description desc("Allowed options"); desc.add_options() ("help", "produce help message") + ("quiet", "be quiet") + ("debug", "write a lot of stupid debug messages") ("src", po::value(), "Source") ("dst", po::value(), "Destination") ; @@ -50,6 +52,15 @@ main(int argc, char* argv[]) return 1; } + if (vm.count("quiet")) + { + be_quiet = 1; + } + + if (vm.count("debug")) + { + be_debug = 1; + } if (vm.count("src")) { @@ -59,14 +70,17 @@ main(int argc, char* argv[]) if (vm.count("dst")) { SocketOption sock = vm["dst"].as(); - if(sock.type == TCP) + if(!be_quiet) { - cout << "TCP Destination is " << sock.hostname << "," << sock.port << ".\n"; - } - else - { - cout << "UNIX Destination is " << sock.file << ".\n"; - } + if(sock.type == TCP) + { + cout << "TCP Destination is " << sock.hostname << "," << sock.port << ".\n"; + } + else + { + cout << "UNIX Destination is " << sock.file << ".\n"; + } + } } else { @@ -79,12 +93,18 @@ main(int argc, char* argv[]) if(src.type == TCP) { - cout << "TCP Source is " << src.hostname << "," << src.port << ".\n"; + if(!be_quiet) + { + cout << "TCP Source is " << src.hostname << "," << src.port << ".\n"; + } bind_inet(lstn, src); } else { - cout << "UNIX Source is " << src.file << ".\n"; + if(!be_quiet) + { + cout << "UNIX Source is " << src.file << ".\n"; + } bind_unix(lstn, src); } @@ -103,11 +123,24 @@ main(int argc, char* argv[]) { int num = epoll_wait(epoll.fd, events.get(), max_events, -1); + if(be_debug) + { + cout << "epoll events available (" << num << ")" << endl; + } + for(int i = 0; i < num; ++i) { + if(be_debug) + { + cout << "process event " << i << endl; + } if((events[i].data.fd == -1) && (events[i].events & EPOLLIN)) { + if(be_debug) + { + cout << "EPOLLIN on listening socket .. create a new connection" << endl; + } boost::shared_ptr target; boost::shared_ptr source; try @@ -156,23 +189,32 @@ main(int argc, char* argv[]) sockets.erase(source->fd); continue; } + if(be_debug) + { + cout << "new connection created" << endl; + } } else { if(sockets[events[i].data.fd] == 0) { -// cerr << "already closed, ignore" << endl; + if(be_debug) + { + cout << "fd " << events[i].data.fd << " already closed, ignore event " << i << endl; + } continue; } - //lookup + boost::shared_ptr target(sockets[events[i].data.fd]); boost::shared_ptr source(sockets[epoll.events[target->fd]->data.fd]); + if((events[i].events & EPOLLIN ) || (events[i].events & EPOLLPRI)) { - // read -> write - cout << "writer: "<< source->fd << endl; - cout << "reader: "<< target->fd << endl; + if(be_debug) + { + cout << "EPOLLIN or EPOLLPRI event from fd " << source->fd << " target is fd " << target->fd << endl; + } try { int len = recv(*source, buffer, 0); @@ -181,21 +223,24 @@ main(int argc, char* argv[]) } catch (SocketErr e) { - // hangup -// cout << "Socket Error, closing " << target->fd << " and " << source->fd << endl; + if(be_debug) + { + cout << "Socket Error, closing socket" << target->fd << " and " << source->fd << endl; + } epoll.del(target->fd); epoll.del(source->fd); sockets.erase(target->fd); sockets.erase(source->fd); -// cerr << "closed" << endl; continue; } } if((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP)) { - // hangup -// cout << target->fd << " closed by " << source->fd << endl; + if(be_debug) + { + cout << "EPOLLERR or EPOLLHUP event from fd " << source->fd << " target is fd " << target->fd << " close both" << endl; + } epoll.del(target->fd); epoll.del(source->fd); sockets.erase(target->fd);