forked from OpenSIPS/opensips
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpass_fd.c
More file actions
261 lines (233 loc) · 6.59 KB
/
Copy pathpass_fd.c
File metadata and controls
261 lines (233 loc) · 6.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
* $Id$
*
* Copyright (C) 2001-2003 FhG Fokus
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* History:
* --------
* 2002-11-29 created by andrei
* 2003-02-20 added solaris support (! HAVE_MSGHDR_MSG_CONTROL) (andrei)
* 2003-11-03 added send_all, recv_all and updated send/get_fd
* to handle signals (andrei)
*/
#ifdef USE_TCP
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <stdlib.h> /* for NULL definition on openbsd */
#include <errno.h>
#include <string.h>
#include "dprint.h"
/* receive all the data or returns error (handles EINTR etc.)
* params: socket
* data - buffer for the results
* data_len -
* flags - recv flags for the first recv (see recv(2)), only
* 0, MSG_WAITALL and MSG_DONTWAIT make sense
* if flags is set to MSG_DONWAIT (or to 0 and the socket fd is non-blocking),
* and if no data is queued on the fd, recv_all will not wait (it will
* return error and set errno to EAGAIN/EWOULDBLOCK). However if even 1 byte
* is queued, the call will block until the whole data_len was read or an
* error or eof occured ("semi-nonblocking" behaviour, some tcp code
* counts on it).
* if flags is set to MSG_WAITALL it will block even if no byte is available.
*
* returns: bytes read or error (<0)
* can return < data_len if EOF */
int recv_all(int socket, void* data, int data_len, int flags)
{
int b_read;
int n;
b_read=0;
again:
n=recv(socket, (char*)data, data_len, flags);
if (n<0){
/* error */
if (errno==EINTR) goto again; /* signal, try again */
/* on EAGAIN just return (let the caller know) */
if ((errno==EAGAIN)||(errno==EWOULDBLOCK)) return n;
LM_CRIT("1st recv on %d failed: %s\n", socket, strerror(errno));
return n;
}
b_read+=n;
while( (b_read!=data_len) && (n)){
n=recv(socket, (char*)data+b_read, data_len-b_read, MSG_WAITALL);
if (n<0){
/* error */
if (errno==EINTR) continue; /* signal, try again */
LM_CRIT("2nd recv on %d failed: %s\n",
socket, strerror(errno));
return n;
}
b_read+=n;
}
return b_read;
}
/* sends all data (takes care of signals) (assumes blocking fd)
* returns number of bytes sent or < 0 for an error */
int send_all(int socket, void* data, int data_len)
{
int n;
again:
n=send(socket, data, data_len, 0);
if (n<0){
/* error */
if (errno==EINTR) goto again; /* signal, try again */
LM_CRIT("send on %d failed: %s\n", socket, strerror(errno));
}
return n;
}
/* at least 1 byte must be sent! */
int send_fd(int unix_socket, void* data, int data_len, int fd)
{
struct msghdr msg;
struct iovec iov[1];
int ret;
#ifdef HAVE_MSGHDR_MSG_CONTROL
struct cmsghdr* cmsg;
/* make sure msg_control will point to properly aligned data */
union {
struct cmsghdr cm;
char control[CMSG_SPACE(sizeof(fd))];
}control_un;
msg.msg_control=control_un.control;
/* openbsd doesn't like "more space", msg_controllen must not
* include the end padding */
msg.msg_controllen=CMSG_LEN(sizeof(fd));
cmsg=CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
*(int*)CMSG_DATA(cmsg)=fd;
msg.msg_flags=0;
#else
msg.msg_accrights=(caddr_t) &fd;
msg.msg_accrightslen=sizeof(fd);
#endif
msg.msg_name=0;
msg.msg_namelen=0;
iov[0].iov_base=data;
iov[0].iov_len=data_len;
msg.msg_iov=iov;
msg.msg_iovlen=1;
again:
ret=sendmsg(unix_socket, &msg, 0);
if (ret<0){
if (errno==EINTR) goto again;
if (errno==EAGAIN || errno==EWOULDBLOCK) {
LM_ERR("sendmsg would block on %d: %s\n",
unix_socket, strerror(errno));
} else {
LM_CRIT("sendmsg failed on %d: %s\n",
unix_socket, strerror(errno));
}
}
return ret;
}
/* receives a fd and data_len data
* params: unix_socket
* data
* data_len
* fd - will be set to the passed fd value or -1 if no fd
* was passed
* flags - 0, MSG_DONTWAIT, MSG_WAITALL; same as recv_all flags
* returns: bytes read on success, -1 on error (and sets errno) */
int receive_fd(int unix_socket, void* data, int data_len, int* fd, int flags)
{
struct msghdr msg;
struct iovec iov[1];
int new_fd;
int ret;
int n;
#ifdef HAVE_MSGHDR_MSG_CONTROL
struct cmsghdr* cmsg;
union{
struct cmsghdr cm;
char control[CMSG_SPACE(sizeof(new_fd))];
}control_un;
msg.msg_control=control_un.control;
msg.msg_controllen=sizeof(control_un.control);
#else
msg.msg_accrights=(caddr_t) &new_fd;
msg.msg_accrightslen=sizeof(int);
#endif
msg.msg_name=0;
msg.msg_namelen=0;
iov[0].iov_base=data;
iov[0].iov_len=data_len;
msg.msg_iov=iov;
msg.msg_iovlen=1;
again:
ret=recvmsg(unix_socket, &msg, flags);
if (ret<0){
if (errno==EINTR) goto again;
if ((errno==EAGAIN)||(errno==EWOULDBLOCK)) goto error;
LM_CRIT("recvmsg on %d failed: %s\n", unix_socket, strerror(errno));
goto error;
}
if (ret==0){
/* EOF */
LM_CRIT("EOF on %d\n", unix_socket);
goto error;
}
if (ret<data_len){
LM_WARN("too few bytes read (%d from %d) trying to fix...\n",
ret, data_len);
/* blocking recv_all */
n=recv_all(unix_socket, (char*)data+ret, data_len-ret, MSG_WAITALL);
if (n>=0) ret+=n;
else{
ret=n;
goto error;
}
}
#ifdef HAVE_MSGHDR_MSG_CONTROL
cmsg=CMSG_FIRSTHDR(&msg);
if ((cmsg!=0) && (cmsg->cmsg_len==CMSG_LEN(sizeof(new_fd)))){
if (cmsg->cmsg_type!= SCM_RIGHTS){
LM_ERR("msg control type != SCM_RIGHTS\n");
ret=-1;
goto error;
}
if (cmsg->cmsg_level!= SOL_SOCKET){
LM_ERR("msg level != SOL_SOCKET\n");
ret=-1;
goto error;
}
*fd=*((int*) CMSG_DATA(cmsg));
}else{
/*
LM_ERR("no descriptor passed, cmsg=%p,len=%d\n",
cmsg, (unsigned)cmsg->cmsg_len); */
*fd=-1;
/* it's not really an error */
}
#else
if (msg.msg_accrightslen==sizeof(int)){
*fd=new_fd;
}else{
/*LM_ERR("no descriptor passed, accrightslen=%d\n",
msg.msg_accrightslen); */
*fd=-1;
}
#endif
error:
return ret;
}
#endif