Compare commits

...

7 Commits

Author SHA1 Message Date
Bananymous 78928b7eb4 Ports: Add curl port! 2024-06-17 21:40:13 +03:00
Bananymous 5236e1ef0d LibC: Add dummy functions for rename() and strftime()
I don't really want to implement them right now, but they are required
to exist for some ports.
2024-06-17 21:02:37 +03:00
Bananymous be7ed8e74a Kernel/LibC: Implement {get,set}sockopt()
These are pretty much dummy functions in the kernel side. Only case that
is handled is SOL_SOCKET with SO_ERROR. This is hard coded to return no
error. Network stack is currently synchronous, so all errors are already
reported through synchronous network functions.
2024-06-17 20:56:48 +03:00
Bananymous 78bcb85679 LibC: Implement gethostbyname()
This is not a POSIX function, but curl seems to depend on it. It is
even deprecated on linux...
2024-06-17 20:55:50 +03:00
Bananymous b98bb9eb27 LibC: Add HOST_NAME_MAX definition 2024-06-17 20:55:17 +03:00
Bananymous cad55a4da5 Kernel/LibC: Implement getsockname for ipv4 sockets 2024-06-17 20:54:45 +03:00
Bananymous 511fc870a1 BAN: Mark RefPtr and WeakPtr operator bool() as explicit 2024-06-17 20:19:36 +03:00
25 changed files with 289 additions and 15 deletions

View File

@ -128,7 +128,7 @@ namespace BAN
bool operator!=(RefPtr other) const { return m_pointer != other.m_pointer; }
bool empty() const { return m_pointer == nullptr; }
operator bool() const { return m_pointer; }
explicit operator bool() const { return m_pointer; }
void clear()
{

View File

@ -91,7 +91,7 @@ namespace BAN
bool valid() const { return m_link && m_link->valid(); }
operator bool() const { return valid(); }
explicit operator bool() const { return valid(); }
private:
WeakPtr(const RefPtr<WeakLink<T>>& link)

View File

@ -105,6 +105,7 @@ namespace Kernel
BAN::ErrorOr<void> listen(int backlog);
BAN::ErrorOr<size_t> sendto(BAN::ConstByteSpan message, const sockaddr* address, socklen_t address_len);
BAN::ErrorOr<size_t> recvfrom(BAN::ByteSpan buffer, sockaddr* address, socklen_t* address_len);
BAN::ErrorOr<void> getsockname(sockaddr* address, socklen_t* address_len);
// General API
BAN::ErrorOr<size_t> read(off_t, BAN::ByteSpan buffer);
@ -139,7 +140,8 @@ namespace Kernel
virtual BAN::ErrorOr<void> listen_impl(int) { return BAN::Error::from_errno(ENOTSUP); }
virtual BAN::ErrorOr<void> bind_impl(const sockaddr*, socklen_t) { return BAN::Error::from_errno(ENOTSUP); }
virtual BAN::ErrorOr<size_t> sendto_impl(BAN::ConstByteSpan, const sockaddr*, socklen_t) { return BAN::Error::from_errno(ENOTSUP); }
virtual BAN::ErrorOr<size_t> recvfrom_impl(BAN::ByteSpan, sockaddr*, socklen_t*) { return BAN::Error::from_errno(ENOTSUP); }
virtual BAN::ErrorOr<size_t> recvfrom_impl(BAN::ByteSpan, sockaddr*, socklen_t*) { return BAN::Error::from_errno(ENOTSUP); }
virtual BAN::ErrorOr<void> getsockname_impl(sockaddr*, socklen_t*) { return BAN::Error::from_errno(ENOTSUP); }
// General API
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) { return BAN::Error::from_errno(ENOTSUP); }

View File

@ -47,6 +47,7 @@ namespace Kernel
virtual void unbind_socket(BAN::RefPtr<NetworkSocket>, uint16_t port) override;
virtual BAN::ErrorOr<void> bind_socket_to_unused(BAN::RefPtr<NetworkSocket>, const sockaddr* send_address, socklen_t send_address_len) override;
virtual BAN::ErrorOr<void> bind_socket_to_address(BAN::RefPtr<NetworkSocket>, const sockaddr* address, socklen_t address_len) override;
virtual BAN::ErrorOr<void> get_socket_address(BAN::RefPtr<NetworkSocket>, sockaddr* address, socklen_t* address_len) override;
virtual BAN::ErrorOr<size_t> sendto(NetworkSocket&, BAN::ConstByteSpan, const sockaddr*, socklen_t) override;

