busybox/networking/libiproute/ll_addr.c
Denis Vlasenko efb545b9bd optimize 16- and 32-bit moves
function                                             old     new   delta
udhcpd_main                                         1239    1257     +18
udhcp_add_simple_option                               93      92      -1
buffer_read_le_u32                                    19      18      -1
unpack_gz_stream_with_info                           526     520      -6
dnsd_main                                           1470    1463      -7
udhcp_run_script                                    1208    1186     -22
send_ACK                                             255     229     -26
arping_main                                         1661    1623     -38
send_offer                                           470     428     -42
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/8 up/down: 18/-143)          Total: -125 bytes
2008-12-08 22:56:18 +00:00

80 lines
1.5 KiB
C

/* vi: set sw=4 ts=4: */
/*
* ll_addr.c
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*
* Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
*/
#include <net/if_arp.h>
#include "libbb.h"
#include "rt_names.h"
#include "utils.h"
const char *ll_addr_n2a(unsigned char *addr, int alen, int type, char *buf, int blen)
{
int i;
int l;
if (alen == 4 &&
(type == ARPHRD_TUNNEL || type == ARPHRD_SIT || type == ARPHRD_IPGRE)) {
return inet_ntop(AF_INET, addr, buf, blen);
}
l = 0;
for (i=0; i<alen; i++) {
if (i==0) {
snprintf(buf+l, blen, ":%02x"+1, addr[i]);
blen -= 2;
l += 2;
} else {
snprintf(buf+l, blen, ":%02x", addr[i]);
blen -= 3;
l += 3;
}
}
return buf;
}
int ll_addr_a2n(unsigned char *lladdr, int len, char *arg)
{
int i;
if (strchr(arg, '.')) {
inet_prefix pfx;
if (get_addr_1(&pfx, arg, AF_INET)) {
bb_error_msg("\"%s\" is invalid lladdr", arg);
return -1;
}
if (len < 4) {
return -1;
}
memcpy(lladdr, pfx.data, 4);
return 4;
}
for (i = 0; i < len; i++) {
int temp;
char *cp = strchr(arg, ':');
if (cp) {
*cp = 0;
cp++;
}
if (sscanf(arg, "%x", &temp) != 1 || (temp < 0 || temp > 255)) {
bb_error_msg("\"%s\" is invalid lladdr", arg);
return -1;
}
lladdr[i] = temp;
if (!cp) {
break;
}
arg = cp;
}
return i+1;
}