xref: /netbsd-src/external/bsd/unbound/dist/pythonmod/pythonmod_utils.c (revision 627f7eb200a4419d89b531d55fccd2ee3ffdcde0)
1 /*
2  * pythonmod_utils.c: utilities used by wrapper
3  *
4  * Copyright (c) 2009, Zdenek Vasicek (vasicek AT fit.vutbr.cz)
5  *                     Marek Vavrusa  (xvavru00 AT stud.fit.vutbr.cz)
6  *
7  * This software is open source.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  *    * Redistributions of source code must retain the above copyright notice,
14  *      this list of conditions and the following disclaimer.
15  *
16  *    * Redistributions in binary form must reproduce the above copyright notice,
17  *      this list of conditions and the following disclaimer in the documentation
18  *      and/or other materials provided with the distribution.
19  *
20  *    * Neither the name of the organization nor the names of its
21  *      contributors may be used to endorse or promote products derived from this
22  *      software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
28  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  */
36 /**
37  * \file
38  * Utility functions for the python module that perform stores and loads and
39  * conversions.
40  */
41 #include "config.h"
42 #include "pythonmod/pythonmod_utils.h"
43 #include "util/module.h"
44 #include "util/netevent.h"
45 #include "util/net_help.h"
46 #include "services/cache/dns.h"
47 #include "services/cache/rrset.h"
48 #include "util/data/msgparse.h"
49 #include "util/data/msgreply.h"
50 #include "util/storage/slabhash.h"
51 #include "util/regional.h"
52 #include "iterator/iter_delegpt.h"
53 #include "sldns/sbuffer.h"
54 
55 #undef _POSIX_C_SOURCE
56 #undef _XOPEN_SOURCE
57 #include <Python.h>
58 
59 /* Store the reply_info and query_info pair in message cache (qstate->msg_cache) */
60 int storeQueryInCache(struct module_qstate* qstate, struct query_info* qinfo, struct reply_info* msgrep, int is_referral)
61 {
62     if (!msgrep)
63        return 0;
64 
65     if (msgrep->authoritative) /*authoritative answer can't be stored in cache*/
66     {
67        PyErr_SetString(PyExc_ValueError, "Authoritative answer can't be stored");
68        return 0;
69     }
70 
71     return dns_cache_store(qstate->env, qinfo, msgrep, is_referral,
72 	qstate->prefetch_leeway, 0, NULL, qstate->query_flags);
73 }
74 
75 /*  Invalidate the message associated with query_info stored in message cache */
76 void invalidateQueryInCache(struct module_qstate* qstate, struct query_info* qinfo)
77 {
78     hashvalue_type h;
79     struct lruhash_entry* e;
80     struct reply_info *r;
81     size_t i, j;
82 
83     h = query_info_hash(qinfo, qstate->query_flags);
84     if ((e=slabhash_lookup(qstate->env->msg_cache, h, qinfo, 0)))
85     {
86 	r = (struct reply_info*)(e->data);
87 	if (r)
88 	{
89 	   r->ttl = 0;
90 	   if(rrset_array_lock(r->ref, r->rrset_count, *qstate->env->now)) {
91 		   for(i=0; i< r->rrset_count; i++)
92 		   {
93 		       struct packed_rrset_data* data =
94 		       	(struct packed_rrset_data*) r->ref[i].key->entry.data;
95 		       if(i>0 && r->ref[i].key == r->ref[i-1].key)
96 			   continue;
97 
98 		       data->ttl = r->ttl;
99 		       for(j=0; j<data->count + data->rrsig_count; j++)
100 			   data->rr_ttl[j] = r->ttl;
101 		   }
102 		   rrset_array_unlock(r->ref, r->rrset_count);
103 	   }
104 	}
105 	lock_rw_unlock(&e->lock);
106     } else {
107 	log_info("invalidateQueryInCache: qinfo is not in cache");
108     }
109 }
110 
111 /* Create response according to the ldns packet content */
112 int createResponse(struct module_qstate* qstate, sldns_buffer* pkt)
113 {
114     struct msg_parse* prs;
115     struct edns_data edns;
116 
117     /* parse message */
118     prs = (struct msg_parse*) regional_alloc(qstate->env->scratch, sizeof(struct msg_parse));
119     if (!prs) {
120 	log_err("storeResponse: out of memory on incoming message");
121 	return 0;
122     }
123 
124     memset(prs, 0, sizeof(*prs));
125     memset(&edns, 0, sizeof(edns));
126 
127     sldns_buffer_set_position(pkt, 0);
128     if (parse_packet(pkt, prs, qstate->env->scratch) != LDNS_RCODE_NOERROR) {
129 	verbose(VERB_ALGO, "storeResponse: parse error on reply packet");
130 	return 0;
131     }
132     /* edns is not examined, but removed from message to help cache */
133     if(parse_extract_edns(prs, &edns, qstate->env->scratch) !=
134 	    LDNS_RCODE_NOERROR)
135 	return 0;
136 
137     /* remove CD-bit, we asked for in case we handle validation ourself */
138     prs->flags &= ~BIT_CD;
139 
140     /* allocate response dns_msg in region */
141     qstate->return_msg = (struct dns_msg*)regional_alloc(qstate->region, sizeof(struct dns_msg));
142     if (!qstate->return_msg)
143        return 0;
144 
145     memset(qstate->return_msg, 0, sizeof(*qstate->return_msg));
146     if(!parse_create_msg(pkt, prs, NULL, &(qstate->return_msg)->qinfo, &(qstate->return_msg)->rep, qstate->region)) {
147 	log_err("storeResponse: malloc failure: allocating incoming dns_msg");
148 	return 0;
149     }
150 
151     /* Make sure that the RA flag is set (since the presence of
152      * this module means that recursion is available) */
153     /* qstate->return_msg->rep->flags |= BIT_RA; */
154 
155     /* Clear the AA flag */
156     /* FIXME: does this action go here or in some other module? */
157     /*qstate->return_msg->rep->flags &= ~BIT_AA; */
158 
159     /* make sure QR flag is on */
160     /*qstate->return_msg->rep->flags |= BIT_QR; */
161 
162     if(verbosity >= VERB_ALGO)
163 	log_dns_msg("storeResponse: packet:", &qstate->return_msg->qinfo, qstate->return_msg->rep);
164 
165     return 1;
166 }
167 
168 
169 /* Convert reply->addr to string */
170 void reply_addr2str(struct comm_reply* reply, char* dest, int maxlen)
171 {
172 	int af = (int)((struct sockaddr_in*) &(reply->addr))->sin_family;
173 	void* sinaddr = &((struct sockaddr_in*) &(reply->addr))->sin_addr;
174 
175 	if(af == AF_INET6)
176 		sinaddr = &((struct sockaddr_in6*)&(reply->addr))->sin6_addr;
177 	dest[0] = 0;
178 	if (inet_ntop(af, sinaddr, dest, (socklen_t)maxlen) == 0)
179 	   return;
180 	dest[maxlen-1] = 0;
181 }
182 
183 /* Convert target->addr to string */
184 void delegpt_addr_addr2str(struct delegpt_addr* target, char *dest, int maxlen)
185 {
186 	int af = (int)((struct sockaddr_in*) &(target->addr))->sin_family;
187 	void* sinaddr = &((struct sockaddr_in*) &(target->addr))->sin_addr;
188 
189 	if(af == AF_INET6)
190 		sinaddr = &((struct sockaddr_in6*)&(target->addr))->sin6_addr;
191 	dest[0] = 0;
192 	if (inet_ntop(af, sinaddr, dest, (socklen_t)maxlen) == 0)
193 	   return;
194 	dest[maxlen-1] = 0;
195 }
196