1 /*- 2 * Copyright (c) 2009 Internet Initiative Japan Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 /* $Id: net_utils.c,v 1.4 2012/05/08 13:18:37 yasuoka Exp $ */ 27 #include <sys/types.h> 28 #include <sys/socket.h> 29 #include <netinet/in.h> 30 #include <net/if.h> 31 #include <ifaddrs.h> 32 #include <netdb.h> 33 #include <stdlib.h> 34 #include <string.h> 35 36 #include "net_utils.h" 37 38 /** Get an interface name from sockaddr */ 39 const char * 40 get_ifname_by_sockaddr(struct sockaddr *sa, char *ifname) 41 { 42 struct ifaddrs *addr, *addr0; 43 struct in_addr *in4a, *in4b; 44 const char *ifname0 = NULL; 45 struct in6_addr *in6a, *in6b; 46 47 ifname0 = NULL; 48 /* I want other way than linear search */ 49 getifaddrs(&addr0); 50 for (addr = addr0; ifname0 == NULL&& addr != NULL; 51 addr = addr->ifa_next) { 52 if (addr->ifa_addr->sa_family != sa->sa_family || 53 addr->ifa_addr->sa_len != sa->sa_len) 54 continue; 55 switch (addr->ifa_addr->sa_family) { 56 default: 57 continue; 58 case AF_INET: 59 in4a = &((struct sockaddr_in *)addr->ifa_addr) 60 ->sin_addr; 61 in4b = &((struct sockaddr_in *)sa)->sin_addr; 62 if (in4a->s_addr == in4b->s_addr) { 63 strlcpy(ifname, addr->ifa_name, IF_NAMESIZE); 64 ifname0 = ifname; 65 } 66 break; 67 case AF_INET6: 68 in6a = &((struct sockaddr_in6 *)addr->ifa_addr) 69 ->sin6_addr; 70 in6b = &((struct sockaddr_in6 *)sa)->sin6_addr; 71 if (IN6_ARE_ADDR_EQUAL(in6a, in6b)) { 72 strlcpy(ifname, addr->ifa_name, IF_NAMESIZE); 73 ifname0 = ifname; 74 } 75 break; 76 } 77 } 78 freeifaddrs(addr0); 79 80 return ifname0; 81 } 82 83 /** 84 * Convert argument like "192.168.160.1:1723/tcp" or "[::1]:1723/tcp" to 85 * match getaddrinfo(3)'s specification and pass them to getaddrinfo(3). 86 */ 87 int 88 addrport_parse(const char *addrport, int proto, struct addrinfo **p_ai) 89 { 90 char buf[256]; 91 char *servp, *nodep, *slash; 92 struct addrinfo hints; 93 94 strlcpy(buf, addrport, sizeof(buf)); 95 if (buf[0] == '[' && (servp = strchr(buf, ']')) != NULL) { 96 nodep = buf + 1; 97 *servp++ = '\0'; 98 if (*servp != ':') 99 servp = NULL; 100 } else { 101 nodep = buf; 102 servp = strrchr(nodep, ':'); 103 } 104 if (servp != NULL) { 105 *servp = '\0'; 106 servp++; 107 slash = strrchr(servp, '/'); 108 if (slash != NULL) { 109 /* 110 * Ignore like "/tcp" 111 */ 112 *slash = '\0'; 113 slash++; 114 } 115 } else 116 servp = NULL; 117 memset(&hints, 0, sizeof(hints)); 118 hints.ai_flags = AI_NUMERICHOST; 119 hints.ai_family = AF_UNSPEC; 120 switch (proto) { 121 case IPPROTO_TCP: 122 hints.ai_socktype = SOCK_STREAM; 123 break; 124 case IPPROTO_UDP: 125 hints.ai_socktype = SOCK_DGRAM; 126 break; 127 } 128 hints.ai_protocol = proto; 129 130 return getaddrinfo(nodep, servp, &hints, p_ai); 131 } 132 133 /** 134 * Make a string like "192.168.160.1:1723" or "[::1]:1723" from a struct 135 * sockaddr 136 * 137 * @param buf the buffer to be stored a string 138 * @param lbuf the length of the buf 139 */ 140 const char * 141 addrport_tostring(struct sockaddr *sa, socklen_t salen, char *buf, int lbuf) 142 { 143 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV]; 144 145 if (getnameinfo(sa, salen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), 146 NI_NUMERICHOST | NI_NUMERICSERV) != 0) 147 return NULL; 148 149 switch (sa->sa_family) { 150 case AF_INET6: 151 strlcpy(buf, "[", lbuf); 152 strlcat(buf, hbuf, lbuf); 153 strlcat(buf, "]:", lbuf); 154 strlcat(buf, sbuf, lbuf); 155 break; 156 case AF_INET: 157 strlcpy(buf, hbuf, lbuf); 158 strlcat(buf, ":", lbuf); 159 strlcat(buf, sbuf, lbuf); 160 break; 161 default: 162 return NULL; 163 } 164 165 return buf; 166 } 167 168 /** Convert 32bit IPv4 netmask to the prefix length in host byte order */ 169 int 170 netmask2prefixlen(uint32_t mask) 171 { 172 switch(mask) { 173 case 0x00000000: return 0; 174 case 0x80000000: return 1; 175 case 0xC0000000: return 2; 176 case 0xE0000000: return 3; 177 case 0xF0000000: return 4; 178 case 0xF8000000: return 5; 179 case 0xFC000000: return 6; 180 case 0xFE000000: return 7; 181 case 0xFF000000: return 8; 182 case 0xFF800000: return 9; 183 case 0xFFC00000: return 10; 184 case 0xFFE00000: return 11; 185 case 0xFFF00000: return 12; 186 case 0xFFF80000: return 13; 187 case 0xFFFC0000: return 14; 188 case 0xFFFE0000: return 15; 189 case 0xFFFF0000: return 16; 190 case 0xFFFF8000: return 17; 191 case 0xFFFFC000: return 18; 192 case 0xFFFFE000: return 19; 193 case 0xFFFFF000: return 20; 194 case 0xFFFFF800: return 21; 195 case 0xFFFFFC00: return 22; 196 case 0xFFFFFE00: return 23; 197 case 0xFFFFFF00: return 24; 198 case 0xFFFFFF80: return 25; 199 case 0xFFFFFFC0: return 26; 200 case 0xFFFFFFE0: return 27; 201 case 0xFFFFFFF0: return 28; 202 case 0xFFFFFFF8: return 29; 203 case 0xFFFFFFFC: return 30; 204 case 0xFFFFFFFE: return 31; 205 case 0xFFFFFFFF: return 32; 206 } 207 return -1; 208 } 209