diff options
author | B Stack <bgstack15@gmail.com> | 2020-09-03 20:28:56 -0400 |
---|---|---|
committer | B Stack <bgstack15@gmail.com> | 2020-09-03 20:28:56 -0400 |
commit | c99c28d51d9617cc67a3ba959ed50067534b5ab7 (patch) | |
tree | e8d3b042d2dde04ed3f85c1c554b4739d9ce40f7 | |
download | full_resolver-c99c28d51d9617cc67a3ba959ed50067534b5ab7.tar.gz full_resolver-c99c28d51d9617cc67a3ba959ed50067534b5ab7.tar.bz2 full_resolver-c99c28d51d9617cc67a3ba959ed50067534b5ab7.zip |
initial commit
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | Makefile | 52 | ||||
-rw-r--r-- | README.md | 25 | ||||
-rw-r--r-- | full_resolver.c | 34 | ||||
-rw-r--r-- | full_resolver.h | 66 |
5 files changed, 180 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b65c7d8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.o +full_resolver +.*.swp diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b190273 --- /dev/null +++ b/Makefile @@ -0,0 +1,52 @@ +# File: full_resolver/Makefile +# License: CC-BY-SA 4.0 +# Author: bgstack15 +# Startdate: 2020-09-02 +# Title: Makefile for full_resolver +# Purpose: compile the project +# History: +# Usage: +# make DEBUG=1 +# References: +# https://bgstack15.wordpress.com/2019/11/04/sample-makefile/ +# Improve: +# investigate why the .o build section is not used +# Dependencies: +# Documentation: +# includes nice DEBUG macro passing, which is not yet documented in reference 1 as of the document startdate. + +CC = gcc +CFLAGS = -g -Wall -Wextra $(DEBUGFLAGS) + +LDFLAGS = -g -ludns +ifneq (,$(DEBUG)) + LDFLAGS+=-DDEBUG=1 +endif + +src = $(wildcard *.c) +obj = $(src:.pp=.o) + +BUILD_DIR ?= . + +OUTEXE = full_resolver + +all: $(OUTEXE) + +# compile, which is not actually used? +$(BUILDDIR)/%.o: %.c + $(CC) -c $(CFLAGS) $+ + +# link +$(OUTEXE): $(obj) + $(CC) -o $@ $^ $(LDFLAGS) + +.PHONY: clean cleanall list + +list: + @$(MAKE) -pRrq -f $(lastword $(MAKEFILE_LIST)) : 2>/dev/null | ${awkbin} -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | ${sortbin} | ${grepbin} -E -v -e '^[^[:alnum:]]' -e '^$@$$' -e '\.(cp*|o)' + +clean: + rm -f $(obj) + +cleanall: + rm -f $(obj) $(OUTEXE) diff --git a/README.md b/README.md new file mode 100644 index 0000000..ff4108f --- /dev/null +++ b/README.md @@ -0,0 +1,25 @@ +# README for full_resolver +## Overview +Project `full_resolver` is part of a solution for blocking ads with dns null-routing. Specifically, this program is a troubleshooting tool for checking if a list of names are being blocked. + +## Using full_resolver +Pass to standard input a list of domains, one per line. The program will resolve the domain names and display a custom output. +This project was designed primarily for taking abbreviated output of a HAR file and turning it into IP addresses. + + <input.har jq ".log.entries" | jq --raw-output "[.[] | .request.url] | sort | unique | .[]" | awk -F'/' '!x[$3]++ {print $3}' | ./full_resolver + +## Compiling full_resolver +Use the provided Makefile. + + make + +Accepted parameters include: +1. DEBUG=1 + This will set the C macro DEBUG so the debugging fprint instructions are included. + +## Dependencies +[udns](http://www.corpit.ru/mjt/udns.html), found via [https://stackoverflow.com/questions/12328093/dns-lookup-in-c-c](https://stackoverflow.com/questions/12328093/dns-lookup-in-c-c) + +## References +[1][file:///mnt/public/Support/Programs/dns/Troubleshooting-Adblocking-with-DNS.md] +[2][] Web version of [reference 1][1] diff --git a/full_resolver.c b/full_resolver.c new file mode 100644 index 0000000..9c885a2 --- /dev/null +++ b/full_resolver.c @@ -0,0 +1,34 @@ +// File: full_resolver.c +// License: CC-BY-SA 4.0 +// Author: bgstack15 +// Startdate: 2020-09-02 13:59 +// Title: Fully Resolve Domain Names from Stdin +// Purpose: custom output for ipv4 dns lookups +// History: +// Usage: +// References: +// https://stackoverflow.com/a/12252195/3569534 +// Improve: +// Dependencies: +// devuan: libudns-dev +// Documentation: +// /mnt/public/Support/Programs/dns/Troubleshooting-Adblocking-With-DNS.md +#include <stdio.h> +#include "/usr/include/udns.h" +#include "full_resolver.h" + +int main(int argc, const char* argv[]) { + char *line = NULL; + size_t size; + + struct dns_ctx *ctx; + dns_init(ctx,1); + #ifdef DEBUG + printf("BEGIN\n"); + #endif + while (getline(&line, &size, stdin) != -1) { + full_resolve(line, ctx); + } +// printf("No line\n"); + return 0; +} diff --git a/full_resolver.h b/full_resolver.h new file mode 100644 index 0000000..ee5dbd1 --- /dev/null +++ b/full_resolver.h @@ -0,0 +1,66 @@ +// File: full_resolver.h +// License: CC-BY-SA 4.0 +// Author: bgstack15 +// Startdate: 2020-09-02 13:59 +// Title: Header for full_resolver +// Purpose: library for full_resolver +// History: +// Usage: +// References: +// [1] https://stackoverflow.com/a/2693826/3569534 +// [2] strtok safety https://stackoverflow.com/a/23961526/3569534 +// [3] https://stackoverflow.com/questions/32845445/convert-ip-address-in-sockaddr-to-uint32-t/32845610#32845610 +// [4] https://codegolf.stackexchange.com/questions/197879/convert-a-32-bit-binary-ipv4-address-to-its-quad-dotted-notation/197925#197925 +// Improve: +// Dependencies: +// Documentation: + +#ifndef FULL_RESOLVER_H +#define FULL_RESOLVER_H 20200902 +#include <stdio.h> +// If switch to getaddrinfo, need sys/types.h sys/socket.h netdb.h +#include <string.h> +#include <udns.h> +#include <linux/in.h> // proper form is to use arpa/inet.h but it will fail the implicit declaration of function ntohl + +// Forward declarations +u_int32_t ntohl(struct in_addr); // have to declare this to avoid warning + +int full_resolve(char*, struct dns_ctx* ctx); + +int conv_ipv4_dec_to_dotquad(long ip); + +// Functions +int full_resolve(char* name, struct dns_ctx *ctx) { + strtok(name, "\n"); // strip newline + struct dns_rr_a4 *rr; + u_int32_t r; + //dns_resolve_dn(ctx, name, DNS_C_IN, DNS_T_A, 0, *dns_parse_a4); + rr = dns_resolve_a4(ctx, name, 0); + switch (dns_status(ctx)) { + case 0: + //printf("%d\n",rr->dnsa4_nrr); + for (int j=0; j < rr->dnsa4_nrr; ++j) { + r = ntohl(rr->dnsa4_addr[j]); + printf("%d: %s %s ",j,rr->dnsa4_qname,rr->dnsa4_cname); + conv_ipv4_dec_to_dotquad(r); + printf("\n"); + //printf("%d \n",rr->dnsa4_addr[j]); + } + //printf("\n"); + break; + default: + printf("%d: %s\n",dns_status(ctx),name); + break; + } + //printf("%s %s\n", prepend, name, ctx); + return 1; +} + +int conv_ipv4_dec_to_dotquad(long ip) { +// ripped from [4] + for(int i=4;i--;) + printf(".%hhu"+i/3,ip>>i*8); +} + +#endif |