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
|
/*
* Copyright (c) 2006-2026, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
#include "af_unix_internal.h"
int af_unix_dgram_connect(struct af_unix_socket *sock,
const struct sockaddr *address, socklen_t length)
{
int result = 0;
struct sockaddr_un peer_address;
socklen_t peer_length;
struct af_unix_socket *peer;
peer = af_unix_namespace_lookup(address, length, SOCK_DGRAM,
&peer_address, &peer_length);
if (peer == RT_NULL)
{
return -1;
}
af_unix_lock();
if (sock->closed)
{
result = af_unix_error(EBADF);
}
else if (sock->write_shutdown)
{
result = af_unix_error(EPIPE);
}
else
{
af_unix_set_peer_locked(sock, peer);
sock->peer_address = peer_address;
sock->peer_length = peer_length;
sock->connected = 1;
}
af_unix_unlock();
af_unix_socket_put(peer);
return result;
}
static struct af_unix_socket *af_unix_dgram_target_get(
struct af_unix_socket *sock, const struct sockaddr *to,
socklen_t to_length)
{
struct af_unix_socket *peer = RT_NULL;
if (to != RT_NULL)
{
return af_unix_namespace_lookup(to, to_length, SOCK_DGRAM,
RT_NULL, RT_NULL);
}
af_unix_lock();
if (sock->connected && sock->peer != RT_NULL)
{
peer = sock->peer;
af_unix_socket_ref_locked(peer);
}
af_unix_unlock();
if (peer == RT_NULL)
{
af_unix_error(EDESTADDRREQ);
}
return peer;
}
int af_unix_dgram_send(struct af_unix_socket *sock, const void *data,
size_t size, int flags, const struct sockaddr *to,
socklen_t to_length, struct af_unix_rights *rights)
{
int error;
int result;
int nonblocking;
int timeout;
struct af_unix_socket *peer;
struct af_unix_message *message;
if (size > AF_UNIX_DGRAM_MAX_SIZE)
{
return af_unix_error(EMSGSIZE);
}
if (size != 0 && data == RT_NULL)
{
return af_unix_error(EINVAL);
}
peer = af_unix_dgram_target_get(sock, to, to_length);
if (peer == RT_NULL)
{
return -1;
}
message = (struct af_unix_message *)rt_malloc(sizeof(*message) + size);
if (message == RT_NULL)
{
af_unix_socket_put(peer);
return af_unix_error(ENOMEM);
}
rt_list_init(&message->node);
message->length = size;
message->rights = rights;
if (size != 0)
{
rt_memcpy(message->data, data, size);
}
for (;;)
{
af_unix_lock();
if (sock->closed)
{
result = af_unix_error(EBADF);
af_unix_unlock();
break;
}
if (sock->write_shutdown)
{
result = af_unix_error(EPIPE);
af_unix_unlock();
break;
}
if (peer->closed)
{
result = af_unix_error(ECONNREFUSED);
af_unix_unlock();
break;
}
if (peer->read_shutdown)
{
result = af_unix_error(EPIPE);
af_unix_unlock();
break;
}
if (peer->message_count < AF_UNIX_DGRAM_QUEUE_LEN)
{
message->source = sock->local_address;
message->source_length = sock->local_length;
rt_list_insert_before(&peer->message_queue, &message->node);
peer->message_count++;
rt_wqueue_wakeup_all(&peer->wait_queue,
(void *)(rt_ubase_t)POLLIN);
af_unix_unlock();
af_unix_socket_put(peer);
return (int)size;
}
nonblocking = af_unix_is_nonblocking(sock, flags);
timeout = sock->send_timeout;
af_unix_unlock();
if (nonblocking)
{
result = af_unix_error(EAGAIN);
break;
}
if (af_unix_wait(&peer->wait_queue, timeout) < 0)
{
result = -1;
break;
}
}
error = rt_get_errno();
rt_free(message);
af_unix_socket_put(peer);
rt_set_errno(error);
return result;
}
int af_unix_dgram_receive(struct af_unix_socket *sock, void *data,
size_t size, int flags, struct sockaddr *from,
socklen_t *from_length, rt_list_t *rights)
{
int result;
int nonblocking;
int timeout;
size_t copy_length;
struct af_unix_message *message;
if (size != 0 && data == RT_NULL)
{
return af_unix_error(EINVAL);
}
for (;;)
{
af_unix_lock();
if (sock->closed)
{
af_unix_unlock();
return af_unix_error(EBADF);
}
if (!rt_list_isempty(&sock->message_queue))
{
message = rt_list_entry(sock->message_queue.next,
struct af_unix_message, node);
copy_length = message->length;
if (copy_length > size)
{
copy_length = size;
}
if (copy_length != 0)
{
rt_memcpy(data, message->data, copy_length);
}
result = af_unix_address_copy(from, from_length,
&message->source,
message->source_length);
if (result < 0)
{
af_unix_unlock();
return result;
}
if ((flags & MSG_PEEK) == 0)
{
rt_list_remove(&message->node);
sock->message_count--;
if (message->rights != RT_NULL)
{
rt_list_insert_before(rights, &message->rights->node);
message->rights = RT_NULL;
}
af_unix_wakeup_writable_locked(sock);
rt_free(message);
}
af_unix_unlock();
return (int)copy_length;
}
if (sock->read_shutdown)
{
af_unix_unlock();
return 0;
}
nonblocking = af_unix_is_nonblocking(sock, flags);
timeout = sock->receive_timeout;
af_unix_unlock();
if (nonblocking)
{
return af_unix_error(EAGAIN);
}
if (af_unix_wait(&sock->wait_queue, timeout) < 0)
{
return -1;
}
}
}
|