[BusyBox 0001130]: arp applet

bugs at busybox.net bugs at busybox.net
Thu Jan 4 23:04:32 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 15:04 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.. 

---------------------------------------------------------------------- 
 bernhardf - 01-04-07 14:45  
---------------------------------------------------------------------- 
$ diff -u arp.c.09b arp.c
--- arp.c.09b	2007-01-04 23:35:02.000000000 +0100
+++ arp.c	2007-01-04 23:44:56.000000000 +0100
@@ -36,7 +36,11 @@
 #define	ARP_OPT_i (0x10)
 #define	ARP_OPT_a (0x20)
 #define	ARP_OPT_d (0x40)
+#if ENABLE_DEBUG
 #define	ARP_OPT_v (0x80)	/* debugging output flag        */
+#else
+#define	ARP_OPT_v (0)	/* debugging output flag        */
+#endif
 #define	ARP_OPT_n (0x100)	/* do not resolve addresses     */
 #define	ARP_OPT_D (0x200)	/* HW-address is devicename     */
 #define	ARP_OPT_s (0x400)
$ size arp.o.09b arp.o
   text	   data	    bss	    dec	    hex	filename
   3876	      0	     36	   3912	    f48	arp.o.09b
   3524	      0	     36	   3560	    de8	arp.o


So this wastes 350 bytes to achieve what? ;) 

---------------------------------------------------------------------- 
 bernhardf - 01-04-07 15:04  
---------------------------------------------------------------------- 
TODOs:
1) use compare_string_array().
see e.g. ip for examples.
candidates for compare_string_array:

arp_del()
arp_set()
arp_disp()

2) host[128]
has to die. See RESERVE_CONFIG_BUFFER

3) if (ap->input(0, host, &sa) < 0) {
is used way too often and sounds like it is (or should be) in libbb in one
form or another.

etc, etc. 

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                          
01-04-07 14:45  bernhardf      Note Added: 0001960                          
01-04-07 15:04  bernhardf      Note Added: 0001961                          
======================================================================




More information about the busybox-cvs mailing list