xref: /titanic_50/usr/src/lib/libsocket/socket/socket.c (revision de3d2ce46fc25c7b67ccbae4afe5f15e5357568f)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <sys/stropts.h>
45 #include <sys/stream.h>
46 #include <sys/socketvar.h>
47 #include <errno.h>
48 #include <unistd.h>
49 #include <stdlib.h>
50 
51 extern int _so_socket();
52 extern int _s_netconfig_path();
53 extern int _setsockopt();
54 
55 int _socket_create(int, int, int, int);
56 
57 #pragma weak socket = _socket
58 
59 int
60 _socket(int family, int type, int protocol)
61 {
62 	return (_socket_create(family, type, protocol, SOV_DEFAULT));
63 }
64 
65 /*
66  * Used by the BCP library.
67  */
68 int
69 _socket_bsd(int family, int type, int protocol)
70 {
71 	return (_socket_create(family, type, protocol, SOV_SOCKBSD));
72 }
73 
74 int
75 _socket_svr4(int family, int type, int protocol)
76 {
77 	return (_socket_create(family, type, protocol, SOV_SOCKSTREAM));
78 }
79 
80 int
81 __xnet_socket(int family, int type, int protocol)
82 {
83 	return (_socket_create(family, type, protocol, SOV_XPG4_2));
84 }
85 
86 /*
87  * Create a socket endpoint for socket() and socketpair().
88  * In SunOS 4.X and in SunOS 5.X prior to XPG 4.2 the only error
89  * that could be returned due to invalid <family, type, protocol>
90  * was EPROTONOSUPPORT. (While the SunOS 4.X source contains EPROTOTYPE
91  * error as well that error can only be generated if the kernel is
92  * incorrectly configured.)
93  * For backwards compatibility only applications that request XPG 4.2
94  * (through c89 or XOPEN_SOURCE) will get EPROTOTYPE or EAFNOSUPPORT errors.
95  */
96 int
97 _socket_create(int family, int type, int protocol, int version)
98 {
99 	int fd;
100 
101 	/*
102 	 * Try creating without knowing the device assuming that
103 	 * the transport provider is registered in /etc/sock2path.
104 	 * If none found fall back to using /etc/netconfig to look
105 	 * up the name of the transport device name. This provides
106 	 * backwards compatibility for transport providers that have not
107 	 * yet been converted to using /etc/sock2path.
108 	 * XXX When all transport providers use /etc/sock2path this
109 	 * part of the code can be removed.
110 	 */
111 	fd = _so_socket(family, type, protocol, NULL, version);
112 	if (fd == -1) {
113 		char *devpath;
114 		int saved_errno = errno;
115 		int prototype = 0;
116 
117 		switch (saved_errno) {
118 		case EAFNOSUPPORT:
119 		case EPROTOTYPE:
120 			if (version != SOV_XPG4_2)
121 				saved_errno = EPROTONOSUPPORT;
122 			break;
123 		case EPROTONOSUPPORT:
124 			break;
125 
126 		default:
127 			errno = saved_errno;
128 			return (-1);
129 		}
130 		if (_s_netconfig_path(family, type, protocol,
131 					&devpath, &prototype) == -1) {
132 			errno = saved_errno;
133 			return (-1);
134 		}
135 		fd = _so_socket(family, type, protocol, devpath, version);
136 		free(devpath);
137 		if (fd == -1) {
138 			errno = saved_errno;
139 			return (-1);
140 		}
141 		if (prototype != 0) {
142 			if (_setsockopt(fd, SOL_SOCKET, SO_PROTOTYPE,
143 			    (caddr_t)&prototype, (int)sizeof (prototype)) < 0) {
144 				(void) close(fd);
145 				/*
146 				 * setsockopt often fails with ENOPROTOOPT
147 				 * but socket() should fail with
148 				 * EPROTONOSUPPORT.
149 				 */
150 				errno = EPROTONOSUPPORT;
151 				return (-1);
152 			}
153 		}
154 	}
155 	return (fd);
156 }
157