-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
376 lines (264 loc) · 10.3 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <strings.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <ctype.h>
#define MAXLINE 4096 /* max text line length */
#define DAYTIME_PORT 3333
#define BUFFER_SIZE 1024
int main(int argc, char **argv)
{
int sockfd;
int n;
char recvline[3][MAXLINE + 1];
char serverNameAddr[BUFFER_SIZE];
char tunnelNameAddr[BUFFER_SIZE];
char ipaddr[100];
char* server_ip;
char* server_name;
char* tunnel_ip;
char* tunnel_name;
char* testval;
struct sockaddr_in servaddr, tunneladdr;
// perform argument check
if (argc != 3 && argc != 5)
{
printf("usage: client <serverName> <server port number> or\n \
client <tunnelName or tunnelIP> <tunnel port Number> <serverName or serverIP> <server port number>");
exit(1);
}
// if everything is ok, do the following
// check if port number is in non-reserved range
int portnum;
portnum = atoi(argv[2]);
if ( !(portnum > 1025 && portnum < 65535) )
{
printf("port number in argv[2] must be between 1025 and 65535\n");
}
if (argc == 3) // if no tunnel arguments
{
// create socket
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("socket error\n");
exit(1);
}
// Specify server address and port number.
// Reverse lookup server ip address
// and convert IPv4 dotted decimal address to binary, then
// store in servaddr.sin_addr pointer
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(atoi(argv[2])); // convert port number to big endian order
char* testval = argv[1];
// check if address is ipv4, i.e. if first character is a number
// see RFC952 for more details
if ( isdigit(testval[0]) )
{
// translate ip address from decimal to binary
if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
{
printf("inet_pton error for %s\n", argv[1]);
exit(1);
}
// converts ip to machine name
if ( getnameinfo((struct sockaddr *) &servaddr, sizeof(servaddr),
serverNameAddr, sizeof(serverNameAddr), NULL, 0, 0) != 0 )
{
printf("reverse lookup failed\n");
exit(1);
}
// assign name and ip address of server to pointers
server_name = serverNameAddr;
server_ip = argv[1];
// establish connection
if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0)
{
printf("connect error\n");
exit(1);
}
}
// if first character is an alphabet
else if ( isalpha(testval[0]) )
{
// convert machine name to ip address
struct addrinfo hints;
struct addrinfo *result, *node;
bzero(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = 0;
if ( getaddrinfo(argv[1], argv[2], &hints, &result) != 0 )
{
printf("reverse lookup failed\n");
exit(1);
}
// search link list for matching node
for (node = result; node != NULL; node = node->ai_next)
{
if ( (sockfd = socket(node->ai_family, node->ai_socktype,
node->ai_protocol) ) < 0)
{
continue;
}
if (connect(sockfd, node->ai_addr, node->ai_addrlen) == 0)
{
break; // connection is successful
}
close(sockfd); // close socket and search again
}
// convert ip address from binary to decimal,
inet_ntop(AF_INET, &(node->ai_addr), ipaddr, INET_ADDRSTRLEN );
// assign name and ip address of server to pointers
server_name = argv[1];
server_ip = ipaddr;
freeaddrinfo(result);
}
// print server name, ip and time
printf("Server Name: %s\n", server_name);
printf("Server IP Address: %s\n", server_ip);
printf("Time: ");
// receive time stamp
bzero(&recvline, sizeof(recvline));
while ( (n = read(sockfd, recvline[0], MAXLINE)) > 0)
{
recvline[0][n] = 0; /* null terminate */
if (fputs(recvline[0], stdout) == EOF)
{
printf("fputs error\n");
exit(1);
}
}
if (n < 0)
{
printf("read error\n");
exit(1);
}
}
else // if tunnel arguments exist, i.e. argvc == 5
{
// check if server port number is in non-reserved range
portnum = atoi(argv[4]);
if ( !(portnum > 1025 && portnum < 65535) )
{
printf("port number in argv[4] must be between 1025 and 65535\n");
}
// create socket
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("socket error\n");
exit(1);
}
// Specify tunnel address and port number.
// Reverse lookup tunnel ip address
// and convert IPv4 dotted decimal address to binary, then
// store in tunneladdr.sin_addr pointer
bzero(&tunneladdr, sizeof(tunneladdr));
tunneladdr.sin_family = AF_INET;
tunneladdr.sin_port = htons(atoi(argv[2])); // convert port number to big endian order
testval = argv[1];
// if first character is a number; see RFC952 for more details
if ( isdigit(testval[0]) )
{
// translate ip address from decimal to binary
if (inet_pton(AF_INET, argv[1], &tunneladdr.sin_addr) <= 0)
{
printf("inet_pton error for %s\n", argv[1]);
exit(1);
}
// converts ip to machine name
if ( getnameinfo((struct sockaddr *) &tunneladdr, sizeof(tunneladdr),
tunnelNameAddr, sizeof(tunnelNameAddr), NULL, 0, 0) != 0 )
{
printf("reverse lookup failed\n");
exit(1);
}
// assign name and ip address of tunnel to pointers
tunnel_name = tunnelNameAddr;
tunnel_ip = argv[1];
// establish connection
if (connect(sockfd, (struct sockaddr *) &tunneladdr, sizeof(tunneladdr)) < 0)
{
printf("connect error\n");
exit(1);
}
}
else if ( isalpha(testval[0]) ) // if first character is an alphabet
{
// convert machine name to ip address
struct addrinfo hints;
struct addrinfo *result, *node;
bzero(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = 0;
if ( getaddrinfo(argv[1], argv[2], &hints, &result) != 0 )
{
printf("reverse lookup failed\n");
exit(1);
}
// search link list for matching node
for (node = result; node != NULL; node = node->ai_next)
{
if ( (sockfd = socket(node->ai_family, node->ai_socktype,
node->ai_protocol) ) < 0)
{
continue;
}
if (connect(sockfd, node->ai_addr, node->ai_addrlen) == 0)
{
break; // connection is successful
}
close(sockfd); // close socket and search again
}
// convert ip address from binary to decimal,
inet_ntop(AF_INET, &(node->ai_addr), ipaddr, INET_ADDRSTRLEN );
// assign name and ip address of tunnel to pointers
tunnel_name = argv[1];
tunnel_ip = ipaddr;
freeaddrinfo(result);
}
// send server name/ip and port to tunnel
int i;
for (i = 3; i < 5; i++)
{
if ( (n = write(sockfd, argv[i], strlen(argv[i]) )) < 0 )
{
printf("write error\n");
exit(1);
}
/* printf("send server details to tunnel: %s\n", argv[i]);*/
sleep(1); // delay for 1 second
}
// receive results from tunnel
bzero(&recvline, sizeof(recvline));
// print server name, ip and time
for (i = 0; i < 3; i++)
{
if ( (n = read(sockfd, recvline[i], sizeof(recvline[i]))) > 0)
{
recvline[i][n] = 0; /* null terminate */
}
else if ( (n = read(sockfd, recvline[i], sizeof(recvline[i]))) < 0)
{
printf("read error\n");
exit(1);
}
}
// print server details
printf("Server Name: %s\n", recvline[0]);
printf("IP Address: %s\n", recvline[1]);
printf("Time: %s\n", recvline[2]);
// print tunnel details
printf("Via Tunnel: %s\n", tunnel_name);
printf("IP Address: %s\n", tunnel_ip);
printf("Port Number: %s\n", argv[2]);
}
exit(0);
}