aboutsummaryrefslogtreecommitdiffstats
path: root/srv.c
blob: c6365e361c56f3d4a6b6c1ab4b770a07dea818b0 (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
/*
 * ENUM Support for Asterisk
 *
 * Copyright (C) 2003 Digium
 *
 * Written by Mark Spencer <markster@digium.com>
 *
 * Funding provided by nic.at
 *
 * Distributed under the terms of the GNU GPL
 *
 */

#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include <errno.h>

#include <asterisk/logger.h>
#include <asterisk/options.h>
#include <asterisk/srv.h>
#include <asterisk/channel.h>
#include <asterisk/config.h>

#define MAX_SIZE 4096

typedef struct {
	unsigned	id :16;		/* query identification number */
#if BYTE_ORDER == BIG_ENDIAN
			/* fields in third byte */
	unsigned	qr: 1;		/* response flag */
	unsigned	opcode: 4;	/* purpose of message */
	unsigned	aa: 1;		/* authoritive answer */
	unsigned	tc: 1;		/* truncated message */
	unsigned	rd: 1;		/* recursion desired */
			/* fields in fourth byte */
	unsigned	ra: 1;		/* recursion available */
	unsigned	unused :1;	/* unused bits (MBZ as of 4.9.3a3) */
	unsigned	ad: 1;		/* authentic data from named */
	unsigned	cd: 1;		/* checking disabled by resolver */
	unsigned	rcode :4;	/* response code */
#endif
#if BYTE_ORDER == LITTLE_ENDIAN || BYTE_ORDER == PDP_ENDIAN
			/* fields in third byte */
	unsigned	rd :1;		/* recursion desired */
	unsigned	tc :1;		/* truncated message */
	unsigned	aa :1;		/* authoritive answer */
	unsigned	opcode :4;	/* purpose of message */
	unsigned	qr :1;		/* response flag */
			/* fields in fourth byte */
	unsigned	rcode :4;	/* response code */
	unsigned	cd: 1;		/* checking disabled by resolver */
	unsigned	ad: 1;		/* authentic data from named */
	unsigned	unused :1;	/* unused bits (MBZ as of 4.9.3a3) */
	unsigned	ra :1;		/* recursion available */
#endif
			/* remaining bytes */
	unsigned	qdcount :16;	/* number of question entries */
	unsigned	ancount :16;	/* number of answer entries */
	unsigned	nscount :16;	/* number of authority entries */
	unsigned	arcount :16;	/* number of resource entries */
} dns_HEADER;

struct dn_answer {
	unsigned short rtype;
	unsigned short class;
	unsigned int ttl;
	unsigned short size;
} __attribute__ ((__packed__));

struct srv {
	unsigned short priority;
	unsigned short weight;
	unsigned short portnum;
} __attribute__ ((__packed__));

static int skip_name(unsigned char *s, int len)
{
	/* Shamelessly take from SER */
	int x = 0;
	while(x < len) {
		if (!*s) {
			s++;
			x++;
			break;
		}
		if (((*s) & 0xc0) == 0xc0) {
			s += 2;
			x += 2;
			break;
		}
		x += *s + 1;
		s += *s + 1;
	}
	if (x >= len)
		return -1;
	return x;
}

static int parse_ie(unsigned char *data, int maxdatalen, unsigned char *src, int srclen)
{
	int len, olen;
	len = olen = (int)src[0];
	src++;
	srclen--;
	if (len > srclen) {
		ast_log(LOG_WARNING, "Want %d, got %d\n", len, srclen);
		return -1;
	}
	if (len > maxdatalen)
		len = maxdatalen;
	memcpy(data, src, len);
	return olen + 1;
}

static int parse_srv(unsigned char *host, int hostlen, int *portno, unsigned char *answer, int len, unsigned char *msg)
{
	int res = 0;
	struct srv *srv = (struct srv *)answer;
	char repl[256] = "";

	if (len < sizeof(struct srv)) {
		printf("Length too short\n");
		return -1;
	}
	answer += sizeof(struct srv);
	len -= sizeof(struct srv);

	if ((res = dn_expand(msg,answer + len,answer, repl, sizeof(repl) - 1)) < 0) {
		ast_log(LOG_WARNING, "Failed to expand hostname\n");
		return -1;
	}
	if (res && strcmp(repl, ".")) {
		ast_verbose( VERBOSE_PREFIX_3 "parse_srv: SRV mapped to host %s, port %d\n", repl, ntohs(srv->portnum));
		if (host) {
			strncpy(host, repl, hostlen - 2);
			host[hostlen-1] = '\0';
		}
		if (portno)
			*portno = ntohs(srv->portnum);
		return(0);
	}
	return(-1);
}

static int parse_answer(unsigned char *host, int hostlen, int *port, char *answer, int len)
{
	/*
	 * This function is influenced by "ser" the SIP router.
	 */
	int x;
	int res;
	dns_HEADER *h;
	struct dn_answer *ans;
/*	int lastlit = 1; */
	char *oanswer = answer;
	host[0] = '\0';
	*port = 0;

#if 0
	for (x=0;x<len;x++) {
		if ((answer[x] < 32) || (answer[x] > 127)) {
			if (lastlit)
				printf("\"");
			printf(" 0x%02x", answer[x]);
			lastlit = 0;
		} else {
			if (!lastlit) 
				printf(" \"");
			printf("%c", answer[x]);
			lastlit = 1;
		}
	}
	printf("\n");
#endif	
	h = (dns_HEADER *)answer;
	/* Skip over DNS header */
	answer += sizeof(dns_HEADER);
	len -= sizeof(dns_HEADER);
#if 0
	printf("Query count: %d\n", ntohs(h->qdcount));
#endif
	for (x=0;x<ntohs(h->qdcount);x++) {
		if ((res = skip_name(answer, len)) < 0) {
			ast_log(LOG_WARNING, "Couldn't skip over name\n");
			return -1;
		}
		answer += res;
		len -= res;
		answer += 4; 	/* Skip QCODE / QCLASS */
		len -= 4;
		if (len < 0) {
			ast_log(LOG_WARNING, "Strange query size\n");
			return -1;
		}
	}
#if 0
	printf("Length remaining: %d, already at %04x\n", len, 0x2a + (answer - oanswer));
	printf("Answer count: %d\n", ntohs(h->ancount));
	printf("Looking for %d/%d\n", C_IN, T_SRV);
	printf("Answer so far: %02x %02x %02x %02x %02x %02x\n", answer[0], answer[1], answer[2], answer[3], answer[4], answer[5]);
#endif
	for (x=0;x<ntohs(h->ancount);x++) {
		if ((res = skip_name(answer, len)) < 0) {
			ast_log(LOG_WARNING, "Failed to skip name :(\n");
			return -1;
		}
		answer += res;
		len -= res;
		ans = (struct dn_answer *)answer;
		answer += sizeof(struct dn_answer);
		len -= sizeof(struct dn_answer);
		if (len < 0)
			return -1;
#if 0
		printf("Type: %d (%04x), class: %d (%04x), ttl: %d (%08x), length: %d (%04x)\n", ntohs(ans->rtype), ntohs(ans->rtype), ntohs(ans->class), ntohs(ans->class),
			ntohl(ans->ttl), ntohl(ans->ttl), ntohs(ans->size), ntohs(ans->size));
#endif			
		len -= ntohs(ans->size);
		if (len < 0) {
			ast_log(LOG_WARNING, "Length exceeds frame by %d\n", -len);
			return -1;
		}
		if ((ntohs(ans->class) == C_IN) && (ntohs(ans->rtype) == T_SRV)) {

			if (parse_srv(host, hostlen, port, answer, ntohs(ans->size),oanswer))
				ast_log(LOG_WARNING, "Failed to parse srv :(\n");

			if (strlen(host))
				return 0;
		}
		answer += ntohs(ans->size);
	}
	return 0;
}

int ast_get_srv(struct ast_channel *chan, char *host, int hostlen, int *port, const char *service)
{
	int res;
	int ret = -1;
	struct __res_state srvstate;
	char answer[MAX_SIZE];

	if (*port)
		*port = 0;
	res_ninit(&srvstate);	
	if (chan && ast_autoservice_start(chan) < 0)
		return -1;
	res = res_nsearch(&srvstate, service, C_IN, T_SRV, answer, sizeof(answer));
	if (res > 0) {
		if ((res = parse_answer(host, hostlen, port, answer, res))) {
			ast_log(LOG_WARNING, "Parse error returned %d\n", res);
			ret = 0;
		} else {
			ast_log(LOG_DEBUG, "Found host '%s', port '%d'\n", host, *port);
			ret = 1;
		}
	} else {
		ast_log(LOG_DEBUG, "No such service found: %s (%s)\n", service, strerror(errno));
		ret = 0;
	}
	if (chan)
		ret |= ast_autoservice_stop(chan);
	res_nclose(&srvstate);
	return ret;
}