2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006 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 if (ast_srv_lookup(&srds->context, service, &host, &port) < 0) {
103 ast_log(LOG_NOTICE, "Error performing lookup of service '%s'\n", service);
108 strcpy(srds->id, service);
110 if (!(datastore = ast_datastore_alloc(&srv_result_datastore_info, srds->id))) {
111 ast_srv_cleanup(&srds->context);
116 datastore->data = srds;
117 ast_channel_lock(chan);
118 ast_channel_datastore_add(chan, datastore);
119 ast_channel_unlock(chan);
120 return srds->context;
123 static int srv_query_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
126 ast_log(LOG_WARNING, "%s cannot be used without a channel\n", cmd);
130 if (ast_strlen_zero(data)) {
131 ast_log(LOG_WARNING, "%s requires a service as an argument\n", cmd);
135 if (!srv_datastore_setup(data, chan)) {
139 ast_copy_string(buf, data, len);
144 static struct ast_custom_function srv_query_function = {
146 .read = srv_query_read,
149 static int srv_result_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
151 struct srv_result_datastore *srds;
152 struct ast_datastore *datastore;
153 struct srv_context *srv_context;
156 unsigned short port, priority, weight;
158 AST_DECLARE_APP_ARGS(args,
160 AST_APP_ARG(resultnum);
165 ast_log(LOG_WARNING, "%s cannot be used without a channel\n", cmd);
169 if (ast_strlen_zero(data)) {
170 ast_log(LOG_WARNING, "%s requires two arguments (id and resultnum)\n", cmd);
174 parse = ast_strdupa(data);
176 AST_STANDARD_APP_ARGS(args, parse);
178 ast_channel_lock(chan);
179 datastore = ast_channel_datastore_find(chan, &srv_result_datastore_info, args.id);
180 ast_channel_unlock(chan);
183 /* They apparently decided to call SRVRESULT without first calling SRVQUERY.
184 * No problem, we'll do the SRV lookup now.
186 srv_context = srv_datastore_setup(args.id, chan);
191 srds = datastore->data;
192 srv_context = srds->context;
195 if (!strcasecmp(args.resultnum, "getnum")) {
196 snprintf(buf, len, "%u", ast_srv_get_record_count(srv_context));
200 if (ast_strlen_zero(args.field)) {
201 ast_log(LOG_ERROR, "A field must be provided when requesting SRV data\n");
205 if (sscanf(args.resultnum, "%30u", &num) != 1) {
206 ast_log(LOG_ERROR, "Invalid value '%s' for resultnum to %s\n", args.resultnum, cmd);
210 if (ast_srv_get_nth_record(srv_context, num, &host, &port, &priority, &weight)) {
211 ast_log(LOG_ERROR, "Failed to get record number %u for %s\n", num, cmd);
215 if (!strcasecmp(args.field, "host")) {
216 ast_copy_string(buf, host, len);
217 } else if (!strcasecmp(args.field, "port")) {
218 snprintf(buf, len, "%u", port);
219 } else if (!strcasecmp(args.field, "priority")) {
220 snprintf(buf, len, "%u", priority);
221 } else if (!strcasecmp(args.field, "weight")) {
222 snprintf(buf, len, "%u", weight);
224 ast_log(LOG_WARNING, "Unrecognized SRV field '%s'\n", args.field);
231 static struct ast_custom_function srv_result_function = {
233 .read = srv_result_read,
236 static int unload_module(void)
240 res |= ast_custom_function_unregister(&srv_query_function);
241 res |= ast_custom_function_unregister(&srv_result_function);
246 static int load_module(void)
248 int res = AST_MODULE_LOAD_SUCCESS;
250 res |= ast_custom_function_register(&srv_query_function);
251 res |= ast_custom_function_register(&srv_result_function);
256 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SRV related dialplan functions");