diff options
author | Jonas Gunz <himself@jonasgunz.de> | 2021-06-01 19:34:19 +0200 |
---|---|---|
committer | Jonas Gunz <himself@jonasgunz.de> | 2021-06-01 19:34:19 +0200 |
commit | f9ecb7e5b111193db0f5d714e748762ee2b8be1b (patch) | |
tree | 80f35a359b08785f772ae9da6687f8b651da8370 /src/dns.c | |
parent | 21369ab14763faec0abeb4e669cb0db62121b6fd (diff) | |
download | dns-f9ecb7e5b111193db0f5d714e748762ee2b8be1b.tar.gz |
C89 Compat
Diffstat (limited to 'src/dns.c')
-rw-r--r-- | src/dns.c | 76 |
1 files changed, 41 insertions, 35 deletions
@@ -10,7 +10,7 @@ int dns_construct_header ( char* _buffer, int _bufflen, dns_header_t* _header ) if ( !_buffer || !_header || _bufflen < 12 ) return -1; - *((uint16_t*)_buffer) = _header->id; //Since only copied, no flipping necessary + *((uint16_t*)_buffer) = _header->id; /* Since only copied, no flipping necessary */ _buffer[2] = ((_header->QR & 0x01) << 7) | ((_header->OPCODE & 0x0F) << 3) | @@ -39,7 +39,7 @@ int dns_construct_answer ( if ( !_buffer || _bufflen <= 0 || !_answer ) return -1; - // Check buffer size + /* Check buffer size */ if ( _answer->qname_len + _answer->rdlength + 10 > _bufflen ) return -1; @@ -64,13 +64,13 @@ int dns_construct_questoin ( int _bufflen, dns_question_t* _question ) { - //TODO Test + /* TODO Test */ int ret = 0; if ( !_buffer || _bufflen <= 0 || !_question ) return -1; - // Check buffer size + /* Check buffer size */ if ( _question->qname_len + 4 > _bufflen ) return -1; @@ -85,7 +85,7 @@ int dns_construct_questoin ( } -// Question and answer count come from header +/* Question and answer count come from header */ int dns_construct_packet ( char* _buffer, int _bufflen, @@ -115,15 +115,16 @@ int dns_destroy_struct ( dns_message_t* _msg ) int dns_parse_packet ( char* _buffer, int _bufflen, dns_message_t* _msg ) { - //TODO refactor + int i = 0; + /* TODO refactor */ if ( !_buffer || !_bufflen || !_msg ) - return 1; //Invalid input + return 1; /* Invalid input */ if ( _bufflen < 12 ) - return 1; //Too short to contain a DNS header + return 1; /* Too short to contain a DNS header */ - //TODO test + /* TODO test */ _msg->header.id = *( (uint16_t*) _buffer ); _msg->header.QR = ( 0x80 & *( (uint8_t*) (_buffer + 2)) ) >> 7; _msg->header.OPCODE = (0x78 & *( (uint8_t*) (_buffer + 2))) >> 3; @@ -138,14 +139,14 @@ int dns_parse_packet ( char* _buffer, int _bufflen, dns_message_t* _msg ) _msg->header.authorative_count = (*((uint8_t*) (_buffer + 8 )) << 8) | *((uint8_t*) (_buffer + 9 )); _msg->header.additional_count = (*((uint8_t*) (_buffer + 10)) << 8) | *((uint8_t*) (_buffer + 11)); - //TODO remove + /* TODO remove */ /*printf("ANSWER %i\n", _msg->header.answer_count); printf("QUESTI %i\n", _msg->header.question_count); printf("AUTHOR %i\n", _msg->header.authorative_count); printf("ADDITI %i\n", _msg->header.additional_count); */ - //Check for sensible QD, AN, NS and ARCOUNTS before massive memory allocation + /* Check for sensible QD, AN, NS and ARCOUNTS before massive memory allocation */ if( _msg->header.question_count > 4 || _msg->header.answer_count > 32 || _msg->header.authorative_count > 32 || @@ -153,30 +154,32 @@ int dns_parse_packet ( char* _buffer, int _bufflen, dns_message_t* _msg ) return 1; } - //Allocate question array - //TODO Only implements question section. - size_t qsize = sizeof(typeof(*(_msg->question))) * _msg->question_count; + /* + * Allocate question array + * TODO Only implements question section. + */ + size_t qsize = sizeof(*(_msg->question)) * _msg->question_count; _msg->question_count = _msg->header.question_count; _msg->question = malloc ( qsize ); memset( _msg->question, 0, qsize ); - if (!_msg->question) //malloc failed + if (!_msg->question) /* malloc failed */ return 1; - int ptr = 12; //byte counter + int ptr = 12; /* byte counter */ - // TODO refactor - for (int i = 0; i < _msg->question_count; i++) { + /* TODO refactor */ + for ( i = 0; i < _msg->question_count; i++ ) { int qname_len = qname_check ( (_buffer + ptr), _bufflen - ptr); - if (qname_len <= 0) //Check for faulty QNAME + if (qname_len <= 0) /* Check for faulty QNAME */ return 1; _msg->question[i].qname = _buffer + ptr; _msg->question[i].qname_len = qname_len; ptr += qname_len; - if( ptr >= (_bufflen - 4) ) //Out of bounds check + if( ptr >= (_bufflen - 4) ) /* Out of bounds check */ return 1; _msg->question[i].qtype = ((uint8_t)*(_buffer + ptr) << 8) | ((uint8_t)*(_buffer + ptr + 1)); @@ -191,24 +194,24 @@ int dns_parse_packet ( char* _buffer, int _bufflen, dns_message_t* _msg ) int fqdn_to_qname( char* _source, int _sourcelen, char* _sink ,int _sinklen ) { - int i; + int i, o; int lastdot = 0; if (_sourcelen < 1 || _sinklen < 1) return -1; - _sink[0] = ' '; //Set to known value + _sink[0] = ' '; /* Set to known value */ - for(i = 0; ((i < _sourcelen) && (i < (_sinklen - 1))); i++) { //Copy offset 1 + for(i = 0; ((i < _sourcelen) && (i < (_sinklen - 1))); i++) { /* Copy offset 1 */ if(! _source[i]) break; _sink[i+1] = _source[i]; } - if( _source[i] ) // _source not terminated, thus no valid string + if( _source[i] ) /* _source not terminated, thus no valid string */ return -1; - for (int o = 0; o < i; o++) { + for (o = 0; o < i; o++) { if( _sink[o] == '.') { _sink[lastdot] = o - lastdot - 1; lastdot = o; @@ -223,14 +226,15 @@ int fqdn_to_qname( char* _source, int _sourcelen, char* _sink ,int _sinklen ) int qname_to_fqdn( char* _source, int _sourcelen, char* _sink, int _sinklen ) { + unsigned int next_dot = _source[0] + 1; + int i = 1; + if ( !_sourcelen || !_sinklen ) { return -1; } - unsigned int next_dot = _source[0] + 1; - int i = 1; for(i = 1; i < _sourcelen; i++) { - if( i > _sinklen){ //Output too small. Not >= bc sink[i-1] is used + if( i > _sinklen){ /* Output too small. Not >= bc sink[i-1] is used */ return -1; } if ( !_source[i] ) { @@ -248,21 +252,23 @@ int qname_to_fqdn( char* _source, int _sourcelen, char* _sink, int _sinklen ) int qname_check( char* _source, int _sourcelen ) { + int next_dot = 0; + int i = 0; + if (!_sourcelen) return -1; - int next_dot = 0; - //TODO questionable control flow - //TODO add ASCII prrintable check - for (int i = 0; i < _sourcelen; i++) { + /* TODO questionable control flow */ + /* TODO add ASCII prrintable check */ + for (i = 0; i < _sourcelen; i++) { if ( i == next_dot ) { - if (_source[i]) { //Not last dot + if (_source[i]) { /* Not last dot */ next_dot = _source[i] + i + 1; - } else { //last dot + } else { /* last dot */ return i+1; } - } else if (!_source[i]) { //Unexpected \0 + } else if (!_source[i]) { /* Unexpected \0 */ return -1; } } |