-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeliver.c
More file actions
165 lines (138 loc) · 5.13 KB
/
deliver.c
File metadata and controls
165 lines (138 loc) · 5.13 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include "packet.h"
#define MAXLINE 1024 // The maximum number of bytes that can be received from one UDP packet
int main(int argc, char *argv[]) {
// Check input, must be supplied exactly 2 arguments
if (argc != 3) {
printf("Error: Invalid input is provided.\n");
exit(EXIT_FAILURE);
}
// Try creating a socket
int socket_fd;
if ((socket_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
printf("Error: Failed to create socket.\n");
exit(EXIT_FAILURE);
}
// Get the server address and port number from the input arguments
short success;
uint32_t server_addr_binary;
short server_port;
struct sockaddr_in server_addr;
success = inet_pton(AF_INET, argv[1], &server_addr_binary);
if (success != 1) {
printf("Error: Invalid server address.\n");
exit(EXIT_FAILURE);
}
server_port = (unsigned short)(atoi(argv[2]));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(server_port);
server_addr.sin_addr.s_addr = server_addr_binary;
// Prompt the user to enter a file name to search for
char file_name[100];
printf("ftp ");
scanf("%s", file_name);
printf("Searching for file %s.\n", file_name);
if (access(file_name, F_OK) == 0) {
// File found
printf("File %s found.\n", file_name);
// strcpy(message, "ftp");
} else {
// File not found
printf("File %s not found, exiting.\n", file_name);
exit(0);
}
// Parse file
FILE *fp;
unsigned int file_size;
unsigned int total_frag;
char total_frag_str[100];
char message[1500];
// Get total file size
fp = fopen(file_name, "r");
fseek(fp, 0L, SEEK_END);
file_size = ftell(fp);
fseek(fp, 0L, SEEK_SET);
printf("File size: %d\n", file_size);
total_frag = (file_size / 1000) + 1;
unsigned int frag_no = 1;
char frag_no_str[100];
unsigned int remaining_bytes = file_size;
char remaining_bytes_str[100];
unsigned int non_data_len;
unsigned int data_size;
for (frag_no = 1; frag_no <= total_frag; frag_no++) {
printf("Total frags: %u\n", total_frag);
sprintf(total_frag_str, "%u", total_frag);
strcpy(message, total_frag_str);
strcat(message, ":");
// printf("Packet: %s\n", message);
printf("Frag no: %u\n", frag_no);
sprintf(frag_no_str, "%u", frag_no);
strcat(message, frag_no_str);
strcat(message, ":");
// printf("Packet: %s\n", message);
if (remaining_bytes > 1000) {
strcat(message, "1000:");
data_size = 1000;
remaining_bytes = remaining_bytes - 1000;
} else {
sprintf(remaining_bytes_str, "%u", remaining_bytes);
strcat(message, remaining_bytes_str);
strcat(message, ":");
data_size = remaining_bytes;
}
strcat(message, file_name);
strcat(message, ":");
non_data_len = strlen(message);
// printf("Data start index: %d\n", non_data_len);
fread(&message[non_data_len], 1, data_size, (FILE*)fp);
printf("Packet: ");
int i = 0;
for (i = 0; i < non_data_len + data_size; i++) {
// printf("%d", i);
printf("%c", message[i]);
}
printf("\n");
// Send a message
int n_bytes_sent = sendto(socket_fd, (char *)message, non_data_len + data_size, 0, (struct sockaddr*)(&server_addr), sizeof(server_addr));
printf("%d bytes sent.\n", n_bytes_sent);
// Set timeout
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 200;
if (setsockopt (socket_fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof timeout) < 0) {
printf("setsockopt failed\n");
exit(EXIT_FAILURE);
}
// Receive one message
int n_bytes_received;
char buffer[MAXLINE];
int server_addr_size;
n_bytes_received = recvfrom(socket_fd, buffer, MAXLINE, 0, (struct sockaddr *)(&server_addr), &server_addr_size);
// If timeout, resend packet
while (n_bytes_received == -1) {
int n_bytes_sent = sendto(socket_fd, (char *)message, non_data_len + data_size, 0, (struct sockaddr*)(&server_addr), sizeof(server_addr));
printf("No ACK received from server, %d bytes retransmitted.\n", n_bytes_sent);
n_bytes_received = recvfrom(socket_fd, buffer, MAXLINE, 0, (struct sockaddr *)(&server_addr), &server_addr_size);
}
buffer[n_bytes_received] = '\0';
// printf("Server : %s\n", buffer);
/* === @JERRY: HACK INSERTED HERE: START === */
buffer[3] = '\0';
/* === @JERRY: HACK INSERTED HERE: END === */
if (strcmp(buffer, "ACK") == 0) {
printf("Recieved ACK from server.\n", buffer);
} else {
printf("Did not receive ACK from server, exiting.\n", buffer);
exit(0);
}
}
return 0;
}