View File

@ -25,6 +25,7 @@ namespace Kernel
virtual void unbind_socket(BAN::RefPtr<NetworkSocket>, uint16_t port) = 0;
virtual BAN::ErrorOr<void> bind_socket_to_unused(BAN::RefPtr<NetworkSocket>, const sockaddr* send_address, socklen_t send_address_len) = 0;
virtual BAN::ErrorOr<void> bind_socket_to_address(BAN::RefPtr<NetworkSocket>, const sockaddr* address, socklen_t address_len) = 0;
virtual BAN::ErrorOr<void> get_socket_address(BAN::RefPtr<NetworkSocket>, sockaddr* address, socklen_t* address_len) = 0;
virtual BAN::ErrorOr<size_t> sendto(NetworkSocket&, BAN::ConstByteSpan, const sockaddr*, socklen_t) = 0;

View File

@ -42,6 +42,7 @@ namespace Kernel
NetworkSocket(NetworkLayer&, ino_t, const TmpInodeInfo&);
virtual BAN::ErrorOr<long> ioctl_impl(int request, void* arg) override;
virtual BAN::ErrorOr<void> getsockname_impl(sockaddr*, socklen_t*) override;
protected:
NetworkLayer& m_network_layer;

View File

@ -119,6 +119,10 @@ namespace Kernel
BAN::ErrorOr<long> sys_chown(const char*, uid_t, gid_t);
BAN::ErrorOr<long> sys_socket(int domain, int type, int protocol);
BAN::ErrorOr<long> sys_getsockname(int socket, sockaddr* address, socklen_t* address_len);
BAN::ErrorOr<long> sys_getsockopt(int socket, int level, int option_name, void* option_value, socklen_t* option_len);
BAN::ErrorOr<long> sys_setsockopt(int socket, int level, int option_name, const void* option_value, socklen_t option_len);
BAN::ErrorOr<long> sys_accept(int socket, sockaddr* address, socklen_t* address_len);
BAN::ErrorOr<long> sys_bind(int socket, const sockaddr* address, socklen_t address_len);
BAN::ErrorOr<long> sys_connect(int socket, const sockaddr* address, socklen_t address_len);

View File

@ -164,6 +164,14 @@ namespace Kernel
return recvfrom_impl(buffer, address, address_len);
};
BAN::ErrorOr<void> Inode::getsockname(sockaddr* address, socklen_t* address_len)
{
LockGuard _(m_mutex);
if (!mode().ifsock())
return BAN::Error::from_errno(ENOTSOCK);
return getsockname_impl(address, address_len);
}
BAN::ErrorOr<size_t> Inode::read(off_t offset, BAN::ByteSpan buffer)
{
LockGuard _(m_mutex);

View File

@ -139,6 +139,28 @@ namespace Kernel
return {};
}
BAN::ErrorOr<void> IPv4Layer::get_socket_address(BAN::RefPtr<NetworkSocket> socket, sockaddr* address, socklen_t* address_len)
{
if (*address_len < (socklen_t)sizeof(sockaddr_in))
return BAN::Error::from_errno(ENOBUFS);
sockaddr_in* in_addr = reinterpret_cast<sockaddr_in*>(address);
SpinLockGuard _(m_bound_socket_lock);
for (auto& [bound_port, bound_socket] : m_bound_sockets)
{
if (socket != bound_socket.lock())
continue;
// FIXME: sockets should have bound address
in_addr->sin_family = AF_INET;
in_addr->sin_port = bound_port;
in_addr->sin_addr.s_addr = INADDR_ANY;
return {};
}
return {};
}
BAN::ErrorOr<size_t> IPv4Layer::sendto(NetworkSocket& socket, BAN::ConstByteSpan buffer, const sockaddr* address, socklen_t address_len)
{
if (address->sa_family != AF_INET)

View File

@ -100,4 +100,10 @@ namespace Kernel
}
}
BAN::ErrorOr<void> NetworkSocket::getsockname_impl(sockaddr* address, socklen_t* address_len)
{
TRY(m_network_layer.get_socket_address(this, address, address_len));
return {};
}
}

