-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
261 lines (203 loc) · 5.2 KB
/
client.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <ctype.h>
#include <unistd.h>
#define BUFFERLENGTH 255
char *serverHost;
int serverPort;
char *operation;
int sockfd, n;
int addRule(char *ipAddress, char* port, char buffer[])
{
bzero (buffer, BUFFERLENGTH);
char message[256] = "0";
strcat(message, ipAddress);
strcat(message, " ");
strcat(message, port);
/* send message */
n = write (sockfd, message, strlen(message));
if (n < 0)
{
perror ("ERROR writing to socket");
exit(-1);
}
bzero (buffer, BUFFERLENGTH);
/* wait for reply */
n = read (sockfd, buffer, BUFFERLENGTH -1);
if (n < 0)
{
perror ("ERROR reading from socket");
exit(-1);
}
printf ("%s\n",buffer);
close(sockfd);
return 0;
}
int checkRule(char *ipAddress, char *port, char buffer[])
{
bzero(buffer, BUFFERLENGTH);
char message[256] = "1";
strcat(message, ipAddress);
strcat(message, " ");
strcat(message, port);
/* send message */
n = write (sockfd, message, strlen(message));
if (n < 0)
{
perror ("ERROR writing to socket");
exit(-1);
}
bzero (buffer, BUFFERLENGTH);
/* wait for reply */
n = read (sockfd, buffer, BUFFERLENGTH -1);
if (n < 0)
{
perror ("ERROR reading from socket");
exit(-1);
}
printf("%s\n",buffer);
close(sockfd);
return 0;
}
int deleteRule(char *ipAddress, char *port, char buffer[])
{
bzero (buffer, BUFFERLENGTH);
char message[256] = "2";
strcat(message, ipAddress);
strcat(message, " ");
strcat(message, port);
/* send message */
n = write (sockfd, message, strlen(message));
if (n < 0)
{
perror ("ERROR writing to socket");
exit(-1);
}
bzero (buffer, BUFFERLENGTH);
/* wait for reply */
n = read (sockfd, buffer, BUFFERLENGTH -1);
if (n < 0)
{
perror ("ERROR reading from socket");
exit(-1);
}
printf ("%s\n",buffer);
close(sockfd);
return 0;
}
int showRules(char buffer[])
{
bzero(buffer, BUFFERLENGTH);
n = write(sockfd, "3", 2);
if (n < 0)
{
perror ("ERROR writing to socket");
exit(-1);
}
bzero (buffer, BUFFERLENGTH);
/* wait for reply */
n = read (sockfd, buffer, BUFFERLENGTH -1);
if (n < 0)
{
perror ("ERROR reading from socket");
exit(-1);
}
printf ("%s\n",buffer);
close(sockfd);
return 0;
}
int opToInt(char *operation)
{
if(strcmp(operation, "A") == 0 || strcmp(operation, "a") == 0)
{
return 1;
}
if(strcmp(operation, "C") == 0 || strcmp(operation, "c") == 0)
{
return 2;
}
if(strcmp(operation, "D") == 0 || strcmp(operation, "d") == 0)
{
return 3;
}
if(strcmp(operation, "L") == 0 || strcmp(operation, "l") == 0)
{
return 4;
}
return -1;
}
int main (int argc, char **argv) {
serverHost = argv[1];
serverPort = atoi(argv[2]);
operation = argv[3];
struct addrinfo hints;
struct addrinfo *result, *rp;
int res;
char buffer[BUFFERLENGTH];
if (argc < 3)
{
fprintf (stderr, "usage %s hostname port\n", argv[0]);
exit(1);
}
int intOperation = opToInt(operation);
if(intOperation == -1)
{
printf("Invalid request");
return -1;
}
/* Obtain address(es) matching host/port */
/* code taken from the manual page for getaddrinfo */
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_STREAM; /* Datagram socket */
hints.ai_flags = 0;
hints.ai_protocol = 0; /* Any protocol */
res = getaddrinfo(argv[1], argv[2], &hints, &result);
if (res != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(res));
exit(EXIT_FAILURE);
}
/* getaddrinfo() returns a list of address structures.
Try each address until we successfully connect(2).
If socket(2) (or connect(2)) fails, we (close the socket
and) try the next address. */
for (rp = result; rp != NULL; rp = rp->ai_next)
{
sockfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (sockfd == -1)
continue;
if (connect(sockfd, rp->ai_addr, rp->ai_addrlen) != -1)
break; /* Success */
close(sockfd);
}
if (rp == NULL)
{ /* No address succeeded */
fprintf(stderr, "Could not connect\n");
exit(EXIT_FAILURE);
}
freeaddrinfo(result); /* No longer needed */
switch(intOperation)
{
case 1:
addRule(argv[4], argv[5], buffer);
break;
case 2:
checkRule(argv[4], argv[5], buffer);
break;
case 3:
deleteRule(argv[4], argv[5], buffer);
break;
case 4:
showRules(buffer);
break;
case -1:
printf("Invalid operation");
return -1;
}
return 0;
}