Fri Aug 24 02:27:30 2007

Asterisk developer's documentation


srv.c File Reference

DNS SRV Record Lookup Support for Asterisk. More...

#include "asterisk.h"
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "asterisk/channel.h"
#include "asterisk/logger.h"
#include "asterisk/srv.h"
#include "asterisk/dns.h"
#include "asterisk/options.h"
#include "asterisk/utils.h"

Include dependency graph for srv.c:

Go to the source code of this file.

Data Structures

struct  srv
struct  srv_context

Functions

int ast_get_srv (struct ast_channel *chan, char *host, int hostlen, int *port, const char *service)
static int parse_srv (char *host, int hostlen, int *portno, unsigned char *answer, int len, unsigned char *msg)
static int srv_callback (void *context, unsigned char *answer, int len, unsigned char *fullanswer)

Variables

srv __packed__


Detailed Description

DNS SRV Record Lookup Support for Asterisk.

Author:
Mark Spencer <markster@digium.com>
Note:
Funding provided by nic.at

Definition in file srv.c.


Function Documentation

int ast_get_srv ( struct ast_channel chan,
char *  host,
int  hostlen,
int *  port,
const char *  service 
)

Parameters:
chan Ast channel
host host name (return value)
hostlen Length of string "host"
port Port number (return value)
service Service tag for SRV lookup (like "_sip._udp" or "_stun._udp"

Definition at line 117 of file srv.c.

References ast_autoservice_start(), ast_autoservice_stop(), ast_search_dns(), context, and srv_callback().

Referenced by ast_get_ip_or_srv(), and create_addr().

00118 {
00119    struct srv_context context;
00120    int ret;
00121 
00122    context.host = host;
00123    context.hostlen = hostlen;
00124    context.port = port;
00125 
00126    if (chan && ast_autoservice_start(chan) < 0)
00127       return -1;
00128 
00129    ret = ast_search_dns(&context, service, C_IN, T_SRV, srv_callback);
00130 
00131    if (chan)
00132       ret |= ast_autoservice_stop(chan);
00133 
00134    if (ret <= 0) {
00135       host[0] = '\0';
00136       *port = -1;
00137       return ret;
00138    }
00139    return ret;
00140 }

static int parse_srv ( char *  host,
int  hostlen,
int *  portno,
unsigned char *  answer,
int  len,
unsigned char *  msg 
) [static]

Definition at line 65 of file srv.c.

References ast_log(), ast_verbose(), LOG_WARNING, option_verbose, and VERBOSE_PREFIX_3.

Referenced by srv_callback().

00066 {
00067    int res = 0;
00068    struct srv *srv = (struct srv *)answer;
00069    char repl[256] = "";
00070 
00071    if (len < sizeof(struct srv)) {
00072       printf("Length too short\n");
00073       return -1;
00074    }
00075    answer += sizeof(struct srv);
00076    len -= sizeof(struct srv);
00077 
00078    if ((res = dn_expand(msg, answer + len, answer, repl, sizeof(repl) - 1)) < 0) {
00079       ast_log(LOG_WARNING, "Failed to expand hostname\n");
00080       return -1;
00081    }
00082    if (res && strcmp(repl, ".")) {
00083       if (option_verbose > 3)
00084          ast_verbose( VERBOSE_PREFIX_3 "parse_srv: SRV mapped to host %s, port %d\n", repl, ntohs(srv->portnum));
00085       if (host) {
00086          ast_copy_string(host, repl, hostlen);
00087          host[hostlen-1] = '\0';
00088       }
00089       if (portno)
00090          *portno = ntohs(srv->portnum);
00091       return 0;
00092    }
00093    return -1;
00094 }

static int srv_callback ( void *  context,
unsigned char *  answer,
int  len,
unsigned char *  fullanswer 
) [static]

Definition at line 102 of file srv.c.

References ast_log(), ast_strlen_zero(), srv_context::host, srv_context::hostlen, LOG_WARNING, parse_srv(), and srv_context::port.

Referenced by ast_get_srv().

00103 {
00104    struct srv_context *c = (struct srv_context *)context;
00105 
00106    if (parse_srv(c->host, c->hostlen, c->port, answer, len, fullanswer)) {
00107       ast_log(LOG_WARNING, "Failed to parse srv\n");
00108       return -1;
00109    }
00110 
00111    if (!ast_strlen_zero(c->host))
00112       return 1;
00113 
00114    return 0;
00115 }


Variable Documentation

struct srv __packed__


Generated on Fri Aug 24 02:27:31 2007 for Asterisk - the Open Source PBX by  doxygen 1.5.1