Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > e3a718fcad37ff363f65d6a6e994e272 > files > 279

ldns-devel-1.6.12-1.fc15.i686.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type"
content="text/html;charset=iso-8859-1">
<title>ldns documentation</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
</head><body>
<div class="logo">
<img src="LogoInGradientBar2-y100.png"/>
</div>
<!-- Generated by Doxygen 1.7.4 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li class="current"><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="annotated.html"><span>Data&#160;Structures</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li><a href="dirs.html"><span>Directories</span></a></li>
    </ul>
  </div>
</div>
<div class="header">
  <div class="headertitle">
<div class="title">Tutorial 2: Reading a zone file </div>  </div>
</div>
<div class="contents">
<div class="textblock"><p>The full source code can be found in <a class="el" href="ldns-read-zone_8c.html">examples/ldns-read-zone.c</a></p>
<p>ldns-read-zone reads a zone file, and prints it to stdout, with 1 resource record per line.</p>
<pre>
% cat example.zone
$ORIGIN example.
$TTL 600</pre><pre>example.        IN SOA  example. op.example. (
                                2004022501 ; serial
                                28800      ; refresh (8 hours)
                                7200       ; retry (2 hours)
                                604800     ; expire (1 week)
                                18000      ; minimum (5 hours)
                                )</pre><pre>@       IN      MX      10 mail.example.
@       IN      NS      ns1
@       IN      NS      ns2
@       IN      A       123.123.123.123</pre><pre>% ldns-read-zone example.zone
example.        600     IN      SOA     example. op.example. 2004022501 28800 7200 604800 18000
example.        600     IN      MX      10 mail.example.
example.        600     IN      NS      ns1.example.
example.        600     IN      NS      ns2.example.
example.        600     IN      A       123.123.123.123
   </pre><p>Again, let's start with including some necessary header files:</p>
<p><div class="fragment"><pre class="fragment"><span class="preprocessor">#include &quot;config.h&quot;</span>
<span class="preprocessor">#include &lt;unistd.h&gt;</span>
<span class="preprocessor">#include &lt;stdlib.h&gt;</span>

<span class="preprocessor">#include &lt;<a class="code" href="ldns_8h.html" title="Including this file will include all ldns files, and define some lookup tables.">ldns/ldns.h</a>&gt;</span>
<span class="preprocessor">#include &lt;<a class="code" href="host2str_8h.html" title="host2str.h - txt presentation of RRs">ldns/host2str.h</a>&gt;</span>

<span class="preprocessor">#include &lt;errno.h&gt;</span>
</pre></div></p>
<p>In this example, we are going to open a file, if that fails, we'll need errno.h to display an error message.</p>
<p>Okay, let's declare the variables we are going to need today:</p>
<p><div class="fragment"><pre class="fragment">        <span class="keywordtype">char</span> *filename;
        FILE *fp;
        <a class="code" href="structldns__struct__zone.html" title="DNS Zone.">ldns_zone</a> *z;
        <span class="keywordtype">int</span> line_nr = 0;
        <span class="keywordtype">int</span> c;
        <span class="keywordtype">bool</span> canonicalize = <span class="keyword">false</span>;
        <span class="keywordtype">bool</span> sort = <span class="keyword">false</span>;
        <span class="keywordtype">bool</span> strip = <span class="keyword">false</span>;
        <span class="keywordtype">bool</span> only_dnssec = <span class="keyword">false</span>;
        <span class="keywordtype">bool</span> print_soa = <span class="keyword">true</span>;
        <a class="code" href="error_8h.html#aaa6d98f86f535cf87b83b89e91f488f9">ldns_status</a> s;
