From 4ccf7d99a5eb7a7ec474241985e63226fd004a03 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Mon, 30 Dec 2019 15:53:35 +0000 Subject: [PATCH] added hawk_uchars_to_bin() and bchars_to_bin() --- hawk/lib/gem-nwif2.c | 102 ++++++++++++++++++++++++++++--------------- hawk/lib/hawk-gem.h | 7 ++- hawk/lib/hawk-utl.h | 22 ++++++++++ hawk/lib/mod-sys.c | 36 +++++++-------- hawk/lib/utl-str.c | 63 ++++++++++++++++++++++++++ 5 files changed, 173 insertions(+), 57 deletions(-) diff --git a/hawk/lib/gem-nwif2.c b/hawk/lib/gem-nwif2.c index 376724f9..b2d32abb 100644 --- a/hawk/lib/gem-nwif2.c +++ b/hawk/lib/gem-nwif2.c @@ -24,10 +24,12 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#if 0 +#if 1 #include "hawk-prv.h" +#include "utl-skad.h" #include + #if defined(_WIN32) # include # include @@ -85,6 +87,37 @@ # endif #endif +#if defined(AF_INET6) +static int prefix_to_in6 (int prefix, struct in6_addr* in6) +{ + int i; + + if (prefix < 0 || prefix > HAWK_SIZEOF(*in6) * 8) return -1; + + HAWK_MEMSET (in6, 0, HAWK_SIZEOF(*in6)); + for (i = 0; ; i++) + { + if (prefix > 8) + { + in6->s6_addr[i] = 0xFF; + prefix -= 8; + } + else + { + in6->s6_addr[i] = 0xFF << (8 - prefix); + break; + } + + } + + return 0; +} +#endif + +static HAWK_INLINE void copy_to_skad (struct sockaddr* sa, hawk_skad_t* skad) +{ + HAWK_MEMCPY (skad, sa, (HAWK_SIZEOF(*sa) > HAWK_SIZEOF(*skad)? HAWK_SIZEOF(*skad): HAWK_SIZEOF(*sa))); +} /* #if defined(HAVE_NET_IF_DL_H) @@ -226,7 +259,7 @@ int hawk_gem_getifcfg (hawk_gem_t* gem, hawk_ifcfg_t* cfg) goto oops; #endif - if (get_nwifs (cfg->mmgr, s4, AF_UNSPEC, &nwifs) <= -1) goto oops; + if (get_nwifs(cfg->mmgr, s4, AF_UNSPEC, &nwifs) <= -1) goto oops; ifr = nwifs.ptr; for (i = 0; i < nwifs.len; i++, ifr++) @@ -254,17 +287,17 @@ int hawk_gem_getifcfg (hawk_gem_t* gem, hawk_ifcfg_t* cfg) hawk_skadtonwad (&ifr->lifr_addr, &head->addr); hawk_copy_bcstr (ifrbuf.lifr_name, HAWK_SIZEOF(ifrbuf.lifr_name), ifr->lifr_name); - if (ioctl (s, SIOCGLIFFLAGS, &ifrbuf) <= -1) goto oops; + if (ioctl(s, SIOCGLIFFLAGS, &ifrbuf) <= -1) goto oops; if (ifrbuf.lifr_flags & IFF_UP) head->flags |= HAWK_IFCFG_UP; if (ifrbuf.lifr_flags & IFF_BROADCAST) { - if (ioctl (s, SIOCGLIFBRDADDR, &ifrbuf) <= -1) goto oops; + if (ioctl(s, SIOCGLIFBRDADDR, &ifrbuf) <= -1) goto oops; hawk_skadtonwad (&ifrbuf.lifr_addr, &head->bcast); head->flags |= HAWK_IFCFG_BCAST; } if (ifrbuf.lifr_flags & IFF_POINTOPOINT) { - if (ioctl (s, SIOCGLIFDSTADDR, &ifrbuf) <= -1) goto oops; + if (ioctl(s, SIOCGLIFDSTADDR, &ifrbuf) <= -1) goto oops; hawk_skadtonwad (&ifrbuf.lifr_addr, &head->ptop); head->flags |= HAWK_IFCFG_PTOP; } @@ -359,19 +392,19 @@ static void read_proc_net_if_inet6 (hawk_gem_t* gem, hawk_ifcfg_t* cfg, struct i if (index == cfg->index) { int ti; + hawk_skad_alt_t* skad; - if (hawk_mbshextobin(tok[0].ptr, tok[0].len, cfg->addr.u.in6.addr.value, HAWK_COUNTOF(cfg->addr.u.in6.addr.value)) <= -1) break; - + skad = (hawk_skad_alt_t*)&cfg->addr; + if (hawk_bchars_to_bin(tok[0].ptr, tok[0].len, (hawk_uint8_t*)&skad->in6.sin6_addr, HAWK_SIZEOF(skad->in6.sin6_addr)) <= -1) break; /* tok[3] is the scope type, not the actual scope. * i leave this code for reference only. - cfg->addr.u.in6.scope = hawk_bchars_to_int(tok[3].ptr, tok[3].len, 16, HAWK_NULL, 1); */ - - cfg->addr.type = HAWK_NWAD_IN6; + skad->in6.sin6_scope_id = hawk_bchars_to_int(tok[3].ptr, tok[3].len, 16, HAWK_NULL, 1); */ + skad->in6.sin6_family = HAWK_AF_INET6; + skad = (hawk_skad_alt_t*)&cfg->mask; ti = hawk_bchars_to_int(tok[2].ptr, tok[0].len, 16, HAWK_NULL, 1); - hawk_prefixtoip6ad (ti, &cfg->mask.u.in6.addr); - - cfg->mask.type = HAWK_NWAD_IN6; + prefix_to_in6 (ti, &skad->in6.sin6_addr); + skad->in6.sin6_family = HAWK_AF_INET6; goto done; } } @@ -386,19 +419,21 @@ static void read_proc_net_if_inet6 (hawk_gem_t* gem, hawk_ifcfg_t* cfg, struct i static int get_ifcfg (hawk_gem_t* gem, int s, hawk_ifcfg_t* cfg, struct ifreq* ifr) { #if defined(_WIN32) + hawk_gem_seterrnum (gem, HAWK_NULL, HAWK_ENOIMPL); return -1; #elif defined(__OS2__) + hawk_gem_seterrnum (gem, HAWK_NULL, HAWK_ENOIMPL); return -1; #elif defined(__DOS__) + hawk_gem_seterrnum (gem, HAWK_NULL, HAWK_ENOIMPL); return -1; -#elif defined(SIOCGLIFADDR) && defined(SIOCGLIFINDEX) && \ - defined(HAVE_STRUCT_LIFCONF) && defined(HAVE_STRUCT_LIFREQ) +#elif defined(SIOCGLIFADDR) && defined(SIOCGLIFINDEX) && defined(HAVE_STRUCT_LIFCONF) && defined(HAVE_STRUCT_LIFREQ) /* opensolaris */ struct lifreq lifrbuf; hawk_oow_t ml, wl; @@ -500,12 +535,12 @@ static int get_ifcfg (hawk_gem_t* gem, int s, hawk_ifcfg_t* cfg, struct ifreq* i HAWK_MEMSET (&iflrbuf, 0, HAWK_SIZEOF(iflrbuf)); hawk_copy_bcstr (iflrbuf.iflr_name, HAWK_SIZEOF(iflrbuf.iflr_name), ifr->ifr_name); - if (ioctl (s, SIOCGLIFADDR, &iflrbuf) >= 0) + if (ioctl(s, SIOCGLIFADDR, &iflrbuf) >= 0) { hawk_skadtonwad (&iflrbuf.addr, &cfg->addr); cfg->mask.type = HAWK_NWAD_IN6; - hawk_prefixtoip6ad (iflrbuf.prefixlen, &cfg->mask.u.in6.addr); + prefix_to_in6 (iflrbuf.prefixlen, &cfg->mask.u.in6.addr); if (cfg->flags & HAWK_IFCFG_PTOP) hawk_skadtonwad (&iflrbuf.dstaddr, &cfg->ptop); @@ -513,20 +548,19 @@ static int get_ifcfg (hawk_gem_t* gem, int s, hawk_ifcfg_t* cfg, struct ifreq* i } else { - if (ioctl (s, SIOCGIFADDR, ifr) >= 0) + if (ioctl(s, SIOCGIFADDR, ifr) >= 0) hawk_skadtonwad (&ifr->ifr_addr, &cfg->addr); - if (ioctl (s, SIOCGIFNETMASK, ifr) >= 0) + if (ioctl(s, SIOCGIFNETMASK, ifr) >= 0) hawk_skadtonwad (&ifr->ifr_addr, &cfg->mask); - if ((cfg->flags & HAWK_IFCFG_BCAST) && - ioctl (s, SIOCGIFBRDADDR, ifr) >= 0) + if ((cfg->flags & HAWK_IFCFG_BCAST) && ioctl(s, SIOCGIFBRDADDR, ifr) >= 0) { hawk_skadtonwad (&ifr->ifr_broadaddr, &cfg->bcast); } if ((cfg->flags & HAWK_IFCFG_PTOP) && - ioctl (s, SIOCGIFDSTADDR, ifr) >= 0) + ioctl(s, SIOCGIFDSTADDR, ifr) >= 0) { hawk_skadtonwad (&ifr->ifr_dstaddr, &cfg->ptop); } @@ -608,34 +642,31 @@ static int get_ifcfg (hawk_gem_t* gem, int s, hawk_ifcfg_t* cfg, struct ifreq* i hawk_clear_skad (&cfg->ptop); HAWK_MEMSET (cfg->ethw, 0, HAWK_SIZEOF(cfg->ethw)); - if (ioctl(s, SIOCGIFADDR, ifr) >= 0) - hawk_skadtonwad (&ifr->ifr_addr, &cfg->addr); + if (ioctl(s, SIOCGIFADDR, ifr) >= 0) copy_to_skad (&ifr->ifr_addr, &cfg->addr); - if (ioctl (s, SIOCGIFNETMASK, ifr) >= 0) - hawk_skadtonwad (&ifr->ifr_addr, &cfg->mask); + if (ioctl(s, SIOCGIFNETMASK, ifr) >= 0) copy_to_skad (&ifr->ifr_addr, &cfg->mask); #if defined(__linux) - if (cfg->addr.type == HAWK_NWAD_NX && cfg->mask.type == HAWK_NWAD_NX && cfg->type == HAWK_IFCFG_IN6) + if (hawk_skad_family(&cfg->addr) == HAWK_AF_UNSPEC && hawk_skad_family(&cfg->mask) == HAWK_AF_UNSPEC && cfg->type == HAWK_IFCFG_IN6) { /* access /proc/net/if_inet6 */ read_proc_net_if_inet6 (gem, cfg, ifr); } #endif - if ((cfg->flags & HAWK_IFCFG_BCAST) && - ioctl (s, SIOCGIFBRDADDR, ifr) >= 0) + if ((cfg->flags & HAWK_IFCFG_BCAST) && ioctl(s, SIOCGIFBRDADDR, ifr) >= 0) { - hawk_skadtonwad (&ifr->ifr_broadaddr, &cfg->bcast); + copy_to_skad (&ifr->ifr_broadaddr, &cfg->bcast); } - if ((cfg->flags & HAWK_IFCFG_PTOP) && - ioctl (s, SIOCGIFDSTADDR, ifr) >= 0) + if ((cfg->flags & HAWK_IFCFG_PTOP) && ioctl(s, SIOCGIFDSTADDR, ifr) >= 0) { - hawk_skadtonwad (&ifr->ifr_dstaddr, &cfg->ptop); + copy_to_skad (&ifr->ifr_dstaddr, &cfg->ptop); } #if defined(SIOCGIFHWADDR) - if (ioctl (s, SIOCGIFHWADDR, ifr) >= 0) + if (ioctl(s, SIOCGIFHWADDR, ifr) >= 0) + if (ioctl(s, SIOCGIFHWADDR, ifr) >= 0) { HAWK_MEMCPY (cfg->ethw, ifr->ifr_hwaddr.sa_data, HAWK_SIZEOF(cfg->ethw)); } @@ -685,8 +716,7 @@ static void get_moreinfo (int s, hawk_ifcfg_t* cfg, struct ifreq* ifr) HAWK_MEMSET (&ev, 0, HAWK_SIZEOF(ev)); ev.cmd= ETHTOOL_GLINK; ifr->ifr_data = &ev; - if (ioctl (s, SIOCETHTOOL,ifr) >= 0) - cfg->flags |= ev.data? HAWK_IFCFG_LINKUP: HAWK_IFCFG_LINKDOWN; + if (ioctl(s, SIOCETHTOOL, ifr) >= 0) cfg->flags |= ev.data? HAWK_IFCFG_LINKUP: HAWK_IFCFG_LINKDOWN; } #endif diff --git a/hawk/lib/hawk-gem.h b/hawk/lib/hawk-gem.h index 34ee76b0..8bffad31 100644 --- a/hawk/lib/hawk-gem.h +++ b/hawk/lib/hawk-gem.h @@ -36,7 +36,7 @@ enum hawk_ifcfg_flag_t HAWK_IFCFG_UP = (1 << 0), HAWK_IFCFG_RUNNING = (1 << 1), HAWK_IFCFG_BCAST = (1 << 2), - HAWK_IFCFG_PTOP = (1 << 3), + HAWK_IFCFG_PTOP = (1 << 3), /* peer to peer */ HAWK_IFCFG_LINKUP = (1 << 4), HAWK_IFCFG_LINKDOWN = (1 << 5) }; @@ -382,6 +382,11 @@ int hawk_gem_ifindextoucstr ( # define hawk_gem_ifindextooocstr hawk_gem_ifindextobcstr #endif +int hawk_gem_getifcfg ( + hawk_gem_t* gem, + hawk_ifcfg_t* cfg +); + #if defined(__cplusplus) } #endif diff --git a/hawk/lib/hawk-utl.h b/hawk/lib/hawk-utl.h index 192be49c..df7e6854 100644 --- a/hawk/lib/hawk-utl.h +++ b/hawk/lib/hawk-utl.h @@ -964,6 +964,28 @@ HAWK_EXPORT int hawk_uchars_to_num ( /* ------------------------------------------------------------------------- */ +HAWK_EXPORT int hawk_uchars_to_bin ( + const hawk_uch_t* hex, + hawk_oow_t hexlen, + hawk_uint8_t* buf, + hawk_oow_t buflen +); + +HAWK_EXPORT int hawk_bchars_to_bin ( + const hawk_bch_t* hex, + hawk_oow_t hexlen, + hawk_uint8_t* buf, + hawk_oow_t buflen +); + +#if defined(HAWK_OOCH_IS_UCH) +# define hawk_oochars_to_bin hawk_uchars_to_bin +#else +# define hawk_oochars_to_bin hawk_bchars_to_bin +#endif + +/* ------------------------------------------------------------------------- */ + HAWK_EXPORT hawk_cmgr_t* hawk_get_cmgr_by_id ( hawk_cmgr_id_t id ); diff --git a/hawk/lib/mod-sys.c b/hawk/lib/mod-sys.c index efd89fb3..d890f553 100644 --- a/hawk/lib/mod-sys.c +++ b/hawk/lib/mod-sys.c @@ -1794,11 +1794,9 @@ static int fnc_getenv (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) return 0; } -static int fnc_getnwifcfg (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) +static int fnc_getifcfg (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) { -#if 0 -// TODO: put this back - hawk_nwifcfg_t cfg; + hawk_ifcfg_t cfg; hawk_rtx_valtostr_out_t out; int ret = -1; @@ -1815,7 +1813,7 @@ static int fnc_getnwifcfg (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) { cfg.type = type; - if (hawk_getnwifcfg(&cfg) >= 0) + if (hawk_gem_getifcfg(hawk_rtx_getgem(rtx), &cfg) >= 0) { /* make a map value containg configuration */ hawk_int_t index, mtu; @@ -1838,32 +1836,34 @@ static int fnc_getnwifcfg (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) md[1].type = HAWK_VAL_MAP_DATA_INT; mtu = cfg.mtu; md[1].vptr = &mtu; - + md[2].key.ptr = HAWK_T("addr"); md[2].key.len = 4; md[2].type = HAWK_VAL_MAP_DATA_STR; - hawk_nwadtostr (&cfg.addr, addr, HAWK_COUNTOF(addr), HAWK_NWADTOSTR_ADDR); + // hawk_nwadtostr (&cfg.addr, addr, HAWK_COUNTOF(addr), HAWK_NWADTOSTR_ADDR); +addr[0] = '\0'; md[2].vptr = addr; md[3].key.ptr = HAWK_T("mask"); md[3].key.len = 4; md[3].type = HAWK_VAL_MAP_DATA_STR; - hawk_nwadtostr (&cfg.mask, mask, HAWK_COUNTOF(mask), HAWK_NWADTOSTR_ADDR); + // hawk_nwadtostr (&cfg.mask, mask, HAWK_COUNTOF(mask), HAWK_NWADTOSTR_ADDR); +mask[0] = '\0'; md[3].vptr = mask; md[4].key.ptr = HAWK_T("ethw"); md[4].key.len = 4; md[4].type = HAWK_VAL_MAP_DATA_STR; - hawk_strxfmt (ethw, HAWK_COUNTOF(ethw), HAWK_T("%02X:%02X:%02X:%02X:%02X:%02X"), + hawk_rtx_fmttooocstr (rtx, ethw, HAWK_COUNTOF(ethw), HAWK_T("%02X:%02X:%02X:%02X:%02X:%02X"), cfg.ethw[0], cfg.ethw[1], cfg.ethw[2], cfg.ethw[3], cfg.ethw[4], cfg.ethw[5]); md[4].vptr = ethw; - if (cfg.flags & (HAWK_NWIFCFG_LINKUP | HAWK_NWIFCFG_LINKDOWN)) + if (cfg.flags & (HAWK_IFCFG_LINKUP | HAWK_IFCFG_LINKDOWN)) { md[5].key.ptr = HAWK_T("link"); md[5].key.len = 4; md[5].type = HAWK_VAL_MAP_DATA_STR; - md[5].vptr = (cfg.flags & HAWK_NWIFCFG_LINKUP)? HAWK_T("up"): HAWK_T("down"); + md[5].vptr = (cfg.flags & HAWK_IFCFG_LINKUP)? HAWK_T("up"): HAWK_T("down"); } tmp = hawk_rtx_makemapvalwithdata(rtx, md); @@ -1871,7 +1871,7 @@ static int fnc_getnwifcfg (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) { int x; hawk_rtx_refupval (rtx, tmp); - x = hawk_rtx_setrefval (rtx, (hawk_val_ref_t*)hawk_rtx_getarg(rtx, 2), tmp); + x = hawk_rtx_setrefval(rtx, (hawk_val_ref_t*)hawk_rtx_getarg(rtx, 2), tmp); hawk_rtx_refdownval (rtx, tmp); if (x <= -1) return -1; ret = 0; @@ -1882,7 +1882,6 @@ static int fnc_getnwifcfg (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) /* no error check for hawk_rtx_makeintval() since ret is 0 or -1 */ hawk_rtx_setretval (rtx, hawk_rtx_makeintval (rtx, ret)); -#endif return 0; } /* ------------------------------------------------------------ */ @@ -2451,7 +2450,7 @@ static fnctab_t fnctab[] = { HAWK_T("getenv"), { { 1, 1, HAWK_NULL }, fnc_getenv, 0 } }, { HAWK_T("geteuid"), { { 0, 0, HAWK_NULL }, fnc_geteuid, 0 } }, { HAWK_T("getgid"), { { 0, 0, HAWK_NULL }, fnc_getgid, 0 } }, - { HAWK_T("getnwifcfg"), { { 3, 3, HAWK_T("vvr") }, fnc_getnwifcfg, 0 } }, + { HAWK_T("getifcfg"), { { 3, 3, HAWK_T("vvr") }, fnc_getifcfg, 0 } }, { HAWK_T("getpgid"), { { 0, 0, HAWK_NULL }, fnc_getpgid, 0 } }, { HAWK_T("getpid"), { { 0, 0, HAWK_NULL }, fnc_getpid, 0 } }, { HAWK_T("getppid"), { { 0, 0, HAWK_NULL }, fnc_getppid, 0 } }, @@ -2511,6 +2510,9 @@ static inttab_t inttab[] = { HAWK_T("DIR_SORT"), { HAWK_DIR_SORT } }, + { HAWK_T("IFCFG_IN4"), { HAWK_IFCFG_IN4 } }, + { HAWK_T("IFCFG_IN6"), { HAWK_IFCFG_IN6 } }, + #if defined(ENABLE_SYSLOG) { HAWK_T("LOG_FAC_AUTH"), { LOG_AUTH } }, { HAWK_T("LOG_FAC_AUTHPRIV"), { LOG_AUTHPRIV } }, @@ -2548,12 +2550,6 @@ static inttab_t inttab[] = { HAWK_T("LOG_PRI_WARNING"), { LOG_WARNING } }, #endif -#if 0 -// PUT THESE BACK - { HAWK_T("NWIFCFG_IN4"), { HAWK_NWIFCFG_IN4 } }, - { HAWK_T("NWIFCFG_IN6"), { HAWK_NWIFCFG_IN6 } }, -#endif - #if defined(O_APPEND) { HAWK_T("O_APPEND"), { O_APPEND } }, #endif diff --git a/hawk/lib/utl-str.c b/hawk/lib/utl-str.c index 85619c41..ed116e5a 100644 --- a/hawk/lib/utl-str.c +++ b/hawk/lib/utl-str.c @@ -2613,6 +2613,69 @@ int hawk_bchars_to_num (int option, const hawk_bch_t* ptr, hawk_oow_t len, hawk_ if (strict && endptr < end) return -1; return 0; /* int */ } + +/* ------------------------------------------------------------------------ */ + +#define XDIGIT_TO_NUM(c) \ + (((c) >= '0' && (c) <= '9')? ((c) - '0'): \ + ((c) >= 'A' && (c) <= 'F')? ((c) - 'A' + 10): \ + ((c) >= 'a' && (c) <= 'f')? ((c) - 'a' + 10): -1) + +int hawk_uchars_to_bin (const hawk_uch_t* hex, hawk_oow_t hexlen, hawk_uint8_t* buf, hawk_oow_t buflen) +{ + const hawk_uch_t* end = hex + hexlen; + hawk_oow_t bi = 0; + + while (hex < end && bi < buflen) + { + int v; + + v = XDIGIT_TO_NUM(*hex); + if (v <= -1) return -1; + buf[bi] = buf[bi] * 16 + v; + + hex++; + if (hex >= end) return -1; + + v = XDIGIT_TO_NUM(*hex); + if (v <= -1) return -1; + buf[bi] = buf[bi] * 16 + v; + + hex++; + bi++; + } + + return 0; +} + +int hawk_bchars_to_bin (const hawk_bch_t* hex, hawk_oow_t hexlen, hawk_uint8_t* buf, hawk_oow_t buflen) +{ + const hawk_bch_t* end = hex + hexlen; + hawk_oow_t bi = 0; + + while (hex < end && bi < buflen) + { + int v; + + v = XDIGIT_TO_NUM(*hex); + if (v <= -1) return -1; + buf[bi] = buf[bi] * 16 + v; + + hex++; + if (hex >= end) return -1; + + v = XDIGIT_TO_NUM(*hex); + if (v <= -1) return -1; + buf[bi] = buf[bi] * 16 + v; + + hex++; + bi++; + } + + return 0; +} + + /* ------------------------------------------------------------------------ */ int hawk_conv_bchars_to_uchars_with_cmgr (