121123Sdist /* 221123Sdist * Copyright (c) 1983 Regents of the University of California. 321123Sdist * All rights reserved. The Berkeley software License Agreement 421123Sdist * specifies the terms and conditions for redistribution. 521123Sdist */ 621123Sdist 710038Ssam #ifndef lint 8*27646Slepreau static char sccsid[] = "@(#)gettable.c 5.2 (Berkeley) 04/30/86"; 921123Sdist #endif not lint 1010038Ssam 1110038Ssam #include <sys/types.h> 1210038Ssam #include <sys/socket.h> 1310038Ssam 1410038Ssam #include <netinet/in.h> 1510038Ssam 1610038Ssam #include <stdio.h> 1710038Ssam #include <netdb.h> 1810038Ssam 1910038Ssam #define OUTFILE "hosts.txt" /* default output file */ 2016078Sralph #define VERFILE "hosts.ver" /* default version file */ 2110038Ssam #define QUERY "ALL\r\n" /* query to hostname server */ 2216078Sralph #define VERSION "VERSION\r\n" /* get version number */ 2310038Ssam 2410038Ssam #define equaln(s1, s2, n) (!strncmp(s1, s2, n)) 2510038Ssam 2610038Ssam struct sockaddr_in sin; 2710038Ssam char buf[BUFSIZ]; 2810038Ssam char *outfile = OUTFILE; 2910038Ssam 3010038Ssam main(argc, argv) 3110038Ssam int argc; 3210038Ssam char *argv[]; 3310038Ssam { 3410038Ssam int s; 3510038Ssam register len; 3610038Ssam register FILE *sfi, *sfo, *hf; 3710038Ssam char *host; 3810038Ssam register struct hostent *hp; 3910038Ssam struct servent *sp; 4016078Sralph int version = 0; 4117581Ssam int beginseen = 0; 4217581Ssam int endseen = 0; 4310038Ssam 4410038Ssam argv++, argc--; 4516078Sralph if (**argv == '-') { 4616078Sralph if (argv[0][1] != 'v') 4716078Sralph fprintf(stderr, "unknown option %s ignored\n", *argv); 4816078Sralph else 4916078Sralph version++, outfile = VERFILE; 5016078Sralph argv++, argc--; 5116078Sralph } 5210038Ssam if (argc < 1 || argc > 2) { 5316078Sralph fprintf(stderr, "usage: gettable [-v] host [ file ]\n"); 5410038Ssam exit(1); 5510038Ssam } 5615167Skarels sp = getservbyname("hostnames", "tcp"); 5710038Ssam if (sp == NULL) { 5815167Skarels fprintf(stderr, "gettable: hostnames/tcp: unknown service\n"); 5910038Ssam exit(3); 6010038Ssam } 6110038Ssam host = *argv; 6210038Ssam argv++, argc--; 6310038Ssam hp = gethostbyname(host); 6410038Ssam if (hp == NULL) { 6510038Ssam fprintf(stderr, "gettable: %s: host unknown\n", host); 6610038Ssam exit(2); 6710038Ssam } 6810038Ssam host = hp->h_name; 6910038Ssam if (argc > 0) 7010038Ssam outfile = *argv; 7110038Ssam sin.sin_family = hp->h_addrtype; 72*27646Slepreau s = socket(hp->h_addrtype, SOCK_STREAM, 0); 7310038Ssam if (s < 0) { 7410038Ssam perror("gettable: socket"); 7510038Ssam exit(4); 7610038Ssam } 77*27646Slepreau if (bind(s, &sin, sizeof (sin)) < 0) { 7810038Ssam perror("gettable: bind"); 7910038Ssam exit(5); 8010038Ssam } 81*27646Slepreau bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length); 8210038Ssam sin.sin_port = sp->s_port; 83*27646Slepreau if (connect(s, &sin, sizeof (sin)) < 0) { 8410038Ssam perror("gettable: connect"); 8510038Ssam exit(6); 8610038Ssam } 8710038Ssam fprintf(stderr, "Connection to %s opened.\n", host); 8810038Ssam sfi = fdopen(s, "r"); 8910038Ssam sfo = fdopen(s, "w"); 9010038Ssam if (sfi == NULL || sfo == NULL) { 9110038Ssam perror("gettable: fdopen"); 9210038Ssam close(s); 9310038Ssam exit(1); 9410038Ssam } 9510038Ssam hf = fopen(outfile, "w"); 9610038Ssam if (hf == NULL) { 9710038Ssam fprintf(stderr, "gettable: "); perror(outfile); 9810038Ssam close(s); 9910038Ssam exit(1); 10010038Ssam } 10116078Sralph fprintf(sfo, version ? VERSION : QUERY); 10210038Ssam fflush(sfo); 10310038Ssam while (fgets(buf, sizeof(buf), sfi) != NULL) { 10410038Ssam len = strlen(buf); 10510038Ssam buf[len-2] = '\0'; 10617581Ssam if (!version && equaln(buf, "BEGIN", 5)) { 10717581Ssam if (beginseen || endseen) { 10817581Ssam fprintf(stderr, 10917581Ssam "gettable: BEGIN sequence error\n"); 11017581Ssam exit(90); 11117581Ssam } 11217581Ssam beginseen++; 11310038Ssam continue; 11410038Ssam } 11517581Ssam if (!version && equaln(buf, "END", 3)) { 11617581Ssam if (!beginseen || endseen) { 11717581Ssam fprintf(stderr, 11817581Ssam "gettable: END sequence error\n"); 11917581Ssam exit(91); 12017581Ssam } 12117581Ssam endseen++; 12210038Ssam continue; 12310038Ssam } 12417581Ssam if (equaln(buf, "ERR", 3)) { 12517581Ssam fprintf(stderr, 12617581Ssam "gettable: hostname service error: %s", buf); 12717581Ssam exit(92); 12817581Ssam } 12910038Ssam fprintf(hf, "%s\n", buf); 13010038Ssam } 13110038Ssam fclose(hf); 13217581Ssam if (!version) { 13317581Ssam if (!beginseen) { 13417581Ssam fprintf(stderr, "gettable: no BEGIN seen\n"); 13517581Ssam exit(93); 13617581Ssam } 13717581Ssam if (!endseen) { 13817581Ssam fprintf(stderr, "gettable: no END seen\n"); 13917581Ssam exit(94); 14017581Ssam } 14117581Ssam fprintf(stderr, "Host table received.\n"); 14217581Ssam } else 14316078Sralph fprintf(stderr, "Version number received.\n"); 14410038Ssam close(s); 14510038Ssam fprintf(stderr, "Connection to %s closed\n", host); 14616078Sralph exit(0); 14710038Ssam } 148