1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 1999 - 2010 Digium, Inc.
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
*
* \brief SRV Functions
*
* \author Mark Michelson <mmichelson@digium.com>
*
* \ingroup functions
*/
/*** MODULEINFO
<support_level>core</support_level>
***/
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision: 413587 $")
#include "asterisk/module.h"
#include "asterisk/srv.h"
#include "asterisk/pbx.h"
#include "asterisk/app.h"
#include "asterisk/datastore.h"
#include "asterisk/channel.h"
/*** DOCUMENTATION
<function name="SRVQUERY" language="en_US">
<synopsis>
Initiate an SRV query.
</synopsis>
<syntax>
<parameter name="service" required="true">
<para>The service for which to look up SRV records. An example would be something
like <literal>_sip._udp.example.com</literal></para>
</parameter>
</syntax>
<description>
<para>This will do an SRV lookup of the given service.</para>
</description>
</function>
<function name="SRVRESULT" language="en_US">
<synopsis>
Retrieve results from an SRVQUERY.
</synopsis>
<syntax>
<parameter name="id" required="true">
<para>The identifier returned by the SRVQUERY function.</para>
</parameter>
<parameter name="resultnum" required="true">
<para>The number of the result that you want to retrieve.</para>
<para>Results start at <literal>1</literal>. If this argument is specified
as <literal>getnum</literal>, then it will return the total number of results
that are available.</para>
</parameter>
</syntax>
<description>
<para>This function will retrieve results from a previous use
of the SRVQUERY function.</para>
</description>
</function>
***/
struct srv_result_datastore {
struct srv_context *context;
char id[1];
};
static void srds_destroy_cb(void *data)
{
struct srv_result_datastore *datastore = data;
ast_srv_cleanup(&datastore->context);
ast_free(datastore);
}
static const struct ast_datastore_info srv_result_datastore_info = {
.type = "SRVQUERY",
.destroy = srds_destroy_cb,
};
static struct srv_context *srv_datastore_setup(const char *service, struct ast_channel *chan)
{
struct srv_result_datastore *srds;
struct ast_datastore *datastore;
const char *host;
unsigned short port;
if (!(srds = ast_calloc(1, sizeof(*srds) + strlen(service)))) {
return NULL;
}
ast_autoservice_start(chan);
if (ast_srv_lookup(&srds->context, service, &host, &port) < 0) {
ast_autoservice_stop(chan);
ast_log(LOG_NOTICE, "Error performing lookup of service '%s'\n", service);
ast_free(srds);
return NULL;
}
ast_autoservice_stop(chan);
strcpy(srds->id, service);
if (!(datastore = ast_datastore_alloc(&srv_result_datastore_info, srds->id))) {
ast_srv_cleanup(&srds->context);
ast_free(srds);
return NULL;
}
datastore->data = srds;
ast_channel_lock(chan);
ast_channel_datastore_add(chan, datastore);
ast_channel_unlock(chan);
return srds->context;
}
static int srv_query_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
{
struct ast_datastore *datastore;
if (!chan) {
ast_log(LOG_WARNING, "%s cannot be used without a channel\n", cmd);
return -1;
}
if (ast_strlen_zero(data)) {
ast_log(LOG_WARNING, "%s requires a service as an argument\n", cmd);
return -1;
}
/* If they already called SRVQUERY for this service once,
* we need to kill the old datastore.
*/
ast_channel_lock(chan);
datastore = ast_channel_datastore_find(chan, &srv_result_datastore_info, data);
ast_channel_unlock(chan);
if (datastore) {
ast_channel_datastore_remove(chan, datastore);
ast_datastore_free(datastore);
}
if (!srv_datastore_setup(data, chan)) {
return -1;
}
ast_copy_string(buf, data, len);
return 0;
}
static struct ast_custom_function srv_query_function = {
.name = "SRVQUERY",
.read = srv_query_read,
};
static int srv_result_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
{
struct srv_result_datastore *srds;
struct ast_datastore *datastore;
struct srv_context *srv_context;
char *parse;
const char *host;
unsigned short port, priority, weight;
unsigned int num;
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(id);
AST_APP_ARG(resultnum);
AST_APP_ARG(field);
);
if (!chan) {
ast_log(LOG_WARNING, "%s cannot be used without a channel\n", cmd);
return -1;
}
if (ast_strlen_zero(data)) {
ast_log(LOG_WARNING, "%s requires two arguments (id and resultnum)\n", cmd);
return -1;
}
parse = ast_strdupa(data);
AST_STANDARD_APP_ARGS(args, parse);
ast_channel_lock(chan);
datastore = ast_channel_datastore_find(chan, &srv_result_datastore_info, args.id);
ast_channel_unlock(chan);
if (!datastore) {
/* They apparently decided to call SRVRESULT without first calling SRVQUERY.
* No problem, we'll do the SRV lookup now.
*/
srv_context = srv_datastore_setup(args.id, chan);
if (!srv_context) {
return -1;
}
} else {
srds = datastore->data;
srv_context = srds->context;
}
if (!strcasecmp(args.resultnum, "getnum")) {
snprintf(buf, len, "%u", ast_srv_get_record_count(srv_context));
return 0;
}
if (ast_strlen_zero(args.field)) {
ast_log(LOG_ERROR, "A field must be provided when requesting SRV data\n");
return -1;
}
if (sscanf(args.resultnum, "%30u", &num) != 1) {
ast_log(LOG_ERROR, "Invalid value '%s' for resultnum to %s\n", args.resultnum, cmd);
return -1;
}
if (ast_srv_get_nth_record(srv_context, num, &host, &port, &priority, &weight)) {
ast_log(LOG_ERROR, "Failed to get record number %u for %s\n", num, cmd);
return -1;
}
if (!strcasecmp(args.field, "host")) {
ast_copy_string(buf, host, len);
} else if (!strcasecmp(args.field, "port")) {
snprintf(buf, len, "%d", port);
} else if (!strcasecmp(args.field, "priority")) {
snprintf(buf, len, "%d", priority);
} else if (!strcasecmp(args.field, "weight")) {
snprintf(buf, len, "%d", weight);
} else {
ast_log(LOG_WARNING, "Unrecognized SRV field '%s'\n", args.field);
return -1;
}
return 0;
}
static struct ast_custom_function srv_result_function = {
.name = "SRVRESULT",
.read = srv_result_read,
};
static int unload_module(void)
{
int res = 0;
res |= ast_custom_function_unregister(&srv_query_function);
res |= ast_custom_function_unregister(&srv_result_function);
return res;
}
static int load_module(void)
{
int res = ast_custom_function_register(&srv_query_function);
if (res < 0) {
return AST_MODULE_LOAD_DECLINE;
}
res = ast_custom_function_register(&srv_result_function);
if (res < 0) {
return AST_MODULE_LOAD_DECLINE;
}
return AST_MODULE_LOAD_SUCCESS;;
}
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SRV related dialplan functions");
|