summaryrefslogtreecommitdiff
path: root/src/dnsqueue.cpp
blob: 2198d0bed2e19677b98a8fbb5e791b57703d7eba (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
/*       +------------------------------------+
 *       | 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.h"
#include "inspircd_io.h"
#include "inspircd_util.h"
#include "inspircd_config.h"
#include <unistd.h>
#include <fcntl.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 <errno.h>
#include <deque>
#include <errno.h>
#include <unistd.h>
#include <sched.h>
#include "connection.h"
#include "users.h"
#include "servers.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 <stdlib.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 <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.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 std::vector<int> fd_reap;

extern int MODCOUNT;

typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, StrHashComp> user_hash;
typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, StrHashComp> chan_hash;
typedef nspace::hash_map<in_addr,string*, nspace::hash<in_addr>, InAddr_HashComp> address_cache;
typedef nspace::hash_map<std::string, WhoWasUser*, nspace::hash<string>, 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;

class Lookup {
private:
	DNS resolver;
	char u[NICKMAX];
public:
	Lookup()
	{
		strcpy(u,"");
	}

	void Reset()
	{
		strcpy(u,"");
	}

	~Lookup()
	{
	}

	bool DoLookup(std::string nick)
	{
		userrec* usr = Find(nick);
		if (usr)
		{
			log(DEBUG,"New Lookup class for %s with DNSServer set to '%s'",nick.c_str(),DNSServer);
			resolver.SetNS(std::string(DNSServer));
			if (!resolver.ReverseLookup(std::string(usr->host)))
				return false;
			strlcpy(u,nick.c_str(),NICKMAX);
			return true;
		}
		return false;
	}

	bool Done()
	{
		userrec* usr = NULL;
		if (resolver.HasResult())
		{
			if (resolver.GetFD() != 0)
			{
				std::string hostname = resolver.GetResult();
				log(DEBUG,"RESULT! %s",hostname.c_str());
				usr = Find(u);
				if (usr)
				{
					if (usr->registered > 3)
					{
						usr->dns_done = true;
						return true;
					}
					if ((hostname != "") && (usr->registered != 7))
					{
						strlcpy(usr->host,hostname.c_str(),MAXBUF);
						strlcpy(usr->dhost,hostname.c_str(),MAXBUF);
						WriteServ(usr->fd,"NOTICE Auth :Resolved your hostname: %s",hostname.c_str());
						usr->dns_done = true;
						return true;
					}
					usr->dns_done = true;
					return true;
				}
			}
			else
			{
				usr = Find(u);
				if (usr)
					usr->dns_done = true;
				return true;
			}
		}
		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;

}