diff options
Diffstat (limited to 'libnet')
-rw-r--r-- | libnet/dns.c | 35 | ||||
-rw-r--r-- | libnet/inc/net.h | 23 |
2 files changed, 52 insertions, 6 deletions
diff --git a/libnet/dns.c b/libnet/dns.c index b35eb72..fc6b06f 100644 --- a/libnet/dns.c +++ b/libnet/dns.c @@ -1,7 +1,11 @@ // MIT License, Copyright (c) 2020 Marvin Borner +// TODO: Less magic, auto xld splitting +// TODO: DNS cache #include <def.h> #include <mem.h> +#include <net.h> +#include <print.h> #include <random.h> #include <socket.h> #include <str.h> @@ -36,13 +40,29 @@ static void dns_make_packet(struct dns_packet *packet, const char *name, const c packet->data[(u8)strlen(name) + (u8)strlen(tld) + 6] = 0x01; // IN } -void dns_request(const char *name, const char *tld) +static u32 dns_handle_packet(struct dns_packet *packet) +{ + u16 flags = htons(packet->flags); + u8 reply_code = flags & 0xf; + if (reply_code != DNS_NOERROR) { + printf("DNS error: %d\n", reply_code); + return 0; + } + + u8 *start = &packet->data[1] + strlen((char *)&packet->data[1]); + printf("TTL of %s: %ds\n", &packet->data[1], (u32)start[14]); + u8 *ip = &start[17]; + printf("IP: %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]); + return ip(ip[0], ip[1], ip[2], ip[3]); +} + +u32 dns_request(const char *name, const char *tld) { struct socket *socket = net_open(S_UDP); - if (socket) - socket->src_port = 50053; + /* if (socket) */ + /* socket->src_port = 50053; */ if (!socket || !net_connect(socket, dns_ip_addr, 53)) - return; + return 0; u32 length = sizeof(struct dns_packet) + strlen(name) + strlen(tld) + 7; // TODO: 7 :) struct dns_packet *packet = malloc(length); @@ -50,4 +70,11 @@ void dns_request(const char *name, const char *tld) dns_make_packet(packet, name, tld); net_send(socket, packet, length); free(packet); + + u8 buf[128] = { 0 }; + int l = net_receive(socket, buf, 128); + if (l > 0) + return dns_handle_packet((void *)buf); + else + return 0; } diff --git a/libnet/inc/net.h b/libnet/inc/net.h index 836e181..fa4284c 100644 --- a/libnet/inc/net.h +++ b/libnet/inc/net.h @@ -4,6 +4,18 @@ #define NET_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) | \ @@ -15,11 +27,18 @@ ((((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_close(socket) (void)sys1(SYS_NET_CLOSE, (int)(socket)) #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)) +static inline int net_receive(struct socket *socket, void *buf, u32 len) +{ + int res = 0; + while ((res = (int)sys3(SYS_NET_RECEIVE, (int)(socket), (int)(buf), (int)(len))) == 0) + ; + return res; +} -void dns_request(const char *name, const char *tld); +u32 dns_request(const char *name, const char *tld); #endif |