xref: /freebsd/lib/libifconfig/libifconfig_internal.c (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 
34 #include <net/if.h>
35 
36 #include <errno.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <sys/ioctl.h>
40 #include <unistd.h>
41 
42 
43 #include "libifconfig.h" // Needed for ifconfig_errstate
44 #include "libifconfig_internal.h"
45 
46 int
47 ifconfig_ioctlwrap_ret(ifconfig_handle_t *h, unsigned long request, int rcode)
48 {
49 	if (rcode != 0) {
50 		h->error.errtype = IOCTL;
51 		h->error.ioctl_request = request;
52 		h->error.errcode = errno;
53 	}
54 	return (rcode);
55 }
56 
57 
58 int
59 ifconfig_ioctlwrap(ifconfig_handle_t *h, const int addressfamily,
60     unsigned long request, struct ifreq *ifr)
61 {
62 	int s;
63 
64 	if (ifconfig_socket(h, addressfamily, &s) != 0) {
65 		return (-1);
66 	}
67 
68 	int rcode = ioctl(s, request, ifr);
69 	return (ifconfig_ioctlwrap_ret(h, request, rcode));
70 }
71 
72 
73 /*
74  * Function to get socket for the specified address family.
75  * If the socket doesn't already exist, attempt to create it.
76  */
77 int ifconfig_socket(ifconfig_handle_t *h, const int addressfamily, int *s)
78 {
79 	if (addressfamily > AF_MAX) {
80 		h->error.errtype = SOCKET;
81 		h->error.errcode = EINVAL;
82 		return (-1);
83 	}
84 
85 	if (h->sockets[addressfamily] != -1) {
86 		*s = h->sockets[addressfamily];
87 		return (0);
88 	}
89 
90 	/* We don't have a socket of that type available. Create one. */
91 	h->sockets[addressfamily] = socket(addressfamily, SOCK_DGRAM, 0);
92 	if (h->sockets[addressfamily] == -1) {
93 		h->error.errtype = SOCKET;
94 		h->error.errcode = errno;
95 		return (-1);
96 	}
97 
98 	*s = h->sockets[addressfamily];
99 	return (0);
100 }
101