forked from unpbook/unpv13e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffers.c
60 lines (50 loc) · 1.74 KB
/
buffers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* Copyright (c) 1993 W. Richard Stevens. All rights reserved.
* Permission to use or modify this software and its documentation only for
* educational purposes and without fee is hereby granted, provided that
* the above copyright notice appear in all copies. The author makes no
* representations about the suitability of this software for any purpose.
* It is provided "as is" without express or implied warranty.
*/
#include "sock.h"
void
buffers(int sockfd)
{
int n;
socklen_t optlen;
/* Allocate the read and write buffers. */
if (rbuf == NULL) {
if ( (rbuf = malloc(readlen)) == NULL)
err_sys("malloc error for read buffer");
}
if (wbuf == NULL) {
if ( (wbuf = malloc(writelen)) == NULL)
err_sys("malloc error for write buffer");
}
/* Set the socket send and receive buffer sizes (if specified).
The receive buffer size is tied to TCP's advertised window. */
if (rcvbuflen) {
if (setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &rcvbuflen,
sizeof(rcvbuflen)) < 0)
err_sys("SO_RCVBUF setsockopt error");
optlen = sizeof(n);
if (getsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &n, &optlen) < 0)
err_sys("SO_RCVBUF getsockopt error");
if (n != rcvbuflen)
err_quit("rcvbuflen = %d, SO_RCVBUF = %d", rcvbuflen, n);
if (verbose)
fprintf(stderr, "SO_RCVBUF = %d\n", n);
}
if (sndbuflen) {
if (setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &sndbuflen,
sizeof(sndbuflen)) < 0)
err_sys("SO_SNDBUF setsockopt error");
optlen = sizeof(n);
if (getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &n, &optlen) < 0)
err_sys("SO_SNDBUF getsockopt error");
if (n != sndbuflen)
err_quit("sndbuflen = %d, SO_SNDBUF = %d", sndbuflen, n);
if (verbose)
fprintf(stderr, "SO_SNDBUF = %d\n", n);
}
}