2015-06-29 17:02:08 +02:00
|
|
|
/*
|
|
|
|
* \brief libc socket operations
|
|
|
|
* \author Christian Helmuth
|
|
|
|
* \author Christian Prochaska
|
|
|
|
* \author Norman Feske
|
|
|
|
* \date 2015-06-23
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-12 10:59:37 +01:00
|
|
|
* Copyright (C) 2015-2017 Genode Labs GmbH
|
2015-06-29 17:02:08 +02:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 13:23:52 +01:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2015-06-29 17:02:08 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Genode includes */
|
|
|
|
#include <base/env.h>
|
|
|
|
#include <os/path.h>
|
|
|
|
#include <util/token.h>
|
|
|
|
|
|
|
|
/* libc includes */
|
2019-04-26 12:47:33 +02:00
|
|
|
extern "C" {
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <libc_private.h>
|
2015-06-29 17:02:08 +02:00
|
|
|
#include <errno.h>
|
2019-04-26 12:47:33 +02:00
|
|
|
}
|
2015-06-29 17:02:08 +02:00
|
|
|
|
|
|
|
/* libc-internal includes */
|
libc: split task.cc into multiple files
This patch is the first step of re-organizing the internal structure of
the libc. The original version involved many direct calls of global
functions (often with side effects) across compilation units, which
made the control flow (e.g., the initialization sequence) hard to
follow.
The new version replaces those ad-hoc interactions with dedicated
interfaces (like suspend.h, resume.h, select.h, current_time.h). The
underlying facilities are provided by the central Libc::Kernel and
selectively propagated to the various compilation units. The latter is
done by a sequence of 'init_*' calls, which eventually will be replaced
by constructor calls.
The addition of new headers increases the chance for name clashes with
existing (public) headers. To disambiguate libc-internal header files
from public headers, this patch moves the former into a new 'internal/'
subdirectory. This makes the include directives easier to follow and the
libc's source-tree structure more tidy.
There are still a few legacies left, which cannot easily be removed
right now (e.g., because noux relies on them). However, the patch moves
those bad apples to legacy.h and legacy.cc, which highlights the
deprecation of those functions.
Issue #3497
2019-09-18 20:19:10 +02:00
|
|
|
#include <internal/file.h>
|
|
|
|
#include <internal/socket_fs_plugin.h>
|
2015-06-29 17:02:08 +02:00
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
|
2015-06-29 17:02:08 +02:00
|
|
|
using namespace Libc;
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
|
|
|
|
#define __SYS_(ret_type, name, args, body) \
|
|
|
|
extern "C" {\
|
|
|
|
ret_type __sys_##name args body \
|
|
|
|
ret_type _##name args __attribute__((alias("__sys_" #name))); \
|
|
|
|
ret_type name args __attribute__((alias("__sys_" #name))); \
|
|
|
|
} \
|
|
|
|
|
2017-02-12 10:59:37 +01:00
|
|
|
namespace Libc { extern char const *config_socket(); }
|
2015-06-29 17:02:08 +02:00
|
|
|
|
2017-02-12 10:59:37 +01:00
|
|
|
|
|
|
|
/***********************
|
|
|
|
** Address functions **
|
|
|
|
***********************/
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
extern "C" int getpeername(int libc_fd, sockaddr *addr, socklen_t *addrlen)
|
2017-02-12 10:59:37 +01:00
|
|
|
{
|
|
|
|
if (*Libc::config_socket())
|
|
|
|
return socket_fs_getpeername(libc_fd, addr, addrlen);
|
|
|
|
|
|
|
|
FD_FUNC_WRAPPER(getpeername, libc_fd, addr, addrlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
extern "C" __attribute__((alias("getpeername")))
|
|
|
|
int _getpeername(int libc_fd, sockaddr *addr, socklen_t *addrlen);
|
2017-02-12 10:59:37 +01:00
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
extern "C" int getsockname(int libc_fd, sockaddr *addr, socklen_t *addrlen)
|
2015-06-29 17:02:08 +02:00
|
|
|
{
|
2017-02-12 10:59:37 +01:00
|
|
|
if (*Libc::config_socket())
|
|
|
|
return socket_fs_getsockname(libc_fd, addr, addrlen);
|
|
|
|
|
|
|
|
FD_FUNC_WRAPPER(getsockname, libc_fd, addr, addrlen);
|
2015-06-29 17:02:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
extern "C" __attribute__((alias("getsockname")))
|
|
|
|
int _getsockname(int libc_fd, sockaddr *addr, socklen_t *addrlen);
|
2017-02-12 10:59:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**************************
|
|
|
|
** Socket transport API **
|
|
|
|
**************************/
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
__SYS_(int, accept, (int libc_fd, sockaddr *addr, socklen_t *addrlen),
|
2017-02-12 10:59:37 +01:00
|
|
|
{
|
|
|
|
if (*Libc::config_socket())
|
|
|
|
return socket_fs_accept(libc_fd, addr, addrlen);
|
|
|
|
|
2015-06-29 17:02:08 +02:00
|
|
|
File_descriptor *ret_fd;
|
|
|
|
FD_FUNC_WRAPPER_GENERIC(ret_fd =, 0, accept, libc_fd, addr, addrlen);
|
|
|
|
return ret_fd ? ret_fd->libc_fd : INVALID_FD;
|
2019-04-26 12:47:33 +02:00
|
|
|
})
|
2015-06-29 17:02:08 +02:00
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
__SYS_(int, accept4, (int libc_fd, struct sockaddr *addr, socklen_t *addrlen, int /*flags*/),
|
2015-06-29 17:02:08 +02:00
|
|
|
{
|
2019-04-26 12:47:33 +02:00
|
|
|
return accept(libc_fd, addr, addrlen);
|
|
|
|
})
|
2015-06-29 17:02:08 +02:00
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
extern "C" int bind(int libc_fd, sockaddr const *addr, socklen_t addrlen)
|
2017-02-12 10:59:37 +01:00
|
|
|
{
|
|
|
|
if (*Libc::config_socket())
|
|
|
|
return socket_fs_bind(libc_fd, addr, addrlen);
|
2015-06-29 17:02:08 +02:00
|
|
|
|
2017-02-12 10:59:37 +01:00
|
|
|
FD_FUNC_WRAPPER(bind, libc_fd, addr, addrlen);
|
|
|
|
}
|
2015-06-29 17:02:08 +02:00
|
|
|
|
2017-02-12 10:59:37 +01:00
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
extern "C" __attribute__((alias("bind")))
|
|
|
|
int _bind(int libc_fd, sockaddr const *addr, socklen_t addrlen);
|
2015-06-29 17:02:08 +02:00
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
__SYS_(int, connect, (int libc_fd, sockaddr const *addr, socklen_t addrlen),
|
2015-06-29 17:02:08 +02:00
|
|
|
{
|
2017-02-12 10:59:37 +01:00
|
|
|
if (*Libc::config_socket())
|
|
|
|
return socket_fs_connect(libc_fd, addr, addrlen);
|
|
|
|
|
|
|
|
FD_FUNC_WRAPPER(connect, libc_fd, addr, addrlen);
|
2019-04-26 12:47:33 +02:00
|
|
|
})
|
2015-06-29 17:02:08 +02:00
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
extern "C" int listen(int libc_fd, int backlog)
|
2015-06-29 17:02:08 +02:00
|
|
|
{
|
2017-02-12 10:59:37 +01:00
|
|
|
if (*Libc::config_socket())
|
|
|
|
return socket_fs_listen(libc_fd, backlog);
|
|
|
|
|
|
|
|
FD_FUNC_WRAPPER(listen, libc_fd, backlog);
|
2015-06-29 17:02:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
__SYS_(ssize_t, recvfrom, (int libc_fd, void *buf, ::size_t len, int flags,
|
|
|
|
sockaddr *src_addr, socklen_t *src_addrlen),
|
2015-06-29 17:02:08 +02:00
|
|
|
{
|
2017-02-12 10:59:37 +01:00
|
|
|
if (*Libc::config_socket())
|
|
|
|
return socket_fs_recvfrom(libc_fd, buf, len, flags, src_addr, src_addrlen);
|
|
|
|
|
|
|
|
FD_FUNC_WRAPPER(recvfrom, libc_fd, buf, len, flags, src_addr, src_addrlen);
|
2019-04-26 12:47:33 +02:00
|
|
|
})
|
2015-06-29 17:02:08 +02:00
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
__SYS_(ssize_t, recv, (int libc_fd, void *buf, ::size_t len, int flags),
|
2017-02-12 10:59:37 +01:00
|
|
|
{
|
|
|
|
if (*Libc::config_socket())
|
|
|
|
return socket_fs_recv(libc_fd, buf, len, flags);
|
2015-06-29 17:02:08 +02:00
|
|
|
|
2017-02-12 10:59:37 +01:00
|
|
|
FD_FUNC_WRAPPER(recv, libc_fd, buf, len, flags);
|
2019-04-26 12:47:33 +02:00
|
|
|
})
|
2015-06-29 17:02:08 +02:00
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
__SYS_(ssize_t, recvmsg, (int libc_fd, msghdr *msg, int flags),
|
2017-02-12 10:59:37 +01:00
|
|
|
{
|
|
|
|
if (*Libc::config_socket())
|
|
|
|
return socket_fs_recvmsg(libc_fd, msg, flags);
|
2015-06-29 17:02:08 +02:00
|
|
|
|
2017-02-12 10:59:37 +01:00
|
|
|
FD_FUNC_WRAPPER(recvmsg, libc_fd, msg, flags);
|
2019-04-26 12:47:33 +02:00
|
|
|
})
|
2015-06-29 17:02:08 +02:00
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
__SYS_(ssize_t, sendto, (int libc_fd, void const *buf, ::size_t len, int flags,
|
|
|
|
sockaddr const *dest_addr, socklen_t dest_addrlen),
|
2017-02-12 10:59:37 +01:00
|
|
|
{
|
|
|
|
if (*Libc::config_socket())
|
|
|
|
return socket_fs_sendto(libc_fd, buf, len, flags, dest_addr, dest_addrlen);
|
2015-06-29 17:02:08 +02:00
|
|
|
|
2017-02-12 10:59:37 +01:00
|
|
|
FD_FUNC_WRAPPER(sendto, libc_fd, buf, len, flags, dest_addr, dest_addrlen);
|
2019-04-26 12:47:33 +02:00
|
|
|
})
|
2015-06-29 17:02:08 +02:00
|
|
|
|
|
|
|
|
2017-02-12 10:59:37 +01:00
|
|
|
extern "C" ssize_t send(int libc_fd, void const *buf, ::size_t len, int flags)
|
2015-06-29 17:02:08 +02:00
|
|
|
{
|
2017-02-12 10:59:37 +01:00
|
|
|
if (*Libc::config_socket())
|
|
|
|
return socket_fs_send(libc_fd, buf, len, flags);
|
|
|
|
|
|
|
|
FD_FUNC_WRAPPER(send, libc_fd, buf, len, flags);
|
2015-06-29 17:02:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
extern "C" int getsockopt(int libc_fd, int level, int optname,
|
2015-06-29 17:02:08 +02:00
|
|
|
void *optval, socklen_t *optlen)
|
|
|
|
{
|
2017-02-12 10:59:37 +01:00
|
|
|
if (*Libc::config_socket())
|
|
|
|
return socket_fs_getsockopt(libc_fd, level, optname, optval, optlen);
|
|
|
|
|
|
|
|
FD_FUNC_WRAPPER(getsockopt, libc_fd, level, optname, optval, optlen);
|
2015-06-29 17:02:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
extern "C" __attribute__((alias("getsockopt")))
|
|
|
|
int _getsockopt(int libc_fd, int level, int optname,
|
|
|
|
void *optval, socklen_t *optlen);
|
2015-06-29 17:02:08 +02:00
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
extern "C" int setsockopt(int libc_fd, int level, int optname,
|
|
|
|
void const *optval, socklen_t optlen)
|
2017-02-12 10:59:37 +01:00
|
|
|
{
|
|
|
|
if (*Libc::config_socket())
|
|
|
|
return socket_fs_setsockopt(libc_fd, level, optname, optval, optlen);
|
|
|
|
|
|
|
|
FD_FUNC_WRAPPER(setsockopt, libc_fd, level, optname, optval, optlen);
|
|
|
|
}
|
2015-06-29 17:02:08 +02:00
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
extern "C" __attribute__((alias("setsockopt")))
|
|
|
|
int _setsockopt(int libc_fd, int level, int optname,
|
|
|
|
void const *optval, socklen_t optlen);
|
2015-06-29 17:02:08 +02:00
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
extern "C" int shutdown(int libc_fd, int how)
|
2017-02-12 10:59:37 +01:00
|
|
|
{
|
|
|
|
if (*Libc::config_socket())
|
|
|
|
return socket_fs_shutdown(libc_fd, how);
|
|
|
|
|
|
|
|
FD_FUNC_WRAPPER(shutdown, libc_fd, how);
|
|
|
|
}
|
2015-06-29 17:02:08 +02:00
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
__SYS_(int, socket, (int domain, int type, int protocol),
|
2017-02-12 10:59:37 +01:00
|
|
|
{
|
|
|
|
if (*Libc::config_socket())
|
|
|
|
return socket_fs_socket(domain, type, protocol);
|
|
|
|
|
2015-06-29 17:02:08 +02:00
|
|
|
Plugin *plugin;
|
|
|
|
File_descriptor *new_fdo;
|
|
|
|
|
|
|
|
plugin = plugin_registry()->get_plugin_for_socket(domain, type, protocol);
|
|
|
|
|
|
|
|
if (!plugin) {
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 19:07:09 +02:00
|
|
|
Genode::error("no plugin found for socket()");
|
2015-06-29 17:02:08 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_fdo = plugin->socket(domain, type, protocol);
|
|
|
|
if (!new_fdo) {
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 19:07:09 +02:00
|
|
|
Genode::error("plugin()->socket() failed");
|
2015-06-29 17:02:08 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new_fdo->libc_fd;
|
2019-04-26 12:47:33 +02:00
|
|
|
})
|