ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libspf/docs/API/doxygen/html/dns_8c-source.html
Revision: 1.1
Committed: Tue Nov 13 00:51:24 2007 UTC (18 years, 10 months ago) by root
Content type: text/html
Branch: MAIN
CVS Tags: HEAD
Log Message:
initial import of libspf-1.0.0-p5 from freebsd ports

File Contents

# User Rev Content
1 root 1.1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2     <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
3     <title>libSPF v1.0: dns.c Source File</title>
4     <link href="doxygen.css" rel="stylesheet" type="text/css">
5     </head><body>
6     <!-- Generated by Doxygen 1.3.8 -->
7     <div class="qindex"> <form class="search" action="search.php" method="get">
8     <a class="qindex" href="main.html">Main&nbsp;Page</a> | <a class="qindex" href="annotated.html">Data&nbsp;Structures</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="functions.html">Data&nbsp;Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <span class="search"><u>S</u>earch&nbsp;for&nbsp;<input class="search" type="text" name="query" value="" size="20" accesskey="s"/></span></form></div>
9     <h1>dns.c</h1><a href="dns_8c.html">Go to the documentation of this file.</a><pre class="fragment"><div>00001 <span class="comment">/* libspf - Sender Policy Framework library</span>
10     00002 <span class="comment">*</span>
11     00003 <span class="comment">* ANSI C implementation of spf-draft-200405.txt</span>
12     00004 <span class="comment">*</span>
13     00005 <span class="comment">* Author: James Couzens &lt;jcouzens@codeshare.ca&gt;</span>
14     00006 <span class="comment">* Author: Sean Comeau &lt;scomeau@obscurity.org&gt;</span>
15     00007 <span class="comment">*</span>
16     00008 <span class="comment">* File: dns.c</span>
17     00009 <span class="comment">* Desc: DNS related functions</span>
18     00010 <span class="comment">*</span>
19     00011 <span class="comment">* License:</span>
20     00012 <span class="comment">*</span>
21     00013 <span class="comment">* The libspf Software License, Version 1.0</span>
22     00014 <span class="comment">*</span>
23     00015 <span class="comment">* Copyright (c) 2004 James Couzens &amp; Sean Comeau All rights</span>
24     00016 <span class="comment">* reserved.</span>
25     00017 <span class="comment">*</span>
26     00018 <span class="comment">* Redistribution and use in source and binary forms, with or without</span>
27     00019 <span class="comment">* modification, are permitted provided that the following conditions</span>
28     00020 <span class="comment">* are met:</span>
29     00021 <span class="comment">*</span>
30     00022 <span class="comment">* 1. Redistributions of source code must retain the above copyright</span>
31     00023 <span class="comment">* notice, this list of conditions and the following disclaimer.</span>
32     00024 <span class="comment">*</span>
33     00025 <span class="comment">* 2. Redistributions in binary form must reproduce the above copyright</span>
34     00026 <span class="comment">* notice, this list of conditions and the following disclaimer in</span>
35     00027 <span class="comment">* the documentation and/or other materials provided with the</span>
36     00028 <span class="comment">* distribution.</span>
37     00029 <span class="comment">*</span>
38     00030 <span class="comment">* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED</span>
39     00031 <span class="comment">* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES</span>
40     00032 <span class="comment">* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE</span>
41     00033 <span class="comment">* DISCLAIMED. IN NO EVENT SHALL THE AUTHORS MAKING USE OF THIS LICESEN</span>
42     00034 <span class="comment">* OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,</span>
43     00035 <span class="comment">* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT</span>
44     00036 <span class="comment">* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF</span>
45     00037 <span class="comment">* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND</span>
46     00038 <span class="comment">* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,</span>
47     00039 <span class="comment">* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT</span>
48     00040 <span class="comment">* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF</span>
49     00041 <span class="comment">* SUCH DAMAGE.</span>
50     00042 <span class="comment">*</span>
51     00043 <span class="comment">*/</span>
52     00044
53     00045
54     00046 <span class="preprocessor">#ifdef _WITH_PTHREADS</span>
55     00047 <span class="preprocessor"></span><span class="preprocessor">#include &lt;pthread.h&gt;</span> <span class="comment">/* pthread_mutex_t */</span>
56     00048 <span class="preprocessor"> #ifdef HAVE_GETHOSTBYNAME_R</span>
57     00049 <span class="preprocessor"></span><span class="preprocessor">#include &lt;errno.h&gt;</span> <span class="comment">/* errno */</span>
58     00050 <span class="preprocessor"> #endif </span><span class="comment">/* HAVE_GETHOSTBYNAME_R */</span>
59     00051 <span class="preprocessor">#endif </span><span class="comment">/* _WITH_PTHREADS */</span>
60     00052
61     00053 <span class="preprocessor">#include "<a class="code" href="spf_8h.html">spf.h</a>"</span> <span class="comment">/* SPF */</span>
62     00054 <span class="preprocessor">#include "<a class="code" href="util_8h.html">util.h</a>"</span> <span class="comment">/* Utility functions */</span>
63     00055 <span class="preprocessor">#include "<a class="code" href="dns_8h.html">dns.h</a>"</span> <span class="comment">/* our header */</span>
64     00056 <span class="preprocessor">#include "../../config.h"</span> <span class="comment">/* autoconf */</span>
65     00057
66     00058 <span class="preprocessor">#ifdef _WITH_PTHREADS</span>
67     00059 <span class="preprocessor"></span>
68     00060 <span class="comment">/* mutex for gethostbyname wrapper */</span>
69     00061 pthread_mutex_t <a class="code" href="dns_8c.html#a0">dns_mutex</a> = PTHREAD_MUTEX_INITIALIZER;
70     00062
71     00063 <span class="preprocessor"> #ifdef HAVE_GETHOSTBYNAME_R</span>
72     00064 <span class="preprocessor"></span>
73     00065 <span class="comment">/* used by gethostbyname_r */</span>
74     00066 <span class="keyword">extern</span> <span class="keywordtype">int</span> <a class="code" href="util_8c.html#a1">errno</a>;
75     00067
76     00068 <span class="preprocessor"> #endif </span><span class="comment">/* HAVE_GETHOSTBYNAME_R */</span>
77     00069 <span class="preprocessor">#else</span>
78     00070 <span class="preprocessor"></span>
79     00071 <span class="comment">/* mutex for gethostbyname dummy wrapper */</span>
80     <a name="l00072"></a><a class="code" href="dns_8c.html#a0">00072</a> <span class="keywordtype">void</span> *<a class="code" href="dns_8c.html#a0">dns_mutex</a>;
81     00073
82     00074 <span class="preprocessor">#endif </span><span class="comment">/* _WITH_PTHREADS */</span>
83     00075
84     00076
85     00202 <span class="comment">/* DNS_query</span>
86     00203 <span class="comment">*</span>
87     00204 <span class="comment">* Author: James Couzens &lt;jcouzens@codeshare.ca&gt;\n</span>
88     00205 <span class="comment">* Author: Travis Anderson &lt;tanderson@codeshare.ca&gt;\n</span>
89     00206 <span class="comment">*</span>
90     00207 <span class="comment">* Date: 12/10/03</span>
91     00208 <span class="comment">* Date: 02/20/04 - Added cache by Travis Anderson &lt;tanderson@codeshare.ca&gt;</span>
92     00209 <span class="comment">*</span>
93     00210 <span class="comment">* Desc:</span>
94     00211 <span class="comment">* Executes a DNS query of type T_TYPE and then calls the</span>
95     00212 <span class="comment">* appropriate answer parsing function based on that type. Returns</span>
96     00213 <span class="comment">* a pointer to allocated memory (a string of space delimited</span>
97     00214 <span class="comment">* records). Upon failure returns NULL.</span>
98     00215 <span class="comment">*</span>
99     00216 <span class="comment">*/</span>
100     <a name="l00217"></a><a class="code" href="dns_8h.html#a8">00217</a> <span class="keywordtype">char</span> *<a class="code" href="dns_8h.html#a8">DNS_query</a>(<a class="code" href="structpeer__info__s.html">peer_info_t</a> *p, <span class="keyword">const</span> <span class="keywordtype">char</span> *s, <span class="keyword">const</span> <span class="keywordtype">int</span> T_TYPE,
101     00218 <span class="keyword">const</span> <span class="keywordtype">char</span> *mta)
102     00219 {
103     00220 HEADER *hdr; <span class="comment">/* pointer to the header of the packet */</span>
104     00221
105     00222 int8_t ancount; <span class="comment">/* number of answers */</span>
106     00223
107     00224 int16_t r_len; <span class="comment">/* res_search return code &amp; packet len */</span>
108     00225 int16_t rc; <span class="comment">/* generic return code / length of */</span>
109     00226
110     00227 <span class="keywordtype">int</span> ttl; <span class="comment">/* answer TTL */</span>
111     00228
112     00229 <span class="keywordtype">char</span> buf[<a class="code" href="dns_8h.html#a2">SPF_MAXDNAME</a>]; <span class="comment">/* record extraction buffer */</span>
113     00230 <span class="keywordtype">char</span> answer[<a class="code" href="dns_8h.html#a1">SPF_PACKETSZ</a>]; <span class="comment">/* answer buffer */</span>
114     00231
115     00232 <span class="keywordtype">char</span> *rr_data = NULL; <span class="comment">/* record */</span>
116     00233
117     00234 u_char *msg_ptr; <span class="comment">/* pointer to beginning of the message */</span>
118     00235 u_char *eom_ptr; <span class="comment">/* pointer to the end of the message */</span>
119     00236 u_char *rd_ptr; <span class="comment">/* pointer to uncompressed message */</span>
120     00237
121     00238
122     00239 <span class="keywordflow">if</span> (s == NULL)
123     00240 {
124     00241 <a class="code" href="util_8h.html#a13">xepprintf</a>(<span class="stringliteral">"Passed a NULL char. Aborting.\n"</span>);
125     00242 <span class="keywordflow">return</span>(NULL);
126     00243 }
127     00244
128     00245 <a class="code" href="util_8h.html#a9">xprintf</a>(<span class="stringliteral">"Called with (%s) and type: %i\n"</span>, s, T_TYPE);
129     00246
130     00247 r_len = res_search(s, C_IN, T_TYPE, (u_char *)answer, <a class="code" href="dns_8h.html#a1">SPF_PACKETSZ</a>);
131     00248
132     00249 <span class="keywordflow">if</span> (r_len &lt;= 0)
133     00250 {
134     00251 <span class="keywordflow">switch</span> (<a class="code" href="dns_8h.html#a6">h_errno</a>)
135     00252 {
136     00253 <span class="keywordflow">case</span> HOST_NOT_FOUND:
137     00254 snprintf(p-&gt;<a class="code" href="structpeer__info__s.html#o24">error</a>, <a class="code" href="spf_8h.html#a16">SPF_MAX_ERROR</a>, <span class="stringliteral">"%s\n"</span>, hstrerror(<a class="code" href="dns_8h.html#a6">h_errno</a>));
138     00255 <a class="code" href="util_8h.html#a47">UTIL_assoc_prefix</a>(p, <a class="code" href="spf_8h.html#a88a52">SPF_NONE</a>, NULL);
139     00256 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"%s\n"</span>, p-&gt;<a class="code" href="structpeer__info__s.html#o24">error</a>);
140     00257
141     00258 <span class="keywordflow">return</span>(NULL);
142     00259
143     00260 <span class="keywordflow">case</span> TRY_AGAIN:
144     00261 snprintf(p-&gt;<a class="code" href="structpeer__info__s.html#o24">error</a>, <a class="code" href="spf_8h.html#a16">SPF_MAX_ERROR</a>, <span class="stringliteral">"%s\n"</span>, hstrerror(<a class="code" href="dns_8h.html#a6">h_errno</a>));
145     00262 <a class="code" href="util_8h.html#a47">UTIL_assoc_prefix</a>(p, <a class="code" href="spf_8h.html#a88a52">SPF_NONE</a>, NULL);
146     00263 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"%s\n"</span>, p-&gt;<a class="code" href="structpeer__info__s.html#o24">error</a>);
147     00264
148     00265 <span class="keywordflow">return</span>(NULL);
149     00266
150     00267 <span class="keywordflow">case</span> NO_RECOVERY:
151     00268 snprintf(p-&gt;<a class="code" href="structpeer__info__s.html#o24">error</a>, <a class="code" href="spf_8h.html#a16">SPF_MAX_ERROR</a>, <span class="stringliteral">"%s\n"</span>, hstrerror(<a class="code" href="dns_8h.html#a6">h_errno</a>));
152     00269 <a class="code" href="util_8h.html#a47">UTIL_assoc_prefix</a>(p, <a class="code" href="spf_8h.html#a88a55">SPF_ERROR</a>, NULL);
153     00270 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"%s\n"</span>, p-&gt;<a class="code" href="structpeer__info__s.html#o24">error</a>);
154     00271
155     00272 <span class="keywordflow">return</span>(NULL);
156     00273
157     00274 <span class="keywordflow">case</span> NO_DATA:
158     00275 snprintf(p-&gt;<a class="code" href="structpeer__info__s.html#o24">error</a>, <a class="code" href="spf_8h.html#a16">SPF_MAX_ERROR</a>, <span class="stringliteral">"%s\n"</span>, hstrerror(<a class="code" href="dns_8h.html#a6">h_errno</a>));
159     00276 <a class="code" href="util_8h.html#a47">UTIL_assoc_prefix</a>(p, <a class="code" href="spf_8h.html#a88a52">SPF_NONE</a>, NULL);
160     00277 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"%s\n"</span>, p-&gt;<a class="code" href="structpeer__info__s.html#o24">error</a>);
161     00278
162     00279 <span class="keywordflow">return</span>(NULL);
163     00280
164     00281 <span class="keywordflow">default</span>:
165     00282 snprintf(p-&gt;<a class="code" href="structpeer__info__s.html#o24">error</a>, <a class="code" href="spf_8h.html#a16">SPF_MAX_ERROR</a>, <span class="stringliteral">"%s\n"</span>, hstrerror(<a class="code" href="dns_8h.html#a6">h_errno</a>));
166     00283 <a class="code" href="util_8h.html#a47">UTIL_assoc_prefix</a>(p, <a class="code" href="spf_8h.html#a88a55">SPF_ERROR</a>, NULL);
167     00284 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"%s\n"</span>, p-&gt;<a class="code" href="structpeer__info__s.html#o24">error</a>);
168     00285
169     00286 <span class="keywordflow">return</span>(NULL);
170     00287 } <span class="comment">/* switch */</span>
171     00288 }
172     00289
173     00290 hdr = (HEADER *)&amp;answer;
174     00291 ancount = ntohs(hdr-&gt;ancount);
175     00292
176     00293 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Received packet size of %i bytes which contains %i answers.\n"</span>,
177     00294 r_len, ancount);
178     00295
179     00296 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"ANSWERS: %i\n"</span>, ancount);
180     00297 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"QUESTIONS: %i\n"</span>, ntohs(hdr-&gt;qdcount));
181     00298
182     00299 <span class="keywordflow">if</span> (ancount &gt; 0)
183     00300 {
184     00301 msg_ptr = (u_char *)&amp;answer; <span class="comment">/* point to start of message */</span>
185     00302 eom_ptr = (u_char *)&amp;answer + r_len; <span class="comment">/* point to end of message */</span>
186     00303 rd_ptr = (u_char *)&amp;answer + HFIXEDSZ; <span class="comment">/* point to start of RDATA */</span>
187     00304
188     00305 <span class="keywordflow">if</span> ((rc = dn_skipname(rd_ptr, eom_ptr)) &lt; 0)
189     00306 {
190     00307 <a class="code" href="util_8h.html#a13">xepprintf</a>(<span class="stringliteral">"Error obtaining QUESTION!\n"</span>);
191     00308
192     00309 <span class="keywordflow">return</span>(NULL);
193     00310 }
194     00311
195     00312 rd_ptr += rc + QFIXEDSZ; <span class="comment">/* jump to start of ANSWER */</span>
196     00313
197     00314 <span class="keywordflow">switch</span> (T_TYPE)
198     00315 {
199     00316 <span class="comment">/*</span>
200     00317 <span class="comment"> * T_A: A 'a' or address record used to associate an IP address with any</span>
201     00318 <span class="comment"> * given hostname.</span>
202     00319 <span class="comment"> */</span>
203     00320 <span class="keywordflow">case</span> T_A:
204     00321 <span class="keywordflow">return</span>((<span class="keywordtype">char</span> *)<a class="code" href="spf_8h.html#a87a50">SPF_TRUE</a>);
205     00322
206     00323 <span class="comment">/*</span>
207     00324 <span class="comment"> * T_TXT: A 'txt' or TEXT record which is what is currently used to store</span>
208     00325 <span class="comment"> * the SPF records within DNS. Its unfortunate that this had to be done</span>
209     00326 <span class="comment"> * because you'll discover that I had to make concessions to deal with</span>
210     00327 <span class="comment"> * the fact that many people publish more than just SPF records using</span>
211     00328 <span class="comment"> * the T_TXT record type, especially considering it was seeing use long</span>
212     00329 <span class="comment"> * before SPF ever came on the scene. Hopefully this will change in the</span>
213     00330 <span class="comment"> * near future.</span>
214     00331 <span class="comment"> */</span>
215     00332 <span class="keywordflow">case</span> T_TXT:
216     00333 <span class="keywordflow">if</span> ((rr_data = <a class="code" href="dns_8h.html#a9">DNS_txt_answer</a>(ancount, (u_char *)msg_ptr, (u_char *)eom_ptr,
217     00334 (u_char *)rd_ptr, buf, &amp;ttl)) == NULL)
218     00335 {
219     00336 <span class="keywordflow">return</span>(NULL);
220     00337 }
221     00338 <span class="keywordflow">break</span>;
222     00339
223     00340 <span class="comment">/*</span>
224     00341 <span class="comment"> * T_MX: A 'mx' or Mail Exchanger (Server) record identifies an authorative</span>
225     00342 <span class="comment"> * mail server (or in multiples server(s)) which may be used to deliver</span>
226     00343 <span class="comment"> * e-mail to a given domain.</span>
227     00344 <span class="comment"> */</span>
228     00345 <span class="keywordflow">case</span> T_MX:
229     00346 <span class="keywordflow">if</span> ((rr_data = <a class="code" href="dns_8h.html#a10">DNS_mx_answer</a>(ancount, (u_char *)msg_ptr, (u_char *)eom_ptr,
230     00347 (u_char *)rd_ptr, buf, &amp;ttl)) == NULL)
231     00348 {
232     00349 <span class="keywordflow">return</span>(NULL);
233     00350 }
234     00351 <span class="keywordflow">break</span>;
235     00352
236     00353 <span class="comment">/*</span>
237     00354 <span class="comment"> * T_PTR: A 'ptr' DNS record contains the reverse address for a</span>
238     00355 <span class="comment"> * given hostname in the format x.x.x.x-in-addr.arpa in the</span>
239     00356 <span class="comment"> * case of IPv4.</span>
240     00357 <span class="comment"> */</span>
241     00358 <span class="keywordflow">case</span> T_PTR:
242     00359
243     00360 <span class="comment">/*</span>
244     00361 <span class="comment"> * Comment by: James Couzens &lt;jcouzens@codeshare.ca&gt;</span>
245     00362 <span class="comment"> * Date: 01/04/04</span>
246     00363 <span class="comment"> *</span>
247     00364 <span class="comment"> * DNS_ptr_answer doesn't allocate any memory and returns SPF_TRUE or</span>
248     00365 <span class="comment"> * SPF_FALSE, however this function returns a char so a the boolean </span>
249     00366 <span class="comment"> * value of SPF_TRUE/FALSE is cast to avoid writing a handler for </span>
250     00367 <span class="comment"> * this one specific instance. I might change this in the future by</span>
251     00368 <span class="comment"> * either altering its return type or something else but for now this</span>
252     00369 <span class="comment"> * seems to do nicely.</span>
253     00370 <span class="comment"> */</span>
254     00371 <span class="keywordflow">if</span> (<a class="code" href="dns_8h.html#a11">DNS_ptr_answer</a>(p, ancount, (u_char *)msg_ptr, (u_char *)eom_ptr,
255     00372 (u_char *)rd_ptr, buf, mta, &amp;ttl) == <a class="code" href="spf_8h.html#a87a49">SPF_FALSE</a>)
256     00373 {
257     00374 <span class="keywordflow">return</span>((<span class="keywordtype">char</span> *)<a class="code" href="spf_8h.html#a87a49">SPF_FALSE</a>);
258     00375 }
259     00376 <span class="keywordflow">else</span>
260     00377 {
261     00378 <span class="keywordflow">return</span>((<span class="keywordtype">char</span> *)<a class="code" href="spf_8h.html#a87a50">SPF_TRUE</a>);
262     00379 }
263     00380 <span class="keywordflow">break</span>;
264     00381
265     00382 <span class="comment">/*</span>
266     00383 <span class="comment"> * T_CNAME: A 'cname' or an alias record for a DNS forward or reverse</span>
267     00384 <span class="comment"> * record which has been assigned more than one value.</span>
268     00385 <span class="comment"> */</span>
269     00386 <span class="keywordflow">case</span> T_CNAME:
270     00387 <span class="keywordflow">if</span> ((rr_data = <a class="code" href="dns_8h.html#a12">DNS_cname_answer</a>(ancount, (u_char *)msg_ptr, (u_char *)eom_ptr,
271     00388 (u_char *)rd_ptr, buf, &amp;ttl)) == NULL)
272     00389 {
273     00390 <span class="keywordflow">return</span>(NULL);
274     00391 }
275     00392 <span class="keywordflow">break</span>;
276     00393 <span class="keywordflow">default</span>:
277     00394 <span class="keywordflow">break</span>;
278     00395 }
279     00396 <span class="keywordflow">return</span>(rr_data);
280     00397 }
281     00398
282     00399 <span class="keywordflow">return</span>(NULL);
283     00400 }
284     00401
285     00402
286     00403 <span class="comment">/* DNS_txt_answer</span>
287     00404 <span class="comment">*</span>
288     00405 <span class="comment">* Author: James Couzens &lt;jcouzens@codeshare.ca&gt;</span>
289     00406 <span class="comment">*</span>
290     00407 <span class="comment">* Date: 01/02/04</span>
291     00408 <span class="comment">* Date: 02/23/04 - Bugfix from Albert Weichselbraun &lt;albert@atnet.at&gt;</span>
292     00409 <span class="comment">* Date: 02/20/04 - Added cache by Travis Anderson &lt;tanderson@codeshare.ca&gt;</span>
293     00410 <span class="comment">*</span>
294     00411 <span class="comment">* Desc:</span>
295     00412 <span class="comment">* SPF_PACKETSZ bytes are allocated and then filled with \0 chars.</span>
296     00413 <span class="comment">* This buffer is then used in a TXT DNS query using data from the passed</span>
297     00414 <span class="comment">* peer_info_t structure. Upon success this buffer is re-cast as a char *</span>
298     00415 <span class="comment">* and then a pointer to this memory is returned. Upon failure a NULL</span>
299     00416 <span class="comment">* pointer is returned.</span>
300     00417 <span class="comment">*</span>
301     00418 <span class="comment">*/</span>
302     <a name="l00419"></a><a class="code" href="dns_8h.html#a9">00419</a> <span class="keywordtype">char</span> *<a class="code" href="dns_8h.html#a9">DNS_txt_answer</a>(int16_t ancount, <span class="keyword">const</span> u_char *msg_ptr,
303     00420 <span class="keyword">const</span> u_char *eom_ptr, u_char *rd_ptr, <span class="keywordtype">char</span> *buf, <span class="keywordtype">int</span> *ttl)
304     00421 {
305     00422 int16_t i; <span class="comment">/* utility */</span>
306     00423 int16_t j; <span class="comment">/* utility */</span>
307     00424 int16_t rc; <span class="comment">/* generic return code / length of */</span>
308     00425 int16_t rd_type; <span class="comment">/* answer type */</span>
309     00426 int16_t rd_len; <span class="comment">/* res_search return code &amp; packet len */</span>
310     00427
311     00428 int32_t rd_ttl; <span class="comment">/* TTL */</span>
312     00429
313     00430 <span class="keywordtype">char</span> *rr_data = NULL; <span class="comment">/* data pointer */</span>
314     00431 <span class="keywordtype">char</span> *r_buf = NULL; <span class="comment">/* return buffer */</span>
315     00432 <span class="keywordtype">char</span> *pos = NULL; <span class="comment">/* utility pointer */</span>
316     00433
317     00434
318     00435 <span class="keywordflow">if</span> ((msg_ptr == NULL) || (eom_ptr == NULL) ||
319     00436 (rd_ptr == NULL) || (buf == NULL))
320     00437 {
321     00438 <a class="code" href="util_8h.html#a13">xepprintf</a>(<span class="stringliteral">"Called with NULL pointers\n"</span>);
322     00439 <span class="keywordflow">return</span>(NULL);
323     00440 }
324     00441
325     00442 <a class="code" href="util_8h.html#a11">xpprintf</a>(<span class="stringliteral">"entering function\n"</span>);
326     00443
327     00444 i = 0;
328     00445 j = ancount;
329     00446 <span class="keywordflow">while</span> ((ancount &gt; 0) &amp;&amp; (rd_ptr &lt; eom_ptr))
330     00447 {
331     00448 <span class="keywordflow">if</span> ((rc = dn_expand(msg_ptr, eom_ptr, rd_ptr, buf, <a class="code" href="dns_8h.html#a3">SPF_MAXCDNAME</a>)) &lt; 0)
332     00449 {
333     00450 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Unable to expand T_TXT response packet!; Reason: %s\n"</span>,
334     00451 hstrerror(<a class="code" href="dns_8h.html#a6">h_errno</a>));
335     00452
336     00453 <span class="keywordflow">if</span> (rr_data != NULL)
337     00454 {
338     00455 <a class="code" href="util_8h.html#a6">xfree</a>(rr_data);
339     00456 }
340     00457
341     00458 <span class="keywordflow">return</span>(NULL);
342     00459 }
343     00460
344     00461 <span class="comment">/* dname, type, class, TTL, rdata len, rdata */</span>
345     00462 rd_ptr += rc; <span class="comment">/* jump to start of ANSWER data */</span>
346     00463 GETSHORT(rd_type, rd_ptr); <span class="comment">/* get response type */</span>
347     00464 rd_ptr += INT16SZ; <span class="comment">/* skip class */</span>
348     00465 GETLONG(rd_ttl, rd_ptr); <span class="comment">/* get TTL */</span>
349     00466 GETSHORT(rd_len, rd_ptr); <span class="comment">/* get data length */</span>
350     00467
351     00468 *ttl = rd_ttl; <span class="comment">/* TTL working pointer */</span>
352     00469
353     00470 <span class="keywordflow">if</span> (rd_type != T_TXT)
354     00471 {
355     00472 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Ignoring record not of T_TXT type. (%i)\n"</span>, rd_type);
356     00473 rd_ptr += rd_len;
357     00474 <span class="keywordflow">continue</span>;
358     00475 }
359     00476
360     00477 rd_ptr++; <span class="comment">/* skip byte */</span>
361     00478
362     00479 rd_ptr[rd_len] = <span class="charliteral">'\0'</span>;
363     00480 rd_ptr[rd_len - 1] = <span class="charliteral">' '</span>;
364     00481
365     00482 i += rd_len;
366     00483
367     00484 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Answer %i has length %i. (%i)\n"</span>, ancount, rd_len, i);
368     00485
369     00486 <span class="comment">/*</span>
370     00487 <span class="comment"> * Only received one answer, so this MUST start with v=spf1 or its</span>
371     00488 <span class="comment"> * not a valid SPFv1 record</span>
372     00489 <span class="comment"> */</span>
373     00490 <span class="keywordflow">if</span> ((j == 1) &amp;&amp; ((*rd_ptr != <span class="charliteral">'v'</span>) &amp;&amp; (*(rd_ptr + 1) != <span class="charliteral">'='</span>)))
374     00491 {
375     00492 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"INVALID Answer Data: (%s) len: %i\n"</span>, rd_ptr, rd_len);
376     00493
377     00494 <span class="keywordflow">if</span> (rr_data != NULL)
378     00495 {
379     00496 <a class="code" href="util_8h.html#a6">xfree</a>(rr_data);
380     00497 }
381     00498
382     00499 <span class="keywordflow">return</span>(NULL);
383     00500 }
384     00501
385     00502 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Answer Data: (%s) len: %i\n"</span>, rd_ptr, rd_len);
386     00503
387     00504 <span class="keywordflow">if</span> ((rd_len &lt;= SPF_MAXDNAME) &amp;&amp; (rd_len &gt; 0))
388     00505 {
389     00506 <span class="keywordflow">if</span> (rr_data == NULL)
390     00507 {
391     00508 rr_data = <a class="code" href="util_8h.html#a4">xmalloc</a>(i + 1);
392     00509 }
393     00510 <span class="keywordflow">else</span>
394     00511 {
395     00512 rr_data = <a class="code" href="util_8h.html#a5">xrealloc</a>(rr_data, (i + 1));
396     00513 }
397     00514
398     00515 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"REALLOCATE memory: %i bytes\n"</span>, i);
399     00516
400     00517 strncat(rr_data, (<span class="keywordtype">char</span> *)rd_ptr, rd_len);
401     00518 rr_data[i - 1] = <span class="charliteral">' '</span>;
402     00519 rr_data[i] = <span class="charliteral">'\0'</span>;
403     00520 }
404     00521
405     00522 rd_ptr += rd_len;
406     00523 ancount--;
407     00524 }
408     00525
409     00526 <span class="keywordflow">if</span> (rr_data == NULL)
410     00527 {
411     00528 <a class="code" href="util_8h.html#a11">xpprintf</a>(<span class="stringliteral">"rr_data is NULL (no ANSWERS in packet(s)), returning\n"</span>);
412     00529
413     00530 <span class="keywordflow">return</span>(NULL);
414     00531 }
415     00532
416     00533 rr_data[i] = <span class="charliteral">'\0'</span>;
417     00534
418     00535 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"RR_DATA: (%s)\n"</span>, rr_data);
419     00536
420     00537 <span class="comment">/*</span>
421     00538 <span class="comment"> * Comment: James Couzens &lt;jcouzens@codeshare.ca&gt;</span>
422     00539 <span class="comment"> *</span>
423     00540 <span class="comment"> * Date: 09/08/04</span>
424     00541 <span class="comment"> *</span>
425     00542 <span class="comment"> * if: see if the string STARTS with v=spf1 </span>
426     00543 <span class="comment"> * else: see if the string CONTAINS v=spf1 </span>
427     00544 <span class="comment"> *</span>
428     00545 <span class="comment"> * Whats all this?! This is because of the way DNS packets return -- that is</span>
429     00546 <span class="comment"> * in random order. This is of consequence because of the wording in the RFC.</span>
430     00547 <span class="comment"> * That wording is that the version identifier may only appear once withing any</span>
431     00548 <span class="comment"> * given SPF record (this makes sense) and that should it appear twice, this</span>
432     00549 <span class="comment"> * is considered a violation (also makes sense). The point here is that as a</span>
433     00550 <span class="comment"> * result of records returning out of order (then how they were declared in</span>
434     00551 <span class="comment"> * the server's configuration that they were requested from) and how this </span>
435     00552 <span class="comment"> * particular implementation deals with the DNS packets (they are concatenated</span>
436     00553 <span class="comment"> * in the order in which they were received) the following code facilitates an</span>
437     00554 <span class="comment"> * early pre-parse check to see if its even a valid SPF record.</span>
438     00555 <span class="comment"> *</span>
439     00556 <span class="comment"> */</span>
440     00557 <span class="keywordflow">if</span> ((*rr_data == <span class="charliteral">'v'</span>) &amp;&amp;
441     00558 (*(rr_data + 1) == <span class="charliteral">'='</span>) &amp;&amp;
442     00559 (*(rr_data + 2) == <span class="charliteral">'s'</span>) &amp;&amp;
443     00560 (*(rr_data + 3) == <span class="charliteral">'p'</span>) &amp;&amp;
444     00561 (*(rr_data + 4) == <span class="charliteral">'f'</span>) &amp;&amp;
445     00562 (*(rr_data + 5) == <span class="charliteral">'1'</span>))
446     00563 {
447     00564 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Returning with valid SPFv1 record: (%s)\n"</span>, rr_data);
448     00565
449     00566 <span class="keywordflow">return</span>(rr_data);
450     00567 }
451     00568 <span class="keywordflow">else</span>
452     00569 {
453     00570 <span class="comment">/* search the buffer for a valid SPFv1 version mechanism */</span>
454     00571 <span class="keywordflow">if</span> ((pos = strstr(rr_data, <span class="stringliteral">"v=spf1"</span>)) != NULL)
455     00572 {
456     00573 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Found SPFv1 version mechanism: (%s)\n"</span>, pos);
457     00574
458     00575 r_buf = <a class="code" href="util_8h.html#a8">xstrndup</a>(pos, <a class="code" href="spf_8h.html#a5">SPF_MAX_STR</a>);
459     00576 r_buf[strlen(pos)] = <span class="charliteral">'\0'</span>;
460     00577
461     00578 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Old buffer: (%s)\n"</span>, rr_data);
462     00579 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"New buffer: (%s)\n"</span>, r_buf);
463     00580
464     00581 pos = NULL;
465     00582 }
466     00583
467     00584 <span class="comment">/* no valid SPFv1 record here */</span>
468     00585 <a class="code" href="util_8h.html#a6">xfree</a>(rr_data);
469     00586
470     00587 <span class="keywordflow">return</span>(r_buf);
471     00588 }
472     00589
473     00590 <a class="code" href="util_8h.html#a11">xpprintf</a>(<span class="stringliteral">"Returning NULL (not a valid SPF TXT record)\n"</span>);
474     00591
475     00592 <span class="keywordflow">return</span>(NULL);
476     00593 }
477     00594
478     00595
479     00596 <span class="comment">/* DNS_mx_answer</span>
480     00597 <span class="comment">*</span>
481     00598 <span class="comment">* Author: James Couzens &lt;jcouzens@codeshare.ca&gt;</span>
482     00599 <span class="comment">*</span>
483     00600 <span class="comment">* Date: 01/02/04</span>
484     00601 <span class="comment">* Date: 02/20/04 - Added cache by Travis Anderson &lt;tanderson@codeshare.ca&gt;</span>
485     00602 <span class="comment">*</span>
486     00603 <span class="comment">* Desc:</span>
487     00604 <span class="comment">* SPF_PACKETSZ bytes are allocated and then filled with \0 chars.</span>
488     00605 <span class="comment">* This buffer is then used in an MX DNS query using data from the passed</span>
489     00606 <span class="comment">* peer_info_t structure. Upon success this buffer is re-cast as a char *</span>
490     00607 <span class="comment">* and then a pointer to this memory is returned. Upon failure a NULL</span>
491     00608 <span class="comment">* pointer is returned.</span>
492     00609 <span class="comment">*</span>
493     00610 <span class="comment">*/</span>
494     <a name="l00611"></a><a class="code" href="dns_8h.html#a10">00611</a> <span class="keywordtype">char</span> *<a class="code" href="dns_8h.html#a10">DNS_mx_answer</a>(int16_t ancount, <span class="keyword">const</span> u_char *msg_ptr,
495     00612 <span class="keyword">const</span> u_char *eom_ptr, u_char *rd_ptr, <span class="keywordtype">char</span> *buf, <span class="keywordtype">int</span> *ttl)
496     00613 {
497     00614 size_t buf_len; <span class="comment">/* buffer length */</span>
498     00615
499     00616 int16_t i; <span class="comment">/* utility */</span>
500     00617 int16_t rc; <span class="comment">/* generic return code / length of */</span>
501     00618 int16_t rd_pref; <span class="comment">/* MX preference */</span>
502     00619 int16_t rd_type; <span class="comment">/* answer type */</span>
503     00620 int16_t rd_len = 0; <span class="comment">/* res_search return code &amp; packet len */</span>
504     00621
505     00622 int32_t rd_ttl; <span class="comment">/* TTL */</span>
506     00623
507     00624 <span class="keywordtype">char</span> *rr_data = NULL; <span class="comment">/* data pointer */</span>
508     00625
509     00626
510     00627 i = 0;
511     00628 <span class="keywordflow">while</span> ((ancount &gt; 0) &amp;&amp; (rd_ptr &lt; eom_ptr))
512     00629 {
513     00630 <span class="keywordflow">if</span> ((rc = dn_expand(msg_ptr, eom_ptr, rd_ptr, buf, <a class="code" href="dns_8h.html#a3">SPF_MAXCDNAME</a>)) &lt; 0)
514     00631 {
515     00632 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Error expanding ANSWER packet at count %i; Reason: %s \n"</span>,
516     00633 ancount, hstrerror(<a class="code" href="dns_8h.html#a6">h_errno</a>));
517     00634
518     00635 <span class="keywordflow">return</span>(NULL);
519     00636 }
520     00637
521     00638 <span class="comment">/* dname, type, class, TTL, rdata len, rdata */</span>
522     00639 rd_ptr += rc; <span class="comment">/* jump to start of ANSWER data */</span>
523     00640 GETSHORT(rd_type, rd_ptr); <span class="comment">/* get response type */</span>
524     00641 rd_ptr += INT16SZ; <span class="comment">/* skip class */</span>
525     00642 GETLONG(rd_ttl, rd_ptr); <span class="comment">/* get TTL */</span>
526     00643 GETSHORT(rd_len, rd_ptr); <span class="comment">/* get data length */</span>
527     00644
528     00645 *ttl = rd_ttl;
529     00646
530     00647 <span class="keywordflow">if</span> (rd_type != T_MX)
531     00648 {
532     00649 <a class="code" href="util_8h.html#a9">xprintf</a>(<span class="stringliteral">"Forged packet?! We requested T_MX (15) but got %i\n"</span>, rd_type);
533     00650 rd_ptr += rd_len;
534     00651 <span class="keywordflow">continue</span>;
535     00652 }
536     00653
537     00654 GETSHORT(rd_pref, rd_ptr); <span class="comment">/* get MX preference */</span>
538     00655
539     00656 <span class="keywordflow">if</span> ((rc = dn_expand(msg_ptr, eom_ptr, rd_ptr, buf, <a class="code" href="dns_8h.html#a3">SPF_MAXCDNAME</a>)) &lt; 0)
540     00657 {
541     00658 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Error expanding ANSWER packet at count %i; Reason: %s \n"</span>,
542     00659 ancount, hstrerror(<a class="code" href="dns_8h.html#a6">h_errno</a>));
543     00660
544     00661 <span class="keywordflow">return</span>(NULL);
545     00662 }
546     00663
547     00664 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"MX: %s Preference: %i\n"</span>, buf, rd_pref);
548     00665
549     00666 buf_len = strlen(buf);
550     00667 i += (buf_len + 1);
551     00668
552     00669 <span class="keywordflow">if</span> ((rd_len &lt;= SPF_MAXDNAME) &amp;&amp; (rd_len &gt; 0))
553     00670 {
554     00671 <span class="keywordflow">if</span> (rr_data == NULL)
555     00672 {
556     00673 rr_data = <a class="code" href="util_8h.html#a4">xmalloc</a>(i + 1);
557     00674 <span class="comment">/*memset(rr_data, '\0', (i + 1));*/</span>
558     00675 }
559     00676 <span class="keywordflow">else</span>
560     00677 {
561     00678 rr_data = <a class="code" href="util_8h.html#a5">xrealloc</a>(rr_data, (i + 1));
562     00679 }
563     00680
564     00681 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"REALLOCATE memory: %i bytes\n"</span>, (i + 1));
565     00682
566     00683 strncat(rr_data, buf, buf_len);
567     00684 rr_data[i - 1] = <span class="charliteral">' '</span>;
568     00685 rr_data[i] = <span class="charliteral">'\0'</span>;
569     00686 }
570     00687
571     00688 rd_ptr += rc;
572     00689 ancount--;
573     00690 }
574     00691
575     00692 <span class="keywordflow">if</span> (rr_data != NULL)
576     00693 {
577     00694 rr_data[i - 1] = <span class="charliteral">'\0'</span>;
578     00695 }
579     00696
580     00697 <span class="keywordflow">return</span>(rr_data);
581     00698 }
582     00699
583     00700
584     00701 <span class="comment">/* DNS_ptr_answer</span>
585     00702 <span class="comment">*</span>
586     00703 <span class="comment">* Author: James Couzens &lt;jcouzens@codeshare.ca&gt;</span>
587     00704 <span class="comment">*</span>
588     00705 <span class="comment">* Date: 01/02/04</span>
589     00706 <span class="comment">* Date: 02/20/04 - Added cache by Travis Anderson &lt;tanderson@codeshare.ca&gt;</span>
590     00707 <span class="comment">*</span>
591     00708 <span class="comment">* Desc:</span>
592     00709 <span class="comment">* A reverse lookup on an IP address leads to a lookup per returned</span>
593     00710 <span class="comment">* PTR answer to see if the returned answer matches. The forward lookups are</span>
594     00711 <span class="comment">* handled by a separate function which calls gethostbyname. Upon a single</span>
595     00712 <span class="comment">* successful match of a forward lookup with a reverse lookup, returns SPF_TRUE.</span>
596     00713 <span class="comment">* Returns SPF_FALSE upon failure.</span>
597     00714 <span class="comment">*</span>
598     00715 <span class="comment">*/</span>
599     <a name="l00716"></a><a class="code" href="dns_8h.html#a11">00716</a> <a class="code" href="spf_8h.html#a35">SPF_BOOL</a> <a class="code" href="dns_8h.html#a11">DNS_ptr_answer</a>(<a class="code" href="structpeer__info__s.html">peer_info_t</a> *p, int16_t ancount,
600     00717 <span class="keyword">const</span> u_char *msg_ptr, <span class="keyword">const</span> u_char *eom_ptr, u_char *rd_ptr, <span class="keywordtype">char</span> *buf,
601     00718 <span class="keyword">const</span> <span class="keywordtype">char</span> *mta, <span class="keywordtype">int</span> *ttl)
602     00719 {
603     00720 int16_t rc; <span class="comment">/* generic return code / length of */</span>
604     00721 int16_t rd_type; <span class="comment">/* answer type */</span>
605     00722 int16_t rd_len = 0; <span class="comment">/* res_search return code &amp; packet len */</span>
606     00723
607     00724 int32_t rd_ttl; <span class="comment">/* TTL */</span>
608     00725
609     00726 <span class="keywordtype">char</span> *buf_cmp = NULL; <span class="comment">/* stores domain from buf's (ptr res) hostname */</span>
610     00727 <span class="keywordtype">char</span> *mta_cmp = NULL; <span class="comment">/* stores domain from rpeer's hostname */</span>
611     00728
612     00729
613     00730 <span class="keywordflow">while</span> ((ancount &gt; 0) &amp;&amp; (rd_ptr &lt; eom_ptr))
614     00731 {
615     00732 <span class="keywordflow">if</span> ((rc = dn_expand(msg_ptr, eom_ptr, rd_ptr, buf, <a class="code" href="dns_8h.html#a3">SPF_MAXCDNAME</a>)) &lt; 0)
616     00733 {
617     00734 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Error expanding ANSWER packet at count %i; Reason: %s \n"</span>,
618     00735 ancount, hstrerror(<a class="code" href="dns_8h.html#a6">h_errno</a>));
619     00736
620     00737 <span class="keywordflow">return</span>(<a class="code" href="spf_8h.html#a87a49">SPF_FALSE</a>);
621     00738 }
622     00739
623     00740 <span class="comment">/* dname, type, class, TTL, rdata len, rdata */</span>
624     00741 rd_ptr += rc; <span class="comment">/* jump to start of ANSWER data */</span>
625     00742 GETSHORT(rd_type, rd_ptr); <span class="comment">/* get response type */</span>
626     00743 rd_ptr += INT16SZ; <span class="comment">/* skip class */</span>
627     00744 GETLONG(rd_ttl, rd_ptr); <span class="comment">/* get TTL */</span>
628     00745 GETSHORT(rd_len, rd_ptr); <span class="comment">/* get data length */</span>
629     00746
630     00747 *ttl = rd_ttl;
631     00748
632     00749 <span class="keywordflow">if</span> (rd_type != T_PTR)
633     00750 {
634     00751 rc = dn_expand(msg_ptr, eom_ptr, rd_ptr, buf, <a class="code" href="dns_8h.html#a3">SPF_MAXCDNAME</a>);
635     00752
636     00753 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Error expanding ANSWER packet at count %i; Reason: %s \n"</span>,
637     00754 ancount, hstrerror(<a class="code" href="dns_8h.html#a6">h_errno</a>));
638     00755
639     00756 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Got answer to type %i (%s) when we asked for T_PTR (%i)\n"</span>,
640     00757 rd_type, buf, T_PTR);
641     00758
642     00759 rd_ptr += rd_len;
643     00760 <span class="keywordflow">continue</span>;
644     00761 }
645     00762
646     00763 <span class="keywordflow">if</span> ((rc = dn_expand(msg_ptr, eom_ptr, rd_ptr, buf, <a class="code" href="dns_8h.html#a3">SPF_MAXCDNAME</a>)) &lt; 0)
647     00764 {
648     00765 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Error expanding ANSWER packet at count %i; Reason: %s \n"</span>,
649     00766 ancount, hstrerror(<a class="code" href="dns_8h.html#a6">h_errno</a>));
650     00767
651     00768 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Error expanding ANSWER packet at count %i. (%s)\n"</span>,
652     00769 ancount, buf);
653     00770
654     00771 <span class="keywordflow">return</span>(<a class="code" href="spf_8h.html#a87a49">SPF_FALSE</a>);
655     00772 }
656     00773
657     00774 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Answer %i has length %i.\n"</span>, ancount, rd_len);
658     00775 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Answer data (buffer): (%s); buffer length: %i\n"</span>,
659     00776 buf, strlen(buf));
660     00777
661     00778 <span class="keywordflow">if</span> ((rd_len &lt;= SPF_MAXDNAME) &amp;&amp; (rd_len &gt; 0))
662     00779 {
663     00780 <span class="keywordflow">if</span> (<a class="code" href="util_8h.html#a54">UTIL_validate_hostname</a>(p, buf, 32) == <a class="code" href="spf_8h.html#a87a49">SPF_FALSE</a>)
664     00781 {
665     00782 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Unable to validate hostname (%s) with (%s)\n"</span>,
666     00783 buf, mta);
667     00784
668     00785 rd_ptr += rc;
669     00786 ancount--;
670     00787 <span class="keywordflow">continue</span>;
671     00788 }
672     00789
673     00790 <span class="comment">/* buf MUST be a subdomain of mta */</span>
674     00791 <span class="keywordflow">if</span> (strlen(buf) &lt; strlen(mta))
675     00792 {
676     00793 <span class="comment">/* not a chance */</span>
677     00794 rd_ptr += rc;
678     00795 ancount--;
679     00796 <span class="keywordflow">continue</span>;
680     00797 }
681     00798 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (strlen(buf) == strlen(mta))
682     00799 {
683     00800 <span class="keywordflow">if</span> (strcasecmp(buf, mta) == 0)
684     00801 {
685     00802 <span class="keywordflow">return</span> (<a class="code" href="spf_8h.html#a87a50">SPF_TRUE</a>);
686     00803 }
687     00804 <span class="keywordflow">else</span>
688     00805 {
689     00806 rd_ptr += rc;
690     00807 ancount--;
691     00808 <span class="keywordflow">continue</span>;
692     00809 }
693     00810 }
694     00811 <span class="keywordflow">else</span>
695     00812 {
696     00813 buf_cmp = &amp;(buf[strlen(buf) - 1]);
697     00814 mta_cmp = (<span class="keywordtype">char</span> *)&amp;(mta[strlen(mta) - 1]);
698     00815
699     00816 <span class="keywordflow">while</span> (mta_cmp != mta - 1)
700     00817 {
701     00818 <span class="keywordflow">if</span> (*mta_cmp-- != *buf_cmp--)
702     00819 {
703     00820 rd_ptr += rc;
704     00821 ancount--;
705     00822 <span class="keywordflow">continue</span>;
706     00823 }
707     00824 }
708     00825
709     00826 <span class="keywordflow">if</span> (*buf_cmp == <span class="charliteral">'.'</span>)
710     00827 {
711     00828 <span class="keywordflow">return</span> (<a class="code" href="spf_8h.html#a87a50">SPF_TRUE</a>);
712     00829 }
713     00830 <span class="keywordflow">else</span>
714     00831 {
715     00832 rd_ptr += rc;
716     00833 ancount--;
717     00834 <span class="keywordflow">continue</span>;
718     00835 }
719     00836 }
720     00837 }
721     00838 <span class="keywordflow">else</span>
722     00839 {
723     00840 <a class="code" href="util_8h.html#a13">xepprintf</a>(<span class="stringliteral">"Answer length is too long!\n"</span>);
724     00841 }
725     00842
726     00843 rd_ptr += rc;
727     00844 ancount--;
728     00845 }
729     00846
730     00847 <span class="keywordflow">return</span>(<a class="code" href="spf_8h.html#a87a49">SPF_FALSE</a>);
731     00848 }
732     00849
733     00850
734     00851 <span class="comment">/* DNS_cname_answer</span>
735     00852 <span class="comment">*</span>
736     00853 <span class="comment">* Author: Teddy &lt;teddy@teddy.ch&gt;</span>
737     00854 <span class="comment">*</span>
738     00855 <span class="comment">* Date: 29/04/04</span>
739     00856 <span class="comment">* Date: 02/20/04 - Added cache by Travis Anderson &lt;tanderson@codeshare.ca&gt;</span>
740     00857 <span class="comment">*</span>
741     00858 <span class="comment">* Desc:</span>
742     00859 <span class="comment">* SPF_PACKETSZ bytes are allocated and then filled with \0 chars.</span>
743     00860 <span class="comment">* This buffer is then used in a TXT DNS query using data from the passed</span>
744     00861 <span class="comment">* peer_info_t structure. Upon success this buffer is re-cast as a char *</span>
745     00862 <span class="comment">* and then a pointer to this memory is returned. Upon failure a NULL</span>
746     00863 <span class="comment">* pointer is returned.</span>
747     00864 <span class="comment">*</span>
748     00865 <span class="comment">*/</span>
749     <a name="l00866"></a><a class="code" href="dns_8h.html#a12">00866</a> <span class="keywordtype">char</span> *<a class="code" href="dns_8h.html#a12">DNS_cname_answer</a>(int16_t ancount, <span class="keyword">const</span> u_char *msg_ptr,
750     00867 <span class="keyword">const</span> u_char *eom_ptr, u_char *rd_ptr, <span class="keywordtype">char</span> *buf, <span class="keywordtype">int</span> *ttl)
751     00868 {
752     00869 int16_t i; <span class="comment">/* utility */</span>
753     00870 int16_t rc; <span class="comment">/* generic return code / length of */</span>
754     00871 int16_t rd_type; <span class="comment">/* answer type */</span>
755     00872 int16_t rd_len; <span class="comment">/* res_search return code &amp; packet len */</span>
756     00873
757     00874 int32_t rd_ttl; <span class="comment">/* TTL */</span>
758     00875
759     00876 size_t buf_len; <span class="comment">/* buffer length */</span>
760     00877
761     00878 <span class="keywordtype">char</span> *rr_data = NULL; <span class="comment">/* data pointer */</span>
762     00879
763     00880
764     00881 <span class="keywordflow">if</span> ((msg_ptr == NULL) || (eom_ptr == NULL) ||
765     00882 (rd_ptr == NULL) || (buf == NULL))
766     00883 {
767     00884 <a class="code" href="util_8h.html#a13">xepprintf</a>(<span class="stringliteral">"Called with NULL pointers\n"</span>);
768     00885
769     00886 <span class="keywordflow">return</span>(NULL);
770     00887 }
771     00888
772     00889 <a class="code" href="util_8h.html#a11">xpprintf</a>(<span class="stringliteral">"entering function\n"</span>);
773     00890
774     00891 i = 0;
775     00892 <span class="keywordflow">while</span> ((ancount &gt; 0) &amp;&amp; (rd_ptr &lt; eom_ptr))
776     00893 {
777     00894 <span class="keywordflow">if</span> ((rc = dn_expand(msg_ptr, eom_ptr, rd_ptr, buf, <a class="code" href="dns_8h.html#a3">SPF_MAXCDNAME</a>)) &lt; 0)
778     00895 {
779     00896 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Error expanding ANSWER packet at count %i; Reason: %s \n"</span>,
780     00897 ancount, hstrerror(<a class="code" href="dns_8h.html#a6">h_errno</a>));
781     00898
782     00899 <span class="keywordflow">return</span>(NULL);
783     00900 }
784     00901
785     00902 <span class="comment">/* dname, type, class, TTL, rdata len, rdata */</span>
786     00903 rd_ptr += rc; <span class="comment">/* jump to start of ANSWER data */</span>
787     00904 GETSHORT(rd_type, rd_ptr); <span class="comment">/* get response type */</span>
788     00905 rd_ptr += INT16SZ; <span class="comment">/* skip class */</span>
789     00906 GETLONG(rd_ttl, rd_ptr); <span class="comment">/* get TTL */</span>
790     00907 GETSHORT(rd_len, rd_ptr); <span class="comment">/* get data length */</span>
791     00908
792     00909 *ttl = rd_ttl;
793     00910
794     00911 <span class="keywordflow">if</span> (rd_type != T_CNAME)
795     00912 {
796     00913 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Ignoring record not of T_CNAME type. (%i)\n"</span>, rd_type);
797     00914 rd_ptr += rd_len;
798     00915 <span class="keywordflow">continue</span>;
799     00916 }
800     00917
801     00918 <span class="keywordflow">if</span> (dn_expand(msg_ptr, eom_ptr, rd_ptr, buf, <a class="code" href="dns_8h.html#a3">SPF_MAXCDNAME</a>) &lt; 0)
802     00919 {
803     00920 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Error expanding ANSWER packet at count %i; Reason: %s \n"</span>,
804     00921 ancount, hstrerror(<a class="code" href="dns_8h.html#a6">h_errno</a>));
805     00922
806     00923 <span class="keywordflow">return</span>(NULL);
807     00924 }
808     00925
809     00926 buf_len = strlen(buf);
810     00927 i += (buf_len + 1);
811     00928
812     00929 <span class="keywordflow">if</span> ((rd_len &lt;= SPF_MAXDNAME) &amp;&amp; (rd_len &gt; 0))
813     00930 {
814     00931 <span class="keywordflow">if</span> (rr_data == NULL)
815     00932 {
816     00933 rr_data = <a class="code" href="util_8h.html#a4">xmalloc</a>(i + 1);
817     00934 <span class="comment">/*memset(rr_data, '\0', (i + 1));*/</span>
818     00935 }
819     00936 <span class="keywordflow">else</span>
820     00937 {
821     00938 rr_data = <a class="code" href="util_8h.html#a5">xrealloc</a>(rr_data, (i + 1));
822     00939 }
823     00940
824     00941 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"REALLOCATE memory: %i bytes\n"</span>, (i + 1));
825     00942
826     00943 strncat(rr_data, buf, buf_len);
827     00944 rr_data[i - 1] = <span class="charliteral">' '</span>;
828     00945 rr_data[i] = <span class="charliteral">'\0'</span>;
829     00946 }
830     00947
831     00948 rd_ptr += rc;
832     00949 ancount--;
833     00950 }
834     00951
835     00952 <span class="keywordflow">if</span> (rr_data != NULL)
836     00953 {
837     00954 rr_data[i - 1] = <span class="charliteral">'\0'</span>;
838     00955 }
839     00956
840     00957 <a class="code" href="util_8h.html#a11">xpprintf</a>(<span class="stringliteral">"leaving function\n"</span>);
841     00958 <span class="keywordflow">return</span>(rr_data);
842     00959 }
843     00960
844     00961
845     00962 <span class="comment">/* DNS_check_client_reverse</span>
846     00963 <span class="comment">*</span>
847     00964 <span class="comment">* Author: Travis Anderson &lt;tanderson@codeshare.ca&gt;</span>
848     00965 <span class="comment">* Adapted from DNS_query</span>
849     00966 <span class="comment">*</span>
850     00967 <span class="comment">* Date: 08/??/04 </span>
851     00968 <span class="comment">*</span>
852     00969 <span class="comment">* Desc:</span>
853     00970 <span class="comment">* First get addr's reverse then for each hostname returned, </span>
854     00971 <span class="comment">* resolve it and then see if it matches addr. If a match is found return </span>
855     00972 <span class="comment">* SPF_TRUE if not return SPF_FALSE.</span>
856     00973 <span class="comment">*</span>
857     00974 <span class="comment">*/</span>
858     <a name="l00975"></a><a class="code" href="dns_8h.html#a13">00975</a> <a class="code" href="spf_8h.html#a35">SPF_BOOL</a> <a class="code" href="dns_8h.html#a13">DNS_check_client_reverse</a>(<a class="code" href="structpeer__info__s.html">peer_info_t</a> *p)
859     00976 {
860     00977
861     00978 HEADER *hdr; <span class="comment">/* pointer to the header of the packet */</span>
862     00979
863     00980 int8_t ancount; <span class="comment">/* number of answers */</span>
864     00981
865     00982 int16_t rd_type; <span class="comment">/* answer type */</span>
866     00983 int16_t rd_len = 0; <span class="comment">/* res_search return code &amp; packet len */</span>
867     00984 int16_t r_len; <span class="comment">/* res_search return code &amp; packet len */</span>
868     00985 int16_t rc; <span class="comment">/* generic return code / length of */</span>
869     00986
870     00987 <span class="keywordtype">char</span> buf[<a class="code" href="dns_8h.html#a2">SPF_MAXDNAME</a>]; <span class="comment">/* record extraction buffer */</span>
871     00988 <span class="keywordtype">char</span> answer[<a class="code" href="dns_8h.html#a1">SPF_PACKETSZ</a>]; <span class="comment">/* answer buffer */</span>
872     00989
873     00990 <span class="keywordtype">char</span> *addr_arpa = NULL; <span class="comment">/* reverse address in dot-quad notation */</span>
874     00991
875     00992 u_char *msg_ptr; <span class="comment">/* pointer to beginning of the message */</span>
876     00993 u_char *eom_ptr; <span class="comment">/* pointer to the end of the message */</span>
877     00994 u_char *rd_ptr; <span class="comment">/* pointer to uncompressed message */</span>
878     00995
879     00996
880     00997 <span class="keywordflow">if</span> (p == NULL)
881     00998 {
882     00999 <a class="code" href="util_8h.html#a13">xepprintf</a>(<span class="stringliteral">"Unable to continue, peer info struct is NULL!\n"</span>);
883     01000
884     01001 <span class="keywordflow">return</span>(<a class="code" href="spf_8h.html#a87a49">SPF_FALSE</a>);
885     01002 }
886     01003
887     01004 <a class="code" href="util_8h.html#a11">xpprintf</a>(<span class="stringliteral">"entering function\n"</span>);
888     01005
889     01006 addr_arpa = <a class="code" href="util_8h.html#a50">UTIL_rev_addr</a>(p-&gt;<a class="code" href="structpeer__info__s.html#o17">r_ip</a>);
890     01007
891     01008 r_len = res_search(addr_arpa, C_IN, T_PTR, (u_char *)answer,
892     01009 <a class="code" href="dns_8h.html#a1">SPF_PACKETSZ</a>);
893     01010
894     01011 <a class="code" href="util_8h.html#a6">xfree</a>(addr_arpa);
895     01012
896     01013 hdr = (HEADER *)&amp;answer;
897     01014 ancount = ntohs(hdr-&gt;ancount);
898     01015
899     01016 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Received packet size of %i bytes which contains %i answers.\n"</span>,
900     01017 r_len, ancount);
901     01018 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"ANSWERS: %i\n"</span>, ancount);
902     01019 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"QUESTIONS: %i\n"</span>, ntohs(hdr-&gt;qdcount));
903     01020
904     01021 <span class="keywordflow">if</span> (ancount &gt; 0)
905     01022 {
906     01023 msg_ptr = (u_char *)&amp;answer; <span class="comment">/* point to start of message */</span>
907     01024 eom_ptr = ((u_char *)&amp;answer + r_len); <span class="comment">/* point to end of message */</span>
908     01025 rd_ptr = ((u_char *)&amp;answer + HFIXEDSZ); <span class="comment">/* point to start of RDATA */</span>
909     01026
910     01027 <span class="keywordflow">if</span> ((rc = dn_skipname(rd_ptr, eom_ptr)) &lt; 0)
911     01028 {
912     01029 <a class="code" href="util_8h.html#a13">xepprintf</a>(<span class="stringliteral">"Error obtaining QUESTION!\n"</span>);
913     01030
914     01031 <span class="keywordflow">return</span>(<a class="code" href="spf_8h.html#a87a49">SPF_FALSE</a>);
915     01032 }
916     01033
917     01034 rd_ptr += rc + QFIXEDSZ; <span class="comment">/* jump to start of ANSWER */</span>
918     01035
919     01036 <span class="keywordflow">while</span> ((ancount &gt; 0) &amp;&amp; (rd_ptr &lt; eom_ptr))
920     01037 {
921     01038 <span class="keywordflow">if</span> ((rc = dn_expand(msg_ptr, eom_ptr, rd_ptr, buf, <a class="code" href="dns_8h.html#a3">SPF_MAXCDNAME</a>)) &lt; 0)
922     01039 {
923     01040 <a class="code" href="util_8h.html#a12">xeprintf</a>(<span class="stringliteral">"Error expanding ANSWER packet at count %i. (%s)\n"</span>,
924     01041 ancount, buf);
925     01042
926     01043 <span class="keywordflow">return</span>(<a class="code" href="spf_8h.html#a87a49">SPF_FALSE</a>);
927     01044 }
928     01045
929     01046 <span class="comment">/* dname, type, class, TTL, rdata len, rdata */</span>
930     01047 rd_ptr += rc; <span class="comment">/* jump to start of ANSWER data */</span>
931     01048 GETSHORT(rd_type, rd_ptr); <span class="comment">/* get response type */</span>
932     01049 rd_ptr += INT16SZ; <span class="comment">/* skip class */</span>
933     01050 rd_ptr += INT32SZ; <span class="comment">/* skip TTL */</span>
934     01051 GETSHORT(rd_len, rd_ptr); <span class="comment">/* get data length */</span>
935     01052
936     01053 <span class="keywordflow">if</span> (rd_type != T_PTR)
937     01054 {
938     01055 rc = dn_expand(msg_ptr, eom_ptr, rd_ptr, buf, <a class="code" href="dns_8h.html#a3">SPF_MAXCDNAME</a>);
939     01056
940     01057 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Got answer to type %i (%s) when we asked for T_PTR (%i)\n"</span>,
941     01058 rd_type, buf, T_PTR);
942     01059
943     01060 rd_ptr += rd_len;
944     01061 <span class="keywordflow">continue</span>;
945     01062 }
946     01063
947     01064 <span class="keywordflow">if</span> ((rc = dn_expand(msg_ptr, eom_ptr, rd_ptr, buf, <a class="code" href="dns_8h.html#a3">SPF_MAXCDNAME</a>)) &lt; 0)
948     01065 {
949     01066 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Error expanding ANSWER packet at count %i. (%s)\n"</span>,
950     01067 ancount, buf);
951     01068
952     01069 <span class="keywordflow">return</span>(<a class="code" href="spf_8h.html#a87a49">SPF_FALSE</a>);
953     01070 }
954     01071
955     01072 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Answer %i has length %i.\n"</span>, ancount, rd_len);
956     01073 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Answer data (buffer): (%s); data length: %i\n"</span>,
957     01074 buf, strlen(buf));
958     01075
959     01076 <span class="keywordflow">if</span> ((rd_len &lt;= SPF_MAXDNAME) &amp;&amp; (rd_len &gt; 0))
960     01077 {
961     01078 <span class="keywordflow">if</span> (<a class="code" href="util_8h.html#a54">UTIL_validate_hostname</a>(p, buf, 32) == <a class="code" href="spf_8h.html#a87a49">SPF_FALSE</a>)
962     01079 {
963     01080 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Unable to validate hostname (%s) with (%s)\n"</span>,
964     01081 buf, p-&gt;<a class="code" href="structpeer__info__s.html#o17">r_ip</a>);
965     01082
966     01083 rd_ptr += rc;
967     01084 ancount--;
968     01085 <span class="keywordflow">continue</span>;
969     01086 }
970     01087 <span class="keywordflow">else</span>
971     01088 {
972     01089 <span class="keywordflow">if</span> (p-&gt;<a class="code" href="structpeer__info__s.html#o18">r_vhname</a> != NULL)
973     01090 {
974     01091 <a class="code" href="util_8h.html#a6">xfree</a>(p-&gt;<a class="code" href="structpeer__info__s.html#o18">r_vhname</a>);
975     01092 }
976     01093
977     01094 p-&gt;<a class="code" href="structpeer__info__s.html#o18">r_vhname</a> = <a class="code" href="util_8h.html#a8">xstrndup</a>(buf, strlen(buf) + 1);
978     01095
979     01096 <span class="keywordflow">return</span>(<a class="code" href="spf_8h.html#a87a50">SPF_TRUE</a>);
980     01097 } <span class="comment">/* if (UTIL_validate.. */</span>
981     01098 }
982     01099 <span class="keywordflow">else</span>
983     01100 {
984     01101 <a class="code" href="util_8h.html#a13">xepprintf</a>(<span class="stringliteral">"Answer length is too long!\n"</span>);
985     01102 <span class="keywordflow">continue</span>;
986     01103 }
987     01104
988     01105 rd_ptr += rc;
989     01106 ancount--;
990     01107
991     01108 } <span class="comment">/* if (rd_len */</span>
992     01109 } <span class="comment">/* while (ancount */</span>
993     01110
994     01111 <span class="keywordflow">return</span>(<a class="code" href="spf_8h.html#a87a49">SPF_FALSE</a>);
995     01112 }
996     01113
997     01114 <span class="preprocessor">#ifdef _WITH_PTHREADS</span>
998     01115 <span class="preprocessor"></span><span class="preprocessor"> #ifdef HAVE_GETHOSTBYNAME_R</span>
999     01116 <span class="preprocessor"></span>
1000     01117 <span class="comment">/* _DNS_gethostbyname_r</span>
1001     01118 <span class="comment">*</span>
1002     01119 <span class="comment">* Author: James Couzens &lt;jcouzens@codeshare.ca&gt;</span>
1003     01120 <span class="comment">* Date: 08/28/04</span>
1004     01121 <span class="comment">*</span>
1005     01122 <span class="comment">* Desc:</span>
1006     01123 <span class="comment">* A wrapper around the GNU gethostbyname_r extension function which</span>
1007     01124 <span class="comment">* introduces reentrant (some) resolver functionality allbeit way too bloody</span>
1008     01125 <span class="comment">* late. Someone smack me with a tire iron and rewrite all of this using DJB.</span>
1009     01126 <span class="comment">*</span>
1010     01127 <span class="comment">* This function is intented to be wrapped by a MACRO going by something along</span>
1011     01128 <span class="comment">* the lines of 'xgethostbyname' or 'xgethostbyname_r'.</span>
1012     01129 <span class="comment">*</span>
1013     01130 <span class="comment">*/</span>
1014     01131 <span class="keyword">struct </span>hostent *_DNS_GNU_gethostbyname_r(<span class="keyword">const</span> <span class="keywordtype">char</span> *name,
1015     01132 <span class="keyword">struct</span> hostent *result, <span class="keywordtype">char</span> *buf, <span class="keywordtype">int</span> buf_len, <span class="keywordtype">int</span> *h_errnop)
1016     01133 {
1017     01134 <span class="keyword">struct </span>hostent *hp = NULL; <span class="comment">/* hostent structure */</span>
1018     01135
1019     01136
1020     01137 <span class="keywordflow">if</span> (!name)
1021     01138 {
1022     01139 <a class="code" href="util_8h.html#a13">xepprintf</a>(<span class="stringliteral">"ERROR: No hostname to resolve.\n"</span>);
1023     01140
1024     01141 <span class="keywordflow">return</span>(NULL);
1025     01142 }
1026     01143
1027     01144 <a class="code" href="util_8h.html#a11">xpprintf</a>(<span class="stringliteral">"entering function\n"</span>);
1028     01145
1029     01146 <span class="keywordflow">if</span> (buf_len &gt;= <a class="code" href="spf_8h.html#a5">SPF_MAX_STR</a>)
1030     01147 {
1031     01148 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"buf_len (%i) is &gt; max size (%i); Disregarded.\n"</span>,
1032     01149 buf_len, SPF_MAX_STR);
1033     01150
1034     01151 <span class="keywordflow">return</span>(NULL);
1035     01152 }
1036     01153
1037     01154 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"called with hostname (%s)\n"</span>, name);
1038     01155 memset(buf, <span class="charliteral">'\0'</span>, SPF_MAX_GHBNR_DBUF);
1039     01156 hp = <a class="code" href="util_8h.html#a4">xmalloc</a>(<a class="code" href="util_8h.html#a3">SIZEOF</a>(<span class="keyword">struct</span> hostent));
1040     01157
1041     01158 gethostbyname_r(name, result, buf, (size_t)buf_len, &amp;hp, h_errnop);
1042     01159
1043     01160 <a class="code" href="util_8h.html#a11">xpprintf</a>(<span class="stringliteral">"leaving function\n"</span>);
1044     01161
1045     01162 <span class="keywordflow">return</span>(hp);
1046     01163 }
1047     01164
1048     01165 <span class="preprocessor"> #else </span><span class="comment">/* HAVE_GETHOSTBYNAME_R */</span>
1049     01166
1050     01167 <span class="comment">/* _DNS_gethostbyname_r</span>
1051     01168 <span class="comment">*</span>
1052     01169 <span class="comment">* Author: James Couzens &lt;jcouzens@codeshare.ca&gt;</span>
1053     01170 <span class="comment">* Date: 08/28/04</span>
1054     01171 <span class="comment">*</span>
1055     01172 <span class="comment">* Desc:</span>
1056     01173 <span class="comment">* Reentrant version of gethostbyname_r which is available as a</span>
1057     01174 <span class="comment">* GNU extension but unavailable to everyone else, so I've opted to</span>
1058     01175 <span class="comment">* implement it here. This function is intented to be wrapped by a</span>
1059     01176 <span class="comment">* MACRO going by something along the lines of 'xgethostbyname' or</span>
1060     01177 <span class="comment">* 'xgethostbyname_r'</span>
1061     01178 <span class="comment">*</span>
1062     01179 <span class="comment">* Calling function is responsible to call _DNS_gethostbyname_r_free</span>
1063     01180 <span class="comment">* to unlock the mutex.</span>
1064     01181 <span class="comment">*</span>
1065     01182 <span class="comment">*/</span>
1066     01183 <span class="keyword">struct </span>hostent *_DNS_gethostbyname_r(<span class="keyword">const</span> <span class="keywordtype">char</span> *name,
1067     01184 <span class="keyword">struct</span> hostent *result, <span class="keywordtype">char</span> *buf, <span class="keywordtype">int</span> buf_len, <span class="keywordtype">int</span> *h_errnop)
1068     01185 {
1069     01186 <span class="keyword">struct </span>hostent *hp; <span class="comment">/* hostent structure */</span>
1070     01187
1071     01188 <span class="keywordflow">if</span> (!name)
1072     01189 {
1073     01190 <a class="code" href="util_8h.html#a13">xepprintf</a>(<span class="stringliteral">"ERROR: No hostname to resolve!\n"</span>);
1074     01191
1075     01192 <span class="keywordflow">return</span>(NULL);
1076     01193 }
1077     01194
1078     01195 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"called with hostname (%s)\n"</span>, name);
1079     01196
1080     01197 <a class="code" href="util_8h.html#a14">xpthread_mutex_lock</a>(&amp;dns_mutex);
1081     01198
1082     01199 hp = gethostbyname(name);
1083     01200 *h_errnop = <a class="code" href="dns_8h.html#a6">h_errno</a>;
1084     01201
1085     01202 <a class="code" href="util_8h.html#a11">xpprintf</a>(<span class="stringliteral">"leaving function\n"</span>);
1086     01203
1087     01204 <span class="keywordflow">return</span>(hp);
1088     01205 }
1089     01206
1090     01207
1091     01208 <span class="comment">/* _DNS_gethostbyname_r_free</span>
1092     01209 <span class="comment">*</span>
1093     01210 <span class="comment">* Author: James Couzens &lt;jcouzens@codeshare.ca&gt;</span>
1094     01211 <span class="comment">* Date: 08/28/04</span>
1095     01212 <span class="comment">*</span>
1096     01213 <span class="comment">* Desc:</span>
1097     01214 <span class="comment">* This function unlocks the mutex which was locked when</span>
1098     01215 <span class="comment">* _DNS_gethostbyname_r was called as a reentrant wrapper around the</span>
1099     01216 <span class="comment">* non-threadsafe 'gethostbyname' function call. This function is</span>
1100     01217 <span class="comment">* intended to be wrapped by a MACRO going by something along the lines</span>
1101     01218 <span class="comment">* of 'xgethostbyname_free' or 'xgethostbyname_r_free'</span>
1102     01219 <span class="comment">*</span>
1103     01220 <span class="comment">*</span>
1104     01221 <span class="comment">*/</span>
1105     01222 <span class="keywordtype">void</span> <a class="code" href="dns_8h.html#a7">_DNS_gethostbyname_r_free</a>(<span class="keywordtype">void</span>)
1106     01223 {
1107     01224 <a class="code" href="util_8h.html#a15">xpthread_mutex_unlock</a>(&amp;dns_mutex);
1108     01225
1109     01226 <span class="keywordflow">return</span>;
1110     01227 }
1111     01228
1112     01229 <span class="preprocessor"> #endif </span><span class="comment">/* HAVE_GETHOSTBYNAME_R */</span>
1113     01230 <span class="preprocessor">#endif </span><span class="comment">/* _WITH_PTHREADS */</span>
1114     01231
1115     01232
1116     01233 <span class="comment">/* end dns.c */</span>
1117     </div></pre><hr size="1"><address style="align: right;"><small>Generated on Thu Sep 16 18:10:46 2004 for libSPF v1.0 by
1118     <a href="http://www.doxygen.org/index.html">
1119     <img src="doxygen.png" alt="doxygen" align="middle" border=0 ></a> 1.3.8 </small></address>
1120     </body>
1121     </html>