-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.c
147 lines (122 loc) · 3.3 KB
/
common.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
#include "common.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <arpa/inet.h>
#define BUF_SIZE 255
const char* op_tbl[] = { "", "REQUEST_DOWN", "REPLY_DOWN", "REQUEST_UP", "REPLY_UP", "ERROR" };
void make_msg_header(msg_header_t* header, uint16_t op, const char* filename) {
uint32_t sz_fn = strlen(filename);
header->data = calloc(2 + 4 + sz_fn, 1); //opcode + fn_len + sz_fn
//would sizeof(int16_t) be clearer?
uint16_t op_net = htons(op);
uint32_t sz_fn_net = htonl(sz_fn);
memcpy(header->data, &op_net, 2);
memcpy(header->data + 2, &sz_fn_net , 4);
memcpy(header->data + 6, filename, sz_fn);
header->size = 6 + sz_fn;
}
void change_opcode(msg_header_t* header, uint16_t op) {
uint16_t op_net = htons(op);
memcpy(header->data, &op_net, 2);
}
void free_msg_header(msg_header_t* header) {
free(header->data);
}
void make_data_header(data_header_t* header, const char* filename) {
FILE* fd = fopen(filename, "rb"); //open binary read file
if(!fd) {
fprintf(stderr, "File error\n");
exit(EXIT_FAILURE);
}
fseek(fd, 0L, SEEK_END);
uint32_t sz_file = (uint32_t)ftell(fd);
fseek(fd, 0L, SEEK_SET);
header->data = malloc(4 + sz_file);
if(header->data == NULL) {
fprintf(stderr, "Out of memory\n");
exit(EXIT_FAILURE);
}
header->size = 4 + sz_file;
uint32_t sz_file_net = htonl(sz_file);
memcpy(header->data, &sz_file_net, 4);
//fill the buffer
if(sz_file != (uint32_t)fread(header->data + 4, 1, sz_file, fd)) {
fprintf(stderr, "File buffer borked\n");
exit(EXIT_FAILURE);
}
}
void free_data_header(data_header_t* header) {
free(header->data);
}
int file_to_socket(int sock, const char* filename) {
FILE* file = fopen(filename, "rb");
if(!file) {
fprintf(stderr, "File error\n");
return -1;
}
//file size, no stat
fseek(file, 0L, SEEK_END);
uint32_t sz_file = (uint32_t)ftell(file);
fseek(file, 0L, SEEK_SET);
sz_file = htonl(sz_file);
if(4 != write(sock, &sz_file, 4)) {
fclose(file);
return -1;
}
char buffer[BUF_SIZE];
int run = 1;
while(run) {
size_t read;
if((read = fread(buffer, 1, 255, file)) < BUF_SIZE)
run = 0;
if(read != (size_t)write(sock, buffer, read)) {
fclose(file);
return -1;
}
}
return 0;
}
int socket_to_file(int sock, const char* filename) {
uint32_t sz_file;
if(4 != read(sock, &sz_file, 4)) {
fprintf(stderr, "Read in socket to file failed!\n");
return -1;
}
sz_file = ntohl(sz_file);
printf("Putting file %s size: %d\n", filename, sz_file);
FILE* file = fopen(filename, "w+b");
if(!file) {
fprintf(stderr, "File error\n");
return -1;
}
int run = 1;
size_t total = 0;
while(run) {
char buffer[BUF_SIZE];
size_t read_n = read(sock, buffer, 255);
total += read_n;
if(total == sz_file)
run = 0;
if(read_n != fwrite(buffer, 1, read_n, file)) {
fprintf(stderr, "File writing error\n");
fclose(file);
return -1;
}
}
fclose(file);
return 0;
}
char* prefix_it(const char* in, const char* prefix) {
char* out;
size_t len1, len2;
len1 = strlen(prefix); len2 = strlen(in);
out = malloc(len1 + len2 + 1);
if(out == NULL) {
fprintf(stderr, "PANIC!!!!!");
exit(EXIT_FAILURE);
}
memcpy(out, prefix, len1);
strcpy(out + len1, in);
return out;
}