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