From 73ede020c76126e81325fcd8d3e186132db21198 Mon Sep 17 00:00:00 2001 From: Jonas Gunz Date: Sat, 2 Nov 2019 16:38:44 +0100 Subject: Added fqdn to qname --- src/dns.c | 40 ++++++++++++++++++++++++++++++++++++++++ src/dns.h | 17 +++++++++++++++++ src/main.c | 29 ++++++++++++++++++++++++++++- 3 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 src/dns.c create mode 100644 src/dns.h (limited to 'src') diff --git a/src/dns.c b/src/dns.c new file mode 100644 index 0000000..5045e2a --- /dev/null +++ b/src/dns.c @@ -0,0 +1,40 @@ +#include "dns.h" + +int fqdn_to_qname( char* _source, int _sourcelen, char* _sink ,int _sinklen ) +{ + // TODO Opttimize + int i = 0; + int lastdot = 0; + + if (_sourcelen < 1 || _sinklen < 1) + return -1; + + _sink[0] = ' '; //Set to known value + + while ( (i < _sourcelen) && (i < (_sinklen - 1))) { //Copy offset 1 + if(! _source[i]) + break; + _sink[i+1] = _source[i]; + i++; + } + + if( _source[i] ) // _source not terminated + return -1; + + for (int o = 0; o < i; o++) { + if( _sink[o] == '.') { + _sink[lastdot] = o - lastdot - 1; + lastdot = o; + } + } + + _sink[lastdot] = i - lastdot; + _sink[i + 1] = 0; + + return i+2; +} + +int qname_to_fqdn( char* _source, int _sourcelen, char* _sink, int _sinklen ) +{ + return -1; +} diff --git a/src/dns.h b/src/dns.h new file mode 100644 index 0000000..9f66367 --- /dev/null +++ b/src/dns.h @@ -0,0 +1,17 @@ +#pragma once + +#include + +/** + * Convert a null terminated string containing a + * fqdn (eg server.example.com) to the binary format used by DNS records + * ( [6]server[7]example[3]com[0] ) + * returns: length of string in _sink, < 0 on failure + * _sink might not be terminated on error. + * */ +int fqdn_to_qname( char* _source, int _sourcelen, char* _sink, int _sinklen ); + +/** + * Opposite of fqdn_to_qname() + * */ +int qname_to_fqdn( char* _source, int _sourcelen, char* _sink, int _sinklen ); diff --git a/src/main.c b/src/main.c index 1dcd0f4..120637d 100644 --- a/src/main.c +++ b/src/main.c @@ -28,8 +28,35 @@ void signal_term ( ); void signal_term_child ( ); +int test_main( int argc, + char* argv[] ) +{ + printf("TEST MODE. NOT FUNCTIONAL\n"); + + char in[128]; + char out[128]; + + strncpy ( in, "www.example.com\0", 127); + + printf("%s\n", in); + + int written = fqdn_to_qname (in,128,out,128); + + if (written < 0) { + printf("invallid fqdn\n"); + return 1; + } + + for(int i = 0; i < written; i++) + printf(" %x ", out[i]); + + printf("\n\n"); + return 0; +} + int main( int argc, - char* argv[] ) { + char* argv[] ) +{ int ret; struct sockaddr_in sock_server_addr; -- cgit v1.2.3