1 /* $OpenBSD: pf_print_test.c,v 1.1 2016/08/24 22:31:41 bluhm Exp $ */ 2 3 /* 4 * Copyright (c) 2008, 2013 Alexander Bluhm <bluhm@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/types.h> 20 #include <sys/socket.h> 21 #include <sys/wait.h> 22 23 #include <net/if.h> 24 #include <net/route.h> 25 #include <netinet/in.h> 26 #include <arpa/inet.h> 27 28 #include <net/pfvar.h> 29 30 #include <err.h> 31 #include <stdio.h> 32 #include <stdlib.h> 33 #include <string.h> 34 #include <unistd.h> 35 36 #define INET 1 37 #define INET6 1 38 39 void pf_print_host(struct pf_addr *, u_int16_t, u_int8_t); 40 #define addlog printf 41 42 char *ipv6_addrs[] = { 43 "::", 44 "::1", 45 "1::", 46 "1::1", 47 "0:1::1:0", 48 "::1:2:0", 49 "0:1::", 50 "::1:0:0:0", 51 "1:2:3:4:5:6:7:8", 52 "0:2:3:4:5:6:7:8", 53 "1:2:3:4:5:6:7:0", 54 "1:0:3:0:5:0:7:8", 55 "::3:4:5:6:7:8", 56 "1:2:3:4:5:6::", 57 "0:2:3::6:7:8", 58 "1:2:0:4:5::8", 59 "1::4:5:0:0:8", 60 "1::5:0:0:8", 61 "1:0:0:4::8", 62 "::4:5:6:0:0", 63 "0:0:3:4:5::", 64 "::4:5:0:0:0", 65 "1234:5678:90ab:cdef:1234:5678:90ab:cdef", 66 NULL 67 }; 68 69 int 70 main(int argc, char *argv[]) 71 { 72 char str[100]; 73 struct pf_addr addr; 74 FILE *fpipe; 75 char **in, *out; 76 size_t len; 77 pid_t pid; 78 int fds[2]; 79 int status, ret = 0; 80 81 for (in = ipv6_addrs; *in; in++) { 82 if (!inet_pton(AF_INET6, *in, &addr.v6)) 83 errx(2, "inet_pton %s", *in); 84 if (!inet_ntop(AF_INET6, &addr.v6, str, sizeof(str))) 85 errx(2, "inet_ntop %s", *in); 86 if (strcmp(*in, str) != 0) { 87 warnx("not equal\nin:\t%s\nstr:\t%s", *in, str); 88 ret = 2; 89 } 90 if (pipe(fds) == -1) 91 err(2, "pipe"); 92 if ((pid = fork()) == -1) 93 err(2, "fork"); 94 if (pid == 0) { 95 close(fds[0]); 96 if (dup2(fds[1], 1) == -1) 97 err(2, "dup2"); 98 close(fds[1]); 99 pf_print_host(&addr, 0, AF_INET6); 100 fflush(stdout); 101 _exit(0); 102 } 103 close(fds[1]); 104 if ((fpipe = fdopen(fds[0], "r")) == NULL) 105 err(2, "fdopen"); 106 if ((out = fgetln(fpipe, &len)) == NULL) 107 err(2, "fgetln"); 108 if (out[len - 1] == '\n') 109 out[len - 1] = '\0'; 110 else { 111 char *tmp; 112 /* EOF without EOL, copy and add the NUL */ 113 if ((tmp = malloc(len + 1)) == NULL) 114 err(2, "malloc"); 115 memcpy(tmp, out, len); 116 tmp[len] = '\0'; 117 out = tmp; 118 } 119 if (fclose(fpipe) == EOF) 120 err(2, "fclose"); 121 if (wait(&status) <= 0) 122 err(2, "wait"); 123 if (status != 0) 124 errx(2, "child exit status: %d", status); 125 if (strcmp(*in, out) != 0) { 126 warnx("not equal\nin:\t%s\nout:\t%s", *in, out); 127 ret = 1; 128 } 129 } 130 return (ret); 131 } 132 133 #include "pf_print_host.c" 134