xref: /openbsd-src/usr.sbin/npppd/common/net_utils.c (revision 7a7bab9d0fc77be5441ab2f8918cffbfaff97012)
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.3 2012/05/08 13:15:11 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 #ifdef NPPPD_USE_RTEV
39 #include "rtev.h"
40 #endif
41 
42 /** Get an interface name from sockaddr */
43 const char *
44 get_ifname_by_sockaddr(struct sockaddr *sa, char *ifname)
45 {
46 	struct ifaddrs *addr, *addr0;
47 	struct in_addr *in4a, *in4b;
48 	const char *ifname0 = NULL;
49 #ifdef INET6
50 	struct in6_addr *in6a, *in6b;
51 #endif
52 
53 	ifname0 = NULL;
54 	/* I want other way than linear search */
55 	getifaddrs(&addr0);
56 	for (addr = addr0; ifname0 == NULL&& addr != NULL;
57 	    addr = addr->ifa_next) {
58 		if (addr->ifa_addr->sa_family != sa->sa_family ||
59 		    addr->ifa_addr->sa_len != sa->sa_len)
60 			continue;
61 		switch (addr->ifa_addr->sa_family) {
62 		default:
63 			continue;
64 		case AF_INET:
65 			in4a = &((struct sockaddr_in *)addr->ifa_addr)
66 			    ->sin_addr;
67 			in4b = &((struct sockaddr_in *)sa)->sin_addr;
68 			if (in4a->s_addr == in4b->s_addr) {
69 				strlcpy(ifname, addr->ifa_name, IF_NAMESIZE);
70 				ifname0 = ifname;
71 			}
72 			break;
73 #ifdef INET6
74 		case AF_INET6:
75 			in6a = &((struct sockaddr_in6 *)addr->ifa_addr)
76 			    ->sin6_addr;
77 			in6b = &((struct sockaddr_in6 *)sa)->sin6_addr;
78 			if (IN6_ARE_ADDR_EQUAL(in6a, in6b)) {
79 				strlcpy(ifname, addr->ifa_name, IF_NAMESIZE);
80 				ifname0 = ifname;
81 			}
82 			break;
83 #endif
84 		}
85 	}
86 	freeifaddrs(addr0);
87 
88 	return ifname0;
89 }
90 
91 /**
92  * Convert argument like "192.168.160.1:1723/tcp" or "[::1]:1723/tcp" to
93  * match getaddrinfo(3)'s specification and pass them to getaddrinfo(3).
94  */
95 int
96 addrport_parse(const char *addrport, int proto, struct addrinfo **p_ai)
97 {
98 	char buf[256];
99 	char *servp, *nodep, *slash;
100 	struct addrinfo hints;
101 
102 	strlcpy(buf, addrport, sizeof(buf));
103 	if (buf[0] == '[' && (servp = strchr(buf, ']')) != NULL) {
104 		nodep = buf + 1;
105 		*servp++ = '\0';
106 		if (*servp != ':')
107 			servp = NULL;
108 	} else {
109 		nodep = buf;
110 		servp = strrchr(nodep, ':');
111 	}
112 	if (servp != NULL) {
113 		*servp = '\0';
114 		servp++;
115 		slash = strrchr(servp, '/');
116 		if (slash != NULL) {
117 			/*
118 			 * Ignore like "/tcp"
119 			 */
120 			*slash = '\0';
121 			slash++;
122 		}
123 	} else
124 		servp = NULL;
125 	memset(&hints, 0, sizeof(hints));
126 	hints.ai_flags = AI_NUMERICHOST;
127 	hints.ai_family = AF_UNSPEC;
128 	switch (proto) {
129 	case IPPROTO_TCP:
130 		hints.ai_socktype = SOCK_STREAM;
131 		break;
132 	case IPPROTO_UDP:
133 		hints.ai_socktype = SOCK_DGRAM;
134 		break;
135 	}
136 	hints.ai_protocol = proto;
137 
138 	return getaddrinfo(nodep, servp, &hints, p_ai);
139 }
140 
141 /**
142  * Make a string like "192.168.160.1:1723" or "[::1]:1723" from a struct
143  * sockaddr
144  *
145  * @param	buf	the buffer to be stored a string
146  * @param	lbuf	the length of the buf
147  */
148 const char *
149 addrport_tostring(struct sockaddr *sa, socklen_t salen, char *buf, int lbuf)
150 {
151 	char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
152 
153 	if (getnameinfo(sa, salen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf),
154 	    NI_NUMERICHOST | NI_NUMERICSERV) != 0)
155 		return NULL;
156 
157 	switch (sa->sa_family) {
158 	case AF_INET6:
159 		strlcpy(buf, "[", lbuf);
160 		strlcat(buf, hbuf, lbuf);
161 		strlcat(buf, "]:", lbuf);
162 		strlcat(buf, sbuf, lbuf);
163 		break;
164 	case AF_INET:
165 		strlcpy(buf, hbuf, lbuf);
166 		strlcat(buf, ":", lbuf);
167 		strlcat(buf, sbuf, lbuf);
168 		break;
169 	default:
170 		return NULL;
171 	}
172 
173 	return buf;
174 }
175 
176 /** Convert 32bit IPv4 netmask to the prefix length in host byte order */
177 int
178 netmask2prefixlen(uint32_t mask)
179 {
180     switch(mask) {
181     case 0x00000000:  return  0;
182     case 0x80000000:  return  1;
183     case 0xC0000000:  return  2;
184     case 0xE0000000:  return  3;
185     case 0xF0000000:  return  4;
186     case 0xF8000000:  return  5;
187     case 0xFC000000:  return  6;
188     case 0xFE000000:  return  7;
189     case 0xFF000000:  return  8;
190     case 0xFF800000:  return  9;
191     case 0xFFC00000:  return 10;
192     case 0xFFE00000:  return 11;
193     case 0xFFF00000:  return 12;
194     case 0xFFF80000:  return 13;
195     case 0xFFFC0000:  return 14;
196     case 0xFFFE0000:  return 15;
197     case 0xFFFF0000:  return 16;
198     case 0xFFFF8000:  return 17;
199     case 0xFFFFC000:  return 18;
200     case 0xFFFFE000:  return 19;
201     case 0xFFFFF000:  return 20;
202     case 0xFFFFF800:  return 21;
203     case 0xFFFFFC00:  return 22;
204     case 0xFFFFFE00:  return 23;
205     case 0xFFFFFF00:  return 24;
206     case 0xFFFFFF80:  return 25;
207     case 0xFFFFFFC0:  return 26;
208     case 0xFFFFFFE0:  return 27;
209     case 0xFFFFFFF0:  return 28;
210     case 0xFFFFFFF8:  return 29;
211     case 0xFFFFFFFC:  return 30;
212     case 0xFFFFFFFE:  return 31;
213     case 0xFFFFFFFF:  return 32;
214     }
215     return -1;
216 }
217