/* * Copyright (c) 2003 Research Engineering Development, Inc. * Author: Alfred Perlstein * 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. * * $Id: test.c,v 1.3 2003/05/07 05:01:37 bright Exp $ * */ #include #include #include #include #include #include "mtrie.h" int get_ip(const char *prompt, uint32_t *ip); int getyesno(const char *prompt); int get_ip(const char *prompt, uint32_t *ip) { uint32_t a, b, c, d; int x; printf("%s:\n", prompt); x = scanf("%u.%u.%u.%u", &a, &b, &c, &d); if (x != 4) return (-1); *ip = (a << 24) | (b << 16) | (c << 8) | d; return (0); } int getyesno(const char *prompt) { int ch; for ( ;; ) { fpurge(stdin); printf("%s?\n", prompt); ch = toupper(getchar()); switch (ch) { case 'N': case EOF: return (0); case 'Y': return (1); default: printf( "'%c' is not a valid answer, enter 'Y' or 'N'\n", ch); continue; } } } int main(void) { mtrie_t m; uint32_t ip; void *data; int val = 1; if (mtrie_alloc(&m, 0) == -1) err(1, "mtrie_alloc"); do { int mask; get_ip("enter ip to add", &ip); printf("enter mask:\n"); scanf("%d", &mask); if (mtrie_insert(m, ip, mask, (void *)val++) == -1) err(1, "mtrie_insert"); } while (getyesno("enter another")); do { get_ip("enter ip to lookup", &ip); if (mtrie_lookup(m, ip, &data) == -1) printf("not found\n"); else printf("found value was the #%d network entered\n", (int)data); } while (getyesno("find another")); mtrie_free(m); return (0); }