· KLDP.org · KLDP.net · KLDP Wiki · KLDP BBS ·
Asterisk Source/Manager Interface

AsteriskSource/ManagerInterface


static struct action {
        char *action;
        int authority;
        int (*func)(struct mansession *s, struct message *m);
} actions[] = {
        { "Ping", 0, action_ping },
        { "Logoff", 0, action_logoff },
        { "Hangup", EVENT_FLAG_CALL, action_hangup },
        { "Status", EVENT_FLAG_CALL, action_status },
        { "Redirect", EVENT_FLAG_CALL, action_redirect },
        { "Originate", EVENT_FLAG_CALL, action_originate },
};

* int init_manager(void)
  • cfg = ast_load("manager.conf");
  • val = ast_variable_retrieve(cfg, "general", "enabled");
  • val = ast_variable_retrieve(cfg, "general", "portno")
  • ba.sin_family = AF_INET;
  • ba.sin_port = htons(portno);
  • memset(&ba.sin_addr, 0, sizeof(ba.sin_addr));
  • val = ast_variable_retrieve(cfg, "general", "bindaddr")
  • if (asock < 0) {
    • asock = socket(AF_INET, SOCK_STREAM, 0);
    • setsockopt(asock, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
    • bind(asock, &ba, sizeof(ba)
    • listen(asock, 2)
    • pthread_create(&t, NULL, accept_thread, NULL);
  • }
  • ast_destroy(cfg);
  • return 0;

* static void *accept_thread(void *ignore)
  • for (;;) {
    • as = accept(asock, &sin, &sinlen);
    • s = malloc(sizeof(struct mansession));
    • s->fd = as;
    • pthread_create(&t, NULL, session_do, s)

* static void *session_do(void *data)
  • ast_cli(s->fd, "Asterisk Call Manager/1.0\r\n");
  • for (;;) {
    • res = get_input(s, m.headersm.hdrcount);
    • process_message(s, &m)
  • }
  • if (s->authenticated) {
  • } else {
  • }
  • destroy_session(s);
  • return NULL;

* static int process_message(struct mansession *s, struct message *m)
  • strncpy(action, get_header(m, "Action"), sizeof(action));
  • if (!s->authenticated) {
    • if (!strcasecmp(action, "Login")) {
      • if (authenticate(s, m)) {
        • send_error(s, "Authentication failed");
        • return -1;
      • } else {
        • s->authenticated = 1;
        • send_ack(s, "Authentication accepted");
      • }
    • } else
      • send_error(s, "Authentication Required");
  • } else {
    • for (x=0;x0);x++) {
      • if ((s->writeperm & actionsx.authority) == actionsx.authority) {
        • if (actionsx.func(s, m))
          • return -1;
      • } else {
        • send_error(s, "Permission denied");
      • }
      • break;
    • }
    • if (x >= sizeof(actions) / sizeof(actions0))
      • send_error(s, "Invalid/unknown command");
  • }
  • return 0;

ID
Password
Join
Far duller than a serpent's tooth it is to spend a quiet youth.


sponsored by andamiro
sponsored by cdnetworks
sponsored by HP

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2009-06-29 08:45:04
Processing time 0.0051 sec