1 #ifndef lint 2 static char sccsid[] = "@(#)gettable.c 4.5 (Berkeley) 12/23/84"; 3 #endif 4 5 #include <sys/types.h> 6 #include <sys/socket.h> 7 8 #include <netinet/in.h> 9 10 #include <stdio.h> 11 #include <netdb.h> 12 13 #define OUTFILE "hosts.txt" /* default output file */ 14 #define VERFILE "hosts.ver" /* default version file */ 15 #define QUERY "ALL\r\n" /* query to hostname server */ 16 #define VERSION "VERSION\r\n" /* get version number */ 17 18 #define equaln(s1, s2, n) (!strncmp(s1, s2, n)) 19 20 struct sockaddr_in sin; 21 char buf[BUFSIZ]; 22 char *outfile = OUTFILE; 23 24 main(argc, argv) 25 int argc; 26 char *argv[]; 27 { 28 int s; 29 register len; 30 register FILE *sfi, *sfo, *hf; 31 register char *p; 32 char *host; 33 register struct hostent *hp; 34 struct servent *sp; 35 int version = 0; 36 int beginseen = 0; 37 int endseen = 0; 38 39 argv++, argc--; 40 if (**argv == '-') { 41 if (argv[0][1] != 'v') 42 fprintf(stderr, "unknown option %s ignored\n", *argv); 43 else 44 version++, outfile = VERFILE; 45 argv++, argc--; 46 } 47 if (argc < 1 || argc > 2) { 48 fprintf(stderr, "usage: gettable [-v] host [ file ]\n"); 49 exit(1); 50 } 51 sp = getservbyname("hostnames", "tcp"); 52 if (sp == NULL) { 53 fprintf(stderr, "gettable: hostnames/tcp: unknown service\n"); 54 exit(3); 55 } 56 host = *argv; 57 argv++, argc--; 58 hp = gethostbyname(host); 59 if (hp == NULL) { 60 fprintf(stderr, "gettable: %s: host unknown\n", host); 61 exit(2); 62 } 63 host = hp->h_name; 64 if (argc > 0) 65 outfile = *argv; 66 sin.sin_family = hp->h_addrtype; 67 s = socket(hp->h_addrtype, SOCK_STREAM, 0, 0); 68 if (s < 0) { 69 perror("gettable: socket"); 70 exit(4); 71 } 72 if (bind(s, &sin, sizeof (sin), 0) < 0) { 73 perror("gettable: bind"); 74 exit(5); 75 } 76 bcopy(hp->h_addr, &sin.sin_addr, hp->h_length); 77 sin.sin_port = sp->s_port; 78 if (connect(s, &sin, sizeof (sin), 0) < 0) { 79 perror("gettable: connect"); 80 exit(6); 81 } 82 fprintf(stderr, "Connection to %s opened.\n", host); 83 sfi = fdopen(s, "r"); 84 sfo = fdopen(s, "w"); 85 if (sfi == NULL || sfo == NULL) { 86 perror("gettable: fdopen"); 87 close(s); 88 exit(1); 89 } 90 hf = fopen(outfile, "w"); 91 if (hf == NULL) { 92 fprintf(stderr, "gettable: "); perror(outfile); 93 close(s); 94 exit(1); 95 } 96 fprintf(sfo, version ? VERSION : QUERY); 97 fflush(sfo); 98 while (fgets(buf, sizeof(buf), sfi) != NULL) { 99 len = strlen(buf); 100 buf[len-2] = '\0'; 101 if (!version && equaln(buf, "BEGIN", 5)) { 102 if (beginseen || endseen) { 103 fprintf(stderr, 104 "gettable: BEGIN sequence error\n"); 105 exit(90); 106 } 107 beginseen++; 108 continue; 109 } 110 if (!version && equaln(buf, "END", 3)) { 111 if (!beginseen || endseen) { 112 fprintf(stderr, 113 "gettable: END sequence error\n"); 114 exit(91); 115 } 116 endseen++; 117 continue; 118 } 119 if (equaln(buf, "ERR", 3)) { 120 fprintf(stderr, 121 "gettable: hostname service error: %s", buf); 122 exit(92); 123 } 124 fprintf(hf, "%s\n", buf); 125 } 126 fclose(hf); 127 if (!version) { 128 if (!beginseen) { 129 fprintf(stderr, "gettable: no BEGIN seen\n"); 130 exit(93); 131 } 132 if (!endseen) { 133 fprintf(stderr, "gettable: no END seen\n"); 134 exit(94); 135 } 136 fprintf(stderr, "Host table received.\n"); 137 } else 138 fprintf(stderr, "Version number received.\n"); 139 close(s); 140 fprintf(stderr, "Connection to %s closed\n", host); 141 exit(0); 142 } 143