I am trying to make simple SIP client (UDP, BSD sockets), but I am stucked with REGISTER message. I manage to send the REGISTER message to asterisk, he will then respond, but client doesn't catch the response so he waits forever.
#include <stdio.h> //printf
#include <string.h> //memset
#include <stdlib.h> //exit(0);
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <ctype.h>
#include <netinet/in.h>
#include <unistd.h>
#include <netdb.h>
#define SERVER "127.0.0.1"
#define BUFLEN 1024 //Max length of buffer
#define PORT 5060 //The port on which to send data
int main(int argc, char **argv)
{
struct sockaddr_in si_other;
int s,slen=sizeof(si_other);
char buf[BUFLEN];
char message[]= "REGISTER sip:127.0.0.1 SIP/2.0\r\nVia: SIP/2.0/UDP 127.0.0.1:5060;branch=z9hG4bKnashds7\r\nFrom: <sip:[email protected]>;tag=as58f4201b\r\nCall-ID: 843817637684230@hefrst\r\nTo: <sip:[email protected]>\r\nCSeq: 1 REGISTER\r\nContact: <sip:[email protected]>\r\nAllow: INVITE,ACK,OPTIONS,BYE,CANCEL,SUBSCRIBE,NOTIFY,REFER,MESSAGE,INFO,PING\r\nExpires: 3600\r\n\r\n";
if ( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
fprintf(stderr, "socket failed");
exit(1);
}
memset((char *) &si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(PORT);
if (inet_aton(SERVER , &si_other.sin_addr) == 0)
{
fprintf(stderr, "inet_aton() failed\n");
exit(1);
}
while(1){
if (sendto(s, message, strlen(message) , 0 , (struct sockaddr *) &si_other, slen)==-1)
{
fprintf(stderr, "sendto failed");
exit(1);
}
memset(buf,'\0', BUFLEN);
if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen) == -1)
{
fprintf(stderr, "recvfrom() failed");
exit(1);
}
}
}
This is just the part of code that seems to be failing to receive the data.
The problem was in the message header. The part Via has to end with ;rport
, so asterisk does actually report.