Skip to content

Commit 870a579

Browse files
authored
Merge pull request #48 from enkiller/0828_1428
完善客户端文件传输请求
2 parents 190096a + 1870e6f commit 870a579

3 files changed

Lines changed: 40 additions & 11 deletions

File tree

tftp/tftp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
struct tftp_client
3333
{
3434
int max_retry;
35+
int err;
3536
void *_private;
3637
};
3738

@@ -47,6 +48,7 @@ struct tftp_client *tftp_client_create(const char *ip_addr, int port);
4748
void tftp_client_destroy(struct tftp_client *client);
4849
int tftp_client_push(struct tftp_client *client, const char *local_name, const char *remote_name);
4950
int tftp_client_pull(struct tftp_client *client, const char *remote_name, const char *local_name);
51+
int tftp_client_err(struct tftp_client *client);
5052
struct tftp_server *tftp_server_create(const char *root_name, int port);
5153
void tftp_server_run(struct tftp_server *server);
5254
void tftp_server_destroy(struct tftp_server *server);

tftp/tftp_client.c

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ struct tftp_client *tftp_client_create(const char *ip_addr, int port)
7474
}
7575
/* Number of Initial Retries */
7676
client->max_retry = TFTP_MAX_RETRY;
77+
/* Initialization error number */
78+
client->err = TFTP_OK;
7779
/* Binding Private Data */
7880
client->_private = _private;
7981
return client;
@@ -102,6 +104,7 @@ int tftp_client_push(struct tftp_client *client, const char *local_name, const c
102104

103105
_private = client->_private;
104106
max_retry = client->max_retry;
107+
client->err = TFTP_OK;
105108
while (max_retry)
106109
{
107110
/* Send Write Request */
@@ -110,6 +113,7 @@ int tftp_client_push(struct tftp_client *client, const char *local_name, const c
110113
{
111114
tftp_printf("tftp send request failed !! retry:%d. exit\n", client->max_retry - max_retry);
112115
max_retry = 0;
116+
client->err = res;
113117
break;
114118
}
115119
/* Waiting for server response */
@@ -121,15 +125,16 @@ int tftp_client_push(struct tftp_client *client, const char *local_name, const c
121125
}
122126
else if (res == -TFTP_ETIMEOUT)
123127
{
124-
tftp_printf("tftp selct timeout. retry\n");
128+
tftp_printf("tftp wait response timeout. retry\n");
125129
max_retry --;
126130
continue;
127131
}
128132
else
129133
{
130134
/* Waiting for Response Error */
131-
tftp_printf("tftp selct err:%d. exit\n", res);
135+
tftp_printf("tftp wait response err:%d. exit\n", res);
132136
max_retry = 0;
137+
client->err = res;
133138
break;
134139
}
135140
}
@@ -143,20 +148,23 @@ int tftp_client_push(struct tftp_client *client, const char *local_name, const c
143148
if (res != TFTP_OK)
144149
{
145150
tftp_printf("wait ack failed!! exit\n");
151+
client->err = res;
146152
return res;
147153
}
148154
/* Open file */
149155
fp = tftp_file_open(local_name, _private->xfer->mode, 1);
150156
if (fp == NULL)
151157
{
152158
tftp_printf("open file \"%s\" error.\n", local_name);
159+
client->err = -TFTP_EFILE;
153160
return -TFTP_EFILE;
154161
}
155162
pack = malloc(sizeof(struct tftp_packet));
156163
if (pack == NULL)
157164
{
158165
tftp_transfer_err(_private->xfer, 0, "malloc pack failed!");
159166
tftp_file_close(fp);
167+
client->err = -TFTP_EMEM;
160168
return -TFTP_EMEM;
161169
}
162170
while (1)
@@ -166,6 +174,7 @@ int tftp_client_push(struct tftp_client *client, const char *local_name, const c
166174
if (r_size < 0)
167175
{
168176
max_retry = 0;
177+
client->err = -TFTP_EFILE;
169178
break;
170179
}
171180
max_retry = client->max_retry;
@@ -177,6 +186,7 @@ int tftp_client_push(struct tftp_client *client, const char *local_name, const c
177186
{
178187
tftp_transfer_err(_private->xfer, 0, "send file err!");
179188
max_retry = 0;
189+
client->err = -TFTP_EDATA;
180190
break;
181191
}
182192
/* Wait server ACK */
@@ -188,14 +198,15 @@ int tftp_client_push(struct tftp_client *client, const char *local_name, const c
188198
}
189199
else if (res == -TFTP_ETIMEOUT)
190200
{
191-
tftp_printf("tftp selct timeout. retry\n");
201+
tftp_printf("tftp wait response timeout. retry\n");
192202
max_retry --;
193203
continue;
194204
}
195205
else
196206
{
197-
tftp_printf("tftp selct err:%d. exit\n", res);
207+
tftp_printf("tftp wait response err:%d. exit\n", res);
198208
max_retry = 0;
209+
client->err = res;
199210
break;
200211
}
201212
}
@@ -208,6 +219,7 @@ int tftp_client_push(struct tftp_client *client, const char *local_name, const c
208219
if (tftp_wait_ack(_private->xfer) != TFTP_OK)
209220
{
210221
tftp_printf("wait ack failed!! exit\n");
222+
client->err = -TFTP_EACK;
211223
break;
212224
}
213225
file_size += r_size;
@@ -234,6 +246,7 @@ int tftp_client_pull(struct tftp_client *client, const char *remote_name, const
234246

235247
_private = client->_private;
236248
max_retry = client->max_retry;
249+
client->err = TFTP_OK;
237250
while (max_retry)
238251
{
239252
/* Send Read File Request */
@@ -242,6 +255,7 @@ int tftp_client_pull(struct tftp_client *client, const char *remote_name, const
242255
{
243256
tftp_printf("tftp send request failed !! retry:%d. exit\n", max_retry);
244257
max_retry = 0;
258+
client->err = res;
245259
break;
246260
}
247261
/* Waiting for the server to respond to the request */
@@ -253,16 +267,17 @@ int tftp_client_pull(struct tftp_client *client, const char *remote_name, const
253267
}
254268
else if (res == -TFTP_ETIMEOUT)
255269
{
256-
tftp_printf("tftp selct timeout. retry\n");
270+
tftp_printf("tftp wait response timeout. retry\n");
257271
max_retry --;
258272
continue;
259273
}
260274
else
261275
{
262-
tftp_printf("tftp selct err:%d. exit\n", res);
276+
tftp_printf("tftp wait response err:%d. exit\n", res);
263277
max_retry = 0;
278+
client->err = res;
264279
break;
265-
}
280+
}
266281
}
267282

268283
/* More than the maximum number of retries. exit */
@@ -276,6 +291,7 @@ int tftp_client_pull(struct tftp_client *client, const char *remote_name, const
276291
if (fp == NULL)
277292
{
278293
tftp_printf("open file \"%s\" error.\n", local_name);
294+
client->err = -TFTP_EFILE;
279295
return -TFTP_EFILE;
280296
}
281297
pack = malloc(sizeof(struct tftp_packet));
@@ -284,6 +300,7 @@ int tftp_client_pull(struct tftp_client *client, const char *remote_name, const
284300
/* malloc failed. send err msg and exit */
285301
tftp_transfer_err(_private->xfer, 0, "malloc pack failed!");
286302
tftp_file_close(fp);
303+
client->err = -TFTP_EMEM;
287304
return -TFTP_EMEM;
288305
}
289306
while (1)
@@ -294,6 +311,7 @@ int tftp_client_pull(struct tftp_client *client, const char *remote_name, const
294311
if (recv_size < 0)
295312
{
296313
tftp_printf("read data err[%d]! exit\n", recv_size);
314+
client->err = -TFTP_EDATA;
297315
break;
298316
}
299317
/* Write data to file */
@@ -302,6 +320,7 @@ int tftp_client_pull(struct tftp_client *client, const char *remote_name, const
302320
{
303321
tftp_printf("write file err! exit\n");
304322
tftp_transfer_err(_private->xfer, 0, "write file err!");
323+
client->err = -TFTP_EFILE;
305324
break;
306325
}
307326
file_size += recv_size;
@@ -324,13 +343,14 @@ int tftp_client_pull(struct tftp_client *client, const char *remote_name, const
324343
}
325344
else if (res == -TFTP_ETIMEOUT)
326345
{
327-
tftp_printf("tftp selct timeout. retry\n");
346+
tftp_printf("tftp wait response timeout. retry\n");
328347
max_retry --;
329348
}
330349
else
331350
{
332-
tftp_printf("tftp selct err:%d. exit\n", res);
351+
tftp_printf("tftp wait response err:%d. exit\n", res);
333352
max_retry = 0;
353+
client->err = res;
334354
break;
335355
}
336356
}
@@ -344,3 +364,8 @@ int tftp_client_pull(struct tftp_client *client, const char *remote_name, const
344364
free(pack);
345365
return file_size;
346366
}
367+
368+
int tftp_client_err(struct tftp_client *client)
369+
{
370+
return client->err;
371+
}

tftp/tftp_xfer.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,11 @@ int tftp_send_request(struct tftp_xfer *xfer, uint16_t cmd, const char *remote_f
202202
}
203203
/* Packing request packet header */
204204
send_packet->cmd = htons(cmd);
205-
size = sprintf(send_packet->info.filename, "%s%c%s%c%d%c", remote_file, 0, xfer->mode, 0, xfer->blksize, 0) + 3;
205+
size = sprintf(send_packet->info.filename, "%s%c%s%c%s%c%d%c%s%c%d%c",
206+
remote_file, 0, xfer->mode, 0, "blksize", 0, xfer->blksize, 0,"tsize", 0, 0, 0) + 2;
206207
/* send data */
207-
r_size = sendto(xfer->sock, send_packet, size, 0, (struct sockaddr *)&_private->server, sizeof(struct sockaddr_in));
208+
r_size = sendto(xfer->sock, send_packet, size, 0,
209+
(struct sockaddr *)&_private->server, sizeof(struct sockaddr_in));
208210
free(send_packet);
209211
if (size != r_size)
210212
{

0 commit comments

Comments
 (0)