1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 1997 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28 /* All Rights Reserved */ 29 30 /* 31 * University Copyright- Copyright (c) 1982, 1986, 1988 32 * The Regents of the University of California 33 * All Rights Reserved 34 * 35 * University Acknowledgment- Portions of this document are derived from 36 * software developed by the University of California, Berkeley, and its 37 * contributors. 38 */ 39 40 #include <sys/types.h> 41 #include <sys/socket.h> 42 #include <sys/stropts.h> 43 #include <sys/stream.h> 44 #include <sys/socketvar.h> 45 #include <errno.h> 46 #include <unistd.h> 47 #include <stdlib.h> 48 49 extern int _socket_create(int, int, int, int); 50 extern int _so_socketpair(int *); 51 52 int _socketpair_create(int, int, int, int [2], int); 53 54 #pragma weak socketpair = _socketpair 55 56 int 57 _socketpair(int family, int type, int protocol, int sv[2]) 58 { 59 return (_socketpair_create(family, type, protocol, sv, SOV_DEFAULT)); 60 } 61 62 /* 63 * Used by the BCP library. 64 */ 65 int 66 _socketpair_bsd(int family, int type, int protocol, int sv[2]) 67 { 68 return (_socketpair_create(family, type, protocol, sv, SOV_SOCKBSD)); 69 } 70 71 int 72 _socketpair_svr4(int family, int type, int protocol, int sv[2]) 73 { 74 return (_socketpair_create(family, type, protocol, sv, SOV_SOCKSTREAM)); 75 } 76 77 int 78 __xnet_socketpair(int family, int type, int protocol, int sv[2]) 79 { 80 return (_socketpair_create(family, type, protocol, sv, SOV_XPG4_2)); 81 } 82 83 int 84 _socketpair_create(int family, int type, int protocol, int sv[2], int version) 85 { 86 int res; 87 int fd1, fd2; 88 89 /* 90 * Create the two sockets and pass them to _so_socketpair, which 91 * will connect them together. 92 */ 93 fd1 = _socket_create(family, type, protocol, version); 94 if (fd1 < 0) 95 return (-1); 96 fd2 = _socket_create(family, type, protocol, version); 97 if (fd2 < 0) { 98 int error = errno; 99 100 (void) close(fd1); 101 errno = error; 102 return (-1); 103 } 104 sv[0] = fd1; 105 sv[1] = fd2; 106 res = _so_socketpair(sv); 107 if (res < 0) { 108 int error = errno; 109 110 (void) close(fd1); 111 (void) close(fd2); 112 errno = error; 113 return (-1); 114 } 115 /* 116 * Check if kernel returned different fds in which case we close 117 * the original ones. This is the case for SOCK_STREAM where 118 * one of the original sockets is used as a listener and 119 * _so_socketpair passes out the newly accepted socket. 120 */ 121 if (sv[0] != fd1) 122 (void) close(fd1); 123 if (sv[1] != fd2) 124 (void) close(fd2); 125 return (res); 126 } 127