/*-*- 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                              â
â                                                                              â
â Permission to use, copy, modify, and/or 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 "libc/dns/dns.h"
#include "libc/dns/resolvconf.h"
#include "libc/mem/arraylist.internal.h"
#include "libc/mem/mem.h"
#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 */
  int rc;
  char *line;
  size_t linesize;
  struct sockaddr_in nameserver;
  char *directive, *value, *tok, *comment;
  rc = 0;
  line = 0;
  linesize = 0;
  nameserver.sin_family = AF_INET;
  nameserver.sin_port = htons(DNS_PORT);
  while (getline(&line, &linesize, f) != -1) {
    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)) {
        if (append(&resolv->nameservers, &nameserver) != -1) ++rc;
      }
    }
  }
  free(line);
  return rc | ferror(f);
}
