1 /* 2 * Copyright (c) 1983 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 */ 17 18 #ifndef lint 19 char copyright[] = 20 "@(#) Copyright (c) 1983 The Regents of the University of California.\n\ 21 All rights reserved.\n"; 22 #endif /* not lint */ 23 24 #ifndef lint 25 static char sccsid[] = "@(#)gettable.c 5.4 (Berkeley) 10/11/88"; 26 #endif /* not lint */ 27 28 #include <sys/types.h> 29 #include <sys/socket.h> 30 31 #include <netinet/in.h> 32 33 #include <stdio.h> 34 #include <netdb.h> 35 36 #define OUTFILE "hosts.txt" /* default output file */ 37 #define VERFILE "hosts.ver" /* default version file */ 38 #define QUERY "ALL\r\n" /* query to hostname server */ 39 #define VERSION "VERSION\r\n" /* get version number */ 40 41 #define equaln(s1, s2, n) (!strncmp(s1, s2, n)) 42 43 struct sockaddr_in sin; 44 char buf[BUFSIZ]; 45 char *outfile = OUTFILE; 46 47 main(argc, argv) 48 int argc; 49 char *argv[]; 50 { 51 int s; 52 register len; 53 register FILE *sfi, *sfo, *hf; 54 char *host; 55 register struct hostent *hp; 56 struct servent *sp; 57 int version = 0; 58 int beginseen = 0; 59 int endseen = 0; 60 61 argv++, argc--; 62 if (**argv == '-') { 63 if (argv[0][1] != 'v') 64 fprintf(stderr, "unknown option %s ignored\n", *argv); 65 else 66 version++, outfile = VERFILE; 67 argv++, argc--; 68 } 69 if (argc < 1 || argc > 2) { 70 fprintf(stderr, "usage: gettable [-v] host [ file ]\n"); 71 exit(1); 72 } 73 sp = getservbyname("hostnames", "tcp"); 74 if (sp == NULL) { 75 fprintf(stderr, "gettable: hostnames/tcp: unknown service\n"); 76 exit(3); 77 } 78 host = *argv; 79 argv++, argc--; 80 hp = gethostbyname(host); 81 if (hp == NULL) { 82 fprintf(stderr, "gettable: %s: ", host); 83 herror((char *)NULL); 84 exit(2); 85 } 86 host = hp->h_name; 87 if (argc > 0) 88 outfile = *argv; 89 sin.sin_family = hp->h_addrtype; 90 s = socket(hp->h_addrtype, SOCK_STREAM, 0); 91 if (s < 0) { 92 perror("gettable: socket"); 93 exit(4); 94 } 95 if (bind(s, &sin, sizeof (sin)) < 0) { 96 perror("gettable: bind"); 97 exit(5); 98 } 99 bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length); 100 sin.sin_port = sp->s_port; 101 if (connect(s, &sin, sizeof (sin)) < 0) { 102 perror("gettable: connect"); 103 exit(6); 104 } 105 fprintf(stderr, "Connection to %s opened.\n", host); 106 sfi = fdopen(s, "r"); 107 sfo = fdopen(s, "w"); 108 if (sfi == NULL || sfo == NULL) { 109 perror("gettable: fdopen"); 110 close(s); 111 exit(1); 112 } 113 hf = fopen(outfile, "w"); 114 if (hf == NULL) { 115 fprintf(stderr, "gettable: "); perror(outfile); 116 close(s); 117 exit(1); 118 } 119 fprintf(sfo, version ? VERSION : QUERY); 120 fflush(sfo); 121 while (fgets(buf, sizeof(buf), sfi) != NULL) { 122 len = strlen(buf); 123 buf[len-2] = '\0'; 124 if (!version && equaln(buf, "BEGIN", 5)) { 125 if (beginseen || endseen) { 126 fprintf(stderr, 127 "gettable: BEGIN sequence error\n"); 128 exit(90); 129 } 130 beginseen++; 131 continue; 132 } 133 if (!version && equaln(buf, "END", 3)) { 134 if (!beginseen || endseen) { 135 fprintf(stderr, 136 "gettable: END sequence error\n"); 137 exit(91); 138 } 139 endseen++; 140 continue; 141 } 142 if (equaln(buf, "ERR", 3)) { 143 fprintf(stderr, 144 "gettable: hostname service error: %s", buf); 145 exit(92); 146 } 147 fprintf(hf, "%s\n", buf); 148 } 149 fclose(hf); 150 if (!version) { 151 if (!beginseen) { 152 fprintf(stderr, "gettable: no BEGIN seen\n"); 153 exit(93); 154 } 155 if (!endseen) { 156 fprintf(stderr, "gettable: no END seen\n"); 157 exit(94); 158 } 159 fprintf(stderr, "Host table received.\n"); 160 } else 161 fprintf(stderr, "Version number received.\n"); 162 close(s); 163 fprintf(stderr, "Connection to %s closed\n", host); 164 exit(0); 165 } 166