10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
58485SPeter.Memishian@Sun.COM  * Common Development and Distribution License (the "License").
68485SPeter.Memishian@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211254Smeem 
220Sstevel@tonic-gate /*
23*12016SGirish.Moodalbail@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #ifndef _LIBINETUTIL_H
280Sstevel@tonic-gate #define	_LIBINETUTIL_H
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
311254Smeem  * Contains SMI-private API for general Internet functionality
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #ifdef	__cplusplus
350Sstevel@tonic-gate extern "C" {
360Sstevel@tonic-gate #endif
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include <netinet/inetutil.h>
390Sstevel@tonic-gate #include <sys/types.h>
400Sstevel@tonic-gate #include <sys/socket.h>
410Sstevel@tonic-gate #include <netinet/in.h>
420Sstevel@tonic-gate #include <net/if.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #if !defined(_KERNEL) && !defined(_BOOT)
450Sstevel@tonic-gate 
460Sstevel@tonic-gate typedef struct {
470Sstevel@tonic-gate 	uint_t		ifsp_ppa;	/* Physical Point of Attachment */
480Sstevel@tonic-gate 	uint_t		ifsp_lun;	/* Logical Unit number */
490Sstevel@tonic-gate 	boolean_t	ifsp_lunvalid;	/* TRUE if lun is valid */
500Sstevel@tonic-gate 	char		ifsp_devnm[LIFNAMSIZ];	/* only the device name */
510Sstevel@tonic-gate } ifspec_t;
520Sstevel@tonic-gate 
53*12016SGirish.Moodalbail@Sun.COM extern boolean_t	ifparse_ifspec(const char *, ifspec_t *);
54*12016SGirish.Moodalbail@Sun.COM extern void		get_netmask4(const struct in_addr *, struct in_addr *);
55*12016SGirish.Moodalbail@Sun.COM extern boolean_t	sockaddrcmp(const struct sockaddr_storage *,
56*12016SGirish.Moodalbail@Sun.COM 			    const struct sockaddr_storage *);
57*12016SGirish.Moodalbail@Sun.COM extern int		plen2mask(uint_t, sa_family_t,
58*12016SGirish.Moodalbail@Sun.COM 			    struct sockaddr_storage *);
59*12016SGirish.Moodalbail@Sun.COM extern int		mask2plen(const struct sockaddr_storage *);
60*12016SGirish.Moodalbail@Sun.COM extern boolean_t	sockaddrunspec(const struct sockaddr_storage *);
610Sstevel@tonic-gate 
620Sstevel@tonic-gate /*
631254Smeem  * Extended version of the classic BSD ifaddrlist() interface:
641254Smeem  *
658485SPeter.Memishian@Sun.COM  *    int ifaddrlist(struct ifaddrlist **addrlistp, int af, uint_t flags,
668485SPeter.Memishian@Sun.COM  *	             char *errbuf);
671254Smeem  *
681254Smeem  * 	* addrlistp: Upon success, ifaddrlist() sets *addrlistp to a
691254Smeem  *	  dynamically-allocated array of addresses.
701254Smeem  *
711254Smeem  *	* af: Either AF_INET to obtain IPv4 addresses, or AF_INET6 to
721254Smeem  *	  obtain IPv6 addresses.
731254Smeem  *
748485SPeter.Memishian@Sun.COM  *	* flags: LIFC_* flags that control the classes of interfaces that
758485SPeter.Memishian@Sun.COM  *	  will be visible.
768485SPeter.Memishian@Sun.COM  *
771254Smeem  *	* errbuf: A caller-supplied buffer of ERRBUFSIZE.  Upon failure,
781254Smeem  *	  provides the reason for the failure.
791254Smeem  *
801254Smeem  * Upon success, ifaddrlist() returns the number of addresses in the array
811254Smeem  * pointed to by `addrlistp'.  If the count is 0, then `addrlistp' is NULL.
821254Smeem  */
831254Smeem union any_in_addr {
841254Smeem 	struct in6_addr	addr6;
851254Smeem 	struct in_addr	addr;
861254Smeem };
871254Smeem 
881254Smeem struct ifaddrlist {
891254Smeem 	int		index;			/* interface index */
901254Smeem 	union any_in_addr addr;			/* interface address */
911254Smeem 	char		device[LIFNAMSIZ + 1];	/* interface name */
921254Smeem 	uint64_t	flags;			/* interface flags */
931254Smeem };
941254Smeem 
958485SPeter.Memishian@Sun.COM #define	ERRBUFSIZE 128			/* expected size of fourth argument */
968485SPeter.Memishian@Sun.COM 
978485SPeter.Memishian@Sun.COM extern int ifaddrlist(struct ifaddrlist **, int, uint_t, char *);
981254Smeem 
998485SPeter.Memishian@Sun.COM /*
1008485SPeter.Memishian@Sun.COM  * Similar to ifaddrlist(), but returns a linked-list of addresses for a
1018485SPeter.Memishian@Sun.COM  * *specific* interface name, and allows specific address flags to be matched
1028485SPeter.Memishian@Sun.COM  * against.  A linked list is used rather than an array so that information
1038485SPeter.Memishian@Sun.COM  * can grow over time without affecting binary compatibility.  Also, leaves
1048485SPeter.Memishian@Sun.COM  * error-handling up to the caller.  Returns the number of ifaddrlistx's
1058485SPeter.Memishian@Sun.COM  * chained through ifaddrp.
1068485SPeter.Memishian@Sun.COM  *
1078485SPeter.Memishian@Sun.COM  *    int ifaddrlistx(const char *ifname, uint64_t set, uint64_t clear,
1088485SPeter.Memishian@Sun.COM  *        ifaddrlistx_t **ifaddrp);
1098485SPeter.Memishian@Sun.COM  *
1108485SPeter.Memishian@Sun.COM  *	* ifname: Interface name to match against.
1118485SPeter.Memishian@Sun.COM  *
1128485SPeter.Memishian@Sun.COM  *	* set: One or more flags that must be set on the address for
1138485SPeter.Memishian@Sun.COM  *	  it to be returned.
1148485SPeter.Memishian@Sun.COM  *
1158485SPeter.Memishian@Sun.COM  *	* clear: Flags that must be clear on the address for it to be
1168485SPeter.Memishian@Sun.COM  *	  returned.
1178485SPeter.Memishian@Sun.COM  *
1188485SPeter.Memishian@Sun.COM  * 	* ifaddrp: Upon success, ifaddrlistx() sets *ifaddrp to the head
1198485SPeter.Memishian@Sun.COM  *	  of a dynamically-allocated array of ifaddrlistx structures.
1208485SPeter.Memishian@Sun.COM  *
1218485SPeter.Memishian@Sun.COM  * Once done, the caller must free `ifaddrp' by calling ifaddrlistx_free().
1228485SPeter.Memishian@Sun.COM  */
1238485SPeter.Memishian@Sun.COM typedef struct ifaddrlistx {
1248485SPeter.Memishian@Sun.COM 	struct ifaddrlistx	*ia_next;
1258485SPeter.Memishian@Sun.COM 	char			ia_name[LIFNAMSIZ];
1268485SPeter.Memishian@Sun.COM 	uint64_t		ia_flags;
1278485SPeter.Memishian@Sun.COM 	struct sockaddr_storage	ia_addr;
1288485SPeter.Memishian@Sun.COM } ifaddrlistx_t;
1298485SPeter.Memishian@Sun.COM 
1308485SPeter.Memishian@Sun.COM extern int ifaddrlistx(const char *, uint64_t, uint64_t, ifaddrlistx_t **);
1318485SPeter.Memishian@Sun.COM extern void ifaddrlistx_free(ifaddrlistx_t *);
1321254Smeem 
1331254Smeem /*
1340Sstevel@tonic-gate  * Timer queues
1350Sstevel@tonic-gate  *
1360Sstevel@tonic-gate  * timer queues are a facility for managing timeouts in unix.  in the
1370Sstevel@tonic-gate  * event driven model, unix provides us with poll(2)/select(3C), which
1380Sstevel@tonic-gate  * allow us to coordinate waiting on multiple descriptors with an
1390Sstevel@tonic-gate  * optional timeout.  however, often (as is the case with the DHCP
1400Sstevel@tonic-gate  * agent), we want to manage multiple independent timeouts (say, one
1410Sstevel@tonic-gate  * for waiting for an OFFER to come back from a server in response to
1420Sstevel@tonic-gate  * a DISCOVER sent out on one interface, and another for waiting for
1430Sstevel@tonic-gate  * the T1 time on another interface).  timer queues allow us to do
1440Sstevel@tonic-gate  * this in the event-driven model.
1450Sstevel@tonic-gate  *
1460Sstevel@tonic-gate  * note that timer queues do not in and of themselves provide the
1470Sstevel@tonic-gate  * event driven model (for instance, there is no handle_events()
1480Sstevel@tonic-gate  * routine).  they merely provide the hooks to support multiple
1490Sstevel@tonic-gate  * independent timeouts.  this is done for both simplicity and
1500Sstevel@tonic-gate  * applicability (for instance, while one approach would be to use
1510Sstevel@tonic-gate  * this timer queue with poll(2), another one would be to use SIGALRM
1520Sstevel@tonic-gate  * to wake up periodically, and then process all the expired timers.)
1530Sstevel@tonic-gate  */
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate typedef struct iu_timer_queue iu_tq_t;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate /*
1580Sstevel@tonic-gate  * a iu_timer_id_t refers to a given timer.  its value should not be
1590Sstevel@tonic-gate  * interpreted by the interface consumer.  it is a signed arithmetic
1600Sstevel@tonic-gate  * type, and no valid iu_timer_id_t has the value `-1'.
1610Sstevel@tonic-gate  */
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate typedef int iu_timer_id_t;
1640Sstevel@tonic-gate 
16510946SSangeeta.Misra@Sun.COM #define	IU_TIMER_ID_MAX	4096	/* max number of concurrent timers */
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate /*
1680Sstevel@tonic-gate  * a iu_tq_callback_t is a function that is called back in response to a
1690Sstevel@tonic-gate  * timer expiring.  it may then carry out any necessary work,
1700Sstevel@tonic-gate  * including rescheduling itself for callback or scheduling /
1710Sstevel@tonic-gate  * cancelling other timers.  the `void *' argument is the same value
1720Sstevel@tonic-gate  * that was passed into iu_schedule_timer(), and if it is dynamically
1730Sstevel@tonic-gate  * allocated, it is the callback's responsibility to know that, and to
1740Sstevel@tonic-gate  * free it.
1750Sstevel@tonic-gate  */
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate typedef void	iu_tq_callback_t(iu_tq_t *, void *);
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate iu_tq_t		*iu_tq_create(void);
1800Sstevel@tonic-gate void		iu_tq_destroy(iu_tq_t *);
1810Sstevel@tonic-gate iu_timer_id_t	iu_schedule_timer(iu_tq_t *, uint32_t, iu_tq_callback_t *,
1820Sstevel@tonic-gate 		    void *);
1830Sstevel@tonic-gate iu_timer_id_t	iu_schedule_timer_ms(iu_tq_t *, uint64_t, iu_tq_callback_t *,
1840Sstevel@tonic-gate 		    void *);
1850Sstevel@tonic-gate int		iu_adjust_timer(iu_tq_t *, iu_timer_id_t, uint32_t);
1860Sstevel@tonic-gate int		iu_cancel_timer(iu_tq_t *, iu_timer_id_t, void **);
1870Sstevel@tonic-gate int		iu_expire_timers(iu_tq_t *);
1880Sstevel@tonic-gate int		iu_earliest_timer(iu_tq_t *);
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate /*
1910Sstevel@tonic-gate  * Event Handler
1920Sstevel@tonic-gate  *
1930Sstevel@tonic-gate  * an event handler is an object-oriented "wrapper" for select(3C) /
1940Sstevel@tonic-gate  * poll(2), aimed to make the event demultiplexing system calls easier
1950Sstevel@tonic-gate  * to use and provide a generic reusable component.  instead of
1960Sstevel@tonic-gate  * applications directly using select(3C) / poll(2), they register
1970Sstevel@tonic-gate  * events that should be received with the event handler, providing a
1980Sstevel@tonic-gate  * callback function to call when the event occurs.  they then call
1990Sstevel@tonic-gate  * iu_handle_events() to wait and callback the registered functions
2000Sstevel@tonic-gate  * when events occur.  also called a `reactor'.
2010Sstevel@tonic-gate  */
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate typedef struct iu_event_handler iu_eh_t;
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate /*
2060Sstevel@tonic-gate  * an iu_event_id_t refers to a given event.  its value should not be
2070Sstevel@tonic-gate  * interpreted by the interface consumer.  it is a signed arithmetic
2080Sstevel@tonic-gate  * type, and no valid iu_event_id_t has the value `-1'.
2090Sstevel@tonic-gate  */
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate typedef int iu_event_id_t;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate /*
2140Sstevel@tonic-gate  * an iu_eh_callback_t is a function that is called back in response to
2150Sstevel@tonic-gate  * an event occurring.  it may then carry out any work necessary in
2160Sstevel@tonic-gate  * response to the event.  it receives the file descriptor upon which
2170Sstevel@tonic-gate  * the event occurred, a bit array of events that occurred (the same
2180Sstevel@tonic-gate  * array used as the revents by poll(2)), and its context through the
2190Sstevel@tonic-gate  * `void *' that was originally passed into iu_register_event().
2200Sstevel@tonic-gate  *
2210Sstevel@tonic-gate  * NOTE: the same descriptor may not be registered multiple times for
2220Sstevel@tonic-gate  * different callbacks.  if this behavior is desired, either use dup(2)
2230Sstevel@tonic-gate  * to get a unique descriptor, or demultiplex in the callback function
2240Sstevel@tonic-gate  * based on the events.
2250Sstevel@tonic-gate  */
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate typedef void	iu_eh_callback_t(iu_eh_t *, int, short, iu_event_id_t, void *);
2280Sstevel@tonic-gate typedef void	iu_eh_sighandler_t(iu_eh_t *, int, void *);
2290Sstevel@tonic-gate typedef boolean_t iu_eh_shutdown_t(iu_eh_t *, void *);
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate iu_eh_t		*iu_eh_create(void);
2320Sstevel@tonic-gate void		iu_eh_destroy(iu_eh_t *);
2330Sstevel@tonic-gate iu_event_id_t	iu_register_event(iu_eh_t *, int, short, iu_eh_callback_t *,
2340Sstevel@tonic-gate 		    void *);
2350Sstevel@tonic-gate int		iu_unregister_event(iu_eh_t *, iu_event_id_t, void **);
2360Sstevel@tonic-gate int		iu_handle_events(iu_eh_t *, iu_tq_t *);
2370Sstevel@tonic-gate void		iu_stop_handling_events(iu_eh_t *, unsigned int,
2380Sstevel@tonic-gate 		    iu_eh_shutdown_t *, void *);
2390Sstevel@tonic-gate int		iu_eh_register_signal(iu_eh_t *, int, iu_eh_sighandler_t *,
2400Sstevel@tonic-gate 		    void *);
2410Sstevel@tonic-gate int		iu_eh_unregister_signal(iu_eh_t *, int, void **);
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate #endif	/* !defined(_KERNEL) && !defined(_BOOT) */
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate #ifdef	__cplusplus
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate #endif
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate #endif	/* !_LIBINETUTIL_H */
250