Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:49:04 2009

## Asterisk developer's documentation

# manager.c File Reference

The Asterisk Management Interface - AMI. More...

`#include <stdio.h>`
`#include <stdlib.h>`
`#include <string.h>`
`#include <sys/time.h>`
`#include <sys/types.h>`
`#include <netdb.h>`
`#include <sys/socket.h>`
`#include <netinet/in.h>`
`#include <netinet/tcp.h>`
`#include <arpa/inet.h>`
`#include <signal.h>`
`#include <errno.h>`
`#include <unistd.h>`
`#include "asterisk.h"`
`#include "asterisk/channel.h"`
`#include "asterisk/file.h"`
`#include "asterisk/manager.h"`
`#include "asterisk/config.h"`
`#include "asterisk/callerid.h"`
`#include "asterisk/lock.h"`
`#include "asterisk/logger.h"`
`#include "asterisk/options.h"`
`#include "asterisk/cli.h"`
`#include "asterisk/app.h"`
`#include "asterisk/pbx.h"`
`#include "asterisk/md5.h"`
`#include "asterisk/acl.h"`
`#include "asterisk/utils.h"`

Include dependency graph for manager.c:

Go to the source code of this file.

## Data Structures

struct | fast_originate_helper
struct | permalias

## Functions

static void * | accept_thread (void *ignore)
static int | action_command (struct mansession *s, struct message *m)
| action_command: Manager command "command" - execute CLI command
static int | action_events (struct mansession *s, struct message *m)
static int | action_extensionstate (struct mansession *s, struct message *m)
static int | action_getvar (struct mansession *s, struct message *m)
static int | action_hangup (struct mansession *s, struct message *m)
static int | action_listcommands (struct mansession *s, struct message *m)
static int | action_logoff (struct mansession *s, struct message *m)
static int | action_mailboxcount (struct mansession *s, struct message *m)
static int | action_mailboxstatus (struct mansession *s, struct message *m)
static int | action_originate (struct mansession *s, struct message *m)
static int | action_ping (struct mansession *s, struct message *m)
static int | action_redirect (struct mansession *s, struct message *m)
| action_redirect: The redirect manager command
static int | action_setvar (struct mansession *s, struct message *m)
static int | action_status (struct mansession *s, struct message *m)
| action_status: Manager "status" command to show channels
static int | action_timeout (struct mansession *s, struct message *m)
static int | append_event (struct mansession *s, const char *str)
int | ast_carefulwrite (int fd, char *s, int len, int timeoutms)
static int | ast_instring (char *bigstr, char *smallstr, char delim)
static int | ast_is_number (char *string)
int | ast_manager_register2 (const char *action, int auth, int(*func)(struct mansession *s, struct message *m), const char *synopsis, const char *description)
| register a new command with manager, including online help. This is the preferred way to register a manager command
static int | ast_manager_register_struct (struct manager_action *act)
int | ast_manager_unregister (char *action)
| AST_MUTEX_DEFINE_STATIC (actionlock)
| AST_MUTEX_DEFINE_STATIC (sessionlock)
static int | ast_strings_to_mask (char *string)
char * | astman_get_header (struct message *m, char *var)
struct ast_variable * | astman_get_variables (struct message *m)
void | astman_send_ack (struct mansession *s, struct message *m, char *msg)
void | astman_send_error (struct mansession *s, struct message *m, char *error)
void | astman_send_response (struct mansession *s, struct message *m, char *resp, char *msg)
static int | authenticate (struct mansession *s, struct message *m)
static char * | authority_to_str (int authority, char *res, int reslen)
static char * | complete_show_mancmd (char *line, char *word, int pos, int state)
static void | destroy_session (struct mansession *s)
static void * | fast_originate (void *data)
static void | free_session (struct mansession *s)
static int | get_input (struct mansession *s, char *output)
static int | get_perm (char *instr)
static int | handle_showmancmd (int fd, int argc, char *argv[])
static int | handle_showmancmds (int fd, int argc, char *argv[])
| handle_showmancmds: CLI command
static int | handle_showmanconn (int fd, int argc, char *argv[])
| handle_showmanconn: CLI command show manager connected
int | init_manager (void)
int | manager_event (int category, char *event, char *fmt,...)
| manager_event: Send AMI event to client
static int | manager_state_cb (char *context, char *exten, int state, void *data)
static int | process_message (struct mansession *s, struct message *m)
int | reload_manager (void)
static void * | session_do (void *data)
static int | set_eventmask (struct mansession *s, char *eventmask)

## Variables

