xref: /titanic_53/usr/src/lib/libinetutil/common/libinetutil.h (revision e11c3f44f531fdff80941ce57c065d2ae861cefc)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*e11c3f44Smeem  * Common Development and Distribution License (the "License").
6*e11c3f44Smeem  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
210406ceaaSmeem 
227c478bd9Sstevel@tonic-gate /*
23*e11c3f44Smeem  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #ifndef _LIBINETUTIL_H
287c478bd9Sstevel@tonic-gate #define	_LIBINETUTIL_H
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
310406ceaaSmeem  * Contains SMI-private API for general Internet functionality
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
357c478bd9Sstevel@tonic-gate extern "C" {
367c478bd9Sstevel@tonic-gate #endif
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <netinet/inetutil.h>
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
407c478bd9Sstevel@tonic-gate #include <sys/socket.h>
417c478bd9Sstevel@tonic-gate #include <netinet/in.h>
427c478bd9Sstevel@tonic-gate #include <net/if.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #if !defined(_KERNEL) && !defined(_BOOT)
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #define	IFSP_MAXMODS	9	/* Max modules that can be pushed on if */
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate typedef struct {
497c478bd9Sstevel@tonic-gate 	uint_t		ifsp_ppa;	/* Physical Point of Attachment */
507c478bd9Sstevel@tonic-gate 	uint_t		ifsp_lun;	/* Logical Unit number */
517c478bd9Sstevel@tonic-gate 	boolean_t	ifsp_lunvalid;	/* TRUE if lun is valid */
527c478bd9Sstevel@tonic-gate 	int		ifsp_modcnt;	/* Number of modules to be pushed */
537c478bd9Sstevel@tonic-gate 	char		ifsp_devnm[LIFNAMSIZ];	/* only the device name */
547c478bd9Sstevel@tonic-gate 	char		ifsp_mods[IFSP_MAXMODS][LIFNAMSIZ]; /* table of mods */
557c478bd9Sstevel@tonic-gate } ifspec_t;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate extern boolean_t ifparse_ifspec(const char *, ifspec_t *);
587c478bd9Sstevel@tonic-gate extern void get_netmask4(const struct in_addr *, struct in_addr *);
59*e11c3f44Smeem extern boolean_t sockaddrcmp(const struct sockaddr_storage *,
60*e11c3f44Smeem     const struct sockaddr_storage *);
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /*
630406ceaaSmeem  * Extended version of the classic BSD ifaddrlist() interface:
640406ceaaSmeem  *
65*e11c3f44Smeem  *    int ifaddrlist(struct ifaddrlist **addrlistp, int af, uint_t flags,
66*e11c3f44Smeem  *	             char *errbuf);
670406ceaaSmeem  *
680406ceaaSmeem  * 	* addrlistp: Upon success, ifaddrlist() sets *addrlistp to a
690406ceaaSmeem  *	  dynamically-allocated array of addresses.
700406ceaaSmeem  *
710406ceaaSmeem  *	* af: Either AF_INET to obtain IPv4 addresses, or AF_INET6 to
720406ceaaSmeem  *	  obtain IPv6 addresses.
730406ceaaSmeem  *
74*e11c3f44Smeem  *	* flags: LIFC_* flags that control the classes of interfaces that
75*e11c3f44Smeem  *	  will be visible.
76*e11c3f44Smeem  *
770406ceaaSmeem  *	* errbuf: A caller-supplied buffer of ERRBUFSIZE.  Upon failure,
780406ceaaSmeem  *	  provides the reason for the failure.
790406ceaaSmeem  *
800406ceaaSmeem  * Upon success, ifaddrlist() returns the number of addresses in the array
810406ceaaSmeem  * pointed to by `addrlistp'.  If the count is 0, then `addrlistp' is NULL.
820406ceaaSmeem  */
830406ceaaSmeem union any_in_addr {
840406ceaaSmeem 	struct in6_addr	addr6;
850406ceaaSmeem 	struct in_addr	addr;
860406ceaaSmeem };
870406ceaaSmeem 
880406ceaaSmeem struct ifaddrlist {
890406ceaaSmeem 	int		index;			/* interface index */
900406ceaaSmeem 	union any_in_addr addr;			/* interface address */
910406ceaaSmeem 	char		device[LIFNAMSIZ + 1];	/* interface name */
920406ceaaSmeem 	uint64_t	flags;			/* interface flags */
930406ceaaSmeem };
940406ceaaSmeem 
95*e11c3f44Smeem #define	ERRBUFSIZE 128			/* expected size of fourth argument */
960406ceaaSmeem 
97*e11c3f44Smeem extern int ifaddrlist(struct ifaddrlist **, int, uint_t, char *);
98*e11c3f44Smeem 
99*e11c3f44Smeem /*
100*e11c3f44Smeem  * Similar to ifaddrlist(), but returns a linked-list of addresses for a
101*e11c3f44Smeem  * *specific* interface name, and allows specific address flags to be matched
102*e11c3f44Smeem  * against.  A linked list is used rather than an array so that information
103*e11c3f44Smeem  * can grow over time without affecting binary compatibility.  Also, leaves
104*e11c3f44Smeem  * error-handling up to the caller.  Returns the number of ifaddrlistx's
105*e11c3f44Smeem  * chained through ifaddrp.
106*e11c3f44Smeem  *
107*e11c3f44Smeem  *    int ifaddrlistx(const char *ifname, uint64_t set, uint64_t clear,
108*e11c3f44Smeem  *        ifaddrlistx_t **ifaddrp);
109*e11c3f44Smeem  *
110*e11c3f44Smeem  *	* ifname: Interface name to match against.
111*e11c3f44Smeem  *
112*e11c3f44Smeem  *	* set: One or more flags that must be set on the address for
113*e11c3f44Smeem  *	  it to be returned.
114*e11c3f44Smeem  *
115*e11c3f44Smeem  *	* clear: Flags that must be clear on the address for it to be
116*e11c3f44Smeem  *	  returned.
117*e11c3f44Smeem  *
118*e11c3f44Smeem  * 	* ifaddrp: Upon success, ifaddrlistx() sets *ifaddrp to the head
119*e11c3f44Smeem  *	  of a dynamically-allocated array of ifaddrlistx structures.
120*e11c3f44Smeem  *
121*e11c3f44Smeem  * Once done, the caller must free `ifaddrp' by calling ifaddrlistx_free().
122*e11c3f44Smeem  */
123*e11c3f44Smeem typedef struct ifaddrlistx {
124*e11c3f44Smeem 	struct ifaddrlistx	*ia_next;
125*e11c3f44Smeem 	char			ia_name[LIFNAMSIZ];
126*e11c3f44Smeem 	uint64_t		ia_flags;
127*e11c3f44Smeem 	struct sockaddr_storage	ia_addr;
128*e11c3f44Smeem } ifaddrlistx_t;
129*e11c3f44Smeem 
130*e11c3f44Smeem extern int ifaddrlistx(const char *, uint64_t, uint64_t, ifaddrlistx_t **);
131*e11c3f44Smeem extern void ifaddrlistx_free(ifaddrlistx_t *);
1320406ceaaSmeem 
1330406ceaaSmeem /*
1347c478bd9Sstevel@tonic-gate  * Timer queues
1357c478bd9Sstevel@tonic-gate  *
1367c478bd9Sstevel@tonic-gate  * timer queues are a facility for managing timeouts in unix.  in the
1377c478bd9Sstevel@tonic-gate  * event driven model, unix provides us with poll(2)/select(3C), which
1387c478bd9Sstevel@tonic-gate  * allow us to coordinate waiting on multiple descriptors with an
1397c478bd9Sstevel@tonic-gate  * optional timeout.  however, often (as is the case with the DHCP
1407c478bd9Sstevel@tonic-gate  * agent), we want to manage multiple independent timeouts (say, one
1417c478bd9Sstevel@tonic-gate  * for waiting for an OFFER to come back from a server in response to
1427c478bd9Sstevel@tonic-gate  * a DISCOVER sent out on one interface, and another for waiting for
1437c478bd9Sstevel@tonic-gate  * the T1 time on another interface).  timer queues allow us to do
1447c478bd9Sstevel@tonic-gate  * this in the event-driven model.
1457c478bd9Sstevel@tonic-gate  *
1467c478bd9Sstevel@tonic-gate  * note that timer queues do not in and of themselves provide the
1477c478bd9Sstevel@tonic-gate  * event driven model (for instance, there is no handle_events()
1487c478bd9Sstevel@tonic-gate  * routine).  they merely provide the hooks to support multiple
1497c478bd9Sstevel@tonic-gate  * independent timeouts.  this is done for both simplicity and
1507c478bd9Sstevel@tonic-gate  * applicability (for instance, while one approach would be to use
1517c478bd9Sstevel@tonic-gate  * this timer queue with poll(2), another one would be to use SIGALRM
1527c478bd9Sstevel@tonic-gate  * to wake up periodically, and then process all the expired timers.)
1537c478bd9Sstevel@tonic-gate  */
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate typedef struct iu_timer_queue iu_tq_t;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate /*
1587c478bd9Sstevel@tonic-gate  * a iu_timer_id_t refers to a given timer.  its value should not be
1597c478bd9Sstevel@tonic-gate  * interpreted by the interface consumer.  it is a signed arithmetic
1607c478bd9Sstevel@tonic-gate  * type, and no valid iu_timer_id_t has the value `-1'.
1617c478bd9Sstevel@tonic-gate  */
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate typedef int iu_timer_id_t;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate #define	IU_TIMER_ID_MAX	1024	/* max number of concurrent timers */
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate /*
1687c478bd9Sstevel@tonic-gate  * a iu_tq_callback_t is a function that is called back in response to a
1697c478bd9Sstevel@tonic-gate  * timer expiring.  it may then carry out any necessary work,
1707c478bd9Sstevel@tonic-gate  * including rescheduling itself for callback or scheduling /
1717c478bd9Sstevel@tonic-gate  * cancelling other timers.  the `void *' argument is the same value
1727c478bd9Sstevel@tonic-gate  * that was passed into iu_schedule_timer(), and if it is dynamically
1737c478bd9Sstevel@tonic-gate  * allocated, it is the callback's responsibility to know that, and to
1747c478bd9Sstevel@tonic-gate  * free it.
1757c478bd9Sstevel@tonic-gate  */
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate typedef void	iu_tq_callback_t(iu_tq_t *, void *);
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate iu_tq_t		*iu_tq_create(void);
1807c478bd9Sstevel@tonic-gate void		iu_tq_destroy(iu_tq_t *);
1817c478bd9Sstevel@tonic-gate iu_timer_id_t	iu_schedule_timer(iu_tq_t *, uint32_t, iu_tq_callback_t *,
1827c478bd9Sstevel@tonic-gate 		    void *);
1837c478bd9Sstevel@tonic-gate iu_timer_id_t	iu_schedule_timer_ms(iu_tq_t *, uint64_t, iu_tq_callback_t *,
1847c478bd9Sstevel@tonic-gate 		    void *);
1857c478bd9Sstevel@tonic-gate int		iu_adjust_timer(iu_tq_t *, iu_timer_id_t, uint32_t);
1867c478bd9Sstevel@tonic-gate int		iu_cancel_timer(iu_tq_t *, iu_timer_id_t, void **);
1877c478bd9Sstevel@tonic-gate int		iu_expire_timers(iu_tq_t *);
1887c478bd9Sstevel@tonic-gate int		iu_earliest_timer(iu_tq_t *);
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate /*
1917c478bd9Sstevel@tonic-gate  * Event Handler
1927c478bd9Sstevel@tonic-gate  *
1937c478bd9Sstevel@tonic-gate  * an event handler is an object-oriented "wrapper" for select(3C) /
1947c478bd9Sstevel@tonic-gate  * poll(2), aimed to make the event demultiplexing system calls easier
1957c478bd9Sstevel@tonic-gate  * to use and provide a generic reusable component.  instead of
1967c478bd9Sstevel@tonic-gate  * applications directly using select(3C) / poll(2), they register
1977c478bd9Sstevel@tonic-gate  * events that should be received with the event handler, providing a
1987c478bd9Sstevel@tonic-gate  * callback function to call when the event occurs.  they then call
1997c478bd9Sstevel@tonic-gate  * iu_handle_events() to wait and callback the registered functions
2007c478bd9Sstevel@tonic-gate  * when events occur.  also called a `reactor'.
2017c478bd9Sstevel@tonic-gate  */
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate typedef struct iu_event_handler iu_eh_t;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate /*
2067c478bd9Sstevel@tonic-gate  * an iu_event_id_t refers to a given event.  its value should not be
2077c478bd9Sstevel@tonic-gate  * interpreted by the interface consumer.  it is a signed arithmetic
2087c478bd9Sstevel@tonic-gate  * type, and no valid iu_event_id_t has the value `-1'.
2097c478bd9Sstevel@tonic-gate  */
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate typedef int iu_event_id_t;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate /*
2147c478bd9Sstevel@tonic-gate  * an iu_eh_callback_t is a function that is called back in response to
2157c478bd9Sstevel@tonic-gate  * an event occurring.  it may then carry out any work necessary in
2167c478bd9Sstevel@tonic-gate  * response to the event.  it receives the file descriptor upon which
2177c478bd9Sstevel@tonic-gate  * the event occurred, a bit array of events that occurred (the same
2187c478bd9Sstevel@tonic-gate  * array used as the revents by poll(2)), and its context through the
2197c478bd9Sstevel@tonic-gate  * `void *' that was originally passed into iu_register_event().
2207c478bd9Sstevel@tonic-gate  *
2217c478bd9Sstevel@tonic-gate  * NOTE: the same descriptor may not be registered multiple times for
2227c478bd9Sstevel@tonic-gate  * different callbacks.  if this behavior is desired, either use dup(2)
2237c478bd9Sstevel@tonic-gate  * to get a unique descriptor, or demultiplex in the callback function
2247c478bd9Sstevel@tonic-gate  * based on the events.
2257c478bd9Sstevel@tonic-gate  */
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate typedef void	iu_eh_callback_t(iu_eh_t *, int, short, iu_event_id_t, void *);
2287c478bd9Sstevel@tonic-gate typedef void	iu_eh_sighandler_t(iu_eh_t *, int, void *);
2297c478bd9Sstevel@tonic-gate typedef boolean_t iu_eh_shutdown_t(iu_eh_t *, void *);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate iu_eh_t		*iu_eh_create(void);
2327c478bd9Sstevel@tonic-gate void		iu_eh_destroy(iu_eh_t *);
2337c478bd9Sstevel@tonic-gate iu_event_id_t	iu_register_event(iu_eh_t *, int, short, iu_eh_callback_t *,
2347c478bd9Sstevel@tonic-gate 		    void *);
2357c478bd9Sstevel@tonic-gate int		iu_unregister_event(iu_eh_t *, iu_event_id_t, void **);
2367c478bd9Sstevel@tonic-gate int		iu_handle_events(iu_eh_t *, iu_tq_t *);
2377c478bd9Sstevel@tonic-gate void		iu_stop_handling_events(iu_eh_t *, unsigned int,
2387c478bd9Sstevel@tonic-gate 		    iu_eh_shutdown_t *, void *);
2397c478bd9Sstevel@tonic-gate int		iu_eh_register_signal(iu_eh_t *, int, iu_eh_sighandler_t *,
2407c478bd9Sstevel@tonic-gate 		    void *);
2417c478bd9Sstevel@tonic-gate int		iu_eh_unregister_signal(iu_eh_t *, int, void **);
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate #endif	/* !defined(_KERNEL) && !defined(_BOOT) */
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate #endif
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate #endif	/* !_LIBINETUTIL_H */
250