Asterisk - The Open Source Telephony Project  18.5.0
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 <[email protected]>
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  </syntax>
69  <description>
70  <para>This function will retrieve results from a previous use
71  of the SRVQUERY function.</para>
72  </description>
73  </function>
74  ***/
75 
78  char id[1];
79 };
80 
81 static void srds_destroy_cb(void *data)
82 {
83  struct srv_result_datastore *datastore = data;
84  ast_srv_cleanup(&datastore->context);
85  ast_free(datastore);
86 }
87 
89  .type = "SRVQUERY",
90  .destroy = srds_destroy_cb,
91 };
92 
93 static struct srv_context *srv_datastore_setup(const char *service, struct ast_channel *chan)
94 {
95  struct srv_result_datastore *srds;
96  struct ast_datastore *datastore;
97  const char *host;
98  unsigned short port;
99 
100  if (!(srds = ast_calloc(1, sizeof(*srds) + strlen(service)))) {
101  return NULL;
102  }
103 
104  ast_autoservice_start(chan);
105  if (ast_srv_lookup(&srds->context, service, &host, &port) < 0) {
106  ast_autoservice_stop(chan);
107  ast_log(LOG_NOTICE, "Error performing lookup of service '%s'\n", service);
108  ast_free(srds);
109  return NULL;
110  }
111  ast_autoservice_stop(chan);
112 
113  strcpy(srds->id, service);
114 
115  if (!(datastore = ast_datastore_alloc(&srv_result_datastore_info, srds->id))) {
116  ast_srv_cleanup(&srds->context);
117  ast_free(srds);
118  return NULL;
119  }
120 
121  datastore->data = srds;
122  ast_channel_lock(chan);
123  ast_channel_datastore_add(chan, datastore);
124  ast_channel_unlock(chan);
125  return srds->context;
126 }
127 
128 static int srv_query_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
129 {
130  struct ast_datastore *datastore;
131 
132  if (!chan) {
133  ast_log(LOG_WARNING, "%s cannot be used without a channel\n", cmd);
134  return -1;
135  }
136 
137  if (ast_strlen_zero(data)) {
138  ast_log(LOG_WARNING, "%s requires a service as an argument\n", cmd);
139  return -1;
140  }
141 
142  /* If they already called SRVQUERY for this service once,
143  * we need to kill the old datastore.
144  */
145  ast_channel_lock(chan);
146  datastore = ast_channel_datastore_find(chan, &srv_result_datastore_info, data);
147  ast_channel_unlock(chan);
148 
149  if (datastore) {
150  ast_channel_datastore_remove(chan, datastore);
151  ast_datastore_free(datastore);
152  }
153 
154  if (!srv_datastore_setup(data, chan)) {
155  return -1;
156  }
157 
158  ast_copy_string(buf, data, len);
159 
160  return 0;
161 }
162 
164  .name = "SRVQUERY",
165  .read = srv_query_read,
166 };
167 
168 static int srv_result_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
169 {
170  struct srv_result_datastore *srds;
171  struct ast_datastore *datastore;
172  struct srv_context *srv_context;
173  char *parse;
174  const char *host;
175  unsigned short port, priority, weight;
176  unsigned int num;
178  AST_APP_ARG(id);
179  AST_APP_ARG(resultnum);
180  AST_APP_ARG(field);
181  );
182 
183  if (!chan) {
184  ast_log(LOG_WARNING, "%s cannot be used without a channel\n", cmd);
185  return -1;
186  }
187 
188  if (ast_strlen_zero(data)) {
189  ast_log(LOG_WARNING, "%s requires two arguments (id and resultnum)\n", cmd);
190  return -1;
191  }
192 
193  parse = ast_strdupa(data);
194 
195  AST_STANDARD_APP_ARGS(args, parse);
196 
197  ast_channel_lock(chan);
198  datastore = ast_channel_datastore_find(chan, &srv_result_datastore_info, args.id);
199  ast_channel_unlock(chan);
200 
201  if (!datastore) {
202  /* They apparently decided to call SRVRESULT without first calling SRVQUERY.
203  * No problem, we'll do the SRV lookup now.
204  */
205  srv_context = srv_datastore_setup(args.id, chan);
206  if (!srv_context) {
207  return -1;
208  }
209  } else {
210  srds = datastore->data;
211  srv_context = srds->context;
212  }
213 
214  if (!strcasecmp(args.resultnum, "getnum")) {
215  snprintf(buf, len, "%u", ast_srv_get_record_count(srv_context));
216  return 0;
217  }
218 
219  if (ast_strlen_zero(args.field)) {
220  ast_log(LOG_ERROR, "A field must be provided when requesting SRV data\n");
221  return -1;
222  }
223 
224  if (sscanf(args.resultnum, "%30u", &num) != 1) {
225  ast_log(LOG_ERROR, "Invalid value '%s' for resultnum to %s\n", args.resultnum, cmd);
226  return -1;
227  }
228 
229  if (ast_srv_get_nth_record(srv_context, num, &host, &port, &priority, &weight)) {
230  ast_log(LOG_ERROR, "Failed to get record number %u for %s\n", num, cmd);
231  return -1;
232  }
233 
234  if (!strcasecmp(args.field, "host")) {
235  ast_copy_string(buf, host, len);
236  } else if (!strcasecmp(args.field, "port")) {
237  snprintf(buf, len, "%d", port);
238  } else if (!strcasecmp(args.field, "priority")) {
239  snprintf(buf, len, "%d", priority);
240  } else if (!strcasecmp(args.field, "weight")) {
241  snprintf(buf, len, "%d", weight);
242  } else {
243  ast_log(LOG_WARNING, "Unrecognized SRV field '%s'\n", args.field);
244  return -1;
245  }
246 
247  return 0;
248 }
249 
251  .name = "SRVRESULT",
252  .read = srv_result_read,
253 };
254 
255 static int unload_module(void)
256 {
257  int res = 0;
258 
259  res |= ast_custom_function_unregister(&srv_query_function);
260  res |= ast_custom_function_unregister(&srv_result_function);
261 
262  return res;
263 }
264 
265 static int load_module(void)
266 {
267  int res = ast_custom_function_register(&srv_query_function);
268  if (res < 0) {
270  }
271  res = ast_custom_function_register(&srv_result_function);
272  if (res < 0) {
274  }
275 
276  return AST_MODULE_LOAD_SUCCESS;;
277 }
278 
279 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
struct srv_context * context
Definition: func_srv.c:77
#define ast_channel_lock(chan)
Definition: channel.h:2945
Main Channel structure associated with a channel.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:567
Asterisk main include file. File version handling, generic pbx functions.
static int srv_result_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_srv.c:168
int ast_autoservice_start(struct ast_channel *chan)
Automatically service a channel for us...
Definition: autoservice.c:200
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
#define LOG_WARNING
Definition: logger.h:274
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
enum ast_cc_service_type service
Definition: chan_sip.c:949
Structure for a data store type.
Definition: datastore.h:31
static int srv_query_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_srv.c:128
Structure for a data store object.
Definition: datastore.h:68
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:2404
static struct srv_context * srv_datastore_setup(const char *service, struct ast_channel *chan)
Definition: func_srv.c:93
const char * args
#define NULL
Definition: resample.c:96
static int unload_module(void)
Definition: func_srv.c:255
static int priority
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
#define ast_strlen_zero(foo)
Definition: strings.h:52
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
#define ast_log
Definition: astobj2.c:42
static char host[256]
Definition: muted.c:77
General Asterisk PBX channel definitions.
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
Core PBX routines and definitions.
int ast_autoservice_stop(struct ast_channel *chan)
Stop servicing a channel for us...
Definition: autoservice.c:266
#define LOG_ERROR
Definition: logger.h:285
char weight
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define LOG_NOTICE
Definition: logger.h:263
#define ast_channel_unlock(chan)
Definition: channel.h:2946
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1872
#define ast_free(a)
Definition: astmm.h:182
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
static void srds_destroy_cb(void *data)
Definition: func_srv.c:81
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
static struct ast_custom_function srv_result_function
Definition: func_srv.c:250
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
static struct ast_custom_function srv_query_function
Definition: func_srv.c:163
void * data
Definition: datastore.h:70
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401
static const struct ast_datastore_info srv_result_datastore_info
Definition: func_srv.c:88
#define ast_datastore_alloc(info, uid)
Definition: datastore.h:89
#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:2390
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;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:2399
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1508
#define AST_APP_ARG(name)
Define an application argument.
static int load_module(void)
Definition: func_srv.c:265