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 0 : fd_get_resolv_conf( fd_resolvconf_t * conf ) { 16 0 : int nns = 0; 17 : 18 0 : conf->ndots = 1; 19 0 : conf->timeout = 5; 20 0 : conf->attempts = 2; 21 : 22 0 : if( fd_etc_resolv_conf_fd<0 ) goto no_resolv_conf; 23 : 24 0 : 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 0 : uchar rbuf[256]; 29 0 : fd_io_buffered_istream_t istream[1]; 30 0 : fd_io_buffered_istream_init( istream, fd_etc_resolv_conf_fd, rbuf, sizeof(rbuf) ); 31 : 32 0 : char line[256]; 33 0 : int err; 34 0 : while( fd_io_fgets( line, sizeof(line), istream, &err ) ) { 35 0 : char * p, * z; 36 0 : if( !strchr( line, '\n' ) && err==0 ) { 37 : /* Ignore lines that get truncated rather than 38 : * potentially misinterpreting them. */ 39 0 : int c; 40 0 : do c = fd_io_fgetc( istream, &err ); 41 0 : while( c!='\n' && c!=-1 ); 42 0 : continue; 43 0 : } 44 0 : if( !strncmp( line, "options", 7 ) && fd_isspace( line[7] ) ) { 45 0 : p = strstr( line, "ndots:" ); 46 0 : if( p && fd_isdigit(p[6]) ) { 47 0 : p += 6; 48 0 : ulong x = strtoul( p, &z, 10 ); 49 0 : if( z != p ) conf->ndots = (uint)( x > 15 ? 15 : x ); 50 0 : } 51 0 : p = strstr( line, "attempts:" ); 52 0 : if( p && fd_isdigit(p[9]) ) { 53 0 : p += 9; 54 0 : ulong x = strtoul( p, &z, 10 ); 55 0 : if( z != p ) conf->attempts = (uint)( x > 10 ? 10 : x ); 56 0 : } 57 0 : p = strstr( line, "timeout:" ); 58 0 : if( p && (isdigit(p[8]) || p[8]=='.') ) { 59 0 : p += 8; 60 0 : ulong x = strtoul( p, &z, 10 ); 61 0 : if( z != p ) conf->timeout = (uint)( x > 60 ? 60 : x ); 62 0 : } 63 0 : continue; 64 0 : } 65 0 : if( !strncmp( line, "nameserver", 10 ) && isspace( line[10] ) ) { 66 0 : if( nns >= MAXNS ) continue; 67 0 : for( p=line+11; isspace(*p); p++ ); 68 0 : for( z=p; *z && !isspace(*z); z++ ); 69 0 : *z=0; 70 0 : if( fd_lookup_ipliteral( conf->ns+nns, p, AF_UNSPEC ) > 0 ) 71 0 : nns++; 72 0 : continue; 73 0 : } 74 0 : } 75 : 76 0 : no_resolv_conf: 77 0 : if( !nns ) { 78 0 : fd_lookup_ipliteral( conf->ns, "127.0.0.1", AF_UNSPEC ); 79 0 : nns = 1; 80 0 : } 81 : 82 0 : conf->nns = (uint)nns; 83 : 84 0 : return 0; 85 0 : }