2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2010 Digium, Inc.
6 * See http://www.asterisk.org for more information about
7 * the Asterisk project. Please do not directly contact
8 * any of the maintainers of this project for assistance;
9 * the project provides a web site, mailing lists and IRC
10 * channels for your use.
12 * This program is free software, distributed under the terms of
13 * the GNU General Public License Version 2. See the LICENSE file
14 * at the top of the source tree.
19 * \brief SRV Functions
21 * \author Mark Michelson <mmichelson@digium.com>
27 <support_level>core</support_level>
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include "asterisk/module.h"
35 #include "asterisk/srv.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/app.h"
38 #include "asterisk/datastore.h"
39 #include "asterisk/channel.h"
42 <function name="SRVQUERY" language="en_US">
44 Initiate an SRV query.
47 <parameter name="service" required="true">
48 <para>The service for which to look up SRV records. An example would be something
49 like <literal>_sip._udp.example.com</literal></para>
53 <para>This will do an SRV lookup of the given service.</para>
56 <function name="SRVRESULT" language="en_US">
58 Retrieve results from an SRVQUERY.
61 <parameter name="id" required="true">
62 <para>The identifier returned by the SRVQUERY function.</para>
64 <parameter name="resultnum" required="true">
65 <para>The number of the result that you want to retrieve.</para>
66 <para>Results start at <literal>1</literal>. If this argument is specified
67 as <literal>getnum</literal>, then it will return the total number of results
68 that are available.</para>
72 <para>This function will retrieve results from a previous use
73 of the SRVQUERY function.</para>
78 struct srv_result_datastore {
79 struct srv_context *context;
83 static void srds_destroy_cb(void *data)
85 struct srv_result_datastore *datastore = data;
86 ast_srv_cleanup(&datastore->context);
90 static const struct ast_datastore_info srv_result_datastore_info = {
92 .destroy = srds_destroy_cb,
95 static struct srv_context *srv_datastore_setup(const char *service, struct ast_channel *chan)
97 struct srv_result_datastore *srds;
98 struct ast_datastore *datastore;
102 if (!(srds = ast_calloc(1, sizeof(*srds) + strlen(service)))) {
106 ast_autoservice_start(chan);
107 if (ast_srv_lookup(&srds->context, service, &host, &port) < 0) {
108 ast_autoservice_stop(chan);
109 ast_log(LOG_NOTICE, "Error performing lookup of service '%s'\n", service);
113 ast_autoservice_stop(chan);
115 strcpy(srds->id, service);
117 if (!(datastore = ast_datastore_alloc(&srv_result_datastore_info, srds->id))) {
118 ast_srv_cleanup(&srds->context);
123 datastore->data = srds;
124 ast_channel_lock(chan);
125 ast_channel_datastore_add(chan, datastore);
126 ast_channel_unlock(chan);
127 return srds->context;
130 static int srv_query_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
132 struct ast_datastore *datastore;
135 ast_log(LOG_WARNING, "%s cannot be used without a channel\n", cmd);
139 if (ast_strlen_zero(data)) {
140 ast_log(LOG_WARNING, "%s requires a service as an argument\n", cmd);
144 /* If they already called SRVQUERY for this service once,
145 * we need to kill the old datastore.
147 ast_channel_lock(chan);
148 datastore = ast_channel_datastore_find(chan, &srv_result_datastore_info, data);
149 ast_channel_unlock(chan);
152 ast_channel_datastore_remove(chan, datastore);
153 ast_datastore_free(datastore);
156 if (!srv_datastore_setup(data, chan)) {
160 ast_copy_string(buf, data, len);
165 static struct ast_custom_function srv_query_function = {
167 .read = srv_query_read,
170 static int srv_result_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
172 struct srv_result_datastore *srds;
173 struct ast_datastore *datastore;
174 struct srv_context *srv_context;
177 unsigned short port, priority, weight;
179 AST_DECLARE_APP_ARGS(args,
181 AST_APP_ARG(resultnum);
186 ast_log(LOG_WARNING, "%s cannot be used without a channel\n", cmd);
190 if (ast_strlen_zero(data)) {
191 ast_log(LOG_WARNING, "%s requires two arguments (id and resultnum)\n", cmd);
195 parse = ast_strdupa(data);
197 AST_STANDARD_APP_ARGS(args, parse);
199 ast_channel_lock(chan);
200 datastore = ast_channel_datastore_find(chan, &srv_result_datastore_info, args.id);
201 ast_channel_unlock(chan);
204 /* They apparently decided to call SRVRESULT without first calling SRVQUERY.
205 * No problem, we'll do the SRV lookup now.
207 srv_context = srv_datastore_setup(args.id, chan);
212 srds = datastore->data;
213 srv_context = srds->context;
216 if (!strcasecmp(args.resultnum, "getnum")) {
217 snprintf(buf, len, "%u", ast_srv_get_record_count(srv_context));
221 if (ast_strlen_zero(args.field)) {
222 ast_log(LOG_ERROR, "A field must be provided when requesting SRV data\n");
226 if (sscanf(args.resultnum, "%30u", &num) != 1) {
227 ast_log(LOG_ERROR, "Invalid value '%s' for resultnum to %s\n", args.resultnum, cmd);
231 if (ast_srv_get_nth_record(srv_context, num, &host, &port, &priority, &weight)) {
232 ast_log(LOG_ERROR, "Failed to get record number %u for %s\n", num, cmd);
236 if (!strcasecmp(args.field, "host")) {
237 ast_copy_string(buf, host, len);
238 } else if (!strcasecmp(args.field, "port")) {
239 snprintf(buf, len, "%u", port);
240 } else if (!strcasecmp(args.field, "priority")) {
241 snprintf(buf, len, "%u", priority);
242 } else if (!strcasecmp(args.field, "weight")) {
243 snprintf(buf, len, "%u", weight);
245 ast_log(LOG_WARNING, "Unrecognized SRV field '%s'\n", args.field);
252 static struct ast_custom_function srv_result_function = {
254 .read = srv_result_read,
257 static int unload_module(void)
261 res |= ast_custom_function_unregister(&srv_query_function);
262 res |= ast_custom_function_unregister(&srv_result_function);
267 static int load_module(void)
269 int res = ast_custom_function_register(&srv_query_function);
271 return AST_MODULE_LOAD_DECLINE;
273 res = ast_custom_function_register(&srv_result_function);
275 return AST_MODULE_LOAD_DECLINE;
278 return AST_MODULE_LOAD_SUCCESS;;
281 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SRV related dialplan functions");