xref: /freebsd/lib/libcasper/services/cap_net/cap_net.h (revision 2b8331622f0b212cf3bb4fc4914a501e5321d506)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2020 Mariusz Zaborski <oshogbo@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS 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
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 
30 #ifndef _CAP_NETWORK_H_
31 #define	_CAP_NETWORK_H_
32 
33 #ifdef HAVE_CASPER
34 #define WITH_CASPER
35 #endif
36 
37 #include <sys/dnv.h>
38 #include <sys/nv.h>
39 
40 #include <sys/socket.h>
41 
42 struct addrinfo;
43 struct hostent;
44 
45 struct cap_net_limit;
46 typedef struct cap_net_limit cap_net_limit_t;
47 
48 #define CAPNET_ADDR2NAME		(0x01)
49 #define CAPNET_NAME2ADDR		(0x02)
50 #define CAPNET_DEPRECATED_ADDR2NAME	(0x04)
51 #define CAPNET_DEPRECATED_NAME2ADDR	(0x08)
52 #define CAPNET_CONNECT			(0x10)
53 #define CAPNET_BIND			(0x20)
54 #define CAPNET_CONNECTDNS		(0x40)
55 
56 #ifdef WITH_CASPER
57 /* Capability functions. */
58 int cap_bind(cap_channel_t *chan, int s, const struct sockaddr *addr,
59     socklen_t addrlen);
60 int cap_connect(cap_channel_t *chan, int s, const struct sockaddr *name,
61     socklen_t namelen);
62 
63 int cap_getaddrinfo(cap_channel_t *chan, const char *hostname,
64     const char *servname, const struct addrinfo *hints, struct addrinfo **res);
65 int cap_getnameinfo(cap_channel_t *chan, const struct sockaddr *sa,
66     socklen_t salen, char *host, size_t hostlen, char *serv, size_t servlen,
67     int flags);
68 
69 /* Limit functions. */
70 cap_net_limit_t *cap_net_limit_init(cap_channel_t *chan, uint64_t mode);
71 int cap_net_limit(cap_net_limit_t *limit);
72 void cap_net_free(cap_net_limit_t *limit);
73 
74 cap_net_limit_t *cap_net_limit_addr2name_family(cap_net_limit_t *limit,
75     int *family, size_t size);
76 cap_net_limit_t *cap_net_limit_addr2name(cap_net_limit_t *limit,
77     const struct sockaddr *sa, socklen_t salen);
78 
79 cap_net_limit_t *cap_net_limit_name2addr_family(cap_net_limit_t *limit,
80     int *family, size_t size);
81 cap_net_limit_t *cap_net_limit_name2addr(cap_net_limit_t *limit,
82     const char *name, const char *serv);
83 
84 cap_net_limit_t *cap_net_limit_connect(cap_net_limit_t *limit,
85     const struct sockaddr *sa, socklen_t salen);
86 
87 cap_net_limit_t *cap_net_limit_bind(cap_net_limit_t *limit,
88     const struct sockaddr *sa, socklen_t salen);
89 
90 /* Deprecated functions. */
91 struct hostent *cap_gethostbyname(cap_channel_t *chan, const char *name);
92 struct hostent *cap_gethostbyname2(cap_channel_t *chan, const char *name,
93     int af);
94 struct hostent *cap_gethostbyaddr(cap_channel_t *chan, const void *addr,
95     socklen_t len, int af);
96 #else
97 /* Capability functions. */
98 #define cap_bind(chan, s, addr, addrlen)					\
99 	bind(s, addr, addrlen)
100 #define cap_connect(chan, s, name, namelen)					\
101 	connect(s, name, namelen)
102 #define	cap_getaddrinfo(chan, hostname, servname, hints, res)			\
103 	getaddrinfo(hostname, servname, hints, res)
104 #define	cap_getnameinfo(chan, sa, salen, host, hostlen, serv, servlen, flags)	\
105 	getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
106 
107 /* Limit functions. */
108 #define cap_net_limit_init(chan, mode)	((cap_net_limit_t *)malloc(8))
109 #define cap_net_free(limit)		free(limit)
110 static inline int
111 cap_net_limit(cap_net_limit_t *limit)
112 {
113 	free(limit);
114 	return (0);
115 }
116 
117 static inline cap_net_limit_t *
118 cap_net_limit_addr2name_family(cap_net_limit_t *limit,
119     int *family __unused, size_t size __unused)
120 {
121 	return (limit);
122 }
123 
124 static inline cap_net_limit_t *
125 cap_net_limit_addr2name(cap_net_limit_t *limit,
126     const struct sockaddr *sa __unused, socklen_t salen __unused)
127 {
128 	return (limit);
129 }
130 
131 static inline cap_net_limit_t *
132 cap_net_limit_name2addr_family(cap_net_limit_t *limit,
133     int *family __unused, size_t size __unused)
134 {
135 	return (limit);
136 }
137 
138 static inline cap_net_limit_t *
139 cap_net_limit_name2addr(cap_net_limit_t *limit,
140     const char *name __unused, const char *serv __unused)
141 {
142 	return (limit);
143 }
144 
145 static inline cap_net_limit_t *
146 cap_net_limit_connect(cap_net_limit_t *limit,
147     const struct sockaddr *sa __unused, socklen_t salen __unused)
148 {
149 	return (limit);
150 }
151 
152 static inline cap_net_limit_t *
153 cap_net_limit_bind(cap_net_limit_t *limit,
154     const struct sockaddr *sa __unused, socklen_t salen __unused)
155 {
156 	return (limit);
157 }
158 
159 /* Deprecated functions. */
160 #define	cap_gethostbyname(chan, name)		 gethostbyname(name)
161 #define	cap_gethostbyname2(chan, name, type)	 gethostbyname2(name, type)
162 #define	cap_gethostbyaddr(chan, addr, len, type) gethostbyaddr(addr, len, type)
163 #endif
164 
165 #endif	/* !_CAP_NETWORK_H_ */
166