2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2018 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
17 #include <ac/socket.h>
18 #include <ac/unistd.h>
22 /* Return a pair of socket descriptors that are connected to each other.
23 * The returned descriptors are suitable for use with select(). The two
24 * descriptors may or may not be identical; the function may return
25 * the same descriptor number in both slots. It is guaranteed that
26 * data written on sds[1] will be readable on sds[0]. The returned
27 * descriptors may be datagram oriented, so data should be written
28 * in reasonably small pieces and read all at once. On Unix systems
29 * this function is best implemented using a single pipe() call.
32 int lutil_pair( ber_socket_t sds[2] )
37 struct sockaddr_in si;
39 ber_socklen_t len = sizeof(si);
42 sd = socket( AF_INET, SOCK_DGRAM, 0 );
43 if ( sd == AC_SOCKET_INVALID ) {
47 (void) memset( (void*) &si, '\0', len );
48 si.sin_family = AF_INET;
50 si.sin_addr.s_addr = htonl( INADDR_LOOPBACK );
52 rc = bind( sd, (struct sockaddr *)&si, len );
53 if ( rc == AC_SOCKET_ERROR ) {
58 rc = getsockname( sd, (struct sockaddr *)&si, &len );
59 if ( rc == AC_SOCKET_ERROR ) {
64 rc = connect( sd, (struct sockaddr *)&si, len );
65 if ( rc == AC_SOCKET_ERROR ) {
72 sds[1] = dup( sds[0] );