aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2020-11-18 13:49:19 +0100
committerMarvin Borner2020-11-18 13:49:19 +0100
commit431c88102153b8b41a15a1105e291ecf161c030e (patch)
tree63063babd988284871db7c27dbb96025d00f2444
parentcc85ffd8a1c4703051655c7f5727157e7e1ce96a (diff)
Started libnet
-rw-r--r--Makefile2
-rw-r--r--apps/Makefile4
-rw-r--r--apps/init.c10
-rw-r--r--kernel/features/net.c55
-rw-r--r--kernel/features/syscall.c17
-rw-r--r--kernel/inc/net.h26
-rw-r--r--libc/inc/socket.h33
-rw-r--r--libc/inc/sys.h6
-rw-r--r--libnet/Makefile21
-rw-r--r--libnet/dns.c53
-rw-r--r--libnet/inc/net.h25
-rwxr-xr-xrun3
12 files changed, 178 insertions, 77 deletions
diff --git a/Makefile b/Makefile
index 92892b2..4c67f3e 100644
--- a/Makefile
+++ b/Makefile
@@ -27,6 +27,8 @@ compile:
@echo "Compiled libgui"
@$(MAKE) --no-print-directory -C libtxt/
@echo "Compiled libtxt"
+ @$(MAKE) --no-print-directory -C libnet/
+ @echo "Compiled libnet"
@$(MAKE) --no-print-directory -C kernel/
@echo "Compiled kernel"
@$(MAKE) --no-print-directory -C boot/
diff --git a/apps/Makefile b/apps/Makefile
index 322b7b6..fd7cc99 100644
--- a/apps/Makefile
+++ b/apps/Makefile
@@ -6,14 +6,14 @@ LD = ccache ../cross/opt/bin/i686-elf-ld
OC = ccache ../cross/opt/bin/i686-elf-objcopy
WARNINGS = -Wall -Wextra -pedantic-errors -Wshadow -Wpointer-arith -Wwrite-strings -Wredundant-decls -Wnested-externs -Wno-long-long
-CFLAGS = $(WARNINGS) -nostdlib -nostdinc -fno-builtin -std=c99 -m32 -I../libc/inc/ -I../libgui/inc/ -I../libtxt/inc/ -fPIE -Duserspace -Ofast
+CFLAGS = $(WARNINGS) -nostdlib -nostdinc -fno-builtin -std=c99 -m32 -I../libc/inc/ -I../libgui/inc/ -I../libtxt/inc/ -I../libnet/inc/ -fPIE -Duserspace -Ofast
all: $(COBJS)
%.o: %.c
@mkdir -p ../build/apps/
@$(CC) -c $(CFLAGS) $< -o $@
- @$(LD) -o $(@:.o=.elf) -Tlink.ld -L../build/ $@ -lgui -ltxt -lc
+ @$(LD) -o $(@:.o=.elf) -Tlink.ld -L../build/ $@ -lgui -ltxt -lnet -lc
@$(OC) -O binary $(@:.o=.elf) ../build/apps/$(@:.o=)
# @cp $(@:.o=.elf) ../build/apps/$(@:.o=.dbg)
diff --git a/apps/init.c b/apps/init.c
index 9d64388..234853f 100644
--- a/apps/init.c
+++ b/apps/init.c
@@ -1,7 +1,9 @@
// MIT License, Copyright (c) 2020 Marvin Borner
#include <def.h>
+#include <net.h>
#include <print.h>
+#include <str.h>
#include <sys.h>
int main(int argc, char **argv)
@@ -13,5 +15,13 @@ int main(int argc, char **argv)
int wm = exec("/bin/wm", "wm", argv[1], NULL);
int exec = exec("/bin/exec", "test", NULL);
+ /* #define http_req "GET / HTTP/1.1\r\nHost: google.de\r\n\r\n" */
+ /* struct socket *socket = net_open(S_TCP); */
+ /* if (socket && net_connect(socket, ip(91, 89, 253, 227), 80)) */
+ /* net_send(socket, strdup(http_req), strlen(http_req)); */
+ /* else */
+ /* print("Couldn't connect!\n"); */
+ dns_request("google", "de");
+
return wm + exec;
}
diff --git a/kernel/features/net.c b/kernel/features/net.c
index 679f06a..01e8721 100644
--- a/kernel/features/net.c
+++ b/kernel/features/net.c
@@ -10,10 +10,10 @@
#include <print.h>
#include <random.h>
#include <rtl8139.h>
+#include <socket.h>
#include <str.h>
#include <timer.h>
-static u32 dns_ip_addr = ip(1, 1, 1, 1);
static u32 current_ip_addr = 0;
static u32 gateway_addr = 0;
static u32 subnet_mask = 0;
@@ -356,24 +356,6 @@ static void dhcp_request(void)
free(packet);
}
-// TODO: Split name into tld etc automagically
-static void dns_make_packet(struct dns_packet *packet, const char *name, const char *tld);
-static void dns_request(const char *name, const char *tld)
-{
- struct socket *socket = net_open(S_UDP);
- if (socket)
- socket->src_port = DNS_PORT;
- if (!socket || !net_connect(socket, dns_ip_addr, 53))
- return;
-
- u32 length = sizeof(struct dns_packet) + strlen(name) + strlen(tld) + 7; // TODO: 7 :)
- struct dns_packet *packet = malloc(length);
- memset(packet, 0, length);
- dns_make_packet(packet, name, tld);
- net_send(socket, packet, length);
- free(packet);
-}
-
/**
* Responses
*/
@@ -698,29 +680,6 @@ static int dhcp_discover(void)
}
/**
- * DNS
- */
-
-// TODO: Cleaner dns implementation
-static void dns_make_packet(struct dns_packet *packet, const char *name, const char *tld)
-{
- packet->qid = htons(rand());
- packet->flags = htons(0x0100); // Standard query
- packet->questions = htons(1);
- packet->answers = htons(0);
- packet->authorities = htons(0);
- packet->additional = htons(0);
-
- packet->data[0] = (u8)strlen(name);
- memcpy(&packet->data[1], name, (u8)strlen(name));
- packet->data[(u8)strlen(name) + 1] = (u8)strlen(tld);
- memcpy(&packet->data[(u8)strlen(name) + 2], tld, (u8)strlen(tld));
- packet->data[(u8)strlen(name) + (u8)strlen(tld) + 2] = 0x00; // Name end
- packet->data[(u8)strlen(name) + (u8)strlen(tld) + 4] = 0x01; // A
- packet->data[(u8)strlen(name) + (u8)strlen(tld) + 6] = 0x01; // IN
-}
-
-/**
* ARP
*/
@@ -861,13 +820,13 @@ void net_install(void)
}
// Request
- dns_request("google", "de");
+ /* dns_request("google", "de"); */
- struct socket *socket = net_open(S_TCP);
- if (socket && net_connect(socket, ip(91, 89, 253, 227), 80))
- net_send(socket, strdup(http_req), strlen(http_req));
- else
- print("Couldn't connect!\n");
+ /* struct socket *socket = net_open(S_TCP); */
+ /* if (socket && net_connect(socket, ip(91, 89, 253, 227), 80)) */
+ /* net_send(socket, strdup(http_req), strlen(http_req)); */
+ /* else */
+ /* print("Couldn't connect!\n"); */
// Server // TODO: Serve using sockets
/* struct socket *socket2 = net_open(S_TCP); */
diff --git a/kernel/features/syscall.c b/kernel/features/syscall.c
index 63badd9..b601d96 100644
--- a/kernel/features/syscall.c
+++ b/kernel/features/syscall.c
@@ -6,6 +6,7 @@
#include <interrupts.h>
#include <load.h>
#include <mem.h>
+#include <net.h>
#include <print.h>
#include <proc.h>
#include <str.h>
@@ -91,6 +92,22 @@ void syscall_handler(struct regs *r)
r->eax = proc_current()->pid;
break;
}
+ case SYS_NET_OPEN: {
+ r->eax = (int)net_open(r->ebx);
+ break;
+ }
+ case SYS_NET_CLOSE: {
+ net_close((void *)r->ebx);
+ break;
+ }
+ case SYS_NET_CONNECT: {
+ r->eax = net_connect((void *)r->ebx, r->ecx, r->edx);
+ break;
+ }
+ case SYS_NET_SEND: {
+ net_send((void *)r->ebx, (void *)r->ecx, r->edx);
+ break;
+ }
default: {
print("Unknown syscall!\n");
break;
diff --git a/kernel/inc/net.h b/kernel/inc/net.h
index 1111f22..5630e97 100644
--- a/kernel/inc/net.h
+++ b/kernel/inc/net.h
@@ -4,6 +4,7 @@
#define NET_H
#include <def.h>
+#include <socket.h>
#define htonl(l) \
((((l)&0xff) << 24) | (((l)&0xff00) << 8) | (((l)&0xff0000) >> 8) | \
@@ -164,31 +165,6 @@ struct arp_table_entry {
u64 mac_addr;
};
-struct tcp_socket {
- u32 seq_no;
- u32 ack_no;
- u32 state;
-};
-
-// TODO: Use actual socket types (stream etc)
-enum socket_type { S_TCP, S_UDP };
-enum socket_state { S_CONNECTING, S_CONNECTED, S_OPEN, S_CLOSED, S_FAILED };
-
-struct socket {
- u32 ip_addr;
- u32 dst_port;
- u32 src_port;
- enum socket_state state;
- enum socket_type type;
- u32 bytes_available;
- u32 bytes_read;
- void *current_packet;
- union {
- struct tcp_socket tcp;
- /* struct udp_socket udp; */
- } prot;
-};
-
void ethernet_handle_packet(struct ethernet_packet *packet, int len);
struct socket *net_open(enum socket_type type);
diff --git a/libc/inc/socket.h b/libc/inc/socket.h
new file mode 100644
index 0000000..651bba0
--- /dev/null
+++ b/libc/inc/socket.h
@@ -0,0 +1,33 @@
+// MIT License, Copyright (c) 2020 Marvin Borner
+
+#ifndef SOCKET_H
+#define SOCKET_H
+
+#include <def.h>
+
+// TODO: Use actual socket types (stream etc)
+enum socket_type { S_TCP, S_UDP };
+enum socket_state { S_CONNECTING, S_CONNECTED, S_OPEN, S_CLOSED, S_FAILED };
+
+struct tcp_socket {
+ u32 seq_no;
+ u32 ack_no;
+ u32 state;
+};
+
+struct socket {
+ u32 ip_addr;
+ u32 dst_port;
+ u32 src_port;
+ enum socket_state state;
+ enum socket_type type;
+ u32 bytes_available;
+ u32 bytes_read;
+ void *current_packet;
+ union {
+ struct tcp_socket tcp;
+ /* struct udp_socket udp; */
+ } prot;
+};
+
+#endif
diff --git a/libc/inc/sys.h b/libc/inc/sys.h
index 86f530b..4e52ac1 100644
--- a/libc/inc/sys.h
+++ b/libc/inc/sys.h
@@ -21,7 +21,11 @@ enum sys {
SYS_UNREGISTER, // Unregister event
SYS_SEND, // Send message to process
SYS_RECEIVE, // Receive message (non-blocking/sync)
- SYS_GETPID // Get the process ID
+ SYS_GETPID, // Get the process ID
+ SYS_NET_OPEN, // Open network socket
+ SYS_NET_CLOSE, // Close network socket
+ SYS_NET_CONNECT, // Connect to destination
+ SYS_NET_SEND, // Send to socket
};
enum event_type { EVENT_KEYBOARD, EVENT_MOUSE, EVENT_MAX };
diff --git a/libnet/Makefile b/libnet/Makefile
new file mode 100644
index 0000000..507a564
--- /dev/null
+++ b/libnet/Makefile
@@ -0,0 +1,21 @@
+# MIT License, Copyright (c) 2020 Marvin Borner
+
+COBJS = dns.o
+CC = ccache ../cross/opt/bin/i686-elf-gcc
+LD = ccache ../cross/opt/bin/i686-elf-ld
+AR = ccache ../cross/opt/bin/i686-elf-ar
+
+WARNINGS = -Wall -Wextra -pedantic-errors -Wshadow -Wpointer-arith -Wwrite-strings -Wredundant-decls -Wnested-externs -Wno-long-long
+CFLAGS = $(WARNINGS) -nostdlib -nostdinc -fno-builtin -mgeneral-regs-only -std=c99 -m32 -Iinc/ -I../libc/inc/ -fPIE -Duserspace -Ofast
+
+all: libtxt
+
+%.o: %.c
+ @$(CC) -c $(CFLAGS) $< -o $@
+
+libtxt: $(COBJS)
+ @mkdir -p ../build/
+ @$(AR) rcs ../build/libnet.a $+
+
+clean:
+ @find . -name "*.o" -type f -delete
diff --git a/libnet/dns.c b/libnet/dns.c
new file mode 100644
index 0000000..b35eb72
--- /dev/null
+++ b/libnet/dns.c
@@ -0,0 +1,53 @@
+// MIT License, Copyright (c) 2020 Marvin Borner
+
+#include <def.h>
+#include <mem.h>
+#include <random.h>
+#include <socket.h>
+#include <str.h>
+
+static u32 dns_ip_addr = ip(1, 1, 1, 1);
+
+struct dns_packet {
+ u16 qid;
+ u16 flags;
+ u16 questions;
+ u16 answers;
+ u16 authorities;
+ u16 additional;
+ u8 data[];
+} __attribute__((packed));
+
+static void dns_make_packet(struct dns_packet *packet, const char *name, const char *tld)
+{
+ packet->qid = htons(rand());
+ packet->flags = htons(0x0100); // Standard query
+ packet->questions = htons(1);
+ packet->answers = htons(0);
+ packet->authorities = htons(0);
+ packet->additional = htons(0);
+
+ packet->data[0] = (u8)strlen(name);
+ memcpy(&packet->data[1], name, (u8)strlen(name));
+ packet->data[(u8)strlen(name) + 1] = (u8)strlen(tld);
+ memcpy(&packet->data[(u8)strlen(name) + 2], tld, (u8)strlen(tld));
+ packet->data[(u8)strlen(name) + (u8)strlen(tld) + 2] = 0x00; // Name end
+ packet->data[(u8)strlen(name) + (u8)strlen(tld) + 4] = 0x01; // A
+ packet->data[(u8)strlen(name) + (u8)strlen(tld) + 6] = 0x01; // IN
+}
+
+void dns_request(const char *name, const char *tld)
+{
+ struct socket *socket = net_open(S_UDP);
+ if (socket)
+ socket->src_port = 50053;
+ if (!socket || !net_connect(socket, dns_ip_addr, 53))
+ return;
+
+ u32 length = sizeof(struct dns_packet) + strlen(name) + strlen(tld) + 7; // TODO: 7 :)
+ struct dns_packet *packet = malloc(length);
+ memset(packet, 0, length);
+ dns_make_packet(packet, name, tld);
+ net_send(socket, packet, length);
+ free(packet);
+}
diff --git a/libnet/inc/net.h b/libnet/inc/net.h
new file mode 100644
index 0000000..836e181
--- /dev/null
+++ b/libnet/inc/net.h
@@ -0,0 +1,25 @@
+// MIT License, Copyright (c) 2020 Marvin Borner
+
+#ifndef NET_H
+#define NET_H
+
+#include <socket.h>
+
+#define htonl(l) \
+ ((((l)&0xff) << 24) | (((l)&0xff00) << 8) | (((l)&0xff0000) >> 8) | \
+ (((l)&0xff000000) >> 24))
+#define htons(s) ((((s)&0xff) << 8) | (((s)&0xff00) >> 8))
+#define ntohl(l) htonl((l))
+#define ntohs(s) htons((s))
+#define ip(a, b, c, d) \
+ ((((a)&0xff) << 24) | (((b)&0xff) << 16) | (((c)&0xff) << 8) | (((d)&0xff) << 0))
+
+#define net_open(type) (void *)sys1(SYS_NET_OPEN, (int)(type))
+#define net_close(socket) (void)sys1(SYS_NET_CLOSE, (int)(type))
+#define net_connect(socket, ip_addr, dst_port) \
+ (int)sys3(SYS_NET_CONNECT, (int)(socket), (int)(ip_addr), (int)(dst_port))
+#define net_send(socket, data, len) (void)sys3(SYS_NET_SEND, (int)(socket), (int)(data), (int)(len))
+
+void dns_request(const char *name, const char *tld);
+
+#endif
diff --git a/run b/run
index eb81e75..c126974 100755
--- a/run
+++ b/run
@@ -210,7 +210,8 @@ make_sync() {
echo "$output" | make_append_commands libc libk libc
echo "$output" | make_append_commands libk libgui libgui
echo "$output" | make_append_commands libgui libtxt libtxt
- echo "$output" | make_append_commands libtxt kernel kernel
+ echo "$output" | make_append_commands libtxt libnet libnet
+ echo "$output" | make_append_commands libnet kernel kernel
echo "$output" | make_append_commands kernel boot boot
echo "$output" | make_append_commands boot apps apps
tr <compile_commands.json '\n' '\r' | sed -e 's/\r]\r\[/,/g' | tr '\r' '\n' >tmp