2 Copyright (C) 2006 by Intra2net AG - Gerd v. Egidy
4 The software in this package is distributed under the GNU General
5 Public License version 2 (with a special exception described below).
7 A copy of GNU General Public License (GPL) is included in this distribution,
8 in the file COPYING.GPL.
10 As a special exception, if other files instantiate templates or use macros
11 or inline functions from this file, or you compile this file and link it
12 with other works to produce a work based on this file, this file
13 does not by itself cause the resulting work to be covered
14 by the GNU General Public License.
16 However the source code for this file must still be made available
17 in accordance with section (3) of the GNU General Public License.
19 This exception does not invalidate any other reasons why a work based
20 on this file might be covered by the GNU General Public License.
27 #include <sys/types.h>
28 #include <sys/socket.h>
31 #include <arpa/inet.h>
32 #include <netinet/in.h>
41 #include "socket_server.hxx"
42 #include "t2n_exception.hxx"
50 /** @brief create a new tcp-based server
51 @param port tcp port you want to listen on
52 @param ip the local ip you want to listen on. "0.0.0.0" means all local ips (default).
54 socket_server::socket_server(int port, const std::string& ip)
55 : server(), socket_handler(0,tcp_s)
57 /* Create the socket. */
58 sock = socket (PF_INET, SOCK_STREAM, 0);
60 EXCEPTIONSTREAM(error,t2n_server_error,"error opening socket: " << strerror(errno));
62 set_socket_options(sock);
64 /* Give the socket a name. */
65 struct sockaddr_in sockaddr;
66 sockaddr.sin_family = AF_INET;
67 sockaddr.sin_port = htons(port);
69 if (inet_aton(ip.c_str(),&sockaddr.sin_addr) == 0)
70 EXCEPTIONSTREAM(error,t2n_server_error,"failed listening on invalid ip " << ip);
72 if (bind (sock, (struct sockaddr *) &sockaddr, sizeof (sockaddr)) < 0)
74 // FIXME: Calls virtual function socket_server::get_logstream() in constructor
75 EXCEPTIONSTREAM(error,t2n_server_error,"error binding socket: " << strerror(errno));
81 /** @brief create a new unix-socked-based server
82 @param path path of the socket
83 @param filemode permissions you want to open the socket with
84 @param user local username for the socket
85 @param group local groupname for the socket
87 socket_server::socket_server(const std::string& path, mode_t filemode, const std::string& user, const std::string& group)
88 : server(), socket_handler(0,unix_s)
92 // TODO: Every EXCEPTIONSTREAM in here calls virtual function get_logstream()
94 /* Create the socket. */
95 sock = socket (PF_UNIX, SOCK_STREAM, 0);
97 EXCEPTIONSTREAM(error,t2n_server_error,"error opening socket: " << strerror(errno));
99 set_socket_options(sock);
101 /* Give the socket a name. */
102 struct sockaddr_un unix_name;
103 unix_name.sun_family = AF_UNIX;
104 strncpy (unix_name.sun_path, unix_path.c_str(),sizeof(unix_name.sun_path));
106 /* just to make sure there is no other socket file */
107 unlink (unix_name.sun_path);
109 if (bind (sock, (struct sockaddr *) &unix_name, sizeof (unix_name)) < 0)
110 EXCEPTIONSTREAM(error,t2n_server_error,"error binding socket: " << strerror(errno));
112 /* change permissions */
113 if (chmod (unix_name.sun_path, filemode) != 0)
114 EXCEPTIONSTREAM(error,t2n_server_error,"error changing permission: " << strerror(errno));
116 if (!user.empty() && !group.empty())
118 // TODO maybe use current user/group if one of them is empty
120 struct passwd *socket_user = getpwnam (user.c_str());
121 if (socket_user == NULL)
122 EXCEPTIONSTREAM(error,t2n_server_error,"error getting socket user: " << strerror(errno));
124 struct group *socket_group = getgrnam (group.c_str());
125 if (socket_group == NULL)
126 EXCEPTIONSTREAM(error,t2n_server_error,"error getting socket group: " << strerror(errno));
128 if (chown (unix_name.sun_path, socket_user->pw_uid, socket_group->gr_gid) != 0)
129 EXCEPTIONSTREAM(error,t2n_server_error,"error changing socket ownership: " << strerror(errno));
138 socket_server::~socket_server()
140 // close all client connections
143 // server socket will be closed by destructor of socket_handler
145 if (get_type()==unix_s)
146 unlink(unix_path.c_str());
148 // disconnect connection<->server pointer
149 std::map<unsigned int, server_connection*>::iterator it, it_end = connections.end();
150 for (it = connections.begin(); it != it_end; ++it)
152 socket_server_connection *conn = dynamic_cast<socket_server_connection*>(it->second);
154 conn->my_server = NULL;
158 /// start listening on a new server socket (called by the constructors)
159 void socket_server::start_listening()
161 if (listen (sock, 5) < 0)
162 EXCEPTIONSTREAM(error,t2n_server_error,"error listening to socket: " << strerror(errno));
164 /* clear & insert server sock into the fd_tab to prepare select */
165 FD_ZERO(&connection_set);
166 FD_SET (sock, &connection_set);
169 /// handle a new connection from a client
170 void socket_server::new_connection()
172 struct sockaddr_un clientname;
174 unsigned int size = sizeof (clientname);
175 int newsock = accept (sock,(struct sockaddr *) &clientname,&size);
178 // return on non-fatal errors (list taken from man-page)
179 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == ECONNABORTED || errno == EINTR ||
180 errno == EMFILE || errno == ENFILE || errno == ENOBUFS || errno == ENOMEM ||
181 errno == EPROTO || errno == EPERM || errno == ETIMEDOUT)
183 LOGSTREAM(error,"non-fatal accept error: " << strerror(errno));
187 /* fatal error: will usually kill or restart the server */
188 EXCEPTIONSTREAM(error,t2n_server_error,"fatal error accepting connection: " << strerror(errno));
191 FD_SET (newsock, &connection_set);
193 socket_server_connection *nc=new socket_server_connection(newsock, get_type(), get_default_timeout());
194 nc->set_socket_options(newsock);
201 /** @brief look for new connections and new data in any of the existing connections
202 @param usec_timeout wait until new data is found, max timeout usecs.
205 @param usec_timeout_remaining if non-NULL the function will write the
206 not used time to the given target
207 @retval true if new data was found (does not mean that the received data
208 is a complete packet though)
210 bool socket_server::fill_buffer(long long usec_timeout,long long* usec_timeout_remaining)
212 fd_set used_fdset=connection_set;
216 struct timeval *timeout_ptr;
218 if (usec_timeout == -1)
224 // timeout von long long usec in int sec + int usec umrechnen
225 tval.tv_sec = usec_timeout / 1000000;
226 tval.tv_usec = usec_timeout % 1000000;
229 int ret=select (FD_SETSIZE, &used_fdset, NULL, NULL, timeout_ptr);
231 // return the timeout we did not use
232 if (usec_timeout > 0 && usec_timeout_remaining != NULL)
233 *usec_timeout_remaining=(tval.tv_sec*1000000)+tval.tv_usec;
239 // select interrupted by signal
243 EXCEPTIONSTREAM(error,t2n_server_error,"select error: " << strerror(errno));
248 // we have data pending
250 // check for new connection
251 if (FD_ISSET (sock, &used_fdset))
256 // check all connections for pending data
257 return fill_connection_buffers();
263 /// call fill_buffer() on all connections, called from fill_buffer()
264 bool socket_server::fill_connection_buffers()
266 bool data_found = false;
268 std::map<unsigned int, server_connection*>::iterator ie=connections.end();
269 for(std::map<unsigned int, server_connection*>::iterator i=connections.begin(); i != ie; i++)
270 if (!i->second->server_connection::is_closed())
272 // shutdown all connections which throw exceptions to protect the server
275 if (i->second->fill_buffer(0))
278 catch (t2n_transfer_error &e)
279 { i->second->close(); }
287 /// remove the socket of a connection after the connection has been closed
288 void socket_server::remove_connection_socket(int sock)
290 FD_CLR(sock, &connection_set);
296 socket_server_connection::~socket_server_connection()
298 // Only notify parent server about going down.
299 // The real socket will be closed by the destructor of the base classes.
300 if (my_server && sock != -1)
302 socket_server *srv = dynamic_cast<socket_server*>(my_server);
304 srv->remove_connection_socket(sock);
308 /// close this connection. complete data waiting in the buffer can still be retrieved.
309 void socket_server_connection::close()
311 if (my_server && sock != -1)
313 socket_server *srv = dynamic_cast<socket_server*>(my_server);
315 srv->remove_connection_socket(sock);
318 if (!server_connection::is_closed())
320 socket_handler::close();
321 server_connection::close();