srv.h File Reference

Support for DNS SRV records, used in to locate SIP services. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int ast_get_srv (struct ast_channel *chan, char *host, int hostlen, int *port, const char *service)
 Lookup entry in SRV records Returns 1 if found, 0 if not found, -1 on hangup.
void ast_srv_cleanup (struct srv_context **context)
 Cleanup resources associated with ast_srv_lookup.
int ast_srv_get_nth_record (struct srv_context *context, int record_num, const char **host, unsigned short *port, unsigned short *priority, unsigned short *weight)
 Retrieve details from a specific SRV record.
unsigned int ast_srv_get_record_count (struct srv_context *context)
 Get the number of records for a given SRV context.
int ast_srv_lookup (struct srv_context **context, const char *service, const char **host, unsigned short *port)
 Retrieve set of SRV lookups, in order.


Detailed Description

Support for DNS SRV records, used in to locate SIP services.

Note:
Note: This SRV record support will respect the priority and weight elements of the records that are returned, but there are no provisions for retrying or failover between records.

Definition in file srv.h.


Function Documentation

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

Lookup entry in SRV records Returns 1 if found, 0 if not found, -1 on hangup.

Only do SRV record lookup if you get a domain without a port. If you get a port #, it's a DNS host name.

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 261 of file srv.c.

References ast_autoservice_start(), ast_autoservice_stop(), ast_copy_string(), ast_debug, ast_free, AST_LIST_HEAD_NOLOCK_INIT_VALUE, AST_LIST_REMOVE_HEAD, ast_search_dns(), srv_context::entries, srv_context::have_weights, srv_entry::host, srv_entry::port, process_weights(), and srv_callback().

Referenced by ast_get_ip_or_srv(), and create_addr().

00262 {
00263    struct srv_context context = { .entries = AST_LIST_HEAD_NOLOCK_INIT_VALUE };
00264    struct srv_entry *current;
00265    int ret;
00266 
00267    if (chan && ast_autoservice_start(chan) < 0) {
00268       return -1;
00269    }
00270 
00271    ret = ast_search_dns(&context, service, C_IN, T_SRV, srv_callback);
00272 
00273    if (context.have_weights) {
00274       process_weights(&context);
00275    }
00276 
00277    if (chan) {
00278       ret |= ast_autoservice_stop(chan);
00279    }
00280 
00281    /* TODO: there could be a "." entry in the returned list of
00282       answers... if so, this requires special handling */
00283 
00284    /* the list of entries will be sorted in the proper selection order
00285       already, so we just need the first one (if any) */
00286 
00287    if ((ret > 0) && (current = AST_LIST_REMOVE_HEAD(&context.entries, list))) {
00288       ast_copy_string(host, current->host, hostlen);
00289       *port = current->port;
00290       ast_free(current);
00291       ast_debug(4, "ast_get_srv: SRV lookup for '%s' mapped to host %s, port %d\n",
00292                 service, host, *port);
00293    } else {
00294       host[0] = '\0';
00295       *port = -1;
00296    }
00297 
00298    while ((current = AST_LIST_REMOVE_HEAD(&context.entries, list))) {
00299       ast_free(current);
00300    }
00301 
00302    return ret;
00303 }

void ast_srv_cleanup ( struct srv_context **  context  ) 

Cleanup resources associated with ast_srv_lookup.

Parameters:
context Pointer passed into ast_srv_lookup

Definition at line 249 of file srv.c.

References ast_srv_lookup(), NULL, and srv_entry::port.

Referenced by launch_ha_netscript(), srds_destroy_cb(), and srv_datastore_setup().

00250 {
00251    const char *host;
00252    unsigned short port;
00253 
00254    if (*context) {
00255       /* We have a context to clean up. */
00256       while (!(ast_srv_lookup(context, NULL, &host, &port))) {
00257       }
00258    }
00259 }

int ast_srv_get_nth_record ( struct srv_context context,
int  record_num,
const char **  host,
unsigned short *  port,
unsigned short *  priority,
unsigned short *  weight 
)

Retrieve details from a specific SRV record.

