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
#!/usr/bin/env python
# Copyright (c) 2016 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
@media only all and (prefers-color-scheme: dark) {
.highlight .hll { background-color: #49483e }
.highlight .c { color: #75715e } /* Comment */
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
.highlight .k { color: #66d9ef } /* Keyword */
.highlight .l { color: #ae81ff } /* Literal */
.highlight .n { color: #f8f8f2 } /* Name */
.highlight .o { color: #f92672 } /* Operator */
.highlight .p { color: #f8f8f2 } /* Punctuation */
.highlight .ch { color: #75715e } /* Comment.Hashbang */
.highlight .cm { color: #75715e } /* Comment.Multiline */
.highlight .cp { color: #75715e } /* Comment.Preproc */
.highlight .cpf { color: #75715e } /* Comment.PreprocFile */
.highlight .c1 { color: #75715e } /* Comment.Single */
.highlight .cs { color: #75715e } /* Comment.Special */
.highlight .gd { color: #f92672 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #75715e } /* Generic.Subheading */
.highlight .kc { color: #66d9ef } /* Keyword.Constant */
.highlight .kd { color: #66d9ef } /* Keyword.Declaration */
.highlight .kn { color: #f92672 } /* Keyword.Namespace */
.highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
.highlight .kr { color: #66d9ef } /* Keyword.Reserved */
.highlight .kt { color: #66d9ef } /* Keyword.Type */
.highlight .ld { color: #e6db74 } /* Literal.Date */
.highlight .m { color: #ae81ff } /* Literal.Number */
.highlight .s { color: #e6db74 } /* Literal.String */
.highlight .na { color: #a6e22e } /* Name.Attribute */
.highlight .nb { color: #f8f8f2 } /* Name.Builtin */
.highlight .nc { color: #a6e22e } /* Name.Class */
.highlight .no { color: #66d9ef } /* Name.Constant */
.highlight .nd { color: #a6e22e } /* Name.Decorator */
.highlight .ni { color: #f8f8f2 } /* Name.Entity */
.highlight .ne { color: #a6e22e } /* Name.Exception */
.highlight .nf { color: #a6e22e } /* Name.Function */
.highlight .nl { color: #f8f8f2 } /* Name.Label */
.highlight .nn { color: #f8f8f2 } /* Name.Namespace */
.highlight .nx { color: #a6e22e } /* Name.Other */
.highlight .py { color: #f8f8f2 } /* Name.Property */
.highlight .nt { color: #f92672 } /* Name.Tag */
.highlight .nv { color: #f8f8f2 } /* Name.Variable */
.highlight .ow { color: #f92672 } /* Operator.Word */
.highlight .w { color: #f8f8f2 } /* Text.Whitespace */
.highlight .mb { color: #ae81ff } /* Literal.Number.Bin */
.highlight .mf { color: #ae81ff } /* Literal.Number.Float */
.highlight .mh { color: #ae81ff } /* Literal.Number.Hex */
.highlight .mi { color: #ae81ff } /* Literal.Number.Integer */
.highlight .mo { color: #ae81ff } /* Literal.Number.Oct */
.highlight .sa { color: #e6db74 } /* Literal.String.Affix */
.highlight .sb { color: #e6db74 } /* Literal.String.Backtick */
.highlight .sc { color: #e6db74 } /* Literal.String.Char */
.highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */
.highlight .sd { color: #e6db74 } /* Literal.String.Doc */
.highlight .s2 { color: #e6db74 } /* Literal.String.Double */
.highlight .se { color: #ae81ff } /* Literal.String.Escape */
.highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */
.highlight .si { color: #e6db74 } /* Literal.String.Interpol */
.highlight .sx { color: #e6db74 } /* Literal.String.Other */
.highlight .sr { color: #e6db74 } /* Literal.String.Regex */
.highlight .s1 { color: #e6db74 } /* Literal.String.Single */
.highlight .ss { color: #e6db74 } /* Literal.String.Symbol */
.highlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #a6e22e } /* Name.Function.Magic */
.highlight .vc { color: #f8f8f2 } /* Name.Variable.Class */
.highlight .vg { color: #f8f8f2 } /* Name.Variable.Global */
.highlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */
.highlight .vm { color: #f8f8f2 } /* Name.Variable.Magic */
.highlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */
}
@media (prefers-color-scheme: light) {
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
}
/*
* Copyright (c) 2015 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
Copyright (c) 2001, 2002, 2003, 2005 Eliot Dresselhaus
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <string.h> /* strchr */
#define __USE_GNU
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#include <vppinfra/mem.h>
#include <vppinfra/vec.h>
#include <vppinfra/socket.h>
#include <vppinfra/format.h>
#include <vppinfra/error.h>
void
clib_socket_tx_add_formatted (clib_socket_t * s, char *fmt, ...)
{
va_list va;
va_start (va, fmt);
clib_socket_tx_add_va_formatted (s, fmt, &va);
va_end (va);
}
/* Return and bind to an unused port. */
static word
find_free_port (word sock)
{
word port;
for (port = IPPORT_USERRESERVED; port < 1 << 16; port++)
{
struct sockaddr_in a;
memset (&a, 0, sizeof (a)); /* Warnings be gone */
a.sin_family = PF_INET;
a.sin_addr.s_addr = INADDR_ANY;
a.sin_port = htons (port);
if (bind (sock, (struct sockaddr *) &a, sizeof (a)) >= 0)
break;
}
return port < 1 << 16 ? port : -1;
}
/* Convert a config string to a struct sockaddr and length for use
with bind or connect. */
static clib_error_t *
socket_config (char *config,
void *addr, socklen_t * addr_len, u32 ip4_default_address)
{
clib_error_t *error = 0;
if (!config)
config = "";
/* Anything that begins with a / is a local PF_LOCAL socket. */
if (config[0] == '/')
{
struct sockaddr_un *su = addr;
su->sun_family = PF_LOCAL;
clib_memcpy (&su->sun_path, config,
clib_min (sizeof (su->sun_path), 1 + strlen (config)));
*addr_len = sizeof (su[0]);
}
/* Hostname or hostname:port or port. */
else
{
char *host_name;
int port = -1;
struct sockaddr_in *sa = addr;
host_name = 0;
port = -1;
if (config[0] != 0)
{
unformat_input_t i;
unformat_init_string (&i, config, strlen (config));
if (unformat (&i, "%s:%d", &host_name, &port)
|| unformat (&i, "%s:0x%x", &host_name, &port))
;
else if (unformat (&i, "%s", &host_name))
;
else
error = clib_error_return (0, "unknown input `%U'",
format_unformat_error, &i);
unformat_free (&i);
if (error)
goto done;
}
sa->sin_family = PF_INET;
*addr_len = sizeof (sa[0]);
if (port != -1)
sa->sin_port = htons (port);
else
sa->sin_port = 0;
if (host_name)
{
struct in_addr host_addr;
/* Recognize localhost to avoid host lookup in most common cast. */
if (!strcmp (host_name, "localhost"))
sa->sin_addr.s_addr = htonl (INADDR_LOOPBACK);
else if (inet_aton (host_name, &host_addr))
sa->sin_addr = host_addr;
else if (host_name && strlen (host_name) > 0)
{
struct hostent *host = gethostbyname (host_name);
if (!host)
error = clib_error_return (0, "unknown host `%s'", config);
else
clib_memcpy (&sa->sin_addr.s_addr, host->h_addr_list[0],
host->h_length);
}
else
sa->sin_addr.s_addr = htonl (ip4_default_address);
vec_free (host_name);
if (error)
goto done;
}
}
done:
return error;
}
static clib_error_t *
default_socket_write (clib_socket_t * s)
{
clib_error_t *err = 0;
word written = 0;
word fd = 0;
word tx_len;
fd = s->fd;
/* Map standard input to standard output.
Typically, fd is a socket for which read/write both work. */
if (fd == 0)
fd = 1;
tx_len = vec_len (s->tx_buffer);
written = write (fd, s->tx_buffer, tx_len);
/* Ignore certain errors. */
if (written < 0 && !unix_error_is_fatal (errno))
written = 0;
/* A "real" error occurred. */
if (written < 0)
{
err = clib_error_return_unix (0, "write %wd bytes (fd %d, '%s')",
tx_len, s->fd, s->config);
vec_free (s->tx_buffer);
goto done;
}
/* Reclaim the transmitted part of the tx buffer on successful writes. */
else if (written > 0)
{
if (written == tx_len)
_vec_len (s->tx_buffer) = 0;
else
vec_delete (s->tx_buffer, written, 0);
}
/* If a non-fatal error occurred AND
the buffer is full, then we must free it. */
else if (written == 0 && tx_len > 64 * 1024)
{
vec_free (s->tx_buffer);
}
done:
return err;
}
static clib_error_t *
default_socket_read (clib_socket_t * sock, int n_bytes)
{
word fd, n_read;
u8 *buf;
/* RX side of socket is down once end of file is reached. */
if (sock->flags & CLIB_SOCKET_F_RX_END_OF_FILE)
return 0;
fd = sock->fd;
n_bytes = clib_max (n_bytes, 4096);
vec_add2 (sock->rx_buffer, buf, n_bytes);
if ((n_read = read (fd, buf, n_bytes)) < 0)
{
n_read = 0;
/* Ignore certain errors. */
if (!unix_error_is_fatal (errno))
goto non_fatal;
return clib_error_return_unix (0, "read %d bytes (fd %d, '%s')",
n_bytes, sock->fd, sock->config);
}
/* Other side closed the socket. */
if (n_read == 0)
sock->flags |= CLIB_SOCKET_F_RX_END_OF_FILE;
non_fatal:
_vec_len (sock->rx_buffer) += n_read - n_bytes;
return 0;
}
static clib_error_t *
default_socket_close (clib_socket_t * s)
{
if (close (s->fd) < 0)
return clib_error_return_unix (0, "close (fd %d, %s)", s->fd, s->config);
return 0;
}
static clib_error_t *
default_socket_sendmsg (clib_socket_t * s, void *msg, int msglen,
int fds[], int num_fds)
{
struct msghdr mh = { 0 };
struct iovec iov[1];
char ctl[CMSG_SPACE (sizeof (int)) * num_fds];
int rv;
iov[0].iov_base = msg;
iov[0].iov_len = msglen;
mh.msg_iov = iov;
mh.msg_iovlen = 1;
if (num_fds > 0)
{
struct cmsghdr *cmsg;
memset (&ctl, 0, sizeof (ctl));
mh.msg_control = ctl;
mh.msg_controllen = sizeof (ctl);
cmsg = CMSG_FIRSTHDR (&mh);
cmsg->cmsg_len = CMSG_LEN (sizeof (int) * num_fds);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
memcpy (CMSG_DATA (cmsg), fds, sizeof (int) * num_fds);
}
rv = sendmsg (s->fd, &mh, 0);
if (rv < 0)
return clib_error_return_unix (0, "sendmsg");
return 0;
}
static clib_error_t *
default_socket_recvmsg (clib_socket_t * s, void *msg, int msglen,
int fds[], int num_fds)
{
char ctl[CMSG_SPACE (sizeof (int) * num_fds) +
CMSG_SPACE (sizeof (struct ucred))];
struct msghdr mh = { 0 };
struct iovec iov[1];
ssize_t size;
struct ucred *cr = 0;
struct cmsghdr *cmsg;
iov[0].iov_base = msg;
iov[0].iov_len = msglen;
mh.msg_iov = iov;
mh.msg_iovlen = 1;
mh.msg_control = ctl;
mh.msg_controllen = sizeof (ctl);
memset (ctl, 0, sizeof (ctl));
/* receive the incoming message */
size = recvmsg (s->fd, &mh, 0);
if (size != msglen)
{
return (size == 0) ? clib_error_return (0, "disconnected") :
clib_error_return_unix (0, "recvmsg: malformed message (fd %d, '%s')",
s->fd, s->config);
}
cmsg = CMSG_FIRSTHDR (&mh);
while (cmsg)
{
if (cmsg->cmsg_level == SOL_SOCKET)
{
if (cmsg->cmsg_type == SCM_CREDENTIALS)
{
cr = (struct ucred *) CMSG_DATA (cmsg);
s->uid = cr->uid;
s->gid = cr->gid;
s->pid = cr->pid;
}
else if (cmsg->cmsg_type == SCM_RIGHTS)
{
clib_memcpy (fds, CMSG_DATA (cmsg), num_fds * sizeof (int));
}
}
cmsg = CMSG_NXTHDR (&mh, cmsg);
}
return 0;
}
static void
socket_init_funcs (clib_socket_t * s)
{
if (!s->write_func)
s->write_func = default_socket_write;
if (!s->read_func)
s->read_func = default_socket_read;
if (!s->close_func)
s->close_func = default_socket_close;
if (!s->sendmsg_func)
s->sendmsg_func = default_socket_sendmsg;
if (!s->recvmsg_func)
s->recvmsg_func = default_socket_recvmsg;
}
clib_error_t *
clib_socket_init (clib_socket_t * s)
{
union
{
struct sockaddr sa;
struct sockaddr_un su;
} addr;
socklen_t addr_len = 0;
int socket_type;
clib_error_t *error = 0;
word port;
error = socket_config (s->config, &addr.sa, &addr_len,
(s->flags & CLIB_SOCKET_F_IS_SERVER
? INADDR_LOOPBACK : INADDR_ANY));
if (error)
goto done;
socket_init_funcs (s);
socket_type = s->flags & CLIB_SOCKET_F_SEQPACKET ?
SOCK_SEQPACKET : SOCK_STREAM;
s->fd = socket (addr.sa.sa_family, socket_type, 0);
if (s->fd < 0)
{
error = clib_error_return_unix (0, "socket (fd %d, '%s')",
s->fd, s->config);
goto done;
}
port = 0;
if (addr.sa.sa_family == PF_INET)
port = ((struct sockaddr_in *) &addr)->sin_port;
if (s->flags & CLIB_SOCKET_F_IS_SERVER)
{
uword need_bind = 1;
if (addr.sa.sa_family == PF_INET)
{
if (port == 0)
{
port = find_free_port (s->fd);
if (port < 0)
{
error = clib_error_return (0, "no free port (fd %d, '%s')",
s->fd, s->config);
goto done;
}
need_bind = 0;
}
}
if (addr.sa.sa_family == PF_LOCAL)
unlink (((struct sockaddr_un *) &addr)->sun_path);
/* Make address available for multiple users. */
{
int v = 1;
if (setsockopt (s->fd, SOL_SOCKET, SO_REUSEADDR, &v, sizeof (v)) < 0)
clib_unix_warning ("setsockopt SO_REUSEADDR fails");
}
if (addr.sa.sa_family == PF_LOCAL && s->flags & CLIB_SOCKET_F_PASSCRED)
{
int x = 1;
if (setsockopt (s->fd, SOL_SOCKET, SO_PASSCRED, &x, sizeof (x)) < 0)
{
error = clib_error_return_unix (0, "setsockopt (SO_PASSCRED, "
"fd %d, '%s')", s->fd,
s->config);
goto done;
}
}
if (need_bind && bind (s->fd, &addr.sa, addr_len) < 0)
{
error = clib_error_return_unix (0, "bind (fd %d, '%s')",
s->fd, s->config);
goto done;
}
if (listen (s->fd, 5) < 0)
{
error = clib_error_return_unix (0, "listen (fd %d, '%s')",
s->fd, s->config);
goto done;
}
if (addr.sa.sa_family == PF_LOCAL
&& s->flags & CLIB_SOCKET_F_ALLOW_GROUP_WRITE)
{
struct stat st = { 0 };
if (stat (((struct sockaddr_un *) &addr)->sun_path, &st) < 0)
{
error = clib_error_return_unix (0, "stat (fd %d, '%s')",
s->fd, s->config);
goto done;
}
st.st_mode |= S_IWGRP;
if (chmod (((struct sockaddr_un *) &addr)->sun_path, st.st_mode) <
0)
{
error =
clib_error_return_unix (0, "chmod (fd %d, '%s', mode %o)",
s->fd, s->config, st.st_mode);
goto done;
}
}
}
else
{
if ((s->flags & CLIB_SOCKET_F_NON_BLOCKING_CONNECT)
&& fcntl (s->fd, F_SETFL, O_NONBLOCK) < 0)
{
error = clib_error_return_unix (0, "fcntl NONBLOCK (fd %d, '%s')",
s->fd, s->config);
goto done;
}
if (connect (s->fd, &addr.sa, addr_len) < 0
&& !((s->flags & CLIB_SOCKET_F_NON_BLOCKING_CONNECT) &&
errno == EINPROGRESS))
{
error = clib_error_return_unix (0, "connect (fd %d, '%s')",
s->fd, s->config);
goto done;
}
}
return error;
done:
if (s->fd > 0)
close (s->fd);
return error;
}
clib_error_t *
clib_socket_accept (clib_socket_t * server, clib_socket_t * client)
{
clib_error_t *err = 0;
socklen_t len = 0;
memset (client, 0, sizeof (client[0]));
/* Accept the new socket connection. */
client->fd = accept (server->fd, 0, 0);
if (client->fd < 0)
return clib_error_return_unix (0, "accept (fd %d, '%s')",
server->fd, server->config);
/* Set the new socket to be non-blocking. */
if (fcntl (client->fd, F_SETFL, O_NONBLOCK) < 0)
{
err = clib_error_return_unix (0, "fcntl O_NONBLOCK (fd %d)",
client->fd);
goto close_client;
}
/* Get peer info. */
len = sizeof (client->peer);
if (getpeername (client->fd, (struct sockaddr *) &client->peer, &len) < 0)
{
err = clib_error_return_unix (0, "getpeername (fd %d)", client->fd);
goto close_client;
}
client->flags = CLIB_SOCKET_F_IS_CLIENT;
socket_init_funcs (client);
return 0;
close_client:
close (client->fd);
return err;
}
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/
|