1 #include "syshdrs.h"
2 
3 #ifndef SO_RCVBUF
4 int
5 GetSocketBufSize(int UNUSED(sockfd), size_t *const rsize, size_t *const ssize)
6 {
7 	LIBSIO_USE_VAR(sockfd);
8 	if (ssize != NULL)
9 		*ssize = 0;
10 	if (rsize != NULL)
11 		*rsize = 0;
12 	return (-1);
13 }	/* GetSocketBufSize */
14 #else
15 int
16 GetSocketBufSize(int sockfd, size_t *const rsize, size_t *const ssize)
17 {
18 	int rc = -1;
19 	int opt;
20 	int optsize;
21 
22 	if (ssize != NULL) {
23 		opt = 0;
24 		optsize = sizeof(opt);
25 		rc = getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, (char *) &opt, &optsize);
26 		if (rc == 0)
27 			*ssize = (size_t) opt;
28 		else
29 			*ssize = 0;
30 	}
31 	if (rsize != NULL) {
32 		opt = 0;
33 		optsize = sizeof(opt);
34 		rc = getsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, (char *) &opt, &optsize);
35 		if (rc == 0)
36 			*rsize = (size_t) opt;
37 		else
38 			*rsize = 0;
39 	}
40 	return (rc);
41 }	/* GetSocketBufSize */
42 #endif
43 
44 
45 
46 
47 #ifndef SO_RCVBUF
48 int
49 SetSocketBufSize(int UNUSED(sockfd), size_t UNUSED(rsize), size_t UNUSED(ssize))
50 {
51 	LIBSIO_USE_VAR(sockfd);
52 	LIBSIO_USE_VAR(rsize);
53 	LIBSIO_USE_VAR(ssize);
54 	return (-1);
55 }	/* SetSocketBufSize */
56 #else
57 int
58 SetSocketBufSize(int sockfd, size_t rsize, size_t ssize)
59 {
60 	int rc = -1;
61 	int opt;
62 	int optsize;
63 
64 	if (ssize > 0) {
65 		opt = (int) ssize;
66 		optsize = sizeof(opt);
67 		rc = setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, (const char *) &opt, optsize);
68 		if (rc < 0)
69 			return (rc);
70 	}
71 	if (rsize > 0) {
72 		opt = (int) rsize;
73 		optsize = sizeof(opt);
74 		rc = setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, (const char *) &opt, optsize);
75 		if (rc < 0)
76 			return (rc);
77 	}
78 	return (0);
79 }	/* SetSocketBufSize */
80 #endif
81 
82 
83 
84 
85 #ifndef TCP_NODELAY
86 int
87 GetSocketNagleAlgorithm(const int UNUSED(fd))
88 {
89 	LIBSIO_USE_VAR(fd);
90 	return (-1);
91 }	/* GetSocketNagleAlgorithm */
92 #else
93 int
94 GetSocketNagleAlgorithm(const int fd)
95 {
96 	int optsize;
97 	int opt;
98 
99 	opt = -2;
100 	optsize = (int) sizeof(opt);
101 	if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &opt, &optsize) < 0)
102 		return (-1);
103 	return (opt);
104 }	/* GetSocketNagleAlgorithm */
105 #endif	/* TCP_NODELAY */
106 
107 
108 
109 
110 
111 #ifndef TCP_NODELAY
112 int
113 SetSocketNagleAlgorithm(const int UNUSED(fd), const int UNUSED(onoff))
114 {
115 	LIBSIO_USE_VAR(fd);
116 	LIBSIO_USE_VAR(onoff);
117 	return (-1);
118 }	/* SetSocketNagleAlgorithm */
119 #else
120 int
121 SetSocketNagleAlgorithm(const int fd, const int onoff)
122 {
123 	int optsize;
124 	int opt;
125 
126 	opt = onoff;
127 	optsize = (int) sizeof(opt);
128 	return (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &opt, optsize));
129 }	/* SetSocketNagleAlgorithm */
130 #endif	/* TCP_NODELAY */
131 
132 
133 
134 #ifndef SO_LINGER
135 int
136 GetSocketLinger(const int UNUSED(fd), int *const UNUSED(lingertime))
137 {
138 	LIBSIO_USE_VAR(fd);
139 	LIBSIO_USE_VAR(lingertime);
140 	return (-1);
141 }	/* GetSocketLinger */
142 #else
143 int
144 GetSocketLinger(const int fd, int *const lingertime)
145 {
146 	int optsize;
147 	struct linger opt;
148 
149 	optsize = (int) sizeof(opt);
150 	opt.l_onoff = 0;
151 	opt.l_linger = 0;
152 	if (getsockopt(fd, SOL_SOCKET, SO_LINGER, (char *) &opt, &optsize) < 0)
153 		return (-1);
154 	if (lingertime != NULL)
155 		*lingertime = opt.l_linger;
156 	return (opt.l_onoff);
157 }	/* GetSocketLinger */
158 #endif	/* SO_LINGER */
159 
160 
161 
162 #ifndef SO_LINGER
163 int
164 SetSocketLinger(const int UNUSED(fd), const int UNUSED(l_onoff), const int UNUSED(l_linger))
165 {
166 	LIBSIO_USE_VAR(fd);
167 	LIBSIO_USE_VAR(l_onoff);
168 	LIBSIO_USE_VAR(l_linger);
169 	return (-1);
170 }	/* SetSocketLinger */
171 #else
172 int
173 SetSocketLinger(const int fd, const int l_onoff, const int l_linger)
174 {
175 	struct linger opt;
176 	int optsize;
177 /*
178  * From hpux:
179  *
180  * Structure used for manipulating linger option.
181  *
182  * if l_onoff == 0:
183  *    close(2) returns immediately; any buffered data is sent later
184  *    (default)
185  *
186  * if l_onoff != 0:
187  *    if l_linger == 0, close(2) returns after discarding any unsent data
188  *    if l_linger != 0, close(2) does not return until buffered data is sent
189  */
190 #if 0
191 struct	linger {
192 	int	l_onoff;		/* 0 = do not wait to send data */
193 					/* non-0 = see l_linger         */
194 	int	l_linger;		/* 0 = discard unsent data      */
195 					/* non-0 = wait to send data    */
196 };
197 #endif
198 	opt.l_onoff = l_onoff;
199 	opt.l_linger = l_linger;
200 	optsize = (int) sizeof(opt);
201 	return (setsockopt(fd, SOL_SOCKET, SO_LINGER, (char *) &opt, optsize));
202 }	/* SetSocketLinger */
203 #endif	/* SO_LINGER */
204