xref: /titanic_44/usr/src/lib/libbc/libc/sys/common/setsockopt.c (revision f6db9f272f0061301cfaa1c0001b7d636eae31f4)
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 1989 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <errno.h>
32 
33 /* multicast setsockopts */
34 #define	SUNOS4X_IP_MULTICAST_IF		2
35 #define	SUNOS4X_IP_MULTICAST_TTL	3
36 #define	SUNOS4X_IP_MULTICAST_LOOP	4
37 #define	SUNOS4X_IP_ADD_MEMBERSHIP	5
38 #define	SUNOS4X_IP_DROP_MEMBERSHIP	6
39 #define	SUNOS5X_IP_MULTICAST_IF		0x10
40 #define	SUNOS5X_IP_MULTICAST_TTL	0x11
41 #define	SUNOS5X_IP_MULTICAST_LOOP	0x12
42 #define	SUNOS5X_IP_ADD_MEMBERSHIP	0x13
43 #define	SUNOS5X_IP_DROP_MEMBERSHIP	0x14
44 
45 
46 int
47 setsockopt(int s, int level, int optname, char *optval, int optlen)
48 {
49 	int	a;
50 
51 	if (level == SOL_SOCKET)
52 		switch (optname) {
53 		case SO_DONTLINGER: {
54 			struct linger ling;
55 			ling.l_onoff = 0;
56 			if ((a = _setsockopt(s, level, SO_LINGER, &ling,
57 			    sizeof (struct linger))) == -1)
58 				maperror(errno);
59 			return (a);
60 		}
61 
62 		case SO_LINGER:
63 			if  (optlen == sizeof (int)) {
64 				struct linger ling;
65 				ling.l_onoff = 1;
66 				ling.l_linger = (int)*optval;
67 				if ((a = _setsockopt(s, level, SO_LINGER, &ling,
68 				    sizeof (struct linger))) == -1)
69 					maperror(errno);
70 				return (a);
71 			}
72 		case SO_DEBUG:
73 		case SO_KEEPALIVE:
74 		case SO_DONTROUTE:
75 		case SO_USELOOPBACK:
76 		case SO_REUSEADDR:
77 			if (!optval) {
78 				int val = 1;
79 				if ((a = _setsockopt(s, level, optname, &val,
80 				    sizeof (int))) == -1)
81 					maperror(errno);
82 				return (a);
83 			}
84 		}
85 	if (level == IPPROTO_IP)
86 		switch (optname) {
87 		case SUNOS4X_IP_MULTICAST_IF:
88 			optname = SUNOS5X_IP_MULTICAST_IF;
89 			break;
90 
91 		case SUNOS4X_IP_MULTICAST_TTL:
92 			optname = SUNOS5X_IP_MULTICAST_TTL;
93 			break;
94 
95 		case SUNOS4X_IP_MULTICAST_LOOP:
96 			optname = SUNOS5X_IP_MULTICAST_LOOP;
97 			break;
98 
99 		case SUNOS4X_IP_ADD_MEMBERSHIP:
100 			optname = SUNOS5X_IP_ADD_MEMBERSHIP;
101 			break;
102 
103 		case SUNOS4X_IP_DROP_MEMBERSHIP:
104 			optname = SUNOS5X_IP_DROP_MEMBERSHIP;
105 			break;
106 		}
107 
108 	if ((a = _setsockopt(s, level, optname, optval, optlen)) == -1)
109 		maperror(errno);
110 	return (a);
111 }
112