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 GE |
39 | #include "t2n_exception.hxx" |
40 | ||
41 | using namespace std; | |
ac7fdc22 GE |
42 | |
43 | namespace libt2n | |
44 | { | |
45 | ||
644c4d26 | 46 | socket_server::socket_server(int port, const std::string& ip) |
a11e19b7 | 47 | : server(), socket_handler(0,tcp_s) |
ac7fdc22 | 48 | { |
a11e19b7 | 49 | // TODO |
ac7fdc22 GE |
50 | } |
51 | ||
644c4d26 | 52 | socket_server::socket_server(const std::string& path, mode_t filemode, const std::string& user, const std::string& group) |
a11e19b7 | 53 | : server(), socket_handler(0,unix_s) |
ac7fdc22 | 54 | { |
ac7fdc22 GE |
55 | unix_path=path; |
56 | ||
ac7fdc22 | 57 | /* Create the socket. */ |
0cf4dc9b GE |
58 | sock = socket (PF_UNIX, SOCK_STREAM, 0); |
59 | if (sock < 0) | |
ac7fdc22 GE |
60 | { |
61 | string err="error opening socket: "; | |
0cf4dc9b GE |
62 | err+=strerror(errno); |
63 | log(error, err); | |
64 | throw t2n_server_error(err); | |
65 | } | |
66 | ||
67 | set_socket_options(sock); | |
68 | ||
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)); | |
73 | ||
74 | /* just to make sure there is no other socket file */ | |
75 | unlink (unix_name.sun_path); | |
76 | ||
77 | if (bind (sock, (struct sockaddr *) &unix_name, sizeof (unix_name)) < 0) | |
78 | { | |
79 | string err="error binding socket: "; | |
80 | err+=strerror(errno); | |
81 | log(error, err); | |
82 | throw t2n_server_error(err); | |
83 | } | |
84 | ||
85 | /* change permissions */ | |
86 | if (chmod (unix_name.sun_path, filemode) != 0) | |
87 | { | |
88 | string err="error changing permission: "; | |
89 | err+=strerror(errno); | |
90 | log(error, err); | |
91 | throw t2n_server_error(err); | |
92 | } | |
93 | ||
644c4d26 | 94 | if (!user.empty() && !group.empty()) |
0cf4dc9b | 95 | { |
644c4d26 | 96 | // TODO maybe use current user/group if one of them is empty |
0cf4dc9b | 97 | |
644c4d26 GE |
98 | struct passwd *socket_user = getpwnam (user.c_str()); |
99 | if (socket_user == NULL) | |
100 | { | |
101 | string err="error getting socket user: "; | |
102 | err+=strerror(errno); | |
103 | log(error, err); | |
104 | throw t2n_server_error(err); | |
105 | } | |
106 | ||
107 | struct group *socket_group = getgrnam (group.c_str()); | |
108 | if (socket_group == NULL) | |
109 | { | |
110 | string err="error getting socket group: "; | |
111 | err+=strerror(errno); | |
112 | log(error, err); | |
113 | throw t2n_server_error(err); | |
114 | } | |
115 | ||
116 | if (chown (unix_name.sun_path, socket_user->pw_uid, socket_group->gr_gid) != 0) | |
117 | { | |
118 | string err="error changing socket ownership: "; | |
119 | err+=strerror(errno); | |
120 | log(error, err); | |
121 | throw t2n_server_error(err); | |
122 | } | |
0cf4dc9b GE |
123 | } |
124 | ||
125 | if (listen (sock, 5) < 0) | |
126 | { | |
127 | string err="error listening to socket: "; | |
128 | err+=strerror(errno); | |
129 | log(error, err); | |
130 | throw t2n_server_error(err); | |
131 | } | |
132 | ||
133 | /* clear & insert server sock into the fd_tab to prepare select */ | |
134 | FD_ZERO(&connection_set); | |
135 | FD_SET (sock, &connection_set); | |
136 | } | |
137 | ||
ac7fdc22 GE |
138 | socket_server::~socket_server() |
139 | { | |
644c4d26 GE |
140 | socket_handler::close(); |
141 | ||
a11e19b7 | 142 | if (get_type()==unix_s) |
ac7fdc22 GE |
143 | unlink(unix_path.c_str()); |
144 | } | |
145 | ||
0cf4dc9b GE |
146 | void socket_server::new_connection() |
147 | { | |
04e6b271 | 148 | struct sockaddr_un clientname; |
ac7fdc22 | 149 | |
04e6b271 GE |
150 | unsigned int size = sizeof (clientname); |
151 | int newsock = accept (sock,(struct sockaddr *) &clientname,&size); | |
152 | if (newsock < 0) | |
153 | { | |
154 | if (errno == EAGAIN) | |
155 | { | |
156 | log(error, "accept error (EAGAIN): no connection waiting"); | |
157 | return; | |
158 | } | |
159 | ||
160 | /* default: break */ | |
161 | string err="error accepting connection: "; | |
162 | err+=strerror(errno); | |
163 | log(error, err); | |
164 | throw t2n_server_error(err); | |
165 | } | |
166 | ||
167 | FD_SET (newsock, &connection_set); | |
168 | ||
a11e19b7 GE |
169 | socket_server_connection *nc=new socket_server_connection(newsock, get_type(), get_default_timeout()); |
170 | nc->set_socket_options(newsock); | |
04e6b271 | 171 | |
a11e19b7 | 172 | add_connection(nc); |
04e6b271 GE |
173 | |
174 | return; | |
0cf4dc9b | 175 | } |
ac7fdc22 GE |
176 | |
177 | void socket_server::fill_buffer(long long usec_timeout) | |
178 | { | |
0cf4dc9b GE |
179 | fd_set used_fdset=connection_set; |
180 | ||
181 | /* set timeout */ | |
182 | struct timeval tval; | |
183 | struct timeval *timeout_ptr; | |
184 | ||
185 | if (usec_timeout == -1) | |
186 | timeout_ptr = NULL; | |
187 | else | |
188 | { | |
189 | timeout_ptr = &tval; | |
190 | ||
191 | // timeout von long long usec in int sec + int usec umrechnen | |
192 | tval.tv_sec = usec_timeout / 1000000; | |
193 | tval.tv_usec = usec_timeout % 1000000; | |
194 | } | |
195 | ||
196 | int ret=select (FD_SETSIZE, &used_fdset, NULL, NULL, timeout_ptr); | |
197 | ||
198 | if (ret < 0) | |
199 | { | |
200 | if (errno == EINTR) | |
201 | { | |
202 | // select interrupted by signal | |
203 | ret=0; | |
204 | } | |
205 | else | |
206 | { | |
207 | string err="select error: "; | |
208 | err+=strerror(errno); | |
209 | log(error, err); | |
210 | throw t2n_server_error(err); | |
211 | } | |
212 | } | |
213 | ||
214 | if (ret > 0) | |
215 | { | |
216 | // we have data pending | |
217 | ||
218 | // check for new connection | |
219 | if (FD_ISSET (sock, &used_fdset)) | |
220 | { | |
221 | new_connection(); | |
222 | } | |
223 | ||
224 | // check all connections for pending data | |
225 | fill_connection_buffers(); | |
226 | } | |
ac7fdc22 | 227 | |
0cf4dc9b | 228 | return; |
ac7fdc22 GE |
229 | } |
230 | ||
aa499d20 GE |
231 | void socket_server::fill_connection_buffers() |
232 | { | |
a11e19b7 GE |
233 | std::map<unsigned int, server_connection*>::iterator ie=connections.end(); |
234 | for(std::map<unsigned int, server_connection*>::iterator i=connections.begin(); i != ie; i++) | |
644c4d26 | 235 | if (!i->second->server_connection::is_closed()) |
a11e19b7 | 236 | i->second->fill_buffer(); |
aa499d20 GE |
237 | } |
238 | ||
a11e19b7 | 239 | void socket_server::remove_connection_socket(int sock) |
ac7fdc22 | 240 | { |
a11e19b7 | 241 | FD_CLR(sock, &connection_set); |
ac7fdc22 GE |
242 | } |
243 | ||
644c4d26 GE |
244 | void socket_server_connection::log(log_level_values level, const char* message) |
245 | { | |
246 | if(my_server) | |
247 | { | |
248 | ostringstream msg; | |
249 | msg << "connection id " << get_id() << ": " << message; | |
250 | my_server->log(level,msg.str().c_str()); | |
251 | } | |
252 | } | |
253 | ||
a11e19b7 | 254 | void socket_server_connection::close() |
ac7fdc22 | 255 | { |
644c4d26 | 256 | if (!server_connection::is_closed()) |
aa499d20 | 257 | { |
a11e19b7 GE |
258 | socket_handler::close(); |
259 | server_connection::close(); | |
aa499d20 GE |
260 | } |
261 | ||
a11e19b7 | 262 | if (my_server) |
aa499d20 | 263 | { |
a11e19b7 | 264 | dynamic_cast<socket_server*>(my_server)->remove_connection_socket(sock); |
aa499d20 | 265 | } |
ac7fdc22 GE |
266 | } |
267 | ||
268 | } |