vfs: check userland buffers before reading them.
[haiku.git] / src / tests / kits / net / sock / sinkudp.c
blob3a45060f8fdef46be12cbd326782e2b7c819e06e
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_udp(int sockfd) /* TODO: use recvfrom ?? */
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)" : "");
44 if (verbose > 1) {
45 fprintf(stderr, "printing %d bytes\n", n);
46 rbuf[n] = 0; /* make certain it's null terminated */
47 fprintf(stderr, "SDAP header: %lx\n", *((long *) rbuf));
48 fprintf(stderr, "next long: %lx\n", *((long *) rbuf+4));
49 fputs(&rbuf[8], stderr);
53 if (pauserw)
54 sleep_us(pauserw*1000);
56 if (flags != 0) {
57 flags = 0; /* avoid infinite loop */
58 goto oncemore; /* read the message again */
62 if (pauseclose) {
63 if (verbose)
64 fprintf(stderr, "pausing before close\n");
65 sleep_us(pauseclose*1000);
68 if (close(sockfd) < 0)
69 err_sys("close error");