static int | asock = -1
static int | block_sockets = 0
static int | displayconnects = 1
static int | enabled = 0
static struct manager_action * | first_action = NULL
static char | mandescr_command []
static char | mandescr_events []
static char | mandescr_extensionstate []
static char | mandescr_getvar []
static char | mandescr_hangup []
static char | mandescr_listcommands []
static char | mandescr_logoff []
static char | mandescr_mailboxcount []
static char | mandescr_mailboxstatus []
| Help text for manager command mailboxstatus.
static char | mandescr_originate []
static char | mandescr_ping []
| PING: Manager PING.
static char | mandescr_redirect []
static char | mandescr_setvar []
static char | mandescr_timeout []
static struct permalias | perms []
static int | portno = DEFAULT_MANAGER_PORT
static int | registered = 0
static struct mansession * | sessions = NULL
static struct ast_cli_entry | show_mancmd_cli
static struct ast_cli_entry | show_mancmds_cli
static struct ast_cli_entry | show_manconn_cli
static char | showmancmd_help []
static char | showmancmds_help []
static char | showmanconn_help []
static pthread_t | t

## Detailed Description

The Asterisk Management Interface - AMI.

Channel Management and more

manager.conf

Definition in file manager.c.

## Function Documentation

int init_manager | ( | void |  | ) |

Called by Asterisk initialization

Definition at line 1651 of file manager.c.

References accept_thread(), action_command(), action_events(), action_extensionstate(), action_getvar(), action_hangup(), action_listcommands(), action_logoff(), action_mailboxcount(), action_mailboxstatus(), action_originate(), action_ping(), action_redirect(), action_setvar(), action_status(), action_timeout(), ast_cli_register(), ast_config_destroy(), ast_config_load(), ast_extension_state_add(), ast_log(), ast_manager_register, ast_manager_register2(), ast_pthread_create, ast_true(), ast_variable_retrieve(), ast_verbose(), cfg, DEFAULT_MANAGER_PORT, EVENT_FLAG_CALL, EVENT_FLAG_COMMAND, LOG_NOTICE, LOG_WARNING, manager_state_cb(), and option_verbose.

