Address Russell's comments on func_srv from reviewboard.
[asterisk/asterisk.git] / funcs / func_srv.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2010 Digium, Inc.
5  *
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.
11  *
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.
15  */
16
17 /*! \file
18  *
19  * \brief SRV Functions
20  *
21  * \author Mark Michelson <mmichelson@digium.com>
22  *
23  * \ingroup functions
24  */
25
26 #include "asterisk.h"
27
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
29
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"
36
37 /*** DOCUMENTATION
38         <function name="SRVQUERY" language="en_US">
39                 <synopsis>
40                         Initiate an SRV query.
41                 </synopsis>
42                 <syntax>
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>
46                         </parameter>
47                 </syntax>
48                 <description>
49                         <para>This will do an SRV lookup of the given service.</para>
50                 </description>
51         </function>
52         <function name="SRVRESULT" language="en_US">
53                 <synopsis>
54                         Retrieve results from an SRVQUERY.
55                 </synopsis>
56                 <syntax>
57                         <parameter name="id" required="true">
58                                 <para>The identifier returned by the SRVQUERY function.</para>
59                         </parameter>
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>
65                         </parameter>
66                 </syntax>
67                 <description>
68                         <para>This function will retrieve results from a previous use
69                         of the SRVQUERY function.</para>
70                 </description>
71         </function>
72  ***/
73
74 struct srv_result_datastore {
75         struct srv_context *context;
76         char id[1];
77 };
78
79 static void srds_destroy_cb(void *data)
80 {
81         struct srv_result_datastore *datastore = data;
82         ast_srv_cleanup(&datastore->context);
83         ast_free(datastore);
84 }
85
86 static const struct ast_datastore_info srv_result_datastore_info = {
87         .type = "SRVQUERY",
88         .destroy = srds_destroy_cb,
89 };
90
91 static struct srv_context *srv_datastore_setup(const char *service, struct ast_channel *chan)
92 {
93         struct srv_result_datastore *srds;
94         struct ast_datastore *datastore;
95         const char *host;
96         unsigned short port;
97
98         if (!(srds = ast_calloc(1, sizeof(*srds) + strlen(service)))) {
99                 return NULL;
100         }
101
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);
106                 ast_free(srds);
107                 return NULL;
108         }
109         ast_autoservice_stop(chan);
110
111         strcpy(srds->id, service);
112
113         if (!(datastore = ast_datastore_alloc(&srv_result_datastore_info, srds->id))) {
114                 ast_srv_cleanup(&srds->context);
115                 ast_free(srds);
116                 return NULL;
117         }
118
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;
124 }
125
126 static int srv_query_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
127 {
128         if (!chan) {
129                 ast_log(LOG_WARNING, "%s cannot be used without a channel\n", cmd);
130                 return -1;
131         }
132
133         if (ast_strlen_zero(data)) {
134                 ast_log(LOG_WARNING, "%s requires a service as an argument\n", cmd);
135                 return -1;
136         }
137
138         if (!srv_datastore_setup(data, chan)) {
139                 return -1;
140         }
141
142         ast_copy_string(buf, data, len);
143
144         return 0;
145 }
146
147 static struct ast_custom_function srv_query_function = {
148         .name = "SRVQUERY",
149         .read = srv_query_read,
150 };
151
152 static int srv_result_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
153 {
154         struct srv_result_datastore *srds;
155         struct ast_datastore *datastore;
156         struct srv_context *srv_context;
157         char *parse;
158         const char *host;
159         unsigned short port, priority, weight;
160         unsigned int num;
161         AST_DECLARE_APP_ARGS(args,
162                 AST_APP_ARG(id);
163                 AST_APP_ARG(resultnum);
164                 AST_APP_ARG(field);
165         );
166
167         if (!chan) {
168                 ast_log(LOG_WARNING, "%s cannot be used without a channel\n", cmd);
169                 return -1;
170         }
171
172         if (ast_strlen_zero(data)) {
173                 ast_log(LOG_WARNING, "%s requires two arguments (id and resultnum)\n", cmd);
174                 return -1;
175         }
176
177         parse = ast_strdupa(data);
178
179         AST_STANDARD_APP_ARGS(args, parse);
180
181         ast_channel_lock(chan);
182         datastore = ast_channel_datastore_find(chan, &srv_result_datastore_info, args.id);
183         ast_channel_unlock(chan);
184
185         if (!datastore) {
186                 /* They apparently decided to call SRVRESULT without first calling SRVQUERY.
187                  * No problem, we'll do the SRV lookup now.
188                  */
189                 srv_context = srv_datastore_setup(args.id, chan);
190                 if (!srv_context) {
191                         return -1;
192                 }
193         } else {
194                 srds = datastore->data;
195                 srv_context = srds->context;
196         }
197
198         if (!strcasecmp(args.resultnum, "getnum")) {
199                 snprintf(buf, len, "%u", ast_srv_get_record_count(srv_context));
200                 return 0;
201         }
202
203         if (ast_strlen_zero(args.field)) {
204                 ast_log(LOG_ERROR, "A field must be provided when requesting SRV data\n");
205                 return -1;
206         }
207
208         if (sscanf(args.resultnum, "%30u", &num) != 1) {
209                 ast_log(LOG_ERROR, "Invalid value '%s' for resultnum to %s\n", args.resultnum, cmd);
210                 return -1;
211         }
212
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);
215                 return -1;
216         }
217
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);
226         } else {
227                 ast_log(LOG_WARNING, "Unrecognized SRV field '%s'\n", args.field);
228                 return -1;
229         }
230
231         return 0;
232 }
233
234 static struct ast_custom_function srv_result_function = {
235         .name = "SRVRESULT",
236         .read = srv_result_read,
237 };
238
239 static int unload_module(void)
240 {
241         int res = 0;
242
243         res |= ast_custom_function_unregister(&srv_query_function);
244         res |= ast_custom_function_unregister(&srv_result_function);
245
246         return res;
247 }
248
249 static int load_module(void)
250 {
251         int res = ast_custom_function_register(&srv_query_function);
252         if (res < 0) {
253                 return AST_MODULE_LOAD_DECLINE;
254         }
255         res = ast_custom_function_register(&srv_result_function);
256         if (res < 0) {
257                 return AST_MODULE_LOAD_DECLINE;
258         }
259
260         return AST_MODULE_LOAD_SUCCESS;;
261 }
262
263 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SRV related dialplan functions");