-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.cpp
More file actions
317 lines (265 loc) · 8.26 KB
/
common.cpp
File metadata and controls
317 lines (265 loc) · 8.26 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
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
/*
Creator : Sayan Mahapatra
Date : 06-02-2022
Description : In this file implementation of common functionality shared
between client and server is given
*/
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <time.h>
#include "constants.hpp"
int send_signal(int dest, char signal[MAX_SIGNAL_LEN]) {
char buff[MAX_SIGNAL_LEN];
memcpy(buff, signal, MAX_SIGNAL_LEN);
int bs = send(dest, buff, 10, 0);
while (bs < MAX_SIGNAL_LEN) {
if (bs < 0) return ERR_SENDING_SIGNAL;
bs += send(dest, buff + bs, MAX_SIGNAL_LEN - bs, 0);
}
return SUCCESS;
}
// Receive with timeout
int recv_t(int socket, char *buffer, int nbytes, int flgs, struct timeval *tv) {
fd_set f;
FD_ZERO(&f);
FD_SET(socket, &f);
int ret = select(socket + 1, &f, NULL, NULL, tv);
if (ret == 0) return ERR_TIMEOUT;
else if (ret < 0)
return ret;
else {
return recv(socket, buffer, nbytes, flgs);
}
}
int wait_for_signal(int src, char dest[MAX_SIGNAL_LEN], int *timeout) {
bzero(dest, MAX_SIGNAL_LEN);
struct timeval *tv;
if (timeout != NULL) {
tv->tv_sec = *timeout;
tv->tv_usec = 0;
} else {
tv = NULL;
}
int br = recv_t(src, dest, MAX_SIGNAL_LEN, 0, tv);
while (br < MAX_SIGNAL_LEN) {
if (br <= 0) return br;
br += recv_t(src, dest + br, MAX_SIGNAL_LEN - br, 0, tv);
}
return SUCCESS;
}
// Sends size bytes from buff in one go
int send_all_bytes(int dest_soc, char *buff, int size) {
int bytes_written;
do {
bytes_written = send(dest_soc, buff, size, 0);
buff += bytes_written;
size -= bytes_written;
// Check for completion
if (size == 0) return SUCCESS;
} while (bytes_written < size && bytes_written >= 0);
// Some error occured
return ERR_TX;
}
// Writes size bytes from buff in one go to dest fd
int write_all_bytes(int dest, char *buff, int size) {
int bytes_written;
do {
bytes_written = write(dest, buff, size);
buff += bytes_written;
size -= bytes_written;
// Check for completion
if (size == 0) return SUCCESS;
} while (bytes_written < size && bytes_written >= 0);
// Some error occured
return ERR_TX;
}
int get_file_size(char *filename) {
FILE *fp = fopen(filename, "r");
if (!fp) return -1;
fseek(fp, 0L, SEEK_END);
return ftell(fp);
}
/* Sends file atomically with handshaking. Timeout = NULL => Blocking*/
int send_file(int dest_soc, char *filename, int *timeout) {
char buffer[BUFFSIZE];
bzero(buffer, BUFFSIZE);
char cbuff[BUFFSIZE];
bzero(cbuff, BUFFSIZE);
int ret;
if ((ret = send_signal(dest_soc, ACK_SIGNAL)) < 0) {
fprintf(stderr, "Unable to begin sending file: %d\n", ret);
return ERR_FILE_NOT_SENT;
}
if ((ret == wait_for_signal(dest_soc, cbuff, timeout)) < 0 ||
strncmp(cbuff, ACK_SIGNAL, strlen(ACK_SIGNAL)) != 0) {
fprintf(stderr, "Didnot receive handshake: %d\n", ret);
return ERR_FILE_NOT_SENT;
}
// Handshaking done begin file transmission
int fd = open(filename, O_RDONLY);
if (fd < 0) {
perror("ERROR Opening file");
return ERR_FILE_ERROR;
}
// First send filesize
int size = get_file_size(filename);
int size_n = htonl(size);
send_all_bytes(dest_soc, (char *)&size_n, 4);
int bytes_read = 0;
// Start sending the file
while (bytes_read < size) {
int br = read(fd, buffer, BUFFSIZE);
if (br == 0) break;
if (br < 0) {
close(fd);
perror("ERROR reading from file");
return ERR_FILE_NOT_SENT;
}
if (send_all_bytes(dest_soc, buffer, br) < 0) return ERR_FILE_NOT_SENT;
bytes_read += br;
}
if (bytes_read != size) {
fprintf(stderr, "Could only send %d of %d bytes of file %s\n", bytes_read, size, filename);
return ERR_FILE_NOT_SENT;
}
if ((ret = send_signal(dest_soc, ACK_SIGNAL)) < 0) {
fprintf(stderr, "Unable to send end of file: %d\n", ret);
return ERR_FILE_NOT_SENT;
}
if ((ret == wait_for_signal(dest_soc, cbuff, timeout)) < 0 ||
strncmp(cbuff, ACK_SIGNAL, strlen(ACK_SIGNAL)) != 0) {
fprintf(stderr, "Didnot receive handshake: %d\n", ret);
return ERR_FILE_NOT_SENT;
}
// printf("Sent %d bytes in total\n", bytes_read);
return SUCCESS;
}
/* Received file atomically with debugging */
int receive_file(int src, char *filename, int *timeout) {
char buffer[BUFFSIZE];
bzero(buffer, BUFFSIZE);
char cbuff[MAX_COMMAND_LEN];
bzero(cbuff, MAX_COMMAND_LEN);
int ret;
if ((ret = wait_for_signal(src, cbuff, timeout)) < 0 ||
strncmp(cbuff, ACK_SIGNAL, strlen(ACK_SIGNAL)) != 0) {
fprintf(stderr, "Error waiting for handshake: %d\n", ret);
return ERR_FILE_NOT_READ;
}
if ((ret = send_signal(src, ACK_SIGNAL)) < 0) {
fprintf(stderr, "Error sending handshake: %d\n", ret);
return ERR_FILE_NOT_READ;
}
// First 4 bytes will be file size
int size, br = 0;
do {
int b = recv(src, &size, 4 - br, 0);
if (b <= 0) return ERR_FILE_NOT_READ;
br += b;
} while (br < 4);
int size_h = ntohl(size);
int fd = open(filename, O_WRONLY | O_CREAT, 0777);
if (fd < 0) {
perror("ERROR Opening file");
return ERR_FILE_ERROR;
}
int bytes_read = 0;
while (bytes_read < size_h) {
int to_read = (size_h - bytes_read) > BUFFSIZE ? BUFFSIZE : (size_h - bytes_read);
int b = recv(src, buffer, to_read, 0);
if (b <= 0) break;
bytes_read += b;
if (write_all_bytes(fd, buffer, b) < 0) {
fprintf(stderr, "Failed to write bytes to receiving file\n");
return ERR_FILE_NOT_READ;
}
}
if (bytes_read != size_h) {
fprintf(stderr, "Failed to read full file\n");
return ERR_FILE_NOT_READ;
}
if ((ret = wait_for_signal(src, cbuff, timeout)) < 0 ||
strncmp(cbuff, ACK_SIGNAL, strlen(ACK_SIGNAL)) != 0) {
fprintf(stderr, "Error waiting for handshake: %d\n", ret);
return ERR_FILE_NOT_READ;
}
if ((ret = send_signal(src, ACK_SIGNAL)) < 0) {
fprintf(stderr, "Error sending handshake: %d\n", ret);
return ERR_FILE_NOT_READ;
}
// printf("Received %d bytes in total\n", bytes_read);
return SUCCESS;
}
int get_lines_in_file(char filename[FILE_NAME_LEN]) {
FILE *fp = fopen(filename, "r");
if (!fp) return ERR_FILE_ERROR;
char linebuff[LINESIZE];
int c = 0;
while (fgets(linebuff, LINESIZE, fp) != NULL) {
c++;
}
fclose(fp);
return c;
}
// Prints file on screen. skiplines = NULL => print all lines
void print_file(char *filename, int *skiplines) {
FILE *fp = fopen(filename, "r");
if (!fp) {
printf("%s not found", filename);
return;
}
char linebuf[LINESIZE];
int c;
if (skiplines) c = *skiplines;
while (fgets(linebuf, LINESIZE, fp) != NULL) {
if (skiplines != NULL && c > 0) c--;
else
fputs(linebuf, stdout);
}
fclose(fp);
}
int check_file_present(char *filename) {
FILE *fp = fopen(filename, "r");
if (fp != NULL) {
fclose(fp);
return SUCCESS;
}
return ERR_FILE_NOT_FOUND;
}
/* Returns name for a temporary file*/
int get_temp_file(char *prefix, char filename[TEMP_FILE_LEN]) {
bzero(filename, TEMP_FILE_LEN);
int p = getpid();
int t = time(NULL);
int r = rand() % 100000;
if (prefix) return sprintf(filename, "%s/%d_%d_%d.tmp", prefix, p, t, r);
else
return sprintf(filename, "%d_%d.tmp", p, t);
}
void remove_temp_file(char fname[TEMP_FILE_LEN]) {
if (fname != NULL) {
remove(fname);
}
}
// Remove newline character from end of string if present
void strip_newline(char *s) {
if (s[strlen(s) - 1] == '\n') s[strlen(s) - 1] = '\0';
}
/* Add newline character to s if not present */
void make_line_terminated(char *s) {
if (s[strlen(s) - 1] != '\n') {
strcat(s, "\n");
}
}