From 0ae736ebb029fe8878814b8c7faad2d276c96765 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Fri, 13 Aug 2021 15:37:34 +0000 Subject: [PATCH] starting to add some dhcp code --- hio/lib/Makefile.am | 2 ++ hio/lib/Makefile.in | 85 ++++++++++++++++++++++++++------------------- hio/lib/dhcp-svr.c | 81 ++++++++++++++++++++++++++++++++++++++++++ hio/lib/hio-dhcp.h | 66 +++++++++++++++++++++++++++++++++++ hio/lib/hio-rad.h | 9 +++-- hio/lib/rad-msg.c | 80 +++++++++++++++++++++++++++++++----------- hio/t/t-003.c | 41 +++++++++++++++++----- 7 files changed, 297 insertions(+), 67 deletions(-) create mode 100644 hio/lib/dhcp-svr.c create mode 100644 hio/lib/hio-dhcp.h diff --git a/hio/lib/Makefile.am b/hio/lib/Makefile.am index a9bf33f..bc38f46 100644 --- a/hio/lib/Makefile.am +++ b/hio/lib/Makefile.am @@ -30,6 +30,7 @@ include_HEADERS = \ hio-cfg.h \ hio-chr.h \ hio-cmn.h \ + hio-dhcp.h \ hio-dns.h \ hio-ecs.h \ hio-fmt.h \ @@ -55,6 +56,7 @@ include_HEADERS = \ lib_LTLIBRARIES = libhio.la libhio_la_SOURCES = \ chr.c \ + dhcp-svr.c \ dns.c \ dns-cli.c \ ecs.c \ diff --git a/hio/lib/Makefile.in b/hio/lib/Makefile.in index fdbd643..f1b0d64 100644 --- a/hio/lib/Makefile.in +++ b/hio/lib/Makefile.in @@ -147,30 +147,31 @@ am__DEPENDENCIES_2 = $(am__DEPENDENCIES_1) libhio_la_DEPENDENCIES = $(am__DEPENDENCIES_2) $(am__DEPENDENCIES_1) \ $(am__DEPENDENCIES_1) $(am__DEPENDENCIES_1) \ $(am__DEPENDENCIES_3) $(am__DEPENDENCIES_4) -am__libhio_la_SOURCES_DIST = chr.c dns.c dns-cli.c ecs.c ecs-imp.h \ - err.c fmt.c fmt-imp.h htb.c htrd.c htre.c http.c http-cgi.c \ - http-fil.c http-prv.h http-svr.c http-thr.c http-txt.c json.c \ - hio-prv.h hio.c md5.c nwif.c opt.c opt-imp.h path.c pipe.c \ - pro.c rad-msg.c sck.c skad.c sys.c sys-ass.c sys-err.c \ - sys-log.c sys-mux.c sys-prv.h sys-tim.c thr.c uch-case.h \ - uch-prop.h tmr.c utf8.c utl.c utl-siph.c utl-str.c mar.c \ - mar-cli.c +am__libhio_la_SOURCES_DIST = chr.c dhcp-svr.c dns.c dns-cli.c ecs.c \ + ecs-imp.h err.c fmt.c fmt-imp.h htb.c htrd.c htre.c http.c \ + http-cgi.c http-fil.c http-prv.h http-svr.c http-thr.c \ + http-txt.c json.c hio-prv.h hio.c md5.c nwif.c opt.c opt-imp.h \ + path.c pipe.c pro.c rad-msg.c sck.c skad.c sys.c sys-ass.c \ + sys-err.c sys-log.c sys-mux.c sys-prv.h sys-tim.c thr.c \ + uch-case.h uch-prop.h tmr.c utf8.c utl.c utl-siph.c utl-str.c \ + mar.c mar-cli.c @ENABLE_MARIADB_TRUE@am__objects_1 = libhio_la-mar.lo \ @ENABLE_MARIADB_TRUE@ libhio_la-mar-cli.lo -am_libhio_la_OBJECTS = libhio_la-chr.lo libhio_la-dns.lo \ - libhio_la-dns-cli.lo libhio_la-ecs.lo libhio_la-err.lo \ - libhio_la-fmt.lo libhio_la-htb.lo libhio_la-htrd.lo \ - libhio_la-htre.lo libhio_la-http.lo libhio_la-http-cgi.lo \ - libhio_la-http-fil.lo libhio_la-http-svr.lo \ - libhio_la-http-thr.lo libhio_la-http-txt.lo libhio_la-json.lo \ - libhio_la-hio.lo libhio_la-md5.lo libhio_la-nwif.lo \ - libhio_la-opt.lo libhio_la-path.lo libhio_la-pipe.lo \ - libhio_la-pro.lo libhio_la-rad-msg.lo libhio_la-sck.lo \ - libhio_la-skad.lo libhio_la-sys.lo libhio_la-sys-ass.lo \ - libhio_la-sys-err.lo libhio_la-sys-log.lo libhio_la-sys-mux.lo \ - libhio_la-sys-tim.lo libhio_la-thr.lo libhio_la-tmr.lo \ - libhio_la-utf8.lo libhio_la-utl.lo libhio_la-utl-siph.lo \ - libhio_la-utl-str.lo $(am__objects_1) +am_libhio_la_OBJECTS = libhio_la-chr.lo libhio_la-dhcp-svr.lo \ + libhio_la-dns.lo libhio_la-dns-cli.lo libhio_la-ecs.lo \ + libhio_la-err.lo libhio_la-fmt.lo libhio_la-htb.lo \ + libhio_la-htrd.lo libhio_la-htre.lo libhio_la-http.lo \ + libhio_la-http-cgi.lo libhio_la-http-fil.lo \ + libhio_la-http-svr.lo libhio_la-http-thr.lo \ + libhio_la-http-txt.lo libhio_la-json.lo libhio_la-hio.lo \ + libhio_la-md5.lo libhio_la-nwif.lo libhio_la-opt.lo \ + libhio_la-path.lo libhio_la-pipe.lo libhio_la-pro.lo \ + libhio_la-rad-msg.lo libhio_la-sck.lo libhio_la-skad.lo \ + libhio_la-sys.lo libhio_la-sys-ass.lo libhio_la-sys-err.lo \ + libhio_la-sys-log.lo libhio_la-sys-mux.lo libhio_la-sys-tim.lo \ + libhio_la-thr.lo libhio_la-tmr.lo libhio_la-utf8.lo \ + libhio_la-utl.lo libhio_la-utl-siph.lo libhio_la-utl-str.lo \ + $(am__objects_1) libhio_la_OBJECTS = $(am_libhio_la_OBJECTS) AM_V_lt = $(am__v_lt_@AM_V@) am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@) @@ -195,6 +196,7 @@ DEFAULT_INCLUDES = depcomp = $(SHELL) $(top_srcdir)/ac/depcomp am__maybe_remake_depfiles = depfiles am__depfiles_remade = ./$(DEPDIR)/libhio_la-chr.Plo \ + ./$(DEPDIR)/libhio_la-dhcp-svr.Plo \ ./$(DEPDIR)/libhio_la-dns-cli.Plo \ ./$(DEPDIR)/libhio_la-dns.Plo ./$(DEPDIR)/libhio_la-ecs.Plo \ ./$(DEPDIR)/libhio_la-err.Plo ./$(DEPDIR)/libhio_la-fmt.Plo \ @@ -249,11 +251,11 @@ am__can_run_installinfo = \ n|no|NO) false;; \ *) (install-info --version) >/dev/null 2>&1;; \ esac -am__include_HEADERS_DIST = hio-cfg.h hio-chr.h hio-cmn.h hio-dns.h \ - hio-ecs.h hio-fmt.h hio-htb.h hio-htrd.h hio-htre.h hio-http.h \ - hio-json.h hio-nwif.h hio-opt.h hio-pac1.h hio-path.h \ - hio-pipe.h hio-pro.h hio-rad.h hio-sck.h hio-skad.h hio-thr.h \ - hio-upac.h hio-utl.h hio.h hio-mar.h +am__include_HEADERS_DIST = hio-cfg.h hio-chr.h hio-cmn.h hio-dhcp.h \ + hio-dns.h hio-ecs.h hio-fmt.h hio-htb.h hio-htrd.h hio-htre.h \ + hio-http.h hio-json.h hio-nwif.h hio-opt.h hio-pac1.h \ + hio-path.h hio-pipe.h hio-pro.h hio-rad.h hio-sck.h hio-skad.h \ + hio-thr.h hio-upac.h hio-utl.h hio.h hio-mar.h HEADERS = $(include_HEADERS) am__tagged_files = $(HEADERS) $(SOURCES) $(TAGS_FILES) $(LISP) \ hio-cfg.h.in @@ -440,19 +442,20 @@ LIBADD_LIB_COMMON = $(LIBM) #pkgincludedir = $(includedir) #pkglibdir = $(libdir) #pkgbindir = $(bindir) -include_HEADERS = hio-cfg.h hio-chr.h hio-cmn.h hio-dns.h hio-ecs.h \ - hio-fmt.h hio-htb.h hio-htrd.h hio-htre.h hio-http.h \ +include_HEADERS = hio-cfg.h hio-chr.h hio-cmn.h hio-dhcp.h hio-dns.h \ + hio-ecs.h hio-fmt.h hio-htb.h hio-htrd.h hio-htre.h hio-http.h \ hio-json.h hio-nwif.h hio-opt.h hio-pac1.h hio-path.h \ hio-pipe.h hio-pro.h hio-rad.h hio-sck.h hio-skad.h hio-thr.h \ hio-upac.h hio-utl.h hio.h $(am__append_1) lib_LTLIBRARIES = libhio.la -libhio_la_SOURCES = chr.c dns.c dns-cli.c ecs.c ecs-imp.h err.c fmt.c \ - fmt-imp.h htb.c htrd.c htre.c http.c http-cgi.c http-fil.c \ - http-prv.h http-svr.c http-thr.c http-txt.c json.c hio-prv.h \ - hio.c md5.c nwif.c opt.c opt-imp.h path.c pipe.c pro.c \ - rad-msg.c sck.c skad.c sys.c sys-ass.c sys-err.c sys-log.c \ - sys-mux.c sys-prv.h sys-tim.c thr.c uch-case.h uch-prop.h \ - tmr.c utf8.c utl.c utl-siph.c utl-str.c $(am__append_2) +libhio_la_SOURCES = chr.c dhcp-svr.c dns.c dns-cli.c ecs.c ecs-imp.h \ + err.c fmt.c fmt-imp.h htb.c htrd.c htre.c http.c http-cgi.c \ + http-fil.c http-prv.h http-svr.c http-thr.c http-txt.c json.c \ + hio-prv.h hio.c md5.c nwif.c opt.c opt-imp.h path.c pipe.c \ + pro.c rad-msg.c sck.c skad.c sys.c sys-ass.c sys-err.c \ + sys-log.c sys-mux.c sys-prv.h sys-tim.c thr.c uch-case.h \ + uch-prop.h tmr.c utf8.c utl.c utl-siph.c utl-str.c \ + $(am__append_2) libhio_la_CPPFLAGS = $(CPPFLAGS_LIB_COMMON) libhio_la_CFLAGS = $(CFLAGS_LIB_COMMON) $(am__append_3) libhio_la_LDFLAGS = $(LDFLAGS_LIB_COMMON) $(am__append_4) @@ -553,6 +556,7 @@ distclean-compile: -rm -f *.tab.c @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhio_la-chr.Plo@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhio_la-dhcp-svr.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhio_la-dns-cli.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhio_la-dns.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhio_la-ecs.Plo@am__quote@ # am--include-marker @@ -630,6 +634,13 @@ libhio_la-chr.lo: chr.c @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libhio_la_CPPFLAGS) $(CPPFLAGS) $(libhio_la_CFLAGS) $(CFLAGS) -c -o libhio_la-chr.lo `test -f 'chr.c' || echo '$(srcdir)/'`chr.c +libhio_la-dhcp-svr.lo: dhcp-svr.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libhio_la_CPPFLAGS) $(CPPFLAGS) $(libhio_la_CFLAGS) $(CFLAGS) -MT libhio_la-dhcp-svr.lo -MD -MP -MF $(DEPDIR)/libhio_la-dhcp-svr.Tpo -c -o libhio_la-dhcp-svr.lo `test -f 'dhcp-svr.c' || echo '$(srcdir)/'`dhcp-svr.c +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libhio_la-dhcp-svr.Tpo $(DEPDIR)/libhio_la-dhcp-svr.Plo +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='dhcp-svr.c' object='libhio_la-dhcp-svr.lo' libtool=yes @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libhio_la_CPPFLAGS) $(CPPFLAGS) $(libhio_la_CFLAGS) $(CFLAGS) -c -o libhio_la-dhcp-svr.lo `test -f 'dhcp-svr.c' || echo '$(srcdir)/'`dhcp-svr.c + libhio_la-dns.lo: dns.c @am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libhio_la_CPPFLAGS) $(CPPFLAGS) $(libhio_la_CFLAGS) $(CFLAGS) -MT libhio_la-dns.lo -MD -MP -MF $(DEPDIR)/libhio_la-dns.Tpo -c -o libhio_la-dns.lo `test -f 'dns.c' || echo '$(srcdir)/'`dns.c @am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libhio_la-dns.Tpo $(DEPDIR)/libhio_la-dns.Plo @@ -1059,6 +1070,7 @@ clean-am: clean-generic clean-libLTLIBRARIES clean-libtool \ distclean: distclean-am -rm -f ./$(DEPDIR)/libhio_la-chr.Plo + -rm -f ./$(DEPDIR)/libhio_la-dhcp-svr.Plo -rm -f ./$(DEPDIR)/libhio_la-dns-cli.Plo -rm -f ./$(DEPDIR)/libhio_la-dns.Plo -rm -f ./$(DEPDIR)/libhio_la-ecs.Plo @@ -1145,6 +1157,7 @@ installcheck-am: maintainer-clean: maintainer-clean-am -rm -f ./$(DEPDIR)/libhio_la-chr.Plo + -rm -f ./$(DEPDIR)/libhio_la-dhcp-svr.Plo -rm -f ./$(DEPDIR)/libhio_la-dns-cli.Plo -rm -f ./$(DEPDIR)/libhio_la-dns.Plo -rm -f ./$(DEPDIR)/libhio_la-ecs.Plo diff --git a/hio/lib/dhcp-svr.c b/hio/lib/dhcp-svr.c new file mode 100644 index 0000000..a3ab735 --- /dev/null +++ b/hio/lib/dhcp-svr.c @@ -0,0 +1,81 @@ +/* + Copyright (c) 2016-2020 Chung, Hyung-Hwan. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAfRRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include "hio-prv.h" + +typedef struct hio_svc_dhcs_t hio_svc_dhcs_t; +struct hio_svc_dhcs_t +{ + HIO_SVC_HEADER; + + hio_dev_sck_t* sck; +}; + + +hio_svc_dhcs_t* hio_svc_dhcs_start (hio_t* hio/*, hio_dev_sck_bind_t* sck_bind*/) +{ + hio_svc_dhcs_t* dhcs; + union + { + hio_dev_sck_make_t m; + hio_dev_sck_listen_t l; + } info; + + dhcs = (hio_svc_dhcs_t*)hio_callocmem(hio, HIO_SIZEOF(*dhcs)); + if (HIO_UNLIKELY(!dhcs)) goto oops; + + dhcs->hio = hio; + + HIO_MEMSET (&info, 0, HIO_SIZEOF(info)); + info.m.type = HIO_DEV_SCK_UDP6; + info.m.options = HIO_DEV_SCK_BIND_REUSEADDR | HIO_DEV_SCK_BIND_REUSEPORT | HIO_DEV_SCK_BIND_IGNERR; + //info.m.on_write = + //info.m.on_read = ... + //info.m.on_connect = ... + //info.m.on_disconnect = ... + dhcs->sck = hio_dev_sck_make(hio, 0, &info.m); + if (HIO_UNLIKELY(!dhcs->sck)) goto oops; + +#if defined(IPV6_RECVPKTINFO) + hio_dev_sck_setsockopt (dhcs->sck, IPPROTO_IPV6, IPV6_RECVPKTINFO, &v); +#elif defined(IPV6_PKTINFO) + hio_dev_sck_setsockopt (dhcs->sck, IPPROTO_IPV6, IPV6_PKTINFO, &v); +#else +//# error no ipv6 pktinfo +#endif + + //hio_dev_sck_bind(dhcs->sck, ) + + HIO_SVCL_APPEND_SVC (&hio->actsvc, (hio_svc_t*)dhcs); + return dhcs; + +oops: + if (dhcs) + { + if (dhcs->sck) hio_dev_sck_kill (dhcs->sck); + hio_freemem (hio, dhcs); + } + return HIO_NULL; +} diff --git a/hio/lib/hio-dhcp.h b/hio/lib/hio-dhcp.h new file mode 100644 index 0000000..28e7947 --- /dev/null +++ b/hio/lib/hio-dhcp.h @@ -0,0 +1,66 @@ +/* + Copyright (c) 2016-2020 Chung, Hyung-Hwan. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAfRRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _HIO_DHCP_H_ +#define _HIO_DHCP_H_ + +#include +#include + +#define HIO_DNS_PORT (53) + +typedef struct hio_svc_dhcs_t hio_svc_dhcs_t; + +/* ---------------------------------------------------------------- */ + +#if defined(__cplusplus) +extern "C" { +#endif + +HIO_EXPORT hio_svc_dhcs_t* hio_svc_dhcs_start ( + hio_t* hio, + const hio_skad_t* serv_addr, /* required */ + const hio_skad_t* bind_addr, /* optional. can be HIO_NULL */ + const hio_ntime_t* send_tmout, /* required */ + const hio_ntime_t* reply_tmout, /* required */ + hio_oow_t max_tries /* required */ +); + +HIO_EXPORT void hio_svc_dhcs_stop ( + hio_svc_dhcs_t* dhcs +); + +#if defined(HIO_HAVE_INLINE) +static HIO_INLINE hio_t* hio_svc_dhcs_gethio(hio_svc_dhcs_t* svc) { return hio_svc_gethio((hio_svc_t*)svc); } +#else +# define hio_svc_dhcs_gethio(svc) hio_svc_gethio(svc) + +#endif + + +#if defined(__cplusplus) +} +#endif + +#endif diff --git a/hio/lib/hio-rad.h b/hio/lib/hio-rad.h index 484d780..4581626 100644 --- a/hio/lib/hio-rad.h +++ b/hio/lib/hio-rad.h @@ -54,7 +54,10 @@ enum hio_rad_code_t }; typedef enum hio_rad_code_t hio_rad_code_t; -#define HIO_RAD_MAX_AUTHENTICATOR_LEN 16 +#define HIO_RAD_AUTHENTICATOR_LEN (16) +#define HIO_RAD_USER_PASSWORD_BLKSIZE (16) /* size of a single block */ +#define HIO_RAD_USER_PASSWORD_TOTSIZE(pwlen) ((pwlen) <= 0? HIO_RAD_USER_PASSWORD_BLKSIZE: HIO_ALIGN(pwlen,HIO_RAD_USER_PASSWORD_BLKSIZE)) + #define HIO_RAD_MAX_ATTR_VALUE_LEN (HIO_TYPE_MAX(hio_uint8_t) - HIO_SIZEOF(hio_rad_attr_hdr_t)) #define HIO_RAD_MAX_XATTR_VALUE_LEN (HIO_TYPE_MAX(hio_uint8_t) - HIO_SIZEOF(hio_rad_xattr_hdr_t)) #define HIO_RAD_MAX_LXATTR_VALUE_LEN (HIO_TYPE_MAX(hio_uint8_t) - HIO_SIZEOF(hio_rad_lxattr_hdr_t)) @@ -62,6 +65,8 @@ typedef enum hio_rad_code_t hio_rad_code_t; #define HIO_RAD_MAX_XVSATTR_VALUE_LEN (HIO_TYPE_MAX(hio_uint8_t) - HIO_SIZEOF(hio_rad_xvsattr_hdr_t)) #define HIO_RAD_MAX_LXVSATTR_VALUE_LEN (HIO_TYPE_MAX(hio_uint8_t) - HIO_SIZEOF(hio_rad_lxvsattr_hdr_t)) + + typedef struct hio_rad_hdr_t hio_rad_hdr_t; typedef struct hio_rad_attr_hdr_t hio_rad_attr_hdr_t; typedef struct hio_rad_xattr_hdr_t hio_rad_xattr_hdr_t; @@ -81,7 +86,7 @@ struct hio_rad_hdr_t hio_uint8_t code; /* hio_rad_code_t */ hio_uint8_t id; hio_uint16_t length; - hio_uint8_t authenticator[HIO_RAD_MAX_AUTHENTICATOR_LEN]; /* authenticator */ + hio_uint8_t authenticator[HIO_RAD_AUTHENTICATOR_LEN]; /* authenticator */ }; struct hio_rad_attr_hdr_t diff --git a/hio/lib/rad-msg.c b/hio/lib/rad-msg.c index c7718a9..a96a6c9 100644 --- a/hio/lib/rad-msg.c +++ b/hio/lib/rad-msg.c @@ -831,8 +831,30 @@ hio_rad_attr_hdr_t* hio_rad_insert_attr (hio_rad_hdr_t* auth, int max, hio_uint1 } else if (HIO_RAD_ATTR_IS_LONG_EXTENDED(hi)) { - /* TODO: mutliple attributes if data is long */ - return (hio_rad_attr_hdr_t*)hio_rad_insert_extended_attribute(auth, max, hi, lo, ptr, len, 0); + hio_rad_xattr_hdr_t* tmp, * ret = HIO_NULL; + hio_uint16_t orglen = auth->length; + + while (len > HIO_RAD_MAX_LXATTR_VALUE_LEN) + { + tmp = hio_rad_insert_extended_attribute(auth, max, hi, lo, ptr, HIO_RAD_MAX_LXATTR_VALUE_LEN, (1 << 7)); + if (!tmp) + { + auth->length = orglen; + return HIO_NULL; + } + if (!ret) ret = tmp; + len -= HIO_RAD_MAX_LXATTR_VALUE_LEN; + ptr = (const hio_uint8_t*)ptr + HIO_RAD_MAX_LXATTR_VALUE_LEN; + } + tmp = hio_rad_insert_extended_attribute(auth, max, hi, lo, ptr, len, 0); + if (!tmp) + { + auth->length = orglen; + return HIO_NULL; + } + if (!ret) ret = tmp; + + return (hio_rad_attr_hdr_t*)ret; } /* attribute code out of range */ @@ -858,8 +880,30 @@ hio_rad_vsattr_hdr_t* hio_rad_insert_vsattr (hio_rad_hdr_t* auth, int max, hio_u } else if (HIO_RAD_ATTR_IS_LONG_EXTENDED(hi)) { - /* TODO: if len is greater than the maxm add multiple extended attributes with continuation */ - return (hio_rad_vsattr_hdr_t*)hio_rad_insert_extended_vendor_specific_attribute(auth, max, vendor, hi, lo, ptr, len, 0); + hio_rad_xvsattr_hdr_t* tmp, * ret = HIO_NULL; + hio_uint16_t orglen = auth->length; + + while (len > HIO_RAD_MAX_LXVSATTR_VALUE_LEN) + { + tmp = hio_rad_insert_extended_vendor_specific_attribute(auth, max, vendor, hi, lo, ptr, HIO_RAD_MAX_LXVSATTR_VALUE_LEN, (1 << 7)); + if (!tmp) + { + auth->length = orglen; + return HIO_NULL; + } + if (!ret) ret = tmp; + len -= HIO_RAD_MAX_LXVSATTR_VALUE_LEN; + ptr = (const hio_uint8_t*)ptr + HIO_RAD_MAX_LXVSATTR_VALUE_LEN; + } + tmp = hio_rad_insert_extended_vendor_specific_attribute(auth, max, vendor, hi, lo, ptr, len, 0); + if (!tmp) + { + auth->length = orglen; + return HIO_NULL; + } + if (!ret) ret = tmp; + + return (hio_rad_vsattr_hdr_t*)ret; } /* attribute code out of range */ @@ -870,15 +914,12 @@ hio_rad_vsattr_hdr_t* hio_rad_insert_vsattr (hio_rad_hdr_t* auth, int max, hio_u * UTILITY FUNCTIONS * ----------------------------------------------------------------------- */ -#define PASS_BLKSIZE HIO_RAD_MAX_AUTHENTICATOR_LEN -#define ALIGN(x,factor) ((((x) + (factor) - 1) / (factor)) * (factor)) - int hio_rad_set_user_password (hio_rad_hdr_t* auth, int max, const hio_bch_t* password, const hio_bch_t* secret) { hio_md5_t md5; hio_uint8_t hashed[HIO_RAD_MAX_ATTR_VALUE_LEN]; /* can't be longer than this */ - hio_uint8_t tmp[PASS_BLKSIZE]; + hio_uint8_t tmp[HIO_RAD_USER_PASSWORD_BLKSIZE]; int i, pwlen, padlen; @@ -886,16 +927,15 @@ int hio_rad_set_user_password (hio_rad_hdr_t* auth, int max, const hio_bch_t* pa pwlen = hio_count_bcstr(password); - /* calculate padlen to be the multiples of 16. - * 0 is forced to 16. */ - padlen = (pwlen <= 0)? PASS_BLKSIZE: ALIGN(pwlen,PASS_BLKSIZE); + /* calculate padlen to be the multiples of 16. 0 is forced to 16. */ + padlen = HIO_RAD_USER_PASSWORD_TOTSIZE(pwlen); /* keep the padded length limited within the maximum attribute length */ if (padlen > HIO_RAD_MAX_ATTR_VALUE_LEN) { padlen = HIO_RAD_MAX_ATTR_VALUE_LEN; - padlen = ALIGN(padlen,PASS_BLKSIZE); - if (padlen > HIO_RAD_MAX_ATTR_VALUE_LEN) padlen -= PASS_BLKSIZE; + padlen = HIO_ALIGN(padlen, HIO_RAD_USER_PASSWORD_BLKSIZE); + if (padlen > HIO_RAD_MAX_ATTR_VALUE_LEN) padlen -= HIO_RAD_USER_PASSWORD_BLKSIZE; /* also limit the original length */ if (pwlen > padlen) pwlen = padlen; @@ -921,9 +961,9 @@ int hio_rad_set_user_password (hio_rad_hdr_t* auth, int max, const hio_bch_t* pa { hio_md5_initialize (&md5); hio_md5_update (&md5, secret, hio_count_bcstr(secret)); - hio_md5_update (&md5, &hashed[(i-1) * PASS_BLKSIZE], PASS_BLKSIZE); + hio_md5_update (&md5, &hashed[(i - 1) * HIO_RAD_USER_PASSWORD_BLKSIZE], HIO_RAD_USER_PASSWORD_BLKSIZE); hio_md5_digest (&md5, tmp, HIO_SIZEOF(tmp)); - xor (&hashed[i * PASS_BLKSIZE], tmp, HIO_SIZEOF(tmp)); + xor (&hashed[i * HIO_RAD_USER_PASSWORD_BLKSIZE], tmp, HIO_SIZEOF(tmp)); } /* ok if not found or deleted. but not ok if an error occurred */ @@ -971,7 +1011,7 @@ int hio_rad_set_authenticator (hio_rad_hdr_t* req, const hio_bch_t* secret) int hio_rad_verify_request (hio_rad_hdr_t* req, const hio_bch_t* secret) { hio_md5_t md5; - hio_uint8_t orgauth[HIO_RAD_MAX_AUTHENTICATOR_LEN]; + hio_uint8_t orgauth[HIO_RAD_AUTHENTICATOR_LEN]; int ret; HIO_MEMCPY (orgauth, req->authenticator, HIO_SIZEOF(req->authenticator)); @@ -992,11 +1032,11 @@ int hio_rad_verify_response (hio_rad_hdr_t* res, const hio_rad_hdr_t* req, const { hio_md5_t md5; - hio_uint8_t calculated[HIO_RAD_MAX_AUTHENTICATOR_LEN]; - hio_uint8_t reply[HIO_RAD_MAX_AUTHENTICATOR_LEN]; + hio_uint8_t calculated[HIO_RAD_AUTHENTICATOR_LEN]; + hio_uint8_t reply[HIO_RAD_AUTHENTICATOR_LEN]; - /*HIO_ASSERT (HIO_SIZEOF(req->authenticator) == HIO_RAD_MAX_AUTHENTICATOR_LEN); - HIO_ASSERT (HIO_SIZEOF(res->authenticator) == HIO_RAD_MAX_AUTHENTICATOR_LEN);*/ + /*HIO_ASSERT (HIO_SIZEOF(req->authenticator) == HIO_RAD_AUTHENTICATOR_LEN); + HIO_ASSERT (HIO_SIZEOF(res->authenticator) == HIO_RAD_AUTHENTICATOR_LEN);*/ /* * We could dispense with the HIO_MEMCPY, and do MD5's of the packet diff --git a/hio/t/t-003.c b/hio/t/t-003.c index 853fac1..446333f 100644 --- a/hio/t/t-003.c +++ b/hio/t/t-003.c @@ -12,9 +12,7 @@ int main () { { hio_uint8_t buf[10240]; - hio_rad_hdr_t* hdr = buf; - - + hio_rad_hdr_t* hdr = (hio_rad_hdr_t*)buf; int i, j, n, exptotlen; struct @@ -32,6 +30,7 @@ int main () { 0, 4329, 5, "ssid", 4 }, { 0, 0, HIO_RAD_ATTR_NAS_IDENTIFIER, "nas identifier", 14 }, { 0, 0, HIO_RAD_ATTR_USER_PASSWORD, "password", 8 }, + { 1, 0, HIO_RAD_ATTR_USER_PASSWORD, "password", 8 }, { 0, 0, HIO_RAD_ATTR_USER_NAME, "username", 8 }, { 1, 10415, 8, "imsi-mcc-mnc-2", 14 }, @@ -53,12 +52,13 @@ int main () { 2, 0, HIO_RAD_ATTR_REPLY_MESSAGE, "reply message 3", 15 }, }; - hio_rad_initialize (buf, HIO_RAD_ACCESS_REQUEST, 255); + hio_rad_initialize (hdr, HIO_RAD_ACCESS_REQUEST, 255); T_ASSERT1 (hdr->code == HIO_RAD_ACCESS_REQUEST, "hdr->code not ok"); T_ASSERT1 (hdr->id == 255, "hdr->id not ok"); exptotlen = HIO_SIZEOF(*hdr); T_ASSERT1 (hdr->length == HIO_CONST_HTON16(HIO_SIZEOF(*hdr)), "hdr->length not ok"); + for (i = 0; i < HIO_COUNTOF(data); i++) { if (data[i].vendor) @@ -74,13 +74,13 @@ int main () if (HIO_RAD_ATTR_IS_LONG_EXTENDED(vsattr->type)) { exptotlen += HIO_SIZEOF(*lxvsattr); - lxvsattr = vsattr; + lxvsattr = (hio_rad_lxvsattr_hdr_t*)vsattr; T_ASSERT1 (lxvsattr->length == HIO_SIZEOF(*lxvsattr) + data[i].len, "wrong attribute length"); } else if (HIO_RAD_ATTR_IS_SHORT_EXTENDED(vsattr->type)) { exptotlen += HIO_SIZEOF(*xvsattr); - xvsattr = vsattr; + xvsattr = (hio_rad_xvsattr_hdr_t*)vsattr; T_ASSERT1 (xvsattr->length == HIO_SIZEOF(*xvsattr) + data[i].len, "wrong attribute length"); } else @@ -102,13 +102,13 @@ int main () if (HIO_RAD_ATTR_IS_LONG_EXTENDED(attr->type)) { exptotlen += HIO_SIZEOF(*lxattr); - lxattr = attr; + lxattr = (hio_rad_lxattr_hdr_t*)attr; T_ASSERT1 (lxattr->length == HIO_SIZEOF(*lxattr) + data[i].len, "wrong attribute length"); } else if (HIO_RAD_ATTR_IS_SHORT_EXTENDED(attr->type)) { exptotlen += HIO_SIZEOF(*xattr); - xattr = attr; + xattr = (hio_rad_xattr_hdr_t*)attr; T_ASSERT1 (xattr->length == HIO_SIZEOF(*xattr) + data[i].len, "wrong attribute length"); } else @@ -223,7 +223,30 @@ int main () } hio_rad_fill_authenticator (hdr); - hio_rad_set_user_password (hdr, HIO_SIZEOF(buf), "real_password", "testing123"); + hio_rad_set_user_password (hdr, HIO_SIZEOF(buf), "real_real_password", "testing123"); + exptotlen -= HIO_SIZEOF(hio_rad_attr_hdr_t) + 8; /* the first User-Password in the data table */ + exptotlen -= HIO_SIZEOF(hio_rad_attr_hdr_t) + 8; /* the second User-Password in the data table */ + exptotlen += HIO_SIZEOF(hio_rad_attr_hdr_t) + HIO_RAD_USER_PASSWORD_TOTSIZE(18); + T_ASSERT1 (hio_ntoh16(hdr->length) == exptotlen, "hdr->length not ok"); + + { + char tmp[1024]; + hio_rad_attr_hdr_t* attr; + hio_rad_lxattr_hdr_t* lxattr; + + for (i = 0; i < HIO_COUNTOF(tmp); i++) tmp[i] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i % 26]; + + /* the following call must insert 5 attributes. it returns the pointer to the first attribute. */ + attr = hio_rad_insert_attr (hdr, HIO_SIZEOF(buf), HIO_RAD_ATTR_CODE_EXTENDED_6(10), tmp, 1024); + T_ASSERT1 (attr != HIO_NULL, "long extended attribue insertion failure"); + T_ASSERT1 (attr->type == HIO_RAD_ATTR_EXTENDED_6, "wrong extended attribute base"); + + lxattr = (hio_rad_lxattr_hdr_t*)attr; + T_ASSERT1 (lxattr->xtype == 10, "wrong extended attribute type"); + T_ASSERT1 (lxattr->xflags == (1 << 7), "wrong long extended attribute flags"); + + /* TODO: inspect 4 continuing attributes */ + } #if 1 {