/*-
 * Copyright (c) 2008 Robert N. M. Watson
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#include <sys/types.h>
#include <sys/socket.h>

#include "/usr/home/robert/freebsd/svncommit/base/head/sys/netinet/in.h"

#include <err.h>
#include <poll.h>
#include <stdio.h>
#include <string.h>

#define	SOCK_ARRAY_LEN	8

#define	BUFLEN	65536
int
main(int argc, char *argv[])
{
	int i, optval, ret, sock_array[SOCK_ARRAY_LEN];
	struct pollfd pollfd[SOCK_ARRAY_LEN];
	struct sockaddr_in sin;
	char buffer[BUFLEN];
	struct ip_subset is;
	ssize_t buflen;

	bzero(&sin, sizeof(sin));
	sin.sin_family = AF_INET;
	sin.sin_len = sizeof(sin);
	sin.sin_addr.s_addr = htonl(INADDR_ANY);
	sin.sin_port = htons(5000);

	bzero(&is, sizeof(is));
	is.is_strategy = IP_SUBSET_STRATEGY_RANDOM;
	is.is_strategy = IP_SUBSET_STRATEGY_FLOW;
	is.is_count = SOCK_ARRAY_LEN;

	optval = 1;

	for (i = 0; i < SOCK_ARRAY_LEN; i++) {

		sock_array[i] = socket(PF_INET, SOCK_DGRAM, 0);
		if (sock_array[i] < 0)
			err(-1, "socket %d", i);
		if (setsockopt(sock_array[i], SOL_SOCKET, SO_REUSEPORT,
		    &optval, sizeof(optval)) < 0)
			err(-1, "socket %d setsockopt SO_REUSEPORT", i);
		if (bind(sock_array[i], (struct sockaddr *)&sin, sizeof(sin))
		    < 0)
			err(-1, "socket %d bind", i);
		is.is_member = i;
		if (setsockopt(sock_array[i], IPPROTO_UDP, IP_SUBSET, &is,
		    sizeof(is)) < 0)
			err(-1, "socket %d setsockopt IP_SUBSET", i);
	}
	while (1) {
		bzero(&pollfd, sizeof(pollfd));
		for (i = 0; i < SOCK_ARRAY_LEN; i++) {
			pollfd[i].fd = sock_array[i];
			pollfd[i].events = POLLIN;
			pollfd[i].revents = 0;
		}
		ret = poll(pollfd, SOCK_ARRAY_LEN, INFTIM);
		if (ret < 0) {
			warn("poll");
			continue;
		}
		printf("poll returns %d\n", ret);
		for (i = 0; i < SOCK_ARRAY_LEN; i++) {
			printf("pollfd index %d fd %d revents %d\n", i,
			    pollfd[i].fd, pollfd[i].revents);
			if (pollfd[i].revents & POLLIN) {
				buflen = recv(pollfd[i].fd, buffer, BUFLEN,
				    0);
				if (buflen < 0) {
					warn("recv");
					continue;
				}
			}
		}
	}
	return (0);
}