View File

@ -95,8 +95,7 @@ namespace Kernel
BAN::ErrorOr<int> OpenFileDescriptorSet::socket(int domain, int type, int protocol)
{
if (protocol != 0)
return BAN::Error::from_errno(EPROTONOSUPPORT);
bool valid_protocol = true;
SocketDomain sock_domain;
switch (domain)
@ -109,6 +108,7 @@ namespace Kernel
break;
case AF_UNIX:
sock_domain = SocketDomain::UNIX;
valid_protocol = false;
break;
default:
return BAN::Error::from_errno(EPROTOTYPE);
@ -119,17 +119,25 @@ namespace Kernel
{
case SOCK_STREAM:
sock_type = SocketType::STREAM;
if (protocol != IPPROTO_TCP)
valid_protocol = false;
break;
case SOCK_DGRAM:
sock_type = SocketType::DGRAM;
if (protocol != IPPROTO_UDP)
valid_protocol = false;
break;
case SOCK_SEQPACKET:
sock_type = SocketType::SEQPACKET;
valid_protocol = false;
break;
default:
return BAN::Error::from_errno(EPROTOTYPE);
}
if (protocol && !valid_protocol)
return BAN::Error::from_errno(EPROTONOSUPPORT);
auto socket = TRY(NetworkManager::get().create_socket(sock_domain, sock_type, 0777, m_credentials.euid(), m_credentials.egid()));
int fd = TRY(get_free_fd());

View File

@ -911,6 +911,56 @@ namespace Kernel
return TRY(m_open_file_descriptors.socket(domain, type, protocol));
}
BAN::ErrorOr<long> Process::sys_getsockname(int socket, sockaddr* address, socklen_t* address_len)
{
LockGuard _(m_process_lock);
TRY(validate_pointer_access(address_len, sizeof(address_len)));
TRY(validate_pointer_access(address, *address_len));
auto inode = TRY(m_open_file_descriptors.inode_of(socket));
if (!inode->mode().ifsock())
return BAN::Error::from_errno(ENOTSOCK);
TRY(inode->getsockname(address, address_len));
return 0;
}
BAN::ErrorOr<long> Process::sys_getsockopt(int socket, int level, int option_name, void* option_value, socklen_t* option_len)
{
LockGuard _(m_process_lock);
TRY(validate_pointer_access(option_len, sizeof(option_len)));
TRY(validate_pointer_access(option_value, *option_len));
auto inode = TRY(m_open_file_descriptors.inode_of(socket));
if (!inode->mode().ifsock())
return BAN::Error::from_errno(ENOTSOCK);
// Because all networking is synchronous, there can not be errors to report
if (level == SOL_SOCKET && option_name == SO_ERROR)
{
if (*option_len)
*reinterpret_cast<uint8_t*>(option_value) = 0;
*option_len = BAN::Math::min<socklen_t>(*option_len, sizeof(int));
return 0;
}
dprintln("SYS_GETSOCKOPT {}, {}, {}, {}, {}", socket, level, option_name, option_value, option_len);
return BAN::Error::from_errno(ENOTSUP);
}
BAN::ErrorOr<long> Process::sys_setsockopt(int socket, int level, int option_name, const void* option_value, socklen_t option_len)
{
LockGuard _(m_process_lock);
TRY(validate_pointer_access(option_value, option_len));
auto inode = TRY(m_open_file_descriptors.inode_of(socket));
if (!inode->mode().ifsock())
return BAN::Error::from_errno(ENOTSOCK);
dprintln("SYS_GETSOCKOPT {}, {}, {}, {}, {}", socket, level, option_name, option_value, option_len);
return BAN::Error::from_errno(ENOTSUP);
}
BAN::ErrorOr<long> Process::sys_accept(int socket, sockaddr* address, socklen_t* address_len)
{
if (address && !address_len)

View File

@ -361,7 +361,7 @@ namespace Kernel
bool TTY::is_initialized()
{
return s_tty;
return !!s_tty;
}
}

View File

