xref: /illumos-gate/usr/src/lib/libinetutil/common/libinetutil.h (revision 350effc1e940138efb65a89b633f586280437495)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _LIBINETUTIL_H
28 #define	_LIBINETUTIL_H
29 
30 /*
31  * Contains SMI-private API for general Internet functionality
32  */
33 
34 #ifdef	__cplusplus
35 extern "C" {
36 #endif
37 
38 #include <netinet/inetutil.h>
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <net/if.h>
43 
44 #if !defined(_KERNEL) && !defined(_BOOT)
45 
46 typedef struct {
47 	uint_t		ifsp_ppa;	/* Physical Point of Attachment */
48 	uint_t		ifsp_lun;	/* Logical Unit number */
49 	boolean_t	ifsp_lunvalid;	/* TRUE if lun is valid */
50 	char		ifsp_devnm[LIFNAMSIZ];	/* only the device name */
51 } ifspec_t;
52 
53 extern boolean_t ifparse_ifspec(const char *, ifspec_t *);
54 extern void get_netmask4(const struct in_addr *, struct in_addr *);
55 extern boolean_t sockaddrcmp(const struct sockaddr_storage *,
56     const struct sockaddr_storage *);
57 
58 /*
59  * Extended version of the classic BSD ifaddrlist() interface:
60  *
61  *    int ifaddrlist(struct ifaddrlist **addrlistp, int af, uint_t flags,
62  *	             char *errbuf);
63  *
64  * 	* addrlistp: Upon success, ifaddrlist() sets *addrlistp to a
65  *	  dynamically-allocated array of addresses.
66  *
67  *	* af: Either AF_INET to obtain IPv4 addresses, or AF_INET6 to
68  *	  obtain IPv6 addresses.
69  *
70  *	* flags: LIFC_* flags that control the classes of interfaces that
71  *	  will be visible.
72  *
73  *	* errbuf: A caller-supplied buffer of ERRBUFSIZE.  Upon failure,
74  *	  provides the reason for the failure.
75  *
76  * Upon success, ifaddrlist() returns the number of addresses in the array
77  * pointed to by `addrlistp'.  If the count is 0, then `addrlistp' is NULL.
78  */
79 union any_in_addr {
80 	struct in6_addr	addr6;
81 	struct in_addr	addr;
82 };
83 
84 struct ifaddrlist {
85 	int		index;			/* interface index */
86 	union any_in_addr addr;			/* interface address */
87 	char		device[LIFNAMSIZ + 1];	/* interface name */
88 	uint64_t	flags;			/* interface flags */
89 };
90 
91 #define	ERRBUFSIZE 128			/* expected size of fourth argument */
92 
93 extern int ifaddrlist(struct ifaddrlist **, int, uint_t, char *);
94 
95 /*
96  * Similar to ifaddrlist(), but returns a linked-list of addresses for a
97  * *specific* interface name, and allows specific address flags to be matched
98  * against.  A linked list is used rather than an array so that information
99  * can grow over time without affecting binary compatibility.  Also, leaves
100  * error-handling up to the caller.  Returns the number of ifaddrlistx's
101  * chained through ifaddrp.
102  *
103  *    int ifaddrlistx(const char *ifname, uint64_t set, uint64_t clear,
104  *        ifaddrlistx_t **ifaddrp);
105  *
106  *	* ifname: Interface name to match against.
107  *
108  *	* set: One or more flags that must be set on the address for
109  *	  it to be returned.
110  *
111  *	* clear: Flags that must be clear on the address for it to be
112  *	  returned.
113  *
114  * 	* ifaddrp: Upon success, ifaddrlistx() sets *ifaddrp to the head
115  *	  of a dynamically-allocated array of ifaddrlistx structures.
116  *
117  * Once done, the caller must free `ifaddrp' by calling ifaddrlistx_free().
118  */
119 typedef struct ifaddrlistx {
120 	struct ifaddrlistx	*ia_next;
121 	char			ia_name[LIFNAMSIZ];
122 	uint64_t		ia_flags;
123 	struct sockaddr_storage	ia_addr;
124 } ifaddrlistx_t;
125 
126 extern int ifaddrlistx(const char *, uint64_t, uint64_t, ifaddrlistx_t **);
127 extern void ifaddrlistx_free(ifaddrlistx_t *);
128 
129 /*
130  * Timer queues
131  *
132  * timer queues are a facility for managing timeouts in unix.  in the
133  * event driven model, unix provides us with poll(2)/select(3C), which
134  * allow us to coordinate waiting on multiple descriptors with an
135  * optional timeout.  however, often (as is the case with the DHCP
136  * agent), we want to manage multiple independent timeouts (say, one
137  * for waiting for an OFFER to come back from a server in response to
138  * a DISCOVER sent out on one interface, and another for waiting for
139  * the T1 time on another interface).  timer queues allow us to do
140  * this in the event-driven model.
141  *
142  * note that timer queues do not in and of themselves provide the
143  * event driven model (for instance, there is no handle_events()
144  * routine).  they merely provide the hooks to support multiple
145  * independent timeouts.  this is done for both simplicity and
146  * applicability (for instance, while one approach would be to use
147  * this timer queue with poll(2), another one would be to use SIGALRM
148  * to wake up periodically, and then process all the expired timers.)
149  */
150 
151 typedef struct iu_timer_queue iu_tq_t;
152 
153 /*
154  * a iu_timer_id_t refers to a given timer.  its value should not be
155  * interpreted by the interface consumer.  it is a signed arithmetic
156  * type, and no valid iu_timer_id_t has the value `-1'.
157  */
158 
159 typedef int iu_timer_id_t;
160 
161 #define	IU_TIMER_ID_MAX	4096	/* max number of concurrent timers */
162 
163 /*
164  * a iu_tq_callback_t is a function that is called back in response to a
165  * timer expiring.  it may then carry out any necessary work,
166  * including rescheduling itself for callback or scheduling /
167  * cancelling other timers.  the `void *' argument is the same value
168  * that was passed into iu_schedule_timer(), and if it is dynamically
169  * allocated, it is the callback's responsibility to know that, and to
170  * free it.
171  */
172 
173 typedef void	iu_tq_callback_t(iu_tq_t *, void *);
174 
175 iu_tq_t		*iu_tq_create(void);
176 void		iu_tq_destroy(iu_tq_t *);
177 iu_timer_id_t	iu_schedule_timer(iu_tq_t *, uint32_t, iu_tq_callback_t *,
178 		    void *);
179 iu_timer_id_t	iu_schedule_timer_ms(iu_tq_t *, uint64_t, iu_tq_callback_t *,
180 		    void *);
181 int		iu_adjust_timer(iu_tq_t *, iu_timer_id_t, uint32_t);
182 int		iu_cancel_timer(iu_tq_t *, iu_timer_id_t, void **);
183 int		iu_expire_timers(iu_tq_t *);
184 int		iu_earliest_timer(iu_tq_t *);
185 
186 /*
187  * Event Handler
188  *
189  * an event handler is an object-oriented "wrapper" for select(3C) /
190  * poll(2), aimed to make the event demultiplexing system calls easier
191  * to use and provide a generic reusable component.  instead of
192  * applications directly using select(3C) / poll(2), they register
193  * events that should be received with the event handler, providing a
194  * callback function to call when the event occurs.  they then call
195  * iu_handle_events() to wait and callback the registered functions
196  * when events occur.  also called a `reactor'.
197  */
198 
199 typedef struct iu_event_handler iu_eh_t;
200 
201 /*
202  * an iu_event_id_t refers to a given event.  its value should not be
203  * interpreted by the interface consumer.  it is a signed arithmetic
204  * type, and no valid iu_event_id_t has the value `-1'.
205  */
206 
207 typedef int iu_event_id_t;
208 
209 /*
210  * an iu_eh_callback_t is a function that is called back in response to
211  * an event occurring.  it may then carry out any work necessary in
212  * response to the event.  it receives the file descriptor upon which
213  * the event occurred, a bit array of events that occurred (the same
214  * array used as the revents by poll(2)), and its context through the
215  * `void *' that was originally passed into iu_register_event().
216  *
217  * NOTE: the same descriptor may not be registered multiple times for
218  * different callbacks.  if this behavior is desired, either use dup(2)
219  * to get a unique descriptor, or demultiplex in the callback function
220  * based on the events.
221  */
222 
223 typedef void	iu_eh_callback_t(iu_eh_t *, int, short, iu_event_id_t, void *);
224 typedef void	iu_eh_sighandler_t(iu_eh_t *, int, void *);
225 typedef boolean_t iu_eh_shutdown_t(iu_eh_t *, void *);
226 
227 iu_eh_t		*iu_eh_create(void);
228 void		iu_eh_destroy(iu_eh_t *);
229 iu_event_id_t	iu_register_event(iu_eh_t *, int, short, iu_eh_callback_t *,
230 		    void *);
231 int		iu_unregister_event(iu_eh_t *, iu_event_id_t, void **);
232 int		iu_handle_events(iu_eh_t *, iu_tq_t *);
233 void		iu_stop_handling_events(iu_eh_t *, unsigned int,
234 		    iu_eh_shutdown_t *, void *);
235 int		iu_eh_register_signal(iu_eh_t *, int, iu_eh_sighandler_t *,
236 		    void *);
237 int		iu_eh_unregister_signal(iu_eh_t *, int, void **);
238 
239 #endif	/* !defined(_KERNEL) && !defined(_BOOT) */
240 
241 #ifdef	__cplusplus
242 }
243 #endif
244 
245 #endif	/* !_LIBINETUTIL_H */
246