Sat Apr 26 2014 22:03:19

Asterisk developer's documentation


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)
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:
chanAst channel
hosthost name (return value)
hostlenLength of string "host"
portPort number (return value)
serviceService tag for SRV lookup (like "_sip._udp" or "_stun._udp"

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

{
   struct srv_context context = { .entries = AST_LIST_HEAD_NOLOCK_INIT_VALUE };
   struct srv_entry *current;
   int ret;

   if (chan && ast_autoservice_start(chan) < 0) {
      return -1;
   }

   ret = ast_search_dns(&context, service, C_IN, T_SRV, srv_callback);

   if (context.have_weights) {
      process_weights(&context);
   }

   if (chan) {
      ret |= ast_autoservice_stop(chan);
   }

   /* TODO: there could be a "." entry in the returned list of
      answers... if so, this requires special handling */

   /* the list of entries will be sorted in the proper selection order
      already, so we just need the first one (if any) */

   if ((ret > 0) && (current = AST_LIST_REMOVE_HEAD(&context.entries, list))) {
      ast_copy_string(host, current->host, hostlen);
      *port = current->port;
      ast_free(current);
      ast_debug(4, "ast_get_srv: SRV lookup for '%s' mapped to host %s, port %d\n",
                service, host, *port);
   } else {
      host[0] = '\0';
      *port = -1;
   }

   while ((current = AST_LIST_REMOVE_HEAD(&context.entries, list))) {
      ast_free(current);
   }

   return ret;
}
void ast_srv_cleanup ( struct srv_context **  context)

Cleanup resources associated with ast_srv_lookup.

Parameters:
contextPointer passed into ast_srv_lookup

Definition at line 251 of file srv.c.

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

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

{
   const char *host;
   unsigned short port;

   if (*context) {
      /* We have a context to clean up. */
      while (!(ast_srv_lookup(context, NULL, &host, &port))) {
      }
   }
}
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:
contextThe context returned by ast_srv_lookup
record_numThe 1-indexed record number to retrieve
[out]hostThe host portion of the record
[out]portThe port portion of the record
[out]priorityThe priority portion of the record
[out]weightThe weight portion of the record
Return values:
-1Failed to retrieve information. Likely due to an out of range record_num
0Success

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

{
   int i = 1;
   int res = -1;
   struct srv_entry *entry;

   if (record_num < 1 || record_num > context->num_records) {
      return res;
   }

   AST_LIST_TRAVERSE(&context->entries, entry, list) {
      if (i == record_num) {
         *host = entry->host;
         *port = entry->port;
         *priority = entry->priority;
         *weight = entry->weight;
         res = 0;
         break;
      }
      ++i;
   }

   return res;
}
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:
contextThe context returned by ast_srv_lookup
Returns:
Number of records in context

Definition at line 307 of file srv.c.

References srv_context::num_records.

Referenced by srv_result_read().

{
   return context->num_records;
}
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]contextA pointer in which to hold the result
[in]serviceThe service name to look up
[out]hostResult host
[out]portAssociated TCP portnum
Return values:
-1Query failed
0Result exists in host and port
1No more results

Definition at line 206 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(), process_weights(), and srv_callback().

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

{
   struct srv_entry *cur;

   if (*context == NULL) {
      if (!(*context = ast_calloc(1, sizeof(struct srv_context)))) {
         return -1;
      }
      AST_LIST_HEAD_INIT_NOLOCK(&(*context)->entries);

      if ((ast_search_dns(*context, service, C_IN, T_SRV, srv_callback)) < 0) {
         ast_free(*context);
         *context = NULL;
         return -1;
      }

      if ((*context)->have_weights) {
         process_weights(*context);
      }

      (*context)->prev = AST_LIST_FIRST(&(*context)->entries);
      *host = (*context)->prev->host;
      *port = (*context)->prev->port;
      AST_LIST_TRAVERSE(&(*context)->entries, cur, list) {
         ++((*context)->num_records);
      }
      return 0;
   }

   if (((*context)->prev = AST_LIST_NEXT((*context)->prev, list))) {
      /* Retrieve next item in result */
      *host = (*context)->prev->host;
      *port = (*context)->prev->port;
      return 0;
   } else {
      /* No more results */
      while ((cur = AST_LIST_REMOVE_HEAD(&(*context)->entries, list))) {
         ast_free(cur);
      }
      ast_free(*context);
      *context = NULL;
      return 1;
   }
}