Support for DNS SRV records, used in to locate SIP services. More...
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. |
Support for DNS SRV records, used in to locate SIP services.
Definition in file srv.h.
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.
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 263 of file srv.c.
References ast_autoservice_start(), ast_autoservice_stop(), ast_copy_string(), ast_free, AST_LIST_HEAD_NOLOCK_INIT_VALUE, AST_LIST_REMOVE_HEAD, ast_search_dns(), ast_verb, 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().
00264 { 00265 struct srv_context context = { .entries = AST_LIST_HEAD_NOLOCK_INIT_VALUE }; 00266 struct srv_entry *current; 00267 int ret; 00268 00269 if (chan && ast_autoservice_start(chan) < 0) { 00270 return -1; 00271 } 00272 00273 ret = ast_search_dns(&context, service, C_IN, T_SRV, srv_callback); 00274 00275 if (context.have_weights) { 00276 process_weights(&context); 00277 } 00278 00279 if (chan) { 00280 ret |= ast_autoservice_stop(chan); 00281 } 00282 00283 /* TODO: there could be a "." entry in the returned list of 00284 answers... if so, this requires special handling */ 00285 00286 /* the list of entries will be sorted in the proper selection order 00287 already, so we just need the first one (if any) */ 00288 00289 if ((ret > 0) && (current = AST_LIST_REMOVE_HEAD(&context.entries, list))) { 00290 ast_copy_string(host, current->host, hostlen); 00291 *port = current->port; 00292 ast_free(current); 00293 ast_verb(4, "ast_get_srv: SRV lookup for '%s' mapped to host %s, port %d\n", 00294 service, host, *port); 00295 } else { 00296 host[0] = '\0'; 00297 *port = -1; 00298 } 00299 00300 while ((current = AST_LIST_REMOVE_HEAD(&context.entries, list))) { 00301 ast_free(current); 00302 } 00303 00304 return ret; 00305 }
void ast_srv_cleanup | ( | struct srv_context ** | context | ) |
Cleanup resources associated with ast_srv_lookup.
context | Pointer passed into ast_srv_lookup |
Definition at line 251 of file srv.c.
References ast_srv_lookup(), and srv_entry::port.
Referenced by launch_ha_netscript(), srds_destroy_cb(), and srv_datastore_setup().
00252 { 00253 const char *host; 00254 unsigned short port; 00255 00256 if (*context) { 00257 /* We have a context to clean up. */ 00258 while (!(ast_srv_lookup(context, NULL, &host, &port))) { 00259 } 00260 } 00261 }
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.
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 |
-1 | Failed to retrieve information. Likely due to an out of range record_num | |
0 | Success |
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().
00314 { 00315 int i = 1; 00316 int res = -1; 00317 struct srv_entry *entry; 00318 00319 if (record_num < 1 || record_num > context->num_records) { 00320 return res; 00321 } 00322 00323 AST_LIST_TRAVERSE(&context->entries, entry, list) { 00324 if (i == record_num) { 00325 *host = entry->host; 00326 *port = entry->port; 00327 *priority = entry->priority; 00328 *weight = entry->weight; 00329 res = 0; 00330 break; 00331 } 00332 ++i; 00333 } 00334 00335 return res; 00336 }
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.
context | The context returned by ast_srv_lookup |
Definition at line 307 of file srv.c.
References srv_context::num_records.
Referenced by srv_result_read().
00308 { 00309 return context->num_records; 00310 }
int ast_srv_lookup | ( | struct srv_context ** | context, | |
const char * | service, | |||
const char ** | host, | |||
unsigned short * | port | |||
) |
Retrieve set of SRV lookups, in order.
[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 |
-1 | Query failed | |
0 | Result exists in host and port | |
1 | No 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().
00207 { 00208 struct srv_entry *cur; 00209 00210 if (*context == NULL) { 00211 if (!(*context = ast_calloc(1, sizeof(struct srv_context)))) { 00212 return -1; 00213 } 00214 AST_LIST_HEAD_INIT_NOLOCK(&(*context)->entries); 00215 00216 if ((ast_search_dns(*context, service, C_IN, T_SRV, srv_callback)) < 0) { 00217 ast_free(*context); 00218 *context = NULL; 00219 return -1; 00220 } 00221 00222 if ((*context)->have_weights) { 00223 process_weights(*context); 00224 } 00225 00226 (*context)->prev = AST_LIST_FIRST(&(*context)->entries); 00227 *host = (*context)->prev->host; 00228 *port = (*context)->prev->port; 00229 AST_LIST_TRAVERSE(&(*context)->entries, cur, list) { 00230 ++((*context)->num_records); 00231 } 00232 return 0; 00233 } 00234 00235 if (((*context)->prev = AST_LIST_NEXT((*context)->prev, list))) { 00236 /* Retrieve next item in result */ 00237 *host = (*context)->prev->host; 00238 *port = (*context)->prev->port; 00239 return 0; 00240 } else { 00241 /* No more results */ 00242 while ((cur = AST_LIST_REMOVE_HEAD(&(*context)->entries, list))) { 00243 ast_free(cur); 00244 } 00245 ast_free(*context); 00246 *context = NULL; 00247 return 1; 00248 } 00249 }