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>
36 #include "socket_server.hxx"
37 #include "t2n_exception.hxx"
44 socket_server::socket_server(int port, const char* ip)
51 socket_server::socket_server(const char* path, mode_t filemode, const char* user, const char* group)
57 /* Create the socket. */
58 sock = socket (PF_UNIX, SOCK_STREAM, 0);
61 string err="error opening socket: ";
64 throw t2n_server_error(err);
67 set_socket_options(sock);
69 /* Give the socket a name. */
70 struct sockaddr_un unix_name;
71 unix_name.sun_family = AF_UNIX;
72 strncpy (unix_name.sun_path, unix_path.c_str(),sizeof(unix_name.sun_path));
74 /* just to make sure there is no other socket file */
75 unlink (unix_name.sun_path);
77 if (bind (sock, (struct sockaddr *) &unix_name, sizeof (unix_name)) < 0)
79 string err="error binding socket: ";
82 throw t2n_server_error(err);
85 /* change permissions */
86 if (chmod (unix_name.sun_path, filemode) != 0)
88 string err="error changing permission: ";
91 throw t2n_server_error(err);
94 struct passwd *socket_user = getpwnam (user);
95 if (socket_user == NULL)
97 string err="error getting socket user: ";
100 throw t2n_server_error(err);
103 struct group *socket_group = getgrnam (group);
104 if (socket_group == NULL)
106 string err="error getting socket group: ";
107 err+=strerror(errno);
109 throw t2n_server_error(err);
112 if (chown (unix_name.sun_path, socket_user->pw_uid, socket_group->gr_gid) != 0)
114 string err="error changing socket ownership: ";
115 err+=strerror(errno);
117 throw t2n_server_error(err);
120 if (listen (sock, 5) < 0)
122 string err="error listening to socket: ";
123 err+=strerror(errno);
125 throw t2n_server_error(err);
128 /* clear & insert server sock into the fd_tab to prepare select */
129 FD_ZERO(&connection_set);
130 FD_SET (sock, &connection_set);
133 void socket_server::set_socket_options(int sock)
137 /* fast reuse enable */
138 if (setsockopt(sock,SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i)) < 0)
140 string err="error setting socket option: ";
141 err+=strerror(errno);
143 throw t2n_server_error(err);
146 /* keepalive enable */
147 if (setsockopt(sock,SOL_SOCKET, SO_KEEPALIVE, &i, sizeof(i)) < 0)
149 string err="error setting socket option: ";
150 err+=strerror(errno);
152 throw t2n_server_error(err);
157 fdflags=fcntl(sock,F_GETFD, 0);
160 string err="fcntl error on socket: ";
161 err+=strerror(errno);
163 throw t2n_server_error(err);
165 fdflags |= FD_CLOEXEC;
166 if (fcntl(sock,F_SETFD,fdflags) < 0)
168 string err="fcntl error on socket: ";
169 err+=strerror(errno);
171 throw t2n_server_error(err);
174 /* non-blocking mode */
176 flflags=fcntl(sock,F_GETFL,0);
179 string err="fcntl error on socket: ";
180 err+=strerror(errno);
182 throw t2n_server_error(err);
184 flflags |= O_NONBLOCK;
185 if (fcntl(sock,F_SETFL,flflags) < 0)
187 string err="fcntl error on socket: ";
188 err+=strerror(errno);
190 throw t2n_server_error(err);
194 socket_server::~socket_server()
198 if (socket_type==unix_s)
199 unlink(unix_path.c_str());
202 void socket_server::new_connection()
204 struct sockaddr_un clientname;
206 unsigned int size = sizeof (clientname);
207 int newsock = accept (sock,(struct sockaddr *) &clientname,&size);
212 log(error, "accept error (EAGAIN): no connection waiting");
217 string err="error accepting connection: ";
218 err+=strerror(errno);
220 throw t2n_server_error(err);
223 FD_SET (newsock, &connection_set);
227 /* keepalive enable */
228 if (setsockopt(newsock,SOL_SOCKET, SO_KEEPALIVE, &i, sizeof(i)) < 0)
230 string err="error setting socket option: ";
231 err+=strerror(errno);
233 throw t2n_server_error(err);
238 fdflags=fcntl(newsock,F_GETFD, 0);
241 string err="fcntl error on socket: ";
242 err+=strerror(errno);
244 throw t2n_server_error(err);
246 fdflags |= FD_CLOEXEC;
247 if (fcntl(newsock,F_SETFD,fdflags) < 0)
249 string err="fcntl error on socket: ";
250 err+=strerror(errno);
252 throw t2n_server_error(err);
255 /* non-blocking mode */
257 flflags=fcntl(newsock,F_GETFL,0);
260 string err="fcntl error on socket: ";
261 err+=strerror(errno);
263 throw t2n_server_error(err);
265 flflags |= O_NONBLOCK;
266 if (fcntl(newsock,F_SETFL,flflags) < 0)
268 string err="fcntl error on socket: ";
269 err+=strerror(errno);
271 throw t2n_server_error(err);
274 add_connection(new socket_connection(newsock, get_default_timeout()));
279 void socket_server::fill_buffer(long long usec_timeout)
281 fd_set used_fdset=connection_set;
285 struct timeval *timeout_ptr;
287 if (usec_timeout == -1)
293 // timeout von long long usec in int sec + int usec umrechnen
294 tval.tv_sec = usec_timeout / 1000000;
295 tval.tv_usec = usec_timeout % 1000000;
298 int ret=select (FD_SETSIZE, &used_fdset, NULL, NULL, timeout_ptr);
304 // select interrupted by signal
309 string err="select error: ";
310 err+=strerror(errno);
312 throw t2n_server_error(err);
318 // we have data pending
320 // check for new connection
321 if (FD_ISSET (sock, &used_fdset))
326 // check all connections for pending data
327 fill_connection_buffers();
333 socket_connection::socket_connection(int _sock, int _timeout)
334 : connection(_timeout)
339 void socket_connection::close()
344 void socket_connection::fill_buffer(void)
349 void socket_connection::write(const std::string& data)