1 /***************************************************************************
2 * Copyright (C) 2006 by Gerd v. Egidy *
5 * This library is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU Lesser General Public License version *
7 * 2.1 as published by the Free Software Foundation. *
9 * This library is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU Lesser General Public License for more details. *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
24 #include <sys/types.h>
25 #include <sys/socket.h>
28 #include <arpa/inet.h>
29 #include <netinet/in.h>
38 #include "socket_server.hxx"
39 #include "t2n_exception.hxx"
46 socket_server::socket_server(int port, const char* ip)
53 socket_server::socket_server(const char* path, mode_t filemode, const char* user, const char* group)
59 /* Create the socket. */
60 sock = socket (PF_UNIX, SOCK_STREAM, 0);
63 string err="error opening socket: ";
66 throw t2n_server_error(err);
69 set_socket_options(sock);
71 /* Give the socket a name. */
72 struct sockaddr_un unix_name;
73 unix_name.sun_family = AF_UNIX;
74 strncpy (unix_name.sun_path, unix_path.c_str(),sizeof(unix_name.sun_path));
76 /* just to make sure there is no other socket file */
77 unlink (unix_name.sun_path);
79 if (bind (sock, (struct sockaddr *) &unix_name, sizeof (unix_name)) < 0)
81 string err="error binding socket: ";
84 throw t2n_server_error(err);
87 /* change permissions */
88 if (chmod (unix_name.sun_path, filemode) != 0)
90 string err="error changing permission: ";
93 throw t2n_server_error(err);
96 struct passwd *socket_user = getpwnam (user);
97 if (socket_user == NULL)
99 string err="error getting socket user: ";
100 err+=strerror(errno);
102 throw t2n_server_error(err);
105 struct group *socket_group = getgrnam (group);
106 if (socket_group == NULL)
108 string err="error getting socket group: ";
109 err+=strerror(errno);
111 throw t2n_server_error(err);
114 if (chown (unix_name.sun_path, socket_user->pw_uid, socket_group->gr_gid) != 0)
116 string err="error changing socket ownership: ";
117 err+=strerror(errno);
119 throw t2n_server_error(err);
122 if (listen (sock, 5) < 0)
124 string err="error listening to socket: ";
125 err+=strerror(errno);
127 throw t2n_server_error(err);
130 /* clear & insert server sock into the fd_tab to prepare select */
131 FD_ZERO(&connection_set);
132 FD_SET (sock, &connection_set);
135 void socket_server::set_socket_options(int sock)
139 /* fast reuse enable */
140 if (setsockopt(sock,SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i)) < 0)
142 string err="error setting socket option: ";
143 err+=strerror(errno);
145 throw t2n_server_error(err);
148 /* keepalive enable */
149 if (setsockopt(sock,SOL_SOCKET, SO_KEEPALIVE, &i, sizeof(i)) < 0)
151 string err="error setting socket option: ";
152 err+=strerror(errno);
154 throw t2n_server_error(err);
159 fdflags=fcntl(sock,F_GETFD, 0);
162 string err="fcntl error on socket: ";
163 err+=strerror(errno);
165 throw t2n_server_error(err);
167 fdflags |= FD_CLOEXEC;
168 if (fcntl(sock,F_SETFD,fdflags) < 0)
170 string err="fcntl error on socket: ";
171 err+=strerror(errno);
173 throw t2n_server_error(err);
176 /* non-blocking mode */
178 flflags=fcntl(sock,F_GETFL,0);
181 string err="fcntl error on socket: ";
182 err+=strerror(errno);
184 throw t2n_server_error(err);
186 flflags |= O_NONBLOCK;
187 if (fcntl(sock,F_SETFL,flflags) < 0)
189 string err="fcntl error on socket: ";
190 err+=strerror(errno);
192 throw t2n_server_error(err);
196 socket_server::~socket_server()
200 if (socket_type==unix_s)
201 unlink(unix_path.c_str());
204 void socket_server::new_connection()
206 struct sockaddr_un clientname;
208 unsigned int size = sizeof (clientname);
209 int newsock = accept (sock,(struct sockaddr *) &clientname,&size);
214 log(error, "accept error (EAGAIN): no connection waiting");
219 string err="error accepting connection: ";
220 err+=strerror(errno);
222 throw t2n_server_error(err);
225 FD_SET (newsock, &connection_set);
229 /* keepalive enable */
230 if (setsockopt(newsock,SOL_SOCKET, SO_KEEPALIVE, &i, sizeof(i)) < 0)
232 string err="error setting socket option: ";
233 err+=strerror(errno);
235 throw t2n_server_error(err);
240 fdflags=fcntl(newsock,F_GETFD, 0);
243 string err="fcntl error on socket: ";
244 err+=strerror(errno);
246 throw t2n_server_error(err);
248 fdflags |= FD_CLOEXEC;
249 if (fcntl(newsock,F_SETFD,fdflags) < 0)
251 string err="fcntl error on socket: ";
252 err+=strerror(errno);
254 throw t2n_server_error(err);
257 /* non-blocking mode */
259 flflags=fcntl(newsock,F_GETFL,0);
262 string err="fcntl error on socket: ";
263 err+=strerror(errno);
265 throw t2n_server_error(err);
267 flflags |= O_NONBLOCK;
268 if (fcntl(newsock,F_SETFL,flflags) < 0)
270 string err="fcntl error on socket: ";
271 err+=strerror(errno);
273 throw t2n_server_error(err);
276 add_connection(new socket_connection(newsock, get_default_timeout()));
281 void socket_server::fill_buffer(long long usec_timeout)
283 fd_set used_fdset=connection_set;
287 struct timeval *timeout_ptr;
289 if (usec_timeout == -1)
295 // timeout von long long usec in int sec + int usec umrechnen
296 tval.tv_sec = usec_timeout / 1000000;
297 tval.tv_usec = usec_timeout % 1000000;
300 int ret=select (FD_SETSIZE, &used_fdset, NULL, NULL, timeout_ptr);
306 // select interrupted by signal
311 string err="select error: ";
312 err+=strerror(errno);
314 throw t2n_server_error(err);
320 // we have data pending
322 // check for new connection
323 if (FD_ISSET (sock, &used_fdset))
328 // check all connections for pending data
329 fill_connection_buffers();
335 void socket_server::fill_connection_buffers()
337 std::map<unsigned int, connection*>::iterator ie=connections.end();
338 for(std::map<unsigned int, connection*>::iterator i=connections.begin(); i != ie; i++)
339 if (!i->second->is_closed())
341 socket_connection* cp=dynamic_cast<socket_connection*>(i->second);
342 cp->fill_buffer(connection_set);
346 socket_connection::socket_connection(int _sock, int _timeout)
347 : connection(_timeout)
352 void socket_connection::close()
357 void socket_connection::fill_buffer(fd_set &cur_fdset)
359 bool try_again=false;
361 if (is_closed() || !FD_ISSET (sock, &cur_fdset))
362 return; // no data pending or connection closed
364 // data pending -> go and get it
365 char socket_buffer[recv_buffer_size];
367 int nbytes = read (sock, socket_buffer, recv_buffer_size);
373 my_server->log(server::error,"read error: no data (EAGAIN) for connection "+get_id_string());
376 else if (errno == EINTR)
378 // interrupted, try again
384 my_server->log(server::error,"error reading from socket of connection "+get_id_string()+": "+strerror(errno));
390 if (nbytes == 0 && !try_again)
396 // Data read -> store it
398 buffer.assign(socket_buffer,nbytes);
400 // more data waiting?
401 fd_set active_fd_set;
404 FD_ZERO (&active_fd_set);
405 FD_SET (sock, &active_fd_set);
411 if (select (FD_SETSIZE, &active_fd_set, NULL, NULL, &tval) >0)
413 /* more data waiting -> recurse */
414 fill_buffer(active_fd_set);
422 void socket_connection::write(const std::string& data)
424 static const unsigned int write_block_size=4096;
429 // prepend packet size to data
430 packet_size_indicator psize=data.size();
431 string send_data(data);
432 send_data.insert(0,(char*)psize,sizeof(packet_size_indicator));
435 while (offset < send_data.size())
437 unsigned int write_size=write_block_size;
439 if (offset+write_size > send_data.size())
440 write_size = send_data.size()-offset;
443 while ((rtn=::write(sock, send_data.data()+offset, write_size)) &&
444 rtn == -1 && (errno == EAGAIN || errno == EINTR))
448 my_server->log(server::debug,"resuming write() call after EAGAIN or EINTR for connection "+get_id_string());
454 my_server->log(server::error,"write() error on connection "+get_id_string()+": "+strerror(errno));
456 else if (rtn != write_size)
461 msg << "write() error on connection " << get_id()
462 << ": wrote " << rtn << " bytes, should have been "
463 << write_size << " (complete: " << send_data.size() << ")";
465 my_server->log(server::error,msg.str());
469 offset += write_size;