manager.h File Reference

#include <stdarg.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <asterisk/lock.h>

Include dependency graph for manager.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  mansession
struct  message
struct  manager_action

Defines

#define DEFAULT_MANAGER_PORT   5038
#define EVENT_FLAG_SYSTEM   (1 << 0)
#define EVENT_FLAG_CALL   (1 << 1)
#define EVENT_FLAG_LOG   (1 << 2)
#define EVENT_FLAG_VERBOSE   (1 << 3)
#define EVENT_FLAG_COMMAND   (1 << 4)
#define EVENT_FLAG_AGENT   (1 << 5)
#define EVENT_FLAG_USER   (1 << 6)
#define MAX_HEADERS   80
#define MAX_LEN   256
#define ast_manager_register(a, b, c, d)   ast_manager_register2(a, b, c, d, NULL)

Functions

int ast_carefulwrite (int fd, char *s, int len, int timeoutms)
int ast_manager_register2 (char *action, int authority, int(*func)(struct mansession *s, struct message *m), char *synopsis, char *description)
int ast_manager_unregister (char *action)
int manager_event (int category, char *event, char *contents,...) __attribute__((format(printf
int char * astman_get_header (struct message *m, char *var)
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)
void astman_send_ack (struct mansession *s, struct message *m, char *msg)
int init_manager (void)
int reload_manager (void)


Define Documentation

#define ast_manager_register ( a,
b,
c,
 )     ast_manager_register2(a, b, c, d, NULL)

Definition at line 97 of file manager.h.

#define DEFAULT_MANAGER_PORT   5038

Definition at line 42 of file manager.h.

#define EVENT_FLAG_AGENT   (1 << 5)

Definition at line 49 of file manager.h.

#define EVENT_FLAG_CALL   (1 << 1)

Definition at line 45 of file manager.h.

#define EVENT_FLAG_COMMAND   (1 << 4)

Definition at line 48 of file manager.h.

#define EVENT_FLAG_LOG   (1 << 2)

Definition at line 46 of file manager.h.

#define EVENT_FLAG_SYSTEM   (1 << 0)

Definition at line 44 of file manager.h.

#define EVENT_FLAG_USER   (1 << 6)

Definition at line 50 of file manager.h.

#define EVENT_FLAG_VERBOSE   (1 << 3)

Definition at line 47 of file manager.h.

#define MAX_HEADERS   80

Definition at line 53 of file manager.h.

#define MAX_LEN   256

Definition at line 54 of file manager.h.


Function Documentation

int ast_carefulwrite ( int  fd,
char *  s,
int  len,
int  timeoutms 
)

Definition at line 85 of file manager.c.

00086 {
00087    /* Try to write string, but wait no more than ms milliseconds
00088       before timing out */
00089    int res=0;
00090    struct pollfd fds[1];
00091    while(len) {
00092       res = write(fd, s, len);
00093       if ((res < 0) && (errno != EAGAIN)) {
00094          return -1;
00095       }
00096       if (res < 0) res = 0;
00097       len -= res;
00098       s += res;
00099       fds[0].fd = fd;
00100       fds[0].events = POLLOUT;
00101       /* Wait until writable again */
00102       res = poll(fds, 1, timeoutms);
00103       if (res < 1)
00104          return -1;
00105    }
00106    return res;
00107 }

int ast_manager_register2 ( char *  action,
int  authority,
int(*)(struct mansession *s, struct message *m)  func,
char *  synopsis,
char *  description 
)

Definition at line 1421 of file manager.c.

01422 {
01423    struct manager_action *cur;
01424 
01425    cur = malloc(sizeof(struct manager_action));
01426    if (!cur) {
01427       ast_log(LOG_WARNING, "Manager: out of memory trying to register action\n");
01428       ast_mutex_unlock(&actionlock);
01429       return -1;
01430    }
01431    cur->action = action;
01432    cur->authority = auth;
01433    cur->func = func;
01434    cur->synopsis = synopsis;
01435    cur->description = description;
01436    cur->next = NULL;
01437 
01438    ast_manager_register_struct(cur);
01439 
01440    return 0;
01441 }

int ast_manager_unregister ( char *  action  ) 

Definition at line 1353 of file manager.c.

01353                                            {
01354    struct manager_action *cur = first_action, *prev = first_action;
01355 
01356    ast_mutex_lock(&actionlock);
01357    while( cur ) {       
01358       if (!strcasecmp(action, cur->action)) {
01359          prev->next = cur->next;
01360          free(cur);
01361          if (option_verbose > 1) 
01362             ast_verbose(VERBOSE_PREFIX_2 "Manager unregistered action %s\n", action);
01363          ast_mutex_unlock(&actionlock);
01364          return 0;
01365       }
01366       prev = cur;
01367       cur = cur->next;
01368    }
01369    ast_mutex_unlock(&actionlock);
01370    return 0;
01371 }

int char* astman_get_header ( struct message m,
char *  var 
)

Definition at line 256 of file manager.c.

00257 {
00258    char cmp[80];
00259    int x;
00260    snprintf(cmp, sizeof(cmp), "%s: ", var);
00261    for (x=0;x<m->hdrcount;x++)
00262       if (!strncasecmp(cmp, m->headers[x], strlen(cmp)))
00263          return m->headers[x] + strlen(cmp);
00264    return "";
00265 }

void astman_send_ack ( struct mansession s,
struct message m,
char *  msg 
)

Definition at line 292 of file manager.c.

00293 {
00294    astman_send_response(s, m, "Success", msg);
00295 }

void astman_send_error ( struct mansession s,
struct message m,
char *  error 
)

Definition at line 267 of file manager.c.

00268 {
00269    char *id = astman_get_header(m,"ActionID");
00270    ast_mutex_lock(&s->lock);
00271    ast_cli(s->fd, "Response: Error\r\n");
00272    if (id && !ast_strlen_zero(id))
00273       ast_cli(s->fd, "ActionID: %s\r\n",id);
00274    ast_cli(s->fd, "Message: %s\r\n\r\n", error);
00275    ast_mutex_unlock(&s->lock);
00276 }

void astman_send_response ( struct mansession s,
struct message m,
char *  resp,
char *  msg 
)

Definition at line 278 of file manager.c.

00279 {
00280    char *id = astman_get_header(m,"ActionID");
00281    ast_mutex_lock(&s->lock);
00282    ast_cli(s->fd, "Response: %s\r\n", resp);
00283    if (id && !ast_strlen_zero(id))
00284       ast_cli(s->fd, "ActionID: %s\r\n",id);
00285    if (msg)
00286       ast_cli(s->fd, "Message: %s\r\n\r\n", msg);
00287    else
00288       ast_cli(s->fd, "\r\n");
00289    ast_mutex_unlock(&s->lock);
00290 }

int init_manager ( void   ) 

Definition at line 1445 of file manager.c.

01446 {
01447    struct ast_config *cfg;
01448    char *val;
01449    int oldportno = portno;
01450    static struct sockaddr_in ba;
01451    int x = 1;
01452    if (!registered) {
01453       /* Register default actions */
01454       ast_manager_register2("Ping", 0, action_ping, "Ping", mandescr_ping);
01455       ast_manager_register2("Events", 0, action_events, "Contol Event Flow", mandescr_events);
01456       ast_manager_register2("Logoff", 0, action_logoff, "Logoff Manager", mandescr_logoff);
01457       ast_manager_register2("Hangup", EVENT_FLAG_CALL, action_hangup, "Hangup Channel", mandescr_hangup);
01458       ast_manager_register( "Status", EVENT_FLAG_CALL, action_status, "Status" );
01459       ast_manager_register2( "Setvar", EVENT_FLAG_CALL, action_setvar, "Set Channel Variable", mandescr_setvar );
01460       ast_manager_register2( "Getvar", EVENT_FLAG_CALL, action_getvar, "Gets a Channel Variable", mandescr_getvar );
01461       ast_manager_register( "Redirect", EVENT_FLAG_CALL, action_redirect, "Redirect" );
01462       ast_manager_register2("Originate", EVENT_FLAG_CALL, action_originate, "Originate Call", mandescr_originate);
01463       ast_manager_register2( "Command", EVENT_FLAG_COMMAND, action_command, "Execute Command", mandescr_command );
01464       ast_manager_register2( "ExtensionState", EVENT_FLAG_CALL, action_extensionstate, "Check Extension Status", mandescr_extensionstate );
01465       ast_manager_register2( "AbsoluteTimeout", EVENT_FLAG_CALL, action_timeout, "Set Absolute Timeout", mandescr_timeout );
01466       ast_manager_register2( "MailboxStatus", EVENT_FLAG_CALL, action_mailboxstatus, "Check Mailbox", mandescr_mailboxstatus );
01467       ast_manager_register2( "MailboxCount", EVENT_FLAG_CALL, action_mailboxcount, "Check Mailbox Message Count", mandescr_mailboxcount );
01468       ast_manager_register2("ListCommands", 0, action_listcommands, "List available manager commands", mandescr_listcommands);
01469 
01470       ast_cli_register(&show_mancmd_cli);
01471       ast_cli_register(&show_mancmds_cli);
01472       ast_cli_register(&show_manconn_cli);
01473       ast_extension_state_add(NULL, NULL, manager_state_cb, NULL);
01474       registered = 1;
01475    }
01476    portno = DEFAULT_MANAGER_PORT;
01477    cfg = ast_load("manager.conf");
01478    if (!cfg) {
01479       ast_log(LOG_NOTICE, "Unable to open management configuration manager.conf.  Call management disabled.\n");
01480       return 0;
01481    }
01482    memset(&ba, 0, sizeof(ba));
01483    val = ast_variable_retrieve(cfg, "general", "enabled");
01484    if (val)
01485       enabled = ast_true(val);
01486 
01487    val = ast_variable_retrieve(cfg, "general", "block-sockets");
01488    if(val)
01489       block_sockets = ast_true(val);
01490 
01491    if ((val = ast_variable_retrieve(cfg, "general", "port"))) {
01492       if (sscanf(val, "%d", &portno) != 1) {
01493          ast_log(LOG_WARNING, "Invalid port number '%s'\n", val);
01494          portno = DEFAULT_MANAGER_PORT;
01495       }
01496    } else if ((val = ast_variable_retrieve(cfg, "general", "portno"))) {
01497       if (sscanf(val, "%d", &portno) != 1) {
01498          ast_log(LOG_WARNING, "Invalid port number '%s'\n", val);
01499          portno = DEFAULT_MANAGER_PORT;
01500       }
01501       ast_log(LOG_NOTICE, "Use of portno in manager.conf deprecated.  Please use 'port=%s' instead.\n", val);
01502    }
01503    
01504    ba.sin_family = AF_INET;
01505    ba.sin_port = htons(portno);
01506    memset(&ba.sin_addr, 0, sizeof(ba.sin_addr));
01507    
01508    if ((val = ast_variable_retrieve(cfg, "general", "bindaddr"))) {
01509       if (!inet_aton(val, &ba.sin_addr)) { 
01510          ast_log(LOG_WARNING, "Invalid address '%s' specified, using 0.0.0.0\n", val);
01511          memset(&ba.sin_addr, 0, sizeof(ba.sin_addr));
01512       }
01513    }
01514    
01515    if ((asock > -1) && ((portno != oldportno) || !enabled)) {
01516 #if 0
01517       /* Can't be done yet */
01518       close(asock);
01519       asock = -1;
01520 #else
01521       ast_log(LOG_WARNING, "Unable to change management port / enabled\n");
01522 #endif
01523    }
01524    ast_destroy(cfg);
01525    
01526    /* If not enabled, do nothing */
01527    if (!enabled) {
01528       return 0;
01529    }
01530    if (asock < 0) {
01531       asock = socket(AF_INET, SOCK_STREAM, 0);
01532       if (asock < 0) {
01533          ast_log(LOG_WARNING, "Unable to create socket: %s\n", strerror(errno));
01534          return -1;
01535       }
01536       setsockopt(asock, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
01537       if (bind(asock, (struct sockaddr *)&ba, sizeof(ba))) {
01538          ast_log(LOG_WARNING, "Unable to bind socket: %s\n", strerror(errno));
01539          close(asock);
01540          asock = -1;
01541          return -1;
01542       }
01543       if (listen(asock, 2)) {
01544          ast_log(LOG_WARNING, "Unable to listen on socket: %s\n", strerror(errno));
01545          close(asock);
01546          asock = -1;
01547          return -1;
01548       }
01549       if (option_verbose)
01550          ast_verbose("Asterisk Management interface listening on port %d\n", portno);
01551       ast_pthread_create(&t, NULL, accept_thread, NULL);
01552    }
01553    return 0;
01554 }

int manager_event ( int  category,
char *  event,
char *  contents,
  ... 
)

int reload_manager ( void   ) 

Definition at line 1556 of file manager.c.

01557 {
01558    manager_event(EVENT_FLAG_SYSTEM, "Reload", "Message: Reload Requested\r\n");
01559    return init_manager();
01560 }


Generated on Wed Oct 28 17:00:58 2009 for Asterisk by  doxygen 1.5.6