natt_terminator.c revision 1.1
11.1Sozaki/*	$NetBSD: natt_terminator.c,v 1.1 2017/10/30 15:59:23 ozaki-r Exp $	*/
21.1Sozaki
31.1Sozaki/*-
41.1Sozaki * Copyright (c) 2017 Internet Initiative Japan Inc.
51.1Sozaki * All rights reserved.
61.1Sozaki *
71.1Sozaki * Redistribution and use in source and binary forms, with or without
81.1Sozaki * modification, are permitted provided that the following conditions
91.1Sozaki * are met:
101.1Sozaki * 1. Redistributions of source code must retain the above copyright
111.1Sozaki *    notice, this list of conditions and the following disclaimer.
121.1Sozaki * 2. Redistributions in binary form must reproduce the above copyright
131.1Sozaki *    notice, this list of conditions and the following disclaimer in the
141.1Sozaki *    documentation and/or other materials provided with the distribution.
151.1Sozaki *
161.1Sozaki * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
171.1Sozaki * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
181.1Sozaki * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
191.1Sozaki * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
201.1Sozaki * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
211.1Sozaki * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
221.1Sozaki * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
231.1Sozaki * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
241.1Sozaki * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
251.1Sozaki * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
261.1Sozaki * POSSIBILITY OF SUCH DAMAGE.
271.1Sozaki */
281.1Sozaki
291.1Sozaki#include <sys/types.h>
301.1Sozaki#include <sys/socket.h>
311.1Sozaki#include <sys/wait.h>
321.1Sozaki#include <sys/time.h>
331.1Sozaki
341.1Sozaki#include <netinet/in.h>
351.1Sozaki#include <netinet/udp.h>
361.1Sozaki
371.1Sozaki#include <stdio.h>
381.1Sozaki#include <err.h>
391.1Sozaki#include <netdb.h>
401.1Sozaki#include <string.h>
411.1Sozaki#include <stdlib.h>
421.1Sozaki#include <unistd.h>
431.1Sozaki
441.1Sozakiint
451.1Sozakimain(int argc, char **argv)
461.1Sozaki{
471.1Sozaki	struct addrinfo hints;
481.1Sozaki	struct addrinfo *res;
491.1Sozaki	int s, e;
501.1Sozaki	const char *addr, *port;
511.1Sozaki	int option;
521.1Sozaki
531.1Sozaki	if (argc != 3) {
541.1Sozaki		fprintf(stderr, "Usage: %s <addr> <port>\n", argv[0]);
551.1Sozaki		return 1;
561.1Sozaki	}
571.1Sozaki
581.1Sozaki	addr = argv[1];
591.1Sozaki	port = argv[2];
601.1Sozaki
611.1Sozaki	memset(&hints, 0, sizeof(hints));
621.1Sozaki	hints.ai_family = AF_INET;
631.1Sozaki	hints.ai_socktype = SOCK_DGRAM;
641.1Sozaki	hints.ai_protocol = IPPROTO_UDP;
651.1Sozaki	hints.ai_flags = 0;
661.1Sozaki
671.1Sozaki	e = getaddrinfo(addr, port, &hints, &res);
681.1Sozaki	if (e != 0)
691.1Sozaki		errx(EXIT_FAILURE, "getaddrinfo failed: %s", gai_strerror(e));
701.1Sozaki
711.1Sozaki	s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
721.1Sozaki	if (s == -1)
731.1Sozaki		err(EXIT_FAILURE, "socket");
741.1Sozaki
751.1Sozaki	/*
761.1Sozaki	 * Set the option to tell the kernel that the socket can handle
771.1Sozaki	 * UDP-encapsulated ESP packets for NAT-T.
781.1Sozaki	 */
791.1Sozaki	option = UDP_ENCAP_ESPINUDP;
801.1Sozaki	e = setsockopt(s, IPPROTO_UDP, UDP_ENCAP, &option, sizeof(option));
811.1Sozaki	if (e == -1)
821.1Sozaki		err(EXIT_FAILURE, "setsockopt(UDP_ENCAP)");
831.1Sozaki
841.1Sozaki	e = bind(s, res->ai_addr, res->ai_addrlen);
851.1Sozaki	if (e == -1)
861.1Sozaki		err(EXIT_FAILURE, "bind");
871.1Sozaki
881.1Sozaki	/* Receiving a packet make the NAPT create a mapping. */
891.1Sozaki	{
901.1Sozaki		char buf[64];
911.1Sozaki		struct sockaddr_storage z;
921.1Sozaki		socklen_t len = sizeof(z);
931.1Sozaki
941.1Sozaki		e = recvfrom(s, buf, 64, MSG_PEEK,
951.1Sozaki		    (struct sockaddr *)&z, &len);
961.1Sozaki		if (e == -1)
971.1Sozaki			err(EXIT_FAILURE, "recvfrom");
981.1Sozaki	}
991.1Sozaki
1001.1Sozaki	/*
1011.1Sozaki	 * Keep the socket in the kernel to handle UDP-encapsulated ESP packets.
1021.1Sozaki	 */
1031.1Sozaki	pause();
1041.1Sozaki
1051.1Sozaki	close(s);
1061.1Sozaki
1071.1Sozaki	return 0;
1081.1Sozaki}
109