/* read a line, put it in a buffer, parse the buffer */
ldns_rr_list *
ldns_get_rr_list_hosts_frm_fp(FILE *fp)
+{
+ return ldns_get_rr_list_hosts_frm_fp_l(fp, NULL);
+}
+
+ldns_rr_list *
+ldns_get_rr_list_hosts_frm_fp_l(FILE *fp, int *line_nr)
{
ssize_t i, j;
size_t cnt;
list = ldns_rr_list_new();
rr = NULL;
- for(i = ldns_fget_token(fp, line, "\n", 0);
+ for(i = ldns_fget_token_l(fp, line, "\n", 0, line_nr);
i > 0;
- i = ldns_fget_token(fp, line, "\n", 0))
+ i = ldns_fget_token_l(fp, line, "\n", 0, line_nr))
{
/* # is comment */
if (line[0] == '#') {
ldns_key *
ldns_key_new_frm_fp(FILE *fp)
+{
+ return ldns_key_new_frm_fp_l(fp, NULL);
+}
+
+ldns_key *
+ldns_key_new_frm_fp_l(FILE *fp, int *line_nr)
{
ldns_key *k;
char *d;
*/
/* get the key format version number */
- if (ldns_fget_keyword_data(fp, "Private-key-format", ": ", d, "\n",
- LDNS_MAX_LINELEN) == -1) {
+ if (ldns_fget_keyword_data_l(fp, "Private-key-format", ": ", d, "\n",
+ LDNS_MAX_LINELEN, line_nr) == -1) {
/* no version information */
return NULL;
}
/* get the algorithm type, our file function strip ( ) so there are
* not in the return string! */
- if (ldns_fget_keyword_data(fp, "Algorithm", ": ", d, "\n",
- LDNS_MAX_LINELEN) == -1) {
+ if (ldns_fget_keyword_data_l(fp, "Algorithm", ": ", d, "\n",
+ LDNS_MAX_LINELEN, line_nr) == -1) {
/* no version information */
return NULL;
}
case LDNS_SIGN_RSASHA1:
ldns_key_set_algorithm(k, alg);
- ldns_key_set_rsa_key(k, ldns_key_new_frm_fp_rsa(fp));
+ ldns_key_set_rsa_key(k, ldns_key_new_frm_fp_rsa_l(fp, line_nr));
break;
case LDNS_SIGN_DSA:
ldns_key_set_algorithm(k, alg);
- ldns_key_set_dsa_key(k, ldns_key_new_frm_fp_dsa(fp));
+ ldns_key_set_dsa_key(k, ldns_key_new_frm_fp_dsa_l(fp, line_nr));
break;
}
RSA *
ldns_key_new_frm_fp_rsa(FILE *f)
+{
+ return ldns_key_new_frm_fp_rsa_l(f, NULL);
+}
+
+RSA *
+ldns_key_new_frm_fp_rsa_l(FILE *f, int *line_nr)
{
/* we parse
* Modulus:
*/
/* Modules, rsa->n */
- if (ldns_fget_keyword_data(f, "Modulus", ": ", d, "\n", LDNS_MAX_LINELEN) == -1) {
+ if (ldns_fget_keyword_data_l(f, "Modulus", ": ", d, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
goto error;
}
i = b64_pton((const char*)d, buf, b64_ntop_calculate_size(strlen(d)));
}
/* PublicExponent, rsa->e */
- if (ldns_fget_keyword_data(f, "PublicExponent", ": ", d, "\n", LDNS_MAX_LINELEN) == -1) {
+ if (ldns_fget_keyword_data_l(f, "PublicExponent", ": ", d, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
goto error;
}
i = b64_pton((const char*)d, buf, b64_ntop_calculate_size(strlen(d)));
}
/* PrivateExponent, rsa->d */
- if (ldns_fget_keyword_data(f, "PrivateExponent", ": ", d, "\n", LDNS_MAX_LINELEN) == -1) {
+ if (ldns_fget_keyword_data_l(f, "PrivateExponent", ": ", d, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
goto error;
}
i = b64_pton((const char*)d, buf, b64_ntop_calculate_size(strlen(d)));
}
/* Prime1, rsa->p */
- if (ldns_fget_keyword_data(f, "Prime1", ": ", d, "\n", LDNS_MAX_LINELEN) == -1) {
+ if (ldns_fget_keyword_data_l(f, "Prime1", ": ", d, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
goto error;
}
i = b64_pton((const char*)d, buf, b64_ntop_calculate_size(strlen(d)));
}
/* Prime2, rsa->q */
- if (ldns_fget_keyword_data(f, "Prime2", ": ", d, "\n", LDNS_MAX_LINELEN) == -1) {
+ if (ldns_fget_keyword_data_l(f, "Prime2", ": ", d, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
goto error;
}
i = b64_pton((const char*)d, buf, b64_ntop_calculate_size(strlen(d)));
}
/* Exponent1, rsa->dmp1 */
- if (ldns_fget_keyword_data(f, "Exponent1", ": ", d, "\n", LDNS_MAX_LINELEN) == -1) {
+ if (ldns_fget_keyword_data_l(f, "Exponent1", ": ", d, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
goto error;
}
i = b64_pton((const char*)d, buf, b64_ntop_calculate_size(strlen(d)));
}
/* Exponent2, rsa->dmq1 */
- if (ldns_fget_keyword_data(f, "Exponent2", ": ", d, "\n", LDNS_MAX_LINELEN) == -1) {
+ if (ldns_fget_keyword_data_l(f, "Exponent2", ": ", d, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
goto error;
}
i = b64_pton((const char*)d, buf, b64_ntop_calculate_size(strlen(d)));
}
/* Coefficient, rsa->iqmp */
- if (ldns_fget_keyword_data(f, "Coefficient", ": ", d, "\n", LDNS_MAX_LINELEN) == -1) {
+ if (ldns_fget_keyword_data_l(f, "Coefficient", ": ", d, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
goto error;
}
i = b64_pton((const char*)d, buf, b64_ntop_calculate_size(strlen(d)));
DSA *
ldns_key_new_frm_fp_dsa(FILE *f)
+{
+ return ldns_key_new_frm_fp_dsa_l(f, NULL);
+}
+
+DSA *
+ldns_key_new_frm_fp_dsa_l(FILE *f, int *line_nr)
{
char *d;
DSA *dsa;
+ line_nr = line_nr;
+ /* not impl apparently */
d = LDNS_XMALLOC(char, LDNS_MAX_LABELLEN);
dsa = DSA_new();
*/
ldns_rr_list *ldns_get_rr_list_hosts_frm_fp(FILE *fp);
+/**
+ * wade through fp (a /etc/hosts like file)
+ * and return a rr_list containing all the
+ * defined hosts in there
+ * \param[in] fp the file pointer to use
+ * \param[in] line_nr pointer to an integer containing the current line number (for debugging purposes)
+ * \return ldns_rr_list * with the names
+ */
+ldns_rr_list *ldns_get_rr_list_hosts_frm_fp_l(FILE *fp, int *line_nr);
+
/**
* wade through fp (a /etc/hosts like file)
* and return a rr_list containing all the
*/
ldns_key *ldns_key_new_frm_fp(FILE *fp);
+/**
+ * creates a new priv key based on the
+ * contents of the file pointed by fp
+ *
+ * \param[in] fp the file pointer to use
+ * \param[in] line_nr pointer to an integer containing the current line number (for debugging purposes)
+ * \return a new ldns_key structure with the key
+ */
+ldns_key *ldns_key_new_frm_fp_l(FILE *fp, int *line_nr);
+
/**
* frm_fp helper function. This function parsed the
* remainder of the (RSA) priv. key file generated from bind9
* \return NULL on failure otherwise a RSA structure
*/
RSA *ldns_key_new_frm_fp_rsa(FILE *fp);
+
+/**
+ * frm_fp helper function. This function parsed the
+ * remainder of the (RSA) priv. key file generated from bind9
+ * \param[in] fp the file to parse
+ * \param[in] line_nr pointer to an integer containing the current line number (for debugging purposes)
+ * \return NULL on failure otherwise a RSA structure
+ */
+RSA *ldns_key_new_frm_fp_rsa_l(FILE *fp, int *line_nr);
+
/**
* frm_fp helper function. This function parsed the
* remainder of the (DSA) priv. key file generated from bind9
*/
DSA *ldns_key_new_frm_fp_dsa(FILE *fp);
+/**
+ * frm_fp helper function. This function parsed the
+ * remainder of the (DSA) priv. key file generated from bind9
+ * \param[in] fp the file to parse
+ * \param[in] line_nr pointer to an integer containing the current line number (for debugging purposes)
+ * \return NULL on failure otherwise a RSA structure
+ */
+DSA *ldns_key_new_frm_fp_dsa_l(FILE *fp, int *line_nr);
+
/* acces write functions */
void ldns_key_set_algorithm(ldns_key *k, ldns_signing_algorithm l);
void ldns_key_set_rsa_key(ldns_key *k, RSA *r);
*/
ssize_t ldns_fget_token(FILE *f, char *token, const char *delim, size_t limit);
+/**
+ * returns a token/char from the stream F.
+ * This function deals with ( and ) in the stream,
+ * and ignores when it finds them.
+ * \param[in] *f the file to read from
+ * \param[out] *token the token is put here
+ * \param[in] *delim chars at which the parsing should stop
+ * \param[in] *limit how much to read. If 0 use builtin maximum
+ * \param[in] line_nr pointer to an integer containing the current line number (for debugging purposes)
+ * \return 0 on error of EOF of F otherwise return the length of what is read
+ */
+ssize_t ldns_fget_token_l(FILE *f, char *token, const char *delim, size_t limit, int *line_nr);
+
/*
* searches for keyword and delim. Gives everything back
* after the keyword + k_del until we hit d_del
*/
ssize_t ldns_fget_keyword_data(FILE *f, const char *keyword, const char *k_del, char *data, const char *d_del, size_t data_limit);
+/*
+ * searches for keyword and delim. Gives everything back
+ * after the keyword + k_del until we hit d_del
+ * \param[in] line_nr pointer to an integer containing the current line number (for debugging purposes)
+ */
+ssize_t ldns_fget_keyword_data_l(FILE *f, const char *keyword, const char *k_del, char *data, const char *d_del, size_t data_limit, int *line_nr);
+
/**
* returns a token/char from the buffer b.
* This function deals with ( and ) in the buffer,
*/
void ldns_fskipcs(FILE *fp, const char *s);
+
+/**
+ * skips all of the characters in the given string in the fp, moving
+ * the position to the first character that is not in *s.
+ * \param[in] *fp file to use
+ * \param[in] *s characters to skip
+ * \param[in] line_nr pointer to an integer containing the current line number (for debugging purposes)
+ * \return void
+ */
+void ldns_fskipcs_l(FILE *fp, const char *s, int *line_nr);
+
#endif /* _LDNS_PARSE_H */
*/
ldns_rdf *ldns_rdf_new_frm_fp(ldns_rdf_type type, FILE *fp);
+/**
+ * creates a new rdf from a file containing a string.
+ * \param[in] type type to use
+ * \param[in] fp the file pointer to use
+ * \param[in] line_nr pointer to an integer containing the current line number (for debugging purposes)
+ * \return ldns_rdf*
+ */
+ldns_rdf *ldns_rdf_new_frm_fp_l(ldns_rdf_type type, FILE *fp, int *line_nr);
+
/* destroy functions */
/**
*/
ldns_resolver* ldns_resolver_new_frm_fp(FILE *fp);
+/**
+ * Create a resolver structure from a file like /etc/resolv.conf
+ * \param[in] fp file pointer to create new resolver from
+ * if NULL use /etc/resolv.conf
+ * \param[in] line_nr pointer to an integer containing the current line number (for debugging purposes)
+ * \return ldns_resolver structure
+ */
+ldns_resolver* ldns_resolver_new_frm_fp_l(FILE *fp, int *line_nr);
+
/**
* configure a resolver by means of a resolv.conf file
* The file may be NULL in which case there will be
*/
ldns_rr* ldns_rr_new_frm_fp(FILE *fp, uint16_t default_ttl, ldns_rdf *origin);
+/**
+ * creates a new rr from a file containing a string.
+ * \param[in] fp the file pointer to use
+ * \param[in] default_ttl a default ttl for the rr. If 0 DEF_TTL will be used
+ * \param[in] origin when the owner is relative add this
+ * \param[in] line_nr pointer to an integer containing the current line number (for debugging purposes)
+ * \return ldns_rr*
+ */
+ldns_rr* ldns_rr_new_frm_fp_l(FILE *fp, uint16_t default_ttl, ldns_rdf *origin, int *line_nr);
+
/**
* sets the owner in the rr structure.
* \param[in] *rr rr to operate on
#ifndef _UTIL_H
#define _UTIL_H
+#include <stdbool.h>
+
#define dprintf(X,Y) fprintf(stderr, (X), (Y))
/* #define dprintf(X, Y) */
*/
int ldns_get_bit_r(uint8_t bits[], size_t index);
+/**
+ * sets the specified bit in the specified byte to
+ * 1 if value is true, 0 if false
+ * The bits are counted from right to left, so bit #0 is the
+ * right most bit.
+ *
+ * \param[in] byte the bit to set the bit in
+ * \param[in] bit_nr the bit to set (0 <= n <= 7)
+ * \param[in] value whether to set the bit to 1 or 0
+ */
+void ldns_set_bit(uint8_t *byte, int bit_nr, bool value);
+
/**
* Returns the value of a to the power of b
* (or 1 of b < 1)
ldns_zone *
ldns_zone_new_frm_fp(FILE *fp, ldns_rdf *origin, uint16_t ttl, ldns_rr_class c);
+ldns_zone *
+ldns_zone_new_frm_fp_l(FILE *fp, ldns_rdf *origin, uint16_t ttl, ldns_rr_class c, int *line_nr);
+
/**
* Frees the allocated memory for the zone, and the rr_list structure in it
* \param[in] zone the zone to free
ssize_t
ldns_fget_keyword_data(FILE *f, const char *keyword, const char *k_del, char *data,
const char *d_del, size_t data_limit)
+{
+ return ldns_fget_keyword_data_l(f, keyword, k_del, data, d_del, data_limit, NULL);
+}
+
+ssize_t
+ldns_fget_keyword_data_l(FILE *f, const char *keyword, const char *k_del, char *data,
+ const char *d_del, size_t data_limit, int *line_nr)
{
/* we assume: keyword|sep|data */
char *fkeyword;
if (strncmp(fkeyword, keyword, LDNS_MAX_KEYWORDLEN - 1) == 0) {
/* whee! */
/* printf("%s\n%s\n", "Matching keyword", fkeyword); */
- i = ldns_fget_token(f, data, d_del, data_limit);
+ i = ldns_fget_token_l(f, data, d_del, data_limit, line_nr);
LDNS_FREE(fkeyword);
return i;
} else {
/* add max_limit here? */
ssize_t
ldns_fget_token(FILE *f, char *token, const char *delim, size_t limit)
+{
+ return ldns_fget_token_l(f, token, delim, limit, NULL);
+}
+
+ssize_t
+ldns_fget_token_l(FILE *f, char *token, const char *delim, size_t limit, int *line_nr)
{
int c;
int p; /* 0 -> no parenthese seen, >0 nr of ( seen */
/* comments */
com = 0;
*t = ' ';
+ if (line_nr) {
+ *line_nr = *line_nr + 1;
+ }
continue;
}
if (c == '\n' && p != 0 && t > token) {
/* in parentheses */
+ if (line_nr) {
+ *line_nr = *line_nr + 1;
+ }
continue;
}
return (ssize_t)i;
tokenread:
- ldns_fskipcs(f, delim);
+ ldns_fskipcs_l(f, delim, line_nr);
*t = '\0';
if (p != 0) {
return -1;
void
ldns_fskipcs(FILE *fp, const char *s)
+{
+ ldns_fskipcs_l(fp, s, NULL);
+}
+
+void
+ldns_fskipcs_l(FILE *fp, const char *s, int *line_nr)
{
bool found;
char c;
const char *d;
while ((c = fgetc(fp)) != EOF) {
+ if (line_nr && c == '\n') {
+ *line_nr = *line_nr + 1;
+ }
found = false;
for (d = s; *d; d++) {
if (*d == c) {
ldns_rdf *
ldns_rdf_new_frm_fp(ldns_rdf_type type, FILE *fp)
+{
+ return ldns_rdf_new_frm_fp_l(type, fp, NULL);
+}
+
+ldns_rdf *
+ldns_rdf_new_frm_fp_l(ldns_rdf_type type, FILE *fp, int *line_nr)
{
char *line;
ldns_rdf *r;
}
/* read an entire line in from the file */
- if ((t = ldns_fget_token(fp, line, LDNS_PARSE_SKIP_SPACE, 0)) == -1) {
+ if ((t = ldns_fget_token_l(fp, line, LDNS_PARSE_SKIP_SPACE, 0, line_nr)) == -1) {
LDNS_FREE(line);
return NULL;
}
ldns_resolver *
ldns_resolver_new_frm_fp(FILE *fp)
+{
+ return ldns_resolver_new_frm_fp_l(fp, NULL);
+}
+
+ldns_resolver *
+ldns_resolver_new_frm_fp_l(FILE *fp, int *line_nr)
{
ldns_resolver *r;
const char *keyword[2];
if (!r) {
return NULL;
}
- gtr = ldns_fget_token(fp, word, LDNS_PARSE_NORMAL, 0);
+ gtr = ldns_fget_token_l(fp, word, LDNS_PARSE_NORMAL, 0, line_nr);
while (gtr > 0) {
/* do something */
switch(expect) {
expect = LDNS_RESOLV_KEYWORD;
break;
}
- gtr = ldns_fget_token(fp, word, LDNS_PARSE_NORMAL, 0);
+ gtr = ldns_fget_token_l(fp, word, LDNS_PARSE_NORMAL, 0, line_nr);
}
LDNS_FREE(word);
ldns_rr *
ldns_rr_new_frm_fp(FILE *fp, uint16_t ttl, ldns_rdf *origin)
+{
+ return ldns_rr_new_frm_fp_l(fp, ttl, origin, NULL);
+}
+
+ldns_rr *
+ldns_rr_new_frm_fp_l(FILE *fp, uint16_t ttl, ldns_rdf *origin, int *line_nr)
{
char *line;
ldns_rr *rr;
return NULL;
}
+ if (line_nr) {
+ *line_nr = *line_nr + 1;
+ }
+
/* read an entire line in from the file */
- if (ldns_fget_token(fp, line, LDNS_PARSE_SKIP_SPACE, LDNS_MAX_LINELEN) == -1) {
+ if (ldns_fget_token_l(fp, line, LDNS_PARSE_SKIP_SPACE, LDNS_MAX_LINELEN, line_nr) == -1) {
return NULL;
}
}
if (ldns_rr_list_rr_count(rr_list) > 0) {
- next_rr = ldns_rr_list_rr(rr_list, 0);
+ next_rr = ldns_rr_list_rr(rr_list, ldns_rr_list_rr_count(rr_list) - 1);
} else {
next_rr = NULL;
}
ldns_rr_list_push_rr(rrset, ldns_rr_list_pop_rr(rr_list));
if (ldns_rr_list_rr_count(rr_list) > 0) {
last_rr = next_rr;
- next_rr = ldns_rr_list_rr(rr_list, 0);
+ next_rr = ldns_rr_list_rr(rr_list, ldns_rr_list_rr_count(rr_list) - 1);
} else {
next_rr = NULL;
}
next_rr = NULL;
}
}
-
+
return rrset;
}
*/
ldns_zone *
ldns_zone_new_frm_fp(FILE *fp, ldns_rdf *origin, uint16_t ttl, ldns_rr_class c)
+{
+ return ldns_zone_new_frm_fp_l(fp, origin, ttl, c, NULL);
+}
+
+ldns_zone *
+ldns_zone_new_frm_fp_l(FILE *fp, ldns_rdf *origin, uint16_t ttl, ldns_rr_class c, int *line_nr)
{
ldns_zone *newzone;
ldns_rr *rr;
my_ttl = ttl;
my_class = c;
-
/* read until we got a soa, all crap above is discarded
* except $directives
*/
i = 0;
do {
- rr = ldns_rr_new_frm_fp(fp, my_ttl, my_origin);
+ rr = ldns_rr_new_frm_fp_l(fp, my_ttl, my_origin, line_nr);
i++;
} while (!rr && i <= 9);
ldns_zone_set_soa(newzone, rr);
while(!feof(fp)) {
- rr = ldns_rr_new_frm_fp(fp, my_ttl, my_origin);
+ rr = ldns_rr_new_frm_fp_l(fp, my_ttl, my_origin, line_nr);
if (rr) {
last_rr = rr;
if (!ldns_zone_push_rr(newzone, rr)) {
my_class = ldns_rr_get_class(rr);
} else {
- fprintf(stderr, "Error in file, unable to read RR.\nLast rr that was parsed:\n");
+ fprintf(stderr, "Error in file, unable to read RR");
+ if (line_nr) {
+ fprintf(stderr, " at line %d.\n", *line_nr);
+ } else {
+ fprintf(stderr, ".");
+ }
+
+ fprintf(stderr, "Last rr that was parsed:\n");
ldns_rr_print(stdout, last_rr);
printf("\n");
}