29 #define ASTMM_LIBC ASTMM_IGNORE 36 #include <netinet/in.h> 37 #include <arpa/inet.h> 38 #include <sys/socket.h> 39 #include <sys/select.h> 49 #define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0])) 53 #define MAX_HEADERS 80 62 #define _NEWT_CAST (void *) 64 #define DEFAULT_MANAGER_PORT 5038 72 struct sockaddr_in sin;
102 int64_t
ast_mark(
int,
int start1_stop0);
114 if (!strcmp(name, chan->
name))
119 memset(chan, 0,
sizeof(
struct ast_chan));
120 strncpy(chan->
name, name,
sizeof(chan->
name) - 1);
131 if (!strcmp(name, chan->
name)) {
141 static void __attribute__((
format(printf, 2, 3)))
fdprintf(
int fd,
char *fmt, ...)
147 vsnprintf(stuff,
sizeof(stuff), fmt, ap);
149 if (write(fd, stuff, strlen(stuff)) < 0) {
150 fprintf(stderr,
"write() failed: %s\n", strerror(
errno));
158 snprintf(cmp,
sizeof(cmp),
"%s: ", var);
160 if (!strncasecmp(cmp, m->
headers[x], strlen(cmp)))
161 return m->
headers[x] + strlen(cmp);
254 if (!strlen(
event)) {
255 fprintf(stderr,
"Missing event in request");
266 fprintf(stderr,
"Ignoring unknown event '%s'",
event);
269 printf(
"Header: %s\n", m->
headers[x]);
279 char tmpn[
sizeof(chan->
name) +
sizeof(chan->
callerid) + 3 - 1];
282 prev = newtListboxGetCurrent(c);
285 snprintf(tmpn,
sizeof(tmpn),
"%s (%s)", chan->
name, chan->
callerid);
286 if (strlen(chan->
exten))
287 snprintf(tmp,
sizeof(tmp),
"%-30s %8s -> %s@%s:%s",
291 snprintf(tmp,
sizeof(tmp),
"%-30s %8s",
293 newtListboxAppendEntry(c, tmp, chan);
297 newtListboxAppendEntry(c,
" << No Active Channels >> ",
NULL);
298 newtListboxSetCurrentByKey(c, prev);
304 for (x=1;x<s->
inlen;x++)
305 if ((s->
inbuf[x] ==
'\n') && (s->
inbuf[x-1] ==
'\r'))
315 struct timeval tv = {0, 0};
317 for (x=1;x<s->
inlen;x++) {
318 if ((s->
inbuf[x] ==
'\n') && (s->
inbuf[x-1] ==
'\r')) {
320 memcpy(output, s->
inbuf, x + 1);
330 fprintf(stderr,
"Dumping long line with no return from %s: %s\n",
inet_ntoa(s->
sin.sin_addr), s->
inbuf);
335 res = select(s->
fd + 1, &fds,
NULL,
NULL, &tv);
337 fprintf(stderr,
"Select returned error: %s\n", strerror(
errno));
338 }
else if (res > 0) {
370 if (mout && strlen(
get_header(&m,
"Response"))) {
376 memset(&m, 0,
sizeof(m));
379 }
else if (res < 0) {
394 tv.tv_sec = timeout / 1000;
395 tv.tv_usec = (timeout % 1000) * 1000;
420 vsnprintf(tmp,
sizeof(tmp), fmt, ap);
422 if ((res = write(s->
fd, tmp, strlen(tmp))) < 0) {
423 fprintf(stderr,
"write() failed: %s\n", strerror(
errno));
434 struct newtExitStruct es;
436 newtCenteredWindow(60,7, title);
438 label = newtLabel(4,1,msg);
439 ok = newtButton(27, 3,
"OK");
441 newtFormAddComponents(form, label, ok,
NULL);
442 newtFormRun(form, &es);
444 newtFormDestroy(form);
451 struct newtExitStruct es;
454 newtCenteredWindow(70,4, title);
455 label = newtLabel(3,1,tmp);
456 newtFormAddComponents(showform,label,
NULL);
457 newtFormSetTimer(showform, 200);
458 newtFormRun(showform, &es);
465 newtFormDestroy(showform);
475 show_message(
"Status Failed",
"Timeout waiting for response");
476 }
else if (strcasecmp(
get_header(m,
"Response"),
"Success")) {
487 chan = newtListboxGetCurrent(c);
492 show_message(
"Hangup Failed",
"Timeout waiting for response");
493 }
else if (strcasecmp(
get_header(m,
"Response"),
"Success")) {
504 newtComponent cancel;
505 newtComponent inpfield;
508 struct newtExitStruct es;
510 newtCenteredWindow(60,7, msg);
512 inpfield = newtEntry(5, 2,
"", 50,
_NEWT_CAST &input, 0);
513 ok = newtButton(22, 3,
"OK");
514 cancel = newtButton(32, 3,
"Cancel");
516 newtFormAddComponents(form, inpfield, ok, cancel,
NULL);
517 newtFormRun(form, &es);
518 strncpy(buf, input, buflen - 1);
524 newtFormDestroy(form);
533 static const char tmp_prefix[] =
"Enter new extension for ";
535 char tmp[
sizeof(tmp_prefix) +
sizeof(channame)];
538 chan = newtListboxGetCurrent(c);
540 snprintf(channame,
sizeof(channame),
"%s", chan->
name);
541 snprintf(tmp,
sizeof(tmp),
"%s%s", tmp_prefix, channame);
544 if ((context = strchr(dest,
'@'))) {
547 manager_action(
"Redirect",
"Channel: %s\r\nContext: %s\r\nExten: %s\r\nPriority: 1\r\n", chan->
name,context,dest);
549 manager_action(
"Redirect",
"Channel: %s\r\nExten: %s\r\nPriority: 1\r\n", chan->
name, dest);
553 show_message(
"Hangup Failed",
"Timeout waiting for response");
554 }
else if (strcasecmp(
get_header(m,
"Response"),
"Success")) {
566 newtComponent redirect;
568 struct newtExitStruct es;
591 snprintf(tmp,
sizeof(tmp),
"Asterisk Manager at %s", host);
592 newtCenteredWindow(74, 20, tmp);
594 newtFormWatchFd(form,
session.
fd, NEWT_FD_READ);
595 newtFormSetTimer(form, 100);
596 quit = newtButton(62, 16,
"Quit");
597 redirect = newtButton(35, 16,
"Redirect");
598 hangup = newtButton(50, 16,
"Hangup");
599 channels = newtListbox(1,1,14, NEWT_FLAG_SCROLL);
600 newtFormAddComponents(form, channels, redirect, hangup, quit,
NULL);
601 newtListboxSetWidth(channels, 72);
603 show_doing(
"Getting Status",
"Retrieving system status...");
608 newtFormRun(form, &es);
611 show_message(
"Disconnected",
"Disconnected from remote host");
614 }
else if (es.reason == NEWT_EXIT_COMPONENT) {
617 if (es.u.co == hangup) {
619 }
else if (es.u.co == redirect) {
625 newtFormDestroy(form);
632 newtComponent cancel;
634 newtComponent username;
637 newtComponent ulabel;
638 newtComponent plabel;
642 struct newtExitStruct es;
647 session.
fd = socket(AF_INET, SOCK_STREAM, 0);
649 snprintf(tmp,
sizeof(tmp),
"socket() failed: %s\n", strerror(
errno));
654 snprintf(tmp,
sizeof(tmp),
"Looking up %s\n", hostname);
660 snprintf(tmp,
sizeof(tmp),
"No such address: %s\n", hostname);
665 snprintf(tmp,
sizeof(tmp),
"Connecting to %s", hostname);
673 snprintf(tmp,
sizeof(tmp),
"%s failed: %s\n", hostname, strerror(
errno));
680 login = newtButton(5, 6,
"Login");
681 cancel = newtButton(25, 6,
"Cancel");
682 newtCenteredWindow(40, 10,
"Asterisk Manager Login");
683 snprintf(tmp,
sizeof(tmp),
"Host: %s", hostname);
684 label = newtLabel(4,1, tmp);
686 ulabel = newtLabel(4,2,
"Username:");
687 plabel = newtLabel(4,3,
"Password:");
689 username = newtEntry(14, 2,
"", 20,
_NEWT_CAST &user, 0);
690 password = newtEntry(14, 3,
"", 20,
_NEWT_CAST &pass, NEWT_FLAG_HIDDEN);
693 newtFormAddComponents(form, username, password, login, cancel, label, ulabel, plabel,
NULL);
694 newtFormRun(form, &es);
695 if (es.reason == NEWT_EXIT_COMPONENT) {
696 if (es.u.co == login) {
697 snprintf(tmp,
sizeof(tmp),
"Logging in '%s'...", user);
702 if (m && !strcasecmp(
get_header(m,
"Response"),
"Success")) {
706 char md5key[256] =
"";
708 unsigned char digest[16];
710 MD5Update(&md5, (
unsigned char *)challenge, strlen(challenge));
711 MD5Update(&md5, (
unsigned char *)pass, strlen(pass));
714 len += sprintf(md5key + len,
"%02hhx", digest[x]);
722 if (!strcasecmp(
get_header(m,
"Response"),
"Success")) {
735 if (!strcasecmp(
get_header(m,
"Response"),
"Success")) {
744 newtFormDestroy(form);
748 int main(
int argc,
char *argv[])
751 fprintf(stderr,
"Usage: astman <host>\n");
756 newtDrawRootText(0, 0,
"Asterisk Manager (C)2002, Linux Support Services, Inc.");
757 newtPushHelpLine(
"Welcome to the Asterisk Manager!");
int64_t ast_mark(int, int start1_stop0)
static newtComponent showform
static char exten[AST_MAX_EXTENSION]
Asterisk main include file. File version handling, generic pbx functions.
static int event_hangup(struct ast_mansession *s, struct message *m)
struct ast_chan::@514 list
static struct message * wait_for_response(int timeout)
static char * get_header(struct message *m, char *var)
#define DEFAULT_MANAGER_PORT
static struct ast_chan * find_chan(char *name)
void MD5Final(unsigned char digest[16], struct MD5Context *context)
static int hangup(void *data)
static int inbuf(struct baseio *bio, FILE *fi)
utility used by inchar(), for base_encode()
static int input(yyscan_t yyscanner)
#define AST_LIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
static struct ast_str * password
void MD5Init(struct MD5Context *context)
static int event_newexten(struct ast_mansession *s, struct message *m)
static struct event events[]
static void try_hangup(newtComponent c)
static struct ast_mansession session
static void try_redirect(newtComponent c)
static void del_chan(char *name)
static int manage_calls(char *host)
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
static void challenge(const char *realm, pjsip_tx_data *tdata, const pjsip_rx_data *rdata, int is_stale)
astobj2 callback for adding digest challenges to responses
A set of macros to manage forward-linked lists.
static struct channel_usage channels
void MD5Update(struct MD5Context *context, unsigned char const *buf, unsigned len)
static void try_status(void)
#define AST_LIST_HEAD_NOLOCK_STATIC(name, type)
Defines a structure to be used to hold a list of specified type, statically initialized.
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
static int show_doing(char *title, char *tmp)
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
static int show_message(char *title, char *msg)
static int process_message(struct ast_mansession *s, struct message *m)
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
#define AST_LIST_ENTRY(type)
Declare a forward link structure inside a list entry.
static int event_newstate(struct ast_mansession *s, struct message *m)
static int get_user_input(char *msg, char *buf, int buflen)
static void rebuild_channels(newtComponent c)
static int manager_login(char *hostname)
static int event_status(struct ast_mansession *s, struct message *m)
static int event_newchannel(struct ast_mansession *s, struct message *m)
static int hide_doing(void)
static int event_ignore(struct ast_mansession *s, struct message *m)
int ast_add_profile(const char *, uint64_t scale)
support for event profiling
static struct ast_str * hostname
const char * headers[AST_MAX_MANHEADERS]
int main(int argc, char *argv[])
static void fdprintf(int fd, char *fmt,...)
#define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
static int get_input(struct ast_mansession *s, char *output)
static int event_rename(struct ast_mansession *s, struct message *m)
static char context[AST_MAX_CONTEXT]
static int manager_action(char *action, char *fmt,...)
static int input_check(struct ast_mansession *s, struct message **mout)
static struct hostent * hp
int64_t ast_profile(int, int64_t)
static snd_pcm_format_t format
static int has_input(struct ast_mansession *s)