Referenced by main(), and reload_manager().

    01652 {
    01653    struct ast_config *cfg;
    01654    char *val;
    01655    int oldportno = portno;
    01656    static struct sockaddr_in ba;
    01657    int x = 1;
    01658    if (!registered) {
    01659       /* Register default actions */
    01660       ast_manager_register2("Ping", 0, action_ping, "Keepalive command", mandescr_ping);
    01661       ast_manager_register2("Events", 0, action_events, "Control Event Flow", mandescr_events);
    01662       ast_manager_register2("Logoff", 0, action_logoff, "Logoff Manager", mandescr_logoff);
    01663       ast_manager_register2("Hangup", EVENT_FLAG_CALL, action_hangup, "Hangup Channel", mandescr_hangup);
    01664       ast_manager_register("Status", EVENT_FLAG_CALL, action_status, "Lists channel status" );
    01665       ast_manager_register2("Setvar", EVENT_FLAG_CALL, action_setvar, "Set Channel Variable", mandescr_setvar );
    01666       ast_manager_register2("Getvar", EVENT_FLAG_CALL, action_getvar, "Gets a Channel Variable", mandescr_getvar );
    01667       ast_manager_register2("Redirect", EVENT_FLAG_CALL, action_redirect, "Redirect (transfer) a call", mandescr_redirect );
    01668       ast_manager_register2("Originate", EVENT_FLAG_CALL, action_originate, "Originate Call", mandescr_originate);
    01669       ast_manager_register2("Command", EVENT_FLAG_COMMAND, action_command, "Execute Asterisk CLI Command", mandescr_command );
    01670       ast_manager_register2("ExtensionState", EVENT_FLAG_CALL, action_extensionstate, "Check Extension Status", mandescr_extensionstate );
    01671       ast_manager_register2("AbsoluteTimeout", EVENT_FLAG_CALL, action_timeout, "Set Absolute Timeout", mandescr_timeout );
    01672       ast_manager_register2("MailboxStatus", EVENT_FLAG_CALL, action_mailboxstatus, "Check Mailbox", mandescr_mailboxstatus );
    01673       ast_manager_register2("MailboxCount", EVENT_FLAG_CALL, action_mailboxcount, "Check Mailbox Message Count", mandescr_mailboxcount );
    01674       ast_manager_register2("ListCommands", 0, action_listcommands, "List available manager commands", mandescr_listcommands);
    01675
    01676       ast_cli_register(&show_mancmd_cli);
    01677       ast_cli_register(&show_mancmds_cli);
    01678       ast_cli_register(&show_manconn_cli);
    01679       ast_extension_state_add(NULL, NULL, manager_state_cb, NULL);
    01680       registered = 1;
    01681    }
    01682    portno = DEFAULT_MANAGER_PORT;
    01683    displayconnects = 1;
    01684    cfg = ast_config_load("manager.conf");
    01685    if (!cfg) {
    01686       ast_log(LOG_NOTICE, "Unable to open management configuration manager.conf.  Call management disabled.\n");
    01687       return 0;
    01688    }
    01689    memset(&ba, 0, sizeof(ba));
    01690    val = ast_variable_retrieve(cfg, "general", "enabled");
    01691    if (val)
    01692       enabled = ast_true(val);
    01693
    01694    val = ast_variable_retrieve(cfg, "general", "block-sockets");
    01695    if(val)
    01696       block_sockets = ast_true(val);
    01697
    01698    if ((val = ast_variable_retrieve(cfg, "general", "port"))) {
    01699       if (sscanf(val, "%5d", &portno) != 1) {
    01700          ast_log(LOG_WARNING, "Invalid port number '%s'\n", val);
    01701          portno = DEFAULT_MANAGER_PORT;
    01702       }
    01703    } else if ((val = ast_variable_retrieve(cfg, "general", "portno"))) {
    01704       if (sscanf(val, "%5d", &portno) != 1) {
    01705          ast_log(LOG_WARNING, "Invalid port number '%s'\n", val);
    01706          portno = DEFAULT_MANAGER_PORT;
    01707       }
    01708       ast_log(LOG_NOTICE, "Use of portno in manager.conf deprecated.  Please use 'port=%s' instead.\n", val);
    01709    }
    01710    /* Parsing the displayconnects */
    01711    if ((val = ast_variable_retrieve(cfg, "general", "displayconnects"))) {
    01712          displayconnects = ast_true(val);;
    01713    }
    01714
    01715
    01716    ba.sin_family = AF_INET;
    01717    ba.sin_port = htons(portno);
    01718    memset(&ba.sin_addr, 0, sizeof(ba.sin_addr));
    01719
    01720    if ((val = ast_variable_retrieve(cfg, "general", "bindaddr"))) {
    01721       if (!inet_aton(val, &ba.sin_addr)) {
    01722          ast_log(LOG_WARNING, "Invalid address '%s' specified, using 0.0.0.0\n", val);
    01723          memset(&ba.sin_addr, 0, sizeof(ba.sin_addr));
    01724       }
    01725    }
    01726
    01727    if ((asock > -1) && ((portno != oldportno) || !enabled)) {
    01728 #if 0
    01729       /* Can't be done yet */
    01730       close(asock);
    01731       asock = -1;
    01732 #else
    01733       ast_log(LOG_WARNING, "Unable to change management port / enabled\n");
    01734 #endif
    01735    }
    01736    ast_config_destroy(cfg);
    01737
    01738    /* If not enabled, do nothing */
    01739    if (!enabled) {
    01740       return 0;
    01741    }
    01742    if (asock < 0) {
    01743       asock = socket(AF_INET, SOCK_STREAM, 0);
    01744       if (asock < 0) {
    01745          ast_log(LOG_WARNING, "Unable to create socket: %s\n", strerror(errno));
    01746          return -1;
    01747       }
    01748       setsockopt(asock, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
    01749       if (bind(asock, (struct sockaddr *)&ba, sizeof(ba))) {
    01750          ast_log(LOG_WARNING, "Unable to bind socket: %s\n", strerror(errno));
    01751          close(asock);
    01752          asock = -1;
    01753          return -1;
    01754       }
    01755       if (listen(asock, 2)) {
    01756          ast_log(LOG_WARNING, "Unable to listen on socket: %s\n", strerror(errno));
    01757          close(asock);
    01758          asock = -1;
    01759          return -1;
    01760       }
    01761       if (option_verbose)
    01762          ast_verbose("Asterisk Management interface listening on port %d\n", portno);
    01763       ast_pthread_create(&t, NULL, accept_thread, NULL);
    01764    }
    01765    return 0;
    01766 }

int reload_manager | ( | void |  | ) |

Called by Asterisk initialization

Definition at line 1768 of file manager.c.

References EVENT_FLAG_SYSTEM, init_manager(), and manager_event().

Referenced by ast_module_reload(), and main().

    01769 {
    01770    manager_event(EVENT_FLAG_SYSTEM, "Reload", "Message: Reload Requested\r\n");
    01771    return init_manager();
    01772 }

## Variable Documentation

int registered = 0`[static]`

END Doxygen group

Definition at line 1649 of file manager.c.