summaryrefslogtreecommitdiff
path: root/indra/newview/llsrv.cpp
blob: cd05d9c10732336288d760a389f65407219727c3 (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
/** 
 * @file llsrv.cpp
 * @brief Wrapper for DNS SRV record lookups
 *
 * Copyright (c) 2007-$CurrentYear$, Linden Research, Inc.
 * $License$
 */

#include "llviewerprecompiledheaders.h"

#include "llsrv.h"

using namespace std;

#if LL_WINDOWS

#undef UNICODE
#include <winsock2.h>
#include <windns.h>

vector<LLSRVRecord> LLSRV::query(const string& name)
{
	vector<LLSRVRecord> recs;
	DNS_RECORD *rec;
	DNS_STATUS status;

	status = DnsQuery(name.c_str(), DNS_TYPE_SRV, DNS_QUERY_STANDARD, NULL, &rec, NULL);
	if (!status)
	{
		for (DNS_RECORD *cur = rec; cur != NULL; cur = cur->pNext)
		{
			if (cur->wType != DNS_TYPE_SRV)
			{
				continue;
			}
			recs.push_back(LLSRVRecord(cur->Data.Srv.wPriority,
									   cur->Data.Srv.wWeight,
									   cur->Data.Srv.pNameTarget,
									   cur->Data.Srv.wPort));
		}
		DnsRecordListFree(rec, DnsFreeRecordListDeep);
	}

	return recs;
}

#else // !LL_WINDOWS

#include <netinet/in.h>
#include <arpa/nameser.h>
#include <arpa/nameser_compat.h>
#include <resolv.h>

#include <netdb.h>

vector<LLSRVRecord> LLSRV::query(const string& queryName)
{
	unsigned char response[16384];
	vector<LLSRVRecord> recs;
	char name[1024];
	int len;
	
	len = res_query(queryName.c_str(), ns_c_in, ns_t_srv, response,
					sizeof(response));

	if (len == -1)
	{
		llinfos << "Query failed for " << queryName << llendl;
		return recs;
	}
	else if (len > (int) sizeof(response))
	{
		llinfos << "Response too big for " << queryName
				<< " (capacity " << sizeof(response)
				<< ", response " << len << ")" << llendl;
		return recs;
	}

    // We "should" be using libresolv's ns_initparse and ns_parserr to
    // parse the result of our query.  However, libresolv isn't
    // packaged correctly on Linux (as of BIND 9), so neither of these
    // functions is available without statically linking against
    // libresolv.  Ugh!  So we parse the response ourselves.

	const unsigned char *pos = response + sizeof(HEADER);
	const unsigned char *end = response + len;
	const HEADER *hdr = (const HEADER *) response;

	// Skip over the query embedded in the response.

	for (int q = ntohs(hdr->qdcount); q > 0; --q)
	{
		len = dn_expand(response, end, pos, name, sizeof(name));

		if (len == -1)
		{
			llinfos << "Could not expand queried name in RR response" << llendl;
			return recs;
		}
		
		pos += len + NS_QFIXEDSZ;
	}
	
	for (int a = ntohs(hdr->ancount); a > 0; --a)
	{
		static const ns_rr *rr;

		len = dn_expand(response, end, pos, name, sizeof(name) - 1);
		if (len == -1)
		{
			llinfos << "Could not expand response name in RR response" << llendl;
			return recs;
		}

		// Skip over the resource name and headers we don't care about.

		pos += len + sizeof(rr->type) + sizeof(rr->rr_class) +
			sizeof(rr->ttl) + sizeof(rr->rdlength);
		
		U16 prio;
		U16 weight;
		U16 port;

		NS_GET16(prio, pos);
		NS_GET16(weight, pos);
		NS_GET16(port, pos);
		
		len = dn_expand(response, end, pos, name, sizeof(name) - 1);

		if (len == -1)
		{
			llinfos << "Could not expand name in RR response" << llendl;
			return recs;
		}

		recs.push_back(LLSRVRecord(prio, weight, name, port));
	}

	// There are likely to be more records in the response, but we
	// don't care about those, at least for now.

	return recs;
}

#endif // LL_WINDOWS

vector<string> LLSRV::rewriteURI(const string& uriStr)
{
	LLURI uri(uriStr);
	const string& scheme = uri.scheme();
	llinfos << "Rewriting " << uriStr << llendl;
	string serviceName("_" + scheme + "._tcp." + uri.hostName());
	llinfos << "Querying for " << serviceName << llendl;
	vector<LLSRVRecord> srvs(LLSRV::query(serviceName));
	vector<string> rewritten;

	if (srvs.empty())
	{
		llinfos << "No query results; using " << uriStr << llendl;
		rewritten.push_back(uriStr);
	}
	else
	{
		vector<LLSRVRecord>::const_iterator iter;
		size_t maxSrvs = 3;
		size_t i;

		llinfos << "Got " << srvs.size() << " results" << llendl;
		if (srvs.size() > maxSrvs)
		{
			llinfos << "Clamping to " << maxSrvs << llendl;
		}

		for (iter = srvs.begin(), i = 0;
			 iter != srvs.end() && i < maxSrvs; ++iter, ++i)
		{
			LLURI newUri(scheme,
						 uri.userName(),
						 uri.password(),
						 iter->target(),
						 uri.defaultPort() ? iter->port() : uri.hostPort(),
						 uri.escapedPath(),
						 uri.escapedQuery());
			string newUriStr(newUri.asString());

			llinfos << "Rewrite[" << i << "] " << newUriStr << llendl;

			rewritten.push_back(newUriStr);
		}
	}

	return rewritten;
}