]> jfr.im git - solanum.git/blame - ircd/dns.c
authd: fix ratbox3.1-ism
[solanum.git] / ircd / dns.c
CommitLineData
7d2852b4
AC
1/*
2 * dns.c: An interface to the resolver module in authd
3 * (based somewhat on ircd-ratbox dns.c)
4 *
5 * Copyright (C) 2005 Aaron Sethman <androsyn@ratbox.org>
6 * Copyright (C) 2005-2012 ircd-ratbox development team
7 * Copyright (C) 2016 William Pitcock <nenolod@dereferenced.org>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
22 * USA
23 */
24
25#include <stdinc.h>
26#include <ratbox_lib.h>
27#include <struct.h>
28#include <ircd_defs.h>
29#include <parse.h>
30#include <dns.h>
31#include <match.h>
32#include <logger.h>
33#include <s_conf.h>
34#include <client.h>
35#include <send.h>
36#include <numeric.h>
37#include <msg.h>
38
39#define DNS_IDTABLE_SIZE 0x2000
40
41#define DNS_HOST ((char)'H')
42#define DNS_REVERSE ((char)'I')
43
44static void submit_dns(const char, uint16_t id, int aftype, const char *addr);
45
7d2852b4
AC
46struct dnsreq
47{
48 DNSCB callback;
49 void *data;
50};
51
52static struct dnsreq querytable[DNS_IDTABLE_SIZE];
53
54static uint16_t
55assign_dns_id(void)
56{
57 static uint16_t id = 1;
58 int loopcnt = 0;
59 while(1)
60 {
61 if(++loopcnt > DNS_IDTABLE_SIZE)
62 return 0;
63 if(id < DNS_IDTABLE_SIZE - 1 || id == 0)
64 id++;
65 else
66 id = 1;
67 if(querytable[id].callback == NULL)
68 break;
69 }
70 return (id);
71}
72
7d2852b4
AC
73static void
74handle_dns_failure(uint16_t xid)
75{
76 struct dnsreq *req;
77
78 req = &querytable[xid];
79 if(req->callback == NULL)
80 return;
81
82 req->callback("FAILED", 0, 0, req->data);
83 req->callback = NULL;
84 req->data = NULL;
85}
86
87void
88cancel_lookup(uint16_t xid)
89{
90 querytable[xid].callback = NULL;
91 querytable[xid].data = NULL;
92}
93
94uint16_t
95lookup_hostname(const char *hostname, int aftype, DNSCB callback, void *data)
96{
97 struct dnsreq *req;
98 int aft;
99 uint16_t nid;
100 check_authd();
101 nid = assign_dns_id();
102 if((nid = assign_dns_id()) == 0)
103 return 0;
104
105 req = &querytable[nid];
106
107 req->callback = callback;
108 req->data = data;
109
110#ifdef RB_IPV6
111 if(aftype == AF_INET6)
112 aft = 6;
113 else
114#endif
115 aft = 4;
116
117 submit_dns(DNS_HOST, nid, aft, hostname);
118 return (nid);
119}
120
121uint16_t
122lookup_ip(const char *addr, int aftype, DNSCB callback, void *data)
123{
124 struct dnsreq *req;
125 int aft;
126 uint16_t nid;
127 check_authd();
128
129 if((nid = assign_dns_id()) == 0)
130 return 0;
131
132 req = &querytable[nid];
133
134 req->callback = callback;
135 req->data = data;
136
137#ifdef RB_IPV6
138 if(aftype == AF_INET6)
139 aft = 6;
140 else
141#endif
142 aft = 4;
143
144 submit_dns(DNS_REVERSE, nid, aft, addr);
145 return (nid);
146}
147
fb7d74ef
AC
148void
149dns_results_callback(const char *callid, const char *status, const char *aftype, const char *results)
7d2852b4
AC
150{
151 struct dnsreq *req;
152 uint16_t nid;
153 int st;
154 int aft;
155 long lnid = strtol(callid, NULL, 16);
156
157 if(lnid > DNS_IDTABLE_SIZE || lnid == 0)
158 return;
159 nid = (uint16_t)lnid;
160 req = &querytable[nid];
161 st = atoi(status);
162 aft = atoi(aftype);
163 if(req->callback == NULL)
164 {
165 /* got cancelled..oh well */
166 req->data = NULL;
167 return;
168 }
169#ifdef RB_IPV6
170 if(aft == 6)
171 aft = AF_INET6;
172 else
173#endif
174 aft = AF_INET;
175
176 req->callback(results, st, aft, req->data);
177 req->callback = NULL;
178 req->data = NULL;
179}
180
7d2852b4
AC
181void
182report_dns_servers(struct Client *source_p)
183{
184#if 0
185 rb_dlink_node *ptr;
186 RB_DLINK_FOREACH(ptr, nameservers.head)
187 {
188 sendto_one_numeric(source_p, RPL_STATSDEBUG, "A %s", (char *)ptr->data);
189 }
190#endif
191}
192
7d2852b4
AC
193static void
194submit_dns(char type, uint16_t nid, int aftype, const char *addr)
195{
196 if(authd_helper == NULL)
197 {
198 handle_dns_failure(nid);
199 return;
200 }
201 rb_helper_write(authd_helper, "%c %x %d %s", type, nid, aftype, addr);
202}