[Asterisk-Users] C Manager Interface Client

Tressler, Joshua A tressler at purdue.edu
Sat Oct 1 10:17:38 MST 2005


When we pull up a telnet session beside this code, the telnet sessions
shows everything in blocks together. We insert lines between socket
reads, therefore we see

Event: *****

Privilege: ******

Instead of 

Event: *****
Privilege: ******

Below is the code that we have. We are getting ready to run a sniffer
and see if/why asterisk is doing the writes separately instead of in one
chunk.

Joshua

=======================

char * echo = "localhost";

int port = 5038;

int readCode = 0;

struct sockaddr_in serverAddr, cliAddr;
struct hostent *server;
            
socketHandle = socket(AF_INET,SOCK_STREAM,0);
if(socketHandle < 0)

{
            printf("ERROR OPENING SOCKET\n");
            exit(0);
}


server = gethostbyname(echo);

if(server == NULL)

{
            printf("ERROR NO SUCH HOST\n");
            exit(0);
}

bzero((char*) &serverAddr, sizeof(serverAddr));

serverAddr.sin_family = AF_INET;

bcopy((char*)server->h_addr, (char*)&serverAddr.sin_addr.s_addr,
server->h_length);

serverAddr.sin_port = htons(port);
if(connect(socketHandle,(struct sockaddr
*)&serverAddr,sizeof(serverAddr)) < 0)
{
            printf("ERROR CONNECTING\n");
            exit(0);
}

readCode = read(socketHandle,buffer,sizeof(buffer));
if(readCode < 0)
{
            printf("ERROR READING FROM SOCKET\n");
            exit(0);
}

bzero(buffer,sizeof(buffer));
strcpy(buffer,"Action: Login\r\nUsername: username\r\nSecret:
secret\r\nEvents: on\r\n\r\n");
readCode = write(socketHandle,buffer,strlen(buffer));
if(readCode < 0)
{
            printf("ERROR WRITING TO SOCKET\n");
            exit(0);

}

while(1)
{
 bzero(buffer,sizeof(buffer));
 readCode = read(socketHandle,buffer,sizeof(buffer));
 if(readCode < 0)
 {
  printf("ERROR READING FROM SOCKET\n");
  exit(0);
 }
 printf("%s",buffer);
}
=======================

-----Original Message-----
From: asterisk-users-bounces at lists.digium.com
[mailto:asterisk-users-bounces at lists.digium.com] On Behalf Of Tzafrir
Cohen
Sent: Saturday, October 01, 2005 10:19 AM
To: asterisk-users at lists.digium.com
Subject: Re: [Asterisk-Users] C Manager Interface Client

On Fri, Sep 30, 2005 at 11:51:48AM -0500, Tressler, Joshua A wrote:
> List:
>  
> This is my first manager client that I've written so please bear with
me:
>  
> I am trying to write a C manager interface client to interface with
our CRM software. I am having an issue while reading the data from the
manager interface.
>  
> I am writing this in C and I have the following code:
> while(1)
> {
>  bzero(buffer,sizeof(buffer));
>  readCode = read(socketHandle,buffer,sizeof(buffer));
>  if(readCode < 0)
>  {
>   printf("ERROR READING FROM SOCKET\n");
>   exit(0);
>  }
>  printf("%s",buffer);
> }

This is just the main loop, right? There has to be a login before that.
Could you please post your full code? (that is: a minimal version of it
that you verified to still be problematic).

> 
> This prints out everything just as connecting to the telnet session
would print it out (I do the logging in elsewhere, that isn't the
problem here)
> 
> This code will read until * has nothing else for me to read from it 
> then print it all out and wait for some more stuff.  Since * seems to 
> print out on 5038 in "blocks" of text read(...) will never cut off in 
> the middle of a block.

Using a C-based program to debug that is not very helpful. telnet
localhost 5038 and see what happens in real-time.

A sniffer could also help.

> However, on one instance, and this is the only one we can reproduce
the 
> results on, * puts out Event: ****** then stops, Privilege: ********, 
> then stops and then prints out the rest.  This really screws up my 
> parsing as i normally parse using a tokenizer on \r\n\r\n and pass
each 
> block off to a parsing method.  I found this problem using the
following code:
> while(1)
> {
>  bzero(buffer,sizeof(buffer));
>  readCode = read(socketHandle,buffer,sizeof(buffer));
>  if(readCode < 0)
>  {
>   printf("ERROR READING FROM SOCKET\n");
>   exit(0);
>  }
>  printf("%s\n",buffer);  ////////This is the main difference \n
> }
> In this case I get output as follows:
> ...
> ...
> .
> .
> Event: Hangup
>  
> Privilege: call,all
>  
> Channel: SIP/1542200-543f
> Uniqueid: 1128041150.26
> Cause: 0
> Cause-txt: Unknown
> ...
> ...
> .
> .
> In this case "Event: Hangup", "Privilege: call, all", and the rest all
get passed off to my parser.  Obviously a problem.
> 
> Is * spitting this data out to me in three seperate chunks or is my
socket not blocking correctly?
> 
> Any suggestions as to why this would happen?
>  
> TIA,
>  
> Joshua

> _______________________________________________
> --Bandwidth and Colocation sponsored by Easynews.com --
> 
> Asterisk-Users mailing list
> Asterisk-Users at lists.digium.com
> http://lists.digium.com/mailman/listinfo/asterisk-users
> To UNSUBSCRIBE or update options visit:
>    http://lists.digium.com/mailman/listinfo/asterisk-users

-- 
Tzafrir Cohen         | tzafrir at jbr.cohens.org.il | VIM is
http://tzafrir.org.il |                           | a Mutt's  
tzafrir at cohens.org.il |                           |  best
ICQ# 16849755         |                           | friend
_______________________________________________
--Bandwidth and Colocation sponsored by Easynews.com --

Asterisk-Users mailing list
Asterisk-Users at lists.digium.com
http://lists.digium.com/mailman/listinfo/asterisk-users
To UNSUBSCRIBE or update options visit:
   http://lists.digium.com/mailman/listinfo/asterisk-users



More information about the asterisk-users mailing list