/*************************************************************************** * Copyright (C) 2006 by Gerd v. Egidy * * gve@intra2net.com * * * * This library is free software; you can redistribute it and/or modify * * it under the terms of the GNU Lesser General Public License version * * 2.1 as published by the Free Software Foundation. * * * * This library is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU Lesser General Public License for more details. * * * * You should have received a copy of the GNU Lesser General Public * * License along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "socket_server.hxx" #include "t2n_exception.hxx" using namespace std; namespace libt2n { socket_server::socket_server(int port, const std::string& ip) : server(), socket_handler(0,tcp_s) { // TODO } socket_server::socket_server(const std::string& path, mode_t filemode, const std::string& user, const std::string& group) : server(), socket_handler(0,unix_s) { unix_path=path; /* Create the socket. */ sock = socket (PF_UNIX, SOCK_STREAM, 0); if (sock < 0) { string err="error opening socket: "; err+=strerror(errno); log(error, err); throw t2n_server_error(err); } set_socket_options(sock); /* Give the socket a name. */ struct sockaddr_un unix_name; unix_name.sun_family = AF_UNIX; strncpy (unix_name.sun_path, unix_path.c_str(),sizeof(unix_name.sun_path)); /* just to make sure there is no other socket file */ unlink (unix_name.sun_path); if (bind (sock, (struct sockaddr *) &unix_name, sizeof (unix_name)) < 0) { string err="error binding socket: "; err+=strerror(errno); log(error, err); throw t2n_server_error(err); } /* change permissions */ if (chmod (unix_name.sun_path, filemode) != 0) { string err="error changing permission: "; err+=strerror(errno); log(error, err); throw t2n_server_error(err); } if (!user.empty() && !group.empty()) { // TODO maybe use current user/group if one of them is empty struct passwd *socket_user = getpwnam (user.c_str()); if (socket_user == NULL) { string err="error getting socket user: "; err+=strerror(errno); log(error, err); throw t2n_server_error(err); } struct group *socket_group = getgrnam (group.c_str()); if (socket_group == NULL) { string err="error getting socket group: "; err+=strerror(errno); log(error, err); throw t2n_server_error(err); } if (chown (unix_name.sun_path, socket_user->pw_uid, socket_group->gr_gid) != 0) { string err="error changing socket ownership: "; err+=strerror(errno); log(error, err); throw t2n_server_error(err); } } if (listen (sock, 5) < 0) { string err="error listening to socket: "; err+=strerror(errno); log(error, err); throw t2n_server_error(err); } /* clear & insert server sock into the fd_tab to prepare select */ FD_ZERO(&connection_set); FD_SET (sock, &connection_set); } socket_server::~socket_server() { socket_handler::close(); if (get_type()==unix_s) unlink(unix_path.c_str()); } void socket_server::new_connection() { struct sockaddr_un clientname; unsigned int size = sizeof (clientname); int newsock = accept (sock,(struct sockaddr *) &clientname,&size); if (newsock < 0) { if (errno == EAGAIN) { log(error, "accept error (EAGAIN): no connection waiting"); return; } /* default: break */ string err="error accepting connection: "; err+=strerror(errno); log(error, err); throw t2n_server_error(err); } FD_SET (newsock, &connection_set); socket_server_connection *nc=new socket_server_connection(newsock, get_type(), get_default_timeout()); nc->set_socket_options(newsock); add_connection(nc); return; } void socket_server::fill_buffer(long long usec_timeout) { fd_set used_fdset=connection_set; /* set timeout */ struct timeval tval; struct timeval *timeout_ptr; if (usec_timeout == -1) timeout_ptr = NULL; else { timeout_ptr = &tval; // timeout von long long usec in int sec + int usec umrechnen tval.tv_sec = usec_timeout / 1000000; tval.tv_usec = usec_timeout % 1000000; } int ret=select (FD_SETSIZE, &used_fdset, NULL, NULL, timeout_ptr); if (ret < 0) { if (errno == EINTR) { // select interrupted by signal ret=0; } else { string err="select error: "; err+=strerror(errno); log(error, err); throw t2n_server_error(err); } } if (ret > 0) { // we have data pending // check for new connection if (FD_ISSET (sock, &used_fdset)) { new_connection(); } // check all connections for pending data fill_connection_buffers(); } return; } void socket_server::fill_connection_buffers() { std::map::iterator ie=connections.end(); for(std::map::iterator i=connections.begin(); i != ie; i++) if (!i->second->server_connection::is_closed()) i->second->fill_buffer(); } void socket_server::remove_connection_socket(int sock) { FD_CLR(sock, &connection_set); } void socket_server_connection::log(log_level_values level, const char* message) { if(my_server) { ostringstream msg; msg << "connection id " << get_id() << ": " << message; my_server->log(level,msg.str().c_str()); } } void socket_server_connection::close() { if (!server_connection::is_closed()) { socket_handler::close(); server_connection::close(); } if (my_server) { dynamic_cast(my_server)->remove_connection_socket(sock); } } }