From e3475838354d6bba414ab8cdc2211313ad29dc9d Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Tue, 4 Aug 2015 14:30:31 +0200 Subject: zcip: another code shrink function old new delta send_arp_request - 185 +185 zcip_main 1273 1272 -1 pick_nip 40 - -40 arp 185 - -185 ------------------------------------------------------------------------------ (add/remove: 1/2 grow/shrink: 0/1 up/down: 185/-226) Total: -41 bytes Signed-off-by: Denys Vlasenko --- networking/zcip.c | 307 ++++++++++++++++++++++++++---------------------------- 1 file changed, 148 insertions(+), 159 deletions(-) (limited to 'networking/zcip.c') diff --git a/networking/zcip.c b/networking/zcip.c index 5877a8317..a167c7e91 100644 --- a/networking/zcip.c +++ b/networking/zcip.c @@ -90,7 +90,7 @@ enum { struct globals { struct sockaddr iface_sockaddr; - struct ether_addr eth_addr; + struct ether_addr our_ethaddr; uint32_t localnet_ip; } FIX_ALIASING; #define G (*(struct globals*)&bb_common_bufsiz1) @@ -121,14 +121,14 @@ static const char *nip_to_a(uint32_t nip) /** * Broadcast an ARP packet. */ -static void arp( +static void send_arp_request( /* int op, - always ARPOP_REQUEST */ - /* const struct ether_addr *source_eth, - always &G.eth_addr */ + /* const struct ether_addr *source_eth, - always &G.our_ethaddr */ uint32_t source_nip, const struct ether_addr *target_eth, uint32_t target_nip) { enum { op = ARPOP_REQUEST }; -#define source_eth (&G.eth_addr) +#define source_eth (&G.our_ethaddr) struct arp_packet p; memset(&p, 0, sizeof(p)); @@ -205,35 +205,34 @@ static ALWAYS_INLINE unsigned random_delay_ms(unsigned secs) int zcip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int zcip_main(int argc UNUSED_PARAM, char **argv) { - int state; char *r_opt; const char *l_opt = "169.254.0.0"; + int state; + int nsent; unsigned opts; - // ugly trick, but I want these zeroed in one go + // Ugly trick, but I want these zeroed in one go struct { - const struct ether_addr null_addr; + const struct ether_addr null_ethaddr; struct ifreq ifr; uint32_t chosen_nip; + int conflicts; int timeout_ms; // must be signed - unsigned conflicts; - unsigned nsent; int verbose; } L; -#define null_addr (L.null_addr ) -#define ifr (L.ifr ) -#define chosen_nip (L.chosen_nip) -#define timeout_ms (L.timeout_ms) -#define conflicts (L.conflicts ) -#define nsent (L.nsent ) -#define verbose (L.verbose ) +#define null_ethaddr (L.null_ethaddr) +#define ifr (L.ifr ) +#define chosen_nip (L.chosen_nip ) +#define conflicts (L.conflicts ) +#define timeout_ms (L.timeout_ms ) +#define verbose (L.verbose ) memset(&L, 0, sizeof(L)); INIT_G(); #define FOREGROUND (opts & 1) #define QUIT (opts & 2) - // parse commandline: prog [options] ifname script + // Parse commandline: prog [options] ifname script // exactly 2 args; -v accumulates and implies -f opt_complementary = "=2:vv:vf"; opts = getopt32(argv, "fqr:l:v", &r_opt, &l_opt, &verbose); @@ -242,7 +241,7 @@ int zcip_main(int argc UNUSED_PARAM, char **argv) if (!FOREGROUND) bb_daemonize_or_rexec(0 /*was: DAEMON_CHDIR_ROOT*/, argv); #endif - // open an ARP socket + // Open an ARP socket // (need to do it before openlog to prevent openlog from taking // fd 3 (sock_fd==3)) xmove_fd(xsocket(AF_PACKET, SOCK_PACKET, htons(ETH_P_ARP)), sock_fd); @@ -282,26 +281,26 @@ int zcip_main(int argc UNUSED_PARAM, char **argv) xsetenv("interface", argv_intf); - // initialize the interface (modprobe, ifup, etc) + // Initialize the interface (modprobe, ifup, etc) if (run(argv, "init", 0)) return EXIT_FAILURE; - // initialize G.iface_sockaddr + // Initialize G.iface_sockaddr // G.iface_sockaddr is: { u16 sa_family; u8 sa_data[14]; } //memset(&G.iface_sockaddr, 0, sizeof(G.iface_sockaddr)); //TODO: are we leaving sa_family == 0 (AF_UNSPEC)?! safe_strncpy(G.iface_sockaddr.sa_data, argv_intf, sizeof(G.iface_sockaddr.sa_data)); - // bind to the interface's ARP socket + // Bind to the interface's ARP socket xbind(sock_fd, &G.iface_sockaddr, sizeof(G.iface_sockaddr)); - // get the interface's ethernet address + // Get the interface's ethernet address //memset(&ifr, 0, sizeof(ifr)); strncpy_IFNAMSIZ(ifr.ifr_name, argv_intf); xioctl(sock_fd, SIOCGIFHWADDR, &ifr); - memcpy(&G.eth_addr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN); + memcpy(&G.our_ethaddr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN); - // start with some stable ip address, either a function of + // Start with some stable ip address, either a function of // the hardware address or else the last address we used. // we are taking low-order four bytes, as top-order ones // aren't random enough. @@ -309,17 +308,14 @@ int zcip_main(int argc UNUSED_PARAM, char **argv) // depending on when we detect conflicts. { uint32_t t; - move_from_unaligned32(t, ((char *)&G.eth_addr + 2)); + move_from_unaligned32(t, ((char *)&G.our_ethaddr + 2)); srand(t); } - if (chosen_nip == 0) - chosen_nip = pick_nip(); - // FIXME cases to handle: // - zcip already running! // - link already has local address... just defend/update - // daemonize now; don't delay system startup + // Daemonize now; don't delay system startup if (!FOREGROUND) { #if BB_MMU bb_daemonize(0 /*was: DAEMON_CHDIR_ROOT*/); @@ -327,14 +323,14 @@ int zcip_main(int argc UNUSED_PARAM, char **argv) bb_info_msg("start, interface %s", argv_intf); } - // run the dynamic address negotiation protocol, + // Run the dynamic address negotiation protocol, // restarting after address conflicts: // - start with some address we want to try // - short random delay // - arp probes to see if another host uses it - // 00:04:e2:64:23:c2 > ff:ff:ff:ff:ff:ff, ARP (0x0806): arp who-has 169.254.194.171 tell 0.0.0.0 + // 00:04:e2:64:23:c2 > ff:ff:ff:ff:ff:ff: arp who-has 169.254.194.171 tell 0.0.0.0 // - arp announcements that we're claiming it - // 00:04:e2:64:23:c2 > ff:ff:ff:ff:ff:ff, ARP (0x0806): arp who-has 169.254.194.171 (00:04:e2:64:23:c2) tell 169.254.194.171 + // 00:04:e2:64:23:c2 > ff:ff:ff:ff:ff:ff: arp who-has 169.254.194.171 (00:04:e2:64:23:c2) tell 169.254.194.171 // - use it // - defend it, within limits // exit if: @@ -342,73 +338,73 @@ int zcip_main(int argc UNUSED_PARAM, char **argv) // run "