42 #include <sys/types.h>
43 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
60 #define IPPORT_USERRESERVED 5000
84 a.sin_family = PF_INET;
85 a.sin_addr.s_addr = INADDR_ANY;
86 a.sin_port = htons (
port);
88 if (bind (sock, (
struct sockaddr *) &
a,
sizeof (
a)) >= 0)
99 void *
addr, socklen_t * addr_len,
u32 ip4_default_address)
107 if (config[0] ==
'/')
109 struct sockaddr_un *su =
addr;
110 su->sun_family = PF_LOCAL;
112 clib_min (
sizeof (su->sun_path), 1 + strlen (config)));
113 *addr_len =
sizeof (su[0]);
121 struct sockaddr_in *sa =
addr;
133 else if (
unformat (&
i,
"%s", &host_name))
144 sa->sin_family = PF_INET;
145 *addr_len =
sizeof (sa[0]);
147 sa->sin_port = htons (
port);
153 struct in_addr host_addr;
156 if (!strcmp (host_name,
"localhost"))
157 sa->sin_addr.s_addr = htonl (INADDR_LOOPBACK);
159 else if (inet_aton (host_name, &host_addr))
160 sa->sin_addr = host_addr;
162 else if (host_name && strlen (host_name) > 0)
164 struct hostent *
host = gethostbyname (host_name);
173 sa->sin_addr.s_addr = htonl (ip4_default_address);
200 tx_len =
vec_len (s->tx_buffer);
201 written = write (fd, s->tx_buffer, tx_len);
211 tx_len, s->fd, s->config);
217 else if (written > 0)
219 if (written == tx_len)
220 _vec_len (s->tx_buffer) = 0;
227 else if (written == 0 && tx_len > 64 * 1024)
260 n_bytes, sock->fd, sock->config);
268 _vec_len (sock->rx_buffer) += n_read -
n_bytes;
276 if (close (s->fd) < 0)
283 int fds[],
int num_fds)
285 struct msghdr mh = { 0 };
287 char ctl[CMSG_SPACE (
sizeof (
int) * num_fds)];
290 iov[0].iov_base = msg;
291 iov[0].iov_len = msglen;
297 struct cmsghdr *cmsg;
299 mh.msg_control = ctl;
300 mh.msg_controllen =
sizeof (ctl);
301 cmsg = CMSG_FIRSTHDR (&mh);
302 cmsg->cmsg_len = CMSG_LEN (
sizeof (
int) * num_fds);
303 cmsg->cmsg_level = SOL_SOCKET;
304 cmsg->cmsg_type = SCM_RIGHTS;
305 memcpy (CMSG_DATA (cmsg), fds,
sizeof (
int) * num_fds);
307 rv = sendmsg (s->fd, &mh, 0);
316 int fds[],
int num_fds)
319 char ctl[CMSG_SPACE (
sizeof (
int) * num_fds) +
320 CMSG_SPACE (
sizeof (
struct ucred))];
321 struct ucred *cr = 0;
323 char ctl[CMSG_SPACE (
sizeof (
int) * num_fds)];
325 struct msghdr mh = { 0 };
328 struct cmsghdr *cmsg;
330 iov[0].iov_base = msg;
331 iov[0].iov_len = msglen;
334 mh.msg_control = ctl;
335 mh.msg_controllen =
sizeof (ctl);
340 size = recvmsg (s->fd, &mh, 0);
348 cmsg = CMSG_FIRSTHDR (&mh);
351 if (cmsg->cmsg_level == SOL_SOCKET)
354 if (cmsg->cmsg_type == SCM_CREDENTIALS)
356 cr = (
struct ucred *) CMSG_DATA (cmsg);
363 if (cmsg->cmsg_type == SCM_RIGHTS)
366 num_fds *
sizeof (
int));
369 cmsg = CMSG_NXTHDR (&mh, cmsg);
383 if (!s->sendmsg_func)
385 if (!s->recvmsg_func)
395 struct sockaddr_un su;
397 socklen_t addr_len = 0;
404 ? INADDR_LOOPBACK : INADDR_ANY));
411 SOCK_SEQPACKET : SOCK_STREAM;
413 s->fd = socket (
addr.sa.sa_family, socket_type, 0);
422 if (
addr.sa.sa_family == PF_INET)
423 port = ((
struct sockaddr_in *) &
addr)->sin_port;
429 if (
addr.sa.sa_family == PF_INET)
443 if (
addr.sa.sa_family == PF_LOCAL)
444 unlink (((
struct sockaddr_un *) &
addr)->sun_path);
449 if (setsockopt (s->fd, SOL_SOCKET, SO_REUSEADDR, &v, sizeof (v)) < 0)
457 if (setsockopt (s->fd, SOL_SOCKET, SO_PASSCRED, &x, sizeof (x)) < 0)
460 "fd %d, '%s')", s->fd,
467 if (need_bind && bind (s->fd, &
addr.sa, addr_len) < 0)
474 if (listen (s->fd, 5) < 0)
480 if (
addr.sa.sa_family == PF_LOCAL
483 struct stat st = { 0 };
484 if (stat (((
struct sockaddr_un *) &
addr)->sun_path, &st) < 0)
490 st.st_mode |= S_IWGRP;
491 if (chmod (((
struct sockaddr_un *) &
addr)->sun_path, st.st_mode) <
496 s->fd, s->config, st.st_mode);
504 && fcntl (s->fd, F_SETFL, O_NONBLOCK) < 0)
511 while ((
rv = connect (s->fd, &
addr.sa, addr_len)) < 0
515 errno == EINPROGRESS))
525 fcntl (s->fd, F_SETFL, O_NONBLOCK) < 0)
550 client->fd = accept (server->fd, 0, 0);
553 server->fd, server->config);
556 if (fcntl (client->fd, F_SETFL, O_NONBLOCK) < 0)
564 len =
sizeof (client->peer);
565 if (getpeername (client->fd, (
struct sockaddr *) &client->peer, &
len) < 0)