Logo Search packages:      
Sourcecode: ldns version File versions  Download package

ldns_status ldns_resolver_new_frm_fp_l ( ldns_resolver **  r,
FILE *  fp,
int *  line_nr 
)

Create a resolver structure from a file like /etc/resolv.conf

Parameters:
[out] r the new resolver
[in] fp file pointer to create new resolver from if NULL use /etc/resolv.conf
[in] line_nr pointer to an integer containing the current line number (for debugging purposes)
Returns:
LDNS_STATUS_OK or the error

Definition at line 556 of file resolver.c.

References ldns_bget_token(), ldns_fget_token_l(), ldns_rdf_deep_free(), ldns_rdf_new_frm_str(), LDNS_RDF_TYPE_A, LDNS_RDF_TYPE_AAAA, LDNS_RDF_TYPE_DNAME, ldns_resolver_deep_free(), ldns_resolver_new(), ldns_resolver_new_frm_fp_l(), ldns_resolver_push_nameserver(), ldns_resolver_push_searchlist(), and ldns_resolver_set_domain().

Referenced by ldns_resolver_new_frm_fp(), and ldns_resolver_new_frm_fp_l().

{
      ldns_resolver *r;
      const char *keyword[LDNS_RESOLV_KEYWORDS];
      char word[LDNS_MAX_LINELEN + 1];
      int8_t expect;
      uint8_t i;
      ldns_rdf *tmp;
      ssize_t gtr;
      ldns_buffer *b;

      /* do this better 
       * expect = 
       * 0: keyword
       * 1: default domain dname
       * 2: NS aaaa or a record
       */

      /* recognized keywords */
      keyword[LDNS_RESOLV_NAMESERVER] = "nameserver";
      keyword[LDNS_RESOLV_DEFDOMAIN] = "domain";
      keyword[LDNS_RESOLV_SEARCH] = "search";
      /* these two are read but not used atm TODO */
      keyword[LDNS_RESOLV_SORTLIST] = "sortlist";
      keyword[LDNS_RESOLV_OPTIONS] = "options";
      expect = LDNS_RESOLV_KEYWORD;

      r = ldns_resolver_new();
      if (!r) {
            return LDNS_STATUS_MEM_ERR;
      }

      gtr = 1;
      word[0] = 0;
      while (gtr > 0) {
            /* check comments */
            if (word[0] == '#') {
                  /* read the rest of the line, should be 1 word */
                  gtr = ldns_fget_token_l(fp, word, LDNS_PARSE_NORMAL, 0, line_nr);
                  /* prepare the next string for further parsing */
                  gtr = ldns_fget_token_l(fp, word, LDNS_PARSE_NORMAL, 0, line_nr);
                  continue;
            }
            switch(expect) {
                  case LDNS_RESOLV_KEYWORD:
                        /* keyword */
                        gtr = ldns_fget_token_l(fp, word, LDNS_PARSE_NORMAL, 0, line_nr);
                        if (gtr != 0) {
                              for(i = 0; i < LDNS_RESOLV_KEYWORDS; i++) {
                                    if (strcasecmp(keyword[i], word) == 0) {
                                          /* chosen the keyword and
                                           * expect values carefully
                                           */
                                          expect = i;
                                          break;
                                    }
                              }
                              if (word[0] == '#') {
                                    /* skip the rest of the line*/
                                    gtr = ldns_fget_token_l(fp, word, LDNS_PARSE_SKIP_SPACE, 0, line_nr);
                                    continue;
                              }
                              /* no keyword recognized */
                              if (expect == LDNS_RESOLV_KEYWORD) {
                                    ldns_resolver_deep_free(r);
                                    return LDNS_STATUS_SYNTAX_KEYWORD_ERR;
                              }
                        }
                        break;
                  case LDNS_RESOLV_DEFDOMAIN:
                        /* default domain dname */
                        gtr = ldns_fget_token_l(fp, word, LDNS_PARSE_NORMAL, 0, line_nr);
                        if (gtr == 0) {
                              return LDNS_STATUS_SYNTAX_MISSING_VALUE_ERR;
                        }
                        tmp = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_DNAME, word);
                        if (!tmp) {
                              ldns_resolver_deep_free(r);
                              return LDNS_STATUS_SYNTAX_DNAME_ERR;
                        }

                        /* DOn't free, because we copy the pointer */
                        ldns_resolver_set_domain(r, tmp);
                        expect = LDNS_RESOLV_KEYWORD;
                        break;
                  case LDNS_RESOLV_NAMESERVER:
                        /* NS aaaa or a record */
                        gtr = ldns_fget_token_l(fp, word, LDNS_PARSE_NORMAL, 0, line_nr);
                        if (gtr == 0) {
                              return LDNS_STATUS_SYNTAX_MISSING_VALUE_ERR;
                        }
                        tmp = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_AAAA, word);
                        if (!tmp) {
                              /* try ip4 */
                              tmp = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_A, word);
                        }
                        /* could not parse it, exit */
                        if (!tmp) {
                              ldns_resolver_deep_free(r);
                              return LDNS_STATUS_SYNTAX_ERR;
                        }
                        (void)ldns_resolver_push_nameserver(r, tmp);
                        ldns_rdf_deep_free(tmp);
                        expect = LDNS_RESOLV_KEYWORD;
                        break;
                  case LDNS_RESOLV_SEARCH:
                        /* search list domain dname */
                        gtr = ldns_fget_token_l(fp, word, LDNS_PARSE_SKIP_SPACE, 0, line_nr);
                        b = LDNS_MALLOC(ldns_buffer);
                        ldns_buffer_new_frm_data(b, word, (size_t) gtr);

                        gtr = ldns_bget_token(b, word, LDNS_PARSE_NORMAL, (size_t) gtr);
                        while (gtr > 0) {
                              tmp = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_DNAME, word);
                              if (!tmp) {
                                    ldns_resolver_deep_free(r);
                                    return LDNS_STATUS_SYNTAX_DNAME_ERR;
                              }

                              ldns_resolver_push_searchlist(r, tmp); 

                              ldns_rdf_deep_free(tmp);
                              gtr = ldns_bget_token(b, word, LDNS_PARSE_NORMAL, (size_t) gtr);
                        }
                        ldns_buffer_free(b);
                        gtr = 1;
                        expect = LDNS_RESOLV_KEYWORD;
                        break;
                  case LDNS_RESOLV_SORTLIST:
                        gtr = ldns_fget_token_l(fp, word, LDNS_PARSE_SKIP_SPACE, 0, line_nr);
                        /* sortlist not implemented atm */
                        expect = LDNS_RESOLV_KEYWORD;
                        break;
                  case LDNS_RESOLV_OPTIONS:
                        gtr = ldns_fget_token_l(fp, word, LDNS_PARSE_SKIP_SPACE, 0, line_nr);
                        /* options not implemented atm */
                        expect = LDNS_RESOLV_KEYWORD;
                        break;
            }
      }
      
      if (res) {
            *res = r;
            return LDNS_STATUS_OK;
      } else {
            return LDNS_STATUS_NULL;
      }
}


Generated by  Doxygen 1.6.0   Back to index