Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src add getifaddrs(3) from bsdi4, as mentioned in tech-net (in S...



details:   https://anonhg.NetBSD.org/src/rev/5eefb2224faa
branches:  trunk
changeset: 482843:5eefb2224faa
user:      itojun <itojun%NetBSD.org@localhost>
date:      Wed Feb 23 06:57:46 2000 +0000

description:
add getifaddrs(3) from bsdi4, as mentioned in tech-net (in SIOCGIFCONF
related thread).
this will save future apps from complexity of SIOCGIFCONF.

getifaddrs(3) does not use SIOCGIFCONF internally, it uses sysctl.

diffstat:

 include/Makefile          |    5 +-
 include/ifaddrs.h         |   55 ++++++
 lib/libc/net/Makefile.inc |    8 +-
 lib/libc/net/getifaddrs.3 |  161 ++++++++++++++++++++
 lib/libc/net/getifaddrs.c |  369 ++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 593 insertions(+), 5 deletions(-)

diffs (truncated from 643 to 300 lines):

diff -r c8698530e255 -r 5eefb2224faa include/Makefile
--- a/include/Makefile  Wed Feb 23 03:44:02 2000 +0000
+++ b/include/Makefile  Wed Feb 23 06:57:46 2000 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile,v 1.82 2000/01/12 05:02:11 mjl Exp $
+#      $NetBSD: Makefile,v 1.83 2000/02/23 06:57:47 itojun Exp $
 #      @(#)Makefile    8.2 (Berkeley) 1/4/94
 
 SRCTOP=        ..
@@ -10,7 +10,8 @@
 
 INCS=  a.out.h ar.h assert.h bitstring.h bm.h cpio.h ctype.h db.h dirent.h \
        disktab.h dlfcn.h err.h errno.h fmtmsg.h fnmatch.h fstab.h fts.h \
-       glob.h grp.h hesiod.h ieeefp.h iso646.h kvm.h langinfo.h libgen.h \
+       glob.h grp.h hesiod.h ieeefp.h ifaddrs.h \
+       iso646.h kvm.h langinfo.h libgen.h \
        limits.h link.h link_aout.h link_elf.h locale.h \
        login_cap.h malloc.h math.h md4.h \
        memory.h mpool.h ndbm.h netdb.h netgroup.h nlist.h nl_types.h \
diff -r c8698530e255 -r 5eefb2224faa include/ifaddrs.h
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/include/ifaddrs.h Wed Feb 23 06:57:46 2000 +0000
@@ -0,0 +1,55 @@
+/*     $NetBSD: ifaddrs.h,v 1.1 2000/02/23 06:57:47 itojun Exp $       */
+
+/*
+ * Copyright (c) 1995, 1999
+ *     Berkeley Software Design, Inc.  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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``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 Berkeley Software Design, Inc. 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.
+ *
+ *     BSDI ifaddrs.h,v 2.4 1999/03/15 20:59:28 jch Exp
+ */
+
+#ifndef        _IFADDRS_H_
+#define        _IFADDRS_H_
+
+struct ifaddrs {
+       struct ifaddrs  *ifa_next;
+       char            *ifa_name;
+       u_int            ifa_flags;
+       struct sockaddr *ifa_addr;
+       struct sockaddr *ifa_netmask;
+       struct sockaddr *ifa_dstaddr;
+       void            *ifa_data;
+};
+
+/*
+ * This may have been defined in <net/if.h>.  Note that if <net/if.h> is
+ * to be included it must be included before this header file.
+ */
+#ifndef        ifa_broadaddr
+#define        ifa_broadaddr   ifa_dstaddr     /* broadcast address interface */
+#endif
+
+#include <sys/cdefs.h>
+
+__BEGIN_DECLS
+extern int getifaddrs __P((struct ifaddrs **));
+__END_DECLS
+
+#endif
diff -r c8698530e255 -r 5eefb2224faa lib/libc/net/Makefile.inc
--- a/lib/libc/net/Makefile.inc Wed Feb 23 03:44:02 2000 +0000
+++ b/lib/libc/net/Makefile.inc Wed Feb 23 06:57:46 2000 +0000
@@ -1,10 +1,11 @@
-#      $NetBSD: Makefile.inc,v 1.49 2000/02/09 12:23:55 itojun Exp $
+#      $NetBSD: Makefile.inc,v 1.50 2000/02/23 06:57:46 itojun Exp $
 #      @(#)Makefile.inc        8.2 (Berkeley) 9/5/93
 
 # net sources
 .PATH: ${ARCHDIR}/net ${.CURDIR}/net
 
-SRCS+= base64.c ethers.c gethnamaddr.c getnetnamadr.c getnetent.c getproto.c \
+SRCS+= base64.c ethers.c gethnamaddr.c getifaddrs.c getnetnamadr.c \
+       getnetent.c getproto.c \
        getprotoent.c getprotoname.c getservbyname.c getservbyport.c \
        getservent.c herror.c hesiod.c inet_lnaof.c inet_makeaddr.c \
        inet_net_ntop.c inet_net_pton.c inet_neta.c inet_ntop.c inet_pton.c \
@@ -37,7 +38,8 @@
 
 .include "${ARCHDIR}/net/Makefile.inc"
 
-MAN+=  byteorder.3 ethers.3 gethostbyname.3 getnetent.3 getprotoent.3 \
+MAN+=  byteorder.3 ethers.3 gethostbyname.3 getifaddrs.3 \
+       getnetent.3 getprotoent.3 \
        getservent.3 hesiod.3 inet.3 inet_net.3 iso_addr.3 linkaddr.3 ns.3 \
        nsdispatch.3 rcmd.3 resolver.3
 
diff -r c8698530e255 -r 5eefb2224faa lib/libc/net/getifaddrs.3
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/libc/net/getifaddrs.3 Wed Feb 23 06:57:46 2000 +0000
@@ -0,0 +1,161 @@
+.\"    $NetBSD: getifaddrs.3,v 1.1 2000/02/23 06:57:46 itojun Exp $
+.\"    BSDI    getifaddrs.3,v 2.4 1999/03/15 20:57:20 jch Exp
+.\"
+.\" Copyright (c) 1995, 1999
+.\"    Berkeley Software Design, Inc.  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.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``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 Berkeley Software Design, Inc. 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.
+.Dd "October 12, 1995"
+.Dt GETIFADDRS 3
+.Sh NAME
+.Nm getifaddrs
+.Nd get interface addresses
+.Sh SYNOPSIS
+.Fd #include <sys/types.h>
+.Fd #include <sys/socket.h>
+.Fd #include <ifaddrs.h>
+.Ft int
+.Fn getifaddrs "struct ifaddrs **ifap"
+.Sh DESCRIPTION
+The
+.Fn getifaddrs
+function stores a reference to a linked list of the network interfaces
+on the local machine in the memory referenced by
+.Fa ifap .
+The list consists of
+.Nm ifaddrs
+structures, as defined in the include file
+.Aq Pa ifaddrs.h .
+The
+.Nm ifaddrs
+structure contains at least the following entries:
+.Bd -literal
+    struct ifaddrs   *ifa_next;         /* Pointer to next struct */
+    char             *ifa_name;         /* Interface name */
+    u_int             ifa_flags;        /* Interface flags */
+    struct sockaddr  *ifa_addr;         /* Interface address */
+    struct sockaddr  *ifa_netmask;      /* Interface netmask */
+    struct sockaddr  *ifa_broadaddr;    /* Interface broadcast address */
+    struct sockaddr  *ifa_dstaddr;      /* P2P interface destination */
+    void             *ifa_data;                /* Address specific data */
+.Ed
+.Pp
+The
+.Li ifa_next
+field contains a pointer to the next structure on the list.
+This field is
+.Dv NULL
+in last structure on the list.
+.Pp
+The
+.Li ifa_name
+field contains the interface name.
+.Pp
+The
+.Li ifa_flags
+field contains the interface flags, as set by
+.Xr ifconfig 8
+utility.
+.Pp
+The
+.Li ifa_addr
+field references either the address of the interface or the link level
+address of the interface, if one exists, otherwise it is NULL.
+(The
+.Li sa_family
+field of the
+.Li ifa_addr 
+field should be consulted to determine the format of the
+.Li ifa_addr
+address.)
+.Pp
+The
+.Li ifa_netmask
+field references the netmask associated with
+.Li ifa_addr ,
+if one is set, otherwise it is NULL.
+.Pp
+The
+.Li ifa_broadaddr
+field,
+which should only be referenced for non-P2P interfaces,
+references the broadcast address associated with
+.Li ifa_addr ,
+if one exists, otherwise it is NULL.
+.Pp
+The
+.Li ifa_dstaddr
+field references the destination address on a P2P interface,
+if one exists, otherwise it is NULL.
+.Pp
+The
+.Li ifa_data
+field references address family specific data.  For 
+.Dv AF_LINK
+addresses it contains a pointer to the
+.Fa struct if_data
+.Pq as defined in include file Aq Pa net/if.h
+which contains various interface attributes and statistics.
+For all other address families, it contains a pointer to the
+.Fa struct ifa_data
+.Pq as defined in include file Aq Pa net/if.h
+which contains per-address interface statistics.
+.Pp
+The data returned by
+.Fn getifaddrs
+is dynamically allocated and should be freed using
+.Fn free
+.Pq see Xr free 3
+when no longer needed.
+.Sh RETURN VALUES
+Upon successful completion, a value of 0 is returned.
+Otherwise, a value of -1 is returned and
+.Va errno
+is set to indicate the error.
+.Sh ERRORS
+The
+.Fn getifaddrs
+may fail and set
+.Va errno
+for any of the errors specified for the library routines
+.Xr ioctl 2 ,
+.Xr socket 2 ,
+.Xr malloc 3
+or
+.Xr sysctl 3 .
+.Sh BUGS
+If both
+.Aq Pa net/if.h
+and
+.Aq Pa ifaddrs.h
+are being included,
+.Aq Pa net/if.h
+.Em must
+be included before
+.Aq Pa ifaddrs.h .
+.Sh SEE ALSO
+.Xr ioctl 2 ,
+.Xr socket 2 ,
+.Xr sysctl 3 ,
+.Xr networking 4 ,
+.Xr ifconfig 8
+.Sh HISTORY
+The
+.Nm
+implementation first appeared in BSDI BSD/OS.
diff -r c8698530e255 -r 5eefb2224faa lib/libc/net/getifaddrs.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/libc/net/getifaddrs.c Wed Feb 23 06:57:46 2000 +0000
@@ -0,0 +1,369 @@
+/*     $NetBSD: getifaddrs.c,v 1.1 2000/02/23 06:57:46 itojun Exp $    */
+
+/*
+ * Copyright (c) 1995, 1999
+ *     Berkeley Software Design, Inc.  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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``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 Berkeley Software Design, Inc. 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.
+ *
+ *     BSDI getifaddrs.c,v 2.11 1999/03/15 20:57:20 jch Exp
+ */



Home | Main Index | Thread Index | Old Index