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

Michael Henderson mdfromusa at gmail.com
Sat Mar 1 00:39:40 CST 2008


The code below complies correctly. Now I open 2 terminals ... one for
executing the code using gcc and the other with asterisk -r command.
When i execute the code, in the asterisk command link it shows Connect
attempt from '192.168.0.150' unable to authenticate. I have defined the
username as admin and the secret as admin in manager.conf. Whereas if I use
telnet 192.168.0.150 5038 I can connect correctly and in the asterisk
commandline it shows Manager 'admin' logged on from 192.168.0.150.

Where am I going wrong in my code. Please let me know.

#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);
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.digium.com/pipermail/asterisk-dev/attachments/20080301/d951d319/attachment.htm 


More information about the asterisk-dev mailing list