@ -10,6 +10,7 @@ set(LIBC_SOURCES
fcntl.cpp
grp.cpp
malloc.cpp
netdb.cpp
printf_impl.cpp
pwd.cpp
scanf_impl.cpp

View File

@ -61,6 +61,7 @@ __BEGIN_DECLS
#define NAME_MAX 255
#define PATH_MAX 256
#define LOGIN_NAME_MAX 256
#define HOST_NAME_MAX 255
__END_DECLS

View File

@ -90,6 +90,7 @@ void endservent(void);
void freeaddrinfo(struct addrinfo* ai);
const char* gai_strerror(int ecode);
int getaddrinfo(const char* __restrict nodename, const char* __restrict servname, const struct addrinfo* __restrict hints, struct addrinfo** __restrict res);
struct hostent* gethostbyname(const char* name);
struct hostent* gethostent(void);
int getnameinfo(const struct sockaddr* __restrict sa, socklen_t salen, char* __restrict node, socklen_t nodelen, char* __restrict service, socklen_t servicelen, int flags);
struct netent* getnetbyaddr(uint32_t net, int type);

View File

@ -78,6 +78,9 @@ __BEGIN_DECLS
O(SYS_SMO_DELETE, smo_delete) \
O(SYS_SMO_MAP, smo_map) \
O(SYS_ISATTY, isatty) \
O(SYS_GETSOCKNAME, getsockname) \
O(SYS_GETSOCKOPT, getsockopt) \
O(SYS_SETSOCKOPT, setsockopt) \
enum Syscall
{

69
libc/netdb.cpp Normal file
View File

@ -0,0 +1,69 @@
#include <BAN/Bitcast.h>
#include <arpa/inet.h>
#include <limits.h>
#include <netdb.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
struct hostent* gethostbyname(const char* name)
{
static char name_buffer[HOST_NAME_MAX + 1];
static in_addr_t addr_buffer;
static in_addr_t* addr_ptrs[2];
static struct hostent hostent;
if (strlen(name) > HOST_NAME_MAX)
return nullptr;
strcpy(name_buffer, name);
hostent.h_name = name_buffer;
hostent.h_aliases = nullptr;
hostent.h_addrtype = AF_INET;
hostent.h_length = sizeof(in_addr_t);
addr_ptrs[0] = &addr_buffer;
addr_ptrs[1] = nullptr;
hostent.h_addr_list = reinterpret_cast<char**>(addr_ptrs);
int socket = -1;
if (in_addr_t ipv4 = inet_addr(name); ipv4 != (in_addr_t)(-1))
addr_buffer = ipv4;
else
{
int socket = ::socket(AF_UNIX, SOCK_SEQPACKET, 0);
if (socket == -1)
return nullptr;
sockaddr_un addr;
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, "/tmp/resolver.sock");
if (connect(socket, (sockaddr*)&addr, sizeof(addr)) == -1)
goto error_close_socket;
if (send(socket, name, strlen(name), 0) == -1)
goto error_close_socket;
sockaddr_storage storage;
if (recv(socket, &storage, sizeof(storage), 0) == -1)
goto error_close_socket;
close(socket);
if (storage.ss_family != AF_INET)
return nullptr;
addr_buffer = *reinterpret_cast<in_addr_t*>(storage.ss_storage);
}
return &hostent;
error_close_socket:
close(socket);
return nullptr;
}

View File

@ -1,3 +1,6 @@
#include <BAN/Assert.h>
#include <BAN/Debug.h>
#include <bits/printf.h>
#include <errno.h>
#include <fcntl.h>
@ -603,7 +606,11 @@ int remove(const char* path)
}
// TODO
int rename(const char*, const char*);
int rename(const char* old, const char* _new)
{
dwarnln("rename({}, {})", old, _new);
ASSERT_NOT_REACHED();
}
void rewind(FILE* file)
{

View File

@ -62,3 +62,18 @@ int socket(int domain, int type, int protocol)
{
return syscall(SYS_SOCKET, domain, type, protocol);
}
int getsockname(int socket, struct sockaddr* __restrict address, socklen_t* __restrict address_len)
{
return syscall(SYS_GETSOCKNAME, socket, address, address_len);
}
int getsockopt(int socket, int level, int option_name, void* __restrict option_value, socklen_t* __restrict option_len)
{
return syscall(SYS_GETSOCKOPT, socket, level, option_name, option_value, option_len);
}
int setsockopt(int socket, int level, int option_name, const void* option_value, socklen_t option_len)
{
return syscall(SYS_SETSOCKOPT, socket, level, option_name, option_value, option_len);
}

View File

@ -1,3 +1,6 @@
#include <BAN/Assert.h>
#include <BAN/Debug.h>
#include <sys/syscall.h>
#include <time.h>
#include <unistd.h>
@ -75,3 +78,9 @@ struct tm* localtime(const time_t* timer)
// FIXME: support timezones
return gmtime(timer);
}
size_t strftime(char* __restrict s, size_t maxsize, const char* __restrict format, const struct tm* __restrict timeptr)
{
dwarnln("strftime({}, {}, {}, {})", s, maxsize, format, timeptr);
ASSERT_NOT_REACHED();
}

