2 * Copyright (c) 1999 - 2000 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * Set `sa' to the unitialized address of address family `af'
43 ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
44 socket_set_any (struct sockaddr *sa, int af)
48 struct sockaddr_in *sin4 = (struct sockaddr_in *)sa;
50 memset (sin4, 0, sizeof(*sin4));
51 sin4->sin_family = AF_INET;
53 sin4->sin_addr.s_addr = INADDR_ANY;
58 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
60 memset (sin6, 0, sizeof(*sin6));
61 sin6->sin6_family = AF_INET6;
63 sin6->sin6_addr = in6addr_any;
68 errx (1, "unknown address family %d", sa->sa_family);
74 * set `sa' to (`ptr', `port')
77 ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
78 socket_set_address_and_port (struct sockaddr *sa, const void *ptr, int port)
80 switch (sa->sa_family) {
82 struct sockaddr_in *sin4 = (struct sockaddr_in *)sa;
84 memset (sin4, 0, sizeof(*sin4));
85 sin4->sin_family = AF_INET;
86 sin4->sin_port = port;
87 memcpy (&sin4->sin_addr, ptr, sizeof(struct in_addr));
92 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
94 memset (sin6, 0, sizeof(*sin6));
95 sin6->sin6_family = AF_INET6;
96 sin6->sin6_port = port;
97 memcpy (&sin6->sin6_addr, ptr, sizeof(struct in6_addr));
102 errx (1, "unknown address family %d", sa->sa_family);
108 * Return the size of an address of the type in `sa'
111 ROKEN_LIB_FUNCTION size_t ROKEN_LIB_CALL
112 socket_addr_size (const struct sockaddr *sa)
114 switch (sa->sa_family) {
116 return sizeof(struct in_addr);
119 return sizeof(struct in6_addr);
127 * Return the size of a `struct sockaddr' in `sa'.
130 ROKEN_LIB_FUNCTION size_t ROKEN_LIB_CALL
131 socket_sockaddr_size (const struct sockaddr *sa)
133 switch (sa->sa_family) {
135 return sizeof(struct sockaddr_in);
138 return sizeof(struct sockaddr_in6);
146 * Return the binary address of `sa'.
149 ROKEN_LIB_FUNCTION void * ROKEN_LIB_CALL
150 socket_get_address (const struct sockaddr *sa)
152 switch (sa->sa_family) {
154 const struct sockaddr_in *sin4 = (const struct sockaddr_in *)sa;
155 return rk_UNCONST(&sin4->sin_addr);
159 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sa;
160 return rk_UNCONST(&sin6->sin6_addr);
169 * Return the port number from `sa'.
172 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
173 socket_get_port (const struct sockaddr *sa)
175 switch (sa->sa_family) {
177 const struct sockaddr_in *sin4 = (const struct sockaddr_in *)sa;
178 return sin4->sin_port;
182 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sa;
183 return sin6->sin6_port;
192 * Set the port in `sa' to `port'.
195 ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
196 socket_set_port (struct sockaddr *sa, int port)
198 switch (sa->sa_family) {
200 struct sockaddr_in *sin4 = (struct sockaddr_in *)sa;
201 sin4->sin_port = port;
206 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
207 sin6->sin6_port = port;
212 errx (1, "unknown address family %d", sa->sa_family);
218 * Set the range of ports to use when binding with port = 0.
220 ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
221 socket_set_portrange (rk_socket_t sock, int restr, int af)
223 #if defined(IP_PORTRANGE)
225 int on = restr ? IP_PORTRANGE_HIGH : IP_PORTRANGE_DEFAULT;
226 setsockopt (sock, IPPROTO_IP, IP_PORTRANGE, &on, sizeof(on));
229 #if defined(IPV6_PORTRANGE)
230 if (af == AF_INET6) {
231 int on = restr ? IPV6_PORTRANGE_HIGH : IPV6_PORTRANGE_DEFAULT;
232 setsockopt (sock, IPPROTO_IPV6, IPV6_PORTRANGE, &on, sizeof(on));
238 * Enable debug on `sock'.
241 ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
242 socket_set_debug (rk_socket_t sock)
244 #if defined(SO_DEBUG) && defined(HAVE_SETSOCKOPT)
246 setsockopt (sock, SOL_SOCKET, SO_DEBUG, (void *) &on, sizeof (on));
251 * Set the type-of-service of `sock' to `tos'.
254 ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
255 socket_set_tos (rk_socket_t sock, int tos)
257 #if defined(IP_TOS) && defined(HAVE_SETSOCKOPT)
258 setsockopt (sock, IPPROTO_IP, IP_TOS, (void *) &tos, sizeof(int));
263 * set the reuse of addresses on `sock' to `val'.
266 ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
267 socket_set_reuseaddr (rk_socket_t sock, int val)
269 #if defined(SO_REUSEADDR) && defined(HAVE_SETSOCKOPT)
270 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&val, sizeof(val));
275 * Set the that the `sock' should bind to only IPv6 addresses.
278 ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
279 socket_set_ipv6only (rk_socket_t sock, int val)
281 #if defined(IPV6_V6ONLY) && defined(HAVE_SETSOCKOPT)
282 setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&val, sizeof(val));
287 * Create a file descriptor from a socket
289 * While the socket handle in \a sock can be used with WinSock
290 * functions after calling socket_to_fd(), it should not be closed
291 * with rk_closesocket(). The socket will be closed when the associated
292 * file descriptor is closed.
294 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
295 socket_to_fd(rk_socket_t sock, int flags)
300 return _open_osfhandle((intptr_t) sock, flags);
305 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
306 rk_SOCK_IOCTL(SOCKET s, long cmd, int * argp) {
307 u_long ul = (argp)? *argp : 0;
310 rv = ioctlsocket(s, cmd, &ul);
317 #ifndef HEIMDAL_SMALLER
320 int rk_socket(int, int, int);
323 rk_socket(int domain, int type, int protocol)
326 s = socket (domain, type, protocol);
328 if ((SOCK_CLOEXEC & type) && s < 0 && errno == EINVAL) {
329 type &= ~SOCK_CLOEXEC;
330 s = socket (domain, type, protocol);
336 #endif /* HEIMDAL_SMALLER */