diff --git a/mio/lib/Makefile.am b/mio/lib/Makefile.am index 428ca44..5dc82f1 100644 --- a/mio/lib/Makefile.am +++ b/mio/lib/Makefile.am @@ -23,6 +23,7 @@ include_HEADERS = \ mio-cfg.h \ mio-cmn.h \ mio-dns.h \ + mio-nwif.h \ mio-pac1.h \ mio-pro.h \ mio-sck.h \ @@ -39,6 +40,7 @@ libmio_la_SOURCES = \ mio-prv.h \ mio-sys.h \ mio.c \ + nwif.c \ pro.c \ sck.c \ skad.c \ diff --git a/mio/lib/Makefile.in b/mio/lib/Makefile.in index c3678b0..5630500 100644 --- a/mio/lib/Makefile.in +++ b/mio/lib/Makefile.in @@ -139,11 +139,11 @@ am__DEPENDENCIES_2 = $(am__DEPENDENCIES_1) libmio_la_DEPENDENCIES = $(am__DEPENDENCIES_2) $(am__DEPENDENCIES_1) \ $(am__DEPENDENCIES_1) $(am__DEPENDENCIES_1) am_libmio_la_OBJECTS = libmio_la-dns.lo libmio_la-err.lo \ - libmio_la-fmt.lo libmio_la-mio.lo libmio_la-pro.lo \ - libmio_la-sck.lo libmio_la-skad.lo libmio_la-sys.lo \ - libmio_la-sys-ass.lo libmio_la-sys-err.lo libmio_la-sys-log.lo \ - libmio_la-sys-mux.lo libmio_la-sys-tim.lo libmio_la-tmr.lo \ - libmio_la-utf8.lo libmio_la-utl.lo + libmio_la-fmt.lo libmio_la-mio.lo libmio_la-nwif.lo \ + libmio_la-pro.lo libmio_la-sck.lo libmio_la-skad.lo \ + libmio_la-sys.lo libmio_la-sys-ass.lo libmio_la-sys-err.lo \ + libmio_la-sys-log.lo libmio_la-sys-mux.lo libmio_la-sys-tim.lo \ + libmio_la-tmr.lo libmio_la-utf8.lo libmio_la-utl.lo libmio_la_OBJECTS = $(am_libmio_la_OBJECTS) AM_V_lt = $(am__v_lt_@AM_V@) am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@) @@ -169,8 +169,9 @@ depcomp = $(SHELL) $(top_srcdir)/ac/depcomp am__maybe_remake_depfiles = depfiles am__depfiles_remade = ./$(DEPDIR)/libmio_la-dns.Plo \ ./$(DEPDIR)/libmio_la-err.Plo ./$(DEPDIR)/libmio_la-fmt.Plo \ - ./$(DEPDIR)/libmio_la-mio.Plo ./$(DEPDIR)/libmio_la-pro.Plo \ - ./$(DEPDIR)/libmio_la-sck.Plo ./$(DEPDIR)/libmio_la-skad.Plo \ + ./$(DEPDIR)/libmio_la-mio.Plo ./$(DEPDIR)/libmio_la-nwif.Plo \ + ./$(DEPDIR)/libmio_la-pro.Plo ./$(DEPDIR)/libmio_la-sck.Plo \ + ./$(DEPDIR)/libmio_la-skad.Plo \ ./$(DEPDIR)/libmio_la-sys-ass.Plo \ ./$(DEPDIR)/libmio_la-sys-err.Plo \ ./$(DEPDIR)/libmio_la-sys-log.Plo \ @@ -397,6 +398,7 @@ include_HEADERS = \ mio-cfg.h \ mio-cmn.h \ mio-dns.h \ + mio-nwif.h \ mio-pac1.h \ mio-pro.h \ mio-sck.h \ @@ -413,6 +415,7 @@ libmio_la_SOURCES = \ mio-prv.h \ mio-sys.h \ mio.c \ + nwif.c \ pro.c \ sck.c \ skad.c \ @@ -527,6 +530,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libmio_la-err.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libmio_la-fmt.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libmio_la-mio.Plo@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libmio_la-nwif.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libmio_la-pro.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libmio_la-sck.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libmio_la-skad.Plo@am__quote@ # am--include-marker @@ -598,6 +602,13 @@ libmio_la-mio.lo: mio.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) $(libmio_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o libmio_la-mio.lo `test -f 'mio.c' || echo '$(srcdir)/'`mio.c +libmio_la-nwif.lo: nwif.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libmio_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT libmio_la-nwif.lo -MD -MP -MF $(DEPDIR)/libmio_la-nwif.Tpo -c -o libmio_la-nwif.lo `test -f 'nwif.c' || echo '$(srcdir)/'`nwif.c +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libmio_la-nwif.Tpo $(DEPDIR)/libmio_la-nwif.Plo +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='nwif.c' object='libmio_la-nwif.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) $(libmio_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o libmio_la-nwif.lo `test -f 'nwif.c' || echo '$(srcdir)/'`nwif.c + libmio_la-pro.lo: pro.c @am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libmio_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT libmio_la-pro.lo -MD -MP -MF $(DEPDIR)/libmio_la-pro.Tpo -c -o libmio_la-pro.lo `test -f 'pro.c' || echo '$(srcdir)/'`pro.c @am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libmio_la-pro.Tpo $(DEPDIR)/libmio_la-pro.Plo @@ -841,6 +852,7 @@ distclean: distclean-am -rm -f ./$(DEPDIR)/libmio_la-err.Plo -rm -f ./$(DEPDIR)/libmio_la-fmt.Plo -rm -f ./$(DEPDIR)/libmio_la-mio.Plo + -rm -f ./$(DEPDIR)/libmio_la-nwif.Plo -rm -f ./$(DEPDIR)/libmio_la-pro.Plo -rm -f ./$(DEPDIR)/libmio_la-sck.Plo -rm -f ./$(DEPDIR)/libmio_la-skad.Plo @@ -903,6 +915,7 @@ maintainer-clean: maintainer-clean-am -rm -f ./$(DEPDIR)/libmio_la-err.Plo -rm -f ./$(DEPDIR)/libmio_la-fmt.Plo -rm -f ./$(DEPDIR)/libmio_la-mio.Plo + -rm -f ./$(DEPDIR)/libmio_la-nwif.Plo -rm -f ./$(DEPDIR)/libmio_la-pro.Plo -rm -f ./$(DEPDIR)/libmio_la-sck.Plo -rm -f ./$(DEPDIR)/libmio_la-skad.Plo diff --git a/mio/lib/mio-nwif.h b/mio/lib/mio-nwif.h new file mode 100644 index 0000000..b53a75f --- /dev/null +++ b/mio/lib/mio-nwif.h @@ -0,0 +1,139 @@ +/* + * $Id$ + * + Copyright (c) 2015-2016 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 _MIO_NWIF_H_ +#define _MIO_NWIF_H_ + +#include +#include + + +typedef struct mio_ifcfg_t mio_ifcfg_t; + +enum mio_ifcfg_flag_t +{ + MIO_IFCFG_UP = (1 << 0), + MIO_IFCFG_RUNNING = (1 << 1), + MIO_IFCFG_BCAST = (1 << 2), + MIO_IFCFG_PTOP = (1 << 3), /* peer to peer */ + MIO_IFCFG_LINKUP = (1 << 4), + MIO_IFCFG_LINKDOWN = (1 << 5) +}; + +enum mio_ifcfg_type_t +{ + MIO_IFCFG_IN4 = MIO_AF_INET, + MIO_IFCFG_IN6 = MIO_AF_INET6 +}; + +typedef enum mio_ifcfg_type_t mio_ifcfg_type_t; +struct mio_ifcfg_t +{ + mio_ifcfg_type_t type; /* in */ + mio_ooch_t name[64]; /* in/out */ + unsigned int index; /* in/out */ + + /* ---------------- */ + int flags; /* out */ + int mtu; /* out */ + + mio_skad_t addr; /* out */ + mio_skad_t mask; /* out */ + mio_skad_t ptop; /* out */ + mio_skad_t bcast; /* out */ + + /* ---------------- */ + + /* TODO: add hwaddr?? */ + /* i support ethernet only currently */ + mio_uint8_t ethw[6]; /* out */ +}; + + +#if defined(__cplusplus) +extern "C" { +#endif + +MIO_EXPORT int mio_bcstrtoifindex ( + mio_t* mio, + const mio_bch_t* ptr, + unsigned int* index +); + +MIO_EXPORT int mio_bcharstoifindex ( + mio_t* mio, + const mio_bch_t* ptr, + mio_oow_t len, + unsigned int* index +); + +MIO_EXPORT int mio_ucstrtoifindex ( + mio_t* mio, + const mio_uch_t* ptr, + unsigned int* index +); + +MIO_EXPORT int mio_ucharstoifindex ( + mio_t* mio, + const mio_uch_t* ptr, + mio_oow_t len, + unsigned int* index +); + +MIO_EXPORT int mio_ifindextobcstr ( + mio_t* mio, + unsigned int index, + mio_bch_t* buf, + mio_oow_t len +); + +MIO_EXPORT int mio_ifindextoucstr ( + mio_t* mio, + unsigned int index, + mio_uch_t* buf, + mio_oow_t len +); + +#if defined(MIO_OOCH_IS_UCH) +# define mio_oocstrtoifindex mio_ucstrtoifindex +# define mio_oocharstoifindex mio_ucharstoifindex +# define mio_ifindextooocstr mio_ifindextoucstr +#else +# define mio_oocstrtoifindex mio_bcstrtoifindex +# define mio_oocharstoifindex mio_bcharstoifindex +# define mio_ifindextooocstr mio_ifindextobcstr +#endif + +MIO_EXPORT int mio_getifcfg ( + mio_t* mio, + mio_ifcfg_t* cfg +); + +#if defined(__cplusplus) +} +#endif + +#endif diff --git a/mio/lib/mio-utl.h b/mio/lib/mio-utl.h index 4adfa5b..2155735 100644 --- a/mio/lib/mio-utl.h +++ b/mio/lib/mio-utl.h @@ -284,6 +284,23 @@ MIO_EXPORT void mio_copy_bchars_to_uchars ( const mio_bch_t* src, mio_oow_t len ); +MIO_EXPORT void mio_copy_uchars_to_bchars ( + mio_bch_t* dst, + const mio_uch_t* src, + mio_oow_t len +); + +MIO_EXPORT mio_oow_t mio_copy_uchars_to_ucstr_unlimited ( + mio_uch_t* dst, + const mio_uch_t* src, + mio_oow_t len +); + +MIO_EXPORT mio_oow_t mio_copy_bchars_to_bcstr_unlimited ( + mio_bch_t* dst, + const mio_bch_t* src, + mio_oow_t len +); MIO_EXPORT mio_oow_t mio_copy_ucstr ( mio_uch_t* dst, @@ -297,6 +314,30 @@ MIO_EXPORT mio_oow_t mio_copy_bcstr ( const mio_bch_t* src ); +MIO_EXPORT mio_oow_t mio_copy_uchars_to_ucstr ( + mio_uch_t* dst, + mio_uch_t dlen, + const mio_uch_t* src, + mio_oow_t slen +); + +MIO_EXPORT mio_oow_t mio_copy_bchars_to_bcstr ( + mio_bch_t* dst, + mio_bch_t dlen, + const mio_bch_t* src, + mio_oow_t slen +); + +MIO_EXPORT mio_oow_t mio_copy_ucstr_unlimited ( + mio_uch_t* dst, + const mio_uch_t* src +); + +MIO_EXPORT mio_oow_t mio_copy_bcstr_unlimited ( + mio_bch_t* dst, + const mio_bch_t* src +); + MIO_EXPORT void mio_fill_uchars ( mio_uch_t* dst, const mio_uch_t ch, @@ -359,9 +400,19 @@ MIO_EXPORT mio_oow_t mio_count_bcstr ( # define mio_comp_oochars_ucstr(str1,len1,str2) mio_comp_uchars_ucstr(str1,len1,str2) # define mio_comp_oochars_oocstr(str1,len1,str2) mio_comp_uchars_ucstr(str1,len1,str2) # define mio_comp_oocstr(str1,str2) mio_comp_ucstr(str1,str2) -# define mio_copy_oochars(dst,src,len) mio_copy_uchars(dst,src,len) -# define mio_copy_bchars_to_oochars(dst,src,len) mio_copy_bchars_to_uchars(dst,src,len) -# define mio_copy_oocstr(dst,len,src) mio_copy_ucstr(dst,len,src) + + +# define mio_copy_oochars mio_copy_uchars +# define mio_copy_bchars_to_oochars mio_copy_bchars_to_uchars +# define mio_copy_oochars_to_bchars mio_copy_uchars_to_bchars +# define mio_copy_uchars_to_oochars mio_copy_uchars +# define mio_copy_oochars_to_uchars mio_copy_uchars + +# define mio_copy_oochars_to_oocstr mio_copy_uchars_to_ucstr +# define mio_copy_oochars_to_oocstr_unlimited mio_copy_uchars_to_ucstr_unlimited +# define mio_copy_oocstr mio_copy_ucstr +# define mio_copy_oocstr_unlimited mio_copy_ucstr_unlimited + # define mio_fill_oochars(dst,ch,len) mio_fill_uchars(dst,ch,len) # define mio_find_oochar(ptr,len,c) mio_find_uchar(ptr,len,c) # define mio_rfind_oochar(ptr,len,c) mio_rfind_uchar(ptr,len,c) @@ -375,9 +426,18 @@ MIO_EXPORT mio_oow_t mio_count_bcstr ( # define mio_comp_oochars_ucstr(str1,len1,str2) mio_comp_bchars_ucstr(str1,len1,str2) # define mio_comp_oochars_oocstr(str1,len1,str2) mio_comp_bchars_bcstr(str1,len1,str2) # define mio_comp_oocstr(str1,str2) mio_comp_bcstr(str1,str2) -# define mio_copy_oochars(dst,src,len) mio_copy_bchars(dst,src,len) -# define mio_copy_bchars_to_oochars(dst,src,len) mio_copy_bchars(dst,src,len) -# define mio_copy_oocstr(dst,len,src) mio_copy_bcstr(dst,len,src) + +# define mio_copy_oochars mio_copy_bchars +# define mio_copy_bchars_to_oochars mio_copy_bchars +# define mio_copy_oochars_to_bchars mio_copy_bchars +# define mio_copy_uchars_to_oochars mio_copy_uchars_to_bchars +# define mio_copy_oochars_to_uchars mio_copy_bchars_to_uchars + +# define mio_copy_oochars_to_oocstr mio_copy_bchars_to_bcstr +# define mio_copy_oochars_to_oocstr_unlimited mio_copy_bchars_to_bcstr_unlimited +# define mio_copy_oocstr mio_copy_bcstr +# define mio_copy_oocstr_unlimited mio_copy_bcstr_unlimited + # define mio_fill_oochars(dst,ch,len) mio_fill_bchars(dst,ch,len) # define mio_find_oochar(ptr,len,c) mio_find_bchar(ptr,len,c) # define mio_rfind_oochar(ptr,len,c) mio_rfind_bchar(ptr,len,c) diff --git a/mio/lib/nwif.c b/mio/lib/nwif.c new file mode 100644 index 0000000..a7b520d --- /dev/null +++ b/mio/lib/nwif.c @@ -0,0 +1,651 @@ +/* + * $Id$ + * + Copyright (c) 2006-2019 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 WARRANTIES + 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 "mio-nwif.h" +#include "mio-prv.h" + +#if defined(_WIN32) + /* TODO: */ +#elif defined(__OS2__) + /* TODO: */ +#elif defined(__DOS__) + /* TODO: */ +#else +# include +# include +# include +# if defined(HAVE_SYS_IOCTL_H) +# include +# endif +# if defined(HAVE_NET_IF_H) +# include +# endif +# if defined(HAVE_SYS_SOCKIO_H) +# include +# endif +# if !defined(IF_NAMESIZE) +# define IF_NAMESIZE 63 +# endif +#endif + +#if defined(SIOCGIFCONF) && (defined(SIOCGIFANUM) || defined(SIOCGIFNUM)) +static int get_sco_ifconf (mio_t* mio, struct ifconf* ifc) +{ + /* SCO doesn't have have any IFINDEX thing. + * i emultate it using IFCONF */ + int h, num; + struct ifreq* ifr; + + h = socket(AF_INET, SOCK_DGRAM, 0); + if (h <= -1) + { + mio_seterrwithsyserr (mio, 0, errno); + return -1; + } + + ifc->ifc_len = 0; + ifc->ifc_buf = MIO_NULL; + + #if defined(SIOCGIFANUM) + if (ioctl(h, SIOCGIFANUM, &num) <= -1) + { + mio_seterrwithsyserr (mio, 0, errno); + goto oops; + } + #else + if (ioctl(h, SIOCGIFNUM, &num) <= -1) + { + mio_seterrwithsyserr (mio, 0, errno); + goto oops; + } + #endif + + /* sco needs reboot when you add an network interface. + * it should be safe not to consider the case when the interface + * is added after SIOCGIFANUM above. + * another thing to note is that SIOCGIFCONF ends with segfault + * if the buffer is not large enough unlike some other OSes + * like opensolaris which truncates the configuration. */ + + ifc->ifc_len = num * MIO_SIZEOF(*ifr); + ifc->ifc_buf = mio_allocmem(mio, ifc->ifc_len); + if (ifc->ifc_buf == MIO_NULL) goto oops; + + if (ioctl(h, SIOCGIFCONF, ifc) <= -1) + { + mio_seterrwithsyserr (mio, 0, errno); + goto oops; + } + close (h); h = -1; + + return 0; + +oops: + if (ifc->ifc_buf) mio_freemem (mio, ifc->ifc_buf); + if (h >= 0) close (h); + return -1; +} + +static MIO_INLINE void free_sco_ifconf (mio_t* mio, struct ifconf* ifc) +{ + mio_freemem (mio, ifc->ifc_buf); +} +#endif + + +int mio_bcstrtoifindex (mio_t* mio, const mio_bch_t* ptr, unsigned int* index) +{ +#if defined(_WIN32) + /* TODO: */ + mio_seterrnum (mio, MIO_ENOIMPL); + return -1; +#elif defined(__OS2__) + /* TODO: */ + mio_seterrnum (mio, MIO_ENOIMPL); + return -1; +#elif defined(__DOS__) + /* TODO: */ + mio_seterrnum (mio, MIO_ENOIMPL); + return -1; + +#elif defined(SIOCGIFINDEX) + int h, x; + mio_oow_t len; + struct ifreq ifr; + + h = socket(AF_INET, SOCK_DGRAM, 0); + if (h <= -1) + { + mio_seterrwithsyserr (mio, 0, errno); + return -1; + } + + MIO_MEMSET (&ifr, 0, MIO_SIZEOF(ifr)); + len = mio_copy_bcstr(ifr.ifr_name, MIO_COUNTOF(ifr.ifr_name), ptr); + if (ptr[len] != '\0') return -1; /* name too long */ + + x = ioctl(h, SIOCGIFINDEX, &ifr); + close (h); + + if (x >= 0) + { + #if defined(HAVE_STRUCT_IFREQ_IFR_IFINDEX) + *index = ifr.ifr_ifindex; + #else + *index = ifr.ifr_index; + #endif + } + + return x; + +#elif defined(HAVE_IF_NAMETOINDEX) + mio_bch_t tmp[IF_NAMESIZE + 1]; + mio_oow_t len; + unsigned int tmpidx; + + len = mio_copy_bcstr(tmp, MIO_COUNTOF(tmp), ptr); + if (ptr[len] != '\0') return -1; /* name too long */ + + tmpidx = if_nametoindex(tmp); + if (tmpidx == 0) + { + mio_seterrwithsyserr (mio, 0, errno); + return -1; + } + *index = tmpidx; + return 0; + +#elif defined(SIOCGIFCONF) && (defined(SIOCGIFANUM) || defined(SIOCGIFNUM)) + + struct ifconf ifc; + int num, i; + + if (get_sco_ifconf(mio, &ifc) <= -1) return -1; + + num = ifc.ifc_len / MIO_SIZEOF(struct ifreq); + for (i = 0; i < num; i++) + { + if (mio_comp_bcstr(ptr, ifc.ifc_req[i].ifr_name) == 0) + { + free_sco_ifconf (mio, &ifc); + *index = i + 1; + return 0; + } + } + + free_sco_ifconf (mio, &ifc); + return -1; + +#else + return -1; +#endif +} + +int mio_bcharstoifindex (mio_t* mio, const mio_bch_t* ptr, mio_oow_t len, unsigned int* index) +{ +#if defined(_WIN32) + /* TODO: */ + mio_seterrnum (mio, MIO_ENOIMPL); + return -1; +#elif defined(__OS2__) + /* TODO: */ + mio_seterrnum (mio, MIO_ENOIMPL); + return -1; +#elif defined(__DOS__) + /* TODO: */ + mio_seterrnum (mio, MIO_ENOIMPL); + return -1; + +#elif defined(SIOCGIFINDEX) + int h, x; + struct ifreq ifr; + + h = socket(AF_INET, SOCK_DGRAM, 0); + if (h <= -1) + { + mio_seterrwithsyserr (mio, 0, errno); + return -1; + } + + MIO_MEMSET (&ifr, 0, MIO_SIZEOF(ifr)); + if (mio_copy_bchars_to_bcstr(ifr.ifr_name, MIO_COUNTOF(ifr.ifr_name), ptr, len) < len) return -1; /* name too long */ + + x = ioctl(h, SIOCGIFINDEX, &ifr); + close (h); + + if (x >= 0) + { + #if defined(HAVE_STRUCT_IFREQ_IFR_IFINDEX) + *index = ifr.ifr_ifindex; + #else + *index = ifr.ifr_index; + #endif + } + + return x; + +#elif defined(HAVE_IF_NAMETOINDEX) + mio_bch_t tmp[IF_NAMESIZE + 1]; + unsigned int tmpidx; + + if (mio_copy_bchars_to_bcstr(tmp, MIO_COUNTOF(tmp), ptr, len) < len) return -1; + + tmpidx = if_nametoindex(tmp); + if (tmpidx == 0) + { + mio_seterrwithsyserr (mio, 0, errno); + return -1; + } + *index = tmpidx; + return 0; + +#elif defined(SIOCGIFCONF) && (defined(SIOCGIFANUM) || defined(SIOCGIFNUM)) + struct ifconf ifc; + int num, i; + + if (get_sco_ifconf(mio, &ifc) <= -1) return -1; + + num = ifc.ifc_len / MIO_SIZEOF(struct ifreq); + for (i = 0; i < num; i++) + { + if (mio_comp_bchars_bcstr(ptr, len, ifc.ifc_req[i].ifr_name) == 0) + { + free_sco_ifconf (mio, &ifc); + *index = i + 1; + return 0; + } + } + + free_sco_ifconf (mio, &ifc); + return -1; + +#else + return -1; +#endif +} + +int mio_ucstrtoifindex (mio_t* mio, const mio_uch_t* ptr, unsigned int* index) +{ +#if defined(_WIN32) + /* TODO: */ + mio_seterrnum (mio, MIO_ENOIMPL); + return -1; +#elif defined(__OS2__) + /* TODO: */ + mio_seterrnum (mio, MIO_ENOIMPL); + return -1; +#elif defined(__DOS__) + /* TODO: */ + mio_seterrnum (mio, MIO_ENOIMPL); + return -1; + +#elif defined(SIOCGIFINDEX) + int h, x; + struct ifreq ifr; + mio_oow_t wl, ml; + + h = socket(AF_INET, SOCK_DGRAM, 0); + if (h <= -1) + { + mio_seterrwithsyserr (mio, 0, errno); + return -1; + } + + ml = MIO_COUNTOF(ifr.ifr_name); + if (mio_convutobcstr(mio, ptr, &wl, ifr.ifr_name, &ml) <= -1) return -1; + + x = ioctl(h, SIOCGIFINDEX, &ifr); + close (h); + + if (x >= 0) + { + #if defined(HAVE_STRUCT_IFREQ_IFR_IFINDEX) + *index = ifr.ifr_ifindex; + #else + *index = ifr.ifr_index; + #endif + } + + return x; + +#elif defined(HAVE_IF_NAMETOINDEX) + mio_bch_t tmp[IF_NAMESIZE + 1]; + mio_oow_t wl, ml; + unsigned int tmpidx; + + ml = MIO_COUNTOF(tmp); + if (mio_convutobcstr(mio, ptr, &wl, tmp, &ml) <= -1) return -1; + + tmpidx = if_nametoindex(tmp); + if (tmpidx == 0) + { + mio_seterrwithsyserr (mio, 0, errno); + return -1; + } + *index = tmpidx; + return 0; + +#elif defined(SIOCGIFCONF) && (defined(SIOCGIFANUM) || defined(SIOCGIFNUM)) + + struct ifconf ifc; + int num, i; + mio_bch_t tmp[IF_NAMESIZE + 1]; + mio_oow_t wl, ml; + + ml = MIO_COUNTOF(tmp); + if (mio_convutobcstr(mio, ptr, &wl, tmp, &ml) <= -1) return -1; + + if (get_sco_ifconf(mio, &ifc) <= -1) return -1; + + num = ifc.ifc_len / MIO_SIZEOF(struct ifreq); + for (i = 0; i < num; i++) + { + if (mio_comp_bcstr(tmp, ifc.ifc_req[i].ifr_name) == 0) + { + free_sco_ifconf (mio, &ifc); + *index = i + 1; + return 0; + } + } + + free_sco_ifconf (mio, &ifc); + return -1; + +#else + return -1; +#endif +} + +int mio_ucharstoifindex (mio_t* mio, const mio_uch_t* ptr, mio_oow_t len, unsigned int* index) +{ +#if defined(_WIN32) + /* TODO: */ + mio_seterrnum (mio, MIO_ENOIMPL); + return -1; +#elif defined(__OS2__) + /* TODO: */ + mio_seterrnum (mio, MIO_ENOIMPL); + return -1; +#elif defined(__DOS__) + /* TODO: */ + mio_seterrnum (mio, MIO_ENOIMPL); + return -1; + +#elif defined(SIOCGIFINDEX) + int h, x; + struct ifreq ifr; + mio_oow_t wl, ml; + + h = socket(AF_INET, SOCK_DGRAM, 0); + if (h <= -1) + { + mio_seterrwithsyserr (mio, 0, errno); + return -1; + } + + wl = len; ml = MIO_COUNTOF(ifr.ifr_name) - 1; + if (mio_convutobchars(mio, ptr, &wl, ifr.ifr_name, &ml) <= -1) return -1; + ifr.ifr_name[ml] = '\0'; + + x = ioctl(h, SIOCGIFINDEX, &ifr); + close (h); + + if (x >= 0) + { + #if defined(HAVE_STRUCT_IFREQ_IFR_IFINDEX) + *index = ifr.ifr_ifindex; + #else + *index = ifr.ifr_index; + #endif + } + + return x; + +#elif defined(HAVE_IF_NAMETOINDEX) + mio_bch_t tmp[IF_NAMESIZE + 1]; + mio_oow_t wl, ml; + unsigned int tmpidx; + + wl = len; ml = MIO_COUNTOF(tmp) - 1; + if (mio_convutobchars(mio, ptr, &wl, tmp, &ml) <= -1) return -1; + tmp[ml] = '\0'; + + tmpidx = if_nametoindex(tmp); + if (tmpidx == 0) + { + mio_seterrwithsyserr (mio, 0, errno); + return -1; + } + *index = tmpidx; + return 0; + +#elif defined(SIOCGIFCONF) && (defined(SIOCGIFANUM) || defined(SIOCGIFNUM)) + struct ifconf ifc; + int num, i; + mio_bch_t tmp[IF_NAMESIZE + 1]; + mio_oow_t wl, ml; + + wl = len; ml = MIO_COUNTOF(tmp) - 1; + if (mio_convutobchars(ptr, &wl, tmp, &ml) <= -1) return -1; + tmp[ml] = '\0'; + + if (get_sco_ifconf(mio, &ifc) <= -1) return -1; + + num = ifc.ifc_len / MIO_SIZEOF(struct ifreq); + for (i = 0; i < num; i++) + { + if (mio_comp_bcstr(tmp, ifc.ifc_req[i].ifr_name) == 0) + { + free_sco_ifconf (mio, &ifc); + *index = i + 1; + return 0; + } + } + + free_sco_ifconf (mio, &ifc); + return -1; +#else + return -1; +#endif +} + +/* ---------------------------------------------------------- */ + +int mio_ifindextobcstr (mio_t* mio, unsigned int index, mio_bch_t* buf, mio_oow_t len) +{ +#if defined(_WIN32) + /* TODO: */ + mio_seterrnum (mio, MIO_ENOIMPL); + return -1; +#elif defined(__OS2__) + /* TODO: */ + mio_seterrnum (mio, MIO_ENOIMPL); + return -1; +#elif defined(__DOS__) + /* TODO: */ + mio_seterrnum (mio, MIO_ENOIMPL); + return -1; + +#elif defined(SIOCGIFNAME) + + int h, x; + struct ifreq ifr; + + h = socket(AF_INET, SOCK_DGRAM, 0); + if (h <= -1) + { + mio_seterrwithsyserr (mio, 0, errno); + return -1; + } + + MIO_MEMSET (&ifr, 0, MIO_SIZEOF(ifr)); + #if defined(HAVE_STRUCT_IFREQ_IFR_IFINDEX) + ifr.ifr_ifindex = index; + #else + ifr.ifr_index = index; + #endif + + x = ioctl(h, SIOCGIFNAME, &ifr); + close (h); + + if (x <= -1) + { + mio_seterrwithsyserr (mio, 0, errno); + return -1; + } + + return mio_copy_bcstr(buf, len, ifr.ifr_name); + +#elif defined(HAVE_IF_INDEXTONAME) + mio_bch_t tmp[IF_NAMESIZE + 1]; + if (if_indextoname (index, tmp) == MIO_NULL) + { + mio_seterrwithsyserr (mio, 0, errno); + return -1; + } + return mio_copy_bcstr(buf, len, tmp); + +#elif defined(SIOCGIFCONF) && (defined(SIOCGIFANUM) || defined(SIOCGIFNUM)) + + struct ifconf ifc; + mio_oow_t ml; + int num; + + if (index <= 0) return -1; + if (get_sco_ifconf(mio, &ifc) <= -1) return -1; + + num = ifc.ifc_len / MIO_SIZEOF(struct ifreq); + if (index > num) + { + mio_seterrnum (mio, MIO_ENOENT); + free_sco_ifconf (mio, &ifc); + return -1; + } + + ml = mio_copy_bcstr(buf, len, ifc.ifc_req[index - 1].ifr_name); + free_sco_ifconf (mio, &ifc); + return ml; + +#else + return -1; +#endif +} + +int mio_ifindextoucstr (mio_t* mio, unsigned int index, mio_uch_t* buf, mio_oow_t len) +{ +#if defined(_WIN32) + /* TODO: */ + mio_seterrnum (mio, MIO_ENOIMPL); + return -1; +#elif defined(__OS2__) + /* TODO: */ + mio_seterrnum (mio, MIO_ENOIMPL); + return -1; +#elif defined(__DOS__) + /* TODO: */ + mio_seterrnum (mio, MIO_ENOIMPL); + return -1; + +#elif defined(SIOCGIFNAME) + + int h, x; + struct ifreq ifr; + mio_oow_t wl, ml; + + h = socket(AF_INET, SOCK_DGRAM, 0); + if (h <= -1) + { + mio_seterrwithsyserr (mio, 0, errno); + return -1; + } + + MIO_MEMSET (&ifr, 0, MIO_SIZEOF(ifr)); + #if defined(HAVE_STRUCT_IFREQ_IFR_IFINDEX) + ifr.ifr_ifindex = index; + #else + ifr.ifr_index = index; + #endif + + x = ioctl(h, SIOCGIFNAME, &ifr); + close (h); + + if (x <= -1) + { + mio_seterrwithsyserr (mio, 0, errno); + return -1; + } + + wl = len; + x = mio_convbtoucstr(mio, ifr.ifr_name, &ml, buf, &wl, 0); + if (x == -2 && wl > 1) buf[wl - 1] = '\0'; + else if (x != 0) return -1; + return wl; + +#elif defined(HAVE_IF_INDEXTONAME) + mio_bch_t tmp[IF_NAMESIZE + 1]; + mio_oow_t ml, wl; + int x; + + if (if_indextoname(index, tmp) == MIO_NULL) + { + mio_seterrwithsyserr (mio, 0, errno); + return -1; + } + wl = len; + x = mio_convbtoucstr(mio, tmp, &ml, buf, &wl, 0); + if (x == -2 && wl > 1) buf[wl - 1] = '\0'; + else if (x != 0) return -1; + return wl; + +#elif defined(SIOCGIFCONF) && (defined(SIOCGIFANUM) || defined(SIOCGIFNUM)) + + struct ifconf ifc; + mio_oow_t wl, ml; + int num, x; + + if (index <= 0) return -1; + if (get_sco_ifconf(mio, &ifc) <= -1) return -1; + + num = ifc.ifc_len / MIO_SIZEOF(struct ifreq); + if (index > num) + { + free_sco_ifconf (mio, &ifc); + return -1; + } + + wl = len; + x = mio_convbtoucstr(ifc.ifc_req[index - 1].ifr_name, &ml, buf, &wl, 0); + free_sco_ifconf (mio, &ifc); + + if (x == -2 && wl > 1) buf[wl - 1] = '\0'; + else if (x != 0) return -1; + + return wl; +#else + return -1; +#endif +} + diff --git a/mio/lib/skad.c b/mio/lib/skad.c index e8c2c06..571a8b8 100644 --- a/mio/lib/skad.c +++ b/mio/lib/skad.c @@ -25,6 +25,7 @@ */ #include "mio-skad.h" +#include "mio-nwif.h" #include "mio-prv.h" #include @@ -296,7 +297,7 @@ int mio_oocharstoskad (mio_t* mio, const mio_ooch_t* str, mio_oow_t len, mio_ska const mio_ooch_t* stmp = p; unsigned int index; do p++; while (p < end && *p != ']'); -/* TODO if (mio_ucharstoifindex(mio, stmp, p - stmp, &index) <= -1) return -1; */ + if (mio_ucharstoifindex(mio, stmp, p - stmp, &index) <= -1) return -1; skad->in6.sin6_scope_id = index; } @@ -369,7 +370,7 @@ int mio_oocharstoskad (mio_t* mio, const mio_ooch_t* str, mio_oow_t len, mio_ska const mio_ooch_t* stmp = p; unsigned int index; do p++; while (p < end); -/* TODO if (mio_ucharstoifindex(mio, stmp, p - stmp, &index) <= -1) return -1;*/ + if (mio_ucharstoifindex(mio, stmp, p - stmp, &index) <= -1) return -1; skad->in6.sin6_scope_id = index; } } @@ -653,7 +654,7 @@ mio_oow_t mio_skadtoucstr (mio_t* mio, const mio_skad_t* _skad, mio_uch_t* buf, if (xlen + 1 >= len) goto done; -/* TODO tmp = mio_ifindextoucstr(mio, skad->in6.sin6_scope_id, &buf[xlen], len - xlen);*/ + tmp = mio_ifindextoucstr(mio, skad->in6.sin6_scope_id, &buf[xlen], len - xlen); if (tmp <= -1) { /* TODO xlen += mio_fmt_uintmax_to_ucstr(&buf[xlen], len - xlen, skad->in6.sin6_scope_id, 10, 0, '\0', MIO_NULL); */ @@ -900,7 +901,7 @@ mio_oow_t mio_skadtobcstr (mio_t* mio, const mio_skad_t* _skad, mio_bch_t* buf, if (xlen + 1 >= len) goto done; - /* TODO tmp = mio_ifindextobcstr(mio, skad->in6.sin6_scope_id, &buf[xlen], len - xlen);*/ + tmp = mio_ifindextobcstr(mio, skad->in6.sin6_scope_id, &buf[xlen], len - xlen); if (tmp <= -1) { /* TODO xlen += mio_fmt_uintmax_to_bcstr(&buf[xlen], len - xlen, skad->in6.sin6_scope_id, 10, 0, '\0', MIO_NULL); */ diff --git a/mio/lib/utl.c b/mio/lib/utl.c index 1a455f4..e8e386a 100644 --- a/mio/lib/utl.c +++ b/mio/lib/utl.c @@ -496,6 +496,8 @@ int mio_comp_bchars_ucstr (const mio_bch_t* str1, mio_oow_t len, const mio_uch_t return (str1 < end)? 1: (*str2 == '\0'? 0: -1); } +/* ========================================================================= */ + void mio_copy_uchars (mio_uch_t* dst, const mio_uch_t* src, mio_oow_t len) { /* take note of no forced null termination */ @@ -513,11 +515,55 @@ void mio_copy_bchars (mio_bch_t* dst, const mio_bch_t* src, mio_oow_t len) void mio_copy_bchars_to_uchars (mio_uch_t* dst, const mio_bch_t* src, mio_oow_t len) { /* copy without conversions. - * use mio_bctouchars() for conversion encoding */ + * use mio_convbtouchars() for conversion encoding */ mio_oow_t i; for (i = 0; i < len; i++) dst[i] = src[i]; } +void mio_copy_uchars_to_bchars (mio_bch_t* dst, const mio_uch_t* src, mio_oow_t len) +{ + /* copy without conversions. + * use mio_convutobchars() for conversion encoding */ + mio_oow_t i; + for (i = 0; i < len; i++) dst[i] = src[i]; +} + +mio_oow_t mio_copy_uchars_to_ucstr (mio_uch_t* dst, mio_uch_t dlen, const mio_uch_t* src, mio_oow_t slen) +{ + mio_oow_t i; + if (dlen <= 0) return 0; + if (dlen <= slen) slen = dlen - 1; + for (i = 0; i < slen; i++) dst[i] = src[i]; + dst[i] = '\0'; + return i; +} + +mio_oow_t mio_copy_bchars_to_bcstr (mio_bch_t* dst, mio_bch_t dlen, const mio_bch_t* src, mio_oow_t slen) +{ + mio_oow_t i; + if (dlen <= 0) return 0; + if (dlen <= slen) slen = dlen - 1; + for (i = 0; i < slen; i++) dst[i] = src[i]; + dst[i] = '\0'; + return i; +} + +mio_oow_t mio_copy_uchars_to_ucstr_unlimited (mio_uch_t* dst, const mio_uch_t* src, mio_oow_t len) +{ + mio_oow_t i; + for (i = 0; i < len; i++) dst[i] = src[i]; + dst[i] = '\0'; + return i; +} + +mio_oow_t mio_copy_bchars_to_bcstr_unlimited (mio_bch_t* dst, const mio_bch_t* src, mio_oow_t len) +{ + mio_oow_t i; + for (i = 0; i < len; i++) dst[i] = src[i]; + dst[i] = '\0'; + return i; +} + mio_oow_t mio_copy_ucstr (mio_uch_t* dst, mio_oow_t len, const mio_uch_t* src) { mio_uch_t* p, * p2; @@ -550,6 +596,22 @@ mio_oow_t mio_copy_bcstr (mio_bch_t* dst, mio_oow_t len, const mio_bch_t* src) return p - dst; } + +mio_oow_t mio_copy_ucstr_unlimited (mio_uch_t* dst, const mio_uch_t* src) +{ + mio_uch_t* org = dst; + while ((*dst++ = *src++) != '\0'); + return dst - org - 1; +} + +mio_oow_t mio_copy_bcstr_unlimited (mio_bch_t* dst, const mio_bch_t* src) +{ + mio_bch_t* org = dst; + while ((*dst++ = *src++) != '\0'); + return dst - org - 1; +} + +/* ========================================================================= */ void mio_fill_uchars (mio_uch_t* dst, mio_uch_t ch, mio_oow_t len) { mio_oow_t i;