dinoco

Query DNS records
git clone git://git.relim.de/dinoco.git
Log | Files | Refs | README | LICENSE

commit aa50e49e58d3cdd90d9470eeab6e74fcfc0a1436
parent 3cb54c83c2317f5b82063ab5f71dda1b46695571
Author: Nibo <kroekerrobin@gmail.com>
Date:   Sun, 25 Jun 2023 23:02:51 +0200

Refactor: parseIPv4(), parseIPv6()

Diffstat:
Mdinoco.c | 98+++++++++++++++++++++++++++++++++++++++++--------------------------------------
1 file changed, 51 insertions(+), 47 deletions(-)

diff --git a/dinoco.c b/dinoco.c @@ -149,13 +149,61 @@ struct byte_array *formDomain(char *domain) return b; } +char *parseIPv4(char *data) +{ + char *ipv4 = NULL; + int e = 0; + uint8_t ipPart = 0; + for (int i=0; i<4; i++) + { + ipPart = data[i] & 0xFF; + char number[4]; + sprintf(number, "%d", ipPart); + number[3] = 0; + for (int k=0; k<strlen(number); k++) + { + ipv4 = realloc(ipv4, (e+1) * sizeof(char)); + ipv4[e] = number[k]; + e++; + } + if (i != 3) + { + ipv4 = realloc(ipv4, (e+1) * sizeof(char)); + ipv4[e] = '.'; + e++; + } + } + ipv4 = realloc(ipv4, (e+1) * sizeof(char)); + ipv4[e] = 0; + return ipv4; +} + char *parseIPv6(char *data) { char *ipv6 = NULL; + char ipv6Part[3]; + int s = 0; for (int i=0; i<16; i++) { - data[i]; + uint8_t n = data[i] & 0xFF; + sprintf(ipv6Part, "%02x", n); + ipv6Part[2] = 0; + for (int k=0; k<strlen(ipv6Part); k++) + { + ipv6 = realloc(ipv6, (s+1) * sizeof(char)); + ipv6[s] = ipv6Part[k]; + s++; + } + if (i%2 != 0 && i != 15) + { + ipv6 = realloc(ipv6, (s+1) * sizeof(char)); + ipv6[s] = ':'; + s++; + } } + ipv6 = realloc(ipv6, (s+1) * sizeof(char)); + ipv6[s] = 0; + return ipv6; } char *parseCharString(char *data, int start) @@ -380,55 +428,11 @@ union dns_type_result *parseAnswerByType { case TYPE_A: ur->a = malloc(sizeof(struct dns_a_result)); - ur->a->ipAddress = NULL; - int e = 0; - uint8_t ipPart = 0; - for (int i=0; i<answer->rdlength; i++) - { - ipPart = answer->rdata->bytes[i] & 0xFF; - char number[4]; - sprintf(number, "%d", ipPart); - number[3] = 0; - for (int k=0; k<strlen(number); k++) - { - ur->a->ipAddress = realloc(ur->a->ipAddress, (e+1) * sizeof(char)); - ur->a->ipAddress[e] = number[k]; - e++; - } - if (i != 3) - { - ur->a->ipAddress = realloc(ur->a->ipAddress, (e+1) * sizeof(char)); - ur->a->ipAddress[e] = '.'; - e++; - } - } - ur->a->ipAddress = realloc(ur->a->ipAddress, (e+1) * sizeof(char)); - ur->a->ipAddress[e] = 0; + ur->a->ipAddress = parseIPv4(answer->rdata->bytes); break; case TYPE_AAAA: ur->aaaa = malloc(sizeof(struct dns_aaaa_result)); - ur->aaaa->ipv6 = NULL; - char ipv6Part[3]; - int s = 0; - for (int i=0; i<16; i++) - { - sprintf(ipv6Part, "%02x", answer->rdata->bytes[i] & 0xFF); - ipv6Part[2] = 0; - for (int k=0; k<strlen(ipv6Part); k++) - { - ur->aaaa->ipv6 = realloc(ur->aaaa->ipv6, (s+1) * sizeof(char)); - ur->aaaa->ipv6[s] = ipv6Part[k]; - s++; - } - if (i%2 != 0 && i != 15) - { - ur->aaaa->ipv6 = realloc(ur->aaaa->ipv6, (s+1) * sizeof(char)); - ur->aaaa->ipv6[s] = ':'; - s++; - } - } - ur->aaaa->ipv6 = realloc(ur->aaaa->ipv6, (s+1) * sizeof(char)); - ur->aaaa->ipv6[s] = 0; + ur->aaaa->ipv6 = parseIPv6(answer->rdata->bytes); break; case TYPE_NS: ur->ns = malloc(sizeof(struct dns_ns_result));