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 /** @brief create a new tcp-based server
47 @param port tcp port you want to listen on
48 @param ip the local ip you want to listen on. "0.0.0.0" means all local ips (default).
50 socket_server::socket_server(int port, const std::string& ip)
51 : server(), socket_handler(0,tcp_s)
56 /** @brief create a new unix-socked-based server
57 @param path path of the socket
58 @param filemode permissions you want to open the socket with
59 @param user local username for the socket
60 @param group local groupname for the socket
62 socket_server::socket_server(const std::string& path, mode_t filemode, const std::string& user, const std::string& group)
63 : server(), socket_handler(0,unix_s)
67 /* Create the socket. */
68 sock = socket (PF_UNIX, SOCK_STREAM, 0);
71 string err="error opening socket: ";
74 throw t2n_server_error(err);
77 set_socket_options(sock);
79 /* Give the socket a name. */
80 struct sockaddr_un unix_name;
81 unix_name.sun_family = AF_UNIX;
82 strncpy (unix_name.sun_path, unix_path.c_str(),sizeof(unix_name.sun_path));
84 /* just to make sure there is no other socket file */
85 unlink (unix_name.sun_path);
87 if (bind (sock, (struct sockaddr *) &unix_name, sizeof (unix_name)) < 0)
89 string err="error binding socket: ";
92 throw t2n_server_error(err);
95 /* change permissions */
96 if (chmod (unix_name.sun_path, filemode) != 0)
98 string err="error changing permission: ";
101 throw t2n_server_error(err);
104 if (!user.empty() && !group.empty())
106 // TODO maybe use current user/group if one of them is empty
108 struct passwd *socket_user = getpwnam (user.c_str());
109 if (socket_user == NULL)
111 string err="error getting socket user: ";
112 err+=strerror(errno);
114 throw t2n_server_error(err);
117 struct group *socket_group = getgrnam (group.c_str());
118 if (socket_group == NULL)
120 string err="error getting socket group: ";
121 err+=strerror(errno);
123 throw t2n_server_error(err);
126 if (chown (unix_name.sun_path, socket_user->pw_uid, socket_group->gr_gid) != 0)
128 string err="error changing socket ownership: ";
129 err+=strerror(errno);
131 throw t2n_server_error(err);
135 if (listen (sock, 5) < 0)
137 string err="error listening to socket: ";
138 err+=strerror(errno);
140 throw t2n_server_error(err);
143 /* clear & insert server sock into the fd_tab to prepare select */
144 FD_ZERO(&connection_set);
145 FD_SET (sock, &connection_set);
148 socket_server::~socket_server()
150 socket_handler::close();
152 if (get_type()==unix_s)
153 unlink(unix_path.c_str());
156 void socket_server::new_connection()
158 struct sockaddr_un clientname;
160 unsigned int size = sizeof (clientname);
161 int newsock = accept (sock,(struct sockaddr *) &clientname,&size);
166 log(error, "accept error (EAGAIN): no connection waiting");
171 string err="error accepting connection: ";
172 err+=strerror(errno);
174 throw t2n_server_error(err);
177 FD_SET (newsock, &connection_set);
179 socket_server_connection *nc=new socket_server_connection(newsock, get_type(), get_default_timeout());
180 nc->set_socket_options(newsock);
187 bool socket_server::fill_buffer(long long usec_timeout)
189 fd_set used_fdset=connection_set;
193 struct timeval *timeout_ptr;
195 if (usec_timeout == -1)
201 // timeout von long long usec in int sec + int usec umrechnen
202 tval.tv_sec = usec_timeout / 1000000;
203 tval.tv_usec = usec_timeout % 1000000;
206 int ret=select (FD_SETSIZE, &used_fdset, NULL, NULL, timeout_ptr);
212 // select interrupted by signal
217 string err="select error: ";
218 err+=strerror(errno);
220 throw t2n_server_error(err);
226 // we have data pending
228 // check for new connection
229 if (FD_ISSET (sock, &used_fdset))
234 // check all connections for pending data
235 return fill_connection_buffers();
241 bool socket_server::fill_connection_buffers()
245 std::map<unsigned int, server_connection*>::iterator ie=connections.end();
246 for(std::map<unsigned int, server_connection*>::iterator i=connections.begin(); i != ie; i++)
247 if (!i->second->server_connection::is_closed())
248 if (i->second->fill_buffer(0))
254 void socket_server::remove_connection_socket(int sock)
256 FD_CLR(sock, &connection_set);
259 void socket_server_connection::log(log_level_values level, const char* message)
264 msg << "connection id " << get_id() << ": " << message;
265 my_server->log(level,msg.str().c_str());
269 void socket_server_connection::close()
271 if (!server_connection::is_closed())
273 socket_handler::close();
274 server_connection::close();
279 dynamic_cast<socket_server*>(my_server)->remove_connection_socket(sock);