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