Sat Apr 26 2014 22:01:38

Asterisk developer's documentation


manager.h
Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 #ifndef _ASTERISK_MANAGER_H
00020 #define _ASTERISK_MANAGER_H
00021 
00022 #include "asterisk/network.h"
00023 #include "asterisk/lock.h"
00024 #include "asterisk/datastore.h"
00025 #include "asterisk/xmldoc.h"
00026 
00027 /*!
00028  \file
00029  \brief The AMI - Asterisk Manager Interface - is a TCP protocol created to
00030  manage Asterisk with third-party software.
00031 
00032  Manager protocol packages are text fields of the form a: b.  There is
00033  always exactly one space after the colon.
00034 
00035 \verbatim
00036 
00037  For Actions replies, the first line of the reply is a "Response:" header with
00038  values "success", "error" or "follows". "Follows" implies that the
00039  response is coming as separate events with the same ActionID. If the
00040  Action request has no ActionID, it will be hard matching events
00041  to the Action request in the manager client.
00042 
00043  The first header type is the "Event" header.  Other headers vary from
00044  event to event.  Headers end with standard \\r\\n termination.
00045  The last line of the manager response or event is an empty line.
00046  (\\r\\n)
00047 
00048 \endverbatim
00049 
00050  \note Please try to \b re-use \b existing \b headers to simplify manager message parsing in clients.
00051     Don't re-use an existing header with a new meaning, please.
00052     You can find a reference of standard headers in doc/manager.txt
00053 
00054 - \ref manager.c Main manager code file
00055  */
00056 
00057 #define AMI_VERSION                     "1.3"
00058 #define DEFAULT_MANAGER_PORT 5038   /* Default port for Asterisk management via TCP */
00059 #define DEFAULT_MANAGER_TLS_PORT 5039  /* Default port for Asterisk management via TCP */
00060 
00061 /*! \name Constant return values
00062  *\note Currently, returning anything other than zero causes the session to terminate.
00063  */
00064 /*@{ */
00065 #define  AMI_SUCCESS (0)
00066 #define  AMI_DESTROY (-1)
00067 /*@} */
00068 
00069 /*! \name Manager event classes */
00070 /*@{ */
00071 #define EVENT_FLAG_SYSTEM     (1 << 0) /* System events such as module load/unload */
00072 #define EVENT_FLAG_CALL       (1 << 1) /* Call event, such as state change, etc */
00073 #define EVENT_FLAG_LOG        (1 << 2) /* Log events */
00074 #define EVENT_FLAG_VERBOSE    (1 << 3) /* Verbose messages */
00075 #define EVENT_FLAG_COMMAND    (1 << 4) /* Ability to read/set commands */
00076 #define EVENT_FLAG_AGENT      (1 << 5) /* Ability to read/set agent info */
00077 #define EVENT_FLAG_USER                 (1 << 6) /* Ability to read/set user info */
00078 #define EVENT_FLAG_CONFIG     (1 << 7) /* Ability to modify configurations */
00079 #define EVENT_FLAG_DTMF       (1 << 8) /* Ability to read DTMF events */
00080 #define EVENT_FLAG_REPORTING     (1 << 9) /* Reporting events such as rtcp sent */
00081 #define EVENT_FLAG_CDR        (1 << 10) /* CDR events */
00082 #define EVENT_FLAG_DIALPLAN      (1 << 11) /* Dialplan events (VarSet, NewExten) */
00083 #define EVENT_FLAG_ORIGINATE  (1 << 12) /* Originate a call to an extension */
00084 #define EVENT_FLAG_AGI        (1 << 13) /* AGI events */
00085 #define EVENT_FLAG_HOOKRESPONSE     (1 << 14) /* Hook Response */
00086 #define EVENT_FLAG_CC         (1 << 15) /* Call Completion events */
00087 #define EVENT_FLAG_AOC        (1 << 16) /* Advice Of Charge events */
00088 #define EVENT_FLAG_TEST       (1 << 17) /* Test event used to signal the Asterisk Test Suite */
00089 /*XXX Why shifted by 30? XXX */
00090 #define EVENT_FLAG_MESSAGE    (1 << 30) /* MESSAGE events. */
00091 /*@} */
00092 
00093 /*! \brief Export manager structures */
00094 #define AST_MAX_MANHEADERS 128
00095 
00096 /*! \brief Manager Helper Function */
00097 typedef int (*manager_hook_t)(int, const char *, char *);
00098 
00099 struct manager_custom_hook {
00100    /*! Identifier */
00101    char *file;
00102    /*! helper function */
00103    manager_hook_t helper;
00104    /*! Linked list information */
00105    AST_RWLIST_ENTRY(manager_custom_hook) list;
00106 };
00107 
00108 /*! \brief Check if AMI is enabled */
00109 int check_manager_enabled(void);
00110 
00111 /*! \brief Check if AMI/HTTP is enabled */
00112 int check_webmanager_enabled(void);
00113 
00114 /*! Add a custom hook to be called when an event is fired 
00115  \param hook struct manager_custom_hook object to add
00116 */
00117 void ast_manager_register_hook(struct manager_custom_hook *hook);
00118 
00119 /*! Delete a custom hook to be called when an event is fired
00120     \param hook struct manager_custom_hook object to delete
00121 */
00122 void ast_manager_unregister_hook(struct manager_custom_hook *hook);
00123 
00124 /*! \brief Registered hooks can call this function to invoke actions and they will receive responses through registered callback
00125  * \param hook the file identifier specified in manager_custom_hook struct when registering a hook
00126  * \param msg ami action mesage string e.g. "Action: SipPeers\r\n"
00127 
00128  * \retval 0 on Success
00129  * \retval non-zero on Failure
00130 */
00131 int ast_hook_send_action(struct manager_custom_hook *hook, const char *msg);
00132 
00133 struct mansession;
00134 
00135 struct message {
00136    unsigned int hdrcount;
00137    const char *headers[AST_MAX_MANHEADERS];
00138 };
00139 
00140 struct manager_action {
00141    /*! Name of the action */
00142    const char *action;
00143    AST_DECLARE_STRING_FIELDS(
00144       AST_STRING_FIELD(synopsis);   /*!< Synopsis text (short description). */
00145       AST_STRING_FIELD(description);   /*!< Description (help text) */
00146       AST_STRING_FIELD(syntax);  /*!< Syntax text */
00147       AST_STRING_FIELD(arguments);  /*!< Description of each argument. */
00148       AST_STRING_FIELD(seealso); /*!< See also */
00149    );
00150    /*! Permission required for action.  EVENT_FLAG_* */
00151    int authority;
00152    /*! Function to be called */
00153    int (*func)(struct mansession *s, const struct message *m);
00154    struct ast_module *module;    /*!< Module this action belongs to */
00155    /*! Where the documentation come from. */
00156    enum ast_doc_src docsrc;
00157    /*! For easy linking */
00158    AST_RWLIST_ENTRY(manager_action) list;
00159    /*!
00160     * \brief TRUE if the AMI action is registered and the callback can be called.
00161     *
00162     * \note Needed to prevent a race between calling the callback
00163     * function and unregestring the AMI action object.
00164     */
00165    unsigned int registered:1;
00166 };
00167 
00168 /*! \brief External routines may register/unregister manager callbacks this way 
00169  * \note  Use ast_manager_register2() to register with help text for new manager commands */
00170 #define ast_manager_register(action, authority, func, synopsis) ast_manager_register2(action, authority, func, ast_module_info->self, synopsis, NULL)
00171 
00172 /*! \brief Register a manager callback using XML documentation to describe the manager. */
00173 #define ast_manager_register_xml(action, authority, func) ast_manager_register2(action, authority, func, ast_module_info->self, NULL, NULL)
00174 
00175 /*!
00176  * \brief Register a manager callback using XML documentation to describe the manager.
00177  *
00178  * \note For Asterisk core modules that are not independently
00179  * loadable.
00180  *
00181  * \warning If you use ast_manager_register_xml() instead when
00182  * you need to use this function, Asterisk will crash on load.
00183  */
00184 #define ast_manager_register_xml_core(action, authority, func) ast_manager_register2(action, authority, func, NULL, NULL, NULL)
00185 
00186 /*!
00187  * \brief Register a manager command with the manager interface
00188  * \param action Name of the requested Action:
00189  * \param authority Required authority for this command
00190  * \param func Function to call for this command
00191  * \param module The module containing func.  (NULL if module is part of core and not loadable)
00192  * \param synopsis Help text (one line, up to 30 chars) for CLI manager show commands
00193  * \param description Help text, several lines
00194  */
00195 int ast_manager_register2(
00196    const char *action,
00197    int authority,
00198    int (*func)(struct mansession *s, const struct message *m),
00199    struct ast_module *module,
00200    const char *synopsis,
00201    const char *description);
00202 
00203 /*!
00204  * \brief Unregister a registered manager command
00205  * \param action Name of registered Action:
00206  */
00207 int ast_manager_unregister(const char *action);
00208 
00209 /*! 
00210  * \brief Verify a session's read permissions against a permission mask.  
00211  * \param ident session identity
00212  * \param perm permission mask to verify
00213  * \retval 1 if the session has the permission mask capabilities
00214  * \retval 0 otherwise
00215  */
00216 int astman_verify_session_readpermissions(uint32_t ident, int perm);
00217 
00218 /*!
00219  * \brief Verify a session's write permissions against a permission mask.  
00220  * \param ident session identity
00221  * \param perm permission mask to verify
00222  * \retval 1 if the session has the permission mask capabilities, otherwise 0
00223  * \retval 0 otherwise
00224  */
00225 int astman_verify_session_writepermissions(uint32_t ident, int perm);
00226 
00227 /*! \brief External routines may send asterisk manager events this way 
00228  *    \param category   Event category, matches manager authorization
00229    \param event   Event name
00230    \param contents   Contents of event
00231 */
00232 
00233 /* XXX the parser in gcc 2.95 gets confused if you don't put a space
00234  * between the last arg before VA_ARGS and the comma */
00235 #define manager_event(category, event, contents , ...)   \
00236         __ast_manager_event_multichan(category, event, 0, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, contents , ## __VA_ARGS__)
00237 #define ast_manager_event(chan, category, event, contents , ...) \
00238    do { \
00239       struct ast_channel *_chans[] = { chan, }; \
00240       __ast_manager_event_multichan(category, event, 1, _chans, __FILE__, __LINE__, __PRETTY_FUNCTION__, contents , ## __VA_ARGS__); \
00241    } while (0)
00242 #define ast_manager_event_multichan(category, event, nchans, chans, contents , ...) \
00243    __ast_manager_event_multichan(category, event, nchans, chans, __FILE__, __LINE__, __PRETTY_FUNCTION__, contents , ## __VA_ARGS__);
00244 
00245 /*! External routines may send asterisk manager events this way
00246  * \param category Event category, matches manager authorization
00247  * \param event Event name
00248  * \param chancount Number of channels in chans parameter
00249  * \param chans A pointer to an array of channels involved in the event
00250  * \param contents Format string describing event
00251  * \since 1.8
00252 */
00253 int __ast_manager_event_multichan(int category, const char *event, int chancount,
00254       struct ast_channel **chans, const char *file, int line, const char *func,
00255       const char *contents, ...) __attribute__((format(printf, 8, 9)));
00256 
00257 /*! \brief Get header from mananger transaction */
00258 const char *astman_get_header(const struct message *m, char *var);
00259 
00260 /*! \brief Get a linked list of the Variable: headers */
00261 struct ast_variable *astman_get_variables(const struct message *m);
00262 
00263 /*! \brief Send error in manager transaction */
00264 void astman_send_error(struct mansession *s, const struct message *m, char *error);
00265 
00266 /*! \brief Send error in manager transaction (with va_args support) */
00267 void __attribute__((format(printf, 3, 4))) astman_send_error_va(struct mansession *s, const struct message *m, const char *fmt, ...);
00268 
00269 /*! \brief Send response in manager transaction */
00270 void astman_send_response(struct mansession *s, const struct message *m, char *resp, char *msg);
00271 
00272 /*! \brief Send ack in manager transaction */
00273 void astman_send_ack(struct mansession *s, const struct message *m, char *msg);
00274 
00275 /*! \brief Send ack in manager list transaction */
00276 void astman_send_listack(struct mansession *s, const struct message *m, char *msg, char *listflag);
00277 
00278 void __attribute__((format(printf, 2, 3))) astman_append(struct mansession *s, const char *fmt, ...);
00279 
00280 /*! \brief Determinie if a manager session ident is authenticated */
00281 int astman_is_authed(uint32_t ident);
00282 
00283 /*! \brief Called by Asterisk initialization */
00284 int init_manager(void);
00285 
00286 /*! \brief Called by Asterisk module functions and the CLI command */
00287 int reload_manager(void);
00288 
00289 /*! 
00290  * \brief Add a datastore to a session
00291  *
00292  * \retval 0 success
00293  * \retval non-zero failure
00294  * \since 1.6.1
00295  */
00296 
00297 int astman_datastore_add(struct mansession *s, struct ast_datastore *datastore);
00298 
00299 /*! 
00300  * \brief Remove a datastore from a session
00301  *
00302  * \retval 0 success
00303  * \retval non-zero failure
00304  * \since 1.6.1
00305  */
00306 int astman_datastore_remove(struct mansession *s, struct ast_datastore *datastore);
00307 
00308 /*! 
00309  * \brief Find a datastore on a session
00310  *
00311  * \retval pointer to the datastore if found
00312  * \retval NULL if not found
00313  * \since 1.6.1
00314  */
00315 struct ast_datastore *astman_datastore_find(struct mansession *s, const struct ast_datastore_info *info, const char *uid);
00316 
00317 #endif /* _ASTERISK_MANAGER_H */