Add missing include for chmod()
[libt2n] / src / socket_server.cpp
... / ...
CommitLineData
1/*
2Copyright (C) 2006 by Intra2net AG - Gerd v. Egidy
3
4The software in this package is distributed under the GNU General
5Public License version 2 (with a special exception described below).
6
7A copy of GNU General Public License (GPL) is included in this distribution,
8in the file COPYING.GPL.
9
10As a special exception, if other files instantiate templates or use macros
11or inline functions from this file, or you compile this file and link it
12with other works to produce a work based on this file, this file
13does not by itself cause the resulting work to be covered
14by the GNU General Public License.
15
16However the source code for this file must still be made available
17in accordance with section (3) of the GNU General Public License.
18
19This exception does not invalidate any other reasons why a work based
20on this file might be covered by the GNU General Public License.
21*/
22
23#include <stdio.h>
24#include <errno.h>
25#include <stdlib.h>
26#include <unistd.h>
27#include <sys/types.h>
28#include <sys/socket.h>
29#include <sys/un.h>
30#include <sys/time.h>
31#include <sys/stat.h>
32#include <arpa/inet.h>
33#include <netinet/in.h>
34#include <netdb.h>
35#include <fcntl.h>
36#include <time.h>
37#include <pwd.h>
38#include <grp.h>
39
40#include <sstream>
41
42#include "socket_server.hxx"
43#include "t2n_exception.hxx"
44#include "log.hxx"
45
46using namespace std;
47
48namespace libt2n
49{
50
51/** @brief create a new tcp-based server
52 @param port tcp port you want to listen on
53 @param ip the local ip you want to listen on. "0.0.0.0" means all local ips (default).
54*/
55socket_server::socket_server(int port, const std::string& ip)
56 : server(), socket_handler(0,tcp_s)
57{
58 /* Create the socket. */
59 sock = socket (PF_INET, SOCK_STREAM, 0);
60 if (sock < 0)
61 EXCEPTIONSTREAM(error,t2n_server_error,"error opening socket: " << strerror(errno));
62
63 set_socket_options(sock);
64
65 /* Give the socket a name. */
66 struct sockaddr_in sockaddr;
67 sockaddr.sin_family = AF_INET;
68 sockaddr.sin_port = htons(port);
69
70 if (inet_aton(ip.c_str(),&sockaddr.sin_addr) == 0)
71 EXCEPTIONSTREAM(error,t2n_server_error,"failed listening on invalid ip " << ip);
72
73 if (bind (sock, (struct sockaddr *) &sockaddr, sizeof (sockaddr)) < 0)
74 {
75 // FIXME: Calls virtual function socket_server::get_logstream() in constructor
76 EXCEPTIONSTREAM(error,t2n_server_error,"error binding socket: " << strerror(errno));
77 }
78
79 start_listening();
80}
81
82/** @brief create a new unix-socked-based server
83 @param path path of the socket
84 @param filemode permissions you want to open the socket with
85 @param user local username for the socket
86 @param group local groupname for the socket
87*/
88socket_server::socket_server(const std::string& path, mode_t filemode, const std::string& user, const std::string& group)
89 : server(), socket_handler(0,unix_s)
90{
91 unix_path=path;
92
93 // TODO: Every EXCEPTIONSTREAM in here calls virtual function get_logstream()
94
95 /* Create the socket. */
96 sock = socket (PF_UNIX, SOCK_STREAM, 0);
97 if (sock < 0)
98 EXCEPTIONSTREAM(error,t2n_server_error,"error opening socket: " << strerror(errno));
99
100 set_socket_options(sock);
101
102 /* Give the socket a name. */
103 struct sockaddr_un unix_name;
104 unix_name.sun_family = AF_UNIX;
105 strncpy (unix_name.sun_path, unix_path.c_str(),sizeof(unix_name.sun_path));
106
107 /* just to make sure there is no other socket file */
108 unlink (unix_name.sun_path);
109
110 if (bind (sock, (struct sockaddr *) &unix_name, sizeof (unix_name)) < 0)
111 EXCEPTIONSTREAM(error,t2n_server_error,"error binding socket: " << strerror(errno));
112
113 /* change permissions */
114 if (chmod (unix_name.sun_path, filemode) != 0)
115 EXCEPTIONSTREAM(error,t2n_server_error,"error changing permission: " << strerror(errno));
116
117 if (!user.empty() && !group.empty())
118 {
119 // TODO maybe use current user/group if one of them is empty
120
121 struct passwd *socket_user = getpwnam (user.c_str());
122 if (socket_user == NULL)
123 EXCEPTIONSTREAM(error,t2n_server_error,"error getting socket user: " << strerror(errno));
124
125 struct group *socket_group = getgrnam (group.c_str());
126 if (socket_group == NULL)
127 EXCEPTIONSTREAM(error,t2n_server_error,"error getting socket group: " << strerror(errno));
128
129 if (chown (unix_name.sun_path, socket_user->pw_uid, socket_group->gr_gid) != 0)
130 EXCEPTIONSTREAM(error,t2n_server_error,"error changing socket ownership: " << strerror(errno));
131 }
132
133 start_listening();
134}
135
136/**
137 * Destructor
138 */
139socket_server::~socket_server()
140{
141 // close all client connections
142 server::close();
143
144 // server socket will be closed by destructor of socket_handler
145
146 if (get_type()==unix_s)
147 unlink(unix_path.c_str());
148
149 // disconnect connection<->server pointer
150 std::map<unsigned int, server_connection*>::iterator it, it_end = connections.end();
151 for (it = connections.begin(); it != it_end; ++it)
152 {
153 socket_server_connection *conn = dynamic_cast<socket_server_connection*>(it->second);
154 if (conn)
155 conn->my_server = NULL;
156 }
157}
158
159/// start listening on a new server socket (called by the constructors)
160void socket_server::start_listening()
161{
162 if (listen (sock, 5) < 0)
163 EXCEPTIONSTREAM(error,t2n_server_error,"error listening to socket: " << strerror(errno));
164
165 /* clear & insert server sock into the fd_tab to prepare select */
166 FD_ZERO(&connection_set);
167 FD_SET (sock, &connection_set);
168}
169
170/// handle a new connection from a client
171void socket_server::new_connection()
172{
173 struct sockaddr_un clientname;
174
175 unsigned int size = sizeof (clientname);
176 int newsock = accept (sock,(struct sockaddr *) &clientname,&size);
177 if (newsock < 0)
178 {
179 // return on non-fatal errors (list taken from man-page)
180 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == ECONNABORTED || errno == EINTR ||
181 errno == EMFILE || errno == ENFILE || errno == ENOBUFS || errno == ENOMEM ||
182 errno == EPROTO || errno == EPERM || errno == ETIMEDOUT)
183 {
184 LOGSTREAM(error,"non-fatal accept error: " << strerror(errno));
185 return;
186 }
187
188 /* fatal error: will usually kill or restart the server */
189 EXCEPTIONSTREAM(error,t2n_server_error,"fatal error accepting connection: " << strerror(errno));
190 }
191
192 FD_SET (newsock, &connection_set);
193
194 socket_server_connection *nc=new socket_server_connection(newsock, get_type(), get_default_timeout());
195 nc->set_socket_options(newsock);
196
197 add_connection(nc);
198
199 return;
200}
201
202/** @brief look for new connections and new data in any of the existing connections
203 @param usec_timeout wait until new data is found, max timeout usecs.
204 -1: wait endless
205 0: return instantly
206 @param usec_timeout_remaining if non-NULL the function will write the
207 not used time to the given target
208 @retval true if new data was found (does not mean that the received data
209 is a complete packet though)
210*/
211bool socket_server::fill_buffer(long long usec_timeout,long long* usec_timeout_remaining)
212{
213 fd_set used_fdset=connection_set;
214
215 /* set timeout */
216 struct timeval tval;
217 struct timeval *timeout_ptr;
218
219 if (usec_timeout == -1)
220 timeout_ptr = NULL;
221 else
222 {
223 timeout_ptr = &tval;
224
225 // timeout von long long usec in int sec + int usec umrechnen
226 tval.tv_sec = usec_timeout / 1000000;
227 tval.tv_usec = usec_timeout % 1000000;
228 }
229
230 int ret=select (FD_SETSIZE, &used_fdset, NULL, NULL, timeout_ptr);
231
232 // return the timeout we did not use
233 if (usec_timeout > 0 && usec_timeout_remaining != NULL)
234 *usec_timeout_remaining=(tval.tv_sec*1000000)+tval.tv_usec;
235
236 if (ret < 0)
237 {
238 if (errno == EINTR)
239 {
240 // select interrupted by signal
241 ret=0;
242 }
243 else
244 EXCEPTIONSTREAM(error,t2n_server_error,"select error: " << strerror(errno));
245 }
246
247 if (ret > 0)
248 {
249 // we have data pending
250
251 // check for new connection
252 if (FD_ISSET (sock, &used_fdset))
253 {
254 new_connection();
255 }
256
257 // check all connections for pending data
258 return fill_connection_buffers();
259 }
260
261 return false;
262}
263
264/// call fill_buffer() on all connections, called from fill_buffer()
265bool socket_server::fill_connection_buffers()
266{
267 bool data_found = false;
268
269 std::map<unsigned int, server_connection*>::iterator ie=connections.end();
270 for(std::map<unsigned int, server_connection*>::iterator i=connections.begin(); i != ie; i++)
271 if (!i->second->server_connection::is_closed())
272 {
273 // shutdown all connections which throw exceptions to protect the server
274 try
275 {
276 if (i->second->fill_buffer(0))
277 data_found=true;
278 }
279 catch (t2n_transfer_error &e)
280 { i->second->close(); }
281 }
282
283 return data_found;
284}
285
286/// remove the socket of a connection after the connection has been closed
287void socket_server::remove_connection_socket(int sock)
288{
289 FD_CLR(sock, &connection_set);
290}
291
292/**
293 * Destructor
294 */
295socket_server_connection::~socket_server_connection()
296{
297 // Only notify parent server about going down.
298 // The real socket will be closed by the destructor of the base classes.
299 if (my_server && sock != -1)
300 {
301 socket_server *srv = dynamic_cast<socket_server*>(my_server);
302 if (srv)
303 srv->remove_connection_socket(sock);
304 }
305}
306
307/// close this connection. complete data waiting in the buffer can still be retrieved.
308void socket_server_connection::close()
309{
310 if (my_server && sock != -1)
311 {
312 socket_server *srv = dynamic_cast<socket_server*>(my_server);
313 if (srv)
314 srv->remove_connection_socket(sock);
315 }
316
317 if (!server_connection::is_closed())
318 {
319 socket_handler::close();
320 server_connection::close();
321 }
322}
323
324}