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