Don't print messages on fake-packet receipt 32/head
authorJoseph C. Lehner <joseph.c.lehner@gmail.com>
Fri, 6 Mar 2020 17:43:03 +0000 (18:43 +0100)
committerJoseph C. Lehner <joseph.c.lehner@gmail.com>
Fri, 6 Mar 2020 17:43:03 +0000 (18:43 +0100)
nmrp.c

diff --git a/nmrp.c b/nmrp.c
index 9df5b307a09eb5db0184a774ea6cce50bfddd97f..4f262d9b6b7ff36c06c95ec4d49251d3e3ce436b 100644 (file)
--- a/nmrp.c
+++ b/nmrp.c
@@ -356,7 +356,7 @@ int nmrp_do(struct nmrpd_args *args)
        uint16_t region;
        char *filename;
        time_t beg;
-       int i, timeout, status, ulreqs, expect, upload_ok, autoip, kareqs;
+       int i, timeout, status, ulreqs, expect, upload_ok, autoip, ka_reqs, fake;
        struct ethsock *sock;
        struct ethsock_ip_undo *ip_undo = NULL;
        struct ethsock_arp_undo *arp_undo = NULL;
@@ -481,6 +481,7 @@ int nmrp_do(struct nmrpd_args *args)
 
        i = 0;
        upload_ok = 0;
+       fake = 0;
        timeout = args->blind ? 10 : NMRP_INITIAL_TIMEOUT;
        beg = time_monotonic();
 
@@ -517,7 +518,8 @@ int nmrp_do(struct nmrpd_args *args)
                                        // we're blind, so fake a response from the MAC specified by -m
                                        memcpy(rx.eh.ether_shost, dest, 6);
                                        msg_init(&rx.msg, NMRP_C_CONF_REQ);
-                                       printf("Faking one.");
+                                       printf("Continuing blindly.");
+                                       fake = 1;
                                        break;
                                }
                        }
@@ -534,7 +536,7 @@ int nmrp_do(struct nmrpd_args *args)
 
        expect = NMRP_C_CONF_REQ;
        ulreqs = 0;
-       kareqs = 0;
+       ka_reqs = 0;
 
        while (!g_interrupted) {
                if (expect != NMRP_C_NONE && rx.msg.code != expect) {
@@ -556,8 +558,10 @@ int nmrp_do(struct nmrpd_args *args)
                                msg_mkconfack(&tx.msg, ipaddr.s_addr, ipmask.s_addr, region);
                                expect = NMRP_C_TFTP_UL_REQ;
 
-                               printf("Received configuration request from %s.\n",
-                                               mac_to_str(rx.eh.ether_shost));
+                               if (!fake) {
+                                       printf("Received configuration request from %s.\n",
+                                                       mac_to_str(rx.eh.ether_shost));
+                               }
 
                                printf("Sending configuration: %s/%d.\n",
                                                args->ipaddr, bitcount(ipmask.s_addr));
@@ -588,7 +592,9 @@ int nmrp_do(struct nmrpd_args *args)
                                        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");
+                                       if (!fake) {
+                                               printf("Received upload request without filename.\n");
+                                       }
                                }
 
                                status = 0;
@@ -653,7 +659,7 @@ int nmrp_do(struct nmrpd_args *args)
                        case NMRP_C_KEEP_ALIVE_REQ:
                                tx.msg.code = NMRP_C_KEEP_ALIVE_ACK;
                                ethsock_set_timeout(sock, args->ul_timeout);
-                               printf("\rReceived keep-alive request (%d).  ", ++kareqs);
+                               printf("\rReceived keep-alive request (%d).  ", ++ka_reqs);
                                break;
                        case NMRP_C_CLOSE_REQ:
                                tx.msg.code = NMRP_C_CLOSE_ACK;
@@ -668,17 +674,13 @@ int nmrp_do(struct nmrpd_args *args)
                }
 
                if (tx.msg.code != NMRP_C_NONE) {
-                       if (pkt_send(sock, &tx) < 0) {
-                               goto out;
-                       }
-
-                       if (tx.msg.code == NMRP_C_CLOSE_REQ) {
+                       if (pkt_send(sock, &tx) != 0 || tx.msg.code == NMRP_C_CLOSE_REQ) {
                                goto out;
                        }
                }
 
                if (rx.msg.code == NMRP_C_CLOSE_REQ) {
-                       if (kareqs) {
+                       if (ka_reqs) {
                                printf("\n");
                        }
 
@@ -689,18 +691,21 @@ int nmrp_do(struct nmrpd_args *args)
                status = pkt_recv(sock, &rx);
                if (status) {
                        if (status == 2) {
-                               fprintf(stderr, "Timeout while waiting for %s. ",
-                                               msg_code_str(expect));
-                       }
+                               if (!args->blind) {
+                                       fprintf(stderr, "Timeout while waiting for %s.\n",
+                                                       msg_code_str(expect));
+                                       goto out;
+                               }
 
-                       if (!args->blind) {
-                               printf("\n");
-                               goto out;
-                       } else {
-                               printf("Faking response.\n");
+                               // fake response
                                msg_init(&rx.msg, expect);
                                memcpy(rx.eh.ether_shost, tx.eh.ether_dhost, 6);
+                               fake = 1;
+                       } else {
+                               goto out;
                        }
+               } else {
+                       fake = 0;
                }
 
                ethsock_set_timeout(sock, args->rx_timeout);