diff options
| author | Bartosz Golaszewski <bartekgola@gmail.com> | 2013-07-25 04:39:04 +0200 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2013-07-25 04:39:04 +0200 |
| commit | c19be75d57ff42dee54b53e21b3eb4723b8cf243 (patch) | |
| tree | 7fc65e75d2c91ee1021e327aaa4e1ba0e81ccb15 | |
| parent | b855460adcda8dfb18ac36755f128ce32088ae87 (diff) | |
| download | busybox-w32-c19be75d57ff42dee54b53e21b3eb4723b8cf243.tar.gz busybox-w32-c19be75d57ff42dee54b53e21b3eb4723b8cf243.tar.bz2 busybox-w32-c19be75d57ff42dee54b53e21b3eb4723b8cf243.zip | |
networking: code shrink
function old new delta
in_ether - 124 +124
hexchar2int 42 - -42
ifconfig_main 1237 1106 -131
ether_input 141 - -141
------------------------------------------------------------------------------
(add/remove: 2/2 grow/shrink: 0/1 up/down: 124/-314) Total: -190 bytes
Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
| -rw-r--r-- | TODO | 2 | ||||
| -rw-r--r-- | include/libbb.h | 1 | ||||
| -rw-r--r-- | libbb/in_ether.c | 58 | ||||
| -rw-r--r-- | networking/ifconfig.c | 43 | ||||
| -rw-r--r-- | networking/interface.c | 55 |
5 files changed, 60 insertions, 99 deletions
| @@ -229,8 +229,6 @@ Minor stuff: | |||
| 229 | See grep -r strtod | 229 | See grep -r strtod |
| 230 | Alot of duplication that wants cleanup. | 230 | Alot of duplication that wants cleanup. |
| 231 | --- | 231 | --- |
| 232 | in_ether duplicated in network/{interface,ifconfig}.c | ||
| 233 | --- | ||
| 234 | unify progress_meter. wget, flash_eraseall, pipe_progress, fbsplash, setfiles. | 232 | unify progress_meter. wget, flash_eraseall, pipe_progress, fbsplash, setfiles. |
| 235 | --- | 233 | --- |
| 236 | support start-stop-daemon -d <chdir-path> | 234 | support start-stop-daemon -d <chdir-path> |
diff --git a/include/libbb.h b/include/libbb.h index f22c1252b..83e9b5fb9 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
| @@ -1140,6 +1140,7 @@ struct hwtype { | |||
| 1140 | }; | 1140 | }; |
| 1141 | extern smallint interface_opt_a; | 1141 | extern smallint interface_opt_a; |
| 1142 | int display_interfaces(char *ifname) FAST_FUNC; | 1142 | int display_interfaces(char *ifname) FAST_FUNC; |
| 1143 | int in_ether(const char *bufp, struct sockaddr *sap) FAST_FUNC; | ||
| 1143 | #if ENABLE_FEATURE_HWIB | 1144 | #if ENABLE_FEATURE_HWIB |
| 1144 | int in_ib(const char *bufp, struct sockaddr *sap) FAST_FUNC; | 1145 | int in_ib(const char *bufp, struct sockaddr *sap) FAST_FUNC; |
| 1145 | #else | 1146 | #else |
diff --git a/libbb/in_ether.c b/libbb/in_ether.c new file mode 100644 index 000000000..dadadbafe --- /dev/null +++ b/libbb/in_ether.c | |||
| @@ -0,0 +1,58 @@ | |||
| 1 | /* vi: set sw=4 ts=4: */ | ||
| 2 | /* | ||
| 3 | * Utility routines. | ||
| 4 | */ | ||
| 5 | |||
| 6 | //kbuild:lib-$(CONFIG_IFCONFIG) += in_ether.o | ||
| 7 | //kbuild:lib-$(CONFIG_IFENSLAVE) += in_ether.o | ||
| 8 | |||
| 9 | #include "libbb.h" | ||
| 10 | #include <net/if_arp.h> | ||
| 11 | #include <net/ethernet.h> | ||
| 12 | |||
| 13 | /* Convert Ethernet address from "XX[:]XX[:]XX[:]XX[:]XX[:]XX" to sockaddr. | ||
| 14 | * Return nonzero on error. | ||
| 15 | */ | ||
| 16 | int FAST_FUNC in_ether(const char *bufp, struct sockaddr *sap) | ||
| 17 | { | ||
| 18 | char *ptr; | ||
| 19 | int i, j; | ||
| 20 | unsigned char val; | ||
| 21 | unsigned char c; | ||
| 22 | |||
| 23 | sap->sa_family = ARPHRD_ETHER; | ||
| 24 | ptr = (char *) sap->sa_data; | ||
| 25 | |||
| 26 | i = ETH_ALEN; | ||
| 27 | goto first; | ||
| 28 | do { | ||
| 29 | /* We might get a semicolon here */ | ||
| 30 | if (*bufp == ':') | ||
| 31 | bufp++; | ||
| 32 | first: | ||
| 33 | j = val = 0; | ||
| 34 | do { | ||
| 35 | c = *bufp; | ||
| 36 | if (((unsigned char)(c - '0')) <= 9) { | ||
| 37 | c -= '0'; | ||
| 38 | } else if ((unsigned char)((c|0x20) - 'a') <= 5) { | ||
| 39 | c = (unsigned char)((c|0x20) - 'a') + 10; | ||
| 40 | } else { | ||
| 41 | if (j && (c == ':' || c == '\0')) | ||
| 42 | /* One-digit byte: __:X:__ */ | ||
| 43 | break; | ||
| 44 | return -1; | ||
| 45 | } | ||
| 46 | ++bufp; | ||
| 47 | val <<= 4; | ||
| 48 | val += c; | ||
| 49 | j ^= 1; | ||
| 50 | } while (j); | ||
| 51 | |||
| 52 | *ptr++ = val; | ||
| 53 | |||
| 54 | } while (--i); | ||
| 55 | |||
| 56 | /* Error if we aren't at end of string */ | ||
| 57 | return *bufp; | ||
| 58 | } | ||
diff --git a/networking/ifconfig.c b/networking/ifconfig.c index 782374b35..999305aff 100644 --- a/networking/ifconfig.c +++ b/networking/ifconfig.c | |||
| @@ -265,49 +265,6 @@ static const struct options OptArray[] = { | |||
| 265 | { NULL, 0, ARG_HOSTNAME, (IFF_UP | IFF_RUNNING) } | 265 | { NULL, 0, ARG_HOSTNAME, (IFF_UP | IFF_RUNNING) } |
| 266 | }; | 266 | }; |
| 267 | 267 | ||
| 268 | #if ENABLE_FEATURE_IFCONFIG_HW | ||
| 269 | /* Input an Ethernet address and convert to binary. */ | ||
| 270 | static int in_ether(const char *bufp, struct sockaddr *sap) | ||
| 271 | { | ||
| 272 | char *ptr; | ||
| 273 | int i, j; | ||
| 274 | unsigned char val; | ||
| 275 | unsigned char c; | ||
| 276 | |||
| 277 | sap->sa_family = ARPHRD_ETHER; | ||
| 278 | ptr = (char *) sap->sa_data; | ||
| 279 | |||
| 280 | i = 0; | ||
| 281 | do { | ||
| 282 | j = val = 0; | ||
| 283 | |||
| 284 | /* We might get a semicolon here - not required. */ | ||
| 285 | if (i && (*bufp == ':')) { | ||
| 286 | bufp++; | ||
| 287 | } | ||
| 288 | |||
| 289 | do { | ||
| 290 | c = *bufp; | ||
| 291 | if (((unsigned char)(c - '0')) <= 9) { | ||
| 292 | c -= '0'; | ||
| 293 | } else if ((unsigned char)((c|0x20) - 'a') <= 5) { | ||
| 294 | c = (unsigned char)((c|0x20) - 'a') + 10; | ||
| 295 | } else if (j && (c == ':' || c == 0)) { | ||
| 296 | break; | ||
| 297 | } else { | ||
| 298 | return -1; | ||
| 299 | } | ||
| 300 | ++bufp; | ||
| 301 | val <<= 4; | ||
| 302 | val += c; | ||
| 303 | } while (++j < 2); | ||
| 304 | *ptr++ = val; | ||
| 305 | } while (++i < ETH_ALEN); | ||
| 306 | |||
| 307 | return *bufp; /* Error if we don't end at end of string. */ | ||
| 308 | } | ||
| 309 | #endif | ||
| 310 | |||
| 311 | int ifconfig_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 268 | int ifconfig_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 312 | int ifconfig_main(int argc UNUSED_PARAM, char **argv) | 269 | int ifconfig_main(int argc UNUSED_PARAM, char **argv) |
| 313 | { | 270 | { |
diff --git a/networking/interface.c b/networking/interface.c index 9ae8b3f03..3dc5b3640 100644 --- a/networking/interface.c +++ b/networking/interface.c | |||
| @@ -722,68 +722,15 @@ static char* FAST_FUNC ether_print(unsigned char *ptr) | |||
| 722 | return buff; | 722 | return buff; |
| 723 | } | 723 | } |
| 724 | 724 | ||
| 725 | static int FAST_FUNC ether_input(const char *bufp, struct sockaddr *sap); | ||
| 726 | |||
| 727 | static const struct hwtype ether_hwtype = { | 725 | static const struct hwtype ether_hwtype = { |
| 728 | .name = "ether", | 726 | .name = "ether", |
| 729 | .title = "Ethernet", | 727 | .title = "Ethernet", |
| 730 | .type = ARPHRD_ETHER, | 728 | .type = ARPHRD_ETHER, |
| 731 | .alen = ETH_ALEN, | 729 | .alen = ETH_ALEN, |
| 732 | .print = ether_print, | 730 | .print = ether_print, |
| 733 | .input = ether_input | 731 | .input = in_ether |
| 734 | }; | 732 | }; |
| 735 | 733 | ||
| 736 | static unsigned hexchar2int(char c) | ||
| 737 | { | ||
| 738 | if (isdigit(c)) | ||
| 739 | return c - '0'; | ||
| 740 | c &= ~0x20; /* a -> A */ | ||
| 741 | if ((unsigned)(c - 'A') <= 5) | ||
| 742 | return c - ('A' - 10); | ||
| 743 | return ~0U; | ||
| 744 | } | ||
| 745 | |||
| 746 | /* Input an Ethernet address and convert to binary. */ | ||
| 747 | static int FAST_FUNC ether_input(const char *bufp, struct sockaddr *sap) | ||
| 748 | { | ||
| 749 | unsigned char *ptr; | ||
| 750 | char c; | ||
| 751 | int i; | ||
| 752 | unsigned val; | ||
| 753 | |||
| 754 | sap->sa_family = ether_hwtype.type; | ||
| 755 | ptr = (unsigned char*) sap->sa_data; | ||
| 756 | |||
| 757 | i = 0; | ||
| 758 | while ((*bufp != '\0') && (i < ETH_ALEN)) { | ||
| 759 | val = hexchar2int(*bufp++) * 0x10; | ||
| 760 | if (val > 0xff) { | ||
| 761 | errno = EINVAL; | ||
| 762 | return -1; | ||
| 763 | } | ||
| 764 | c = *bufp; | ||
| 765 | if (c == ':' || c == 0) | ||
| 766 | val >>= 4; | ||
| 767 | else { | ||
| 768 | val |= hexchar2int(c); | ||
| 769 | if (val > 0xff) { | ||
| 770 | errno = EINVAL; | ||
| 771 | return -1; | ||
| 772 | } | ||
| 773 | } | ||
| 774 | if (c != 0) | ||
| 775 | bufp++; | ||
| 776 | *ptr++ = (unsigned char) val; | ||
| 777 | i++; | ||
| 778 | |||
| 779 | /* We might get a semicolon here - not required. */ | ||
| 780 | if (*bufp == ':') { | ||
| 781 | bufp++; | ||
| 782 | } | ||
| 783 | } | ||
| 784 | return 0; | ||
| 785 | } | ||
| 786 | |||
| 787 | static const struct hwtype ppp_hwtype = { | 734 | static const struct hwtype ppp_hwtype = { |
| 788 | .name = "ppp", | 735 | .name = "ppp", |
| 789 | .title = "Point-to-Point Protocol", | 736 | .title = "Point-to-Point Protocol", |
