Home | History | Annotate | Line # | Download | only in roken
      1  1.1     elric /*	$NetBSD: socket.c,v 1.2 2017/01/28 21:31:50 christos Exp $	*/
      2  1.1     elric 
      3  1.1     elric /*
      4  1.1     elric  * Copyright (c) 1999 - 2000 Kungliga Tekniska Hgskolan
      5  1.1     elric  * (Royal Institute of Technology, Stockholm, Sweden).
      6  1.1     elric  * All rights reserved.
      7  1.1     elric  *
      8  1.1     elric  * Redistribution and use in source and binary forms, with or without
      9  1.1     elric  * modification, are permitted provided that the following conditions
     10  1.1     elric  * are met:
     11  1.1     elric  *
     12  1.1     elric  * 1. Redistributions of source code must retain the above copyright
     13  1.1     elric  *    notice, this list of conditions and the following disclaimer.
     14  1.1     elric  *
     15  1.1     elric  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1     elric  *    notice, this list of conditions and the following disclaimer in the
     17  1.1     elric  *    documentation and/or other materials provided with the distribution.
     18  1.1     elric  *
     19  1.1     elric  * 3. Neither the name of the Institute nor the names of its contributors
     20  1.1     elric  *    may be used to endorse or promote products derived from this software
     21  1.1     elric  *    without specific prior written permission.
     22  1.1     elric  *
     23  1.1     elric  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
     24  1.1     elric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1     elric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1     elric  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
     27  1.1     elric  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1     elric  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1     elric  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1     elric  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1     elric  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1     elric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1     elric  * SUCH DAMAGE.
     34  1.1     elric  */
     35  1.1     elric 
     36  1.1     elric #include <config.h>
     37  1.1     elric 
     38  1.1     elric #include <krb5/roken.h>
     39  1.1     elric #include <err.h>
     40  1.1     elric 
     41  1.1     elric /*
     42  1.1     elric  * Set `sa' to the unitialized address of address family `af'
     43  1.1     elric  */
     44  1.1     elric 
     45  1.1     elric ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
     46  1.1     elric socket_set_any (struct sockaddr *sa, int af)
     47  1.1     elric {
     48  1.1     elric     switch (af) {
     49  1.1     elric     case AF_INET : {
     50  1.1     elric 	struct sockaddr_in *sin4 = (struct sockaddr_in *)sa;
     51  1.1     elric 
     52  1.1     elric 	memset (sin4, 0, sizeof(*sin4));
     53  1.1     elric 	sin4->sin_family = AF_INET;
     54  1.1     elric 	sin4->sin_port   = 0;
     55  1.1     elric 	sin4->sin_addr.s_addr = INADDR_ANY;
     56  1.1     elric 	break;
     57  1.1     elric     }
     58  1.1     elric #ifdef HAVE_IPV6
     59  1.1     elric     case AF_INET6 : {
     60  1.1     elric 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
     61  1.1     elric 
     62  1.1     elric 	memset (sin6, 0, sizeof(*sin6));
     63  1.1     elric 	sin6->sin6_family = AF_INET6;
     64  1.1     elric 	sin6->sin6_port   = 0;
     65  1.1     elric 	sin6->sin6_addr   = in6addr_any;
     66  1.1     elric 	break;
     67  1.1     elric     }
     68  1.1     elric #endif
     69  1.1     elric     default :
     70  1.1     elric 	errx (1, "unknown address family %d", sa->sa_family);
     71  1.1     elric 	break;
     72  1.1     elric     }
     73  1.1     elric }
     74  1.1     elric 
     75  1.1     elric /*
     76  1.1     elric  * set `sa' to (`ptr', `port')
     77  1.1     elric  */
     78  1.1     elric 
     79  1.1     elric ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
     80  1.1     elric socket_set_address_and_port (struct sockaddr *sa, const void *ptr, int port)
     81  1.1     elric {
     82  1.1     elric     switch (sa->sa_family) {
     83  1.1     elric     case AF_INET : {
     84  1.1     elric 	struct sockaddr_in *sin4 = (struct sockaddr_in *)sa;
     85  1.1     elric 
     86  1.1     elric 	memset (sin4, 0, sizeof(*sin4));
     87  1.1     elric 	sin4->sin_family = AF_INET;
     88  1.1     elric 	sin4->sin_port   = port;
     89  1.1     elric 	memcpy (&sin4->sin_addr, ptr, sizeof(struct in_addr));
     90  1.1     elric 	break;
     91  1.1     elric     }
     92  1.1     elric #ifdef HAVE_IPV6
     93  1.1     elric     case AF_INET6 : {
     94  1.1     elric 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
     95  1.1     elric 
     96  1.1     elric 	memset (sin6, 0, sizeof(*sin6));
     97  1.1     elric 	sin6->sin6_family = AF_INET6;
     98  1.1     elric 	sin6->sin6_port   = port;
     99  1.1     elric 	memcpy (&sin6->sin6_addr, ptr, sizeof(struct in6_addr));
    100  1.1     elric 	break;
    101  1.1     elric     }
    102  1.1     elric #endif
    103  1.1     elric     default :
    104  1.1     elric 	errx (1, "unknown address family %d", sa->sa_family);
    105  1.1     elric 	break;
    106  1.1     elric     }
    107  1.1     elric }
    108  1.1     elric 
    109  1.1     elric /*
    110  1.1     elric  * Return the size of an address of the type in `sa'
    111  1.1     elric  */
    112  1.1     elric 
    113  1.1     elric ROKEN_LIB_FUNCTION size_t ROKEN_LIB_CALL
    114  1.1     elric socket_addr_size (const struct sockaddr *sa)
    115  1.1     elric {
    116  1.1     elric     switch (sa->sa_family) {
    117  1.1     elric     case AF_INET :
    118  1.1     elric 	return sizeof(struct in_addr);
    119  1.1     elric #ifdef HAVE_IPV6
    120  1.1     elric     case AF_INET6 :
    121  1.1     elric 	return sizeof(struct in6_addr);
    122  1.1     elric #endif
    123  1.1     elric     default :
    124  1.1     elric 	return 0;
    125  1.1     elric     }
    126  1.1     elric }
    127  1.1     elric 
    128  1.1     elric /*
    129  1.1     elric  * Return the size of a `struct sockaddr' in `sa'.
    130  1.1     elric  */
    131  1.1     elric 
    132  1.1     elric ROKEN_LIB_FUNCTION size_t ROKEN_LIB_CALL
    133  1.1     elric socket_sockaddr_size (const struct sockaddr *sa)
    134  1.1     elric {
    135  1.1     elric     switch (sa->sa_family) {
    136  1.1     elric     case AF_INET :
    137  1.1     elric 	return sizeof(struct sockaddr_in);
    138  1.1     elric #ifdef HAVE_IPV6
    139  1.1     elric     case AF_INET6 :
    140  1.1     elric 	return sizeof(struct sockaddr_in6);
    141  1.1     elric #endif
    142  1.1     elric     default:
    143  1.1     elric 	return 0;
    144  1.1     elric     }
    145  1.1     elric }
    146  1.1     elric 
    147  1.1     elric /*
    148  1.1     elric  * Return the binary address of `sa'.
    149  1.1     elric  */
    150  1.1     elric 
    151  1.1     elric ROKEN_LIB_FUNCTION void * ROKEN_LIB_CALL
    152  1.1     elric socket_get_address (const struct sockaddr *sa)
    153  1.1     elric {
    154  1.1     elric     switch (sa->sa_family) {
    155  1.1     elric     case AF_INET : {
    156  1.1     elric 	const struct sockaddr_in *sin4 = (const struct sockaddr_in *)sa;
    157  1.1     elric 	return rk_UNCONST(&sin4->sin_addr);
    158  1.1     elric     }
    159  1.1     elric #ifdef HAVE_IPV6
    160  1.1     elric     case AF_INET6 : {
    161  1.1     elric 	const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sa;
    162  1.1     elric 	return rk_UNCONST(&sin6->sin6_addr);
    163  1.1     elric     }
    164  1.1     elric #endif
    165  1.1     elric     default:
    166  1.1     elric 	return NULL;
    167  1.1     elric     }
    168  1.1     elric }
    169  1.1     elric 
    170  1.1     elric /*
    171  1.1     elric  * Return the port number from `sa'.
    172  1.1     elric  */
    173  1.1     elric 
    174  1.1     elric ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
    175  1.1     elric socket_get_port (const struct sockaddr *sa)
    176  1.1     elric {
    177  1.1     elric     switch (sa->sa_family) {
    178  1.1     elric     case AF_INET : {
    179  1.1     elric 	const struct sockaddr_in *sin4 = (const struct sockaddr_in *)sa;
    180  1.1     elric 	return sin4->sin_port;
    181  1.1     elric     }
    182  1.1     elric #ifdef HAVE_IPV6
    183  1.1     elric     case AF_INET6 : {
    184  1.1     elric 	const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sa;
    185  1.1     elric 	return sin6->sin6_port;
    186  1.1     elric     }
    187  1.1     elric #endif
    188  1.1     elric     default :
    189  1.1     elric 	return 0;
    190  1.1     elric     }
    191  1.1     elric }
    192  1.1     elric 
    193  1.1     elric /*
    194  1.1     elric  * Set the port in `sa' to `port'.
    195  1.1     elric  */
    196  1.1     elric 
    197  1.1     elric ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
    198  1.1     elric socket_set_port (struct sockaddr *sa, int port)
    199  1.1     elric {
    200  1.1     elric     switch (sa->sa_family) {
    201  1.1     elric     case AF_INET : {
    202  1.1     elric 	struct sockaddr_in *sin4 = (struct sockaddr_in *)sa;
    203  1.1     elric 	sin4->sin_port = port;
    204  1.1     elric 	break;
    205  1.1     elric     }
    206  1.1     elric #ifdef HAVE_IPV6
    207  1.1     elric     case AF_INET6 : {
    208  1.1     elric 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
    209  1.1     elric 	sin6->sin6_port = port;
    210  1.1     elric 	break;
    211  1.1     elric     }
    212  1.1     elric #endif
    213  1.1     elric     default :
    214  1.1     elric 	errx (1, "unknown address family %d", sa->sa_family);
    215  1.1     elric 	break;
    216  1.1     elric     }
    217  1.1     elric }
    218  1.1     elric 
    219  1.1     elric /*
    220  1.1     elric  * Set the range of ports to use when binding with port = 0.
    221  1.1     elric  */
    222  1.1     elric ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
    223  1.1     elric socket_set_portrange (rk_socket_t sock, int restr, int af)
    224  1.1     elric {
    225  1.1     elric #if defined(IP_PORTRANGE)
    226  1.1     elric 	if (af == AF_INET) {
    227  1.1     elric 		int on = restr ? IP_PORTRANGE_HIGH : IP_PORTRANGE_DEFAULT;
    228  1.1     elric 		setsockopt (sock, IPPROTO_IP, IP_PORTRANGE, &on, sizeof(on));
    229  1.1     elric 	}
    230  1.1     elric #endif
    231  1.1     elric #if defined(IPV6_PORTRANGE)
    232  1.1     elric 	if (af == AF_INET6) {
    233  1.1     elric 		int on = restr ? IPV6_PORTRANGE_HIGH : IPV6_PORTRANGE_DEFAULT;
    234  1.1     elric 		setsockopt (sock, IPPROTO_IPV6, IPV6_PORTRANGE, &on, sizeof(on));
    235  1.1     elric 	}
    236  1.1     elric #endif
    237  1.1     elric }
    238  1.2  christos 
    239  1.1     elric /*
    240  1.1     elric  * Enable debug on `sock'.
    241  1.1     elric  */
    242  1.1     elric 
    243  1.1     elric ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
    244  1.1     elric socket_set_debug (rk_socket_t sock)
    245  1.1     elric {
    246  1.1     elric #if defined(SO_DEBUG) && defined(HAVE_SETSOCKOPT)
    247  1.1     elric     int on = 1;
    248  1.1     elric     setsockopt (sock, SOL_SOCKET, SO_DEBUG, (void *) &on, sizeof (on));
    249  1.1     elric #endif
    250  1.1     elric }
    251  1.1     elric 
    252  1.1     elric /*
    253  1.1     elric  * Set the type-of-service of `sock' to `tos'.
    254  1.1     elric  */
    255  1.1     elric 
    256  1.1     elric ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
    257  1.1     elric socket_set_tos (rk_socket_t sock, int tos)
    258  1.1     elric {
    259  1.1     elric #if defined(IP_TOS) && defined(HAVE_SETSOCKOPT)
    260  1.1     elric     setsockopt (sock, IPPROTO_IP, IP_TOS, (void *) &tos, sizeof(int));
    261  1.1     elric #endif
    262  1.1     elric }
    263  1.1     elric 
    264  1.1     elric /*
    265  1.2  christos  * Set the non-blocking-ness of the socket.
    266  1.2  christos  */
    267  1.2  christos 
    268  1.2  christos ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
    269  1.2  christos socket_set_nonblocking(rk_socket_t sock, int nonblock)
    270  1.2  christos {
    271  1.2  christos #if defined(O_NONBLOCK)
    272  1.2  christos     int flags = fcntl(sock, F_GETFL, 0);
    273  1.2  christos     if (flags == -1)
    274  1.2  christos 	return;
    275  1.2  christos     if (nonblock)
    276  1.2  christos 	flags |= O_NONBLOCK;
    277  1.2  christos     else
    278  1.2  christos 	flags &= ~O_NONBLOCK;
    279  1.2  christos     fcntl(sock, F_SETFL, flags);
    280  1.2  christos #elif defined(FIOBIO)
    281  1.2  christos     int flags = !!nonblock;
    282  1.2  christos     return ioctl(sock, FIOBIO, &flags);
    283  1.2  christos #endif
    284  1.2  christos }
    285  1.2  christos 
    286  1.2  christos /*
    287  1.1     elric  * set the reuse of addresses on `sock' to `val'.
    288  1.1     elric  */
    289  1.1     elric 
    290  1.1     elric ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
    291  1.1     elric socket_set_reuseaddr (rk_socket_t sock, int val)
    292  1.1     elric {
    293  1.1     elric #if defined(SO_REUSEADDR) && defined(HAVE_SETSOCKOPT)
    294  1.1     elric     setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&val, sizeof(val));
    295  1.1     elric #endif
    296  1.1     elric }
    297  1.1     elric 
    298  1.1     elric /*
    299  1.1     elric  * Set the that the `sock' should bind to only IPv6 addresses.
    300  1.1     elric  */
    301  1.1     elric 
    302  1.1     elric ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
    303  1.1     elric socket_set_ipv6only (rk_socket_t sock, int val)
    304  1.1     elric {
    305  1.1     elric #if defined(IPV6_V6ONLY) && defined(HAVE_SETSOCKOPT)
    306  1.1     elric     setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&val, sizeof(val));
    307  1.1     elric #endif
    308  1.1     elric }
    309  1.1     elric 
    310  1.1     elric /**
    311  1.1     elric  * Create a file descriptor from a socket
    312  1.1     elric  *
    313  1.1     elric  * While the socket handle in \a sock can be used with WinSock
    314  1.1     elric  * functions after calling socket_to_fd(), it should not be closed
    315  1.1     elric  * with rk_closesocket().  The socket will be closed when the associated
    316  1.1     elric  * file descriptor is closed.
    317  1.1     elric  */
    318  1.1     elric ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
    319  1.1     elric socket_to_fd(rk_socket_t sock, int flags)
    320  1.1     elric {
    321  1.1     elric #ifndef _WIN32
    322  1.1     elric     return sock;
    323  1.1     elric #else
    324  1.1     elric     return _open_osfhandle((intptr_t) sock, flags);
    325  1.1     elric #endif
    326  1.1     elric }
    327  1.1     elric 
    328  1.1     elric #ifdef HAVE_WINSOCK
    329  1.1     elric ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
    330  1.1     elric rk_SOCK_IOCTL(SOCKET s, long cmd, int * argp) {
    331  1.1     elric     u_long ul = (argp)? *argp : 0;
    332  1.1     elric     int rv;
    333  1.1     elric 
    334  1.1     elric     rv = ioctlsocket(s, cmd, &ul);
    335  1.1     elric     if (argp)
    336  1.1     elric 	*argp = (int) ul;
    337  1.1     elric     return rv;
    338  1.1     elric }
    339  1.1     elric #endif
    340  1.1     elric 
    341  1.1     elric #ifndef HEIMDAL_SMALLER
    342  1.1     elric #undef socket
    343  1.1     elric 
    344  1.1     elric int rk_socket(int, int, int);
    345  1.1     elric 
    346  1.1     elric int
    347  1.1     elric rk_socket(int domain, int type, int protocol)
    348  1.1     elric {
    349  1.1     elric     int s;
    350  1.1     elric     s = socket (domain, type, protocol);
    351  1.1     elric #ifdef SOCK_CLOEXEC
    352  1.1     elric     if ((SOCK_CLOEXEC & type) && s < 0 && errno == EINVAL) {
    353  1.1     elric 	type &= ~SOCK_CLOEXEC;
    354  1.1     elric 	s = socket (domain, type, protocol);
    355  1.1     elric     }
    356  1.1     elric #endif
    357  1.1     elric     return s;
    358  1.1     elric }
    359  1.1     elric 
    360  1.1     elric #endif /* HEIMDAL_SMALLER */
    361