Asterisk - The Open Source Telephony Project  21.4.1
func_srv.c
Go to the documentation of this file.
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 /*** MODULEINFO
27  <support_level>core</support_level>
28  ***/
29 
30 #include "asterisk.h"
31 
32 #include "asterisk/module.h"
33 #include "asterisk/srv.h"
34 #include "asterisk/pbx.h"
35 #include "asterisk/app.h"
36 #include "asterisk/datastore.h"
37 #include "asterisk/channel.h"
38 
39 /*** DOCUMENTATION
40  <function name="SRVQUERY" language="en_US">
41  <synopsis>
42  Initiate an SRV query.
43  </synopsis>
44  <syntax>
45  <parameter name="service" required="true">
46  <para>The service for which to look up SRV records. An example would be something
47  like <literal>_sip._udp.example.com</literal></para>
48  </parameter>
49  </syntax>
50  <description>
51  <para>This will do an SRV lookup of the given service.</para>
52  </description>
53  </function>
54  <function name="SRVRESULT" language="en_US">
55  <synopsis>
56  Retrieve results from an SRVQUERY.
57  </synopsis>
58  <syntax>
59  <parameter name="id" required="true">
60  <para>The identifier returned by the SRVQUERY function.</para>
61  </parameter>
62  <parameter name="resultnum" required="true">
63  <para>The number of the result that you want to retrieve.</para>
64  <para>Results start at <literal>1</literal>. If this argument is specified
65  as <literal>getnum</literal>, then it will return the total number of results
66  that are available.</para>
67  </parameter>
68  <parameter name="field" required="false">
69  <para>The field of the result to retrieve.</para>
70  <para>The fields that can be retrieved are:</para>
71  <enumlist>
72  <enum name="host"/>
73  <enum name="port"/>
74  <enum name="priority"/>
75  <enum name="weight"/>
76  </enumlist>
77  </parameter>
78  </syntax>
79  <description>
80  <para>This function will retrieve results from a previous use
81  of the SRVQUERY function.</para>
82  </description>
83  </function>
84  ***/
85 
87  struct srv_context *context;
88  char id[1];
89 };
90 
91 static void srds_destroy_cb(void *data)
92 {
93  struct srv_result_datastore *datastore = data;
94  ast_srv_cleanup(&datastore->context);
95  ast_free(datastore);
96 }
97 
98 static const struct ast_datastore_info srv_result_datastore_info = {
99  .type = "SRVQUERY",
100  .destroy = srds_destroy_cb,
101 };
102 
103 static struct srv_context *srv_datastore_setup(const char *service, struct ast_channel *chan)
104 {
105  struct srv_result_datastore *srds;
106  struct ast_datastore *datastore;
107  const char *host;
108  unsigned short port;
109 
110  if (!(srds = ast_calloc(1, sizeof(*srds) + strlen(service)))) {
111  return NULL;
112  }
113 
114  ast_autoservice_start(chan);
115  if (ast_srv_lookup(&srds->context, service, &host, &port) < 0) {
116  ast_autoservice_stop(chan);
117  ast_log(LOG_NOTICE, "Error performing lookup of service '%s'\n", service);
118  ast_free(srds);
119  return NULL;
120  }
121  ast_autoservice_stop(chan);
122 
123  strcpy(srds->id, service);
124 
125  if (!(datastore = ast_datastore_alloc(&srv_result_datastore_info, srds->id))) {
126  ast_srv_cleanup(&srds->context);
127  ast_free(srds);
128  return NULL;
129  }
130 
131  datastore->data = srds;
132  ast_channel_lock(chan);
133  ast_channel_datastore_add(chan, datastore);
134  ast_channel_unlock(chan);
135  return srds->context;
136 }
137 
138 static int srv_query_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
139 {
140  struct ast_datastore *datastore;
141 
142  if (!chan) {
143  ast_log(LOG_WARNING, "%s cannot be used without a channel\n", cmd);
144  return -1;
145  }
146 
147  if (ast_strlen_zero(data)) {
148  ast_log(LOG_WARNING, "%s requires a service as an argument\n", cmd);
149  return -1;
150  }
151 
152  /* If they already called SRVQUERY for this service once,
153  * we need to kill the old datastore.
154  */
155  ast_channel_lock(chan);
156  datastore = ast_channel_datastore_find(chan, &srv_result_datastore_info, data);
157  ast_channel_unlock(chan);
158 
159  if (datastore) {
160  ast_channel_datastore_remove(chan, datastore);
161  ast_datastore_free(datastore);
162  }
163 
164  if (!srv_datastore_setup(data, chan)) {
165  return -1;
166  }
167 
168  ast_copy_string(buf, data, len);
169 
170  return 0;
171 }
172 
173 static struct ast_custom_function srv_query_function = {
174  .name = "SRVQUERY",
175  .read = srv_query_read,
176 };
177 
178 static int srv_result_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
179 {
180  struct srv_result_datastore *srds;
181  struct ast_datastore *datastore;
182  struct srv_context *srv_context;
183  char *parse;
184  const char *host;
185  unsigned short port, priority, weight;
186  unsigned int num;
188  AST_APP_ARG(id);
189  AST_APP_ARG(resultnum);
190  AST_APP_ARG(field);
191  );
192 
193  if (!chan) {
194  ast_log(LOG_WARNING, "%s cannot be used without a channel\n", cmd);
195  return -1;
196  }
197 
198  if (ast_strlen_zero(data)) {
199  ast_log(LOG_WARNING, "%s requires two arguments (id and resultnum)\n", cmd);
200  return -1;
201  }
202 
203  parse = ast_strdupa(data);
204 
205  AST_STANDARD_APP_ARGS(args, parse);
206 
207  ast_channel_lock(chan);
208  datastore = ast_channel_datastore_find(chan, &srv_result_datastore_info, args.id);
209  ast_channel_unlock(chan);
210 
211  if (!datastore) {
212  /* They apparently decided to call SRVRESULT without first calling SRVQUERY.
213  * No problem, we'll do the SRV lookup now.
214  */
215  srv_context = srv_datastore_setup(args.id, chan);
216  if (!srv_context) {
217  return -1;
218  }
219  } else {
220  srds = datastore->data;
221  srv_context = srds->context;
222  }
223 
224  if (!strcasecmp(args.resultnum, "getnum")) {
225  snprintf(buf, len, "%u", ast_srv_get_record_count(srv_context));
226  return 0;
227  }
228 
229  if (ast_strlen_zero(args.field)) {
230  ast_log(LOG_ERROR, "A field must be provided when requesting SRV data\n");
231  return -1;
232  }
233 
234  if (sscanf(args.resultnum, "%30u", &num) != 1) {
235  ast_log(LOG_ERROR, "Invalid value '%s' for resultnum to %s\n", args.resultnum, cmd);
236  return -1;
237  }
238 
239  if (ast_srv_get_nth_record(srv_context, num, &host, &port, &priority, &weight)) {
240  ast_log(LOG_ERROR, "Failed to get record number %u for %s\n", num, cmd);
241  return -1;
242  }
243 
244  if (!strcasecmp(args.field, "host")) {
245  ast_copy_string(buf, host, len);
246  } else if (!strcasecmp(args.field, "port")) {
247  snprintf(buf, len, "%d", port);
248  } else if (!strcasecmp(args.field, "priority")) {
249  snprintf(buf, len, "%d", priority);
250  } else if (!strcasecmp(args.field, "weight")) {
251  snprintf(buf, len, "%d", weight);
252  } else {
253  ast_log(LOG_WARNING, "Unrecognized SRV field '%s'\n", args.field);
254  return -1;
255  }
256 
257  return 0;
258 }
259 
260 static struct ast_custom_function srv_result_function = {
261  .name = "SRVRESULT",
262  .read = srv_result_read,
263 };
264 
265 static int unload_module(void)
266 {
267  int res = 0;
268 
269  res |= ast_custom_function_unregister(&srv_query_function);
270  res |= ast_custom_function_unregister(&srv_result_function);
271 
272  return res;
273 }
274 
275 static int load_module(void)
276 {
277  int res = ast_custom_function_register(&srv_query_function);
278  if (res < 0) {
280  }
281  res = ast_custom_function_register(&srv_result_function);
282  if (res < 0) {
284  }
285 
286  return AST_MODULE_LOAD_SUCCESS;;
287 }
288 
289 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SRV related dialplan functions");
const char * name
Definition: pbx.h:119
const char * type
Definition: datastore.h:32
Main Channel structure associated with a channel.
Asterisk main include file. File version handling, generic pbx functions.
int ast_autoservice_start(struct ast_channel *chan)
Automatically service a channel for us...
Definition: autoservice.c:200
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
Support for DNS SRV records, used in to locate SIP services.
void ast_srv_cleanup(struct srv_context **context)
Cleanup resources associated with ast_srv_lookup.
Definition: srv.c:248
Structure for a data store type.
Definition: datastore.h:31
Structure for a data store object.
Definition: datastore.h:64
struct ast_datastore * ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
Find a datastore on a channel.
Definition: channel.c:2399
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
Definition: datastore.c:68
Asterisk datastore objects.
int ast_srv_get_nth_record(struct srv_context *context, int record_num, const char **host, unsigned short *port, unsigned short *priority, unsigned short *weight)
Retrieve details from a specific SRV record.
Definition: srv.c:309
General Asterisk PBX channel definitions.
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
Core PBX routines and definitions.
int ast_autoservice_stop(struct ast_channel *chan)
Stop servicing a channel for us...
Definition: autoservice.c:266
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
unsigned int ast_srv_get_record_count(struct srv_context *context)
Get the number of records for a given SRV context.
Definition: srv.c:304
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
int ast_srv_lookup(struct srv_context **context, const char *service, const char **host, unsigned short *port)
Retrieve set of SRV lookups, in order.
Definition: srv.c:202
void * data
Definition: datastore.h:66
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
Definition: channel.c:2385
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
int ast_channel_datastore_remove(struct ast_channel *chan, struct ast_datastore *datastore)
Remove a datastore from a channel.
Definition: channel.c:2394
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1558
#define AST_APP_ARG(name)
Define an application argument.