After calling ast_srv_lookup, the srv_context will contain the data from several records. You can retrieve the data of a specific one by asking for a specific record number. The records are sorted based on priority and secondarily based on weight. See RFC 2782 for the exact sorting rules.

Parameters:
context The context returned by ast_srv_lookup
record_num The 1-indexed record number to retrieve
[out] host The host portion of the record
[out] port The port portion of the record
[out] priority The priority portion of the record
[out] weight The weight portion of the record
Return values:
-1 Failed to retrieve information. Likely due to an out of range record_num
0 Success

Definition at line 310 of file srv.c.

References AST_LIST_TRAVERSE, srv_context::entries, srv_entry::host, srv_context::num_records, srv_entry::port, srv_entry::priority, and srv_entry::weight.

Referenced by srv_result_read().

00312 {
00313    int i = 1;
00314    int res = -1;
00315    struct srv_entry *entry;
00316 
00317    if (record_num < 1 || record_num > context->num_records) {
00318       return res;
00319    }
00320 
00321    AST_LIST_TRAVERSE(&context->entries, entry, list) {
00322       if (i == record_num) {
00323          *host = entry->host;
00324          *port = entry->port;
00325          *priority = entry->priority;
00326          *weight = entry->weight;
00327          res = 0;
00328          break;
00329       }
00330       ++i;
00331    }
00332 
00333    return res;
00334 }

unsigned int ast_srv_get_record_count ( struct srv_context context  ) 

Get the number of records for a given SRV context.

This is meant to be used after calling ast_srv_lookup, so that one may retrieve the number of records returned during a specific SRV lookup.

Parameters:
context The context returned by ast_srv_lookup
Returns:
Number of records in context

Definition at line 305 of file srv.c.

References srv_context::num_records.

Referenced by srv_result_read().

00306 {
00307    return context->num_records;
00308 }

int ast_srv_lookup ( struct srv_context **  context,
const char *  service,
const char **  host,
unsigned short *  port 
)

Retrieve set of SRV lookups, in order.

Parameters:
[in] context A pointer in which to hold the result
[in] service The service name to look up
[out] host Result host
[out] port Associated TCP portnum
Return values:
-1 Query failed
0 Result exists in host and port
1 No more results

Definition at line 204 of file srv.c.

References ast_calloc, ast_free, AST_LIST_FIRST, AST_LIST_HEAD_INIT_NOLOCK, AST_LIST_NEXT, AST_LIST_REMOVE_HEAD, AST_LIST_TRAVERSE, ast_search_dns(), NULL, process_weights(), and srv_callback().

Referenced by ast_srv_cleanup(), launch_ha_netscript(), and srv_datastore_setup().

00205 {
00206    struct srv_entry *cur;
00207 
00208    if (*context == NULL) {
00209       if (!(*context = ast_calloc(1, sizeof(struct srv_context)))) {
00210          return -1;
00211       }
00212       AST_LIST_HEAD_INIT_NOLOCK(&(*context)->entries);
00213 
00214       if ((ast_search_dns(*context, service, C_IN, T_SRV, srv_callback)) < 0) {
00215          ast_free(*context);
00216          *context = NULL;
00217          return -1;
00218       }
00219 
00220       if ((*context)->have_weights) {
00221          process_weights(*context);
00222       }
00223 
00224       (*context)->prev = AST_LIST_FIRST(&(*context)->entries);
00225       *host = (*context)->prev->host;
00226       *port = (*context)->prev->port;
00227       AST_LIST_TRAVERSE(&(*context)->entries, cur, list) {
00228          ++((*context)->num_records);
00229       }
00230       return 0;
00231    }
00232 
00233    if (((*context)->prev = AST_LIST_NEXT((*context)->prev, list))) {
00234       /* Retrieve next item in result */
00235       *host = (*context)->prev->host;
00236       *port = (*context)->prev->port;
00237       return 0;
00238    } else {
00239       /* No more results */
00240       while ((cur = AST_LIST_REMOVE_HEAD(&(*context)->entries, list))) {
00241          ast_free(cur);
00242       }
00243       ast_free(*context);
00244       *context = NULL;
00245       return 1;
00246    }
00247 }


Generated on Thu Apr 16 06:33:54 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6