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