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