xref: /freebsd/lib/libifconfig/libifconfig_internal.h (revision 09703627879df49648e065c5e64db7ab807263d1)
1ec214349SKristof Provost /*
2b1d757bcSAlan Somers  * Copyright (c) 2016-2017, Marie Helene Kvello-Aune
3ec214349SKristof Provost  * All rights reserved.
4ec214349SKristof Provost  *
5ec214349SKristof Provost  * Redistribution and use in source and binary forms, with or without modification,
6ec214349SKristof Provost  * are permitted provided that the following conditions are met:
7ec214349SKristof Provost  *
8ec214349SKristof Provost  * 1. Redistributions of source code must retain the above copyright notice,
9ec214349SKristof Provost  * thislist of conditions and the following disclaimer.
10ec214349SKristof Provost  *
11ec214349SKristof Provost  * 2. Redistributions in binary form must reproduce the above copyright notice,
12ec214349SKristof Provost  * this list of conditions and the following disclaimer in the documentation and/or
13ec214349SKristof Provost  * other materials provided with the distribution.
14ec214349SKristof Provost  *
15ec214349SKristof Provost  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16ec214349SKristof Provost  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17ec214349SKristof Provost  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18ec214349SKristof Provost  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19ec214349SKristof Provost  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20ec214349SKristof Provost  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21ec214349SKristof Provost  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22ec214349SKristof Provost  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23ec214349SKristof Provost  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24ec214349SKristof Provost  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25ec214349SKristof Provost  *
26ec214349SKristof Provost  * $FreeBSD$
27ec214349SKristof Provost  */
28ec214349SKristof Provost 
29ec214349SKristof Provost #pragma once
30ec214349SKristof Provost 
31ec214349SKristof Provost #include "libifconfig.h"
32ec214349SKristof Provost 
33ec214349SKristof Provost 
34ec214349SKristof Provost struct errstate {
35ec214349SKristof Provost 	/**
36ec214349SKristof Provost 	 * Type of error.
37ec214349SKristof Provost 	 */
38ec214349SKristof Provost 	ifconfig_errtype errtype;
39ec214349SKristof Provost 
40ec214349SKristof Provost 	/**
41*09703627SGordon Bergling 	 * The error occurred in this ioctl() request.
42ec214349SKristof Provost 	 * Populated if errtype = IOCTL
43ec214349SKristof Provost 	 */
44ec214349SKristof Provost 	unsigned long ioctl_request;
45ec214349SKristof Provost 
46ec214349SKristof Provost 	/**
47*09703627SGordon Bergling 	 * The value of the global errno variable when the error occurred.
48ec214349SKristof Provost 	 */
49ec214349SKristof Provost 	int errcode;
50ec214349SKristof Provost };
51ec214349SKristof Provost 
52ec214349SKristof Provost struct ifconfig_handle {
53ec214349SKristof Provost 	struct errstate error;
54ec214349SKristof Provost 	int sockets[AF_MAX + 1];
55b1d757bcSAlan Somers 	/** Cached output of getifaddrs */
56b1d757bcSAlan Somers 	struct ifaddrs *ifap;
57ec214349SKristof Provost };
58ec214349SKristof Provost 
59b1d757bcSAlan Somers /* Fetch the list of interface addrs, if it hasn't already been fetched */
60b1d757bcSAlan Somers int ifconfig_getifaddrs(ifconfig_handle_t *h);
61b1d757bcSAlan Somers 
62ec214349SKristof Provost /**
63ec214349SKristof Provost  * Retrieves socket for address family <paramref name="addressfamily"> from
64ec214349SKristof Provost  * cache, or creates it if it doesn't already exist.
65ec214349SKristof Provost  * @param addressfamily The address family of the socket to retrieve
66ec214349SKristof Provost  * @param s The retrieved socket.
67ec214349SKristof Provost  * @return 0 on success, -1 on failure.
68ec214349SKristof Provost  * {@example
69ec214349SKristof Provost  * This example shows how to retrieve a socket from the cache.
70ec214349SKristof Provost  * {@code
71ec214349SKristof Provost  * static void myfunc() \{
72ec214349SKristof Provost  *    int s;
73ec214349SKristof Provost  *    if (ifconfig_socket(AF_LOCAL, &s) != 0) \{
74ec214349SKristof Provost  *        // Handle error state here
75ec214349SKristof Provost  *    \}
76ec214349SKristof Provost  *    // user code here
77ec214349SKristof Provost  * \}
78ec214349SKristof Provost  * }
79ec214349SKristof Provost  * }
80ec214349SKristof Provost  */
81ec214349SKristof Provost int ifconfig_socket(ifconfig_handle_t *h, const int addressfamily, int *s);
82ec214349SKristof Provost 
83ec214349SKristof Provost /** Function to wrap ioctl() and automatically populate ifconfig_errstate when appropriate.*/
84ec214349SKristof Provost int ifconfig_ioctlwrap(ifconfig_handle_t *h, const int addressfamily,
85b1d757bcSAlan Somers     unsigned long request, void *data);
86