2003-06-12 22:14:03 +00:00
|
|
|
/*
|
|
|
|
* 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 <sys/types.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/nameser.h>
|
2003-10-27 20:00:41 +00:00
|
|
|
#if __APPLE_CC__ >= 1495
|
|
|
|
#include <arpa/nameser_compat.h>
|
|
|
|
#endif
|
2003-06-12 22:14:03 +00:00
|
|
|
#include <resolv.h>
|
2003-09-27 00:22:46 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2003-06-12 22:14:03 +00:00
|
|
|
|
2003-09-27 00:22:46 +00:00
|
|
|
#include <asterisk/channel.h>
|
2003-06-12 22:14:03 +00:00
|
|
|
#include <asterisk/logger.h>
|
|
|
|
#include <asterisk/srv.h>
|
2003-09-27 00:22:46 +00:00
|
|
|
#include <asterisk/dns.h>
|
|
|
|
#include <asterisk/options.h>
|
2004-05-04 14:54:42 +00:00
|
|
|
#include <asterisk/utils.h>
|
2003-06-12 22:14:03 +00:00
|
|
|
|
2003-10-26 18:50:49 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#undef T_SRV
|
|
|
|
#define T_SRV 33
|
|
|
|
#endif
|
|
|
|
|
2003-06-12 22:14:03 +00:00
|
|
|
struct srv {
|
|
|
|
unsigned short priority;
|
|
|
|
unsigned short weight;
|
|
|
|
unsigned short portnum;
|
|
|
|
} __attribute__ ((__packed__));
|
|
|
|
|
2003-08-13 23:56:16 +00:00
|
|
|
static int parse_srv(unsigned char *host, int hostlen, int *portno, unsigned char *answer, int len, unsigned char *msg)
|
2003-06-12 22:14:03 +00:00
|
|
|
{
|
|
|
|
int res = 0;
|
|
|
|
struct srv *srv = (struct srv *)answer;
|
|
|
|
char repl[256] = "";
|
2003-08-13 23:56:16 +00:00
|
|
|
|
2003-06-12 22:14:03 +00:00
|
|
|
if (len < sizeof(struct srv)) {
|
|
|
|
printf("Length too short\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
answer += sizeof(struct srv);
|
|
|
|
len -= sizeof(struct srv);
|
|
|
|
|
2003-08-13 23:56:16 +00:00
|
|
|
if ((res = dn_expand(msg,answer + len,answer, repl, sizeof(repl) - 1)) < 0) {
|
2003-06-12 22:14:03 +00:00
|
|
|
ast_log(LOG_WARNING, "Failed to expand hostname\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (res && strcmp(repl, ".")) {
|
2003-08-13 23:56:16 +00:00
|
|
|
ast_verbose( VERBOSE_PREFIX_3 "parse_srv: SRV mapped to host %s, port %d\n", repl, ntohs(srv->portnum));
|
2003-06-12 22:14:03 +00:00
|
|
|
if (host) {
|
2003-08-13 23:56:16 +00:00
|
|
|
strncpy(host, repl, hostlen - 2);
|
|
|
|
host[hostlen-1] = '\0';
|
2003-06-12 22:14:03 +00:00
|
|
|
}
|
|
|
|
if (portno)
|
|
|
|
*portno = ntohs(srv->portnum);
|
2003-08-13 23:56:16 +00:00
|
|
|
return(0);
|
2003-06-12 22:14:03 +00:00
|
|
|
}
|
2003-08-13 23:56:16 +00:00
|
|
|
return(-1);
|
2003-06-12 22:14:03 +00:00
|
|
|
}
|
|
|
|
|
2003-09-27 00:22:46 +00:00
|
|
|
struct srv_context {
|
|
|
|
char *host;
|
|
|
|
int hostlen;
|
|
|
|
int *port;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int srv_callback(void *context, u_char *answer, int len, u_char *fullanswer)
|
2003-06-12 22:14:03 +00:00
|
|
|
{
|
2003-09-27 00:22:46 +00:00
|
|
|
struct srv_context *c = (struct srv_context *)context;
|
2003-08-13 23:56:16 +00:00
|
|
|
|
2004-03-03 03:12:59 +00:00
|
|
|
if (parse_srv(c->host, c->hostlen, c->port, answer, len, fullanswer)) {
|
2003-09-27 00:22:46 +00:00
|
|
|
ast_log(LOG_WARNING, "Failed to parse srv\n");
|
2004-03-03 03:12:59 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2003-08-13 23:56:16 +00:00
|
|
|
|
2004-05-04 14:54:42 +00:00
|
|
|
if (!ast_strlen_zero(c->host))
|
2003-09-27 00:22:46 +00:00
|
|
|
return 1;
|
2003-08-13 23:56:16 +00:00
|
|
|
|
2004-06-22 20:11:15 +00:00
|
|
|
return 0;
|
2003-06-12 22:14:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ast_get_srv(struct ast_channel *chan, char *host, int hostlen, int *port, const char *service)
|
|
|
|
{
|
2003-09-27 00:22:46 +00:00
|
|
|
struct srv_context context;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
context.host = host;
|
|
|
|
context.hostlen = hostlen;
|
|
|
|
context.port = port;
|
2003-06-12 22:14:03 +00:00
|
|
|
|
|
|
|
if (chan && ast_autoservice_start(chan) < 0)
|
|
|
|
return -1;
|
2003-09-27 00:22:46 +00:00
|
|
|
|
|
|
|
ret = ast_search_dns(&context, service, C_IN, T_SRV, srv_callback);
|
|
|
|
|
2003-06-12 22:14:03 +00:00
|
|
|
if (chan)
|
|
|
|
ret |= ast_autoservice_stop(chan);
|
2003-09-27 00:22:46 +00:00
|
|
|
|
2003-10-04 21:58:16 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
strcpy(host, "");
|
2004-06-22 20:11:15 +00:00
|
|
|
*port = -1;
|
2003-09-27 00:22:46 +00:00
|
|
|
return ret;
|
2003-10-04 21:58:16 +00:00
|
|
|
}
|
2003-06-12 22:14:03 +00:00
|
|
|
return ret;
|
|
|
|
}
|