summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Kling <anton@kling.gg>2023-10-27 00:48:21 +0200
committerAnton Kling <anton@kling.gg>2023-10-30 21:49:48 +0100
commit5026f823fa2708404302aa59d03401635a435c0b (patch)
tree03d8db6da25416fa27b9744ae60df2cfa5fc1d2b
parentf8e15da04472f5ed6a26e588de4a23cb3e1ba20b (diff)
Kernel/Networking/LibC: Add syscalls and libc functions for UDP
This allows a UDP server to be created in userland and read data. Currently it can't send data and is very very simplistic. Code is horrible and probably needs some fixing until it can be further built upon.
-rw-r--r--Makefile2
-rw-r--r--cpu/syscall.c6
-rw-r--r--drivers/rtl8139.c3
-rw-r--r--fs/fifo.h5
-rw-r--r--network/arp.c5
-rw-r--r--network/arp.h2
-rw-r--r--network/ethernet.c8
-rw-r--r--network/ethernet.h2
-rw-r--r--network/ipv4.c27
-rw-r--r--network/ipv4.h3
-rw-r--r--network/udp.c26
-rw-r--r--network/udp.h3
-rw-r--r--scalls/recvfrom.c15
-rw-r--r--scalls/recvfrom.h4
-rw-r--r--socket.c106
-rw-r--r--socket.h44
-rw-r--r--userland/libc/Makefile5
-rw-r--r--userland/libc/include/socket.h16
-rw-r--r--userland/libc/include/sys/socket.h7
-rw-r--r--userland/libc/include/syscall.h1
20 files changed, 215 insertions, 75 deletions
diff --git a/Makefile b/Makefile
index 04fad93..c5438fe 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
CC="./sysroot/bin/i686-sb-gcc"
AS="./sysroot/bin/i686-sb-as"
-OBJ = arch/i386/boot.o init/kernel.o cpu/gdt.o cpu/reload_gdt.o cpu/idt.o cpu/io.o libc/stdio/print.o drivers/keyboard.o log.o drivers/pit.o libc/string/memcpy.o libc/string/strlen.o libc/string/memcmp.o drivers/ata.o libc/string/memset.o cpu/syscall.o read_eip.o libc/exit/assert.o process.o cpu/int_syscall.o libc/string/strcpy.o arch/i386/mmu.o kmalloc.o fs/ext2.o fs/vfs.o fs/devfs.o cpu/spinlock.o random.o libc/string/strcmp.o crypto/ChaCha20/chacha20.o crypto/SHA1/sha1.o fs/tmpfs.o libc/string/isequal.o drivers/pst.o halts.o scalls/ppoll.o scalls/ftruncate.o kubsan.o scalls/mmap.o drivers/serial.o scalls/accept.o scalls/bind.o scalls/socket.o socket.o poll.o fs/fifo.o hashmap/hashmap.o fs/shm.o scalls/shm.o elf.o ksbrk.o sched/scheduler.o scalls/stat.o libc/string/copy.o libc/string/strncpy.o drivers/mouse.o libc/string/strlcpy.o libc/string/strcat.o drivers/vbe.o scalls/msleep.o scalls/uptime.o scalls/mkdir.o drivers/pci.o drivers/rtl8139.o network/ethernet.o network/arp.o network/bytes.o
+OBJ = arch/i386/boot.o init/kernel.o cpu/gdt.o cpu/reload_gdt.o cpu/idt.o cpu/io.o libc/stdio/print.o drivers/keyboard.o log.o drivers/pit.o libc/string/memcpy.o libc/string/strlen.o libc/string/memcmp.o drivers/ata.o libc/string/memset.o cpu/syscall.o read_eip.o libc/exit/assert.o process.o cpu/int_syscall.o libc/string/strcpy.o arch/i386/mmu.o kmalloc.o fs/ext2.o fs/vfs.o fs/devfs.o cpu/spinlock.o random.o libc/string/strcmp.o crypto/ChaCha20/chacha20.o crypto/SHA1/sha1.o fs/tmpfs.o libc/string/isequal.o drivers/pst.o halts.o scalls/ppoll.o scalls/ftruncate.o kubsan.o scalls/mmap.o drivers/serial.o scalls/accept.o scalls/bind.o scalls/socket.o socket.o poll.o fs/fifo.o hashmap/hashmap.o fs/shm.o scalls/shm.o elf.o ksbrk.o sched/scheduler.o scalls/stat.o libc/string/copy.o libc/string/strncpy.o drivers/mouse.o libc/string/strlcpy.o libc/string/strcat.o drivers/vbe.o scalls/msleep.o scalls/uptime.o scalls/mkdir.o drivers/pci.o drivers/rtl8139.o network/ethernet.o network/arp.o network/bytes.o network/ipv4.o network/udp.o scalls/recvfrom.o
CFLAGS = -O2 -fsanitize=vla-bound,shift-exponent,pointer-overflow,shift,signed-integer-overflow,bounds -ggdb -ffreestanding -Wall -Werror -mgeneral-regs-only -Wimplicit-fallthrough -I./libc/include/ -I.
INCLUDE=-I./includes/ -I./libc/include/
diff --git a/cpu/syscall.c b/cpu/syscall.c
index c072053..6ad5514 100644
--- a/cpu/syscall.c
+++ b/cpu/syscall.c
@@ -9,15 +9,16 @@
#include <kmalloc.h>
#include <scalls/accept.h>
#include <scalls/bind.h>
+#include <scalls/ftruncate.h>
+#include <scalls/mkdir.h>
#include <scalls/mmap.h>
#include <scalls/msleep.h>
#include <scalls/ppoll.h>
+#include <scalls/recvfrom.h>
#include <scalls/shm.h>
-#include <scalls/ftruncate.h>
#include <scalls/socket.h>
#include <scalls/stat.h>
#include <scalls/uptime.h>
-#include <scalls/mkdir.h>
#include <stdint.h>
#include <string.h>
@@ -162,6 +163,7 @@ void (*syscall_functions[])() = {
(void(*))syscall_ftruncate, (void(*))syscall_stat,
(void(*))syscall_msleep, (void(*))syscall_uptime,
(void(*))syscall_mkdir,
+ (void(*))syscall_recvfrom,
};
void syscall_function_handler(uint32_t eax, uint32_t arg1, uint32_t arg2,
diff --git a/drivers/rtl8139.c b/drivers/rtl8139.c
index 0c1606b..02a5159 100644
--- a/drivers/rtl8139.c
+++ b/drivers/rtl8139.c
@@ -178,7 +178,4 @@ void rtl8139_init(void) {
// I have no fucking clue why that happens and it was a pain to debug.
for (int i = 0; i < 4; i++)
send_buffers[i] = ksbrk(0x1000);
- asm("sti");
- for (;;)
- asm("sti");
}
diff --git a/fs/fifo.h b/fs/fifo.h
index ee91ba1..1ba7168 100644
--- a/fs/fifo.h
+++ b/fs/fifo.h
@@ -1,17 +1,18 @@
+typedef struct S_FIFO_FILE FIFO_FILE;
#ifndef FIFO_H
#define FIFO_H
#include "vfs.h"
#include <stddef.h>
#include <stdint.h>
-typedef struct {
+struct S_FIFO_FILE {
char *buffer;
uint64_t buffer_len;
uint64_t write_len;
uint8_t is_blocking;
uint8_t has_data;
uint8_t can_write;
-} FIFO_FILE;
+};
int create_fifo(void);
FIFO_FILE *create_fifo_object(void);
diff --git a/network/arp.c b/network/arp.c
index eb8aca3..f86c732 100644
--- a/network/arp.c
+++ b/network/arp.c
@@ -43,7 +43,7 @@ void print_ip(const char *str, uint8_t *ip) {
kprintf("\n");
}
-void handle_arp(uint8_t *payload) {
+void handle_arp(const uint8_t *payload) {
struct ARP_DATA *data = (struct ARP_DATA *)payload;
// Assert that communication is over ethernet
@@ -79,5 +79,6 @@ void handle_arp(uint8_t *payload) {
memcpy(response.dsthw, data->srchw, sizeof(uint8_t[6]));
memcpy(response.dstpr, data->srcpr, sizeof(uint8_t[4]));
- send_ethernet_packet(data->srchw, 0x0806, (uint8_t*)&response, sizeof(response));
+ send_ethernet_packet(data->srchw, 0x0806, (uint8_t *)&response,
+ sizeof(response));
}
diff --git a/network/arp.h b/network/arp.h
index 1e8df1b..8efda97 100644
--- a/network/arp.h
+++ b/network/arp.h
@@ -1,3 +1,3 @@
#include <stdint.h>
-void handle_arp(uint8_t *payload);
+void handle_arp(const uint8_t *payload);
diff --git a/network/ethernet.c b/network/ethernet.c
index ae8e814..11a3876 100644
--- a/network/ethernet.c
+++ b/network/ethernet.c
@@ -4,6 +4,7 @@
#include <network/arp.h>
#include <network/bytes.h>
#include <network/ethernet.h>
+#include <network/ipv4.h>
#include <stdio.h>
struct ETHERNET_HEADER {
@@ -44,10 +45,10 @@ uint32_t crc32(const char *buf, size_t len) {
return ~crc;
}
-void handle_ethernet(uint8_t *packet, uint64_t packet_length) {
+void handle_ethernet(const uint8_t *packet, uint64_t packet_length) {
struct ETHERNET_HEADER *eth_header = (struct ETHERNET_HEADER *)packet;
packet += sizeof(struct ETHERNET_HEADER);
- uint8_t *payload = packet;
+ const uint8_t *payload = packet;
packet += packet_length - sizeof(struct ETHERNET_HEADER);
uint32_t crc = *((uint32_t *)packet - 1);
kprintf("PACKET crc: %x\n", crc);
@@ -70,7 +71,7 @@ void handle_ethernet(uint8_t *packet, uint64_t packet_length) {
handle_arp(payload);
break;
case 0x0800:
- kprintf("IPV4 message\n");
+ handle_ipv4(payload, packet_length - sizeof(struct ETHERNET_HEADER) - 4);
break;
default:
kprintf("Can't handle ethernet type\n");
@@ -97,5 +98,6 @@ void send_ethernet_packet(uint8_t mac_dst[6], uint16_t type, uint8_t *payload,
htonl(crc32((const char *)buffer_start, buffer_size - 4));
assert(rtl8139_send_data(buffer_start, buffer_size));
+ kfree(buffer_start);
kprintf("sent data\n");
}
diff --git a/network/ethernet.h b/network/ethernet.h
index 88a4dc6..0fdcee3 100644
--- a/network/ethernet.h
+++ b/network/ethernet.h
@@ -1,5 +1,5 @@
#include <stdint.h>
-void handle_ethernet(uint8_t *packet, uint64_t packet_length);
+void handle_ethernet(const uint8_t *packet, uint64_t packet_length);
void send_ethernet_packet(uint8_t mac_dst[6], uint16_t type, uint8_t *payload,
uint64_t payload_length);
diff --git a/network/ipv4.c b/network/ipv4.c
new file mode 100644
index 0000000..27b38ec
--- /dev/null
+++ b/network/ipv4.c
@@ -0,0 +1,27 @@
+#include <assert.h>
+#include <network/bytes.h>
+#include <network/ipv4.h>
+#include <network/udp.h>
+
+void handle_ipv4(const uint8_t *payload, uint32_t packet_length) {
+ assert(packet_length > 4);
+ uint8_t version = (*payload & 0xF0) >> 4;
+ uint8_t IHL = (*payload & 0xF);
+ kprintf("version: %x\n", version);
+ assert(4 == version);
+ assert(5 == IHL);
+ uint16_t ipv4_total_length = ntohs(*(uint16_t *)(payload + 2));
+ assert(ipv4_total_length >= 20);
+ // Make sure the ipv4 header is not trying to get uninitalized memory
+ assert(ipv4_total_length <= packet_length);
+
+ uint8_t protocol = *(payload + 9);
+ switch (protocol) {
+ case 0x11:
+ handle_udp(payload + 20, ipv4_total_length - 20);
+ break;
+ default:
+ kprintf("Protocol given in IPv4 header not handeld: %x\n", protocol);
+ break;
+ }
+}
diff --git a/network/ipv4.h b/network/ipv4.h
new file mode 100644
index 0000000..294a0ef
--- /dev/null
+++ b/network/ipv4.h
@@ -0,0 +1,3 @@
+#include <stdint.h>
+
+void handle_ipv4(const uint8_t *payload, uint32_t packet_length);
diff --git a/network/udp.c b/network/udp.c
new file mode 100644
index 0000000..fb8ba44
--- /dev/null
+++ b/network/udp.c
@@ -0,0 +1,26 @@
+#include <assert.h>
+#include <network/bytes.h>
+#include <network/udp.h>
+#include <socket.h>
+
+void handle_udp(const uint8_t *payload, uint32_t packet_length) {
+ assert(packet_length >= 8);
+ uint16_t source_port = ntohs(*(uint16_t *)payload);
+ uint16_t dst_port = ntohs(*(uint16_t *)(payload + 2));
+ uint16_t length = ntohs(*(uint16_t *)(payload + 4));
+ assert(length == packet_length);
+ kprintf("source_port: %d\n", source_port);
+ kprintf("dst_port: %d\n", dst_port);
+ uint32_t data_length = length - 8;
+ const uint8_t *data = payload + 8;
+
+ // Find the open port
+ OPEN_INET_SOCKET *in_s = find_open_udp_port(htons(dst_port));
+ assert(in_s);
+ SOCKET *s = in_s->s;
+ vfs_fd_t *fifo_file = s->ptr_socket_fd;
+ raw_vfs_pwrite(fifo_file, (char *)data, data_length, 0);
+ for (uint32_t i = 0; i < data_length; i++) {
+ kprintf("%c", data[i]);
+ }
+}
diff --git a/network/udp.h b/network/udp.h
new file mode 100644
index 0000000..c6ac753
--- /dev/null
+++ b/network/udp.h
@@ -0,0 +1,3 @@
+#include <stdint.h>
+
+void handle_udp(const uint8_t *payload, uint32_t packet_length);
diff --git a/scalls/recvfrom.c b/scalls/recvfrom.c
new file mode 100644
index 0000000..0e37153
--- /dev/null
+++ b/scalls/recvfrom.c
@@ -0,0 +1,15 @@
+#include <fs/vfs.h>
+#include <poll.h>
+#include <scalls/recvfrom.h>
+
+size_t syscall_recvfrom(int socket, void *buffer, size_t length, int flags,
+ struct sockaddr *address, socklen_t *address_len) {
+ if (flags & MSG_WAITALL) {
+ struct pollfd fds[1];
+ fds[0].fd = socket;
+ fds[0].events = POLLIN;
+ poll(fds, 1, 0);
+ }
+ kprintf("got event\n");
+ return vfs_pread(socket, buffer, length, 0);
+}
diff --git a/scalls/recvfrom.h b/scalls/recvfrom.h
new file mode 100644
index 0000000..9897899
--- /dev/null
+++ b/scalls/recvfrom.h
@@ -0,0 +1,4 @@
+#include <socket.h>
+
+size_t syscall_recvfrom(int socket, void *buffer, size_t length, int flags,
+ struct sockaddr *address, socklen_t *address_len);
diff --git a/socket.c b/socket.c
index 59f86bf..4ef4b3a 100644
--- a/socket.c
+++ b/socket.c
@@ -1,3 +1,4 @@
+#include <assert.h>
#include <errno.h>
#include <fs/devfs.h>
#include <fs/tmpfs.h>
@@ -5,7 +6,19 @@
#include <sched/scheduler.h>
#include <socket.h>
+// FIXME: Make these more dynamic
OPEN_UNIX_SOCKET *un_sockets[100] = {0};
+OPEN_INET_SOCKET *inet_sockets[100] = {0};
+
+OPEN_INET_SOCKET *find_open_udp_port(uint16_t port) {
+ for (int i = 0; i < 100; i++) {
+ if (!inet_sockets[i])
+ continue;
+ if (inet_sockets[i]->port == port)
+ return inet_sockets[i];
+ }
+ return NULL;
+}
int uds_open(const char *path) {
// FIXME: This is super ugly
@@ -35,8 +48,8 @@ int uds_open(const char *path) {
dual_pipe(fd);
char c = 'i';
- raw_vfs_pwrite(s->ptr_fifo_fd, &c, 1, 0);
- raw_vfs_pwrite(s->ptr_socket_fd, &c, 1, 0);
+ fifo_object_write((uint8_t *)&c, 1, 0, s->fifo_file);
+ s->ptr_socket_fd->inode->has_data = 1;
s->incoming_fd = get_current_task()->file_descriptors[fd[1]];
// vfs_close(fd[1]);
@@ -52,7 +65,7 @@ int accept(int socket, struct sockaddr *address, socklen_t *address_len) {
if (NULL == s->incoming_fd) {
// Wait until we have gotten a connection
struct pollfd fds[1];
- fds[0].fd = s->fifo_fd;
+ fds[0].fd = socket;
fds[0].events = POLLIN;
fds[0].revents = 0;
poll(fds, 1, 0);
@@ -64,8 +77,8 @@ int accept(int socket, struct sockaddr *address, socklen_t *address_len) {
get_current_task()->file_descriptors[n] = s->incoming_fd;
get_current_task()->file_descriptors[n]->reference_count++;
s->incoming_fd = NULL;
- for (char c; 0 < vfs_pread(s->fifo_fd, &c, 1, 0);)
- ;
+ // for (char c; 0 < vfs_pread(s->fifo_fd, &c, 1, 0);)
+ // ;
inode->has_data = 0;
// s->ptr_fifo_fd->inode->has_data = 0;
@@ -74,8 +87,6 @@ int accept(int socket, struct sockaddr *address, socklen_t *address_len) {
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
(void)addrlen;
- struct sockaddr_un *un = (struct sockaddr_un *)addr;
-
vfs_fd_t *fd = get_vfs_fd(sockfd);
if (!fd)
return -EBADF;
@@ -83,44 +94,64 @@ int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
if (!inode)
return -EBADF;
SOCKET *s = (SOCKET *)inode->internal_object;
-
- size_t path_len = strlen(un->sun_path);
- s->path = kmalloc(path_len + 1);
- memcpy(s->path, un->sun_path, path_len);
- s->path[path_len] = '\0';
-
- OPEN_UNIX_SOCKET *us;
- int i = 0;
- for (; i < 100; i++)
- if (!un_sockets[i])
- break;
-
- us = un_sockets[i] = kmalloc(sizeof(OPEN_UNIX_SOCKET));
-
- us->path = s->path;
- us->s = s;
- devfs_add_file(us->path, NULL, NULL, NULL, 1, 1, FS_TYPE_UNIX_SOCKET);
+ if (AF_UNIX == s->domain) {
+ struct sockaddr_un *un = (struct sockaddr_un *)addr;
+ size_t path_len = strlen(un->sun_path);
+ s->path = kmalloc(path_len + 1);
+ memcpy(s->path, un->sun_path, path_len);
+ s->path[path_len] = '\0';
+
+ OPEN_UNIX_SOCKET *us;
+ int i = 0;
+ for (; i < 100; i++)
+ if (!un_sockets[i])
+ break;
+
+ us = un_sockets[i] = kmalloc(sizeof(OPEN_UNIX_SOCKET));
+
+ us->path = s->path;
+ us->s = s;
+ devfs_add_file(us->path, NULL, NULL, NULL, 1, 1, FS_TYPE_UNIX_SOCKET);
+ return 0;
+ }
+ if (AF_INET == s->domain) {
+ struct sockaddr_in *in = (struct sockaddr_in *)addr;
+ assert(in->sin_family == AF_INET); // FIXME: Figure out error value
+ OPEN_INET_SOCKET *inet;
+ int i = 0;
+ for (; i < 100; i++)
+ if (!inet_sockets[i])
+ break;
+
+ inet = inet_sockets[i] = kmalloc(sizeof(OPEN_INET_SOCKET));
+ inet->address = in->sin_addr.s_addr;
+ inet->port = in->sin_port;
+ inet->s = s;
+ return 0;
+ }
return 0;
}
int socket_write(uint8_t *buffer, uint64_t offset, uint64_t len, vfs_fd_t *fd) {
- (void)buffer;
- (void)offset;
- fd->inode->has_data = 1;
- return len;
+ SOCKET *s = (SOCKET *)fd->inode->internal_object;
+ FIFO_FILE *file = s->fifo_file;
+ int rc = fifo_object_write(buffer, 0, len, file);
+ fd->inode->has_data = file->has_data;
+ return rc;
}
int socket_read(uint8_t *buffer, uint64_t offset, uint64_t len, vfs_fd_t *fd) {
- (void)buffer;
- (void)offset;
- fd->inode->has_data = 0;
- return len;
+ SOCKET *s = (SOCKET *)fd->inode->internal_object;
+ FIFO_FILE *file = s->fifo_file;
+ int rc = fifo_object_read(buffer, 0, len, file);
+ fd->inode->has_data = file->has_data;
+ return rc;
}
void socket_close(vfs_fd_t *fd) { fd->inode->is_open = 0; }
int socket(int domain, int type, int protocol) {
- if (AF_UNIX != domain)
+ if (!(AF_UNIX == domain || AF_INET == domain))
return -EINVAL;
SOCKET *new_socket = kmalloc_eternal(sizeof(SOCKET));
@@ -128,7 +159,8 @@ int socket(int domain, int type, int protocol) {
0 /*inode_num*/, FS_TYPE_UNIX_SOCKET, 0 /*has_data*/, 1 /*can_write*/,
1 /*is_open*/, new_socket /*internal_object*/, 0 /*file_size*/,
NULL /*open*/, NULL /*create_file*/, socket_read, socket_write,
- socket_close, NULL/*create_directory*/, NULL /*get_vm_object*/, NULL/*truncate*/);
+ socket_close, NULL /*create_directory*/, NULL /*get_vm_object*/,
+ NULL /*truncate*/);
vfs_fd_t *fd;
int n = vfs_create_fd(O_RDWR | O_NONBLOCK, 0, inode, &fd);
@@ -138,11 +170,9 @@ int socket(int domain, int type, int protocol) {
new_socket->protocol = protocol;
new_socket->path = NULL;
new_socket->incoming_fd = NULL;
- new_socket->fifo_fd = create_fifo();
- new_socket->ptr_fifo_fd =
- get_current_task()->file_descriptors[new_socket->fifo_fd];
- new_socket->ptr_socket_fd = fd;
+ new_socket->fifo_file = create_fifo_object();
+ new_socket->ptr_socket_fd = fd;
return n;
}
diff --git a/socket.h b/socket.h
index debe29d..168386e 100644
--- a/socket.h
+++ b/socket.h
@@ -1,34 +1,23 @@
#ifndef SOCKET_H
#define SOCKET_H
-#include "fs/vfs.h"
+#include <fs/fifo.h>
+#include <fs/vfs.h>
#include <stddef.h>
#include <stdint.h>
-/*
-// Create a directory with the proper permissions
-mkdir(path, 0700);
-// Append the name of the socket
-strcat(path, "/socket_name");
-
-// Create the socket normally
-sockaddr_un address;
-address.sun_family = AF_UNIX;
-strcpy(address.sun_path, path);
-int fd = socket(AF_UNIX, SOCK_STREAM, 0);
-bind(fd, (sockaddr*)(&address), sizeof(address));
-listen(fd, 100);
-*/
-
#define AF_UNIX 0
+#define AF_INET 1
#define AF_LOCAL AF_UNIX
+#define SOCK_DGRAM 0
+
#define INADDR_ANY 0
-typedef struct {
- int fifo_fd;
- vfs_fd_t *ptr_fifo_fd;
+#define MSG_WAITALL 1
+typedef struct {
vfs_fd_t *ptr_socket_fd;
+ FIFO_FILE *fifo_file;
int domain;
int type;
@@ -44,21 +33,36 @@ typedef struct {
SOCKET *s;
} OPEN_UNIX_SOCKET;
+typedef struct {
+ uint32_t address;
+ uint16_t port;
+ SOCKET *s;
+} OPEN_INET_SOCKET;
+
typedef uint32_t in_addr_t;
typedef uint16_t in_port_t;
typedef unsigned int sa_family_t;
-typedef uint32_t socklen_t;
+typedef int socklen_t;
struct sockaddr {
sa_family_t sa_family; /* Address family */
char *sa_data; /* Socket address */
};
+struct sockaddr_in {
+ sa_family_t sin_family;
+ union {
+ uint32_t s_addr;
+ } sin_addr;
+ uint16_t sin_port;
+};
+
struct sockaddr_un {
sa_family_t sun_family; /* Address family */
char *sun_path; /* Socket pathname */
};
+OPEN_INET_SOCKET *find_open_udp_port(uint16_t port);
int uds_open(const char *path);
int socket(int domain, int type, int protocol);
int accept(int socket, struct sockaddr *address, socklen_t *address_len);
diff --git a/userland/libc/Makefile b/userland/libc/Makefile
index 704bf79..02f5584 100644
--- a/userland/libc/Makefile
+++ b/userland/libc/Makefile
@@ -2,7 +2,7 @@ CC="i686-sb-gcc"
AR="i686-sb-ar"
AS="i686-sb-as"
CFLAGS = -ggdb -ffreestanding -O2 -Wall -pedantic -Wimplicit-fallthrough -I./include/ -static
-OBJ=crt0.o libc.o malloc/malloc.o pty.o sys/mman/mmap.o memset.o assert.o stdio/snprintf.o stdio/vfprintf.o string/memcpy.o string/memcmp.o string/strcmp.o ubsan.o string/strcpy.o isspace.o stdio/puts.o stdio/putchar.o dirent/opendir.o dirent/readdir.o dirent/closedir.o unistd/getopt.o dirent/scandir.o dirent/alphasort.o stdio/printf.o stdio/vdprintf.o stdio/vprintf.o stdio/dprintf.o stdio/vprintf.o string/strlen.o string/strnlen.o stdio/stdin.o stdio/getchar.o stdio/fgetc.o arpa/inet/htons.o arpa/inet/htonl.o stdio/fread.o stdio/fwrite.o stdio/fopen.o stdio/fclose.o stdio/fseek.o ctype/isascii.o stdio/fprintf.o stdlib/atoi.o stdlib/strtol.o ctype/toupper.o ctype/tolower.o string/strcat.o string/strchr.o string/sscanf.o sys/stat/stat.o stdlib/getenv.o string/strrchr.o stdio/ftell.o stdio/tmpfile.o stdio/fgets.o stdio/feof.o stdio/fscanf.o stdio/ungetc.o string/strncmp.o stdio/fputc.o string/strncpy.o stdio/remove.o stdio/ferror.o stdio/fputs.o stdlib/rand.o stdlib/srand.o unistd/getpid.o stdlib/strtoul.o stdio/fflush.o stdlib/abort.o string/strcspn.o time/localtime.o time/time.o time/clock_gettime.o time/gmtime.o time/strftime.o string/strpbrk.o ctype/isdigit.o ctype/isalpha.o ctype/isxdigit.o ctype/ispunct.o stdio/setvbuf.o stdio/fileno.o stdio/putc.o stdio/sprintf.o stdlib/abs.o string/strspn.o stdlib/qsort.o string/memmove.o setjmp/longjmp.o setjmp/setjmp.o libgen/basename.o string/strdup.o string/strndup.o string/strlcpy.o stdlib/atexit.o stdio/open_memstream.o libgen/dirname.o unistd/unlink.o string/strstr.o string/strcasecmp.o string/strncasecmp.o stdlib/mkstemp.o string/strtok.o unistd/execvp.o unistd/_exit.o ctype/isalnum.o time/ctime_r.o stdlib/strtold.o sys/time/gettimeofday.o stdio/fgetpos.o stdio/fsetpos.o ctype/isprint.o stdlib/system.o stdio/tmpnam.o unistd/msleep.o stdlib/atof.o stdlib/strtod.o stdio/rename.o sys/stat/mkdir.o unistd/uptime.o unistd/ftruncate.o
+OBJ=crt0.o libc.o malloc/malloc.o pty.o sys/mman/mmap.o memset.o assert.o stdio/snprintf.o stdio/vfprintf.o string/memcpy.o string/memcmp.o string/strcmp.o ubsan.o string/strcpy.o isspace.o stdio/puts.o stdio/putchar.o dirent/opendir.o dirent/readdir.o dirent/closedir.o unistd/getopt.o dirent/scandir.o dirent/alphasort.o stdio/printf.o stdio/vdprintf.o stdio/vprintf.o stdio/dprintf.o stdio/vprintf.o string/strlen.o string/strnlen.o stdio/stdin.o stdio/getchar.o stdio/fgetc.o arpa/inet/htons.o arpa/inet/htonl.o stdio/fread.o stdio/fwrite.o stdio/fopen.o stdio/fclose.o stdio/fseek.o ctype/isascii.o stdio/fprintf.o stdlib/atoi.o stdlib/strtol.o ctype/toupper.o ctype/tolower.o string/strcat.o string/strchr.o string/sscanf.o sys/stat/stat.o stdlib/getenv.o string/strrchr.o stdio/ftell.o stdio/tmpfile.o stdio/fgets.o stdio/feof.o stdio/fscanf.o stdio/ungetc.o string/strncmp.o stdio/fputc.o string/strncpy.o stdio/remove.o stdio/ferror.o stdio/fputs.o stdlib/rand.o stdlib/srand.o unistd/getpid.o stdlib/strtoul.o stdio/fflush.o stdlib/abort.o string/strcspn.o time/localtime.o time/time.o time/clock_gettime.o time/gmtime.o time/strftime.o string/strpbrk.o ctype/isdigit.o ctype/isalpha.o ctype/isxdigit.o ctype/ispunct.o stdio/setvbuf.o stdio/fileno.o stdio/putc.o stdio/sprintf.o stdlib/abs.o string/strspn.o stdlib/qsort.o string/memmove.o setjmp/longjmp.o setjmp/setjmp.o libgen/basename.o string/strdup.o string/strndup.o string/strlcpy.o stdlib/atexit.o stdio/open_memstream.o libgen/dirname.o unistd/unlink.o string/strstr.o string/strcasecmp.o string/strncasecmp.o stdlib/mkstemp.o string/strtok.o unistd/execvp.o unistd/_exit.o ctype/isalnum.o time/ctime_r.o stdlib/strtold.o sys/time/gettimeofday.o stdio/fgetpos.o stdio/fsetpos.o ctype/isprint.o stdlib/system.o stdio/tmpnam.o unistd/msleep.o stdlib/atof.o stdlib/strtod.o stdio/rename.o sys/stat/mkdir.o unistd/uptime.o unistd/ftruncate.o sys/socket/recvfrom.o
all: libc.a
%.o: %.c
@@ -18,6 +18,9 @@ install:
cp crt0.o ../../sysroot/lib/
cp libc.a ../../sysroot/lib/
cp -r include ../../sysroot/usr/
+ cp crt0.o ~/prj/osdev/sysroot/lib/
+ cp libc.a ~/prj/osdev/sysroot/lib/
+ cp -r include ~/prj/osdev/sysroot/usr/
clean:
rm libc.a *.o */*.o */*/*.o
diff --git a/userland/libc/include/socket.h b/userland/libc/include/socket.h
index 5e86b45..c4a3e73 100644
--- a/userland/libc/include/socket.h
+++ b/userland/libc/include/socket.h
@@ -1,9 +1,14 @@
+#ifndef SOCKET_H
+#define SOCKET_H
#include <stddef.h>
#include <stdint.h>
#define AF_UNIX 0
+#define AF_INET 1
#define AF_LOCAL AF_UNIX
+#define SOCK_DGRAM 0
+
#define INADDR_ANY 0
typedef struct {
@@ -24,13 +29,21 @@ typedef struct {
typedef uint32_t in_addr_t;
typedef uint16_t in_port_t;
typedef unsigned int sa_family_t;
-typedef uint32_t socklen_t;
+typedef int socklen_t;
struct sockaddr {
sa_family_t sa_family; /* Address family */
char *sa_data; /* Socket address */
};
+struct sockaddr_in {
+ sa_family_t sin_family;
+ union {
+ uint32_t s_addr;
+ } sin_addr;
+ uint16_t sin_port;
+};
+
struct sockaddr_un {
sa_family_t sun_family; /* Address family */
char *sun_path; /* Socket pathname */
@@ -39,3 +52,4 @@ struct sockaddr_un {
int socket(int domain, int type, int protocol);
int accept(int socket, struct sockaddr *address, socklen_t *address_len);
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
+#endif // SOCKET_H
diff --git a/userland/libc/include/sys/socket.h b/userland/libc/include/sys/socket.h
index e69de29..18402d0 100644
--- a/userland/libc/include/sys/socket.h
+++ b/userland/libc/include/sys/socket.h
@@ -0,0 +1,7 @@
+#include <socket.h>
+#include <stddef.h>
+
+#define MSG_WAITALL 1
+
+size_t recvfrom(int socket, void *buffer, size_t length, int flags,
+ struct sockaddr *address, socklen_t *address_len);
diff --git a/userland/libc/include/syscall.h b/userland/libc/include/syscall.h
index b3863d2..7d84b8f 100644
--- a/userland/libc/include/syscall.h
+++ b/userland/libc/include/syscall.h
@@ -32,6 +32,7 @@
#define SYS_MSLEEP 24
#define SYS_UPTIME 25
#define SYS_MKDIR 26
+#define SYS_RECVFROM 27
int syscall(uint32_t eax, uint32_t ebx, uint32_t ecx, uint32_t edx,
uint32_t esi, uint32_t edi);