Add and use file_remote

This commit is contained in:
Joseph C. Lehner 2016-02-12 14:37:26 +01:00
parent 98607d141b
commit 54e4724b9a
4 changed files with 38 additions and 10 deletions

1
main.c
View file

@ -72,6 +72,7 @@ int main(int argc, char **argv)
.ul_timeout = 120000, .ul_timeout = 120000,
.tftpcmd = NULL, .tftpcmd = NULL,
.file_local = NULL, .file_local = NULL,
.file_remote = NULL,
.ipaddr = NULL, .ipaddr = NULL,
.ipmask = "255.255.255.0", .ipmask = "255.255.255.0",
.intf = NULL, .intf = NULL,

35
nmrp.c
View file

@ -315,6 +315,14 @@ int nmrp_do(struct nmrpd_args *args)
return 1; return 1;
} }
if (args->file_remote) {
if (!tftp_is_valid_filename(args->file_remote)) {
fprintf(stderr, "Invalid remote filename '%s'.\n",
args->file_remote);
return 1;
}
}
err = 1; err = 1;
sock = ethsock_create(args->intf, ETH_P_NMRP); sock = ethsock_create(args->intf, ETH_P_NMRP);
@ -437,15 +445,21 @@ int nmrp_do(struct nmrpd_args *args)
break; break;
} }
if (verbosity) { len = 0;
len = 0; filename = msg_opt_data(&rx.msg, NMRP_O_FILE_NAME, &len);
filename = msg_opt_data(&rx.msg, NMRP_O_FILE_NAME, &len); if (filename) {
if (filename) { if (!args->file_remote) {
printf("Received upload request for '%.*s'.\n", len, args->file_remote = filename;
filename);
} else {
printf("No filename specified in upload request.");
} }
printf("Received upload request: filename '%.*s'.\n",
len, filename);
} else if (!args->file_remote) {
if (tftp_is_valid_filename(args->file_local)) {
args->file_remote = args->file_local;
} else {
args->file_remote = "firmware";
}
printf("Received upload request with empty filename.");
} }
err = 0; err = 0;
@ -462,6 +476,11 @@ int nmrp_do(struct nmrpd_args *args)
} }
if (!err && args->file_local) { if (!err && args->file_local) {
if (verbosity) {
printf("Using remote filename '%s'.\n",
args->file_remote);
}
if (!strcmp(args->file_local, "-")) { if (!strcmp(args->file_local, "-")) {
printf("Uploading from stdin ... "); printf("Uploading from stdin ... ");
} else { } else {

View file

@ -60,6 +60,7 @@ struct nmrpd_args {
unsigned ul_timeout; unsigned ul_timeout;
const char *tftpcmd; const char *tftpcmd;
const char *file_local; const char *file_local;
const char *file_remote;
const char *ipaddr; const char *ipaddr;
const char *ipmask; const char *ipmask;
const char *intf; const char *intf;
@ -70,6 +71,8 @@ struct nmrpd_args {
}; };
int tftp_put(struct nmrpd_args *args); int tftp_put(struct nmrpd_args *args);
bool tftp_is_valid_filename(const char *filename);
int nmrp_do(struct nmrpd_args *args); int nmrp_do(struct nmrpd_args *args);
int select_fd(int fd, unsigned timeout); int select_fd(int fd, unsigned timeout);

9
tftp.c
View file

@ -86,7 +86,7 @@ static void pkt_mkwrq(char *pkt, const char *filename)
size_t len = 2; size_t len = 2;
filename = leafname(filename); filename = leafname(filename);
if (!is_netascii(filename) || strlen(filename) > 500) { if (!tftp_is_valid_filename(filename)) {
fprintf(stderr, "Overlong/illegal filename; using 'firmware'.\n"); fprintf(stderr, "Overlong/illegal filename; using 'firmware'.\n");
filename = "firmware"; filename = "firmware";
} else if (!strcmp(filename, "-")) { } else if (!strcmp(filename, "-")) {
@ -223,6 +223,11 @@ inline void sock_perror(const char *msg)
} }
#endif #endif
inline bool tftp_is_valid_filename(const char *filename)
{
return strlen(filename) <= 500 && is_netascii(filename);
}
int tftp_put(struct nmrpd_args *args) int tftp_put(struct nmrpd_args *args)
{ {
struct sockaddr_in addr; struct sockaddr_in addr;
@ -267,7 +272,7 @@ int tftp_put(struct nmrpd_args *args)
/* Not really, but this way the loop sends our WRQ before receiving */ /* Not really, but this way the loop sends our WRQ before receiving */
timeout = 1; timeout = 1;
pkt_mkwrq(tx, args->file_local); pkt_mkwrq(tx, args->file_remote);
do { do {
if (!timeout && pkt_num(rx) == ACK) { if (!timeout && pkt_num(rx) == ACK) {