42 #include <sys/types.h>
43 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
61 #define IPPORT_USERRESERVED 5000
85 a.sin_family = PF_INET;
86 a.sin_addr.s_addr = INADDR_ANY;
87 a.sin_port = htons (
port);
89 if (bind (sock, (
struct sockaddr *) &
a,
sizeof (
a)) >= 0)
100 void *
addr, socklen_t * addr_len,
u32 ip4_default_address)
108 if (config[0] ==
'/')
110 struct sockaddr_un *su =
addr;
111 su->sun_family = PF_LOCAL;
113 clib_min (
sizeof (su->sun_path), 1 + strlen (config)));
114 *addr_len =
sizeof (su[0]);
118 else if (config[0] ==
'@')
120 struct sockaddr_un *su =
addr;
121 su->sun_family = PF_LOCAL;
123 clib_min (
sizeof (su->sun_path), 1 + strlen (config)));
125 *addr_len =
sizeof (su->sun_family) + strlen (config);
126 su->sun_path[0] =
'\0';
134 struct sockaddr_in *sa =
addr;
146 else if (
unformat (&
i,
"%s", &host_name))
157 sa->sin_family = PF_INET;
158 *addr_len =
sizeof (sa[0]);
160 sa->sin_port = htons (
port);
166 struct in_addr host_addr;
169 if (!strcmp (host_name,
"localhost"))
170 sa->sin_addr.s_addr = htonl (INADDR_LOOPBACK);
172 else if (inet_aton (host_name, &host_addr))
173 sa->sin_addr = host_addr;
175 else if (host_name && strlen (host_name) > 0)
177 struct hostent *
host = gethostbyname (host_name);
186 sa->sin_addr.s_addr = htonl (ip4_default_address);
213 tx_len =
vec_len (s->tx_buffer);
214 written = write (fd, s->tx_buffer, tx_len);
224 tx_len, s->fd, s->config);
230 else if (written > 0)
232 if (written == tx_len)
233 _vec_len (s->tx_buffer) = 0;
240 else if (written == 0 && tx_len > 64 * 1024)
273 n_bytes, sock->fd, sock->config);
281 _vec_len (sock->rx_buffer) += n_read -
n_bytes;
289 if (close (s->fd) < 0)
296 int fds[],
int num_fds)
298 struct msghdr mh = { 0 };
300 char ctl[CMSG_SPACE (
sizeof (
int) * num_fds)];
303 iov[0].iov_base = msg;
304 iov[0].iov_len = msglen;
310 struct cmsghdr *cmsg;
312 mh.msg_control = ctl;
313 mh.msg_controllen =
sizeof (ctl);
314 cmsg = CMSG_FIRSTHDR (&mh);
315 cmsg->cmsg_len = CMSG_LEN (
sizeof (
int) * num_fds);
316 cmsg->cmsg_level = SOL_SOCKET;
317 cmsg->cmsg_type = SCM_RIGHTS;
318 memcpy (CMSG_DATA (cmsg), fds,
sizeof (
int) * num_fds);
320 rv = sendmsg (s->fd, &mh, 0);
329 int fds[],
int num_fds)
332 char ctl[CMSG_SPACE (
sizeof (
int) * num_fds) +
333 CMSG_SPACE (
sizeof (
struct ucred))];
334 struct ucred *cr = 0;
336 char ctl[CMSG_SPACE (
sizeof (
int) * num_fds)];
338 struct msghdr mh = { 0 };
341 struct cmsghdr *cmsg;
343 iov[0].iov_base = msg;
344 iov[0].iov_len = msglen;
347 mh.msg_control = ctl;
348 mh.msg_controllen =
sizeof (ctl);
353 size = recvmsg (s->fd, &mh, 0);
361 cmsg = CMSG_FIRSTHDR (&mh);
364 if (cmsg->cmsg_level == SOL_SOCKET)
367 if (cmsg->cmsg_type == SCM_CREDENTIALS)
369 cr = (
struct ucred *) CMSG_DATA (cmsg);
376 if (cmsg->cmsg_type == SCM_RIGHTS)
379 num_fds *
sizeof (
int));
382 cmsg = CMSG_NXTHDR (&mh, cmsg);
396 if (!s->sendmsg_func)
398 if (!s->recvmsg_func)
408 struct sockaddr_un su;
410 socklen_t addr_len = 0;
417 ? INADDR_LOOPBACK : INADDR_ANY));
424 SOCK_SEQPACKET : SOCK_STREAM;
426 s->fd = socket (
addr.sa.sa_family, socket_type, 0);
435 if (
addr.sa.sa_family == PF_INET)
436 port = ((
struct sockaddr_in *) &
addr)->sin_port;
442 if (
addr.sa.sa_family == PF_INET)
456 if (
addr.sa.sa_family == PF_LOCAL &&
457 ((
struct sockaddr_un *) &
addr)->sun_path[0] != 0)
458 unlink (((
struct sockaddr_un *) &
addr)->sun_path);
463 if (setsockopt (s->fd, SOL_SOCKET, SO_REUSEADDR, &v, sizeof (v)) < 0)
471 if (setsockopt (s->fd, SOL_SOCKET, SO_PASSCRED, &x, sizeof (x)) < 0)
474 "fd %d, '%s')", s->fd,
481 if (need_bind && bind (s->fd, &
addr.sa, addr_len) < 0)
488 if (listen (s->fd, 5) < 0)
494 if (
addr.sa.sa_family == PF_LOCAL &&
496 ((
struct sockaddr_un *) &
addr)->sun_path[0] != 0)
498 struct stat st = { 0 };
499 if (stat (((
struct sockaddr_un *) &
addr)->sun_path, &st) < 0)
505 st.st_mode |= S_IWGRP;
506 if (chmod (((
struct sockaddr_un *) &
addr)->sun_path, st.st_mode) <
511 s->fd, s->config, st.st_mode);
519 && fcntl (s->fd, F_SETFL, O_NONBLOCK) < 0)
526 while ((
rv = connect (s->fd, &
addr.sa, addr_len)) < 0
530 errno == EINPROGRESS))
540 fcntl (s->fd, F_SETFL, O_NONBLOCK) < 0)
559 if (
namespace == NULL ||
namespace[0] == 0)
563 int old_netns_fd, nfd = -1;
584 close (old_netns_fd);
601 client->fd = accept (server->fd, 0, 0);
604 server->fd, server->config);
607 if (fcntl (client->fd, F_SETFL, O_NONBLOCK) < 0)
615 len =
sizeof (client->peer);
616 if (getpeername (client->fd, (
struct sockaddr *) &client->peer, &
len) < 0)