1/* -*- c-basic-offset: 8; -*-
2 *
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 */
10
11#include <stdio.h>
12#include	"sock.h"
13
14void
15sink_udp(int sockfd)	/* TODO: use recvfrom ?? */
16{
17	int n, flags;
18
19	if (pauseinit)
20		sleep_us(pauseinit*1000);
21
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");
28
29		} else if (n == 0) {
30			if (verbose)
31				fprintf(stderr, "connection closed by peer\n");
32			break;
33
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
38	}
39#endif
40
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);
50		}
51	}
52
53	if (pauserw)
54		sleep_us(pauserw*1000);
55
56	if (flags != 0) {
57		flags = 0;		/* avoid infinite loop */
58		goto oncemore;	/* read the message again */
59	}
60}
61
62if (pauseclose) {
63	if (verbose)
64		fprintf(stderr, "pausing before close\n");
65	sleep_us(pauseclose*1000);
66 }
67
68if (close(sockfd) < 0)
69	err_sys("close error");
70}
71