Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 270 lines (241 sloc) 7.322 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 poll...
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)
6c6659c - solaris fixes - ser + tcp compiles now on solaris
Andrei Pelinescu-Onciul authored
36 */
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
37
38 #ifdef USE_TCP
39
40 #include <sys/types.h>
41 #include <sys/socket.h>
b988dae - compile fixes: ser & tm + tcp compiles on linux, freebsd, openbsd & ne...
Andrei Pelinescu-Onciul authored
42 #include <sys/uio.h>
43 #include <stdlib.h> /* for NULL definition on openbsd */
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
44 #include <errno.h>
45 #include <string.h>
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
46
b988dae - compile fixes: ser & tm + tcp compiles on linux, freebsd, openbsd & ne...
Andrei Pelinescu-Onciul authored
47 #include "dprint.h"
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
48
49
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
50
51 /* receive all the data or returns error (handles EINTR etc.)
0ba367e - tcp major changes part 1: support for >1024 connection, better io poll...
Andrei Pelinescu-Onciul authored
52 * params: socket
53 * data - buffer for the results
54 * data_len -
55 * flags - recv flags for the first recv (see recv(2)), only
56 * 0, MSG_WAITALL and MSG_DONTWAIT make sense
57 * if flags is set to MSG_DONWAIT (or to 0 and the socket fd is non-blocking),
58 * and if no data is queued on the fd, recv_all will not wait (it will
59 * return error and set errno to EAGAIN/EWOULDBLOCK). However if even 1 byte
60 * is queued, the call will block until the whole data_len was read or an
61 * error or eof occured ("semi-nonblocking" behaviour, some tcp code
62 * counts on it).
63 * if flags is set to MSG_WAITALL it will block even if no byte is available.
64 *
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
65 * returns: bytes read or error (<0)
66 * can return < data_len if EOF */
0ba367e - tcp major changes part 1: support for >1024 connection, better io poll...
Andrei Pelinescu-Onciul authored
67 int recv_all(int socket, void* data, int data_len, int flags)
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
68 {
69 int b_read;
70 int n;
71
72 b_read=0;
0ba367e - tcp major changes part 1: support for >1024 connection, better io poll...
Andrei Pelinescu-Onciul authored
73 again:
74 n=recv(socket, (char*)data, data_len, flags);
75 if (n<0){
76 /* error */
77 if (errno==EINTR) goto again; /* signal, try again */
78 /* on EAGAIN just return (let the caller know) */
79 if ((errno==EAGAIN)||(errno==EWOULDBLOCK)) return n;
80 LOG(L_CRIT, "ERROR: recv_all: 1st recv on %d failed: %s\n",
81 socket, strerror(errno));
82 return n;
83 }
84 b_read+=n;
85 while( (b_read!=data_len) && (n)){
b23a7a0 - warning and fixes (cfg.y $$=0 on error) merged from testing-0.8.12-r1
Andrei Pelinescu-Onciul authored
86 n=recv(socket, (char*)data+b_read, data_len-b_read, MSG_WAITALL);
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
87 if (n<0){
88 /* error */
89 if (errno==EINTR) continue; /* signal, try again */
0ba367e - tcp major changes part 1: support for >1024 connection, better io poll...
Andrei Pelinescu-Onciul authored
90 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
91 socket, strerror(errno));
92 return n;
93 }
94 b_read+=n;
0ba367e - tcp major changes part 1: support for >1024 connection, better io poll...
Andrei Pelinescu-Onciul authored
95 }
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
96 return b_read;
97 }
98
99
0ba367e - tcp major changes part 1: support for >1024 connection, better io poll...
Andrei Pelinescu-Onciul authored
100
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
101 /* sends all data (takes care of signals) (assumes blocking fd)
102 * returns number of bytes sent or < 0 for an error */
103 int send_all(int socket, void* data, int data_len)
104 {
105 int n;
106
107 again:
108 n=send(socket, data, data_len, 0);
109 if (n<0){
110 /* error */
111 if (errno==EINTR) goto again; /* signal, try again */
2826050 - tcp children control fds are now non-blocking
Andrei Pelinescu-Onciul authored
112 if ((errno!=EAGAIN) &&(errno!=EWOULDBLOCK))
113 LOG(L_CRIT, "ERROR: send_all: send on %d failed: %s\n",
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
114 socket, strerror(errno));
115 }
116 return n;
117 }
118
119
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
120 /* at least 1 byte must be sent! */
121 int send_fd(int unix_socket, void* data, int data_len, int fd)
122 {
123 struct msghdr msg;
124 struct iovec iov[1];
125 int ret;
6c6659c - solaris fixes - ser + tcp compiles now on solaris
Andrei Pelinescu-Onciul authored
126 #ifdef HAVE_MSGHDR_MSG_CONTROL
127 struct cmsghdr* cmsg;
1d5ef18 - openbsd 64 tcp fix
Andrei Pelinescu-Onciul authored
128 /* make sure msg_control will point to properly aligned data */
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
129 union {
130 struct cmsghdr cm;
131 char control[CMSG_SPACE(sizeof(fd))];
132 }control_un;
133
134 msg.msg_control=control_un.control;
1d5ef18 - openbsd 64 tcp fix
Andrei Pelinescu-Onciul authored
135 /* openbsd doesn't like "more space", msg_controllen must not
136 * include the end padding */
137 msg.msg_controllen=CMSG_LEN(sizeof(fd));
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
138
6c6659c - solaris fixes - ser + tcp compiles now on solaris
Andrei Pelinescu-Onciul authored
139 cmsg=CMSG_FIRSTHDR(&msg);
140 cmsg->cmsg_level = SOL_SOCKET;
141 cmsg->cmsg_type = SCM_RIGHTS;
142 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
143 *(int*)CMSG_DATA(cmsg)=fd;
144 msg.msg_flags=0;
145 #else
146 msg.msg_accrights=(caddr_t) &fd;
147 msg.msg_accrightslen=sizeof(fd);
148 #endif
149
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
150 msg.msg_name=0;
151 msg.msg_namelen=0;
152
153 iov[0].iov_base=data;
154 iov[0].iov_len=data_len;
155 msg.msg_iov=iov;
156 msg.msg_iovlen=1;
157
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
158 again:
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
159 ret=sendmsg(unix_socket, &msg, 0);
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
160 if (ret<0){
161 if (errno==EINTR) goto again;
2826050 - tcp children control fds are now non-blocking
Andrei Pelinescu-Onciul authored
162 if ((errno!=EAGAIN) && (errno!=EWOULDBLOCK))
163 LOG(L_CRIT, "ERROR: send_fd: sendmsg failed on %d: %s\n",
164 unix_socket, strerror(errno));
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
165 }
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
166
167 return ret;
168 }
169
170
171
0ba367e - tcp major changes part 1: support for >1024 connection, better io poll...
Andrei Pelinescu-Onciul authored
172 /* receives a fd and data_len data
173 * params: unix_socket
174 * data
175 * data_len
176 * fd - will be set to the passed fd value or -1 if no fd
177 * was passed
178 * flags - 0, MSG_DONTWAIT, MSG_WAITALL; same as recv_all flags
179 * returns: bytes read on success, -1 on error (and sets errno) */
180 int receive_fd(int unix_socket, void* data, int data_len, int* fd, int flags)
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
181 {
182 struct msghdr msg;
183 struct iovec iov[1];
184 int new_fd;
185 int ret;
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
186 int n;
6c6659c - solaris fixes - ser + tcp compiles now on solaris
Andrei Pelinescu-Onciul authored
187 #ifdef HAVE_MSGHDR_MSG_CONTROL
188 struct cmsghdr* cmsg;
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
189 union{
190 struct cmsghdr cm;
191 char control[CMSG_SPACE(sizeof(new_fd))];
192 }control_un;
193
194 msg.msg_control=control_un.control;
195 msg.msg_controllen=sizeof(control_un.control);
6c6659c - solaris fixes - ser + tcp compiles now on solaris
Andrei Pelinescu-Onciul authored
196 #else
197 msg.msg_accrights=(caddr_t) &new_fd;
198 msg.msg_accrightslen=sizeof(int);
199 #endif
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
200
201 msg.msg_name=0;
202 msg.msg_namelen=0;
203
204 iov[0].iov_base=data;
205 iov[0].iov_len=data_len;
206 msg.msg_iov=iov;
207 msg.msg_iovlen=1;
208
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
209 again:
0ba367e - tcp major changes part 1: support for >1024 connection, better io poll...
Andrei Pelinescu-Onciul authored
210 ret=recvmsg(unix_socket, &msg, flags);
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
211 if (ret<0){
212 if (errno==EINTR) goto again;
0ba367e - tcp major changes part 1: support for >1024 connection, better io poll...
Andrei Pelinescu-Onciul authored
213 if ((errno==EAGAIN)||(errno==EWOULDBLOCK)) goto error;
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
214 LOG(L_CRIT, "ERROR: receive_fd: recvmsg on %d failed: %s\n",
215 unix_socket, strerror(errno));
216 goto error;
217 }
218 if (ret==0){
219 /* EOF */
220 LOG(L_CRIT, "ERROR: receive_fd: EOF on %d\n", unix_socket);
221 goto error;
222 }
223 if (ret<data_len){
224 LOG(L_WARN, "WARNING: receive_fd: too few bytes read (%d from %d)"
225 "trying to fix...\n", ret, data_len);
0ba367e - tcp major changes part 1: support for >1024 connection, better io poll...
Andrei Pelinescu-Onciul authored
226 /* blocking recv_all */
227 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
228 if (n>=0) ret+=n;
229 else{
230 ret=n;
231 goto error;
232 }
233 }
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
234
6c6659c - solaris fixes - ser + tcp compiles now on solaris
Andrei Pelinescu-Onciul authored
235 #ifdef HAVE_MSGHDR_MSG_CONTROL
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
236 cmsg=CMSG_FIRSTHDR(&msg);
237 if ((cmsg!=0) && (cmsg->cmsg_len==CMSG_LEN(sizeof(new_fd)))){
238 if (cmsg->cmsg_type!= SCM_RIGHTS){
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
239 LOG(L_ERR, "ERROR: receive_fd: msg control type != SCM_RIGHTS\n");
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
240 ret=-1;
241 goto error;
242 }
243 if (cmsg->cmsg_level!= SOL_SOCKET){
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
244 LOG(L_ERR, "ERROR: receive_fd: msg level != SOL_SOCKET\n");
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
245 ret=-1;
246 goto error;
247 }
248 *fd=*((int*) CMSG_DATA(cmsg));
249 }else{
0ba367e - tcp major changes part 1: support for >1024 connection, better io poll...
Andrei Pelinescu-Onciul authored
250 /*
06aaa54 - merge from testing-0.8.12-r0:
Andrei Pelinescu-Onciul authored
251 LOG(L_ERR, "ERROR: receive_fd: no descriptor passed, cmsg=%p,"
0ba367e - tcp major changes part 1: support for >1024 connection, better io poll...
Andrei Pelinescu-Onciul authored
252 "len=%d\n", cmsg, (unsigned)cmsg->cmsg_len); */
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
253 *fd=-1;
254 /* it's not really an error */
255 }
6c6659c - solaris fixes - ser + tcp compiles now on solaris
Andrei Pelinescu-Onciul authored
256 #else
257 if (msg.msg_accrightslen==sizeof(int)){
258 *fd=new_fd;
259 }else{
0ba367e - tcp major changes part 1: support for >1024 connection, better io poll...
Andrei Pelinescu-Onciul authored
260 /*LOG(L_ERR, "ERROR: receive_fd: no descriptor passed,"
261 " accrightslen=%d\n", msg.msg_accrightslen); */
6c6659c - solaris fixes - ser + tcp compiles now on solaris
Andrei Pelinescu-Onciul authored
262 *fd=-1;
263 }
264 #endif
5b532c7 - preliminary tcp support
Andrei Pelinescu-Onciul authored
265
266 error:
267 return ret;
268 }
269 #endif
Something went wrong with that request. Please try again.