add ExtenSpy variant of ChanSpy
[asterisk-bristuff.git] / srv.c
blob8b7e76bfc34eaf81d4684108d175f283220c5458
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * Funding provided by nic.at
10 * See http://www.asterisk.org for more information about
11 * the Asterisk project. Please do not directly contact
12 * any of the maintainers of this project for assistance;
13 * the project provides a web site, mailing lists and IRC
14 * channels for your use.
16 * This program is free software, distributed under the terms of
17 * the GNU General Public License Version 2. See the LICENSE file
18 * at the top of the source tree.
21 /*! \file
23 * \brief DNS SRV Record Lookup Support for Asterisk
25 * \author Mark Spencer <markster@digium.com>
27 * \arg See also \ref AstENUM
29 * \note Funding provided by nic.at
32 #include "asterisk.h"
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36 #include <sys/types.h>
37 #include <netinet/in.h>
38 #include <arpa/nameser.h>
39 #if __APPLE_CC__ >= 1495
40 #include <arpa/nameser_compat.h>
41 #endif
42 #include <resolv.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <unistd.h>
47 #include "asterisk/channel.h"
48 #include "asterisk/logger.h"
49 #include "asterisk/srv.h"
50 #include "asterisk/dns.h"
51 #include "asterisk/options.h"
52 #include "asterisk/utils.h"
54 #ifdef __APPLE__
55 #undef T_SRV
56 #define T_SRV 33
57 #endif
59 struct srv {
60 unsigned short priority;
61 unsigned short weight;
62 unsigned short portnum;
63 } __attribute__ ((__packed__));
65 static int parse_srv(char *host, int hostlen, int *portno, unsigned char *answer, int len, unsigned char *msg)
67 int res = 0;
68 struct srv *srv = (struct srv *)answer;
69 char repl[256] = "";
71 if (len < sizeof(struct srv)) {
72 printf("Length too short\n");
73 return -1;
75 answer += sizeof(struct srv);
76 len -= sizeof(struct srv);
78 if ((res = dn_expand(msg, answer + len, answer, repl, sizeof(repl) - 1)) < 0) {
79 ast_log(LOG_WARNING, "Failed to expand hostname\n");
80 return -1;
82 if (res && strcmp(repl, ".")) {
83 if (option_verbose > 3)
84 ast_verbose( VERBOSE_PREFIX_3 "parse_srv: SRV mapped to host %s, port %d\n", repl, ntohs(srv->portnum));
85 if (host) {
86 ast_copy_string(host, repl, hostlen);
87 host[hostlen-1] = '\0';
89 if (portno)
90 *portno = ntohs(srv->portnum);
91 return 0;
93 return -1;
96 struct srv_context {
97 char *host;
98 int hostlen;
99 int *port;
102 static int srv_callback(void *context, unsigned char *answer, int len, unsigned char *fullanswer)
104 struct srv_context *c = (struct srv_context *)context;
106 if (parse_srv(c->host, c->hostlen, c->port, answer, len, fullanswer)) {
107 ast_log(LOG_WARNING, "Failed to parse srv\n");
108 return -1;
111 if (!ast_strlen_zero(c->host))
112 return 1;
114 return 0;
117 int ast_get_srv(struct ast_channel *chan, char *host, int hostlen, int *port, const char *service)
119 struct srv_context context;
120 int ret;
122 context.host = host;
123 context.hostlen = hostlen;
124 context.port = port;
126 if (chan && ast_autoservice_start(chan) < 0)
127 return -1;
129 ret = ast_search_dns(&context, service, C_IN, T_SRV, srv_callback);
131 if (chan)
132 ret |= ast_autoservice_stop(chan);
134 if (ret <= 0) {
135 host[0] = '\0';
136 *port = -1;
137 return ret;
139 return ret;