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