aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2020-11-21 21:52:10 +0100
committerMarvin Borner2020-11-21 21:52:10 +0100
commitb05a30248a49913b6d4c09cc2f76c9527d88935a (patch)
tree9e9fa4b40d21015e9004cd5b1c05b2ec7fe41c37
parent9b0fb12b247542defa3b85b0a29344b6e6d6c803 (diff)
Added magic DNS algorithm
Coffein helped me write this lol. It's actually pretty dumb but WTH - it works!
-rw-r--r--apps/browser.c4
-rw-r--r--libnet/dns.c59
-rw-r--r--libnet/inc/dns.h21
-rw-r--r--libnet/inc/net.h15
4 files changed, 71 insertions, 28 deletions
diff --git a/apps/browser.c b/apps/browser.c
index b434c7c..c51b0a4 100644
--- a/apps/browser.c
+++ b/apps/browser.c
@@ -72,9 +72,7 @@ void on_submit(void *event, struct element *box)
u32 ip = 0;
if (!ip_pton(url, &ip)) {
- char *dns[2];
- dns_split(url, dns);
- ip = dns_request(dns[0], dns[1]);
+ ip = dns_request(url);
}
struct element_text_box *l = output->data;
diff --git a/libnet/dns.c b/libnet/dns.c
index 6682831..dc1a5f0 100644
--- a/libnet/dns.c
+++ b/libnet/dns.c
@@ -22,7 +22,40 @@ struct dns_packet {
u8 data[];
} __attribute__((packed));
-static void dns_make_packet(struct dns_packet *packet, const char *name, const char *tld)
+static u32 part_count(const char *name)
+{
+ u32 cnt = 0;
+ for (u32 i = 0; i < strlen(name); i++) {
+ if (name[i] == '.')
+ cnt++;
+ }
+ return cnt + 1;
+}
+
+static u32 part_len(const char *name, u32 index)
+{
+ char *data = (char *)name;
+
+ u32 cnt = 0;
+ for (u32 i = 0; i < strlen(name); i++) {
+ if (cnt == index) {
+ data += i;
+ break;
+ }
+
+ if (name[i] == '.')
+ cnt++;
+ }
+
+ for (cnt = 0; cnt < strlen(data); cnt++) {
+ if (data[cnt] == '.' || data[cnt] == '\0')
+ break;
+ }
+
+ return cnt;
+}
+
+static void dns_make_packet(struct dns_packet *packet, const char *name)
{
packet->qid = htons(rand());
packet->flags = htons(0x0100); // Standard query
@@ -31,13 +64,17 @@ static void dns_make_packet(struct dns_packet *packet, const char *name, const c
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
+ u8 *data = packet->data;
+ u32 cnt = 0;
+ for (u32 i = 0; i < part_count(name) * 2; i += 2) {
+ data[cnt] = part_len(name, i / 2);
+ memcpy(&data[cnt + 1], &name[cnt], data[i]);
+ cnt += data[cnt] + 1;
+ }
+
+ packet->data[cnt + 0] = 0x00; // Name end
+ packet->data[cnt + 2] = 0x01; // A
+ packet->data[cnt + 4] = 0x01; // IN
}
static u32 dns_handle_packet(struct dns_packet *packet)
@@ -56,16 +93,16 @@ static u32 dns_handle_packet(struct dns_packet *packet)
return ip(ip[0], ip[1], ip[2], ip[3]);
}
-u32 dns_request(const char *name, const char *tld)
+u32 dns_request(const char *name)
{
struct socket *socket = net_open(S_UDP);
if (!socket || !net_connect(socket, dns_ip_addr, 53))
return 0;
- u32 length = sizeof(struct dns_packet) + strlen(name) + strlen(tld) + 7; // TODO: 7 :)
+ u32 length = sizeof(struct dns_packet) + strlen(name) + part_count(name) + 4;
struct dns_packet *packet = malloc(length);
memset(packet, 0, length);
- dns_make_packet(packet, name, tld);
+ dns_make_packet(packet, name);
net_send(socket, packet, length);
free(packet);
diff --git a/libnet/inc/dns.h b/libnet/inc/dns.h
new file mode 100644
index 0000000..d6673e6
--- /dev/null
+++ b/libnet/inc/dns.h
@@ -0,0 +1,21 @@
+// MIT License, Copyright (c) 2020 Marvin Borner
+
+#ifndef DNS_H
+#define DNS_H
+
+#include <def.h>
+
+#define DNS_NOERROR 0
+#define DNS_FORMERR 1
+#define DNS_SERVFAIL 2
+#define DNS_NXDOMAIN 3
+#define DNS_NOTIMP 4
+#define DNS_REFUSED 5
+#define DNS_YXDOMAIN 6
+#define DNS_XRRSET 7
+#define DNS_NOTAUTH 8
+#define DNS_NOTZONE 9
+
+u32 dns_request(const char *name);
+
+#endif
diff --git a/libnet/inc/net.h b/libnet/inc/net.h
index eb80cf9..5dd4ee1 100644
--- a/libnet/inc/net.h
+++ b/libnet/inc/net.h
@@ -3,22 +3,12 @@
#ifndef NET_H
#define NET_H
+#include <dns.h>
#include <http.h>
#include <ip.h>
#include <socket.h>
#include <sys.h>
-#define DNS_NOERROR 0
-#define DNS_FORMERR 1
-#define DNS_SERVFAIL 2
-#define DNS_NXDOMAIN 3
-#define DNS_NOTIMP 4
-#define DNS_REFUSED 5
-#define DNS_YXDOMAIN 6
-#define DNS_XRRSET 7
-#define DNS_NOTAUTH 8
-#define DNS_NOTZONE 9
-
#define htonl(l) \
((((l)&0xff) << 24) | (((l)&0xff00) << 8) | (((l)&0xff0000) >> 8) | \
(((l)&0xff000000) >> 24))
@@ -32,7 +22,6 @@
#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))
-#include <print.h>
static inline int net_close(struct socket *socket)
{
int res = 0;
@@ -48,6 +37,4 @@ static inline int net_receive(struct socket *socket, void *buf, u32 len)
return res;
}
-u32 dns_request(const char *name, const char *tld);
-
#endif