LCOV - code coverage report
Current view: top level - waltz/resolv - fd_resolvconf.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 40 63 63.5 %
Date: 2025-10-13 04:42:14 Functions: 1 1 100.0 %

          Line data    Source code
       1             : #include "fd_lookup.h"
       2             : #include <ctype.h>
       3             : #include <errno.h>
       4             : #include <fcntl.h>
       5             : #include <unistd.h>
       6             : #include <string.h>
       7             : #include <stdlib.h>
       8             : #include <netinet/in.h>
       9             : #include "../../util/cstr/fd_cstr.h"
      10             : #include "../../util/log/fd_log.h"
      11             : #include "../../util/io/fd_io.h"
      12             : #include "fd_io_readline.h"
      13             : 
      14             : int
      15           6 : fd_get_resolv_conf( fd_resolvconf_t * conf ) {
      16           6 :   int nns = 0;
      17             : 
      18           6 :   conf->ndots = 1;
      19           6 :   conf->timeout = 5;
      20           6 :   conf->attempts = 2;
      21             : 
      22           6 :   if( fd_etc_resolv_conf_fd<0 ) goto no_resolv_conf;
      23             : 
      24           6 :   if( FD_UNLIKELY( -1==lseek( fd_etc_resolv_conf_fd, 0, SEEK_SET ) ) ) {
      25           0 :     FD_LOG_ERR(( "lseek(/etc/resolv.conf,0,SEEK_SET) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
      26           0 :   }
      27             : 
      28           6 :   uchar rbuf[256];
      29           6 :   fd_io_buffered_istream_t istream[1];
      30           6 :   fd_io_buffered_istream_init( istream, fd_etc_resolv_conf_fd, rbuf, sizeof(rbuf) );
      31             : 
      32           6 :   char line[256];
      33           6 :   int fgets_err = 0;
      34          24 :   while( fgets_err!=-1 &&
      35          24 :          fd_io_fgets( line, sizeof(line), istream, &fgets_err ) ) {
      36          18 :     char * p, * z;
      37          18 :     if( !strchr( line, '\n' ) && fgets_err==0 ) {
      38             :       /* Ignore lines that get truncated rather than
      39             :        * potentially misinterpreting them. */
      40           0 :       int c;
      41           0 :       int err;
      42           0 :       do c = fd_io_fgetc( istream, &err );
      43           0 :       while( c!='\n' && c!=-1 );
      44           0 :       continue;
      45           0 :     }
      46          18 :     if( !strncmp( line, "options", 7 ) && fd_isspace( line[7] ) ) {
      47           6 :       p = strstr( line, "ndots:" );
      48           6 :       if( p && fd_isdigit(p[6]) ) {
      49           0 :         p += 6;
      50           0 :         ulong x = strtoul( p, &z, 10 );
      51           0 :         if( z != p ) conf->ndots = (uint)( x > 15 ? 15 : x );
      52           0 :       }
      53           6 :       p = strstr( line, "attempts:" );
      54           6 :       if( p && fd_isdigit(p[9]) ) {
      55           0 :         p += 9;
      56           0 :         ulong x = strtoul( p, &z, 10 );
      57           0 :         if( z != p ) conf->attempts = (uint)( x > 10 ? 10 : x );
      58           0 :       }
      59           6 :       p = strstr( line, "timeout:" );
      60           6 :       if( p && (isdigit(p[8]) || p[8]=='.') ) {
      61           0 :         p += 8;
      62           0 :         ulong x = strtoul( p, &z, 10 );
      63           0 :         if( z != p ) conf->timeout = (uint)( x > 60 ? 60 : x );
      64           0 :       }
      65           6 :       continue;
      66           6 :     }
      67          12 :     if( !strncmp( line, "nameserver", 10 ) && isspace( line[10] ) ) {
      68           6 :       if( nns >= MAXNS ) continue;
      69           6 :       for( p=line+11; isspace(*p); p++ );
      70          66 :       for( z=p; *z && !isspace(*z); z++ );
      71           6 :       *z=0;
      72           6 :       if( fd_lookup_ipliteral( conf->ns+nns, p, AF_UNSPEC ) > 0 )
      73           6 :         nns++;
      74           6 :       continue;
      75           6 :     }
      76          12 :   }
      77             : 
      78           6 : no_resolv_conf:
      79           6 :   if( !nns ) {
      80           0 :     fd_lookup_ipliteral( conf->ns, "127.0.0.1", AF_UNSPEC );
      81           0 :     nns = 1;
      82           0 :   }
      83             : 
      84           6 :   conf->nns = (uint)nns;
      85             : 
      86           6 :   return 0;
      87           6 : }

Generated by: LCOV version 1.14