libcli/dns: Add resolv.conf parsing
[bbaumbach/samba-autobuild/.git] / libcli / dns / resolvconftest.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  Internal DNS query structures
4  *  Copyright (C) Volker Lendecke 2018
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <talloc.h>
23 #include <errno.h>
24 #include "libcli/dns/resolvconf.h"
25 #include "lib/util/memory.h"
26
27 static int resolvconftest1(void)
28 {
29         const char *content =
30                 "#foo\nbar\nnameserver 1.2.3.4\nnameserver 2.3.4.5";
31         char *file;
32         FILE *fp;
33         int ret;
34         char **nameservers;
35         size_t num_nameservers;
36
37         file = strdup(content);
38         if (file == NULL) {
39                 perror("strdup failed");
40                 return ENOMEM;
41         }
42         fp = fmemopen(file, strlen(file), "r");
43         if (fp == NULL) {
44                 perror("fmemopen failed");
45                 return errno;
46         }
47
48         ret = parse_resolvconf_fp(fp, NULL, &nameservers, &num_nameservers);
49         if (ret != 0) {
50                 fprintf(stderr, "parse_resolvconf_fp failed: %s\n",
51                         strerror(ret));
52                 return ret;
53         }
54
55         if (num_nameservers != 2) {
56                 fprintf(stderr, "expected 2 nameservers, got %zu\n",
57                         num_nameservers);
58                 return EIO;
59         }
60         if ((strcmp(nameservers[0], "1.2.3.4") != 0) ||
61             (strcmp(nameservers[1], "2.3.4.5") != 0)) {
62                 fprintf(stderr, "got wrong nameservers\n");
63                 return EIO;
64         }
65
66         TALLOC_FREE(nameservers);
67         fclose(fp);
68         SAFE_FREE(file);
69
70         return 0;
71 }
72
73 int main(void) {
74         int ret;
75
76         ret = resolvconftest1();
77         if (ret != 0) {
78                 return 1;
79         }
80
81         return 0;
82 }