From 839a9de688f0a3c09ab8ef00337ca03c32c44796 Mon Sep 17 00:00:00 2001 From: Jonas Gunz Date: Sun, 19 Sep 2021 23:15:27 +0200 Subject: add fqdn_check() --- src/dns.c | 29 +++++++++++++++++++++++++++++ src/dns.h | 6 ++++++ tests/dns.c | 5 ++++- 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/dns.c b/src/dns.c index 50db12c..647c1b2 100644 --- a/src/dns.c +++ b/src/dns.c @@ -228,6 +228,35 @@ int fqdn_to_qname( char* _source, int _sourcelen, char* _sink ,int _sinklen ) return i+2; } +int fqdn_check ( char* _source, int _sourcelen ) { + int i; + char c; + + if ( !_source || _sourcelen <= 0 ) + return -1; + + for( i=0; i<_sourcelen; i++ ) { + c = _source[i]; + if (!c) + break; + if ( + !(c>='0' && c<='9') && + !(c>='A' && c<='Z') && + !(c>='a' && c<='z') && + !(c== '-') && !(c=='_') && !(c=='.') + ) { + return i+1; + } + } + + if ( _source[ i-1 ] != '.' ) + return i; + if ( _source[ 0 ] == '-' ) + return 1; + + return 0; +} + int qname_to_fqdn( char* _source, int _sourcelen, char* _sink, int _sinklen ) { unsigned int next_dot; diff --git a/src/dns.h b/src/dns.h index 7357bf0..7d51b0e 100644 --- a/src/dns.h +++ b/src/dns.h @@ -197,6 +197,12 @@ int dns_parse_packet ( char* _buffer, int _bufflen, dns_message_t* _msg ); * */ int fqdn_to_qname( char* _source, int _sourcelen, char* _sink, int _sinklen ); +/** + * Check FQDN validity + * Returns: 0 when valid, <0 for string errors, position > 0 of invalid character + */ +int fqdn_check ( char* _source, int _sourcelen ); + /** * Convert a QNAME back to a FQDN, reversing fqdn_to_qname( ) * returns: length of string in _sink, < 0 on failure diff --git a/tests/dns.c b/tests/dns.c index a282b8b..49310ba 100644 --- a/tests/dns.c +++ b/tests/dns.c @@ -23,7 +23,8 @@ START_TEST (dns_qname) { /* Check for working invalid protection */ ck_assert_int_gt( fqdn_to_qname (inval_fqdn,strlen(inval_fqdn),out,128), 0 ); - ck_assert_int_lt( qname_check(out,128), 0 ); + ck_assert_int_ne( qname_check(out,128), 0 ); + ck_assert_int_ne( fqdn_check(inval_fqdn,strlen(inval_fqdn)), 0 ); } END_TEST START_TEST (dns_qname_fuzz) { @@ -44,6 +45,7 @@ START_TEST (dns_qname_fuzz) { } } + /* Do not allow more than 10% false-positives */ ck_assert_float_le( (float)valid_cnt / (float)limit * 100, 10); } END_TEST @@ -69,6 +71,7 @@ START_TEST (dns_message_fuzz) { } } + /* Do not allow more than 10% false-positives */ ck_assert_float_le( (float)valid_cnt / (float)limit * 100, 10); } END_TEST -- cgit v1.2.3