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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
// SOCK_Dgram.cpp
// $Id$
#define ACE_BUILD_DLL
#include "ace/SOCK_Dgram.h"
#include "ace/Handle_Set.h"
#include "ace/Synch.h"
ACE_ALLOC_HOOK_DEFINE(ACE_SOCK_Dgram)
void
ACE_SOCK_Dgram::dump (void) const
{
ACE_TRACE ("ACE_SOCK_Dgram::dump");
}
// Allows a client to read from a socket without having to provide a
// buffer to read. This method determines how much data is in the
// socket, allocates a buffer of this size, reads in the data, and
// returns the number of bytes read.
ssize_t
ACE_SOCK_Dgram::recv (iovec *io_vec,
ACE_Addr &addr,
int flags) const
{
ACE_TRACE ("ACE_SOCK_Dgram::recv");
#if defined (FIONREAD)
sockaddr *saddr = (sockaddr *) addr.get_addr ();
int addr_len = addr.get_size ();
u_long inlen;
if (ACE_OS::ioctl (this->get_handle (),
FIONREAD, (u_long *) &inlen) == -1)
return -1;
else if (inlen > 0)
{
ACE_NEW_RETURN (io_vec->iov_base, char[inlen], -1);
io_vec->iov_len = ACE_OS::recvfrom (this->get_handle (),
(char *) io_vec->iov_base,
inlen,
flags,
(sockaddr *) saddr,
&addr_len);
addr.set_size (addr_len);
return io_vec->iov_len;
}
else
return 0;
#else
ACE_UNUSED_ARG (flags);
ACE_UNUSED_ARG (addr);
ACE_UNUSED_ARG (io_vec);
ACE_NOTSUP_RETURN (-1);
#endif /* FIONREAD */
}
// Here's the shared open function. Note that if we are using the
// PF_INET protocol family and the address of LOCAL == the address of
// the special variable SAP_ANY then we are going to arbitrarily bind
// to a portnumber.
int
ACE_SOCK_Dgram::shared_open (const ACE_Addr &local,
int protocol_family)
{
ACE_TRACE ("ACE_SOCK_Dgram::shared_open");
int error = 0;
if (local == ACE_Addr::sap_any && protocol_family == PF_INET)
{
if (ACE::bind_port (this->get_handle ()) == -1)
error = 1;
}
else if (ACE_OS::bind (this->get_handle (), (sockaddr *) local.get_addr (),
local.get_size ()) == -1)
error = 1;
if (error)
this->close ();
return error ? -1 : 0;
}
// Here's the general-purpose constructor used by a connectionless
// datagram ``server''...
ACE_SOCK_Dgram::ACE_SOCK_Dgram (const ACE_Addr &local,
int protocol_family,
int protocol,
int reuse_addr)
: ACE_SOCK (SOCK_DGRAM, protocol_family, protocol, reuse_addr)
{
ACE_TRACE ("ACE_SOCK_Dgram::ACE_SOCK_Dgram");
if (this->shared_open (local, protocol_family) == -1)
ACE_ERROR ((LM_ERROR, "%p\n", "ACE_SOCK_Dgram"));
}
// Here's the general-purpose open routine.
int
ACE_SOCK_Dgram::open (const ACE_Addr &local,
int protocol_family,
int protocol,
int reuse_addr)
{
ACE_TRACE ("ACE_SOCK_Dgram::open");
if (ACE_SOCK::open (SOCK_DGRAM, protocol_family,
protocol, reuse_addr) == -1)
return -1;
else
return this->shared_open (local, protocol_family);
}
#if defined (ACE_HAS_MSG)
// Send an iovec of size N to ADDR as a datagram (connectionless
// version).
ssize_t
ACE_SOCK_Dgram::send (const iovec iov[],
size_t n,
const ACE_Addr &addr,
int flags) const
{
ACE_TRACE ("ACE_SOCK_Dgram::send");
msghdr send_msg;
send_msg.msg_iov = (iovec *) iov;
send_msg.msg_iovlen = n;
#if defined (ACE_HAS_SOCKADDR_MSG_NAME)
send_msg.msg_name = (struct sockaddr *) addr.get_addr ();
#else
send_msg.msg_name = (char *) addr.get_addr ();
#endif /* ACE_HAS_SOCKADDR_MSG_NAME */
send_msg.msg_namelen = addr.get_size ();
#if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG)
send_msg.msg_control = 0;
send_msg.msg_controllen = 0;
send_msg.msg_flags = 0;
#else
send_msg.msg_accrights = 0;
send_msg.msg_accrightslen = 0;
#endif /* ACE_HAS_4_4BSD_SENDMSG_RECVMSG */
return ACE_OS::sendmsg (this->get_handle (), &send_msg, flags);
}
// Recv an iovec of size N to ADDR as a datagram (connectionless
// version).
ssize_t
ACE_SOCK_Dgram::recv (iovec iov[],
size_t n,
ACE_Addr &addr,
int flags) const
{
ACE_TRACE ("ACE_SOCK_Dgram::recv");
msghdr recv_msg;
recv_msg.msg_iov = (iovec *) iov;
recv_msg.msg_iovlen = n;
#if defined (ACE_HAS_SOCKADDR_MSG_NAME)
recv_msg.msg_name = (struct sockaddr *) addr.get_addr ();
#else
recv_msg.msg_name = (char *) addr.get_addr ();
#endif /* ACE_HAS_SOCKADDR_MSG_NAME */
recv_msg.msg_namelen = addr.get_size ();
#if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG)
recv_msg.msg_control = 0 ;
recv_msg.msg_controllen = 0 ;
#else
recv_msg.msg_accrights = 0;
recv_msg.msg_accrightslen = 0;
#endif /* ACE_HAS_4_4BSD_SENDMSG_RECVMSG */
ssize_t status = ACE_OS::recvmsg (this->get_handle (),
&recv_msg, flags);
addr.set_size (recv_msg.msg_namelen);
return status;
}
#else /* ACE_HAS_MSG */
// Send an iovec of size N to ADDR as a datagram (connectionless
// version).
ssize_t
ACE_SOCK_Dgram::send (const iovec iov[],
size_t n,
const ACE_Addr &addr,
int flags) const
{
ACE_TRACE ("ACE_SOCK_Dgram::send");
size_t length = 0;
size_t i;
// Determine the total length of all the buffers in <iov>.
for (i = 0; i < n; i++)
if (iov[i].iov_len < 0)
return -1;
else
length += iov[i].iov_len;
char *buf;
#if defined (ACE_HAS_ALLOCA)
buf = alloca (length);
#else
ACE_NEW_RETURN (buf, char[length], -1);
#endif /* !defined (ACE_HAS_ALLOCA) */
char *ptr = buf;
for (i = 0; i < n; i++)
{
ACE_OS::memcpy (ptr, iov[i].iov_base, iov[i].iov_len);
ptr += iov[i].iov_len;
}
ssize_t result = ACE_SOCK_Dgram::send (buf, length, addr, flags);
#if !defined (ACE_HAS_ALLOCA)
delete [] buf;
#endif /* !defined (ACE_HAS_ALLOCA) */
return result;
}
// Recv an iovec of size N to ADDR as a datagram (connectionless
// version).
ssize_t
ACE_SOCK_Dgram::recv (iovec iov[],
size_t n,
ACE_Addr &addr,
int flags) const
{
ACE_TRACE ("ACE_SOCK_Dgram::recv");
ssize_t length = 0;
size_t i;
for (i = 0; i < n; i++)
if (iov[i].iov_len < 0)
return -1;
else
length += iov[i].iov_len;
char *buf;
#if defined (ACE_HAS_ALLOCA)
buf = alloca (length);
#else
ACE_NEW_RETURN (buf, char[length], -1);
#endif /* !defined (ACE_HAS_ALLOCA) */
length = ACE_SOCK_Dgram::recv (buf, length, addr, flags);
if (length != -1)
{
char *ptr = buf;
int copyn = length;
for (i = 0;
i < n && copyn > 0;
i++)
{
ACE_OS::memcpy (iov[i].iov_base, ptr,
// iov_len is int on some platforms, size_t on others
copyn > (int) iov[i].iov_len
? (size_t) iov[i].iov_len
: (size_t) copyn);
ptr += iov[i].iov_len;
copyn -= iov[i].iov_len;
}
}
#if !defined (ACE_HAS_ALLOCA)
delete [] buf;
#endif /* !defined (ACE_HAS_ALLOCA) */
return length;
}
#endif /* ACE_HAS_MSG */
ssize_t
ACE_SOCK_Dgram::recv (void *buf,
size_t n,
ACE_Addr &addr,
int flags,
const ACE_Time_Value *timeout) const
{
ACE_Handle_Set handle_set;
handle_set.reset ();
handle_set.set_bit (this->get_handle ());
// Check the status of the current socket.
switch (ACE_OS::select (int (this->get_handle ()) + 1,
handle_set,
0, 0,
timeout))
{
case -1:
return -1;
/* NOTREACHED */
case 0:
errno = ETIME;
return -1;
/* NOTREACHED */
default:
// Goes fine, call <recv> to get data
return this->recv (buf, n, addr, flags);
}
}
|