8000 minix/minix/net/uds/uds.c at master · iankuan/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
1417 lines (1163 loc) · 36.7 KB

uds.c

File metadata and controls

1417 lines (1163 loc) · 36.7 KB
/*
* If the intended target is linked to another socket, we
* refuse linking to it. Sending or receiving would never work
* anyway. Do allow a socket to link to itself after being
* linked to another socket. The error code is the same as in
* the sending code, borrowed from Linux.
*/
if (uds != link && uds_has_link(link) && link->uds_link != uds)
return EPERM;
/*
* Reset this socket's previous link to another socket, if any.
*/
if (uds_has_link(uds))
uds_del_queue(uds->uds_link, uds);
/*
* Reset any links to this socket, except for the one by
* the intended target. Sending or receiving would no longer
* work anyway. If the socket was linked to itself, clear its
* self-link without generating an ECONNRESET. If the socket
* is relinking to itself, reestablish the link after first
* clearing it.
*/
uds_clear_queue(uds, (uds != link) ? link : NULL);
uds_add_queue(link, uds);
return OK;
}
/*
* For connection-oriented sockets there is more to do. First, make
* sure that the peer is a listening socket, that it has not been shut
* down, and that its backlog is not already at the configured maximum.
*/
if (!uds_is_listening(link))
return ECONNREFUSED;
if (uds_is_shutdown(link, SFL_SHUT_RD | SFL_SHUT_WR))
return ECONNREFUSED;
if (link->uds_queued >= link->uds_backlog)
return ECONNREFUSED;
/*
* The behavior of connect(2) now depends on whether LOCAL_CONNWAIT is
* set on either the connecting or the listening socket. If it is not,
* the socket will be connected to a new as-yet invisible socket, which
* will be the one returned from accept(2) later. If it was, the
* socket will be put in the connecting state.
*/
if (!((uds->uds_flags | link->uds_flags) & UDSF_CONNWAIT)) {
if ((r = uds_attach(link, uds)) != OK)
return r;
assert(uds_is_connected(uds));
} else {
/*
* Disconnected sockets now stop being connected. Any pending
* data can still be received, though.
*/
uds->uds_flags &= ~UDSF_CONNECTED;
r = SUSPEND;
}
/* Obtain credentials for the socket. */
uds_get_cred(uds, user_endpt);
/* Add the socket at the end of the listening socket's queue. */
uds_add_queue(link, uds);
assert(r != SUSPEND || uds_is_connecting(uds));
/*
* Let an accept call handle the rest, which will in turn resume this
* connect call. The sockevent library ensures that this works even if
* the call is non-blocking.
*/
sockevent_raise(&link->uds_sock, SEV_ACCEPT);
return r;
}
/*
* Put a socket in listening mode.
*/
static int
uds_listen(struct sock * sock, int backlog)
{
struct udssock *uds = (struct udssock *)sock;
/* The maximum backlog value must not exceed its field size. */
assert(SOMAXCONN <= USHRT_MAX);
dprintf(("UDS: listen(%d)\n", uds_get_id(uds)));
/* Only connection-oriented types may be put in listening mode. */
if (uds_get_type(uds) == SOCK_DGRAM)
return EOPNOTSUPP;
/* A connecting or connected socket may not listen. */
if (uds_is_connecting(uds) || uds_is_connected(uds))
return EINVAL;
/* POSIX says that this is now the appropriate error code here. */
if (!uds_is_bound(uds))
return EDESTADDRREQ;
/*
* The socket is now entering the listening state. If it was
* previously disconnected, clear the connection flag.
*/
uds->uds_flags &= ~UDSF_CONNECTED;
/*
* We do not remove sockets from the backlog if it is now being dropped
* below the current number of queued sockets. We only refuse newly
* connecting sockets beyond the backlog size.
*/
uds->uds_backlog = backlog;
return OK;
}
/*
* Test whether an accept request would block. Return OK if a socket could be
* accepted, an appropriate error code if an accept call would fail instantly,
* or SUSPEND if the accept request would block waiting for a connection.
*/
static int
uds_test_accept(struct sock * sock)
{
struct udssock *uds = (struct udssock *)sock;
/*
* Ensure that the socket is in listening mode. If not, we must return
* the error code that is appropriate for this socket type.
*/
if (uds_get_type(uds) == SOCK_DGRAM)
return EOPNOTSUPP;
if (!uds_is_listening(uds))
return EINVAL;
/*
* If the socket has been shut down, new connections are no longer
* accepted and accept calls no longer block. This is not a POSIX
* requirement, but rather an application convenience feature.
*/
if (uds->uds_queued == 0) {
if (uds_is_shutdown(uds, SFL_SHUT_RD | SFL_SHUT_WR))
return ECONNABORTED;
return SUSPEND;
}
return OK;
}
/*
* Accept a connection on a listening socket, creating a new socket. On
* success, return the new socket identifier, with the new socket stored in
* 'newsockp'. Otherwise, return an error code.
*/
static sockid_t
uds_accept(struct sock * sock, struct sockaddr * addr, socklen_t * addr_len,
endpoint_t user_endpt __unused, struct sock ** newsockp)
{
struct udssock *uds = (struct udssock *)sock;
struct udssock *link, *conn;
sockid_t r;
dprintf(("UDS: accept(%d)\n", uds_get_id(uds)));
if ((r = uds_test_accept(sock)) != OK)
return r;
/*
* Take the first connecting socket off the listening queue.
*/
assert(!TAILQ_EMPTY(&uds->uds_queue));
link = TAILQ_FIRST(&uds->uds_queue);
/*
* Depending on the LOCAL_CONNWAIT setting at the time of connect(2),
* the socket may be connecting or connected. In the latter case, its
* attached socket is the socket we will return now. Otherwise we have
* to attach a socket first.
*/
assert(uds_is_connecting(link) || uds_is_connected(link));
if (uds_is_connecting(link)) {
/*
* Attach a new socket. If this fails, return the error but
* leave the connecting socket on the listening queue.
*/
if ((r = uds_attach(uds, link)) != OK)
return r;
assert(uds_is_connected(link));
/*
* Wake up blocked (connect, send, select) calls on the peer
* socket.
*/
sockevent_raise(&link->uds_sock, SEV_CONNECT);
}
uds_del_queue(uds, link);
/* Return the peer socket's address to the caller. */
uds_make_addr(link->uds_path, link->uds_pathlen, addr, addr_len);
conn = link->uds_conn;
dprintf(("UDS: accept returns %d\n", uds_get_id(conn)));
/*
* We already cloned the sock object, so return its ID but not a
* pointer to it. That tells libsockevent not to reinitialize it.
*/
*newsockp = NULL;
return uds_get_id(conn);
}
/*
* Set socket options.
*/
static int
uds_setsockopt(struct sock * sock, int level, int name,
const struct sockdriver_data * data, socklen_t len)
{
struct udssock *uds = (struct udssock *)sock;
int r, val;
dprintf(("UDS: setsockopt(%d,%d,%d)\n", uds_get_id(uds), level, name));
switch (level) {
case SOL_SOCKET:
switch (name) {
case SO_SNDBUF:
case SO_RCVBUF:
/*
* The send buffer size may not be changed because the
* buffer is the same as the other side's receive
* buffer, and what the other side is may vary from
* send call to send call. Changing the receive buffer
* size would disallow us from even accurately guessing
* the send buffer size in getsockopt calls. Therefore
* both are hardcoded and cannot actually be changed.
* In order to support applications that want at least
* a certain minimum, we do accept requests to shrink
0