[ldns-users] suggestion for a parse-rrs callback method

Robert Edmonds edmonds at debian.org
Mon May 27 15:17:31 UTC 2013

Jelte Jansen wrote:
> So what would be very useful is a general construct to define a
> function that is called for every RR in a given file stream, e.g. a
> callback that gets an RR and probably a callback-specific void*
> 'state' object. Said construct would keep track of the state
> involved in 'real' parsing so that I don't have to repeat the same
> code over and over again.
> Ideally, the calling program would then just have to define its
> callback, open the file, and call the ldns parser code, and be done
> with it, e.g. in the case of ldns I'd want something like:
> // Callback to print the RR, no state.
> int printer(ldns_rr* rr, void* state) {
> 	ldns_rr_print(stdout, rr);
> 	return 0;
> }
> in = fopen("my.zone", "r");
> if (in != NULL) {
> 	ldns_parse_rr_stream(in, printer, NULL);
> 	fclose(in);
> }

this seems like a very simple iterator pattern.  why complicate it with
a callback?  e.g.,

    in = fopen("my.zone", "r");
    if (in != NULL) {
        ldns_rr *rr;
        ldns_rr_iter *iter = ldns_parse_rr_iter_frm_fp(in);
        while ((rr = ldns_rr_iter_next(iter)) != NULL)
            ldns_rr_print(stdout, rr);

this makes the interface much easier to call in languages that are not C
or C++.

Robert Edmonds
edmonds at debian.org

More information about the ldns-users mailing list