mirror of https://github.com/ericonr/avahi-dns.git
Initial commit.
This commit is contained in:
commit
ecc0b29e91
|
@ -0,0 +1,76 @@
|
|||
/*
|
||||
* read-avahi.c
|
||||
*
|
||||
* Read avahi-browse output and create a config file for a local
|
||||
* DNS server. Requires avahi-browse(1) and a running Avahi daemon.
|
||||
*
|
||||
* Output is hardcoded:
|
||||
* - dns.conf for unbound
|
||||
*
|
||||
* Copyright (c) 2021 Érico Nogueira
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
/* unbound config file */
|
||||
FILE *fu = fopen("dns.conf", "w");
|
||||
if (!fu) return 1;
|
||||
|
||||
FILE *p = popen("avahi-browse --all --resolve --no-db-lookup --parsable --terminate", "re");
|
||||
if (!p) return 1;
|
||||
|
||||
char *line = 0;
|
||||
size_t n;
|
||||
while (getline(&line, &n, p) > 0) {
|
||||
/* not resolved */
|
||||
if (line[0] != '=') continue;
|
||||
|
||||
int ipv6 = -1;
|
||||
char *name=0, *ip=0;
|
||||
char *sp, *s, *liner = line;
|
||||
for (int i=0; s = strtok_r(liner, ";", &sp); i++, liner=0) {
|
||||
switch (i) {
|
||||
case 2:
|
||||
if (strcmp(s, "IPv4") == 0) ipv6 = 0;
|
||||
else if (strcmp(s, "IPv6") == 0) ipv6 = 1;
|
||||
break;
|
||||
case 6:
|
||||
name = s;
|
||||
break;
|
||||
case 7:
|
||||
ip = s;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* failed to find the wanted info */
|
||||
if (!(ipv6>=0 && name && ip)) continue;
|
||||
|
||||
char *type = ipv6 ? "AAAA" : "A";
|
||||
|
||||
fprintf(fu, "local-data: \"%s. IN %s %s\"\n", name, type, ip);
|
||||
}
|
||||
free(line);
|
||||
|
||||
fclose(fu);
|
||||
pclose(p);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue