Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 344 lines (312 sloc) 9.137 kb
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
1 /*
2 * $Id$
3 *
53c7e0f @janakj - Spelling checked
janakj authored
4 * Copyright (C) 2001-2003 FhG Fokus
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
5 *
6 * This file is part of ser, a free SIP server.
7 *
8 * ser is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version
12 *
13 * For a license to use the ser software under conditions
14 * other than those described here, or to purchase support for this
15 * software, please contact iptel.org by e-mail at the following addresses:
16 * info@iptel.org
17 *
18 * ser is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 */
6c6659c - solaris fixes - ser + tcp compiles now on solaris
Andrei Pelinescu-Onciul authored
27 /*
28 * History:
29 * --------
30 * 2002-11-29 created by andrei
31 * 2003-02-20 added solaris support (! HAVE_MSGHDR_MSG_CONTROL) (andrei)
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
32 * 2003-11-03 added send_all, recv_all and updated send/get_fd
33 * to handle signals (andrei)
0ba367e - tcp major changes part 1: support for >1024 connection, better io p…
Andrei Pelinescu-Onciul authored
34 * 2005-06-13 added flags to recv_all & receive_fd, to allow full blocking
35 * or semi-nonblocking mode (andrei)
3d4bb4b - MSG_WAITALL emulation for OSes that don't support it (win+cygwin)
Andrei Pelinescu-Onciul authored
36 * 2008-04-30 added MSG_WAITALL emulation for cygwin (andrei)
6c6659c - solaris fixes - ser + tcp compiles now on solaris
Andrei Pelinescu-Onciul authored
37 */
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
38
1d0661d @oej Adding doxygen template to all core .c files
oej authored
39 /*!
40 * \file
41 * \brief SIP-router core ::
42 * \ingroup core
43 * Module: \ref core
44 */
45
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
46 #ifdef USE_TCP
47
3d4bb4b - MSG_WAITALL emulation for OSes that don't support it (win+cygwin)
Andrei Pelinescu-Onciul authored
48 #include "pass_fd.h"
49
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
50 #include <sys/types.h>
51 #include <sys/socket.h>
b988dae - compile fixes: ser & tm + tcp compiles on linux, freebsd, openbsd &…
Andrei Pelinescu-Onciul authored
52 #include <sys/uio.h>
53 #include <stdlib.h> /* for NULL definition on openbsd */
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
54 #include <errno.h>
55 #include <string.h>
3d4bb4b - MSG_WAITALL emulation for OSes that don't support it (win+cygwin)
Andrei Pelinescu-Onciul authored
56 #ifdef NO_MSG_WAITALL
57 #include <poll.h>
58 #endif /* NO_MSG_WAITALL */
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
59
b988dae - compile fixes: ser & tm + tcp compiles on linux, freebsd, openbsd &…
Andrei Pelinescu-Onciul authored
60 #include "dprint.h"
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
61
62
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
63
64 /* receive all the data or returns error (handles EINTR etc.)
0ba367e - tcp major changes part 1: support for >1024 connection, better io p…
Andrei Pelinescu-Onciul authored
65 * params: socket
66 * data - buffer for the results
67 * data_len -
68 * flags - recv flags for the first recv (see recv(2)), only
69 * 0, MSG_WAITALL and MSG_DONTWAIT make sense
70 * if flags is set to MSG_DONWAIT (or to 0 and the socket fd is non-blocking),
71 * and if no data is queued on the fd, recv_all will not wait (it will
72 * return error and set errno to EAGAIN/EWOULDBLOCK). However if even 1 byte
73 * is queued, the call will block until the whole data_len was read or an
74 * error or eof occured ("semi-nonblocking" behaviour, some tcp code
75 * counts on it).
76 * if flags is set to MSG_WAITALL it will block even if no byte is available.
77 *
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
78 * returns: bytes read or error (<0)
79 * can return < data_len if EOF */
0ba367e - tcp major changes part 1: support for >1024 connection, better io p…
Andrei Pelinescu-Onciul authored
80 int recv_all(int socket, void* data, int data_len, int flags)
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
81 {
82 int b_read;
83 int n;
3d4bb4b - MSG_WAITALL emulation for OSes that don't support it (win+cygwin)
Andrei Pelinescu-Onciul authored
84 #ifdef NO_MSG_WAITALL
85 struct pollfd pfd;
86 #endif /* NO_MSG_WAITALL */
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
87
88 b_read=0;
0ba367e - tcp major changes part 1: support for >1024 connection, better io p…
Andrei Pelinescu-Onciul authored
89 again:
3d4bb4b - MSG_WAITALL emulation for OSes that don't support it (win+cygwin)
Andrei Pelinescu-Onciul authored
90 #ifdef NO_MSG_WAITALL
91 if (flags & MSG_WAITALL){
92 n=-1;
93 goto poll_recv; /* simulate MSG_WAITALL */
94 }
95 #endif /* NO_MSG_WAITALL */
0ba367e - tcp major changes part 1: support for >1024 connection, better io p…
Andrei Pelinescu-Onciul authored
96 n=recv(socket, (char*)data, data_len, flags);
97 if (n<0){
98 /* error */
99 if (errno==EINTR) goto again; /* signal, try again */
100 /* on EAGAIN just return (let the caller know) */
101 if ((errno==EAGAIN)||(errno==EWOULDBLOCK)) return n;
102 LOG(L_CRIT, "ERROR: recv_all: 1st recv on %d failed: %s\n",
103 socket, strerror(errno));
104 return n;
105 }
106 b_read+=n;
107 while( (b_read!=data_len) && (n)){
3d4bb4b - MSG_WAITALL emulation for OSes that don't support it (win+cygwin)
Andrei Pelinescu-Onciul authored
108 #ifdef NO_MSG_WAITALL
109 /* cygwin & win do not support MSG_WAITALL => workaround using poll */
110 poll_recv:
111 n=recv(socket, (char*)data+b_read, data_len-b_read, 0);
112 #else /* NO_MSG_WAITALL */
b23a7a0 - warning and fixes (cfg.y $$=0 on error) merged from testing-0.8.12-r1
Andrei Pelinescu-Onciul authored
113 n=recv(socket, (char*)data+b_read, data_len-b_read, MSG_WAITALL);
3d4bb4b - MSG_WAITALL emulation for OSes that don't support it (win+cygwin)
Andrei Pelinescu-Onciul authored
114 #endif /* NO_MSG_WAITALL */
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
115 if (n<0){
116 /* error */
117 if (errno==EINTR) continue; /* signal, try again */
3d4bb4b - MSG_WAITALL emulation for OSes that don't support it (win+cygwin)
Andrei Pelinescu-Onciul authored
118 #ifdef NO_MSG_WAITALL
119 if (errno==EAGAIN || errno==EWOULDBLOCK){
120 /* emulate MSG_WAITALL using poll */
121 pfd.fd=socket;
122 pfd.events=POLLIN;
123 poll_retry:
124 n=poll(&pfd, 1, -1);
125 if (n<0){
126 if (errno==EINTR) goto poll_retry;
127 LOG(L_CRIT, "ERROR: recv_all: poll on %d failed: %s\n",
128 socket, strerror(errno));
129 return n;
130 } else continue; /* try recv again */
131 }
132 #endif /* NO_MSG_WAITALL */
0ba367e - tcp major changes part 1: support for >1024 connection, better io p…
Andrei Pelinescu-Onciul authored
133 LOG(L_CRIT, "ERROR: recv_all: 2nd recv on %d failed: %s\n",
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
134 socket, strerror(errno));
135 return n;
136 }
137 b_read+=n;
0ba367e - tcp major changes part 1: support for >1024 connection, better io p…
Andrei Pelinescu-Onciul authored
138 }
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
139 return b_read;
140 }
141
142
0ba367e - tcp major changes part 1: support for >1024 connection, better io p…
Andrei Pelinescu-Onciul authored
143
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
144 /* sends all data (takes care of signals) (assumes blocking fd)
145 * returns number of bytes sent or < 0 for an error */
146 int send_all(int socket, void* data, int data_len)
147 {
148 int n;
149
150 again:
151 n=send(socket, data, data_len, 0);
152 if (n<0){
153 /* error */
154 if (errno==EINTR) goto again; /* signal, try again */
2826050 - tcp children control fds are now non-blocking
Andrei Pelinescu-Onciul authored
155 if ((errno!=EAGAIN) &&(errno!=EWOULDBLOCK))
156 LOG(L_CRIT, "ERROR: send_all: send on %d failed: %s\n",
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
157 socket, strerror(errno));
158 }
159 return n;
160 }
161
162
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
163 /* at least 1 byte must be sent! */
164 int send_fd(int unix_socket, void* data, int data_len, int fd)
165 {
166 struct msghdr msg;
167 struct iovec iov[1];
168 int ret;
6c6659c - solaris fixes - ser + tcp compiles now on solaris
Andrei Pelinescu-Onciul authored
169 #ifdef HAVE_MSGHDR_MSG_CONTROL
a19584f core: pointer aliasing warnings fixed
Andrei Pelinescu-Onciul authored
170 int* pi;
6c6659c - solaris fixes - ser + tcp compiles now on solaris
Andrei Pelinescu-Onciul authored
171 struct cmsghdr* cmsg;
1d5ef18 - openbsd 64 tcp fix
Andrei Pelinescu-Onciul authored
172 /* make sure msg_control will point to properly aligned data */
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
173 union {
174 struct cmsghdr cm;
175 char control[CMSG_SPACE(sizeof(fd))];
176 }control_un;
177
178 msg.msg_control=control_un.control;
1d5ef18 - openbsd 64 tcp fix
Andrei Pelinescu-Onciul authored
179 /* openbsd doesn't like "more space", msg_controllen must not
180 * include the end padding */
181 msg.msg_controllen=CMSG_LEN(sizeof(fd));
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
182
6c6659c - solaris fixes - ser + tcp compiles now on solaris
Andrei Pelinescu-Onciul authored
183 cmsg=CMSG_FIRSTHDR(&msg);
184 cmsg->cmsg_level = SOL_SOCKET;
185 cmsg->cmsg_type = SCM_RIGHTS;
186 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
a19584f core: pointer aliasing warnings fixed
Andrei Pelinescu-Onciul authored
187 pi=(int*)CMSG_DATA(cmsg);
188 *pi=fd;
6c6659c - solaris fixes - ser + tcp compiles now on solaris
Andrei Pelinescu-Onciul authored
189 msg.msg_flags=0;
190 #else
191 msg.msg_accrights=(caddr_t) &fd;
192 msg.msg_accrightslen=sizeof(fd);
193 #endif
194
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
195 msg.msg_name=0;
196 msg.msg_namelen=0;
197
198 iov[0].iov_base=data;
199 iov[0].iov_len=data_len;
200 msg.msg_iov=iov;
201 msg.msg_iovlen=1;
202
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
203 again:
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
204 ret=sendmsg(unix_socket, &msg, 0);
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
205 if (ret<0){
206 if (errno==EINTR) goto again;
2826050 - tcp children control fds are now non-blocking
Andrei Pelinescu-Onciul authored
207 if ((errno!=EAGAIN) && (errno!=EWOULDBLOCK))
bf755a3 tcp: more complete error messages
Andrei Pelinescu-Onciul authored
208 LOG(L_CRIT, "ERROR: send_fd: sendmsg failed sending %d on %d:"
209 " %s (%d)\n", fd, unix_socket, strerror(errno), errno);
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
210 }
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
211
212 return ret;
213 }
214
215
216
0ba367e - tcp major changes part 1: support for >1024 connection, better io p…
Andrei Pelinescu-Onciul authored
217 /* receives a fd and data_len data
218 * params: unix_socket
219 * data
220 * data_len
221 * fd - will be set to the passed fd value or -1 if no fd
222 * was passed
223 * flags - 0, MSG_DONTWAIT, MSG_WAITALL; same as recv_all flags
224 * returns: bytes read on success, -1 on error (and sets errno) */
225 int receive_fd(int unix_socket, void* data, int data_len, int* fd, int flags)
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
226 {
227 struct msghdr msg;
228 struct iovec iov[1];
229 int new_fd;
230 int ret;
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
231 int n;
3d4bb4b - MSG_WAITALL emulation for OSes that don't support it (win+cygwin)
Andrei Pelinescu-Onciul authored
232 #ifdef NO_MSG_WAITALL
233 struct pollfd pfd;
234 int f;
235 #endif /*NO_MSG_WAITALL */
6c6659c - solaris fixes - ser + tcp compiles now on solaris
Andrei Pelinescu-Onciul authored
236 #ifdef HAVE_MSGHDR_MSG_CONTROL
a19584f core: pointer aliasing warnings fixed
Andrei Pelinescu-Onciul authored
237 int* pi;
6c6659c - solaris fixes - ser + tcp compiles now on solaris
Andrei Pelinescu-Onciul authored
238 struct cmsghdr* cmsg;
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
239 union{
240 struct cmsghdr cm;
241 char control[CMSG_SPACE(sizeof(new_fd))];
242 }control_un;
243
244 msg.msg_control=control_un.control;
245 msg.msg_controllen=sizeof(control_un.control);
6c6659c - solaris fixes - ser + tcp compiles now on solaris
Andrei Pelinescu-Onciul authored
246 #else
247 msg.msg_accrights=(caddr_t) &new_fd;
248 msg.msg_accrightslen=sizeof(int);
249 #endif
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
250
251 msg.msg_name=0;
252 msg.msg_namelen=0;
253
254 iov[0].iov_base=data;
255 iov[0].iov_len=data_len;
256 msg.msg_iov=iov;
257 msg.msg_iovlen=1;
258
3d4bb4b - MSG_WAITALL emulation for OSes that don't support it (win+cygwin)
Andrei Pelinescu-Onciul authored
259 #ifdef NO_MSG_WAITALL
260 f=flags & ~MSG_WAITALL;
261 #endif /* NO_MSG_WAITALL */
262
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
263 again:
3d4bb4b - MSG_WAITALL emulation for OSes that don't support it (win+cygwin)
Andrei Pelinescu-Onciul authored
264 #ifdef NO_MSG_WAITALL
265 ret=recvmsg(unix_socket, &msg, f);
266 #else /* NO_MSG_WAITALL */
267 ret=recvmsg(unix_socket, &msg, flags);
268 #endif /* NO_MSG_WAITALL */
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
269 if (ret<0){
270 if (errno==EINTR) goto again;
3d4bb4b - MSG_WAITALL emulation for OSes that don't support it (win+cygwin)
Andrei Pelinescu-Onciul authored
271 if ((errno==EAGAIN)||(errno==EWOULDBLOCK)){
272 #ifdef NO_MSG_WAITALL
273 if (flags & MSG_WAITALL){
274 /* emulate MSG_WAITALL using poll */
275 pfd.fd=unix_socket;
276 pfd.events=POLLIN;
277 poll_again:
278 ret=poll(&pfd, 1, -1);
279 if (ret>=0) goto again;
280 else if (errno==EINTR) goto poll_again;
281 LOG(L_CRIT, "ERROR: receive_fd: poll on %d failed: %s\n",
282 unix_socket, strerror(errno));
283 }
284 #endif /* NO_MSG_WAITALL */
285 goto error;
286 }
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
287 LOG(L_CRIT, "ERROR: receive_fd: recvmsg on %d failed: %s\n",
288 unix_socket, strerror(errno));
289 goto error;
290 }
291 if (ret==0){
292 /* EOF */
293 LOG(L_CRIT, "ERROR: receive_fd: EOF on %d\n", unix_socket);
294 goto error;
295 }
296 if (ret<data_len){
297 LOG(L_WARN, "WARNING: receive_fd: too few bytes read (%d from %d)"
298 "trying to fix...\n", ret, data_len);
0ba367e - tcp major changes part 1: support for >1024 connection, better io p…
Andrei Pelinescu-Onciul authored
299 /* blocking recv_all */
300 n=recv_all(unix_socket, (char*)data+ret, data_len-ret, MSG_WAITALL);
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
301 if (n>=0) ret+=n;
302 else{
303 ret=n;
304 goto error;
305 }
306 }
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
307
6c6659c - solaris fixes - ser + tcp compiles now on solaris
Andrei Pelinescu-Onciul authored
308 #ifdef HAVE_MSGHDR_MSG_CONTROL
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
309 cmsg=CMSG_FIRSTHDR(&msg);
310 if ((cmsg!=0) && (cmsg->cmsg_len==CMSG_LEN(sizeof(new_fd)))){
311 if (cmsg->cmsg_type!= SCM_RIGHTS){
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
312 LOG(L_ERR, "ERROR: receive_fd: msg control type != SCM_RIGHTS\n");
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
313 ret=-1;
314 goto error;
315 }
316 if (cmsg->cmsg_level!= SOL_SOCKET){
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
317 LOG(L_ERR, "ERROR: receive_fd: msg level != SOL_SOCKET\n");
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
318 ret=-1;
319 goto error;
320 }
a19584f core: pointer aliasing warnings fixed
Andrei Pelinescu-Onciul authored
321 pi=(int*) CMSG_DATA(cmsg);
322 *fd=*pi;
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
323 }else{
0ba367e - tcp major changes part 1: support for >1024 connection, better io p…
Andrei Pelinescu-Onciul authored
324 /*
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
325 LOG(L_ERR, "ERROR: receive_fd: no descriptor passed, cmsg=%p,"
0ba367e - tcp major changes part 1: support for >1024 connection, better io p…
Andrei Pelinescu-Onciul authored
326 "len=%d\n", cmsg, (unsigned)cmsg->cmsg_len); */
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
327 *fd=-1;
328 /* it's not really an error */
329 }
6c6659c - solaris fixes - ser + tcp compiles now on solaris
Andrei Pelinescu-Onciul authored
330 #else
331 if (msg.msg_accrightslen==sizeof(int)){
332 *fd=new_fd;
333 }else{
0ba367e - tcp major changes part 1: support for >1024 connection, better io p…
Andrei Pelinescu-Onciul authored
334 /*LOG(L_ERR, "ERROR: receive_fd: no descriptor passed,"
335 " accrightslen=%d\n", msg.msg_accrightslen); */
6c6659c - solaris fixes - ser + tcp compiles now on solaris
Andrei Pelinescu-Onciul authored
336 *fd=-1;
337 }
338 #endif
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
339
340 error:
341 return ret;
342 }
343 #endif
Something went wrong with that request. Please try again.