| 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 |
#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 |
|
| 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 |
struct srv_result_datastore { |
| 93 |
struct srv_context *context; |
| 94 |
char id[1]; |
| 95 |
}; |
| 96 |
|
| 97 |
static void srds_destroy_cb(void *data) |
| 98 |
{ |
| 99 |
struct srv_result_datastore *datastore = data; |
| 100 |
ast_srv_cleanup(&datastore->context); |
| 101 |
ast_free(datastore); |
| 102 |
} |
| 103 |
|
| 104 |
static const struct ast_datastore_info srv_result_datastore_info = { |
| 105 |
.type = "SRVQUERY", |
| 106 |
.destroy = srds_destroy_cb, |
| 107 |
}; |
| 108 |
|
| 109 |
static struct srv_context *srv_datastore_setup(const char *service, struct ast_channel *chan) |
| 110 |
{ |
| 111 |
struct srv_result_datastore *srds; |
| 112 |
struct ast_datastore *datastore; |
| 113 |
const char *host; |
| 114 |
unsigned short port; |
| 115 |
|
| 116 |
if (!(srds = ast_calloc(1, sizeof(*srds) + strlen(service)))) { |
| 117 |
return NULL; |
| 118 |
} |
| 119 |
|
| 120 |
ast_autoservice_start(chan); |
| 121 |
if (ast_srv_lookup(&srds->context, service, &host, &port) < 0) { |
| 122 |
ast_autoservice_stop(chan); |
| 123 |
ast_log(LOG_NOTICE, "Error performing lookup of service '%s'\n", service); |
| 124 |
ast_free(srds); |
| 125 |
return NULL; |
| 126 |
} |
| 127 |
ast_autoservice_stop(chan); |
| 128 |
|
| 129 |
strcpy(srds->id, service); |
| 130 |
|
| 131 |
if (!(datastore = ast_datastore_alloc(&srv_result_datastore_info, srds->id))) { |
| 132 |
ast_srv_cleanup(&srds->context); |
| 133 |
ast_free(srds); |
| 134 |
return NULL; |
| 135 |
} |
| 136 |
|
| 137 |
datastore->data = srds; |
| 138 |
ast_channel_lock(chan); |
| 139 |
ast_channel_datastore_add(chan, datastore); |
| 140 |
ast_channel_unlock(chan); |
| 141 |
return srds->context; |
| 142 |
} |
| 143 |
|
| 144 |
static int srv_query_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| 145 |
{ |
| 146 |
struct ast_datastore *datastore; |
| 147 |
|
| 148 |
if (!chan) { |
| 149 |
ast_log(LOG_WARNING, "%s cannot be used without a channel\n", cmd); |
| 150 |
return -1; |
| 151 |
} |
| 152 |
|
| 153 |
if (ast_strlen_zero(data)) { |
| 154 |
ast_log(LOG_WARNING, "%s requires a service as an argument\n", cmd); |
| 155 |
return -1; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
ast_channel_lock(chan); |
| 162 |
datastore = ast_channel_datastore_find(chan, &srv_result_datastore_info, data); |
| 163 |
ast_channel_unlock(chan); |
| 164 |
|
| 165 |
if (datastore) { |
| 166 |
ast_channel_datastore_remove(chan, datastore); |
| 167 |
ast_datastore_free(datastore); |
| 168 |
} |
| 169 |
|
| 170 |
if (!srv_datastore_setup(data, chan)) { |
| 171 |
return -1; |
| 172 |
} |
| 173 |
|
| 174 |
ast_copy_string(buf, data, len); |
| 175 |
|
| 176 |
return 0; |
| 177 |
} |
| 178 |
|
| 179 |
static struct ast_custom_function srv_query_function = { |
| 180 |
.name = "SRVQUERY", |
| 181 |
.read = srv_query_read, |
| 182 |
}; |
| 183 |
|
| 184 |
static int srv_result_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| 185 |
{ |
| 186 |
struct srv_result_datastore *srds; |
| 187 |
struct ast_datastore *datastore; |
| 188 |
struct srv_context *srv_context; |
| 189 |
char *parse; |
| 190 |
const char *host; |
| 191 |
unsigned short port, priority, weight; |
| 192 |
unsigned int num; |
| 193 |
AST_DECLARE_APP_ARGS(args, |
| 194 |
AST_APP_ARG(id); |
| 195 |
AST_APP_ARG(resultnum); |
| 196 |
AST_APP_ARG(field); |
| 197 |
); |
| 198 |
|
| 199 |
if (!chan) { |
| 200 |
ast_log(LOG_WARNING, "%s cannot be used without a channel\n", cmd); |
| 201 |
return -1; |
| 202 |
} |
| 203 |
|
| 204 |
if (ast_strlen_zero(data)) { |
| 205 |
ast_log(LOG_WARNING, "%s requires two arguments (id and resultnum)\n", cmd); |
| 206 |
return -1; |
| 207 |
} |
| 208 |
|
| 209 |
parse = ast_strdupa(data); |
| 210 |
|
| 211 |
AST_STANDARD_APP_ARGS(args, parse); |
| 212 |
|
| 213 |
ast_channel_lock(chan); |
| 214 |
datastore = ast_channel_datastore_find(chan, &srv_result_datastore_info, args.id); |
| 215 |
ast_channel_unlock(chan); |
| 216 |
|
| 217 |
if (!datastore) { |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
srv_context = srv_datastore_setup(args.id, chan); |
| 222 |
if (!srv_context) { |
| 223 |
return -1; |
| 224 |
} |
| 225 |
} else { |
| 226 |
srds = datastore->data; |
| 227 |
srv_context = srds->context; |
| 228 |
} |
| 229 |
|
| 230 |
if (!strcasecmp(args.resultnum, "getnum")) { |
| 231 |
snprintf(buf, len, "%u", ast_srv_get_record_count(srv_context)); |
| 232 |
return 0; |
| 233 |
} |
| 234 |
|
| 235 |
if (ast_strlen_zero(args.field)) { |
| 236 |
ast_log(LOG_ERROR, "A field must be provided when requesting SRV data\n"); |
| 237 |
return -1; |
| 238 |
} |
| 239 |
|
| 240 |
if (sscanf(args.resultnum, "%30u", &num) != 1) { |
| 241 |
ast_log(LOG_ERROR, "Invalid value '%s' for resultnum to %s\n", args.resultnum, cmd); |
| 242 |
return -1; |
| 243 |
} |
| 244 |
|
| 245 |
if (ast_srv_get_nth_record(srv_context, num, &host, &port, &priority, &weight)) { |
| 246 |
ast_log(LOG_ERROR, "Failed to get record number %u for %s\n", num, cmd); |
| 247 |
return -1; |
| 248 |
} |
| 249 |
|
| 250 |
if (!strcasecmp(args.field, "host")) { |
| 251 |
ast_copy_string(buf, host, len); |
| 252 |
} else if (!strcasecmp(args.field, "port")) { |
| 253 |
snprintf(buf, len, "%d", port); |
| 254 |
} else if (!strcasecmp(args.field, "priority")) { |
| 255 |
snprintf(buf, len, "%d", priority); |
| 256 |
} else if (!strcasecmp(args.field, "weight")) { |
| 257 |
snprintf(buf, len, "%d", weight); |
| 258 |
} else { |
| 259 |
ast_log(LOG_WARNING, "Unrecognized SRV field '%s'\n", args.field); |
| 260 |
return -1; |
| 261 |
} |
| 262 |
|
| 263 |
return 0; |
| 264 |
} |
| 265 |
|
| 266 |
static struct ast_custom_function srv_result_function = { |
| 267 |
.name = "SRVRESULT", |
| 268 |
.read = srv_result_read, |
| 269 |
}; |
| 270 |
|
| 271 |
static int unload_module(void) |
| 272 |
{ |
| 273 |
int res = 0; |
| 274 |
|
| 275 |
res |= ast_custom_function_unregister(&srv_query_function); |
| 276 |
res |= ast_custom_function_unregister(&srv_result_function); |
| 277 |
|
| 278 |
return res; |
| 279 |
} |
| 280 |
|
| 281 |
static int load_module(void) |
| 282 |
{ |
| 283 |
int res = ast_custom_function_register(&srv_query_function); |
| 284 |
if (res < 0) { |
| 285 |
return AST_MODULE_LOAD_DECLINE; |
| 286 |
} |
| 287 |
res = ast_custom_function_register(&srv_result_function); |
| 288 |
if (res < 0) { |
| 289 |
return AST_MODULE_LOAD_DECLINE; |
| 290 |
} |
| 291 |
|
| 292 |
return AST_MODULE_LOAD_SUCCESS;; |
| 293 |
} |
| 294 |
|
| 295 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SRV related dialplan functions"); |