-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathawget.c
240 lines (207 loc) · 7.06 KB
/
awget.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
//==============================================================================
// Name : awget.c
// Author : Nicholas
// Version : 1
// Copyright : None
// Description : v1: handles the chaining and file request for awget
// Arguments : This program takes in 3 arguments
// : arg1 = URL of file to request
// : arg2 = -c or -h (optional)
// -c specifies the chainfile
// -h prints the help msg
// : arg3 = Name of chainfile (optional but required if -c is
// present)
//
// This program was written for CS457 at CSU as apart of a CS degree program.
//
//==============================================================================
#include "awget.h"
void print_help(){
printf("awget program:\n");
printf("This program will request a file and traverse the stepping stones"
"\nspecified by the chainfile to download it.\n");
printf("Usage: ./awget <URL> [options]\n");
printf("\nREQUIRED ARGUMENTS\n");
printf("<URL> Specifies the file to download.\n");
printf("\nOPTIONAL FLAGS\n");
printf("-h Prints the help message.\n");
printf("-c <chainfile> Specifies the chainfile location\n");
printf("\nUsing this program with no options will attempt to find the "
"chainfile\nin the working directory as 'chaingang.txt'\n");
}
int errorCheck(int ec) {
switch (ec) { //Sets up switch to detect type of error.
case 0:
return 0;
case -1: //File issue
fprintf(stderr, "Invalid Chainfile\n");
break;
case -2: //Argument issue
fprintf(stderr, "Arguments not correct\n\n");
print_help();
break;
case -3:
fprintf(stderr, "awget failed to connect\n");
break;
case -4:
fprintf(stderr, "awget failed to send\n");
break;
case -10:
fprintf(stderr, "File Requested invalid!\n");
break;
default:
fprintf(stderr, "Unknown error.\n");
}
return -1;
}
const char* get_filename(const char* request){
int last = 0;
for(int i=0; i<strlen(request);i++) {
if (request[i] == '/') last = i;
}
if (last == 0 || last == strlen(request)-1){
return "index.html";
} else {
return request+last+1;
}
}
int main(int argc, char* argv[]) {
char *request;
char *chainfile = "chaingang.txt";
/* Check Arguments */
if (argc == 2 && strcmp(argv[1], "-h") == 0) {
print_help();
return 0;
} else if (argc == 2) {
request = argv[1];
} else if (argc == 4 && strcmp(argv[2], "-c") == 0) {
request = argv[1];
chainfile = argv[3];
} else {
return errorCheck(-2);
}
/* Check If file is able to be read */
if (access(chainfile, R_OK) != 0) {
return errorCheck(-1);
}
/* Open File */
FILE *fp;
fp = fopen(chainfile, "r");
if (fp == NULL) return errorCheck(-1);
/* Get Number of Stones */
int num_step_stone;
if (fscanf(fp, "%d", &num_step_stone) != 1) return errorCheck(-1);
printf("<%d>\n", num_step_stone);
StepStone stones[num_step_stone];
/* Read File*/
for (int x = 0; x < num_step_stone; x++) {
if (fscanf(fp, "%s %d", stones[x].SSaddr, &stones[x].SSport) != 2)
return errorCheck(-1);
printf("<%s, %d>\n", stones[x].SSaddr, stones[x].SSport);
}
/* Init Random Seed */
srand(time(NULL));
/* Determine next SS */
int next = rand() % num_step_stone;
printf("next SS is <%s, %d>\n", stones[next].SSaddr, stones[next].SSport);
/* Create Socket */
int sock;
struct sockaddr_in servAddr;
char buffer[BUFFERSIZE];
int bytesRcvd;
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
fprintf(stderr, "Socket failed\n");
return errorCheck(-3);
}
/* Connect */
memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = inet_addr(stones[next].SSaddr);
servAddr.sin_port = htons(stones[next].SSport);
if (connect(sock, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) {
fprintf(stderr, "Connect failed\n");
return errorCheck(-3);
}
/* Send request and confirm */
if (send(sock, request, strlen(request), 0) != strlen(request)) {
fprintf(stderr, "Sent failure");
return errorCheck(-4);
}
memset(&buffer, '\0', BUFFERSIZE);
if ((bytesRcvd = recv(sock, buffer, BUFFERSIZE, 0)) < 0) {
fprintf(stderr, "Recv ACK. failed");
return errorCheck(-3);
}
if (strcmp(buffer, "ACK.") != 0) {
fprintf(stderr, "Request send failed");
return errorCheck(-4);
}
/* Send stepping stones */
for (int x = 0; x < num_step_stone; x++) {
if (x != next) {
if (send(sock, &stones[x], sizeof(StepStone), 0) !=
sizeof(StepStone)) {
fprintf(stderr, "Sent failure");
return errorCheck(-4);
}
memset(&buffer, '\0', BUFFERSIZE);
if ((bytesRcvd = recv(sock, buffer, BUFFERSIZE, 0)) < 0) {
fprintf(stderr, "Recv ACK. failed");
return errorCheck(-3);
}
if (strcmp(buffer, "ACK.") != 0) {
fprintf(stderr, "Stone send failed");
return errorCheck(-4);
}
}
}
/* Send End of Transmission */
if (send(sock, "EOT.", sizeof("EOT."), 0) != sizeof("EOT.")) {
fprintf(stderr, "Send EOT. failure");
return errorCheck(-4);
}
/* Begin to wait for file to return */
printf("Waiting for file...\n");
fflush(stdout);
/* Get Returned Packet, set size */
bytesRcvd = 0;
memset(&buffer, '\0', BUFFERSIZE);
while (strcmp(buffer, "RET.")!=0) {
if ((bytesRcvd = recv(sock, buffer, 4, 0)) < 0) {
fprintf(stderr, "Recv on return. failed");
return errorCheck(-5);
}
}
memset(&buffer, '\0', BUFFERSIZE);
if (send(sock, "ACK.", sizeof("ACK."), 0) != sizeof("ACK.")) {
return errorCheck(-4);
}
if ((bytesRcvd = recv(sock, buffer, BUFFERSIZE, 0)) < 0) {
fprintf(stderr, "Recv on return. failed");
return errorCheck(-5);
}
int sz = atoi(buffer);
if (sz == 0) return errorCheck(-10);
char *file = malloc(sz);
/* Set filename */
const char* filename = get_filename(request);
fp = fopen(filename, "w");
if (fp == NULL) return errorCheck(-6);
if (send(sock, "ACK.", sizeof("ACK."), 0) != sizeof("ACK.")) {
fprintf(stderr, "Send ACK. on return failure");
return errorCheck(-4);
}
int recv_b = 0;
while(recv_b < sz) {
recv_b = recv_b + recv(sock, file+recv_b, sz-recv_b, 0);
if (recv_b < 0) return errorCheck(-5);
}
printf("Received File %s\n", filename);
fflush(stdout);
fwrite(file, sz, 1, fp);
fclose(fp);
printf("Goodbye!\n");
fflush(stdout);
free(file);
return errorCheck(0);
}