9715a276cf9f6cf61b47a17ffe0a28c9b2a0a3e0
[asterisk/asterisk.git] / funcs / func_srv.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006 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         if (ast_srv_lookup(&srds->context, service, &host, &port) < 0) {
103                 ast_log(LOG_NOTICE, "Error performing lookup of service '%s'\n", service);
104                 ast_free(srds);
105                 return NULL;
106         }
107
108         strcpy(srds->id, service);
109         
110         if (!(datastore = ast_datastore_alloc(&srv_result_datastore_info, srds->id))) {
111                 ast_srv_cleanup(&srds->context);
112                 ast_free(srds);
113                 return NULL;
114         }
115
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;
121 }
122
123 static int srv_query_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
124 {
125         if (!chan) {
126                 ast_log(LOG_WARNING, "%s cannot be used without a channel\n", cmd);
127                 return -1;
128         }
129
130         if (ast_strlen_zero(data)) {
131                 ast_log(LOG_WARNING, "%s requires a service as an argument\n", cmd);
132                 return -1;
133         }
134
135         if (!srv_datastore_setup(data, chan)) {
136                 return -1;
137         }
138
139         ast_copy_string(buf, data, len);
140
141         return 0;
142 }
143
144 static struct ast_custom_function srv_query_function = {
145         .name = "SRVQUERY",
146         .read = srv_query_read,
147 };
148
149 static int srv_result_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
150 {
151         struct srv_result_datastore *srds;
152         struct ast_datastore *datastore;
153         struct srv_context *srv_context;
154         char *parse;
155         const char *host;
156         unsigned short port, priority, weight;
157         unsigned int num;
158         AST_DECLARE_APP_ARGS(args,
159                 AST_APP_ARG(id);
160                 AST_APP_ARG(resultnum);
161                 AST_APP_ARG(field);
162         );
163
164         if (!chan) {
165                 ast_log(LOG_WARNING, "%s cannot be used without a channel\n", cmd);
166                 return -1;
167         }
168
169         if (ast_strlen_zero(data)) {
170                 ast_log(LOG_WARNING, "%s requires two arguments (id and resultnum)\n", cmd);
171                 return -1;
172         }
173
174         parse = ast_strdupa(data);
175
176         AST_STANDARD_APP_ARGS(args, parse);
177
178         ast_channel_lock(chan);
179         datastore = ast_channel_datastore_find(chan, &srv_result_datastore_info, args.id);
180         ast_channel_unlock(chan);
181
182         if (!datastore) {
183                 /* They apparently decided to call SRVRESULT without first calling SRVQUERY.
184                  * No problem, we'll do the SRV lookup now.
185                  */
186                 srv_context = srv_datastore_setup(args.id, chan);
187                 if (!srv_context) {
188                         return -1;
189                 }
190         } else {
191                 srds = datastore->data;
192                 srv_context = srds->context;
193         }
194
195         if (!strcasecmp(args.resultnum, "getnum")) {
196                 snprintf(buf, len, "%u", ast_srv_get_record_count(srv_context));
197                 return 0;
198         }
199
200         if (ast_strlen_zero(args.field)) {
201                 ast_log(LOG_ERROR, "A field must be provided when requesting SRV data\n");
202                 return -1;
203         }
204
205         if (sscanf(args.resultnum, "%30u", &num) != 1) {
206                 ast_log(LOG_ERROR, "Invalid value '%s' for resultnum to %s\n", args.resultnum, cmd);
207                 return -1;
208         }
209
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);
212                 return -1;
213         }
214
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);
223         } else {
224                 ast_log(LOG_WARNING, "Unrecognized SRV field '%s'\n", args.field);
225                 return -1;
226         }
227
228         return 0;
229 }
230
231 static struct ast_custom_function srv_result_function = {
232         .name = "SRVRESULT",
233         .read = srv_result_read,
234 };
235
236 static int unload_module(void)
237 {
238         int res = 0;
239
240         res |= ast_custom_function_unregister(&srv_query_function);
241         res |= ast_custom_function_unregister(&srv_result_function);
242
243         return res;
244 }
245
246 static int load_module(void)
247 {
248         int res = AST_MODULE_LOAD_SUCCESS;
249
250         res |= ast_custom_function_register(&srv_query_function);
251         res |= ast_custom_function_register(&srv_result_function);
252
253         return res;
254 }
255
256 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SRV related dialplan functions");