-
Notifications
You must be signed in to change notification settings - Fork 1
/
awget.c.save.c
366 lines (308 loc) · 9.31 KB
/
awget.c.save.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
/*
CS457DL Project 2
Steve Watts
*/
/* This is the awget program portion of the CS457DL Lab/Programming Assignnment 1 */
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <limits.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>
#include <time.h>
#include <netdb.h>
#include "awget.h"
#define MAXPORTNUMBER 65535
char *serverName = NULL; /* -s servername */
void chainDataAndURLToString(struct chainData *cd, char *urlValue, char **cdString) {
int i;
int thisSize = 0;
char thisLine[255];
thisSize = 6*sizeof(char) + strlen(urlValue)*sizeof(char);
*cdString = malloc(thisSize);
strcpy(*cdString,urlValue);
strcat(*cdString,"\n");
sprintf(thisLine,"%d\n",cd->numLinks);
strcat(*cdString,thisLine);
for (i = 0; i < cd->numLinks; i++) {
thisSize += 7*sizeof(char) + strlen(cd->links[i].SSaddr) * sizeof(char);
*cdString = realloc(*cdString,thisSize);
sprintf(thisLine,"%s,%d\n",cd->links[i].SSaddr,cd->links[i].SSport);
strcat(*cdString,thisLine);
}
}
void removeLinkFromChainData(struct chainData *cd, int linkNum) {
int i;
for (i = 0; i < cd->numLinks; i++) {
if (i >= linkNum) {
strcpy(cd->links[i].SSaddr,cd->links[i+1].SSaddr);
cd->links[i].SSport = cd->links[i+1].SSport;
}
}
cd->numLinks = cd->numLinks - 1;
}
void getRandomSS (struct chainData *cd, char **SSaddr, int *SSport, int *linkNumChosen) {
int i = 0;
i = rand() % cd->numLinks;
*SSaddr = cd->links[i].SSaddr;
*SSport = cd->links[i].SSport;
*linkNumChosen = i;
}
void packChainData (struct chainData *cd_in, struct chainData *cd_out) {
int i;
cd_out->numLinks = cd_in->numLinks;
for (i = 0; i < cd_in->numLinks; i++) {
strcpy(cd_out->links[i].SSaddr,cd_in->links[i].SSaddr);
cd_out->links[i].SSport = htonl(cd_in->links[i].SSport);
}
}
int
printUsage ()
{
fprintf (stdout, "Usage: awget <URL> [-c chainfile]\n");
return 0;
}
int readChainFile(FILE *fd, struct chainData *cd) {
char buf[255];
char *token = NULL;
int i;
int iRead = 0;
/* read and verify the integer on line 1 of the file */
if (fgets(buf,255,fd) == NULL) {
return 1;
} else {
if ((iRead = atoi(buf)) == 0) {
return 1;
}
}
cd->numLinks = iRead;
for (i = 0; i < cd->numLinks; i++) {
if (fgets(buf,255,fd) == NULL) {
return 1;
} else {
token = strtok(buf,",");
strcpy(cd->links[i].SSaddr,token);
token = strtok(NULL,",");
cd->links[i].SSport = atoi(token);
}
}
return 0;
}
int sanityCheckFile (char *fname) {
char buf[255];
char SSaddr[255];
char SSport[255];
char *token = NULL;
int i,j;
char *comma = NULL;
int commaPosition = 0;
int sLen;
int iRead = 0;
int jRead = 0;
FILE *fd = fopen(fname,"r");
/* read and verify the integer on line 1 of the file */
if (fgets(buf,255,fd) == NULL) {
return 1;
} else {
if (strcmp("0",buf) != 0) {
if ((iRead = atoi(buf)) == 0) {
return 1;
}
}
}
for (i = 0; i < iRead; i++) {
if (fgets(buf,255,fd) == NULL) {
return(2+i);
} else {
sLen = strlen(buf);
comma = strchr(buf,',');
commaPosition = comma - buf;
if ((commaPosition < 1) || (commaPosition > (sLen - 1))) {
return (2+i);
}
token = strtok(buf,",");
strcpy(SSaddr,token);
token = strtok(NULL,",");
strcpy(SSport,token);
for (j=0; j<strlen(SSport)-1; j++) {
if (!isdigit(SSport[j])) {
return (2+i);
}
}
if ((jRead = atoi(SSport)) == 0) {
return (2+i);
}
if ((jRead <= 0) || (jRead > MAXPORTNUMBER)) {
DieWithError("Invalid port number.");
}
}
}
fclose(fd);
return 0;
}
void
makeStream(char **buf, struct chainData *cd, char *urlValue) {
*buf = malloc (255*sizeof(char) * sizeof(struct chainData));
if (*buf == NULL) DieWithError("Malloc error.");
memcpy(*buf,cd,sizeof(struct chainData));
memcpy(*buf+sizeof(struct chainData),urlValue,255);
}
void
sendURLandChainData (struct chainData *cd, char *urlValue)
{
int sock; /* Socket descriptor */
struct sockaddr_in myServAddr; /* server address */
unsigned short myServPort; /* server port */
char rcvBuffer; /* Buffer for reply string */
int bytesRcvd, totalBytesRcvd; /* Bytes read in single recv()
and total bytes read */
struct hostent *hostentStruct; /* helper struct for gethostbyname() */
char *serverName;
char *SSaddr;
int SSport,portNumber;
int i;
char *buf;
int chosenLinkNum;
int ipAddressEntered = 1; /* flag to indicate an IP address was entered as -s arg */
struct chainData cdSend;
getRandomSS(cd,&SSaddr,&SSport,&chosenLinkNum);
/* if all digits and periods, then IP address was entered */
for (i = 0; i < strlen (SSaddr); i++)
{
if (SSaddr[i] != '.')
{
if (isdigit (SSaddr[i]) == 0)
{
ipAddressEntered = 0;
}
}
}
serverName = SSaddr;
portNumber = SSport;
/* Create a stream socket using TCP */
if ((sock = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
DieWithError ("socket() failed");
/* establish 3 seconds as the timeout value */
struct timeval tv;
tv.tv_sec = 3;
tv.tv_usec = 0;
if (setsockopt (sock, SOL_SOCKET, SO_RCVTIMEO, (char *) &tv, sizeof tv))
DieWithError ("setsockopt() failed");
/* Construct the server address structure */
memset (&myServAddr, 0, sizeof (myServAddr)); /* Zero out structure */
myServAddr.sin_family = AF_INET; /* Internet address family */
if (ipAddressEntered)
{
/* load the address */
myServAddr.sin_addr.s_addr = inet_addr (serverName);
}
else
{
/* if a hostname was entered, we need to look for IP address */
hostentStruct = gethostbyname (serverName);
if (hostentStruct == NULL)
DieWithError ("gethostbyname() failed");
/* get the correct "format" of the IP address */
serverName = inet_ntoa (*(struct in_addr *) (hostentStruct->h_addr_list[0]));
if (serverName == NULL)
{
fprintf (stderr, "inet_ntoa() failed.\n");
exit (1);
}
/* load the address */
memcpy (&myServAddr.sin_addr, hostentStruct->h_addr_list[0], hostentStruct->h_length);
}
/* load the port */
myServAddr.sin_port = htons (portNumber); /* Server port */
/* Establish the connection to the server */
if (connect (sock, (struct sockaddr *) &myServAddr, sizeof (myServAddr)) < 0)
DieWithError ("connect() failed");
/* Send the struct to the server */
chainDataAndURLToString(cd,urlValue,&buf);
if (send (sock, buf, strlen(buf), 0) != strlen(buf))
DieWithError ("send() sent a different number of bytes than expected");
/* let everyone know the URL was sent */
fprintf (stdout, "send buffer size = %d\n", strlen(buf));
fprintf (stdout, "Sent chainfile contents to server %s:%d via TCP\n", serverName, portNumber);
fprintf (stdout, "(sent)*****\n%s\n*****\n", buf);
free(buf);
/* sent the bytes to server */
/* so close things up */
if (close (sock) != 0)
DieWithError ("close() failed");
fprintf (stderr, "Send was a Success - exiting.\n");
exit (0);
}
int
main (int argc, char **argv)
{
struct chainData cd;
char urlValue[255] = "";
char *argValue = NULL;
char *SSaddr;
int SSport;
char chainfileName[255] = "./chaingang.txt";
int c;
int i;
int chosenLinkNum;
int iMaxPortNumber = 65535;
opterr = 0;
srand(time(NULL));
while ((c = getopt (argc, argv, "c:")) != -1)
{
argValue = optarg;
switch (c)
{
case 'c':
strcpy(chainfileName,argValue);
break;
default:
fprintf (stderr, "Unknown command line option: -%c\n", optopt);
printUsage ();
exit (1);
}
}
for (i=optind; i < argc; i++)
strcpy(urlValue,argv[i]);
if (urlValue == "")
{
fprintf (stderr, "Command line argument '<URL>' is required.\n");
printUsage ();
exit (1);
}
FILE *fd = fopen(chainfileName,"r");
if (fd == NULL) {
fprintf (stderr, "Error opening chainfile specified ('%s').\n",chainfileName);
exit (1);
}
if ((i = sanityCheckFile(chainfileName)) != 0) {
fprintf (stderr, "Error in line number %d of Chainfile specified ('%s').\n",i,chainfileName);
exit (1);
}
if (readChainFile(fd,&cd)) {
fclose(fd);
fprintf (stderr, "Error reading chainfile ('%s').\n",chainfileName);
exit (1);
}
fclose(fd);
dbgPrintChainData(&cd);
printf("\n");
for (i=0; i< 10; i++) {
getRandomSS(&cd,&SSaddr,&SSport,&chosenLinkNum);
printf("%s,%d,%d\n",SSaddr,SSport,chosenLinkNum);
}
sendURLandChainData (&cd, urlValue);
// if (goGetFile(urlValue)) {
// DieWithError("Error retreiving URL.\n");
// }
return 0;
}