Commit | Line | Data |
---|---|---|
ac7fdc22 GE |
1 | /*************************************************************************** |
2 | * Copyright (C) 2006 by Gerd v. Egidy * | |
3 | * gve@intra2net.com * | |
4 | * * | |
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. * | |
8 | * * | |
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. * | |
13 | * * | |
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 | ***************************************************************************/ | |
19 | ||
0cf4dc9b GE |
20 | #include <stdio.h> |
21 | #include <errno.h> | |
22 | #include <stdlib.h> | |
23 | #include <unistd.h> | |
24 | #include <sys/types.h> | |
25 | #include <sys/socket.h> | |
26 | #include <sys/un.h> | |
27 | #include <sys/time.h> | |
28 | #include <arpa/inet.h> | |
29 | #include <netinet/in.h> | |
30 | #include <netdb.h> | |
31 | #include <fcntl.h> | |
32 | #include <time.h> | |
33 | #include <pwd.h> | |
34 | #include <grp.h> | |
35 | ||
aa499d20 GE |
36 | #include <sstream> |
37 | ||
ac7fdc22 | 38 | #include "socket_server.hxx" |
0cf4dc9b | 39 | #include "t2n_exception.hxx" |
a7170401 | 40 | #include "log.hxx" |
0cf4dc9b GE |
41 | |
42 | using namespace std; | |
ac7fdc22 GE |
43 | |
44 | namespace libt2n | |
45 | { | |
46 | ||
94247295 GE |
47 | /** @brief create a new tcp-based server |
48 | @param port tcp port you want to listen on | |
49 | @param ip the local ip you want to listen on. "0.0.0.0" means all local ips (default). | |
50 | */ | |
644c4d26 | 51 | socket_server::socket_server(int port, const std::string& ip) |
a11e19b7 | 52 | : server(), socket_handler(0,tcp_s) |
ac7fdc22 | 53 | { |
cc68aabb GE |
54 | /* Create the socket. */ |
55 | sock = socket (PF_INET, SOCK_STREAM, 0); | |
56 | if (sock < 0) | |
a7170401 | 57 | EXCEPTIONSTREAM(error,t2n_server_error,"error opening socket: " << strerror(errno)); |
cc68aabb GE |
58 | |
59 | set_socket_options(sock); | |
60 | ||
61 | /* Give the socket a name. */ | |
62 | struct sockaddr_in sockaddr; | |
63 | sockaddr.sin_family = AF_INET; | |
64 | sockaddr.sin_port = htons(port); | |
65 | ||
66 | if (inet_aton(ip.c_str(),&sockaddr.sin_addr) == 0) | |
a7170401 | 67 | EXCEPTIONSTREAM(error,t2n_server_error,"failed listening on invalid ip " << ip); |
cc68aabb GE |
68 | |
69 | if (bind (sock, (struct sockaddr *) &sockaddr, sizeof (sockaddr)) < 0) | |
a7170401 | 70 | EXCEPTIONSTREAM(error,t2n_server_error,"error binding socket: " << strerror(errno)); |
cc68aabb GE |
71 | |
72 | start_listening(); | |
ac7fdc22 GE |
73 | } |
74 | ||
94247295 GE |
75 | /** @brief create a new unix-socked-based server |
76 | @param path path of the socket | |
77 | @param filemode permissions you want to open the socket with | |
78 | @param user local username for the socket | |
79 | @param group local groupname for the socket | |
80 | */ | |
644c4d26 | 81 | socket_server::socket_server(const std::string& path, mode_t filemode, const std::string& user, const std::string& group) |
a11e19b7 | 82 | : server(), socket_handler(0,unix_s) |
ac7fdc22 | 83 | { |
ac7fdc22 GE |
84 | unix_path=path; |
85 | ||
ac7fdc22 | 86 | /* Create the socket. */ |
0cf4dc9b GE |
87 | sock = socket (PF_UNIX, SOCK_STREAM, 0); |
88 | if (sock < 0) | |
a7170401 | 89 | EXCEPTIONSTREAM(error,t2n_server_error,"error opening socket: " << strerror(errno)); |
0cf4dc9b GE |
90 | |
91 | set_socket_options(sock); | |
92 | ||
93 | /* Give the socket a name. */ | |
94 | struct sockaddr_un unix_name; | |
95 | unix_name.sun_family = AF_UNIX; | |
96 | strncpy (unix_name.sun_path, unix_path.c_str(),sizeof(unix_name.sun_path)); | |
97 | ||
98 | /* just to make sure there is no other socket file */ | |
99 | unlink (unix_name.sun_path); | |
100 | ||
101 | if (bind (sock, (struct sockaddr *) &unix_name, sizeof (unix_name)) < 0) | |
a7170401 | 102 | EXCEPTIONSTREAM(error,t2n_server_error,"error binding socket: " << strerror(errno)); |
0cf4dc9b GE |
103 | |
104 | /* change permissions */ | |
105 | if (chmod (unix_name.sun_path, filemode) != 0) | |
a7170401 | 106 | EXCEPTIONSTREAM(error,t2n_server_error,"error changing permission: " << strerror(errno)); |
0cf4dc9b | 107 | |
644c4d26 | 108 | if (!user.empty() && !group.empty()) |
0cf4dc9b | 109 | { |
644c4d26 | 110 | // TODO maybe use current user/group if one of them is empty |
0cf4dc9b | 111 | |
644c4d26 GE |
112 | struct passwd *socket_user = getpwnam (user.c_str()); |
113 | if (socket_user == NULL) | |
a7170401 GE |
114 | EXCEPTIONSTREAM(error,t2n_server_error,"error getting socket user: " << strerror(errno)); |
115 | ||
644c4d26 GE |
116 | struct group *socket_group = getgrnam (group.c_str()); |
117 | if (socket_group == NULL) | |
a7170401 GE |
118 | EXCEPTIONSTREAM(error,t2n_server_error,"error getting socket group: " << strerror(errno)); |
119 | ||
644c4d26 | 120 | if (chown (unix_name.sun_path, socket_user->pw_uid, socket_group->gr_gid) != 0) |
a7170401 | 121 | EXCEPTIONSTREAM(error,t2n_server_error,"error changing socket ownership: " << strerror(errno)); |
0cf4dc9b GE |
122 | } |
123 | ||
cc68aabb GE |
124 | start_listening(); |
125 | } | |
126 | ||
127 | socket_server::~socket_server() | |
128 | { | |
129 | socket_handler::close(); | |
130 | ||
131 | if (get_type()==unix_s) | |
132 | unlink(unix_path.c_str()); | |
133 | } | |
134 | ||
135 | /// start listening on a new server socket (called by the constructors) | |
136 | void socket_server::start_listening() | |
137 | { | |
0cf4dc9b | 138 | if (listen (sock, 5) < 0) |
a7170401 | 139 | EXCEPTIONSTREAM(error,t2n_server_error,"error listening to socket: " << strerror(errno)); |
0cf4dc9b GE |
140 | |
141 | /* clear & insert server sock into the fd_tab to prepare select */ | |
142 | FD_ZERO(&connection_set); | |
143 | FD_SET (sock, &connection_set); | |
144 | } | |
145 | ||
cc68aabb | 146 | /// handle a new connection from a client |
0cf4dc9b GE |
147 | void socket_server::new_connection() |
148 | { | |
04e6b271 | 149 | struct sockaddr_un clientname; |
ac7fdc22 | 150 | |
04e6b271 GE |
151 | unsigned int size = sizeof (clientname); |
152 | int newsock = accept (sock,(struct sockaddr *) &clientname,&size); | |
153 | if (newsock < 0) | |
154 | { | |
155 | if (errno == EAGAIN) | |
156 | { | |
a7170401 | 157 | LOGSTREAM(error,"accept error (EAGAIN): no connection waiting"); |
04e6b271 GE |
158 | return; |
159 | } | |
160 | ||
161 | /* default: break */ | |
a7170401 | 162 | EXCEPTIONSTREAM(error,t2n_server_error,"error accepting connection: " << strerror(errno)); |
04e6b271 GE |
163 | } |
164 | ||
165 | FD_SET (newsock, &connection_set); | |
166 | ||
a11e19b7 GE |
167 | socket_server_connection *nc=new socket_server_connection(newsock, get_type(), get_default_timeout()); |
168 | nc->set_socket_options(newsock); | |
04e6b271 | 169 | |
a11e19b7 | 170 | add_connection(nc); |
04e6b271 GE |
171 | |
172 | return; | |
0cf4dc9b | 173 | } |
ac7fdc22 | 174 | |
45a2ebc9 GE |
175 | /** @brief look for new connections and new data in any of the existing connections |
176 | @param usec_timeout wait until new data is found, max timeout usecs. | |
177 | -1: wait endless | |
178 | 0: return instantly | |
179 | @param usec_timeout_remaining if non-NULL the function will write the | |
180 | not used time to the given target | |
181 | @retval true if new data was found (does not mean that the received data | |
182 | is a complete packet though) | |
183 | */ | |
184 | bool socket_server::fill_buffer(long long usec_timeout,long long* usec_timeout_remaining) | |
ac7fdc22 | 185 | { |
0cf4dc9b GE |
186 | fd_set used_fdset=connection_set; |
187 | ||
188 | /* set timeout */ | |
189 | struct timeval tval; | |
190 | struct timeval *timeout_ptr; | |
191 | ||
192 | if (usec_timeout == -1) | |
193 | timeout_ptr = NULL; | |
194 | else | |
195 | { | |
196 | timeout_ptr = &tval; | |
197 | ||
198 | // timeout von long long usec in int sec + int usec umrechnen | |
199 | tval.tv_sec = usec_timeout / 1000000; | |
200 | tval.tv_usec = usec_timeout % 1000000; | |
201 | } | |
202 | ||
203 | int ret=select (FD_SETSIZE, &used_fdset, NULL, NULL, timeout_ptr); | |
204 | ||
45a2ebc9 GE |
205 | // return the timeout we did not use |
206 | if (usec_timeout > 0 && usec_timeout_remaining != NULL) | |
207 | *usec_timeout_remaining=(tval.tv_sec*1000000)+tval.tv_usec; | |
208 | ||
0cf4dc9b GE |
209 | if (ret < 0) |
210 | { | |
211 | if (errno == EINTR) | |
212 | { | |
213 | // select interrupted by signal | |
214 | ret=0; | |
215 | } | |
216 | else | |
a7170401 | 217 | EXCEPTIONSTREAM(error,t2n_server_error,"select error: " << strerror(errno)); |
0cf4dc9b GE |
218 | } |
219 | ||
220 | if (ret > 0) | |
221 | { | |
222 | // we have data pending | |
223 | ||
224 | // check for new connection | |
225 | if (FD_ISSET (sock, &used_fdset)) | |
226 | { | |
227 | new_connection(); | |
228 | } | |
229 | ||
230 | // check all connections for pending data | |
94247295 | 231 | return fill_connection_buffers(); |
0cf4dc9b | 232 | } |
ac7fdc22 | 233 | |
94247295 | 234 | return false; |
ac7fdc22 GE |
235 | } |
236 | ||
cc68aabb | 237 | /// call fill_buffer() on all connections, called from fill_buffer() |
94247295 | 238 | bool socket_server::fill_connection_buffers() |
aa499d20 | 239 | { |
94247295 GE |
240 | bool data_found; |
241 | ||
a11e19b7 GE |
242 | std::map<unsigned int, server_connection*>::iterator ie=connections.end(); |
243 | for(std::map<unsigned int, server_connection*>::iterator i=connections.begin(); i != ie; i++) | |
644c4d26 | 244 | if (!i->second->server_connection::is_closed()) |
94247295 GE |
245 | if (i->second->fill_buffer(0)) |
246 | data_found=true; | |
247 | ||
248 | return data_found; | |
aa499d20 GE |
249 | } |
250 | ||
cc68aabb | 251 | /// remove the socket of a connection after the connection has been closed |
a11e19b7 | 252 | void socket_server::remove_connection_socket(int sock) |
ac7fdc22 | 253 | { |
a11e19b7 | 254 | FD_CLR(sock, &connection_set); |
ac7fdc22 GE |
255 | } |
256 | ||
cc68aabb | 257 | /// close this connection. complete data waiting in the buffer can still be retrieved. |
a11e19b7 | 258 | void socket_server_connection::close() |
ac7fdc22 | 259 | { |
644c4d26 | 260 | if (!server_connection::is_closed()) |
aa499d20 | 261 | { |
a11e19b7 GE |
262 | socket_handler::close(); |
263 | server_connection::close(); | |
aa499d20 GE |
264 | } |
265 | ||
a11e19b7 | 266 | if (my_server) |
aa499d20 | 267 | { |
a11e19b7 | 268 | dynamic_cast<socket_server*>(my_server)->remove_connection_socket(sock); |
aa499d20 | 269 | } |
ac7fdc22 GE |
270 | } |
271 | ||
272 | } |