summaryrefslogtreecommitdiff
path: root/src/dnsqueue.cpp
blob: d7b4119add2a2b740f9dde0415eba3723f10ea21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*       +------------------------------------+
 *       | Inspire Internet Relay Chat Daemon |
 *       +------------------------------------+
 *
 *  Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
 *                       E-mail:
 *                <brain@chatspike.net>
 *           	  <Craig@chatspike.net>
 *     
 * Written by Craig Edwards, Craig McLure, and others.
 * This program is free but copyrighted software; see
 *            the file COPYING for details.
 *
 * ---------------------------------------------------
 */

/* Now with added unF! ;) */

using namespace std;

#include "inspircd_config.h"
#include "inspircd.h"
#include "inspircd_io.h"
#include "inspircd_util.h"
#include <unistd.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include <sys/utsname.h>
#include <cstdio>
#include <time.h>
#include <string>
#ifdef GCC3
#include <ext/hash_map>
#else
#include <hash_map>
#endif
#include <map>
#include <sstream>
#include <vector>
#include <deque>
#include "users.h"
#include "ctables.h"
#include "globals.h"
#include "modules.h"
#include "dynamic.h"
#include "wildcard.h"
#include "message.h"
#include "mode.h"
#include "commands.h"
#include "xline.h"
#include "inspstring.h"
#include "dnsqueue.h"
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/poll.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <string.h>
#include "dns.h"
#include "helperfuncs.h"
#include "hashcomp.h"

extern int MaxWhoResults;

extern std::vector<Module*> modules;
extern std::vector<std::string> module_names;
extern std::vector<ircd_module*> factory;

extern int MODCOUNT;

typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, irc::StrHashComp> user_hash;
typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, irc::StrHashComp> chan_hash;
typedef nspace::hash_map<in_addr,string*, nspace::hash<in_addr>, irc::InAddr_HashComp> address_cache;
typedef nspace::hash_map<std::string, WhoWasUser*, nspace::hash<string>, irc::StrHashComp> whowas_hash;
typedef std::deque<command_t> command_table;

extern user_hash clientlist;
extern chan_hash chanlist;
extern whowas_hash whowas;
extern command_table cmdlist;

extern ClassVector Classes;

extern char DNSServer[MAXBUF];
long max_fd_alloc = 0;

extern time_t TIME;

//enum LookupState { reverse, forward };

class Lookup {
private:
	DNS resolver1;
	DNS resolver2;
	char u[NICKMAX];
	std::string hostname;
public:
	Lookup()
	{
		strcpy(u,"");
		log(DEBUG,"Create class Lookup");
	}

	void Reset()
	{
		strcpy(u,"");
		log(DEBUG,"Reset class Lookup");
	}

	~Lookup()
	{
		log(DEBUG,"Delete class Lookup");
	}

	bool DoLookup(std::string nick)
	{
		hostname = "";
		userrec* usr = Find(nick);
		if (usr)
		{
			log(DEBUG,"New Lookup class for %s with DNSServer set to '%s'",nick.c_str(),DNSServer);
			resolver1.SetNS(std::string(DNSServer));
			if (!resolver1.ReverseLookup(std::string(usr->host)))
			{
				log(DEBUG,"ReverseLookup didnt return true, we're outta here");
				return false;
			}
			strlcpy(u,nick.c_str(),NICKMAX);
			return true;
		}
		log(DEBUG,"We couldnt find that user");
		return false;
	}

	bool Done()
	{
		if (hostname != "")
		{
			log(DEBUG,"Doing forward lookup here with host %s",hostname.c_str());
			// doing forward lookup
			userrec* usr = NULL;
			if (resolver2.HasResult())
			{
				log(DEBUG,"resolver2 has result");
				if (resolver2.GetFD() != 0)
				{
					std::string ip = resolver2.GetResultIP();
					log(DEBUG,"FORWARD RESULT! %s",ip.c_str());

					usr = Find(u);
					if (usr)
					{
						if (usr->registered > 3)
						{
							log(DEBUG,"Point 1: Returning true as usr->dns_done is true");
							usr->dns_done = true;
							return true;
						}
						if ((hostname != "") && (usr->registered != 7))
						{
							if (std::string(usr->ip) == ip)
							{
								strlcpy(usr->host,hostname.c_str(),MAXBUF);
								strlcpy(usr->dhost,hostname.c_str(),MAXBUF);
								log(DEBUG,"Forward and reverse match, assigning hostname");
							}
							else
							{
								log(DEBUG,"AWOOGA! Forward lookup doesn't match reverse: R='%s',F='%s',IP='%s'",hostname.c_str(),ip.c_str(),usr->ip);
							}
							usr->dns_done = true;
							return true;
						}
					}
				}
				else
				{
					usr = Find(u);
					if (usr)
					{
						log(DEBUG,"Point 2: Returning true");
						usr->dns_done = true;
					}
					return true;
				}
			}
			log(DEBUG,"Returning false in forward");
			return false;
		}
		else
		{
			// doing reverse lookup
			userrec* usr = NULL;
			if (resolver1.HasResult())
			{
				usr = Find(u);
				if ((usr) && (usr->dns_done))
					return true;
				if (resolver1.GetFD() != 0)
				{
					hostname = resolver1.GetResult();
					if (usr)
					{
						if ((usr->registered > 3) || (hostname == ""))
						{
							log(DEBUG,"Hostname is blank and user->registered > 3, returning true and setting done");
							usr->dns_done = true;
							return true;
						}
					}
					log(DEBUG,"Starting forwardlookup now for host '%s'...",hostname.c_str());
					if ((resolver2.GetFD() <= 0) && (hostname != ""))
					{
						resolver2.ForwardLookup(hostname);
					}
				}
			}
		}
		log(DEBUG,"Returning false");
		return false;
	}

	int GetFD()
	{
		userrec* usr = Find(u);
		if (!usr)
			return 0;
		if (usr->dns_done)
			return 0;
		return usr->fd;
	}
};

Lookup dnsq[255];

bool lookup_dns(std::string nick)
{
	userrec* u = Find(nick);
	if (u)
	{
		// place a new user into the queue...
		log(DEBUG,"Queueing DNS lookup for %s",u->nick);
		WriteServ(u->fd,"NOTICE Auth :Looking up your hostname...");
		Lookup L;
		if (L.DoLookup(nick))
		{
			for (int j = 0; j < 255; j++)
			{
				if (!dnsq[j].GetFD())
				{
					dnsq[j] = L;
					return true;
				}
			}
			// calculate the maximum value, this saves cpu time later
			for (int p = 0; p < 255; p++)
				if (dnsq[p].GetFD())
					max_fd_alloc = p;
		}
		else
		{
			return false;
		}
	}
	return false;
}

void dns_poll()
{
	// do we have items in the queue?
	for (int j = 0; j <= max_fd_alloc; j++)
	{
		// are any ready, or stale?
		if (dnsq[j].GetFD())
		{
			if (dnsq[j].Done())
			{
				dnsq[j].Reset();
			}
		}
	}
	// looks like someones freed an item, recalculate end of list.
	if ((!dnsq[max_fd_alloc].GetFD()) && (max_fd_alloc != 0))
		for (int p = 0; p < 255; p++)
			if (dnsq[p].GetFD())
				max_fd_alloc = p;

}