tests: add dns_handle_packet_file tool
[project/mdnsd.git] / tests / dns_handle_packet_file.c
1 #define _GNU_SOURCE
2 #include <stdio.h>
3 #include <stdint.h>
4 #include <stdlib.h>
5 #include <stddef.h>
6 #include <string.h>
7 #include <fcntl.h>
8 #include <errno.h>
9 #include <unistd.h>
10
11 #include <sys/types.h>
12 #include <sys/stat.h>
13
14 #include "dns.h"
15 #include "cache.c"
16 #include "interface.h"
17
18 int cfg_proto = 0;
19 int cfg_no_subnet = 0;
20
21 static void fuzz_dns_handle_packet(uint8_t *input, size_t size)
22 {
23 struct sockaddr from;
24 struct interface iface;
25 struct cache_service *s, *t;
26
27 memset(&from, 0, sizeof(from));
28 memset(&iface, 0, sizeof(iface));
29
30 cache_init();
31 dns_handle_packet(&iface, &from, 1922, input, size);
32
33 avl_for_each_element_safe(&services, s, avl, t)
34 cache_service_free(s);
35 }
36
37 int main(int argc, char *argv[])
38 {
39 size_t len = 0;
40 FILE *fd = NULL;
41 uint8_t *buf = NULL;
42
43 if (argc != 2) {
44 fprintf(stderr, "Usage: %s <packet.bin>\n", argv[0]);
45 return -1;
46 }
47
48 fd = fopen(argv[1], "r");
49 if (!fd) {
50 perror("unable to open input file\n");
51 return -1;
52 }
53
54 buf = calloc(1, MDNS_BUF_LEN+1);
55 if (!buf)
56 return -1;
57
58 len = fread(buf, 1, MDNS_BUF_LEN, fd);
59
60 fuzz_dns_handle_packet(buf, len);
61
62 fclose(fd);
63 free(buf);
64 }