#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#define MAXDATASIZE 1000 // max number of bytes we can get at once
#define MAXBUF 1000
#define MAX_VEC 5
#define PORT "3492" // the port users will be connecting to
#define PORT_HLR "3493" // Port of HLR Server
#define HLR_ADDR "127.0.0.1" // IP Address of HLR
#define LOCAL_ID 1234
#define BACKLOG 10 // how many pending connections queue will hold
struct authentication_vector
{
int mac;
int ak;
int random_no;
int xres;
int ck;
int ik;
char autn[100];
};
typedef struct authentication_vector av;
//*********************
// VLR Table and identities
int vlr_table[MAXBUF][2]; //IMUI and TMUI of MS
int lai=LOCAL_ID;
av au_vector[MAX_VEC];
int current=-1;
int current_ms=0;
//**********************
void sigchld_handler(int s)
{
while(waitpid(-1, NULL, WNOHANG) > 0);
}
// get sockaddr, IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET)
{
return &(((struct sockaddr_in*)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
int main(void)
{
int sockfd, new_fd; // listen on sock_fd, new connection on new_fd
char buf[MAXDATASIZE];
struct addrinfo hints, *servinfo, *p;
struct sockaddr_storage their_addr; // connector's address information
socklen_t sin_size;
struct sigaction sa;
int yes=1;
char s[INET6_ADDRSTRLEN];
int rv;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE; // use my IP
if ((rv = getaddrinfo(NULL, PORT, &hints, &servinfo)) != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
// loop through all the results and bind to the first we can
for(p = servinfo; p != NULL; p = p->ai_next)
{
if ((sockfd = socket(p->ai_family, p->ai_socktype,p->ai_protocol)) == -1)
{
perror("server: socket");
continue;
}
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes,sizeof(int)) == -1)
{
perror("setsockopt");
exit(1);
}
if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1)
{
close(sockfd);
perror("server: bind");
continue;
}
break;
}
if (p == NULL)
{
fprintf(stderr, "server: failed to bind\n");
return 2;
}
freeaddrinfo(servinfo); // all done with this structure
if (listen(sockfd, BACKLOG) == -1)
{
perror("listen");
exit(1);
}
sa.sa_handler = sigchld_handler; // reap all dead processes
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGCHLD, &sa, NULL) == -1)
{
perror("sigaction");
exit(1);
}
printf("server: waiting for connections...\n");
int numbytes;
while(1)
{ // main accept() loop
sin_size = sizeof their_addr;
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
if (new_fd == -1)
{
perror("accept");
continue;
}
inet_ntop(their_addr.ss_family,get_in_addr((struct sockaddr *)&their_addr),s, sizeof s);
printf("\nserver: got connection from %s\n", s);
if (!fork())
{ // this is the child process
close(sockfd); // child doesn't need the listener
/*---Recieve IMUI and MODE from MS---*/
if ((numbytes = recv(new_fd, buf, MAXDATASIZE-1, 0)) == -1)
{
perror("recv");
exit(1);
}
buf[numbytes] = '\0';
printf("packet Recieved: %s\n",buf);
/*----Extract IMUI and Mode from Packet---------*/
int i=0;
int flag=0;
char vlr_tmode[5];
char vlr_timui[10];
int k=0;
while(buf[i]!='\0')
{
if(buf[i]=='#')
{ flag++; k=0; i++; continue;}
else if(flag==0)
{ vlr_timui[k]=buf[i]; k++;}
else if(flag==1)
{ vlr_tmode[k]=buf[i]; k++;}
else break;
i++;
}
int vlr_mode=atoi(vlr_tmode);
int vlr_imui=atoi(vlr_timui);
printf("\n IMUI: %d",vlr_imui);
printf("\n MODE: %d",vlr_mode);
/************************ Send MS the Latest Unused Authentication Vector*********/
if(current<MAX_VEC && current!=-1)
{
char trand[10];
bzero(buf,MAXDATASIZE);
bzero(trand,10);
sprintf(trand,"%d",au_vector[current].random_no);
strcat(buf,trand);
strcat(buf,"*");
strcat(buf,au_vector[current].autn);
if (send(new_fd, buf, sizeof(buf), 0) == -1)
perror("send");
}
else
{
/************************Connect to HLR for New Authentication Token *****************/
int sockfd, numbytes;
struct addrinfo hints, *servinfo, *p;
int rv;
char s[INET6_ADDRSTRLEN];
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if ((rv = getaddrinfo(HLR_ADDR, PORT_HLR, &hints, &servinfo)) != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
// loop through all the results and connect to the first we can
for(p = servinfo; p != NULL; p = p->ai_next)
{
if ((sockfd = socket(p->ai_family, p->ai_socktype,p->ai_protocol)) == -1)
{
perror("\nclient HLR: socket");
continue;
}
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1)
{
close(sockfd);
perror("\nclient HLR: connect");
continue;
}
break;
}
if (p == NULL)
{
fprintf(stderr, "client HLR: failed to connect\n");
return 2;
}
inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr),s, sizeof s);
printf("\nclient HLR: connecting to %s\n", s);
freeaddrinfo(servinfo); // all done with this structure
/******************* Send Request to HLR *****************/
send(sockfd, buf, sizeof(buf), 0);
printf("\n\n Request Sent to HLR");
//memset(buf,0,sizeof(buf));
//recv(sockfd,buf,sizeof(buf),0);
/***************** Recieve Authentication Vectors *********/
memset(buf,0,sizeof(buf));
if ((numbytes = recv(sockfd, buf,MAXDATASIZE, 0)) == -1)
{
perror("recv");
exit(1);
}
buf[numbytes] = '\0';
printf("\nAuthentication Vectors received '%s'\n",buf);
close(sockfd);
/************** Store New authentication Vectors ***********/
int i=0,flag=0,k=0,x=0;
char ch,tmac[10],tak[10],trand[10],txres[10],tck[10],tik[10],tautn[10];
while((ch=buf[x++])!='\0')
{
if(ch=='*'){ flag++; if(flag!=7)i=0;}
else if(flag==0){ tmac[i]=ch; i++;}
else if(flag==1){ tak[i]=ch; i++;}
else if(flag==2){ trand[i]=ch; i++;}
else if(flag==3){ txres[i]=ch; i++;}
else if(flag==4){ tck[i]=ch; i++;}
else if(flag==5){ tik[i]=ch; i++;}
else if(flag==6){ au_vector[k].autn[i]=ch; i++;}
else if(flag==7)
{
au_vector[k].mac=atoi(tmac);
au_vector[k].ak=atoi(tak);
au_vector[k].random_no=atoi(trand);
au_vector[k].xres=atoi(txres);
au_vector[k].ck=atoi(tck);
au_vector[k].ik=atoi(tik);
bzero(tmac,10);
bzero(tak,10);
bzero(trand,10);
bzero(txres,10);
bzero(tck,10);
bzero(tik,10);
k++;
i=0;
tmac[i]=ch; i++;
flag=0;
}
}
au_vector[k].mac=atoi(tmac);
au_vector[k].ak=atoi(tak);
au_vector[k].random_no=atoi(trand);
au_vector[k].xres=atoi(txres);
au_vector[k].ck=atoi(tck);
au_vector[k].ik=atoi(tik);
printf("\n\nNo of elements in the vector : %d",MAX_VEC);
int choice;
printf("View Authentication Vectors :(Yes=1/No=0)");
scanf("%d",&choice);
if(choice)
{
int i=0;
while(i<MAX_VEC)
{
printf(" \nMAC %d : %d",i+1,au_vector[i].mac);
printf(" \nAnonymity Key %d : %d",i+1,au_vector[i].ak);
printf(" \nRAND %d : %d",i+1,au_vector[i].random_no);
printf(" \nXRES %d : %d",i+1,au_vector[i].xres);
printf(" \nCipher Key %d : %d",i+1,au_vector[i].ck);
printf(" \nIntegrity Key %d : %d",i+1,au_vector[i].ik);
printf(" \nAuthentication Token %d : %s\n\n",i+1,au_vector[i].autn);
i++;
}
}
current=0;
/************* Send latest Vector **********/
bzero(buf,MAXDATASIZE);
bzero(trand,10);
sprintf(trand,"%d",au_vector[current].random_no);
strcat(buf,trand);
strcat(buf,"#");
strcat(buf,au_vector[current].autn);
if (send(new_fd, buf, sizeof(buf), 0) == -1)
perror("send");
}
/************* Recieve RES From MS ***************/
numbytes=0;
bzero(buf,MAXDATASIZE);
if ((numbytes = recv(new_fd, buf, MAXDATASIZE-1, 0)) == -1)
{
perror("recv");
exit(1);
}
printf("\n\n Packet recieved :\n\n %s",buf);
int vlr_xres=atoi(buf);
if(vlr_xres==au_vector[current].xres)
{ printf("\n\nMOBLE STATION VERIFIED SUCCESSFULLY.....!!!!");}
current++;
close(new_fd);
exit(0);
}
close(new_fd); // parent doesn't need this
}
return 0;
}
No comments:
Post a Comment
Have some problem with this code? or Request the code you want if you cant find it in Blog Archive.