</pre></div></p>
<p>The only two ldns-specific types here are <code>ldns_zone</code> and <code>ldns_status</code>.</p>
<ul>
<li><code>ldns_zone</code> is the structure that can contain a complete zone</li>
<li><code>ldns_status</code> again is used to check return values of ldns functions</li>
</ul>
<p>If we get no filename, we'll read standard input, otherwise, we'll try to open the given filename: <div class="fragment"><pre class="fragment">        <span class="keywordflow">if</span> (argc == 0) {
                fp = stdin;
        } <span class="keywordflow">else</span> {
                filename = argv[0];

                fp = fopen(filename, <span class="stringliteral">&quot;r&quot;</span>);
                <span class="keywordflow">if</span> (!fp) {
                        fprintf(stderr, <span class="stringliteral">&quot;Unable to open %s: %s\n&quot;</span>, filename, strerror(errno));
                        exit(EXIT_FAILURE);
                }
        }
</pre></div></p>
<p>With the <code>FILE</code> pointer in our hands, we visit ldns to pour it into a zone structure: <div class="fragment"><pre class="fragment">        s = <a class="code" href="zone_8c.html#accd66901157e00acd3b4b571aa7092b8">ldns_zone_new_frm_fp_l</a>(&amp;z, fp, NULL, 0, <a class="code" href="rr_8h.html#adc72070b39f210fae670577de8136600a2ad89c0befc939420e3b1157c07a8b46" title="the Internet">LDNS_RR_CLASS_IN</a>, &amp;line_nr);
</pre></div></p>
<p>There is also a <code>ldns_zone_new_frm_fp</code>, but this one also remembers the line number it was on, so we can use that if we encounter a parse error.</p>
<p>Just like in <a class="el" href="tutorial1_mx.html">Tutorial 1: Querying for MX records</a>, the first argument is a pointer to the place ldns should store its creation in, and again, the return value is the status code.</p>
<p>The second argument is the file pointer where our zone data should reside.</p>
<p>The third argument, if not NULL, is a <code>dname</code> that contains the zones origin. It will place this dname after every name in the file that is not a fully qualified domain name.</p>
<p>The fourth argument, if not 0, is the default TTL to use.</p>
<p>Both these values can be specified in the zone file by setting <code>$ORIGIN</code> and <code>$TTL</code>.</p>
<p>The fifth argument specifies the default class, which defaults to IN (<a class="el" href="rr_8h.html#adc72070b39f210fae670577de8136600a2ad89c0befc939420e3b1157c07a8b46">LDNS_RR_CLASS_IN</a>).</p>
<p>And finally, every time <code>ldns_zone_new_frm_fp_l</code> reads a line from the input file pointer, it will increment the value pointed to by the last argument with 1.</p>
<p>Okay, with that, we should have a nice zone structure. Of course we need to check whether it has succeeded.</p>
<p><div class="fragment"><pre class="fragment">        <span class="keywordflow">if</span> (s == <a class="code" href="error_8h.html#a11f34802bb1624af46054952e3b853afac58492ee3fc8d23f33c79824ed08c465">LDNS_STATUS_OK</a>) {
                <span class="keywordflow">if</span> (canonicalize) {
                        <a class="code" href="rr_8c.html#a6e26228733c74b4fb7aacc985a350519" title="converts each dname in a rr to its canonical form.">ldns_rr2canonical</a>(<a class="code" href="zone_8c.html#aae16d59c27e1f2292f8bd87604517e0c" title="Return the soa record of a zone.">ldns_zone_soa</a>(z));
                        <span class="keywordflow">for</span> (i = 0; i &lt; <a class="code" href="rr_8c.html#a7ac3192fe79ba66e47579bb2b267ce05" title="returns the number of rr&#39;s in an rr_list.">ldns_rr_list_rr_count</a>(<a class="code" href="zone_8c.html#a5a75b7744ea0d91770d579730a84bbf9" title="Get a list of a zone&#39;s content.">ldns_zone_rrs</a>(z)); i++) {
                                <a class="code" href="rr_8c.html#a6e26228733c74b4fb7aacc985a350519" title="converts each dname in a rr to its canonical form.">ldns_rr2canonical</a>(<a class="code" href="rr_8c.html#a07b1ccea9f2694b8b88904c543e16783" title="returns a specific rr of an rrlist.">ldns_rr_list_rr</a>(<a class="code" href="zone_8c.html#a5a75b7744ea0d91770d579730a84bbf9" title="Get a list of a zone&#39;s content.">ldns_zone_rrs</a>(z), i));
                        }
                }
                <span class="keywordflow">if</span> (sort) {
                        <a class="code" href="zone_8c.html#ad929e3b1e4a25f676218a1507a7b4277" title="Sort the rrs in a zone, with the current impl.">ldns_zone_sort</a>(z);
                }

                <span class="keywordflow">if</span> (print_soa &amp;&amp; <a class="code" href="zone_8c.html#aae16d59c27e1f2292f8bd87604517e0c" title="Return the soa record of a zone.">ldns_zone_soa</a>(z)) {
                        <span class="keywordflow">if</span> (soa_serial_increment_func) {
                                <a class="code" href="rr__functions_8c.html#a2dc56922e3bef62bc9db11a0b2162833" title="Increment the serial number of the given SOA with the given function using data as an argument for th...">ldns_rr_soa_increment_func_int</a>(
                                          <a class="code" href="zone_8c.html#aae16d59c27e1f2292f8bd87604517e0c" title="Return the soa record of a zone.">ldns_zone_soa</a>(z)
                                        , soa_serial_increment_func
                                        , soa_serial_increment_func_data
                                        );
                        }
                        <a class="code" href="host2str_8c.html#afe9595498f147ada7f15e96265ac6e33" title="Prints the data in the resource record to the given file stream (in presentation format)">ldns_rr_print_fmt</a>(stdout, fmt, <a class="code" href="zone_8c.html#aae16d59c27e1f2292f8bd87604517e0c" title="Return the soa record of a zone.">ldns_zone_soa</a>(z));
                }
                <a class="code" href="host2str_8c.html#a365fd4fa828fbb0330aa60f524e7ceb9" title="print a rr_list to output">ldns_rr_list_print_fmt</a>(stdout, fmt, <a class="code" href="zone_8c.html#a5a75b7744ea0d91770d579730a84bbf9" title="Get a list of a zone&#39;s content.">ldns_zone_rrs</a>(z));

                <a class="code" href="zone_8c.html#a84e80b82e10bbc6939ecdfd556674c1a" title="Frees the allocated memory for the zone, the soa rr in it, and the rr_list structure in it...">ldns_zone_deep_free</a>(z);
</pre></div></p>
<p>If everything went well, we sort the zone if necessary, print it, and free it.</p>
<p>Since <code>ldns_zone</code> contains other ldns structures, we use <code>ldns_deep_free</code> so that every <code>ldns_rr_list</code>, <code>ldns_rr</code> et cetera are freed too.</p>
<p><div class="fragment"><pre class="fragment">        } <span class="keywordflow">else</span> {
                fprintf(stderr, <span class="stringliteral">&quot;%s at %d\n&quot;</span>, 
                                <a class="code" href="error_8c.html#a4005bb78082a40de485f947470fa5017" title="look up a descriptive text by each error.">ldns_get_errorstr_by_id</a>(s),
                                line_nr);
                exit(EXIT_FAILURE);
        }
</pre></div></p>
<p>If something went wrong, we use <code><a class="el" href="error_8c.html#a4005bb78082a40de485f947470fa5017" title="look up a descriptive text by each error.">ldns_get_errorstr_by_id()</a></code> to get a nice error string instead of just a status integer.</p>
<p>And of course, we should play nice and close the <a href="file:">file:</a> <div class="fragment"><pre class="fragment">        fclose(fp);

        exit(EXIT_SUCCESS);
</pre></div> </p>
</div></div>
<hr class="footer"/><address class="footer"><small>Generated on Wed Jan 11 2012 for ldns by&#160;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.4 </small></address>
</body>
</html>