11 February 2008: Wouter
- changed library to use ub_ instead of ub_val_ as prefix.
- statistics output text nice.
+ - etc/hosts handling.
8 February 2008: Wouter
- test program for multiple queries over a TCP channel.
.B ub_ctx_config,
.B ub_ctx_set_fwd,
.B ub_ctx_resolvconf,
+.B ub_ctx_hosts,
.B ub_ctx_add_ta,
.B ub_ctx_add_ta_file,
.B ub_ctx_trustedkeys,
\fBub_ctx_resolvconf\fR(\fIstruct ub_ctx*\fR ctx, \fIchar*\fR fname);
.LP
\fIint\fR
+\fBub_ctx_hosts\fR(\fIstruct ub_ctx*\fR ctx, \fIchar*\fR fname);
+.LP
+\fIint\fR
\fBub_ctx_add_ta\fR(\fIstruct ub_ctx*\fR ctx, \fIchar*\fR ta);
.LP
\fIint\fR
.TP
.B ub_ctx_create
Create a new context, initialised with defaults.
+The information from /etc/resolv.conf and /etc/hosts is not utilised
+by default. Use
+.B ub_ctx_resolvconf
+and
+.B ub_ctx_hosts
+to read them.
.TP
.B ub_ctx_delete
Delete validation context and free associated resources.
At this time it is only possible to set configuration before the
first resolve is done.
.TP
+.B ub_ctx_hosts
+Read list of hosts from the filename given.
+Usually "/etc/hosts". When queried for, these addresses are not marked
+DNSSEC secure. If fname NULL is passed, "/etc/hosts" is used.
+At this time it is only possible to set configuration before the
+first resolve is done.
+.TP
.B
ub_ctx_add_ta
Add a trust anchor to the given context.
}
return UB_NOERROR;
}
+
+int
+ub_ctx_hosts(struct ub_ctx* ctx, char* fname)
+{
+ FILE* in;
+ char buf[1024], ldata[1024];
+ char* parse, *addr, *name, *ins;
+ lock_basic_lock(&ctx->cfglock);
+ if(ctx->finalized) {
+ lock_basic_unlock(&ctx->cfglock);
+ errno=EINVAL;
+ return UB_AFTERFINAL;
+ }
+ lock_basic_unlock(&ctx->cfglock);
+ if(fname == NULL)
+ fname = "/etc/hosts";
+ in = fopen(fname, "r");
+ if(!in) {
+ /* error in errno! perror(fname) */
+ return UB_READFILE;
+ }
+ while(fgets(buf, (int)sizeof(buf), in)) {
+ buf[sizeof(buf)-1] = 0;
+ parse=buf;
+ while(*parse == ' ' || *parse == '\t')
+ parse++;
+ if(*parse == '#')
+ continue; /* skip comment */
+ /* format: <addr> spaces <name> spaces <name> ... */
+ addr = parse;
+ /* skip addr */
+ while(isxdigit(*parse) || *parse == '.' || *parse == ':')
+ parse++;
+ if(*parse != ' ' && *parse != '\t') {
+ /* must have whitespace after address */
+ fclose(in);
+ errno=EINVAL;
+ return UB_SYNTAX;
+ }
+ *parse++ = 0; /* end delimiter for addr ... */
+ /* go to names and add them */
+ while(*parse) {
+ while(*parse == ' ' || *parse == '\t' || *parse=='\n')
+ parse++;
+ if(*parse == 0 || *parse == '#')
+ break;
+ /* skip name, allows (too) many printable characters */
+ name = parse;
+ while('!' <= *parse && *parse <= '~')
+ parse++;
+ if(*parse)
+ *parse++ = 0; /* end delimiter for name */
+ snprintf(ldata, sizeof(ldata), "%s %s %s",
+ name, str_is_ip6(addr)?"AAAA":"A", addr);
+ ins = strdup(ldata);
+ if(!ins) {
+ /* out of memory */
+ fclose(in);
+ errno=ENOMEM;
+ return UB_NOMEM;
+ }
+ lock_basic_lock(&ctx->cfglock);
+ if(!cfg_strlist_insert(&ctx->env->cfg->local_data,
+ ins)) {
+ lock_basic_unlock(&ctx->cfglock);
+ fclose(in);
+ free(ins);
+ errno=ENOMEM;
+ return UB_NOMEM;
+ }
+ lock_basic_unlock(&ctx->cfglock);
+ }
+ }
+ fclose(in);
+ return UB_NOERROR;
+}
-ub_val_ctx_create
-ub_val_ctx_delete
-ub_val_ctx_config
-ub_val_ctx_set_fwd
-ub_val_ctx_resolvconf
-ub_val_ctx_add_ta
-ub_val_ctx_add_ta_file
-ub_val_ctx_trustedkeys
-ub_val_ctx_debuglevel
-ub_val_ctx_async
-ub_val_poll
-ub_val_wait
-ub_val_fd
-ub_val_process
-ub_val_resolve
-ub_val_resolve_async
-ub_val_cancel
-ub_val_resolve_free
-ub_val_strerror
+ub_ctx_create
+ub_ctx_delete
+ub_ctx_config
+ub_ctx_set_fwd
+ub_ctx_resolvconf
+ub_ctx_hosts
+ub_ctx_add_ta
+ub_ctx_add_ta_file
+ub_ctx_trustedkeys
+ub_ctx_debuglevel
+ub_ctx_async
+ub_poll
+ub_wait
+ub_fd
+ub_process
+ub_resolve
+ub_resolve_async
+ub_cancel
+ub_resolve_free
+ub_strerror
/**
* Create a resolving and validation context.
+ * The information from /etc/resolv.conf and /etc/hosts is not utilised by
+ * default. Use ub_ctx_resolvconf and ub_ctx_hosts to read them.
* @return a new context. default initialisation.
* returns NULL on error.
*/
*/
int ub_ctx_resolvconf(struct ub_ctx* ctx, char* fname);
+/**
+ * Read list of hosts from the filename given.
+ * Usually "/etc/hosts".
+ * These addresses are not flagged as DNSSEC secure when queried for.
+ *
+ * @param ctx: context.
+ * At this time it is only possible to set configuration before the
+ * first resolve is done.
+ * @param fname: file name string. If NULL "/etc/hosts" is used.
+ * @return 0 if OK, else error.
+ */
+int ub_ctx_hosts(struct ub_ctx* ctx, char* fname);
+
/**
* Add a trust anchor to the given context.
* The trust anchor is a string, on one line, that holds a valid DNSKEY or
printf(" -d : enable debug output\n");
printf(" -f addr : use addr, forward to that server\n");
printf(" -h : this help message\n");
+ printf(" -H fname : read hosts from fname\n");
printf(" -r fname : read resolv.conf from fname\n");
printf(" -t : use a resolver thread instead of forking a process\n");
printf(" -x : perform extended threaded test\n");
if(argc == 1) {
usage(argv);
}
- while( (c=getopt(argc, argv, "bcdf:hr:tx")) != -1) {
+ while( (c=getopt(argc, argv, "bcdf:hH:r:tx")) != -1) {
switch(c) {
case 'd':
r = ub_ctx_debuglevel(ctx, 3);
return 1;
}
break;
+ case 'H':
+ r = ub_ctx_hosts(ctx, optarg);
+ if(r != 0) {
+ printf("ub_ctx_hosts "
+ "error: %s : %s\n",
+ ub_strerror(r),
+ strerror(errno));
+ return 1;
+ }
+ break;
case 'f':
r = ub_ctx_set_fwd(ctx, optarg);
checkerr("ub_ctx_set_fwd", r);