-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriviald.c
286 lines (229 loc) · 5.49 KB
/
triviald.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
#include "common.h"
void wrqAction(int sockID, struct sockaddr_in sockInfo, char *buffer, struct PARAMS *params);
void rwqAction(int sockID, struct sockaddr_in sockInfo, char *buffer, struct PARAMS *params);
void terminator();
int socketBind();
int main(int argc, char *argv[])
{
if(argc > 5)
{
printf("triviald -h\n");
return(EXIT_FAILURE);
}
int s_id = 0;
char buffer[MAX_BUFFER];
socklen_t socklen = sizeof(struct sockaddr_in);
struct PARAMS params;
struct sockaddr_in sockInfo;
bzero(buffer, MAX_BUFFER);
bzero(¶ms, sizeof(struct PARAMS));
bzero(&sockInfo, sizeof(struct sockaddr_in));
params.sc = TRIVIALD;
params.port = 8108;
params.rexmt = 5;
param_parser(argc, argv, ¶ms);
if( (s_id = create_socket(PF_INET, SOCK_DGRAM, 0)) == -1)
{
printf("Error creating socket\n");
exit(EXIT_FAILURE);
}
if(make_bind(s_id, AF_INET, params.port, NULL) == -1)
{
printf("Error making bind\n");
exit(EXIT_FAILURE);
}
signal(SIGCHLD, &terminator);
while(1)
{
if(params.verbose)
{
printf("Waiting for new connection\n");
}
bzero(buffer, MAX_BUFFER);
recvfrom(s_id, (char *) buffer, MAX_BUFFER, 0, (struct sockaddr *) &sockInfo, &socklen);
if(fork() == 0)
{
close(s_id);
s_id = socketBind();
switch(ldshort(buffer))
{
case RFC1350_OP_RRQ:
{
rwqAction(s_id, sockInfo, buffer, ¶ms);
}break;
case RFC1350_OP_WRQ:
{
wrqAction(s_id, sockInfo, buffer, ¶ms);
}break;
default:
{
if(sendError(s_id, sockInfo, RFC1350_OP_ERROR, RFC1350_ERR_ILEGALOP, "Illegal TFTP operation") == -1)
{
printf("Error default case\n");
}
}break;
}
break;
}
}
close(s_id);
return(EXIT_SUCCESS);
}
void terminator()
{
while(wait3(NULL, WNOHANG, NULL) > 0 );
}
int socketBind()
{
int s_id = 0;
if( (s_id = create_socket(PF_INET, SOCK_DGRAM, 0)) == -1)
{
printf("Error creating socket\n");
exit(EXIT_FAILURE);
}
if(make_bind(s_id, AF_INET, 0, NULL) == -1)
{
printf("Error making bind\n");
exit(EXIT_FAILURE);
}
return(s_id);
}
void wrqAction(int sockID, struct sockaddr_in sockInfo, char *buffer, struct PARAMS *params)
{
tftp_rwq_hdr rwq;
tftp_data_hdr data;
rwq_deserialize(&rwq, buffer);
if(mode_transfer(rwq.mode, "octet", params) == -1)
{
sendError(sockID, sockInfo, RFC1350_OP_ERROR, RFC1350_ERR_NOTDEF, "Set transfer mode to octet");
return;
}
if(file_exists(rwq.filename) == 1)
{
sendError(sockID, sockInfo, RFC1350_OP_ERROR, RFC1350_ERR_FEXISTS, "File already exists");
return;
}
FILE *pFile = NULL;
if((pFile = open_file(rwq.filename, "wb", params)) == NULL)
{
switch(errno)
{
case EACCES:
{
sendError(sockID, sockInfo, RFC1350_OP_ERROR, RFC1350_ERR_ACCESS, "Permission denied");
}break;
case ENOMEM:
{
sendError(sockID, sockInfo, RFC1350_OP_ERROR, RFC1350_ERR_DISKFULL, "Not enough space");
}break;
default:
{
sendError(sockID, sockInfo, RFC1350_OP_ERROR, RFC1350_ERR_NOTDEF, strerror(errno));
}break;
}
return;
}
if(timeout_option(&rwq, params) == -1)
{
printf("Ok!");
sendACK(sockID, sockInfo, 0);
}
else
{
printf("NOk!");
sendACKOpt(sockID, sockInfo, "timeout", params->rexmt);
}
int sz = 0;
int t_out = 0;
int intents = 5;
if(params->verbose)
{
printf("C write\t %s: %s\n", inet_ntoa(sockInfo.sin_addr), rwq.filename);
}
do
{
sz = 0;
intents = 5;
do
{
if((t_out = select_func(sockID, params->rexmt)) == 1)
{
sz = get_data(&data, sockID, sockInfo, params);
sendACK(sockID, sockInfo, data.num_block);
fwrite(data.data, sz, 1, pFile);
}
}while(t_out == 0 && --intents);
}while(sz == RFC1350_BLOCKSIZE && t_out != -1);
if(intents == 0)
{
printf("Transfer timed out\n");
}
fclose(pFile);
}
void rwqAction(int sockID, struct sockaddr_in sockInfo, char *buffer, struct PARAMS *params)
{
unsigned short int bnum = 1;
unsigned short int sz = 0;
tftp_rwq_hdr rwq;
tftp_data_hdr data;
tftp_ack_hdr ack;
bzero(&data, sizeof(tftp_data_hdr));
bzero(&rwq, sizeof(tftp_rwq_hdr));
rwq_deserialize(&rwq, buffer);
if(mode_transfer(rwq.mode, "octet", params) == -1)
{
sendError(sockID, sockInfo, RFC1350_OP_ERROR, RFC1350_ERR_NOTDEF, "Set transfer mode to octet");
return;
}
FILE *pFile = NULL;
if((pFile = open_file(rwq.filename, "rb", params)) == NULL)
{
switch(errno)
{
case ENOENT:
{
sendError(sockID, sockInfo, RFC1350_OP_ERROR, RFC1350_ERR_FNOTFOUND, "No such file or directory");
}break;
case EACCES:
{
sendError(sockID, sockInfo, RFC1350_OP_ERROR, RFC1350_ERR_ACCESS, "Permission denied");
}break;
default:
{
sendError(sockID, sockInfo, RFC1350_OP_ERROR, RFC1350_ERR_NOTDEF, strerror(errno));
}break;
}
return;
}
if(timeout_option(&rwq, params) == 1)
{
sendACKOpt(sockID, sockInfo, "timeout", params->rexmt);
}
int t_out = 0;
int intents = 5;
do
{
bzero(buffer, MAX_BUFFER);
bzero(&data, sizeof(tftp_data_hdr));
data.opcode = RFC1350_OP_DATA;
data.num_block = bnum++;
sz = fread(data.data, 1, RFC1350_BLOCKSIZE, pFile);
sz = data_serialize(&data, buffer, sz);
bzero(&ack, sizeof(tftp_ack_hdr));
intents = 5;
do
{
sendInfo(sockID, sockInfo, buffer, sz);
if((t_out = select_func(sockID, params->rexmt)) == 1)
{
get_ack(&ack, sockID, sockInfo, params);
ack_serialize(&ack, buffer);
}
}while(t_out == 0 && --intents);
}while((sz - SHORT_SIZE * 2) == RFC1350_BLOCKSIZE && t_out != -1);
if(intents == 0)
{
printf("Transfer timed out\n");
}
fclose(pFile);
}