Add and use file_remote
This commit is contained in:
parent
98607d141b
commit
54e4724b9a
4 changed files with 38 additions and 10 deletions
1
main.c
1
main.c
|
@ -72,6 +72,7 @@ int main(int argc, char **argv)
|
|||
.ul_timeout = 120000,
|
||||
.tftpcmd = NULL,
|
||||
.file_local = NULL,
|
||||
.file_remote = NULL,
|
||||
.ipaddr = NULL,
|
||||
.ipmask = "255.255.255.0",
|
||||
.intf = NULL,
|
||||
|
|
35
nmrp.c
35
nmrp.c
|
@ -315,6 +315,14 @@ int nmrp_do(struct nmrpd_args *args)
|
|||
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;
|
||||
|
||||
sock = ethsock_create(args->intf, ETH_P_NMRP);
|
||||
|
@ -437,15 +445,21 @@ int nmrp_do(struct nmrpd_args *args)
|
|||
break;
|
||||
}
|
||||
|
||||
if (verbosity) {
|
||||
len = 0;
|
||||
filename = msg_opt_data(&rx.msg, NMRP_O_FILE_NAME, &len);
|
||||
if (filename) {
|
||||
printf("Received upload request for '%.*s'.\n", len,
|
||||
filename);
|
||||
} else {
|
||||
printf("No filename specified in upload request.");
|
||||
len = 0;
|
||||
filename = msg_opt_data(&rx.msg, NMRP_O_FILE_NAME, &len);
|
||||
if (filename) {
|
||||
if (!args->file_remote) {
|
||||
args->file_remote = filename;
|
||||
}
|
||||
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;
|
||||
|
@ -462,6 +476,11 @@ int nmrp_do(struct nmrpd_args *args)
|
|||
}
|
||||
|
||||
if (!err && args->file_local) {
|
||||
if (verbosity) {
|
||||
printf("Using remote filename '%s'.\n",
|
||||
args->file_remote);
|
||||
}
|
||||
|
||||
if (!strcmp(args->file_local, "-")) {
|
||||
printf("Uploading from stdin ... ");
|
||||
} else {
|
||||
|
|
3
nmrpd.h
3
nmrpd.h
|
@ -60,6 +60,7 @@ struct nmrpd_args {
|
|||
unsigned ul_timeout;
|
||||
const char *tftpcmd;
|
||||
const char *file_local;
|
||||
const char *file_remote;
|
||||
const char *ipaddr;
|
||||
const char *ipmask;
|
||||
const char *intf;
|
||||
|
@ -70,6 +71,8 @@ struct nmrpd_args {
|
|||
};
|
||||
|
||||
int tftp_put(struct nmrpd_args *args);
|
||||
bool tftp_is_valid_filename(const char *filename);
|
||||
|
||||
int nmrp_do(struct nmrpd_args *args);
|
||||
|
||||
int select_fd(int fd, unsigned timeout);
|
||||
|
|
9
tftp.c
9
tftp.c
|
@ -86,7 +86,7 @@ static void pkt_mkwrq(char *pkt, const char *filename)
|
|||
size_t len = 2;
|
||||
|
||||
filename = leafname(filename);
|
||||
if (!is_netascii(filename) || strlen(filename) > 500) {
|
||||
if (!tftp_is_valid_filename(filename)) {
|
||||
fprintf(stderr, "Overlong/illegal filename; using 'firmware'.\n");
|
||||
filename = "firmware";
|
||||
} else if (!strcmp(filename, "-")) {
|
||||
|
@ -223,6 +223,11 @@ inline void sock_perror(const char *msg)
|
|||
}
|
||||
#endif
|
||||
|
||||
inline bool tftp_is_valid_filename(const char *filename)
|
||||
{
|
||||
return strlen(filename) <= 500 && is_netascii(filename);
|
||||
}
|
||||
|
||||
int tftp_put(struct nmrpd_args *args)
|
||||
{
|
||||
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 */
|
||||
timeout = 1;
|
||||
|
||||
pkt_mkwrq(tx, args->file_local);
|
||||
pkt_mkwrq(tx, args->file_remote);
|
||||
|
||||
do {
|
||||
if (!timeout && pkt_num(rx) == ACK) {
|
||||
|
|
Loading…
Add table
Reference in a new issue