vfs: check userland buffers before reading them.
[haiku.git] / src / tests / kits / net / sock / buffers.c
blobec56d993af78d91ee84543e4abb09ce01b862fbf
1 /* -*- c-basic-offset: 8; -*-
3 * Copyright (c) 1993 W. Richard Stevens. All rights reserved.
4 * Permission to use or modify this software and its documentation only for
5 * educational purposes and without fee is hereby granted, provided that
6 * the above copyright notice appear in all copies. The author makes no
7 * representations about the suitability of this software for any purpose.
8 * It is provided "as is" without express or implied warranty.
9 */
11 #include <sock.h>
13 void buffers(int sockfd)
15 int n;
16 socklen_t optlen;
18 /* Allocate the read and write buffers. */
20 if (rbuf == NULL) {
21 if ( (rbuf = malloc(readlen)) == NULL)
22 err_sys("malloc error for read buffer");
25 if (wbuf == NULL) {
26 if ( (wbuf = malloc(writelen)) == NULL)
27 err_sys("malloc error for write buffer");
30 /* Set the socket send and receive buffer sizes (if specified).
31 The receive buffer size is tied to TCP's advertised window. */
33 if (rcvbuflen) {
34 if (setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &rcvbuflen,
35 sizeof(rcvbuflen)) < 0)
36 err_sys("SO_RCVBUF setsockopt error");
38 optlen = sizeof(n);
39 if (getsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &n, &optlen) < 0)
40 err_sys("SO_RCVBUF getsockopt error");
41 if (n != rcvbuflen)
42 err_quit("error: requested rcvbuflen = %d, resulting SO_RCVBUF = %d", rcvbuflen, n);
43 if (verbose)
44 fprintf(stderr, "SO_RCVBUF = %d\n", n);
47 if (sndbuflen) {
48 if (setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &sndbuflen,
49 sizeof(sndbuflen)) < 0)
50 err_sys("SO_SNDBUF setsockopt error");
52 optlen = sizeof(n);
53 if (getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &n, &optlen) < 0)
54 err_sys("SO_SNDBUF getsockopt error");
55 if (n != sndbuflen)
56 err_quit("error: requested sndbuflen = %d, resulting SO_SNDBUF = %d", sndbuflen, n);
57 if (verbose)
58 fprintf(stderr, "SO_SNDBUF = %d\n", n);