From e8aa2eb5787b5074b5b2867cb89653387f7e8d67 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Sat, 21 Nov 2020 13:57:54 +0100 Subject: Added browser IP address support --- apps/browser.c | 12 +++++++++--- libnet/Makefile | 2 +- libnet/inc/ip.h | 12 ++++++++++++ libnet/inc/net.h | 1 + libnet/ip.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 libnet/inc/ip.h create mode 100644 libnet/ip.c diff --git a/apps/browser.c b/apps/browser.c index d03d963..b434c7c 100644 --- a/apps/browser.c +++ b/apps/browser.c @@ -70,13 +70,18 @@ void on_submit(void *event, struct element *box) } char *query = http_query_get(url, path ? path : "/"); - char *dns[2]; - dns_split(url, dns); + u32 ip = 0; + if (!ip_pton(url, &ip)) { + char *dns[2]; + dns_split(url, dns); + ip = dns_request(dns[0], dns[1]); + } + struct element_text_box *l = output->data; struct element_label *c = code_label->data; struct socket *socket = net_open(S_TCP); - if (socket && net_connect(socket, dns_request(dns[0], dns[1]), 80)) { + if (socket && net_connect(socket, ip, 80)) { net_send(socket, query, strlen(query)); char buf[4096] = { 0 }; net_receive(socket, buf, 4096); @@ -86,6 +91,7 @@ void on_submit(void *event, struct element *box) } else { l->text = strdup("Can't connect to server."); c->text = strdup("000"); + c->color_fg = COLOR_RED; } gui_sync(root, output); gui_sync(root, code_label); diff --git a/libnet/Makefile b/libnet/Makefile index 95baa33..43de0e2 100644 --- a/libnet/Makefile +++ b/libnet/Makefile @@ -1,6 +1,6 @@ # MIT License, Copyright (c) 2020 Marvin Borner -COBJS = dns.o http.o +COBJS = dns.o http.o ip.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 diff --git a/libnet/inc/ip.h b/libnet/inc/ip.h new file mode 100644 index 0000000..e06aba2 --- /dev/null +++ b/libnet/inc/ip.h @@ -0,0 +1,12 @@ +// MIT License, Copyright (c) 2020 Marvin Borner +// Most net/ip handlers are in the kernel space +// This is a userspace wrapper for some things + +#ifndef IP_H +#define IP_H + +#include + +int ip_pton(const char *src, u32 *dst); + +#endif diff --git a/libnet/inc/net.h b/libnet/inc/net.h index ac9296e..eb80cf9 100644 --- a/libnet/inc/net.h +++ b/libnet/inc/net.h @@ -4,6 +4,7 @@ #define NET_H #include +#include #include #include diff --git a/libnet/ip.c b/libnet/ip.c new file mode 100644 index 0000000..eb2e202 --- /dev/null +++ b/libnet/ip.c @@ -0,0 +1,49 @@ +// MIT License, Copyright (c) 2020 Marvin Borner +// Most net/ip handlers are in the kernel space +// This is a userspace wrapper for some things + +#include +#include +#include +#include + +// Inspired by Paul Vixie, 1996 +int ip_pton(const char *src, u32 *dst) +{ + const char *end = src + strlen(src); + u8 tmp[4], *tp; + int ch = 0; + int saw_digit = 0; + int octets = 0; + *(tp = tmp) = 0; + + while (src < end) { + ch = *src++; + if (ch >= '0' && ch <= '9') { + u32 new = *tp * 10 + (ch - '0'); + + if ((saw_digit && *tp == 0) || new > 255) + return 0; + + *tp = new; + if (!saw_digit) { + if (++octets > 4) + return 0; + saw_digit = 1; + } + } else if (ch == '.' && saw_digit) { + if (octets == 4) + return 0; + *++tp = 0; + saw_digit = 0; + } else { + return 0; + } + } + + if (octets < 4) + return 0; + + *dst = htonl(*(u32 *)tmp); + return 1; +} -- cgit v1.2.3