vfs: check userland buffers before reading them.
[haiku.git] / src / tests / kits / net / sock / sourceudp.c
blob08c1e14398525ee4b1309b1a2aa80f0ae985dbb0
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 */
10 #include <sys/types.h>
11 #include <sys/socket.h>
14 #include <stdio.h>
15 #include "sock.h"
17 void
18 source_udp(int sockfd) /* TODO: use sendto ?? */
20 int i, n, option;
21 socklen_t optlen;
23 pattern(wbuf, writelen); /* fill send buffer with a pattern */
25 if (pauseinit)
26 sleep_us(pauseinit*1000);
28 for (i = 1; i <= nbuf; i++) {
29 if (connectudp) {
30 if ( (n = write(sockfd, wbuf, writelen)) != writelen) {
31 if (ignorewerr) {
32 err_ret("write returned %d, expected %d", n, writelen);
33 /* also call getsockopt() to clear so_error */
34 optlen = sizeof(option);
35 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
36 &option, &optlen) < 0)
37 err_sys("SO_ERROR getsockopt error");
38 } else
39 err_sys("write returned %d, expected %d", n, writelen);
41 } else {
42 if ( (n = sendto(sockfd, wbuf, writelen, 0,
43 (struct sockaddr *) &servaddr,
44 sizeof(struct sockaddr))) != writelen) {
45 if (ignorewerr) {
46 err_ret("sendto returned %d, expected %d", n, writelen);
47 /* also call getsockopt() to clear so_error */
48 optlen = sizeof(option);
49 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
50 &option, &optlen) < 0)
51 err_sys("SO_ERROR getsockopt error");
52 } else
53 err_sys("sendto returned %d, expected %d", n, writelen);
57 if (verbose)
58 fprintf(stderr, "wrote %d bytes\n", n);
60 if (pauserw)
61 sleep_us(pauserw*1000);
64 if (pauseclose) {
65 if (verbose)
66 fprintf(stderr, "pausing before close\n");
67 sleep_us(pauseclose*1000);
70 if (close(sockfd) < 0)
71 err_sys("close error"); /* since SO_LINGER may be set */