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.3 (Berkeley) 09/20/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 unknown\n", host); 83 exit(2); 84 } 85 host = hp->h_name; 86 if (argc > 0) 87 outfile = *argv; 88 sin.sin_family = hp->h_addrtype; 89 s = socket(hp->h_addrtype, SOCK_STREAM, 0); 90 if (s < 0) { 91 perror("gettable: socket"); 92 exit(4); 93 } 94 if (bind(s, &sin, sizeof (sin)) < 0) { 95 perror("gettable: bind"); 96 exit(5); 97 } 98 bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length); 99 sin.sin_port = sp->s_port; 100 if (connect(s, &sin, sizeof (sin)) < 0) { 101 perror("gettable: connect"); 102 exit(6); 103 } 104 fprintf(stderr, "Connection to %s opened.\n", host); 105 sfi = fdopen(s, "r"); 106 sfo = fdopen(s, "w"); 107 if (sfi == NULL || sfo == NULL) { 108 perror("gettable: fdopen"); 109 close(s); 110 exit(1); 111 } 112 hf = fopen(outfile, "w"); 113 if (hf == NULL) { 114 fprintf(stderr, "gettable: "); perror(outfile); 115 close(s); 116 exit(1); 117 } 118 fprintf(sfo, version ? VERSION : QUERY); 119 fflush(sfo); 120 while (fgets(buf, sizeof(buf), sfi) != NULL) { 121 len = strlen(buf); 122 buf[len-2] = '\0'; 123 if (!version && equaln(buf, "BEGIN", 5)) { 124 if (beginseen || endseen) { 125 fprintf(stderr, 126 "gettable: BEGIN sequence error\n"); 127 exit(90); 128 } 129 beginseen++; 130 continue; 131 } 132 if (!version && equaln(buf, "END", 3)) { 133 if (!beginseen || endseen) { 134 fprintf(stderr, 135 "gettable: END sequence error\n"); 136 exit(91); 137 } 138 endseen++; 139 continue; 140 } 141 if (equaln(buf, "ERR", 3)) { 142 fprintf(stderr, 143 "gettable: hostname service error: %s", buf); 144 exit(92); 145 } 146 fprintf(hf, "%s\n", buf); 147 } 148 fclose(hf); 149 if (!version) { 150 if (!beginseen) { 151 fprintf(stderr, "gettable: no BEGIN seen\n"); 152 exit(93); 153 } 154 if (!endseen) { 155 fprintf(stderr, "gettable: no END seen\n"); 156 exit(94); 157 } 158 fprintf(stderr, "Host table received.\n"); 159 } else 160 fprintf(stderr, "Version number received.\n"); 161 close(s); 162 fprintf(stderr, "Connection to %s closed\n", host); 163 exit(0); 164 } 165