17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*d2b5b2d3SAnders Persson  * Common Development and Distribution License (the "License").
6*d2b5b2d3SAnders Persson  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*d2b5b2d3SAnders Persson  * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
267c478bd9Sstevel@tonic-gate /*	  All Rights Reserved   */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
307c478bd9Sstevel@tonic-gate  * The Regents of the University of California
317c478bd9Sstevel@tonic-gate  * All Rights Reserved
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
347c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
357c478bd9Sstevel@tonic-gate  * contributors.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate #include <sys/socket.h>
407c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
417c478bd9Sstevel@tonic-gate #include <sys/stream.h>
427c478bd9Sstevel@tonic-gate #include <sys/socketvar.h>
437c478bd9Sstevel@tonic-gate #include <errno.h>
447c478bd9Sstevel@tonic-gate #include <unistd.h>
457c478bd9Sstevel@tonic-gate #include <stdlib.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate extern int _so_socket();
487c478bd9Sstevel@tonic-gate extern int _s_netconfig_path();
497c478bd9Sstevel@tonic-gate extern int _setsockopt();
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate int _socket_create(int, int, int, int);
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #pragma weak socket = _socket
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate int
_socket(int family,int type,int protocol)567c478bd9Sstevel@tonic-gate _socket(int family, int type, int protocol)
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate 	return (_socket_create(family, type, protocol, SOV_DEFAULT));
597c478bd9Sstevel@tonic-gate }
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate  * Used by the BCP library.
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate int
_socket_bsd(int family,int type,int protocol)657c478bd9Sstevel@tonic-gate _socket_bsd(int family, int type, int protocol)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate 	return (_socket_create(family, type, protocol, SOV_SOCKBSD));
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate int
_socket_svr4(int family,int type,int protocol)717c478bd9Sstevel@tonic-gate _socket_svr4(int family, int type, int protocol)
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate 	return (_socket_create(family, type, protocol, SOV_SOCKSTREAM));
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate int
__xnet_socket(int family,int type,int protocol)777c478bd9Sstevel@tonic-gate __xnet_socket(int family, int type, int protocol)
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate 	return (_socket_create(family, type, protocol, SOV_XPG4_2));
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate  * Create a socket endpoint for socket() and socketpair().
847c478bd9Sstevel@tonic-gate  * In SunOS 4.X and in SunOS 5.X prior to XPG 4.2 the only error
857c478bd9Sstevel@tonic-gate  * that could be returned due to invalid <family, type, protocol>
867c478bd9Sstevel@tonic-gate  * was EPROTONOSUPPORT. (While the SunOS 4.X source contains EPROTOTYPE
877c478bd9Sstevel@tonic-gate  * error as well that error can only be generated if the kernel is
887c478bd9Sstevel@tonic-gate  * incorrectly configured.)
897c478bd9Sstevel@tonic-gate  * For backwards compatibility only applications that request XPG 4.2
907c478bd9Sstevel@tonic-gate  * (through c89 or XOPEN_SOURCE) will get EPROTOTYPE or EAFNOSUPPORT errors.
917c478bd9Sstevel@tonic-gate  */
927c478bd9Sstevel@tonic-gate int
_socket_create(int family,int type,int protocol,int version)937c478bd9Sstevel@tonic-gate _socket_create(int family, int type, int protocol, int version)
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate 	int fd;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	/*
987c478bd9Sstevel@tonic-gate 	 * Try creating without knowing the device assuming that
99*d2b5b2d3SAnders Persson 	 * the transport provider is registered in /etc/sock2path.d.
1007c478bd9Sstevel@tonic-gate 	 * If none found fall back to using /etc/netconfig to look
1017c478bd9Sstevel@tonic-gate 	 * up the name of the transport device name. This provides
1027c478bd9Sstevel@tonic-gate 	 * backwards compatibility for transport providers that have not
103*d2b5b2d3SAnders Persson 	 * yet been converted to using /etc/sock2path.d.
104*d2b5b2d3SAnders Persson 	 * XXX When all transport providers use /etc/sock2path.d. this
1057c478bd9Sstevel@tonic-gate 	 * part of the code can be removed.
1067c478bd9Sstevel@tonic-gate 	 */
1077c478bd9Sstevel@tonic-gate 	fd = _so_socket(family, type, protocol, NULL, version);
1087c478bd9Sstevel@tonic-gate 	if (fd == -1) {
1097c478bd9Sstevel@tonic-gate 		char *devpath;
1107c478bd9Sstevel@tonic-gate 		int saved_errno = errno;
1117c478bd9Sstevel@tonic-gate 		int prototype = 0;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 		switch (saved_errno) {
1147c478bd9Sstevel@tonic-gate 		case EAFNOSUPPORT:
1157c478bd9Sstevel@tonic-gate 		case EPROTOTYPE:
1167c478bd9Sstevel@tonic-gate 			if (version != SOV_XPG4_2)
1177c478bd9Sstevel@tonic-gate 				saved_errno = EPROTONOSUPPORT;
1187c478bd9Sstevel@tonic-gate 			break;
1197c478bd9Sstevel@tonic-gate 		case EPROTONOSUPPORT:
1207c478bd9Sstevel@tonic-gate 			break;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 		default:
1237c478bd9Sstevel@tonic-gate 			errno = saved_errno;
1247c478bd9Sstevel@tonic-gate 			return (-1);
1257c478bd9Sstevel@tonic-gate 		}
1267c478bd9Sstevel@tonic-gate 		if (_s_netconfig_path(family, type, protocol,
127*d2b5b2d3SAnders Persson 		    &devpath, &prototype) == -1) {
1287c478bd9Sstevel@tonic-gate 			errno = saved_errno;
1297c478bd9Sstevel@tonic-gate 			return (-1);
1307c478bd9Sstevel@tonic-gate 		}
1317c478bd9Sstevel@tonic-gate 		fd = _so_socket(family, type, protocol, devpath, version);
1327c478bd9Sstevel@tonic-gate 		free(devpath);
1337c478bd9Sstevel@tonic-gate 		if (fd == -1) {
1347c478bd9Sstevel@tonic-gate 			errno = saved_errno;
1357c478bd9Sstevel@tonic-gate 			return (-1);
1367c478bd9Sstevel@tonic-gate 		}
1377c478bd9Sstevel@tonic-gate 		if (prototype != 0) {
1387c478bd9Sstevel@tonic-gate 			if (_setsockopt(fd, SOL_SOCKET, SO_PROTOTYPE,
1397c478bd9Sstevel@tonic-gate 			    (caddr_t)&prototype, (int)sizeof (prototype)) < 0) {
1407c478bd9Sstevel@tonic-gate 				(void) close(fd);
1417c478bd9Sstevel@tonic-gate 				/*
1427c478bd9Sstevel@tonic-gate 				 * setsockopt often fails with ENOPROTOOPT
1437c478bd9Sstevel@tonic-gate 				 * but socket() should fail with
1447c478bd9Sstevel@tonic-gate 				 * EPROTONOSUPPORT.
1457c478bd9Sstevel@tonic-gate 				 */
1467c478bd9Sstevel@tonic-gate 				errno = EPROTONOSUPPORT;
1477c478bd9Sstevel@tonic-gate 				return (-1);
1487c478bd9Sstevel@tonic-gate 			}
1497c478bd9Sstevel@tonic-gate 		}
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate 	return (fd);
1527c478bd9Sstevel@tonic-gate }
153