00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "config.h"
00010
00011 #include <sys/types.h>
00012
00013 #include <err.h>
00014 #include <errno.h>
00015 #include <stdio.h>
00016 #include <stdlib.h>
00017 #include <string.h>
00018 #include <unistd.h>
00019
00020 #include "dnet.h"
00021 #include "mod.h"
00022
00023 static void
00024 usage(void)
00025 {
00026 fprintf(stderr, "Usage: dnet route show\n"
00027 " dnet route get <dst>\n"
00028 " dnet route add <dst> <gw>\n"
00029 " dnet route delete <dst>\n");
00030 exit(1);
00031 }
00032
00033 static int
00034 print_route(const struct route_entry *entry, void *arg)
00035 {
00036 printf("%-20s %-20s\n",
00037 addr_ntoa(&entry->route_dst), addr_ntoa(&entry->route_gw));
00038 return (0);
00039 }
00040
00041 int
00042 route_main(int argc, char *argv[])
00043 {
00044 struct route_entry entry;
00045 route_t *r;
00046 char *cmd;
00047
00048 if (argc < 2)
00049 usage();
00050
00051 cmd = argv[1];
00052
00053 if ((r = route_open()) == NULL)
00054 err(1, "route_open");
00055
00056 if (strcmp(cmd, "show") == 0) {
00057 printf("%-20s %-20s\n", "Destination", "Gateway");
00058 if (route_loop(r, print_route, NULL) < 0)
00059 err(1, "route_loop");
00060 } else if (strcmp(cmd, "get") == 0) {
00061 if (addr_aton(argv[2], &entry.route_dst) < 0)
00062 err(1, "addr_aton");
00063 if (route_get(r, &entry) < 0)
00064 err(1, "route_get");
00065 printf("get %s %s: gateway %s\n",
00066 (entry.route_dst.addr_bits < IP_ADDR_BITS) ?
00067 "net" : "host", addr_ntoa(&entry.route_dst),
00068 addr_ntoa(&entry.route_gw));
00069 } else if (strcmp(cmd, "add") == 0) {
00070 if (argc < 4 ||
00071 addr_aton(argv[2], &entry.route_dst) < 0 ||
00072 addr_aton(argv[3], &entry.route_gw) < 0)
00073 err(1, "addr_aton");
00074 if (route_add(r, &entry) < 0)
00075 err(1, "route_add");
00076 printf("add %s %s: gateway %s\n",
00077 (entry.route_dst.addr_bits < IP_ADDR_BITS) ?
00078 "net" : "host", addr_ntoa(&entry.route_dst),
00079 addr_ntoa(&entry.route_gw));
00080 } else if (strcmp(cmd, "delete") == 0) {
00081 if (addr_aton(argv[2], &entry.route_dst) < 0)
00082 err(1, "addr_aton");
00083 if (route_delete(r, &entry) < 0)
00084 err(1, "route_delete");
00085 printf("delete %s %s\n",
00086 (entry.route_dst.addr_bits < IP_ADDR_BITS) ?
00087 "net" : "host", addr_ntoa(&entry.route_dst));
00088 } else
00089 usage();
00090
00091 route_close(r);
00092
00093 exit(0);
00094 }
00095
00096 struct mod mod_route = {
00097 "route",
00098 MOD_TYPE_KERN,
00099 route_main
00100 };