xref: /freebsd/lib/libifconfig/libifconfig_internal.h (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
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 
27ec214349SKristof Provost #pragma once
28ec214349SKristof Provost 
29ec214349SKristof Provost #include "libifconfig.h"
30ec214349SKristof Provost 
31ec214349SKristof Provost 
32ec214349SKristof Provost struct errstate {
33ec214349SKristof Provost 	/**
34ec214349SKristof Provost 	 * Type of error.
35ec214349SKristof Provost 	 */
36ec214349SKristof Provost 	ifconfig_errtype errtype;
37ec214349SKristof Provost 
38ec214349SKristof Provost 	/**
3909703627SGordon Bergling 	 * The error occurred in this ioctl() request.
40ec214349SKristof Provost 	 * Populated if errtype = IOCTL
41ec214349SKristof Provost 	 */
42ec214349SKristof Provost 	unsigned long ioctl_request;
43ec214349SKristof Provost 
44ec214349SKristof Provost 	/**
4509703627SGordon Bergling 	 * The value of the global errno variable when the error occurred.
46ec214349SKristof Provost 	 */
47ec214349SKristof Provost 	int errcode;
48ec214349SKristof Provost };
49ec214349SKristof Provost 
50ec214349SKristof Provost struct ifconfig_handle {
51ec214349SKristof Provost 	struct errstate error;
52ec214349SKristof Provost 	int sockets[AF_MAX + 1];
53b1d757bcSAlan Somers 	/** Cached output of getifaddrs */
54b1d757bcSAlan Somers 	struct ifaddrs *ifap;
55ec214349SKristof Provost };
56ec214349SKristof Provost 
57b1d757bcSAlan Somers /* Fetch the list of interface addrs, if it hasn't already been fetched */
58b1d757bcSAlan Somers int ifconfig_getifaddrs(ifconfig_handle_t *h);
59b1d757bcSAlan Somers 
60ec214349SKristof Provost /**
61ec214349SKristof Provost  * Retrieves socket for address family <paramref name="addressfamily"> from
62ec214349SKristof Provost  * cache, or creates it if it doesn't already exist.
63ec214349SKristof Provost  * @param addressfamily The address family of the socket to retrieve
64ec214349SKristof Provost  * @param s The retrieved socket.
65ec214349SKristof Provost  * @return 0 on success, -1 on failure.
66ec214349SKristof Provost  * {@example
67ec214349SKristof Provost  * This example shows how to retrieve a socket from the cache.
68ec214349SKristof Provost  * {@code
69ec214349SKristof Provost  * static void myfunc() \{
70ec214349SKristof Provost  *    int s;
71ec214349SKristof Provost  *    if (ifconfig_socket(AF_LOCAL, &s) != 0) \{
72ec214349SKristof Provost  *        // Handle error state here
73ec214349SKristof Provost  *    \}
74ec214349SKristof Provost  *    // user code here
75ec214349SKristof Provost  * \}
76ec214349SKristof Provost  * }
77ec214349SKristof Provost  * }
78ec214349SKristof Provost  */
79ec214349SKristof Provost int ifconfig_socket(ifconfig_handle_t *h, const int addressfamily, int *s);
80ec214349SKristof Provost 
81ec214349SKristof Provost /** Function to wrap ioctl() and automatically populate ifconfig_errstate when appropriate.*/
82ec214349SKristof Provost int ifconfig_ioctlwrap(ifconfig_handle_t *h, const int addressfamily,
83b1d757bcSAlan Somers     unsigned long request, void *data);
84*40e04359SKristof Provost 
85*40e04359SKristof Provost void ifconfig_error_clear(ifconfig_handle_t *h);
86*40e04359SKristof Provost void ifconfig_error(ifconfig_handle_t *h, ifconfig_errtype type, int error);
87