1999-09-09 03:06:24 +08:00
|
|
|
/* $OpenLDAP$ */
|
2003-11-26 07:17:08 +08:00
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
|
|
*
|
2021-01-12 03:25:53 +08:00
|
|
|
* Copyright 1998-2021 The OpenLDAP Foundation.
|
2003-11-26 07:17:08 +08:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted only as authorized by the OpenLDAP
|
|
|
|
* Public License.
|
|
|
|
*
|
|
|
|
* A copy of this license is available in the file LICENSE in the
|
|
|
|
* top-level directory of the distribution or, alternatively, at
|
|
|
|
* <http://www.OpenLDAP.org/license.html>.
|
1999-08-31 11:25:23 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
#include <ac/socket.h>
|
1999-09-01 01:51:04 +08:00
|
|
|
#include <ac/unistd.h>
|
1999-08-31 11:25:23 +08:00
|
|
|
|
1999-08-31 13:18:06 +08:00
|
|
|
#include <lutil.h>
|
|
|
|
|
|
|
|
/* Return a pair of socket descriptors that are connected to each other.
|
|
|
|
* The returned descriptors are suitable for use with select(). The two
|
1999-08-31 11:25:23 +08:00
|
|
|
* descriptors may or may not be identical; the function may return
|
|
|
|
* the same descriptor number in both slots. It is guaranteed that
|
1999-08-31 13:18:06 +08:00
|
|
|
* data written on sds[1] will be readable on sds[0]. The returned
|
1999-08-31 11:25:23 +08:00
|
|
|
* descriptors may be datagram oriented, so data should be written
|
|
|
|
* in reasonably small pieces and read all at once. On Unix systems
|
|
|
|
* this function is best implemented using a single pipe() call.
|
|
|
|
*/
|
1999-08-31 13:18:06 +08:00
|
|
|
|
2000-10-18 08:29:21 +08:00
|
|
|
int lutil_pair( ber_socket_t sds[2] )
|
1999-08-31 11:25:23 +08:00
|
|
|
{
|
1999-09-01 01:01:10 +08:00
|
|
|
#ifdef USE_PIPE
|
|
|
|
return pipe( sds );
|
|
|
|
#else
|
1999-08-31 11:25:23 +08:00
|
|
|
struct sockaddr_in si;
|
2007-06-11 04:35:30 +08:00
|
|
|
int rc;
|
|
|
|
ber_socklen_t len = sizeof(si);
|
2000-10-18 08:29:21 +08:00
|
|
|
ber_socket_t sd;
|
1999-08-31 11:25:23 +08:00
|
|
|
|
1999-08-31 13:18:06 +08:00
|
|
|
sd = socket( AF_INET, SOCK_DGRAM, 0 );
|
2000-10-18 08:29:21 +08:00
|
|
|
if ( sd == AC_SOCKET_INVALID ) {
|
1999-08-31 13:18:06 +08:00
|
|
|
return sd;
|
2000-10-18 08:29:21 +08:00
|
|
|
}
|
1999-08-31 11:25:23 +08:00
|
|
|
|
2000-06-11 06:39:30 +08:00
|
|
|
(void) memset( (void*) &si, '\0', len );
|
1999-08-31 11:25:23 +08:00
|
|
|
si.sin_family = AF_INET;
|
|
|
|
si.sin_port = 0;
|
|
|
|
si.sin_addr.s_addr = htonl( INADDR_LOOPBACK );
|
|
|
|
|
1999-09-01 00:47:42 +08:00
|
|
|
rc = bind( sd, (struct sockaddr *)&si, len );
|
|
|
|
if ( rc == AC_SOCKET_ERROR ) {
|
1999-08-31 13:18:06 +08:00
|
|
|
tcp_close(sd);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
1999-09-01 00:47:42 +08:00
|
|
|
rc = getsockname( sd, (struct sockaddr *)&si, &len );
|
|
|
|
if ( rc == AC_SOCKET_ERROR ) {
|
1999-08-31 13:18:06 +08:00
|
|
|
tcp_close(sd);
|
1999-08-31 11:25:23 +08:00
|
|
|
return rc;
|
|
|
|
}
|
1999-08-31 13:18:06 +08:00
|
|
|
|
1999-09-01 00:47:42 +08:00
|
|
|
rc = connect( sd, (struct sockaddr *)&si, len );
|
|
|
|
if ( rc == AC_SOCKET_ERROR ) {
|
1999-08-31 13:18:06 +08:00
|
|
|
tcp_close(sd);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2000-12-09 07:23:08 +08:00
|
|
|
sds[0] = sd;
|
2000-12-22 06:38:06 +08:00
|
|
|
#if !HAVE_WINSOCK
|
2000-12-09 07:23:08 +08:00
|
|
|
sds[1] = dup( sds[0] );
|
2000-12-22 06:38:06 +08:00
|
|
|
#else
|
|
|
|
sds[1] = sds[0];
|
|
|
|
#endif
|
1999-08-31 11:25:23 +08:00
|
|
|
return 0;
|
1999-09-01 01:01:10 +08:00
|
|
|
#endif
|
1999-08-31 11:25:23 +08:00
|
|
|
}
|