vfs: check userland buffers before reading them.
[haiku.git] / src / tests / kits / net / sock / sinktcp.c
blob957da48d0fdf3541690557580bcbca6b5483ed8d
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 <stdio.h>
12 #include "sock.h"
14 void
15 sink_tcp(int sockfd)
17 int n, flags;
19 if (pauseinit)
20 sleep_us(pauseinit*1000);
22 for ( ; ; ) { /* read until peer closes connection; -n opt ignored */
23 /* msgpeek = 0 or MSG_PEEK */
24 flags = msgpeek;
25 oncemore:
26 if ( (n = recv(sockfd, rbuf, readlen, flags)) < 0) {
27 err_sys("recv error");
29 } else if (n == 0) {
30 if (verbose)
31 fprintf(stderr, "connection closed by peer\n");
32 break;
34 #ifdef notdef /* following not possible with TCP */
35 } else if (n != readlen)
36 err_quit("read returned %d, expected %d", n, readlen);
37 #else
39 #endif
41 if (verbose)
42 fprintf(stderr, "received %d bytes%s\n", n,
43 (flags == MSG_PEEK) ? " (MSG_PEEK)" : "");
45 if (pauserw)
46 sleep_us(pauserw*1000);
48 if (flags != 0) {
49 flags = 0; /* no infinite loop */
50 goto oncemore; /* read the message again */
54 if (pauseclose) { /* pausing here puts peer into FIN_WAIT_2 */
55 if (verbose)
56 fprintf(stderr, "pausing before close\n");
57 sleep_us(pauseclose*1000);
60 if (close(sockfd) < 0)
61 err_sys("close error"); /* since SO_LINGER may be set */