[asterisk-users] C Code to connect to Asterisk Manager Interface

Michael Henderson mdfromusa at gmail.com
Thu Feb 28 01:14:36 CST 2008


Hi,

I have written a C code which would let me connect to the Asterisk Manager
Interface. The code compiles successfully but on running the code I get
unauthorized login shown in the Asterisk command line console.

Here is my C code:

#include<stdio.h>
#include<netdb.h>
#include<unistd.h>
#include<string.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>

#define MAX_MSG_SIZE 512
#define SERVER_ADDRESS "192.168.0.150"
#define CLIENT_ADDRESS "192.168.0.150"
#define SERVER_PORT 5038
#define    CLIENT_PORT 5100

int main()
{
    int sd;
    struct sockaddr_in serveraddr, clientaddr;
    char msg[MAX_MSG_SIZE];

    bzero((char *) &serveraddr, sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_addr.s_addr = inet_addr(SERVER_ADDRESS);
    serveraddr.sin_port = htons(SERVER_PORT);

    bzero((char *) &clientaddr, sizeof(clientaddr));
    clientaddr.sin_family = AF_INET;
    clientaddr.sin_addr.s_addr = INADDR_ANY;
    clientaddr.sin_port = htons(CLIENT_PORT);

    sd = socket(AF_INET, SOCK_STREAM, 0);
    printf("\nCreated socket ...");

    bind(sd,(struct sockaddr *) &clientaddr, sizeof(clientaddr));
    printf("\nBinding successful ...");

    connect(sd,(struct sockaddr *) &serveraddr, sizeof(serveraddr));
    printf("\nConnected ...");

    *msg=(char)"Action: Login\r\nUsername: admin\r\nSecret:
admin\r\nActionID: 1\r\n\r\n";
    send(sd,msg,strlen(msg)+1,0);
    close(sd);

    return(1);
}



Please correct me where I am going wrong. In manager.conf the username and
secret has been defined.
Thank you.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.digium.com/pipermail/asterisk-users/attachments/20080228/095e4f7a/attachment.htm 


More information about the asterisk-users mailing list