Skip to content

Instantly share code, notes, and snippets.

@Croolman
Last active November 21, 2015 13:00
Show Gist options
  • Save Croolman/2e9eb1b6f3147867d4c4 to your computer and use it in GitHub Desktop.
Save Croolman/2e9eb1b6f3147867d4c4 to your computer and use it in GitHub Desktop.
#include <stdio.h> //printf
#include <string.h> //memset
#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\nVia: SIP/2.0/UDP 127.0.0.1:5060;branch=z9hG4bKnashds7\nFrom: <sip:bob@127.0.0.1>;tag=as58f4201b\nCall-ID: 843817637684230@hefrst\nTo: <sip:bob@127.0.0.1>\nCSeq: 1 REGISTER\nContact: <sip:bob@127.10.10.1>\nAllow: INVITE,ACK,OPTIONS,BYE,CANCEL,SUBSCRIBE,NOTIFY,REFER,MESSAGE,INFO,PING\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);
}
//receive a reply and print it
// GETS STUCK HERE
//clear the buffer by filling null, it might have previously received data
bzero(buf,BUFLEN);
if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen) == -1)
{
fprintf(stderr, "recvfrom() failed");
exit(1);
}
}
if (close(s) < 0)
{
fprintf(stderr, "close() failed \n");
exit(1);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment