cosmopolitan/libc/dns/parseresolvconf.c

70 lines
3.4 KiB
C
Raw Normal View History

2020-06-15 14:18:57 +00:00
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/alg/arraylist.internal.h"
2020-06-15 14:18:57 +00:00
#include "libc/dns/dns.h"
#include "libc/dns/resolvconf.h"
2020-09-03 12:44:37 +00:00
#include "libc/mem/mem.h"
2020-06-15 14:18:57 +00:00
#include "libc/runtime/runtime.h"
#include "libc/sock/sock.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/af.h"
#include "libc/sysv/consts/inaddr.h"
/**
* Parses /etc/resolv.conf file.
*
* The content of the file usually looks like this:
*
* nameserver 8.8.8.8
* nameserver 8.8.4.4
*
* @param resolv points to a ResolvConf object, which should be zero
* initialized by the caller; or if it already contains items,
* this function will append
* @param f is an open stream with file content
* @return number of nameservers appended, or -1 w/ errno
*/
int parseresolvconf(struct ResolvConf *resolv, struct FILE *f) {
/* TODO(jart): options ndots:5 */
2020-12-01 11:43:40 +00:00
int rc;
2020-09-03 12:44:37 +00:00
char *line;
size_t linesize;
2020-06-15 14:18:57 +00:00
struct sockaddr_in nameserver;
2020-12-01 11:43:40 +00:00
char *directive, *value, *tok, *comment;
rc = 0;
2020-09-03 12:44:37 +00:00
line = NULL;
linesize = 0;
2020-06-15 14:18:57 +00:00
nameserver.sin_family = AF_INET;
nameserver.sin_port = htons(DNS_PORT);
2020-09-03 12:44:37 +00:00
while (getline(&line, &linesize, f) != -1) {
2020-06-15 14:18:57 +00:00
if ((comment = strchr(line, '#'))) *comment = '\0';
if ((directive = strtok_r(line, " \t\r\n\v", &tok)) &&
(value = strtok_r(NULL, " \t\r\n\v", &tok))) {
if ((strcmp(directive, "nameserver") == 0 &&
inet_pton(AF_INET, value, &nameserver.sin_addr.s_addr) == 1)) {
2020-06-15 14:18:57 +00:00
if (append(&resolv->nameservers, &nameserver) != -1) ++rc;
}
}
}
2020-09-03 12:44:37 +00:00
free(line);
2020-06-15 14:18:57 +00:00
return rc | ferror(f);
}