1
ports/curl/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
curl-*

58
ports/curl/build.sh Executable file
View File

@ -0,0 +1,58 @@
#!/bin/sh
if [ -z $BANAN_ARCH ]; then
echo "You must set the BANAN_ARCH environment variable" >&2
exit 1
fi
if [ -z $BANAN_SYSROOT ]; then
echo "You must set the BANAN_ARCH environment variable" >&2
exit 1
fi
if [ -z $BANAN_TOOLCHAIN_PREFIX ]; then
echo "You must set the BANAN_TOOLCHAIN_PREFIX environment variable" >&2
exit 1
fi
CURL_VERSION="curl-8.8.0"
CURL_TAR="$CURL_VERSION.tar.gz"
CURL_URL="https://curl.se/download/$CURL_TAR"
cd $(dirname $(realpath $0))
if [ ! -d $CURL_VERSION ]; then
if [ ! -f $CURL_TAR ]; then
wget $CURL_URL
fi
tar xf $CURL_TAR
for patch in ./patches/*; do
patch -ruN -d $CURL_VERSION < "$patch"
done
grep -qxF doom ../installed || echo curl >> ../installed
fi
cd $CURL_VERSION
export PATH="$BANAN_TOOLCHAIN_PREFIX/bin:$PATH"
if [ ! -d "build-${BANAN_ARCH}" ]; then
mkdir -p "build-${BANAN_ARCH}"
cd "build-${BANAN_ARCH}"
../configure \
--host=x86_64-banan_os \
--prefix=$BANAN_SYSROOT/usr \
--without-ssl \
--disable-threaded-resolver \
--disable-ipv6 \
--disable-docs
cd ..
fi
cd "build-${BANAN_ARCH}"
make && make install

View File

@ -0,0 +1,12 @@
diff -ruN curl-8.8.0/config.sub curl-patched/config.sub
--- curl-8.8.0/config.sub 2023-03-13 00:39:42.000000000 +0200
+++ curl-patched/config.sub 2024-06-17 21:16:00.348556957 +0300
@@ -1754,7 +1754,7 @@
| onefs* | tirtos* | phoenix* | fuchsia* | redox* | bme* \
| midnightbsd* | amdhsa* | unleashed* | emscripten* | wasi* \
| nsk* | powerunix* | genode* | zvmoe* | qnx* | emx* | zephyr* \
- | fiwix* )
+ | fiwix* | banan_os* )
;;
# This one is extra strict with allowed versions
sco3.2v2 | sco3.2v[4-9]* | sco5v6*)

View File

@ -5,7 +5,7 @@ Subject: [PATCH] Add support for banan-os
---
doomgeneric/Makefile.banan_os | 57 +++++++++++
doomgeneric/doomgeneric_banan_os.cpp | 144 +++++++++++++++++++++++++++
doomgeneric/doomgeneric_banan_os.cpp | 138 +++++++++++++++++++++++++++
2 files changed, 200 insertions(+)
create mode 100644 doomgeneric/Makefile.banan_os
create mode 100644 doomgeneric/doomgeneric_banan_os.cpp
@ -79,7 +79,7 @@ new file mode 100644
index 0000000..9161771
--- /dev/null
+++ b/doomgeneric/doomgeneric_banan_os.cpp
@@ -0,0 +1,144 @@
@@ -0,0 +1,138 @@
+extern "C"
+{
+#include "doomgeneric.h"
@ -217,12 +217,6 @@ index 0000000..9161771
+ return 0;
+}
+
+int rename(const char* oldp, const char* newp)
+{
+ fprintf(stderr, "rename(\"%s\", \"%s\")\n", oldp, newp);
+ exit(1);
+}
+
+}
--
2.45.2