1 /* $OpenBSD: utiltest.c,v 1.2 2016/09/02 16:54:28 mikeb Exp $ */ 2 3 /* 4 * Copyright (c) 2001 Niklas Hallqvist. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/types.h> 28 #include <sys/socket.h> 29 #include <netinet/in.h> 30 #include <stdio.h> 31 32 #include "util.h" 33 34 int test_1 (char *, char *, int); 35 36 int 37 main (int argc, char *argv[]) 38 { 39 test_1 ("10.0.0.1", "10", 0); 40 test_1 ("10.0.0.1", "isakmp", 0); 41 test_1 ("10::1", "10", 0); 42 test_1 ("10::1", "isakmp", 0); 43 test_1 ("10.0x0.1", "10", -1); 44 test_1 ("10.0.0.1", "telnet", -1); 45 test_1 ("10::x:1", "10", -1); 46 test_1 ("10::1", "telnet", -1); 47 return 0; 48 } 49 50 int test_1 (char *address, char *port, int ok) 51 { 52 struct sockaddr *sa; 53 #ifdef DEBUG 54 struct sockaddr_in *sai; 55 struct sockaddr_in6 *sai6; 56 int i; 57 #endif 58 int rv; 59 60 printf ("test_1 (\"%s\", \"%s\") ", address, port); 61 rv = text2sockaddr (address, port, &sa, 0, 0) == ok; 62 printf (rv ? "OK" : "FAIL"); 63 printf ("\n"); 64 65 #ifdef DEBUG 66 printf ("af %d len %d ", sa->sa_family, sa->sa_len); 67 if (sa->sa_family == AF_INET) 68 { 69 sai = (struct sockaddr_in *)sa; 70 printf ("addr %08x port %d\n", ntohl (sai->sin_addr.s_addr), 71 ntohs (sai->sin_port)); 72 } 73 else 74 { 75 sai6 = (struct sockaddr_in6 *)sa; 76 printf ("addr "); 77 for (i = 0; i < sizeof sai6->sin6_addr; i++) 78 printf ("%02x", sai6->sin6_addr.s6_addr[i]); 79 printf (" port %d\n", ntohs (sai6->sin6_port)); 80 } 81 #endif 82 return rv; 83 } 84