-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnsSer.c
170 lines (160 loc) · 4.34 KB
/
dnsSer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<string.h>
#include<stdio.h>
#include<signal.h>
#include<stdlib.h>
#include<unistd.h>
#include<ctype.h>
#include <math.h>
#define MAXLINE 100
typedef struct _data
{
char address[MAXLINE];
char name[MAXLINE];
}database;
void signalHandler(int sig); //programmer-defined signal handler for Ctrl+C command
void databaseInit(char *); //creates database
int findUPC(int); //searches and returns item_code supplied in the database and returns accordingly
void childprocess(int,int);
database *domain; //Item-Name-Price list.
int records; //No. of Items in the database.
int listensd,connSD; //Socket descriptors.
void databaseInit(char *file_name)
{
FILE *fp;
char line_buff[MAXLINE];
int i=0;
fp=fopen(file_name,"r");
if(fp==NULL)
{
// printf("\nDatabase Configuration Error.. Terminating Server.%s",file_name);
signalHandler(0);
exit(0);
}
fgets(line_buff,MAXLINE,fp); //reads the first line that has the number of entries in the text file
records=atoi(line_buff); //gets number of records
domain=(database *)malloc(records*sizeof(database)); //allocates memory for database
for(i=0;i<records;i++)
{
fscanf(fp,"%s",domain[i].name);
fscanf(fp,"%s",domain[i].address);
}
// printf("Database Configuration..... Complete\n" );
return ;
}
int dnsSearch(char** a)
{
int cnt;
for(cnt=0;cnt<records;cnt++) {if(strcmp(domain[cnt].name,*a)==0) { *a=domain[cnt].address;return 1;}}
return 0;
}
int ipSearch(char **a)
{
int cnt;
for(cnt=0;cnt<records;cnt++) {if(strcmp(domain[cnt].address,*a)==0) { *a=domain[cnt].name;return 1;}}
return 0;
}
void signalHandler(int sig)
{
char msg[MAXLINE];
close(listensd);
// printf("Server terminating!..\n");
sprintf(msg,"----||||----");
send(connSD,msg,MAXLINE,0);
close(connSD);
exit(0);
}
void childprocess(int connSD,int id)
{
int len,flag;
double total=0.0;
char buffer[MAXLINE],msg[MAXLINE],*name,*token;
const char delim[2]="$";
len=0;
memset(msg,0,MAXLINE); //clears contents of msg
len=recv(connSD,buffer,MAXLINE,0);
if(len<0)
{
sprintf(msg,"Communication Error. Terminating Connection. Please reconnect.\n");
send(connSD,msg,MAXLINE,0);
close(connSD);
exit(0);
}
if(strcmp(buffer,"----||||----")==0)
{
// printf("\nTerminating Child No: %d 'cause corresponding client terminated\n", getpid());
close(connSD);
exit(0);
}
token=strtok(buffer,delim);
token=strtok(NULL,delim);
if(buffer[0]-'0'==1) flag=dnsSearch(&token);
if(buffer[0]-'0'==2) flag=ipSearch(&token);
if(flag==0)
{
sprintf(msg,"4$Entry not found in the database");
send(connSD,msg,MAXLINE,0);
signalHandler(1);
}
else if(flag==1)
{
sprintf(msg,"3$%s",token);
send(connSD,msg,MAXLINE,0);
signalHandler(1);
}
}
int main(int argc, char *argv[])
{
if(argc<2)
{
printf("\nToo few arguments to server!..exiting");
exit(0);
}
int clilen; //clilen is the length of the client socket, used as a value-result argument
struct sockaddr_in ServAddr, CliAddr; //sockaddr structure for sockets; one for server and the other for client
listensd=socket(AF_INET,SOCK_STREAM,0); //Creating a TCP Socket
if(listensd<0)
{
perror("Cannot create socket!");
return 0;
}
signal(SIGINT,signalHandler);
//Setting up the Server socket
ServAddr.sin_family=AF_INET;
ServAddr.sin_addr.s_addr = INADDR_ANY; //Connection from any IP.
ServAddr.sin_port = htons(atoi(argv[1])); //self defined server port
//Binding socket
if(bind(listensd,(struct sockaddr *) &ServAddr, sizeof(ServAddr))<0)
{
perror("Cannot bind port!");
return 0;
}
if (listen(listensd,SOMAXCONN) <0 )
{
perror("Unable to set the socket to listen");
return 0;
}
//Initializing Database
databaseInit("database.txt");
while(1!=0)
{
clilen=sizeof(CliAddr);
if((connSD=accept(listensd,(struct sockaddr *)&CliAddr,&clilen))<0)
{
perror("Cannot establish connection!");
return 0;
}
if((fork())==0)
{
close(listensd); //Allowing only one socket to listen by closing child process' listensd.
// printf("\nRequest from %s servcied with child process %d\n",inet_ntoa(CliAddr.sin_addr) ,getpid());
childprocess(connSD,getpid()); //A Child Process for a connection.
close(connSD); //Child closes its version of connSD after transaction is completed.
exit(0); //Child terminates.
}
close(connSD); //Parent looks for more connections by closing the connSD.
}
}