-
Notifications
You must be signed in to change notification settings - Fork 0
/
siphon.c
504 lines (470 loc) · 15.7 KB
/
siphon.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/*
* File: siphon.c
* Author: solkin
*
* Created on December 19, 2012, 10:53 PM
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
#include <getopt.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#define BLOCK_FILE_START 0x01
#define BLOCK_DONE 0x02
#define BLOCK_MD5_WITH_FILE 0x03
#define BLOCK_FILE_WITH_MD5 0x04
/** Default values **/
int buffer_size = 0x1400;
int port = 3214;
char *directory = "";
/** Main methods **/
int open_file(const char *file_path, int flags);
int open_socket(const char *ip);
int send_files(char **files_path, int files_count, int sock);
int load_file(const char *directory, int sock);
int transfer_data(int src, int dest, off_t file_seek, off_t file_size);
/** Socket utils */
void *read_data(int src, char stop_byte, off_t stop_size);
ssize_t write_total(int __fd, const void *__buf, size_t __n);
ssize_t read_total(int __fd, void *__buf, size_t __nbytes);
/** File utils **/
off_t fsize(const char *file_path);
const char *fname(const char *file_path);
char *fpath(const char *file_name, const char *directory);
/** Progress methods **/
static void setup_bar(const char *file_name, off_t file_size);
static inline void show_bar(size_t total_read);
/** Additional **/
static void show_help();
/** Progress static variables **/
static int bar_percent, bar_c;
static const char *bar_size_metrics;
static char bar_progress[22];
static const char *bar_file_name;
static off_t bar_file_size;
static size_t bar_total_read;
static size_t bar_total_read_bytes;
static struct winsize tty_size;
/*
* Main method
*/
int main(int argc, char **argv) {
char **files = malloc(0);
int c, files_count = 0;
char *host = NULL;
while (1) {
static struct option long_options[] = {
{"listen", no_argument, 0, 'l'},
{"connect", required_argument, 0, 'c'},
{"version", no_argument, 0, 'v'},
{"help", no_argument, 0, 'h'},
{"port", required_argument, 0, 'p'},
{"file", required_argument, 0, 'f'},
{"buffer", required_argument, 0, 'b'},
{"directory", required_argument, 0, 'd'},
{0, 0, 0, 0}
};
/** getopt_long stores the option index here **/
int option_index = 0;
c = getopt_long(argc, argv, "lc:vhp:f:b:d:", long_options, &option_index);
/** Detect the end of the options **/
if (c == -1)
break;
switch (c) {
case 0:
/** If this option set a flag, do nothing else now **/
if (long_options[option_index].flag != 0) break;
printf("option %s", long_options[option_index].name);
if (optarg) printf(" with arg %s", optarg);
printf("\n");
break;
case 'l':
host = NULL;
break;
case 'v':
printf("Siphon - Utility to send files via direct connection, written in C\nTomClaw Software\nVersion 1.0\n");
break;
case 'h':
show_help();
break;
case 'c':
host = optarg;
break;
case 'p':
port = *(int *) optarg;
break;
case 'f': {
char **updated = malloc((files_count + 1) * sizeof(char *));
for (int i = 0; i < files_count; i++) {
updated[i] = files[i];
}
free(files);
files = updated;
files[files_count] = optarg;
files_count += 1;
break;
}
case 'b':
buffer_size = *(int *) optarg;
break;
case 'd':
directory = optarg;
break;
case '?':
/** getopt_long already printed an error message **/
break;
default:
show_help();
}
}
/** On incorrect case **/
if (optind < argc) {
printf("You must specify mode.\n");
show_help();
} else {
int sock;
/** Opening socket descriptor */
if ((sock = open_socket(host)) == EXIT_FAILURE) {
return EXIT_FAILURE;
}
if (host != NULL) {
/** Send and then load files in client mode **/
send_files(files, files_count, sock);
load_file(directory, sock);
} else {
/** Load and then send files in host mode **/
load_file(directory, sock);
send_files(files, files_count, sock);
}
shutdown(sock, SHUT_RDWR);
}
return EXIT_SUCCESS;
}
int open_file(const char *file_path, int flags) {
/** Opening file **/
int mode = 0644;
if ((flags & O_CREAT) > 0) {
mode = 0777;
}
int a_file = open(file_path, flags, mode);
if (a_file == -1) {
fprintf(stderr, "Unable to open file %s: %s\n", file_path,
strerror(errno));
return EXIT_FAILURE;
}
return a_file;
}
int open_socket(const char *ip) {
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
fprintf(stderr, "Unable to open socket: %s\n", strerror(errno));
return EXIT_FAILURE;
}
struct sockaddr_in addr;
if (ip != NULL) {
struct addrinfo *addr_info;
if (getaddrinfo(ip, NULL, NULL, &addr_info) != EXIT_SUCCESS) {
fprintf(stderr, "Unable to resolve host name: %s\n", strerror(errno));
return EXIT_FAILURE;
}
addr = *(struct sockaddr_in *) addr_info->ai_addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
if (connect(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
fprintf(stderr, "Unable to connect to socket: %s\n", strerror(errno));
return EXIT_FAILURE;
}
return sock;
} else {
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
fprintf(stderr, "Unable to bind socket: %s\n", strerror(errno));
return EXIT_FAILURE;
}
/** Listening for clients **/
struct sockaddr_in cli_addr;
socklen_t cli_len;
/** Waiting for client to connect **/
listen(sock, 1);
cli_len = sizeof(cli_addr);
/** Accepting client connection **/
int cli_sock = accept(sock, (struct sockaddr *) &cli_addr, &cli_len);
if (cli_sock < 0) {
fprintf(stderr, "Unable to connect to client: %s\n", strerror(errno));
return EXIT_FAILURE;
}
return cli_sock;
}
}
int send_files(char **files_path, int files_count, int sock) {
char block_type;
int file, c;
int trans_cond = EXIT_SUCCESS;
for (c = 0; c < files_count; c++) {
/** Opening file descriptor */
if ((file = open_file((const char *) files_path[c], O_RDONLY)) == EXIT_FAILURE) {
return EXIT_FAILURE;
}
/** Sending block, file name and file size **/
block_type = BLOCK_FILE_START;
write_total(sock, &block_type, 1);
const char *file_name = fname((const char *) files_path[c]);
write_total(sock, file_name, strlen(file_name));
write_total(sock, &"\n", 1);
off_t file_size = fsize((const char *) files_path[c]);
/** Checking for file size unavailable **/
if (file_size == -1) {
return EXIT_FAILURE;
}
write_total(sock, &file_size, 8);
setup_bar(file_name, file_size);
trans_cond = transfer_data(file, sock, 0, file_size);
/** Closing streams **/
close(file);
/** Checking for transfer condition **/
if (trans_cond == EXIT_FAILURE) {
/** Nothing to do **/
break;
}
printf("\n");
}
/** Checking for transaction success **/
if (trans_cond == EXIT_SUCCESS) {
/** Sending end-block **/
block_type = BLOCK_DONE;
write_total(sock, &block_type, 1);
}
return trans_cond;
}
int load_file(const char *directory, int sock) {
char block_type;
int file;
int trans_cond = EXIT_SUCCESS;
/** Reading block until all files will not be received **/
while (read_total(sock, &block_type, 1) == 1
&& block_type == BLOCK_FILE_START) {
/** Reading file name **/
const char *file_name = (const char *) read_data(sock, '\n', 0);
/** Concatinating into file path **/
char *file_path = fpath(file_name, directory);
/** Reading file size **/
off_t file_size = *((unsigned long *) read_data(sock, 0, 8));
/** Opening file descriptor */
if ((file = open_file(file_path, O_CREAT | O_WRONLY)) == EXIT_FAILURE) {
return EXIT_FAILURE;
}
setup_bar(file_name, file_size);
trans_cond = transfer_data(sock, file, 0, file_size);
/** Closing streams **/
close(file);
/** Checking for transfer condition **/
if (trans_cond == EXIT_FAILURE) {
/** Nothing to do now **/
break;
}
printf("\n");
}
return trans_cond;
}
int transfer_data(int src, int dest, off_t file_seek, off_t file_size) {
ssize_t bytes_read, bytes_written;
size_t total_read;
void *buffer = malloc(buffer_size);
/** Seek **/
lseek(src, file_seek, SEEK_SET);
int b_size = buffer_size;
/** Reading and sending file **/
for (total_read = file_seek; total_read < file_size;) {
if (file_size - total_read < buffer_size) {
b_size = (int) (file_size - total_read);
} else {
b_size = buffer_size;
}
bytes_read = read(src, buffer, b_size);
total_read += bytes_read;
show_bar(total_read);
if (bytes_read == 0) {
/** No more data **/
printf("\n");
return EXIT_SUCCESS;
}
if (bytes_read < 0) {
/** Error while reading data **/
fprintf(stderr, "\nUnable to read source: %s\n", strerror(errno));
return EXIT_FAILURE;
}
while (bytes_read > 0) {
bytes_written = write(dest, buffer, bytes_read);
if (bytes_written < 0) {
/** Error while writing data **/
fprintf(stderr, "\nUnable to write data: %s\n",
strerror(errno));
return EXIT_FAILURE;
}
bytes_read -= bytes_written;
buffer += bytes_written;
}
buffer -= bytes_written;
}
return EXIT_SUCCESS;
}
void *read_data(int src, char stop_byte, off_t stop_size) {
int size = 1;
void *data = malloc(size);
char buffer;
/** Reading char by char **/
while (read(src, &buffer, 1) > 0) {
/** Comparing byte **/
if (stop_byte != 0 && buffer == stop_byte) {
break;
}
/** Checking for length **/
if (stop_size != 0 && size == stop_size) {
break;
}
/** Copying bytes **/
memcpy(data + size - 1, &buffer, 1);
size += 1;
/** Shrinking size **/
data = realloc(data, size);
}
/** Stop byte **/
buffer = '\0';
memcpy(data + size - 1, &buffer, 1);
return data;
}
ssize_t write_total(int __fd, const void *__buf, size_t __n) {
int __written = 0;
while (__written < __n) {
__written += write(__fd, __buf + __written, __n - __written);
}
return __written;
}
ssize_t read_total(int __fd, void *__buf, size_t __nbytes) {
int __read = 0;
while (__read < __nbytes) {
__read += read(__fd, __buf + __read, __nbytes - __read);
}
return __read;
}
off_t fsize(const char *file_path) {
struct stat stat_struct;
/** Obtain file info **/
if (stat(file_path, &stat_struct) == 0) {
return stat_struct.st_size;
}
return -1;
}
const char *fname(const char *file_path) {
if (file_path) {
char *slash_index;
/** Unix FS **/
slash_index = strstr(file_path, "/");
if (slash_index != NULL) {
return strrchr(file_path, '/') + 1;
}
/** Windows FS **/
slash_index = strstr(file_path, "\\");
if (slash_index != NULL) {
return strrchr(file_path, '\\') + 1;
}
}
return file_path;
}
char *fpath(const char *file_name, const char *directory) {
/** Checking for parameters are specified **/
if (file_name && directory) {
/** Allocating memory **/
char *file_path = (char *) malloc(strlen(directory) + strlen(file_name) + 1);
/** Concatinating **/
strcat(file_path, directory);
strcat(file_path, file_name);
strcat(file_path, "\0");
return file_path;
}
return NULL;
}
static void setup_bar(const char *file_name, off_t file_size) {
bar_file_name = file_name;
bar_file_size = file_size;
}
static inline void show_bar(size_t total_read) {
bar_total_read_bytes = total_read;
/** Calculating metrics **/
if (total_read < 1024) {
bar_size_metrics = "Byte";
}
if (total_read >= 1024) {
total_read /= 1024;
bar_size_metrics = "KiB ";
}
if (total_read >= 1024) {
total_read /= 1024;
bar_size_metrics = "MiB ";
}
if (total_read >= 1024) {
total_read /= 1024;
bar_size_metrics = "GiB ";
}
if (total_read >= 1024) {
total_read /= 1024;
bar_size_metrics = "TiB ";
}
/** Checking for something changed **/
if (bar_total_read != total_read
|| ((bar_total_read_bytes * 100) / bar_file_size) != bar_percent) {
/** Updating values **/
bar_total_read = total_read;
bar_percent = (int) ((bar_total_read_bytes * 100) / bar_file_size);
/** Padding **/
for (bar_c = 0; bar_c < 22; bar_c += 1) {
if (bar_c * 100 / 22 <= bar_percent) {
bar_progress[bar_c] = '#';
} else {
bar_progress[bar_c] = '-';
}
}
/** Looking for output size **/
ioctl(0, TIOCGWINSZ, &tty_size);
if (tty_size.ws_col == 0) {
/** Default console width **/
tty_size.ws_col = 80;
}
/** Output **/
printf(" %*s %4d %4s [%s] %3d %%\r", -(tty_size.ws_col - 42), strndup(bar_file_name, tty_size.ws_col - 42),
(int) bar_total_read,
bar_size_metrics, bar_progress, bar_percent);
}
}
static void show_help() {
printf("Usage:\n");
printf("\n");
printf(" siphon --listen [options]\n");
printf(" siphon --connect <address> [options]\n");
printf("\n");
printf("Options:\n");
printf("\n");
printf(" --version, -v Show Siphon version and exit.\n");
printf(" --help, -h Show this text and exit.\n");
printf(" --port, -p Use specified port. Defaults to 3214.\n");
printf(" --file, -f Send specified files after connection.\n"
" Use \"-f file1 -f file2\" to send multiple files.\n");
printf(" --buffer, -b Use specified buffer size in bytes.\n"
" Defaults to 5120 bytes.\n");
printf(" --directory, -d Use specified directory to store received files.\n"
" Format is: /home/user/folder/.\n\n");
exit(EXIT_SUCCESS);
}