xref: /openbsd-src/regress/sbin/isakmpd/util/utiltest.c (revision e8bc5a8a8f952f79708916e59947f26e026484f0)
1*e8bc5a8aSmikeb /*	$OpenBSD: utiltest.c,v 1.2 2016/09/02 16:54:28 mikeb Exp $	*/
271ec8d3aScloder 
371ec8d3aScloder /*
471ec8d3aScloder  * Copyright (c) 2001 Niklas Hallqvist.  All rights reserved.
571ec8d3aScloder  *
671ec8d3aScloder  * Redistribution and use in source and binary forms, with or without
771ec8d3aScloder  * modification, are permitted provided that the following conditions
871ec8d3aScloder  * are met:
971ec8d3aScloder  * 1. Redistributions of source code must retain the above copyright
1071ec8d3aScloder  *    notice, this list of conditions and the following disclaimer.
1171ec8d3aScloder  * 2. Redistributions in binary form must reproduce the above copyright
1271ec8d3aScloder  *    notice, this list of conditions and the following disclaimer in the
1371ec8d3aScloder  *    documentation and/or other materials provided with the distribution.
1471ec8d3aScloder  *
1571ec8d3aScloder  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1671ec8d3aScloder  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1771ec8d3aScloder  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1871ec8d3aScloder  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1971ec8d3aScloder  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2071ec8d3aScloder  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2171ec8d3aScloder  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2271ec8d3aScloder  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2371ec8d3aScloder  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2471ec8d3aScloder  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2571ec8d3aScloder  */
2671ec8d3aScloder 
2771ec8d3aScloder #include <sys/types.h>
2871ec8d3aScloder #include <sys/socket.h>
2971ec8d3aScloder #include <netinet/in.h>
3071ec8d3aScloder #include <stdio.h>
3171ec8d3aScloder 
3271ec8d3aScloder #include "util.h"
3371ec8d3aScloder 
3471ec8d3aScloder int test_1 (char *, char *, int);
3571ec8d3aScloder 
3671ec8d3aScloder int
main(int argc,char * argv[])3771ec8d3aScloder main (int argc, char *argv[])
3871ec8d3aScloder {
3971ec8d3aScloder   test_1 ("10.0.0.1", "10", 0);
4071ec8d3aScloder   test_1 ("10.0.0.1", "isakmp", 0);
4171ec8d3aScloder   test_1 ("10::1", "10", 0);
4271ec8d3aScloder   test_1 ("10::1", "isakmp", 0);
4371ec8d3aScloder   test_1 ("10.0x0.1", "10", -1);
4471ec8d3aScloder   test_1 ("10.0.0.1", "telnet", -1);
4571ec8d3aScloder   test_1 ("10::x:1", "10", -1);
4671ec8d3aScloder   test_1 ("10::1", "telnet", -1);
4771ec8d3aScloder   return 0;
4871ec8d3aScloder }
4971ec8d3aScloder 
test_1(char * address,char * port,int ok)5071ec8d3aScloder int test_1 (char *address, char *port, int ok)
5171ec8d3aScloder {
5271ec8d3aScloder   struct sockaddr *sa;
5371ec8d3aScloder #ifdef DEBUG
5471ec8d3aScloder   struct sockaddr_in *sai;
5571ec8d3aScloder   struct sockaddr_in6 *sai6;
5671ec8d3aScloder   int i;
5771ec8d3aScloder #endif
5871ec8d3aScloder   int rv;
5971ec8d3aScloder 
6071ec8d3aScloder   printf ("test_1 (\"%s\", \"%s\") ", address, port);
6171ec8d3aScloder   rv = text2sockaddr (address, port, &sa, 0, 0) == ok;
6271ec8d3aScloder   printf (rv ? "OK" : "FAIL");
6371ec8d3aScloder   printf ("\n");
6471ec8d3aScloder 
6571ec8d3aScloder #ifdef DEBUG
6671ec8d3aScloder   printf ("af %d len %d ", sa->sa_family, sa->sa_len);
6771ec8d3aScloder   if (sa->sa_family == AF_INET)
6871ec8d3aScloder     {
6971ec8d3aScloder       sai = (struct sockaddr_in *)sa;
7071ec8d3aScloder       printf ("addr %08x port %d\n", ntohl (sai->sin_addr.s_addr),
7171ec8d3aScloder 	      ntohs (sai->sin_port));
7271ec8d3aScloder     }
7371ec8d3aScloder   else
7471ec8d3aScloder     {
7571ec8d3aScloder       sai6 = (struct sockaddr_in6 *)sa;
7671ec8d3aScloder       printf ("addr ");
7771ec8d3aScloder       for (i = 0; i < sizeof sai6->sin6_addr; i++)
7871ec8d3aScloder 	printf ("%02x", sai6->sin6_addr.s6_addr[i]);
7971ec8d3aScloder       printf (" port %d\n", ntohs (sai6->sin6_port));
8071ec8d3aScloder     }
8171ec8d3aScloder #endif
8271ec8d3aScloder   return rv;
8371ec8d3aScloder }
84