ReactOS 0.4.15-dev-7924-g5949c20
SocketUtil.c
Go to the documentation of this file.
1#include "syshdrs.h"
2
3#ifndef SO_RCVBUF
4int
5GetSocketBufSize(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
15int
16GetSocketBufSize(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
48int
49SetSocketBufSize(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
57int
58SetSocketBufSize(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
86int
88{
90 return (-1);
91} /* GetSocketNagleAlgorithm */
92#else
93int
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
112int
114{
117 return (-1);
118} /* SetSocketNagleAlgorithm */
119#else
120int
121SetSocketNagleAlgorithm(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
135int
136GetSocketLinger(const int UNUSED(fd), int *const UNUSED(lingertime))
137{
139 LIBSIO_USE_VAR(lingertime);
140 return (-1);
141} /* GetSocketLinger */
142#else
143int
144GetSocketLinger(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
163int
164SetSocketLinger(const int UNUSED(fd), const int UNUSED(l_onoff), const int UNUSED(l_linger))
165{
169 return (-1);
170} /* SetSocketLinger */
171#else
172int
173SetSocketLinger(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
191struct 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 */
int GetSocketNagleAlgorithm(const int UNUSED(fd))
Definition: SocketUtil.c:87
int GetSocketLinger(const int UNUSED(fd), int *const UNUSED(lingertime))
Definition: SocketUtil.c:136
int SetSocketBufSize(int UNUSED(sockfd), size_t UNUSED(rsize), size_t UNUSED(ssize))
Definition: SocketUtil.c:49
int SetSocketNagleAlgorithm(const int UNUSED(fd), const int UNUSED(onoff))
Definition: SocketUtil.c:113
int GetSocketBufSize(int UNUSED(sockfd), size_t *const rsize, size_t *const ssize)
Definition: SocketUtil.c:5
int SetSocketLinger(const int UNUSED(fd), const int UNUSED(l_onoff), const int UNUSED(l_linger))
Definition: SocketUtil.c:164
static const char * onoff(int bool)
Definition: cmds.c:777
#define UNUSED(x)
Definition: btrfs_drv.h:82
#define NULL
Definition: types.h:112
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31
#define IPPROTO_TCP
Definition: ip.h:196
__kernel_size_t size_t
Definition: linux.h:237
#define LIBSIO_USE_VAR(a)
Definition: sio.h:140
static int fd
Definition: io.c:51
INT WSAAPI setsockopt(IN SOCKET s, IN INT level, IN INT optname, IN CONST CHAR FAR *optval, IN INT optlen)
Definition: sockctrl.c:421
INT WSAAPI getsockopt(IN SOCKET s, IN INT level, IN INT optname, OUT CHAR FAR *optval, IN OUT INT FAR *optlen)
Definition: sockctrl.c:271
u_short l_onoff
Definition: winsock.h:142
u_short l_linger
Definition: winsock.h:143
#define TCP_NODELAY
Definition: tcpdef.h:117
#define SO_RCVBUF
Definition: winsock.h:189
#define SO_LINGER
Definition: winsock.h:185
#define SOL_SOCKET
Definition: winsock.h:398
#define SO_SNDBUF
Definition: winsock.h:188