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