[BusyBox 0001130]: arp applet

bugs at busybox.net bugs at busybox.net
Thu Jan 4 22:41:30 UTC 2007


A NOTE has been added to this issue. 
====================================================================== 
http://busybox.net/bugs/view.php?id=1130 
====================================================================== 
Reported By:                espakman
Assigned To:                BusyBox
====================================================================== 
Project:                    BusyBox
Issue ID:                   1130
Category:                   New Features
Reproducibility:            N/A
Severity:                   feature
Priority:                   normal
Status:                     assigned
====================================================================== 
Date Submitted:             12-23-2006 12:35 PST
Last Modified:              01-04-2007 14:41 PST
====================================================================== 
Summary:                    arp applet
Description: 
Busybox currently doesn't contain an arp applet. Someone wrote an arp
applet a long time ago. I rediffed this version against bb 1.3.0 and it
applies cleanly against current SVN.
====================================================================== 

---------------------------------------------------------------------- 
 vda - 12-23-06 23:44  
---------------------------------------------------------------------- 
Unused #define:

+
+#define APPLET_NAME "arp"
+

Inconsistent style: 3-space indent AND 4-space indent in the same file:

+    (void) fclose(fp);
+
+    return 0;
+}
+
+int arp_main(int argc, char **argv)
+{
+   int what, i;
+
+   /* Initialize variables... */
+   if ((hw = get_hwtype(DFLT_HW)) == NULL) {
+      bb_error_msg("%s: hardware type not supported!\n", DFLT_HW);
+      return -1;
+   }

Please reformat to bbox style (indents by 1 tab).

 

---------------------------------------------------------------------- 
 espakman - 12-24-06 09:07  
---------------------------------------------------------------------- 
Second try, new patch attached. 

---------------------------------------------------------------------- 
 espakman - 12-24-06 10:42  
---------------------------------------------------------------------- 
Now run through indent, to be fully bbox style complient. 

---------------------------------------------------------------------- 
 vda - 12-25-06 20:10  
---------------------------------------------------------------------- 
Reviewed the patch.

+/* This structure defines protocol families and their handlers. */
+struct aftype {
+        char *name;
+        char *title;
+        int af;
+        int alen;

Use tabs for indentation.

+#define arp_trivial_usage \
+        "[-evn] [-H type] [-i if] -a [hostname]\n" \
+        "\tarp [-v] [-i if] -d hostname [pub]\n" \
+        "\tarp [-v] [-H type] [-i if] -s hostname hw_addr [temp]\n" \

Follow usage.h rules which are written in the comment on top of that
file.

+#include <sys/ioctl.h>
+#include <sys/signal.h>
+#include <sys/time.h>
+
+#include <errno.h>
+#include <netdb.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include <unistd.h>
+
+#include <arpa/inet.h>
+#include <net/if.h>
+#include <net/if_arp.h>
+#include <netinet/ether.h>
+#include <netpacket/packet.h>
+
+#include "busybox.h"

Most of these standard includes are already included by #include
"busybox.h"

+extern struct aftype *get_aftype(const char *name);
+extern struct hwtype *get_hwntype(int type);
+extern struct hwtype *get_hwtype(const char *name);
+
+int opt_n = 0;
+int opt_v = 0;

These functions and variables should be static.

+               /* Skip trailing whitespace. */
+               if (*ptr != '\0') {
+                       while (*ptr == ' ' || *ptr == '\t')
+                               ptr++;
+               }

We have skip_whitespace, use that.

+       memcpy((char *) &req.arp_pa, (char *) &sa, sizeof(struct
sockaddr));

Casts are superfluous here.

+                       bb_error_msg("args=%s\n", *args);

bb_[ph]error_msgXXX print trailing '\n' by themself.

+               perror("SIOCSARP");

use bb_perror_msg

+                       ("Address                  HWtype  HWaddress      
    Flags Mask            Iface\n");

Maybe use tabs?

+static int arp_show(char *name)
+{
+       char host[100];
+       struct sockaddr sa;
+       char ip[100];
+       char hwa[100];
+       char mask[100];
+       char line[200];
+       char dev[100];
+       int type, flags;
+       FILE *fp;
+       char *hostname;
+       int num, entries = 0, showed = 0;
+
....
+       /* Bypass header -- read until newline */
+       if (fgets(line, sizeof(line), fp) != (char *) NULL) {

use xmalloc_fget{s,line} instead of fixed-sized buffers. It saves memory.

+       (void) fclose(fp);

Don't think we need this cast

+                               bb_error_msg("%s: unknown address
family.\n", optarg);

Trailing period (and '\n') is not needed 

---------------------------------------------------------------------- 
 espakman - 12-26-06 05:02  
---------------------------------------------------------------------- 
> +/* This structure defines protocol families and their handlers. */
> +struct aftype {
> + char *name;
> + char *title;
> + int af;
> + int alen;
>
> Use tabs for indentation.

Fixed

> +#define arp_trivial_usage \
> + "[-evn] [-H type] [-i if] -a [hostname]\n" \
> + "\tarp [-v] [-i if] -d hostname [pub]\n" \
> + "\tarp [-v] [-H type] [-i if] -s hostname hw_addr [temp]\n" \
> 
> Follow usage.h rules which are written in the comment on top of that
file.

Fixed

> +#include <sys/ioctl.h>
> +#include <sys/signal.h>
> ....
> +
> +#include "busybox.h"
> 
> Most of these standard includes are already included by #include
"busybox.h"

Fixed

> +extern struct aftype *get_aftype(const char *name);
> +extern struct hwtype *get_hwntype(int type);
> +extern struct hwtype *get_hwtype(const char *name);
> +
> +int opt_n = 0;
> +int opt_v = 0;
> 
> These functions and variables should be static.
>
> + /* Skip trailing whitespace. */
> + if (*ptr != '\0') {
> + while (*ptr == ' ' || *ptr == '\t')
> + ptr++;
> + }
> 
> We have skip_whitespace, use that.
> 
> + memcpy((char *) &req.arp_pa, (char *) &sa, sizeof(struct sockaddr));
> 
> Casts are superfluous here.

I'm more of a user than a programmer, this goes beyond my capabilities to
change or comment on...

> + bb_error_msg("args=%s\n", *args);
> 
> bb_[ph]error_msgXXX print trailing '\n' by themself.
>
> + perror("SIOCSARP");
> 
> use bb_perror_msg

Fixed

> + ("Address HWtype HWaddress Flags Mask Iface\n");
> 
> Maybe use tabs?

That breaks formatting unfortuanatly 

> +static int arp_show(char *name)
> +{
> + char host[100];
> + struct sockaddr sa;
> + char ip[100];
> + char hwa[100];
> + char mask[100];
> + char line[200];
> + char dev[100];
> + int type, flags;
> + FILE *fp;
> + char *hostname;
> + int num, entries = 0, showed = 0;
> +
> ....
> + /* Bypass header -- read until newline */
> + if (fgets(line, sizeof(line), fp) != (char *) NULL) {
> 
> use xmalloc_fget{s,line} instead of fixed-sized buffers. It saves
memory.

This also goes beyond my capabilities, I need a bit of help on this one
too.

> + (void) fclose(fp);
> 
> Don't think we need this cast

Removed both instances

> + bb_error_msg("%s: unknown address family.\n", optarg);
> 
> Trailing period (and '\n') is not needed

Fixed 

---------------------------------------------------------------------- 
 espakman - 12-26-06 06:55  
---------------------------------------------------------------------- 
A few more fixes:

> + memcpy((char *) &req.arp_pa, (char *) &sa, sizeof(struct sockaddr));
>
> Casts are superfluous here.

Fixed on all instances 

---------------------------------------------------------------------- 
 vda - 12-26-06 10:05  
---------------------------------------------------------------------- 
Thanks for your fixes. I feel it still is a bit rough on edges, so

svn-arp5 is arp5 diff rediffed against current svn.
svn-arp6 is next revision of it with my changes.
arp5-arp6 is diff between arp5 and arp6.

Review notes (examine arp5-arp6.patch to see):

Never place extern decls in .c file. You had a bug there (functions were
returning const pointers but your extern decl in arp.c did not have
const).

Use static for local data/functions: _static_ int opt_n;

getargs() is too big (use skip_whitespace() to trim it down) and I think
it has bug in handling of \", \' - not fixed in arp6.

Many superfluous casts.


-               if (!(xhw = get_hwntype(ifr.ifr_hwaddr.sa_family))
-                       || (xhw->print == 0)) {
+               xhw = get_hwntype(ifr.ifr_hwaddr.sa_family);
+               if (!xhw || !xhw->print) {

Do not use assignment-inside-if - it is hard to read (for no gain in code
size at all).


while ((i = getopt(argc, argv, "A:H:adfp:nsei:t:vh?DNV")) != EOF)

Replace with getopt32. There are many examples in the tree (say,
od_bloaty.c)


-               if ((hw = get_hwtype(DFLT_HW)) == NULL) {
-                       bb_error_msg("%s: hardware type not supported!",
DFLT_HW);
-                       return (-1);
-               }
+               hw = get_hwtype(DFLT_HW);
+               if (!hw)
+                       bb_error_msg_and_die("%s: hardware type not
supported", DFLT_HW);

Die on errors, do not bother returning/closing files/freeing memory if
error is fatal anyway. 

---------------------------------------------------------------------- 
 espakman - 12-27-06 14:05  
---------------------------------------------------------------------- 
Thanks for your fixes and comments.

I decided to remove the /etc/ethers handling all together for a few
reasons:
both getargs() and arp_file() add considerable size.
I think that most users, if any, won't use or need /etc/ethers with arp
anyway.
It doesn't seem to work reliable, at least not in my tests.

The arp.c file still needs getopt -> getopt32, but as non-programmer it
will cost me a bit more time...

New patch (against latest SVN) attached 

---------------------------------------------------------------------- 
 espakman - 01-04-07 13:23  
---------------------------------------------------------------------- 
With the help of a friend getopt32 is implemented. Also some more
cleanups.
New patch (busybox-arp8.patch) attached. 

---------------------------------------------------------------------- 
 bernhardf - 01-04-07 14:36  
---------------------------------------------------------------------- 
It's still way to huge, imo.

$ size arp.o.08a arp.o.09b 
   text	   data	    bss	    dec	    hex	filename
   3940	      0	     48	   3988	    f94	arp.o.08a
   3876	      0	     36	   3912	    f48	arp.o.09b 

---------------------------------------------------------------------- 
 bernhardf - 01-04-07 14:41  
---------------------------------------------------------------------- 
wth are all these -v stuff ment to do in there?
Please explain.. 

Issue History 
Date Modified   Username       Field                    Change               
====================================================================== 
12-23-06 12:35  espakman       New Issue                                    
12-23-06 12:35  espakman       Status                   new => assigned     
12-23-06 12:35  espakman       Assigned To               => BusyBox         
12-23-06 12:35  espakman       File Added: busybox-arp.patch                    
12-23-06 23:44  vda            Note Added: 0001931                          
12-23-06 23:44  vda            Note Edited: 0001931                         
12-24-06 09:06  espakman       File Added: busybox-arp2.patch                   

12-24-06 09:07  espakman       Note Added: 0001932                          
12-24-06 10:41  espakman       File Added: busybox-arp3.patch                   

12-24-06 10:42  espakman       Note Added: 0001933                          
12-25-06 20:10  vda            Note Added: 0001934                          
12-26-06 05:02  espakman       Note Added: 0001935                          
12-26-06 05:02  espakman       File Added: busybox-arp4.patch                   

12-26-06 06:54  espakman       File Added: busybox-arp5.patch                   

12-26-06 06:55  espakman       Note Added: 0001936                          
12-26-06 09:52  vda            File Added: svn-arp5.patch                    
12-26-06 09:52  vda            File Added: svn-arp6.patch                    
12-26-06 09:52  vda            File Added: arp5-arp6.patch                    
12-26-06 10:05  vda            Note Added: 0001937                          
12-27-06 13:54  espakman       File Added: busybox-arp7.patch                   

12-27-06 14:05  espakman       Note Added: 0001940                          
01-04-07 13:23  espakman       File Added: busybox-arp8.patch                   

01-04-07 13:23  espakman       Note Added: 0001957                          
01-04-07 14:36  bernhardf      File Added: busybox.applet-arp.09b.diff          
         
01-04-07 14:36  bernhardf      Note Added: 0001958                          
01-04-07 14:41  bernhardf      Note Added: 0001959                          
======================================================================




More information about the busybox-cvs mailing list