toaruos/kernel/net/socket.c

246 lines
6.5 KiB
C
Raw Normal View History

2021-05-31 04:47:02 +03:00
/**
* @file kernel/net/socket.c
* @brief Top-level socket manager.
*
* Provides the standard socket interface.
*
* @copyright This file is part of ToaruOS and is released under the terms
* of the NCSA / University of Illinois License - see LICENSE.md
* @author 2021 K. Lange
*/
#include <errno.h>
#include <kernel/types.h>
2021-06-06 08:22:14 +03:00
#include <kernel/string.h>
2021-05-31 04:47:02 +03:00
#include <kernel/printf.h>
2021-06-09 02:23:34 +03:00
#include <kernel/list.h>
2021-05-31 04:47:02 +03:00
#include <kernel/syscall.h>
2021-06-06 08:22:14 +03:00
#include <kernel/vfs.h>
2021-05-31 04:47:02 +03:00
2021-06-09 02:23:34 +03:00
#include <kernel/net/netif.h>
2021-05-31 04:47:02 +03:00
#include <sys/socket.h>
#ifndef MISAKA_DEBUG_NET
#define printf(...)
#endif
2021-05-31 04:47:02 +03:00
/**
* TODO: Should we have an interface for modules to install protocol handlers?
* Thinking this should work like the VFS, with method tables for different
* protocol handlers, but a lot of this stuff is also just generic...
*/
extern long net_ipv4_socket(int,int);
2021-06-09 02:23:34 +03:00
void net_sock_alert(sock_t * sock) {
spin_lock(sock->alert_lock);
while (sock->alert_wait->head) {
node_t * node = list_dequeue(sock->alert_wait);
process_t * p = node->value;
free(node);
spin_unlock(sock->alert_lock);
process_alert_node(p, (fs_node_t*)sock);
spin_lock(sock->alert_lock);
}
spin_unlock(sock->alert_lock);
}
void net_sock_add(sock_t * sock, void * frame, size_t size) {
2021-06-09 02:23:34 +03:00
spin_lock(sock->rx_lock);
char * bleh = malloc(size);
memcpy(bleh, frame, size);
2021-06-09 02:23:34 +03:00
list_insert(sock->rx_queue, bleh);
wakeup_queue(sock->rx_wait);
net_sock_alert(sock);
spin_unlock(sock->rx_lock);
}
void * net_sock_get(sock_t * sock) {
while (!sock->rx_queue->length) {
sleep_on(sock->rx_wait);
}
spin_lock(sock->rx_lock);
node_t * n = list_dequeue(sock->rx_queue);
void* value = n->value;
free(n);
spin_unlock(sock->rx_lock);
return value;
}
int sock_generic_check(fs_node_t *node) {
sock_t * sock = (sock_t*)node;
return sock->rx_queue->length ? 0 : 1;
}
int sock_generic_wait(fs_node_t *node, void * process) {
sock_t * sock = (sock_t*)node;
spin_lock(sock->alert_lock);
if (!list_find(sock->alert_wait, process)) {
list_insert(sock->alert_wait, process);
}
list_insert(((process_t *)process)->node_waits, sock);
spin_unlock(sock->alert_lock);
return 0;
}
void sock_generic_close(fs_node_t *node) {
sock_t * sock = (sock_t*)node;
sock->sock_close(sock);
printf("net: socket closed\n");
}
sock_t * net_sock_create(void) {
sock_t * sock = calloc(sizeof(struct SockData),1);
sock->_fnode.flags = FS_PIPE; /* uh, FS_SOCKET? */
sock->_fnode.mask = 0600;
sock->_fnode.device = NULL;
sock->_fnode.selectcheck = sock_generic_check;
sock->_fnode.selectwait = sock_generic_wait;
sock->_fnode.close = sock_generic_close;
sock->alert_wait = list_create("socket alert wait", sock);
sock->rx_wait = list_create("socket rx wait", sock);
sock->rx_queue = list_create("socket rx queue", sock);
open_fs((fs_node_t*)sock,0);
return sock;
}
spin_lock_t net_raw_sockets_lock = {0};
list_t * net_raw_sockets_list = NULL;
static long sock_raw_recv(sock_t * sock, struct msghdr * msg, int flags) {
if (!sock->_fnode.device) return -EINVAL;
if (msg->msg_iovlen > 1) {
printf("net: todo: can't recv multiple iovs\n");
return -ENOTSUP;
}
if (msg->msg_iovlen == 0) return 0;
if (msg->msg_iov[0].iov_len != 8092) return -EINVAL;
void * data = net_sock_get(sock);
memcpy(msg->msg_iov[0].iov_base, data, 8092);
free(data);
return 8092;
}
static long sock_raw_send(sock_t * sock, const struct msghdr *msg, int flags) {
if (!sock->_fnode.device) return -EINVAL;
if (msg->msg_iovlen > 1) {
printf("net: todo: can't send multiple iovs\n");
return -ENOTSUP;
}
if (msg->msg_iovlen == 0) return 0;
return write_fs(sock->_fnode.device, 0, msg->msg_iov[0].iov_len, msg->msg_iov[0].iov_base);
}
static void sock_raw_close(sock_t * sock) {
spin_lock(net_raw_sockets_lock);
list_delete(net_raw_sockets_list, list_find(net_raw_sockets_list, sock));
spin_unlock(net_raw_sockets_lock);
/* free stuff ? */
}
2021-06-06 08:22:14 +03:00
/**
* Raw sockets
*/
long net_raw_socket(int type, int protocol) {
if (type != SOCK_RAW) return -EINVAL;
/* Make a new raw socket? */
2021-06-09 02:23:34 +03:00
sock_t * sock = net_sock_create();
sock->sock_recv = sock_raw_recv;
sock->sock_send = sock_raw_send;
sock->sock_close = sock_raw_close;
spin_lock(net_raw_sockets_lock);
list_insert(net_raw_sockets_list, sock);
spin_unlock(net_raw_sockets_lock);
2021-06-06 08:22:14 +03:00
2021-06-09 02:23:34 +03:00
int fd = process_append_fd((process_t *)this_core->current_process, (fs_node_t *)sock);
2021-06-06 08:22:14 +03:00
return fd;
}
2021-05-31 04:47:02 +03:00
long net_socket(int domain, int type, int protocol) {
switch (domain) {
case AF_INET:
return net_ipv4_socket(type, protocol);
2021-06-06 08:22:14 +03:00
case AF_RAW:
return net_raw_socket(type, protocol);
2021-05-31 04:47:02 +03:00
default:
return -EINVAL;
}
}
2021-06-09 02:23:34 +03:00
long net_so_socket(struct SockData * sock, int optname, const void *optval, socklen_t optlen) {
switch (optname) {
case SO_BINDTODEVICE: {
if (optlen < 1 || optlen > 32 || ((const char*)optval)[optlen-1] != 0) return -EINVAL;
fs_node_t * netif = net_if_lookup((const char*)optval);
if (!netif) return -ENOENT;
sock->_fnode.device = netif;
return 0;
}
default:
return -ENOPROTOOPT;
}
}
2021-05-31 04:47:02 +03:00
long net_setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen) {
2021-06-09 02:23:34 +03:00
if (!FD_CHECK(sockfd)) return -EBADF;
PTR_VALIDATE(optval);
sock_t * node = (sock_t*)FD_ENTRY(sockfd);
switch (level) {
case SOL_SOCKET:
return net_so_socket(node,optname,optval,optlen);
default:
return -ENOPROTOOPT;
}
2021-05-31 04:47:02 +03:00
return -EINVAL;
}
long net_getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen) {
2021-06-14 08:18:23 +03:00
if (!FD_CHECK(sockfd)) return -EBADF;
2021-05-31 04:47:02 +03:00
return -EINVAL;
}
long net_bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
2021-06-14 08:18:23 +03:00
if (!FD_CHECK(sockfd)) return -EBADF;
2021-05-31 04:47:02 +03:00
return -EINVAL;
}
long net_accept(int sockfd, struct sockaddr * addr, socklen_t * addrlen) {
2021-06-14 08:18:23 +03:00
if (!FD_CHECK(sockfd)) return -EBADF;
2021-05-31 04:47:02 +03:00
return -EINVAL;
}
2021-06-14 08:18:23 +03:00
long net_listen(int sockfd, int backlog) {
if (!FD_CHECK(sockfd)) return -EBADF;
2021-05-31 04:47:02 +03:00
return -EINVAL;
}
long net_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
2021-06-14 08:18:23 +03:00
if (!FD_CHECK(sockfd)) return -EBADF;
sock_t * node = (sock_t*)FD_ENTRY(sockfd);
if (!node->sock_connect) return -EINVAL;
return node->sock_connect(node,addr,addrlen);
2021-05-31 04:47:02 +03:00
}
long net_recv(int sockfd, struct msghdr * msg, int flags) {
2021-06-09 02:23:34 +03:00
if (!FD_CHECK(sockfd)) return -EBADF;
PTR_VALIDATE(msg);
sock_t * node = (sock_t*)FD_ENTRY(sockfd);
return node->sock_recv(node,msg,flags);
2021-05-31 04:47:02 +03:00
}
long net_send(int sockfd, const struct msghdr * msg, int flags) {
2021-06-09 02:23:34 +03:00
if (!FD_CHECK(sockfd)) return -EBADF;
PTR_VALIDATE(msg);
sock_t * node = (sock_t*)FD_ENTRY(sockfd);
return node->sock_send(node,msg,flags);
2021-05-31 04:47:02 +03:00
}
long net_shutdown(int sockfd, int how) {
return -EINVAL;
}