1135042Srwatson/*-
2135042Srwatson * Copyright (c) 2004 Robert N. M. Watson
3135042Srwatson * All rights reserved.
4135042Srwatson *
5135042Srwatson * Redistribution and use in source and binary forms, with or without
6135042Srwatson * modification, are permitted provided that the following conditions
7135042Srwatson * are met:
8135042Srwatson * 1. Redistributions of source code must retain the above copyright
9135042Srwatson *    notice, this list of conditions and the following disclaimer.
10135042Srwatson * 2. Redistributions in binary form must reproduce the above copyright
11135042Srwatson *    notice, this list of conditions and the following disclaimer in the
12135042Srwatson *    documentation and/or other materials provided with the distribution.
13135042Srwatson *
14135042Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15135042Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16135042Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17135042Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18135042Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19135042Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20135042Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21135042Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22135042Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23135042Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24135042Srwatson * SUCH DAMAGE.
25135042Srwatson *
26135042Srwatson * $FreeBSD$
27135042Srwatson */
28135042Srwatson
29135042Srwatson#include <sys/types.h>
30135042Srwatson#include <sys/socket.h>
31135042Srwatson#include <sys/time.h>
32135042Srwatson
33135042Srwatson#include <netinet/in.h>
34135042Srwatson
35135042Srwatson#include <arpa/inet.h>
36135042Srwatson
37135042Srwatson#include <stdio.h>
38135042Srwatson#include <stdlib.h>
39135042Srwatson#include <string.h>
40135042Srwatson
41135042Srwatsonstatic void
42135042Srwatsonusage(void)
43135042Srwatson{
44135042Srwatson
45135042Srwatson	fprintf(stderr, "netreceive [port]\n");
46135042Srwatson	exit(-1);
47135042Srwatson}
48135042Srwatson
49135042Srwatsonint
50135042Srwatsonmain(int argc, char *argv[])
51135042Srwatson{
52135042Srwatson	struct sockaddr_in sin;
53135042Srwatson	char *dummy, *packet;
54135042Srwatson	long port;
55135536Srwatson	int s, v;
56135042Srwatson
57135042Srwatson	if (argc != 2)
58135042Srwatson		usage();
59135042Srwatson
60135042Srwatson	bzero(&sin, sizeof(sin));
61135042Srwatson	sin.sin_len = sizeof(sin);
62135042Srwatson	sin.sin_family = AF_INET;
63135042Srwatson	sin.sin_addr.s_addr = htonl(INADDR_ANY);
64135042Srwatson
65135042Srwatson	port = strtoul(argv[1], &dummy, 10);
66135042Srwatson	if (port < 1 || port > 65535 || *dummy != '\0')
67135042Srwatson		usage();
68135042Srwatson	sin.sin_port = htons(port);
69135042Srwatson
70135042Srwatson	packet = malloc(65536);
71135042Srwatson	if (packet == NULL) {
72135042Srwatson		perror("malloc");
73135042Srwatson		return (-1);
74135042Srwatson	}
75135042Srwatson	bzero(packet, 65536);
76135042Srwatson
77135042Srwatson	s = socket(PF_INET, SOCK_DGRAM, 0);
78135042Srwatson	if (s == -1) {
79135042Srwatson		perror("socket");
80135042Srwatson		return (-1);
81135042Srwatson	}
82135042Srwatson
83135536Srwatson	v = 128 * 1024;
84135536Srwatson	if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &v, sizeof(v)) < 0) {
85135536Srwatson		perror("SO_RCVBUF");
86135536Srwatson		return (-1);
87135536Srwatson	}
88135536Srwatson
89135042Srwatson	if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
90135042Srwatson		perror("bind");
91135042Srwatson		return (-1);
92135042Srwatson	}
93135042Srwatson
94135042Srwatson	printf("netreceive listening on UDP port %d\n", (u_short)port);
95135042Srwatson
96135042Srwatson	while (1) {
97135042Srwatson		if (recv(s, packet, 65536, 0) < 0)
98135042Srwatson			perror("recv");
99135042Srwatson	}
100135042Srwatson}
101