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
279a2ff315SKristof Provost #include <sys/ioctl.h>
28ec214349SKristof Provost
29ec214349SKristof Provost #include <net/if.h>
30ec214349SKristof Provost
31ec214349SKristof Provost #include <errno.h>
32b1d757bcSAlan Somers #include <ifaddrs.h>
33ec214349SKristof Provost #include <stdio.h>
34ec214349SKristof Provost #include <stdlib.h>
35ec214349SKristof Provost #include <unistd.h>
36ec214349SKristof Provost
37ec214349SKristof Provost #include "libifconfig.h" // Needed for ifconfig_errstate
38ec214349SKristof Provost #include "libifconfig_internal.h"
39ec214349SKristof Provost
40ec214349SKristof Provost int
ifconfig_getifaddrs(ifconfig_handle_t * h)41b1d757bcSAlan Somers ifconfig_getifaddrs(ifconfig_handle_t *h)
42ec214349SKristof Provost {
43b1d757bcSAlan Somers int ret;
449a2ff315SKristof Provost
45b1d757bcSAlan Somers if (h->ifap == NULL) {
46b1d757bcSAlan Somers ret = getifaddrs(&h->ifap);
47b1d757bcSAlan Somers return (ret);
48b1d757bcSAlan Somers } else {
49b1d757bcSAlan Somers return (0);
50ec214349SKristof Provost }
51ec214349SKristof Provost }
52ec214349SKristof Provost
53ec214349SKristof Provost int
ifconfig_ioctlwrap(ifconfig_handle_t * h,const int addressfamily,unsigned long request,void * data)54ec214349SKristof Provost ifconfig_ioctlwrap(ifconfig_handle_t *h, const int addressfamily,
55b1d757bcSAlan Somers unsigned long request, void *data)
56ec214349SKristof Provost {
57ec214349SKristof Provost int s;
58ec214349SKristof Provost
59ec214349SKristof Provost if (ifconfig_socket(h, addressfamily, &s) != 0) {
60ec214349SKristof Provost return (-1);
61ec214349SKristof Provost }
62ec214349SKristof Provost
63b1d757bcSAlan Somers if (ioctl(s, request, data) != 0) {
64b1d757bcSAlan Somers h->error.errtype = IOCTL;
65b1d757bcSAlan Somers h->error.ioctl_request = request;
66b1d757bcSAlan Somers h->error.errcode = errno;
67b1d757bcSAlan Somers return (-1);
68b1d757bcSAlan Somers }
69b1d757bcSAlan Somers
70b1d757bcSAlan Somers return (0);
71ec214349SKristof Provost }
72ec214349SKristof Provost
73ec214349SKristof Provost /*
74ec214349SKristof Provost * Function to get socket for the specified address family.
75ec214349SKristof Provost * If the socket doesn't already exist, attempt to create it.
76ec214349SKristof Provost */
779a2ff315SKristof Provost int
ifconfig_socket(ifconfig_handle_t * h,const int addressfamily,int * s)789a2ff315SKristof Provost ifconfig_socket(ifconfig_handle_t *h, const int addressfamily, int *s)
79ec214349SKristof Provost {
809a2ff315SKristof Provost
81ec214349SKristof Provost if (addressfamily > AF_MAX) {
82ec214349SKristof Provost h->error.errtype = SOCKET;
83ec214349SKristof Provost h->error.errcode = EINVAL;
84ec214349SKristof Provost return (-1);
85ec214349SKristof Provost }
86ec214349SKristof Provost
87ec214349SKristof Provost if (h->sockets[addressfamily] != -1) {
88ec214349SKristof Provost *s = h->sockets[addressfamily];
89ec214349SKristof Provost return (0);
90ec214349SKristof Provost }
91ec214349SKristof Provost
92ec214349SKristof Provost /* We don't have a socket of that type available. Create one. */
93ec214349SKristof Provost h->sockets[addressfamily] = socket(addressfamily, SOCK_DGRAM, 0);
94ec214349SKristof Provost if (h->sockets[addressfamily] == -1) {
95ec214349SKristof Provost h->error.errtype = SOCKET;
96ec214349SKristof Provost h->error.errcode = errno;
97ec214349SKristof Provost return (-1);
98ec214349SKristof Provost }
99ec214349SKristof Provost
100ec214349SKristof Provost *s = h->sockets[addressfamily];
101ec214349SKristof Provost return (0);
102ec214349SKristof Provost }
103*40e04359SKristof Provost
104*40e04359SKristof Provost void
ifconfig_error_clear(ifconfig_handle_t * h)105*40e04359SKristof Provost ifconfig_error_clear(ifconfig_handle_t *h)
106*40e04359SKristof Provost {
107*40e04359SKristof Provost h->error.errtype = OK;
108*40e04359SKristof Provost h->error.errcode = 0;
109*40e04359SKristof Provost }
110*40e04359SKristof Provost
111*40e04359SKristof Provost void
ifconfig_error(ifconfig_handle_t * h,ifconfig_errtype type,int error)112*40e04359SKristof Provost ifconfig_error(ifconfig_handle_t *h, ifconfig_errtype type, int error)
113*40e04359SKristof Provost {
114*40e04359SKristof Provost h->error.errtype = type;
115*40e04359SKristof Provost h->error.errcode = error;
116*40e04359SKristof Provost }
117