Sat Mar 24 23:30:19 2007

Asterisk developer's documentation


srv.c File Reference

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

#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.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, char *answer, int len, char *msg)
static int srv_callback (void *context, char *answer, int len, char *fullanswer)

Variables

srv __packed__


Detailed Description

DNS SRV Record Lookup Support for Asterisk.

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 114 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().

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

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

Definition at line 62 of file srv.c.

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

Referenced by srv_callback().

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

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

Definition at line 99 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().

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


Variable Documentation

struct srv __packed__
 


Generated on Sat Mar 24 23:30:19 2007 for Asterisk - the Open Source PBX by  doxygen 1.4.6