2 * Copyright (c) 2016-2017, Marie Helene Kvello-Aune
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * thislist of conditions and the following disclaimer.
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.
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.
29 #include <sys/ioctl.h>
39 #include "libifconfig.h" // Needed for ifconfig_errstate
40 #include "libifconfig_internal.h"
43 ifconfig_getifaddrs(ifconfig_handle_t *h)
47 if (h->ifap == NULL) {
48 ret = getifaddrs(&h->ifap);
56 ifconfig_ioctlwrap(ifconfig_handle_t *h, const int addressfamily,
57 unsigned long request, void *data)
61 if (ifconfig_socket(h, addressfamily, &s) != 0) {
65 if (ioctl(s, request, data) != 0) {
66 h->error.errtype = IOCTL;
67 h->error.ioctl_request = request;
68 h->error.errcode = errno;
76 * Function to get socket for the specified address family.
77 * If the socket doesn't already exist, attempt to create it.
80 ifconfig_socket(ifconfig_handle_t *h, const int addressfamily, int *s)
83 if (addressfamily > AF_MAX) {
84 h->error.errtype = SOCKET;
85 h->error.errcode = EINVAL;
89 if (h->sockets[addressfamily] != -1) {
90 *s = h->sockets[addressfamily];
94 /* We don't have a socket of that type available. Create one. */
95 h->sockets[addressfamily] = socket(addressfamily, SOCK_DGRAM, 0);
96 if (h->sockets[addressfamily] == -1) {
97 h->error.errtype = SOCKET;
98 h->error.errcode = errno;
102 *s = h->sockets[addressfamily];