1 #ifndef lint 2 static char sccsid[] = "@(#)route.c 4.5 82/11/15"; 3 #endif 4 5 #include <sys/types.h> 6 #include <sys/socket.h> 7 #include <sys/ioctl.h> 8 9 #include <net/route.h> 10 #include <netinet/in.h> 11 12 #include <stdio.h> 13 #include <errno.h> 14 #include <ctype.h> 15 #include <netdb.h> 16 17 struct rtentry route; 18 int options; 19 int s; 20 struct sockaddr_in sin = { AF_INET }; 21 22 main(argc, argv) 23 int argc; 24 char *argv[]; 25 { 26 27 if (argc < 2) 28 printf("usage: route [ cmd ] [ args ]\n"), exit(1); 29 s = socket(AF_INET, SOCK_RAW, 0, 0); 30 if (s < 0) { 31 perror("route: socket"); 32 exit(1); 33 } 34 argc--, argv++; 35 if (strcmp(*argv, "add") == 0) 36 newroute(argc, argv); 37 else if (strcmp(*argv, "delete") == 0) 38 newroute(argc, argv); 39 else if (strcmp(*argv, "change") == 0) 40 changeroute(argc-1, argv+1); 41 else 42 printf("%s: huh?\n", *argv); 43 } 44 45 newroute(argc, argv) 46 int argc; 47 char *argv[]; 48 { 49 struct sockaddr_in *sin; 50 char *cmd; 51 52 if (argc < 3 || argc > 4) { 53 printf("usage: %s destination gateway [ metric ]\n", argv[0]); 54 return; 55 } 56 cmd = argv[0]; 57 getaddr(argv[1], &route.rt_dst); 58 getaddr(argv[2], &route.rt_gateway); 59 sin = (struct sockaddr_in *)&route.rt_dst; 60 route.rt_flags = RTF_UP; 61 if (inet_lnaof(sin->sin_addr) != 0) 62 route.rt_flags |= RTF_HOST; 63 if (argc > 3 && atoi(argv[3]) > 0) 64 route.rt_flags |= RTF_GATEWAY; 65 printf("%s %x: gateway %x, flags %x\n", cmd, sin->sin_addr, 66 ((struct sockaddr_in *)&route.rt_gateway)->sin_addr, 67 route.rt_flags); 68 if (ioctl(s, *cmd == 'a' ? SIOCADDRT : SIOCDELRT, (caddr_t)&route)) 69 error(cmd); 70 } 71 72 changeroute(argc, argv) 73 int argc; 74 char *argv[]; 75 { 76 printf("not supported\n"); 77 } 78 79 error(cmd) 80 char *cmd; 81 { 82 extern int errno; 83 84 if (errno == ESRCH) 85 fprintf(stderr, "not in table\n"); 86 else if (errno == EBUSY) 87 fprintf(stderr, "entry in use\n"); 88 else if (errno == ENOBUFS) 89 fprintf(stderr, "routing table overflow\n"); 90 else 91 perror(cmd); 92 } 93 94 getaddr(s, sin) 95 char *s; 96 struct sockaddr_in *sin; 97 { 98 struct hostent *hp; 99 100 hp = gethostbyname(s); 101 if (hp == 0) { 102 sin->sin_addr.s_addr = inet_addr(s); 103 if (sin->sin_addr.s_addr == -1) { 104 fprintf(stderr, "%s: bad value\n", s); 105 exit(1); 106 } 107 sin->sin_family = AF_INET; 108 return; 109 } 110 sin->sin_family = hp->h_addrtype; 111 bcopy(hp->h_addr, &sin->sin_addr, hp->h_length); 112 } 113