2 * Asterisk -- A telephony toolkit for Linux.
4 * Channel Management and more
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
19 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <netinet/tcp.h>
24 #include <arpa/inet.h>
28 #include <asterisk/channel.h>
29 #include <asterisk/file.h>
30 #include <asterisk/manager.h>
31 #include <asterisk/config.h>
32 #include <asterisk/lock.h>
33 #include <asterisk/logger.h>
34 #include <asterisk/options.h>
35 #include <asterisk/cli.h>
36 #include <asterisk/app.h>
37 #include <asterisk/pbx.h>
38 #include <asterisk/md5.h>
39 #include <asterisk/acl.h>
41 static int enabled = 0;
42 static int portno = DEFAULT_MANAGER_PORT;
43 static int asock = -1;
45 static ast_mutex_t sessionlock = AST_MUTEX_INITIALIZER;
47 static struct permalias {
51 { EVENT_FLAG_SYSTEM, "system" },
52 { EVENT_FLAG_CALL, "call" },
53 { EVENT_FLAG_LOG, "log" },
54 { EVENT_FLAG_VERBOSE, "verbose" },
55 { EVENT_FLAG_COMMAND, "command" },
56 { EVENT_FLAG_AGENT, "agent" },
57 { EVENT_FLAG_USER, "user" },
61 static struct mansession *sessions = NULL;
62 static struct manager_action *first_action = NULL;
63 static ast_mutex_t actionlock = AST_MUTEX_INITIALIZER;
65 static int handle_showmancmds(int fd, int argc, char *argv[])
67 struct manager_action *cur = first_action;
69 ast_mutex_lock(&actionlock);
70 while(cur) { /* Walk the list of actions */
71 ast_cli(fd, "\t%s %s\r\n",cur->action, cur->synopsis);
75 ast_mutex_unlock(&actionlock);
76 return RESULT_SUCCESS;
79 static int handle_showmanconn(int fd, int argc, char *argv[])
83 ast_mutex_lock(&sessionlock);
85 ast_cli(fd, " Username\tIP Address\n");
87 ast_cli(fd, " %s\t\t%s\r\n",s->username, inet_ntoa(s->sin.sin_addr));
91 ast_mutex_unlock(&sessionlock);
92 return RESULT_SUCCESS;
95 static char showmancmds_help[] =
96 "Usage: show manager commands\n"
97 " Prints a listing of all the available manager commands.\n";
99 static char showmanconn_help[] =
100 "Usage: show manager connected\n"
101 " Prints a listing of the users that are connected to the\n"
102 "manager interface.\n";
104 static struct ast_cli_entry show_mancmds_cli =
105 { { "show", "manager", "commands", NULL },
106 handle_showmancmds, "Show manager commands", showmancmds_help };
108 static struct ast_cli_entry show_manconn_cli =
109 { { "show", "manager", "connected", NULL },
110 handle_showmanconn, "Show connected manager users", showmanconn_help };
112 static void destroy_session(struct mansession *s)
114 struct mansession *cur, *prev = NULL;
115 ast_mutex_lock(&sessionlock);
125 prev->next = cur->next;
127 sessions = cur->next;
132 ast_log(LOG_WARNING, "Trying to delete non-existant session %p?\n", s);
133 ast_mutex_unlock(&sessionlock);
137 char *astman_get_header(struct message *m, char *var)
141 snprintf(cmp, sizeof(cmp), "%s: ", var);
142 for (x=0;x<m->hdrcount;x++)
143 if (!strncasecmp(cmp, m->headers[x], strlen(cmp)))
144 return m->headers[x] + strlen(cmp);
148 void astman_send_error(struct mansession *s, struct message *m, char *error)
150 char *id = astman_get_header(m,"ActionID");
151 ast_mutex_lock(&s->lock);
152 ast_cli(s->fd, "Response: Error\r\n");
153 if (id && strlen(id))
154 ast_cli(s->fd, "ActionID: %s\r\n",id);
155 ast_cli(s->fd, "Message: %s\r\n\r\n", error);
156 ast_mutex_unlock(&s->lock);
159 void astman_send_response(struct mansession *s, struct message *m, char *resp, char *msg)
161 char *id = astman_get_header(m,"ActionID");
162 ast_mutex_lock(&s->lock);
163 ast_cli(s->fd, "Response: %s\r\n", resp);
164 if (id && strlen(id))
165 ast_cli(s->fd, "ActionID: %s\r\n",id);
167 ast_cli(s->fd, "Message: %s\r\n\r\n", msg);
169 ast_cli(s->fd, "\r\n");
170 ast_mutex_unlock(&s->lock);
173 void astman_send_ack(struct mansession *s, struct message *m, char *msg)
175 astman_send_response(s, m, "Success", msg);
178 static int get_perm(char *instr)
187 strncpy(tmp, instr, sizeof(tmp) - 1);
189 c = strsep(&stringp, ",");
191 for (x=0;x<sizeof(perms) / sizeof(perms[0]);x++) {
192 if (!strcasecmp(perms[x].label, c))
195 c = strsep(&stringp, ",");
200 static int authenticate(struct mansession *s, struct message *m)
202 struct ast_config *cfg;
204 char *user = astman_get_header(m, "Username");
205 char *pass = astman_get_header(m, "Secret");
206 char *authtype = astman_get_header(m, "AuthType");
207 char *key = astman_get_header(m, "Key");
209 cfg = ast_load("manager.conf");
212 cat = ast_category_browse(cfg, NULL);
214 if (strcasecmp(cat, "general")) {
216 if (!strcasecmp(cat, user)) {
217 struct ast_variable *v;
218 struct ast_ha *ha = NULL;
219 char *password = NULL;
220 v = ast_variable_browse(cfg, cat);
222 if (!strcasecmp(v->name, "secret")) {
224 } else if (!strcasecmp(v->name, "permit") ||
225 !strcasecmp(v->name, "deny")) {
226 ha = ast_append_ha(v->name, v->value, ha);
230 if (ha && !ast_apply_ha(ha, &(s->sin))) {
231 ast_log(LOG_NOTICE, "%s failed to pass IP ACL as '%s'\n", inet_ntoa(s->sin.sin_addr), user);
237 if (!strcasecmp(authtype, "MD5")) {
238 if (key && strlen(key) && s->challenge) {
241 char md5key[256] = "";
242 struct MD5Context md5;
243 unsigned char digest[16];
245 MD5Update(&md5, s->challenge, strlen(s->challenge));
246 MD5Update(&md5, password, strlen(password));
247 MD5Final(digest, &md5);
249 len += sprintf(md5key + len, "%2.2x", digest[x]);
250 if (!strcmp(md5key, key))
257 } else if (password && !strcasecmp(password, pass)) {
260 ast_log(LOG_NOTICE, "%s failed to authenticate as '%s'\n", inet_ntoa(s->sin.sin_addr), user);
266 cat = ast_category_browse(cfg, cat);
269 strncpy(s->username, cat, sizeof(s->username) - 1);
270 s->readperm = get_perm(ast_variable_retrieve(cfg, cat, "read"));
271 s->writeperm = get_perm(ast_variable_retrieve(cfg, cat, "write"));
275 ast_log(LOG_NOTICE, "%s tried to authenticate with non-existant user '%s'\n", inet_ntoa(s->sin.sin_addr), user);
280 static int action_ping(struct mansession *s, struct message *m)
282 astman_send_response(s, m, "Pong", NULL);
286 static int action_logoff(struct mansession *s, struct message *m)
288 astman_send_response(s, m, "Goodbye", "Thanks for all the fish.");
292 static int action_hangup(struct mansession *s, struct message *m)
294 struct ast_channel *c = NULL;
295 char *name = astman_get_header(m, "Channel");
297 astman_send_error(s, m, "No channel specified");
300 c = ast_channel_walk(NULL);
302 if (!strcasecmp(c->name, name)) {
305 c = ast_channel_walk(c);
308 astman_send_error(s, m, "No such channel");
311 ast_softhangup(c, AST_SOFTHANGUP_EXPLICIT);
312 astman_send_ack(s, m, "Channel Hungup");
316 static int action_status(struct mansession *s, struct message *m)
318 char *id = astman_get_header(m,"ActionID");
319 char idText[256] = "";
320 struct ast_channel *c;
322 astman_send_ack(s, m, "Channel status will follow");
323 c = ast_channel_walk(NULL);
324 if (id && strlen(id))
325 snprintf(idText,256,"ActionID: %s\r\n",id);
328 snprintf(bridge, sizeof(bridge), "Link: %s\r\n", c->bridge->name);
344 c->name, c->callerid ? c->callerid : "<unknown>",
345 ast_state2str(c->_state), c->context,
346 c->exten, c->priority, bridge, c->uniqueid, idText);
357 c->name, c->callerid ? c->callerid : "<unknown>",
358 ast_state2str(c->_state), bridge, c->uniqueid, idText);
360 c = ast_channel_walk(c);
365 static int action_redirect(struct mansession *s, struct message *m)
367 char *name = astman_get_header(m, "Channel");
368 char *name2 = astman_get_header(m, "ExtraChannel");
369 char *exten = astman_get_header(m, "Exten");
370 char *context = astman_get_header(m, "Context");
371 char *priority = astman_get_header(m, "Priority");
374 if (!name || !strlen(name)) {
375 astman_send_error(s, m, "Channel not specified");
378 if (strlen(priority) && (sscanf(priority, "%d", &pi) != 1)) {
379 astman_send_error(s, m, "Invalid priority\n");
382 res = ast_async_goto_by_name(name, context, exten, pi);
385 res = ast_async_goto_by_name(name2, context, exten, pi);
387 astman_send_ack(s, m, "Dual Redirect successful");
389 astman_send_error(s, m, "Secondary redirect failed");
391 astman_send_ack(s, m, "Redirect successful");
393 astman_send_error(s, m, "Redirect failed");
397 static int action_command(struct mansession *s, struct message *m)
399 char *cmd = astman_get_header(m, "Command");
400 ast_mutex_lock(&s->lock);
402 ast_mutex_unlock(&s->lock);
403 ast_cli(s->fd, "Response: Follows\r\n");
404 /* FIXME: Wedge a ActionID response in here, waiting for later changes */
405 ast_cli_command(s->fd, cmd);
406 ast_cli(s->fd, "--END COMMAND--\r\n\r\n");
407 ast_mutex_lock(&s->lock);
409 ast_mutex_unlock(&s->lock);
413 static int action_originate(struct mansession *s, struct message *m)
415 char *name = astman_get_header(m, "Channel");
416 char *exten = astman_get_header(m, "Exten");
417 char *context = astman_get_header(m, "Context");
418 char *priority = astman_get_header(m, "Priority");
419 char *timeout = astman_get_header(m, "Timeout");
420 char *callerid = astman_get_header(m, "CallerID");
421 char *variable = astman_get_header(m, "Variable");
422 char *account = astman_get_header(m, "Account");
423 char *app = astman_get_header(m, "Application");
424 char *appdata = astman_get_header(m, "Data");
432 astman_send_error(s, m, "Channel not specified");
435 if (strlen(priority) && (sscanf(priority, "%d", &pi) != 1)) {
436 astman_send_error(s, m, "Invalid priority\n");
439 if (strlen(timeout) && (sscanf(timeout, "%d", &to) != 1)) {
440 astman_send_error(s, m, "Invalid timeout\n");
443 strncpy(tmp, name, sizeof(tmp) - 1);
445 data = strchr(tmp, '/');
447 astman_send_error(s, m, "Invalid channel\n");
453 res = ast_pbx_outgoing_app(tech, AST_FORMAT_SLINEAR, data, to, app, appdata, &reason, 1, strlen(callerid) ? callerid : NULL, variable, account);
455 res = ast_pbx_outgoing_exten(tech, AST_FORMAT_SLINEAR, data, to, context, exten, pi, &reason, 1, strlen(callerid) ? callerid : NULL, variable, account);
458 astman_send_ack(s, m, "Originate successfully queued");
460 astman_send_error(s, m, "Originate failed");
464 static int action_mailboxstatus(struct mansession *s, struct message *m)
466 char *mailbox = astman_get_header(m, "Mailbox");
467 char *id = astman_get_header(m,"ActionID");
468 char idText[256] = "";
469 if (!mailbox || !strlen(mailbox)) {
470 astman_send_error(s, m, "Mailbox not specified");
473 if (id && strlen(id))
474 snprintf(idText,256,"ActionID: %s\r\n",id);
475 ast_cli(s->fd, "Response: Success\r\n"
477 "Message: Mailbox Status\r\n"
479 "Waiting: %d\r\n\r\n", idText, mailbox, ast_app_has_voicemail(mailbox));
483 static int action_mailboxcount(struct mansession *s, struct message *m)
485 char *mailbox = astman_get_header(m, "Mailbox");
486 char *id = astman_get_header(m,"ActionID");
487 char idText[256] = "";
488 int newmsgs = 0, oldmsgs = 0;
489 if (!mailbox || !strlen(mailbox)) {
490 astman_send_error(s, m, "Mailbox not specified");
493 ast_app_messagecount(mailbox, &newmsgs, &oldmsgs);
494 if (id && strlen(id)) {
495 snprintf(idText,256,"ActionID: %s\r\n",id);
497 ast_cli(s->fd, "Response: Success\r\n"
499 "Message: Mailbox Message Count\r\n"
501 "NewMessages: %d\r\n"
502 "OldMessages: %d\r\n"
504 idText,mailbox, newmsgs, oldmsgs);
508 static int action_extensionstate(struct mansession *s, struct message *m)
510 char *exten = astman_get_header(m, "Exten");
511 char *context = astman_get_header(m, "Context");
512 char *id = astman_get_header(m,"ActionID");
513 char idText[256] = "";
516 if (!exten || !strlen(exten)) {
517 astman_send_error(s, m, "Extension not specified");
520 if (!context || !strlen(context))
522 status = ast_extension_state(NULL, context, exten);
523 ast_get_hint(hint, sizeof(hint) - 1, NULL, context, exten);
524 if (id && strlen(id)) {
525 snprintf(idText,256,"ActionID: %s\r\n",id);
527 ast_cli(s->fd, "Response: Success\r\n"
529 "Message: Extension Status\r\n"
533 "Status: %d\r\n\r\n",
534 idText,exten, context, hint, status);
538 static int action_timeout(struct mansession *s, struct message *m)
540 struct ast_channel *c = NULL;
541 char *name = astman_get_header(m, "Channel");
542 int timeout = atoi(astman_get_header(m, "Timeout"));
544 astman_send_error(s, m, "No channel specified");
548 astman_send_error(s, m, "No timeout specified");
551 c = ast_channel_walk(NULL);
553 if (!strcasecmp(c->name, name)) {
556 c = ast_channel_walk(c);
559 astman_send_error(s, m, "No such channel");
562 ast_channel_setwhentohangup(c, timeout);
563 astman_send_ack(s, m, "Timeout Set");
567 static int process_message(struct mansession *s, struct message *m)
570 struct manager_action *tmp = first_action;
571 char *id = astman_get_header(m,"ActionID");
572 char idText[256] = "";
574 strncpy(action, astman_get_header(m, "Action"), sizeof(action));
575 ast_log( LOG_DEBUG, "Manager received command '%s'\n", action );
577 if (!strlen(action)) {
578 astman_send_error(s, m, "Missing action in request");
581 if (id && strlen(id)) {
582 snprintf(idText,256,"ActionID: %s\r\n",id);
584 if (!s->authenticated) {
585 if (!strcasecmp(action, "Challenge")) {
587 authtype = astman_get_header(m, "AuthType");
588 if (!strcasecmp(authtype, "MD5")) {
589 if (!s->challenge || !strlen(s->challenge)) {
590 ast_mutex_lock(&s->lock);
591 snprintf(s->challenge, sizeof(s->challenge), "%d", rand());
592 ast_mutex_unlock(&s->lock);
594 ast_cli(s->fd, "Response: Success\r\n"
596 "Challenge: %s\r\n\r\n",
597 idText,s->challenge);
600 astman_send_error(s, m, "Must specify AuthType");
603 } else if (!strcasecmp(action, "Login")) {
604 if (authenticate(s, m)) {
606 astman_send_error(s, m, "Authentication failed");
609 s->authenticated = 1;
610 if (option_verbose > 1)
611 ast_verbose(VERBOSE_PREFIX_2 "Manager '%s' logged on from %s\n", s->username, inet_ntoa(s->sin.sin_addr));
612 ast_log(LOG_EVENT, "Manager '%s' logged on from %s\n", s->username, inet_ntoa(s->sin.sin_addr));
613 astman_send_ack(s, m, "Authentication accepted");
615 } else if (!strcasecmp(action, "Logoff")) {
616 astman_send_ack(s, m, "See ya");
619 astman_send_error(s, m, "Authentication Required");
622 if (!strcasecmp(action, tmp->action)) {
623 if ((s->writeperm & tmp->authority) == tmp->authority) {
627 astman_send_error(s, m, "Permission denied");
633 astman_send_error(s, m, "Invalid/unknown command");
638 static int get_input(struct mansession *s, char *output)
640 /* output must have at least sizeof(s->inbuf) space */
644 for (x=1;x<s->inlen;x++) {
645 if ((s->inbuf[x] == '\n') && (s->inbuf[x-1] == '\r')) {
646 /* Copy output data up to and including \r\n */
647 memcpy(output, s->inbuf, x + 1);
648 /* Add trailing \0 */
650 /* Move remaining data back to the front */
651 memmove(s->inbuf, s->inbuf + x + 1, s->inlen - x);
656 if (s->inlen >= sizeof(s->inbuf) - 1) {
657 ast_log(LOG_WARNING, "Dumping long line with no return from %s: %s\n", inet_ntoa(s->sin.sin_addr), s->inbuf);
662 res = ast_select(s->fd + 1, &fds, NULL, NULL, NULL);
664 ast_log(LOG_WARNING, "Select returned error: %s\n", strerror(errno));
665 } else if (res > 0) {
666 ast_mutex_lock(&s->lock);
667 res = read(s->fd, s->inbuf + s->inlen, sizeof(s->inbuf) - 1 - s->inlen);
668 ast_mutex_unlock(&s->lock);
673 s->inbuf[s->inlen] = '\0';
677 static void *session_do(void *data)
679 struct mansession *s = data;
683 ast_mutex_lock(&s->lock);
684 ast_cli(s->fd, "Asterisk Call Manager/1.0\r\n");
685 ast_mutex_unlock(&s->lock);
686 memset(&m, 0, sizeof(&m));
688 res = get_input(s, m.headers[m.hdrcount]);
690 /* Strip trailing \r\n */
691 if (strlen(m.headers[m.hdrcount]) < 2)
693 m.headers[m.hdrcount][strlen(m.headers[m.hdrcount]) - 2] = '\0';
694 if (!strlen(m.headers[m.hdrcount])) {
695 if (process_message(s, &m))
697 memset(&m, 0, sizeof(&m));
698 } else if (m.hdrcount < MAX_HEADERS - 1)
703 if (s->authenticated) {
704 if (option_verbose > 1)
705 ast_verbose(VERBOSE_PREFIX_2 "Manager '%s' logged off from %s\n", s->username, inet_ntoa(s->sin.sin_addr));
706 ast_log(LOG_EVENT, "Manager '%s' logged off from %s\n", s->username, inet_ntoa(s->sin.sin_addr));
708 if (option_verbose > 1)
709 ast_verbose(VERBOSE_PREFIX_2 "Connect attempt from '%s' unable to authenticate\n", inet_ntoa(s->sin.sin_addr));
710 ast_log(LOG_EVENT, "Failed attempt from %s\n", inet_ntoa(s->sin.sin_addr));
716 static void *accept_thread(void *ignore)
719 struct sockaddr_in sin;
721 struct mansession *s;
726 pthread_attr_init(&attr);
727 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
730 sinlen = sizeof(sin);
731 as = accept(asock, (struct sockaddr *)&sin, &sinlen);
733 ast_log(LOG_NOTICE, "Accept returned -1: %s\n", strerror(errno));
736 p = getprotobyname("tcp");
738 if( setsockopt(as, p->p_proto, TCP_NODELAY, (char *)&arg, sizeof(arg) ) < 0 ) {
739 ast_log(LOG_WARNING, "Failed to set manager tcp connection to TCP_NODELAY mode: %s\n", strerror(errno));
742 s = malloc(sizeof(struct mansession));
744 ast_log(LOG_WARNING, "Failed to allocate management session: %s\n", strerror(errno));
747 memset(s, 0, sizeof(struct mansession));
748 memcpy(&s->sin, &sin, sizeof(sin));
749 ast_mutex_init(&s->lock);
751 ast_mutex_lock(&sessionlock);
754 ast_mutex_unlock(&sessionlock);
755 if (pthread_create(&t, &attr, session_do, s))
758 pthread_attr_destroy(&attr);
762 int manager_event(int category, char *event, char *fmt, ...)
764 struct mansession *s;
768 ast_mutex_lock(&sessionlock);
771 if ((s->readperm & category) == category) {
772 ast_mutex_lock(&s->lock);
774 ast_cli(s->fd, "Event: %s\r\n", event);
776 vsnprintf(tmp, sizeof(tmp), fmt, ap);
778 write(s->fd, tmp, strlen(tmp));
779 ast_cli(s->fd, "\r\n");
781 ast_mutex_unlock(&s->lock);
785 ast_mutex_unlock(&sessionlock);
789 int ast_manager_unregister( char *action ) {
790 struct manager_action *cur = first_action, *prev = first_action;
792 ast_mutex_lock(&actionlock);
794 if (!strcasecmp(action, cur->action)) {
795 prev->next = cur->next;
797 if (option_verbose > 1)
798 ast_verbose(VERBOSE_PREFIX_2 "Manager unregistered action %s\n", action);
799 ast_mutex_unlock(&actionlock);
805 ast_mutex_unlock(&actionlock);
809 static int manager_state_cb(char *context, char *exten, int state, void *data)
811 /* Notify managers of change */
812 manager_event(EVENT_FLAG_CALL, "ExtensionStatus", "Exten: %s\r\nContext: %s\r\nStatus: %d\r\n", exten, context, state);
816 int ast_manager_register( char *action, int auth,
817 int (*func)(struct mansession *s, struct message *m), char *synopsis)
819 struct manager_action *cur = first_action, *prev = NULL;
821 ast_mutex_lock(&actionlock);
822 while(cur) { /* Walk the list of actions */
823 if (!strcasecmp(cur->action, action)) {
824 ast_log(LOG_WARNING, "Manager: Action '%s' already registered\n", action);
825 ast_mutex_unlock(&actionlock);
831 cur = malloc( sizeof(struct manager_action) );
833 ast_log(LOG_WARNING, "Manager: out of memory trying to register action\n");
834 ast_mutex_unlock(&actionlock);
837 strncpy( cur->action, action, 255 );
838 cur->authority = auth;
840 cur->synopsis = synopsis;
843 if( prev ) prev->next = cur;
844 else first_action = cur;
846 if (option_verbose > 1)
847 ast_verbose(VERBOSE_PREFIX_2 "Manager registered action %s\n", action);
848 ast_mutex_unlock(&actionlock);
852 static int registered = 0;
854 int init_manager(void)
856 struct ast_config *cfg;
858 int oldportno = portno;
859 static struct sockaddr_in ba;
862 /* Register default actions */
863 ast_manager_register( "Ping", 0, action_ping, "Ping" );
864 ast_manager_register( "Logoff", 0, action_logoff, "Logoff Manager" );
865 ast_manager_register( "Hangup", EVENT_FLAG_CALL, action_hangup, "Hangup Channel" );
866 ast_manager_register( "Status", EVENT_FLAG_CALL, action_status, "Status" );
867 ast_manager_register( "Redirect", EVENT_FLAG_CALL, action_redirect, "Redirect" );
868 ast_manager_register( "Originate", EVENT_FLAG_CALL, action_originate, "Originate Call" );
869 ast_manager_register( "MailboxStatus", EVENT_FLAG_CALL, action_mailboxstatus, "Check Mailbox" );
870 ast_manager_register( "Command", EVENT_FLAG_COMMAND, action_command, "Execute Command" );
871 ast_manager_register( "ExtensionState", EVENT_FLAG_CALL, action_extensionstate, "Check Extension Status" );
872 ast_manager_register( "AbsoluteTimeout", EVENT_FLAG_CALL, action_timeout, "Set Absolute Timeout" );
873 ast_manager_register( "MailboxCount", EVENT_FLAG_CALL, action_mailboxcount, "Check Mailbox Message Count" );
875 ast_cli_register(&show_mancmds_cli);
876 ast_cli_register(&show_manconn_cli);
877 ast_extension_state_add(NULL, NULL, manager_state_cb, NULL);
880 portno = DEFAULT_MANAGER_PORT;
881 cfg = ast_load("manager.conf");
883 ast_log(LOG_NOTICE, "Unable to open management configuration manager.conf. Call management disabled.\n");
886 memset(&ba, 0, sizeof(ba));
887 val = ast_variable_retrieve(cfg, "general", "enabled");
889 enabled = ast_true(val);
891 if ((val = ast_variable_retrieve(cfg, "general", "portno"))) {
892 if (sscanf(val, "%d", &portno) != 1) {
893 ast_log(LOG_WARNING, "Invalid port number '%s'\n", val);
894 portno = DEFAULT_MANAGER_PORT;
898 ba.sin_family = AF_INET;
899 ba.sin_port = htons(portno);
900 memset(&ba.sin_addr, 0, sizeof(ba.sin_addr));
902 if ((val = ast_variable_retrieve(cfg, "general", "bindaddr"))) {
903 if (!inet_aton(val, &ba.sin_addr)) {
904 ast_log(LOG_WARNING, "Invalid address '%s' specified, using 0.0.0.0\n", val);
905 memset(&ba.sin_addr, 0, sizeof(ba.sin_addr));
909 if ((asock > -1) && ((portno != oldportno) || !enabled)) {
911 /* Can't be done yet */
915 ast_log(LOG_WARNING, "Unable to change management port / enabled\n");
920 /* If not enabled, do nothing */
925 asock = socket(AF_INET, SOCK_STREAM, 0);
927 ast_log(LOG_WARNING, "Unable to create socket: %s\n", strerror(errno));
930 setsockopt(asock, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
931 if (bind(asock, (struct sockaddr *)&ba, sizeof(ba))) {
932 ast_log(LOG_WARNING, "Unable to bind socket: %s\n", strerror(errno));
937 if (listen(asock, 2)) {
938 ast_log(LOG_WARNING, "Unable to listen on socket: %s\n", strerror(errno));
944 ast_verbose("Asterisk Management interface listening on port %d\n", portno);
945 pthread_create(&t, NULL, accept_thread, NULL);
950 int reload_manager(void)
952 manager_event(EVENT_FLAG_SYSTEM, "Reload", "Message: Reload Requested\r\n");
953 return init_manager();