- printf("Uploading %s ... ", arg_filename);
- fflush(stdout);
- err = tftp_put(arg_filename, arg_ipaddr, arg_port);
- if (!err) {
- printf("OK\nWaiting for router to respond.\n");
- sock_set_rx_timeout(fd, arg_ul_timeout);
- expect = NMRP_C_CLOSE_REQ;
- } else if (err != -3) {
+
+ filename = msg_filename(&rx.msg);
+ if (filename) {
+ if (!args->file_remote) {
+ args->file_remote = filename;
+ }
+ printf("Received upload request: filename '%s'.\n", filename);
+ } else if (!args->file_remote) {
+ args->file_remote = leafname(args->file_local);
+ printf("Received upload request without filename.\n");
+ }
+
+ status = 0;
+
+ if (args->tftpcmd) {
+ printf("Executing '%s' ... \n", args->tftpcmd);
+ setenv("IP", inet_ntoa(ipaddr), 1);
+ setenv("PORT", lltostr(args->port, 10), 1);
+ setenv("MAC", mac_to_str(rx.eh.ether_shost), 1);
+ setenv("NETMASK", inet_ntoa(ipmask), 1);
+ //setenv("FILENAME", args->file_remote ? args->file_remote : "", 1);
+ status = system(args->tftpcmd);
+ }
+
+ if (!status && args->file_local) {
+ if (!autoip) {
+ status = is_valid_ip(sock, &ipaddr, &ipmask);
+ if (status < 0) {
+ goto out;
+ } else if (!status) {
+ printf("IP address of %s has changed. Please assign a "
+ "static ip to the interface.\n", args->intf);
+ tx.msg.code = NMRP_C_CLOSE_REQ;
+ break;
+ }
+ }
+
+ if (verbosity) {
+ printf("Using remote filename '%s'.\n",
+ args->file_remote);
+ }
+
+ if (!strcmp(args->file_local, "-")) {
+ printf("Uploading from stdin ... ");
+ } else {
+ printf("Uploading %s ... ", leafname(args->file_local));
+ }
+ fflush(stdout);
+ if (!(status = tftp_put(args))) {
+ printf("OK\n");
+ }
+
+ }
+
+ if (!status) {
+ printf("Waiting for remote to respond.\n");
+ upload_ok = 1;
+ ethsock_set_timeout(sock, args->ul_timeout);
+ tx.msg.code = NMRP_C_KEEP_ALIVE_REQ;
+ expect = NMRP_C_NONE;
+ } else if (status == -2) {
+ expect = NMRP_C_TFTP_UL_REQ;
+ } else {