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>
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30 #include "asterisk/module.h"
31 #include "asterisk/srv.h"
32 #include "asterisk/pbx.h"
33 #include "asterisk/app.h"
34 #include "asterisk/datastore.h"
35 #include "asterisk/channel.h"
38 <function name="SRVQUERY" language="en_US">
40 Initiate an SRV query.
43 <parameter name="service" required="true">
44 <para>The service for which to look up SRV records. An example would be something
45 like <literal>_sip._udp.example.com</literal></para>
49 <para>This will do an SRV lookup of the given service.</para>
52 <function name="SRVRESULT" language="en_US">
54 Retrieve results from an SRVQUERY.
57 <parameter name="id" required="true">
58 <para>The identifier returned by the SRVQUERY function.</para>
60 <parameter name="resultnum" required="true">
61 <para>The number of the result that you want to retrieve.</para>
62 <para>Results start at <literal>1</literal>. If this argument is specified
63 as <literal>getnum</literal>, then it will return the total number of results
64 that are available.</para>
68 <para>This function will retrieve results from a previous use
69 of the SRVQUERY function.</para>
74 struct srv_result_datastore {
75 struct srv_context *context;
79 static void srds_destroy_cb(void *data)
81 struct srv_result_datastore *datastore = data;
82 ast_srv_cleanup(&datastore->context);
86 static const struct ast_datastore_info srv_result_datastore_info = {
88 .destroy = srds_destroy_cb,
91 static struct srv_context *srv_datastore_setup(const char *service, struct ast_channel *chan)
93 struct srv_result_datastore *srds;
94 struct ast_datastore *datastore;
98 if (!(srds = ast_calloc(1, sizeof(*srds) + strlen(service)))) {
102 ast_autoservice_start(chan);
103 if (ast_srv_lookup(&srds->context, service, &host, &port) < 0) {
104 ast_autoservice_stop(chan);
105 ast_log(LOG_NOTICE, "Error performing lookup of service '%s'\n", service);
109 ast_autoservice_stop(chan);
111 strcpy(srds->id, service);
113 if (!(datastore = ast_datastore_alloc(&srv_result_datastore_info, srds->id))) {
114 ast_srv_cleanup(&srds->context);
119 datastore->data = srds;
120 ast_channel_lock(chan);
121 ast_channel_datastore_add(chan, datastore);
122 ast_channel_unlock(chan);
123 return srds->context;
126 static int srv_query_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
129 ast_log(LOG_WARNING, "%s cannot be used without a channel\n", cmd);
133 if (ast_strlen_zero(data)) {
134 ast_log(LOG_WARNING, "%s requires a service as an argument\n", cmd);
138 if (!srv_datastore_setup(data, chan)) {
142 ast_copy_string(buf, data, len);
147 static struct ast_custom_function srv_query_function = {
149 .read = srv_query_read,
152 static int srv_result_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
154 struct srv_result_datastore *srds;
155 struct ast_datastore *datastore;
156 struct srv_context *srv_context;
159 unsigned short port, priority, weight;
161 AST_DECLARE_APP_ARGS(args,
163 AST_APP_ARG(resultnum);
168 ast_log(LOG_WARNING, "%s cannot be used without a channel\n", cmd);
172 if (ast_strlen_zero(data)) {
173 ast_log(LOG_WARNING, "%s requires two arguments (id and resultnum)\n", cmd);
177 parse = ast_strdupa(data);
179 AST_STANDARD_APP_ARGS(args, parse);
181 ast_channel_lock(chan);
182 datastore = ast_channel_datastore_find(chan, &srv_result_datastore_info, args.id);
183 ast_channel_unlock(chan);
186 /* They apparently decided to call SRVRESULT without first calling SRVQUERY.
187 * No problem, we'll do the SRV lookup now.
189 srv_context = srv_datastore_setup(args.id, chan);
194 srds = datastore->data;
195 srv_context = srds->context;
198 if (!strcasecmp(args.resultnum, "getnum")) {
199 snprintf(buf, len, "%u", ast_srv_get_record_count(srv_context));
203 if (ast_strlen_zero(args.field)) {
204 ast_log(LOG_ERROR, "A field must be provided when requesting SRV data\n");
208 if (sscanf(args.resultnum, "%30u", &num) != 1) {
209 ast_log(LOG_ERROR, "Invalid value '%s' for resultnum to %s\n", args.resultnum, cmd);
213 if (ast_srv_get_nth_record(srv_context, num, &host, &port, &priority, &weight)) {
214 ast_log(LOG_ERROR, "Failed to get record number %u for %s\n", num, cmd);
218 if (!strcasecmp(args.field, "host")) {
219 ast_copy_string(buf, host, len);
220 } else if (!strcasecmp(args.field, "port")) {
221 snprintf(buf, len, "%u", port);
222 } else if (!strcasecmp(args.field, "priority")) {
223 snprintf(buf, len, "%u", priority);
224 } else if (!strcasecmp(args.field, "weight")) {
225 snprintf(buf, len, "%u", weight);
227 ast_log(LOG_WARNING, "Unrecognized SRV field '%s'\n", args.field);
234 static struct ast_custom_function srv_result_function = {
236 .read = srv_result_read,
239 static int unload_module(void)
243 res |= ast_custom_function_unregister(&srv_query_function);
244 res |= ast_custom_function_unregister(&srv_result_function);
249 static int load_module(void)
251 int res = ast_custom_function_register(&srv_query_function);
253 return AST_MODULE_LOAD_DECLINE;
255 res = ast_custom_function_register(&srv_result_function);
257 return AST_MODULE_LOAD_DECLINE;
260 return AST_MODULE_LOAD_SUCCESS;;
263 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SRV related dialplan functions");