1 /* $NetBSD: socket.c,v 1.1.1.2 2014/04/24 12:45:52 pettai Exp $ */
4 * Copyright (c) 1999 - 2000 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <krb5/roken.h>
42 * Set `sa' to the unitialized address of address family `af'
45 ROKEN_LIB_FUNCTION
void ROKEN_LIB_CALL
46 socket_set_any (struct sockaddr
*sa
, int af
)
50 struct sockaddr_in
*sin4
= (struct sockaddr_in
*)sa
;
52 memset (sin4
, 0, sizeof(*sin4
));
53 sin4
->sin_family
= AF_INET
;
55 sin4
->sin_addr
.s_addr
= INADDR_ANY
;
60 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)sa
;
62 memset (sin6
, 0, sizeof(*sin6
));
63 sin6
->sin6_family
= AF_INET6
;
65 sin6
->sin6_addr
= in6addr_any
;
70 errx (1, "unknown address family %d", sa
->sa_family
);
76 * set `sa' to (`ptr', `port')
79 ROKEN_LIB_FUNCTION
void ROKEN_LIB_CALL
80 socket_set_address_and_port (struct sockaddr
*sa
, const void *ptr
, int port
)
82 switch (sa
->sa_family
) {
84 struct sockaddr_in
*sin4
= (struct sockaddr_in
*)sa
;
86 memset (sin4
, 0, sizeof(*sin4
));
87 sin4
->sin_family
= AF_INET
;
88 sin4
->sin_port
= port
;
89 memcpy (&sin4
->sin_addr
, ptr
, sizeof(struct in_addr
));
94 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)sa
;
96 memset (sin6
, 0, sizeof(*sin6
));
97 sin6
->sin6_family
= AF_INET6
;
98 sin6
->sin6_port
= port
;
99 memcpy (&sin6
->sin6_addr
, ptr
, sizeof(struct in6_addr
));
104 errx (1, "unknown address family %d", sa
->sa_family
);
110 * Return the size of an address of the type in `sa'
113 ROKEN_LIB_FUNCTION
size_t ROKEN_LIB_CALL
114 socket_addr_size (const struct sockaddr
*sa
)
116 switch (sa
->sa_family
) {
118 return sizeof(struct in_addr
);
121 return sizeof(struct in6_addr
);
129 * Return the size of a `struct sockaddr' in `sa'.
132 ROKEN_LIB_FUNCTION
size_t ROKEN_LIB_CALL
133 socket_sockaddr_size (const struct sockaddr
*sa
)
135 switch (sa
->sa_family
) {
137 return sizeof(struct sockaddr_in
);
140 return sizeof(struct sockaddr_in6
);
148 * Return the binary address of `sa'.
151 ROKEN_LIB_FUNCTION
void * ROKEN_LIB_CALL
152 socket_get_address (const struct sockaddr
*sa
)
154 switch (sa
->sa_family
) {
156 const struct sockaddr_in
*sin4
= (const struct sockaddr_in
*)sa
;
157 return rk_UNCONST(&sin4
->sin_addr
);
161 const struct sockaddr_in6
*sin6
= (const struct sockaddr_in6
*)sa
;
162 return rk_UNCONST(&sin6
->sin6_addr
);
171 * Return the port number from `sa'.
174 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
175 socket_get_port (const struct sockaddr
*sa
)
177 switch (sa
->sa_family
) {
179 const struct sockaddr_in
*sin4
= (const struct sockaddr_in
*)sa
;
180 return sin4
->sin_port
;
184 const struct sockaddr_in6
*sin6
= (const struct sockaddr_in6
*)sa
;
185 return sin6
->sin6_port
;
194 * Set the port in `sa' to `port'.
197 ROKEN_LIB_FUNCTION
void ROKEN_LIB_CALL
198 socket_set_port (struct sockaddr
*sa
, int port
)
200 switch (sa
->sa_family
) {
202 struct sockaddr_in
*sin4
= (struct sockaddr_in
*)sa
;
203 sin4
->sin_port
= port
;
208 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)sa
;
209 sin6
->sin6_port
= port
;
214 errx (1, "unknown address family %d", sa
->sa_family
);
220 * Set the range of ports to use when binding with port = 0.
222 ROKEN_LIB_FUNCTION
void ROKEN_LIB_CALL
223 socket_set_portrange (rk_socket_t sock
, int restr
, int af
)
225 #if defined(IP_PORTRANGE)
227 int on
= restr
? IP_PORTRANGE_HIGH
: IP_PORTRANGE_DEFAULT
;
228 setsockopt (sock
, IPPROTO_IP
, IP_PORTRANGE
, &on
, sizeof(on
));
231 #if defined(IPV6_PORTRANGE)
232 if (af
== AF_INET6
) {
233 int on
= restr
? IPV6_PORTRANGE_HIGH
: IPV6_PORTRANGE_DEFAULT
;
234 setsockopt (sock
, IPPROTO_IPV6
, IPV6_PORTRANGE
, &on
, sizeof(on
));
240 * Enable debug on `sock'.
243 ROKEN_LIB_FUNCTION
void ROKEN_LIB_CALL
244 socket_set_debug (rk_socket_t sock
)
246 #if defined(SO_DEBUG) && defined(HAVE_SETSOCKOPT)
248 setsockopt (sock
, SOL_SOCKET
, SO_DEBUG
, (void *) &on
, sizeof (on
));
253 * Set the type-of-service of `sock' to `tos'.
256 ROKEN_LIB_FUNCTION
void ROKEN_LIB_CALL
257 socket_set_tos (rk_socket_t sock
, int tos
)
259 #if defined(IP_TOS) && defined(HAVE_SETSOCKOPT)
260 setsockopt (sock
, IPPROTO_IP
, IP_TOS
, (void *) &tos
, sizeof(int));
265 * set the reuse of addresses on `sock' to `val'.
268 ROKEN_LIB_FUNCTION
void ROKEN_LIB_CALL
269 socket_set_reuseaddr (rk_socket_t sock
, int val
)
271 #if defined(SO_REUSEADDR) && defined(HAVE_SETSOCKOPT)
272 setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, (void *)&val
, sizeof(val
));
277 * Set the that the `sock' should bind to only IPv6 addresses.
280 ROKEN_LIB_FUNCTION
void ROKEN_LIB_CALL
281 socket_set_ipv6only (rk_socket_t sock
, int val
)
283 #if defined(IPV6_V6ONLY) && defined(HAVE_SETSOCKOPT)
284 setsockopt(sock
, IPPROTO_IPV6
, IPV6_V6ONLY
, (void *)&val
, sizeof(val
));
289 * Create a file descriptor from a socket
291 * While the socket handle in \a sock can be used with WinSock
292 * functions after calling socket_to_fd(), it should not be closed
293 * with rk_closesocket(). The socket will be closed when the associated
294 * file descriptor is closed.
296 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
297 socket_to_fd(rk_socket_t sock
, int flags
)
302 return _open_osfhandle((intptr_t) sock
, flags
);
307 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
308 rk_SOCK_IOCTL(SOCKET s
, long cmd
, int * argp
) {
309 u_long ul
= (argp
)? *argp
: 0;
312 rv
= ioctlsocket(s
, cmd
, &ul
);
319 #ifndef HEIMDAL_SMALLER
322 int rk_socket(int, int, int);
325 rk_socket(int domain
, int type
, int protocol
)
328 s
= socket (domain
, type
, protocol
);
330 if ((SOCK_CLOEXEC
& type
) && s
< 0 && errno
== EINVAL
) {
331 type
&= ~SOCK_CLOEXEC
;
332 s
= socket (domain
, type
, protocol
);
338 #endif /* HEIMDAL_SMALLER */