[macnocker] Only send data if interface is running

Submitted by Fabian Blaese on April 22, 2020, 4:47 p.m.

Details

Message ID 20200422164729.1146104-1-fabian@blaese.de
State Accepted
Headers show

Commit Message

Fabian Blaese April 22, 2020, 4:47 p.m.
If the macnocker client tries to send data, but the interface is not running,
this will result in error messages. However, an interface being down is a
valid state and should not result in error messages. So first check the current
state of the interface before trying to send data.

Fixes: #104
Signed-off-by: Fabian Bläse <fabian@blaese.de>
---
 macnockclient.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

Patch hide | download patch | download mbox

diff --git a/macnockclient.c b/macnockclient.c
index 0ef19a2..cdfba53 100644
--- a/macnockclient.c
+++ b/macnockclient.c
@@ -86,6 +86,19 @@  void macNockClient_run()
     {
         log_trace("[c] sending\n");
 
+        // check if bound interface is down, trying to send data would result in an error
+        if (!(ioctl(fd, SIOCGIFFLAGS, &ifr) == 0))
+        {
+            perror("[c] ERROR: Can't read Interface information");
+            goto retry;
+        }
+        if (!(ifr.ifr_flags & IFF_RUNNING))
+        {
+            // interface is not running, silently ignore
+            log_debug("[c] interface is not running\n");
+            goto retry;
+        }
+
         int sent = sendto(fd, nock, len, 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
         if (sent == -1)
         {
@@ -96,6 +109,7 @@  void macNockClient_run()
             perror("[c] ERROR: Can't send all data");
         }
 
+retry:
         usleep(1 * 1000 * 1000); // sleep 1 s
     }
 

Comments

Adrian Schmutzler April 22, 2020, 5:06 p.m.
Hallo,

habe den Code mit Erklärungen nachvollzogen und halte ihn für plausibel.

Vollständig reviewen kann ich ihn nicht, aber es sollte das tun, was versprochen wird und nichts kaputt machen.

Acked-by: Adrian Schmutzler <freifunk@adrianschmutzler.de>

Grüße

Adrian

> -----Original Message-----
> From: franken-dev [mailto:franken-dev-bounces@freifunk.net] On Behalf
> Of Fabian Bläse
> Sent: Mittwoch, 22. April 2020 18:47
> To: franken-dev@freifunk.net
> Subject: [PATCH macnocker] Only send data if interface is running
> 
> If the macnocker client tries to send data, but the interface is not running,
> this will result in error messages. However, an interface being down is a valid
> state and should not result in error messages. So first check the current state
> of the interface before trying to send data.
> 
> Fixes: #104
> Signed-off-by: Fabian Bläse <fabian@blaese.de>
> ---
>  macnockclient.c | 14 ++++++++++++++
>  1 file changed, 14 insertions(+)
> 
> diff --git a/macnockclient.c b/macnockclient.c index 0ef19a2..cdfba53 100644
> --- a/macnockclient.c
> +++ b/macnockclient.c
> @@ -86,6 +86,19 @@ void macNockClient_run()
>      {
>          log_trace("[c] sending\n");
> 
> +        // check if bound interface is down, trying to send data would result in
> an error
> +        if (!(ioctl(fd, SIOCGIFFLAGS, &ifr) == 0))
> +        {
> +            perror("[c] ERROR: Can't read Interface information");
> +            goto retry;
> +        }
> +        if (!(ifr.ifr_flags & IFF_RUNNING))
> +        {
> +            // interface is not running, silently ignore
> +            log_debug("[c] interface is not running\n");
> +            goto retry;
> +        }
> +
>          int sent = sendto(fd, nock, len, 0, (struct sockaddr *)&servaddr,
> sizeof(servaddr));
>          if (sent == -1)
>          {
> @@ -96,6 +109,7 @@ void macNockClient_run()
>              perror("[c] ERROR: Can't send all data");
>          }
> 
> +retry:
>          usleep(1 * 1000 * 1000); // sleep 1 s
>      }
> 
> --
> 2.26.2
Fabian Blaese April 23, 2020, 9:53 a.m.
applied.