|
|
1.1 ! root 1: #include <X/mit-copyright.h> ! 2: /* Copyright 1985 Massachusetts Institute of Technology */ ! 3: #ifndef lint ! 4: static char *rcsid_xhost_c = "$Header: xhost.c,v 10.12 86/11/19 19:24:01 jg Rel $"; ! 5: #endif ! 6: ! 7: #include <signal.h> ! 8: #include <setjmp.h> ! 9: #include <ctype.h> ! 10: #include <sys/types.h> ! 11: #include <sys/socket.h> ! 12: #include <stdio.h> ! 13: #include <netdb.h> ! 14: #include <netinet/in.h> ! 15: #ifdef DNETCONN ! 16: #include <netdnet/dn.h> ! 17: #include <netdnet/dnetdb.h> ! 18: #endif ! 19: #include <X/Xlib.h> ! 20: #include <X/Xproto.h> ! 21: ! 22: char *index(); ! 23: int local_xerror(); ! 24: ! 25: #define NAMESERVER_TIMEOUT 5 /* time to wait for nameserver */ ! 26: int nameserver_timedout; ! 27: ! 28: main(argc, argv) ! 29: int argc; ! 30: char **argv; ! 31: { ! 32: Display *dpy; ! 33: char host[256]; ! 34: register char *arg; ! 35: int display, i, w, nhosts; ! 36: char *address, *get_address(); ! 37: char *hostname, *get_hostname(); ! 38: struct in_addr *list, *XGetHosts(); ! 39: #ifdef DNETCONN ! 40: char *dnet_htoa(); ! 41: struct nodeent *np; ! 42: struct dn_naddr *nlist, dnaddr, *dnaddrp, *XGetNodes(), *dnet_addr(); ! 43: char *cp, *index(); ! 44: #endif ! 45: ! 46: if ((dpy = XOpenDisplay(NULL)) == NULL) { ! 47: fprintf(stderr, "%s: Can't open display '%s'\n", ! 48: argv[0], XDisplayName("\0")); ! 49: exit(1); ! 50: } ! 51: ! 52: XErrorHandler(local_xerror); ! 53: ! 54: ! 55: if (argc == 1) { ! 56: /* ! 57: * Get all the INET host names ! 58: */ ! 59: list = XGetHosts(&nhosts); ! 60: if (nhosts != 0) { ! 61: sethostent(1); /* don't close the data base each time */ ! 62: for (i = 0; i < nhosts; i++ ) { ! 63: hostname = get_hostname(list[i]); ! 64: printf("%s\t", hostname); ! 65: if (nameserver_timedout) ! 66: printf("(nameserver did not respond in %d seconds)\n", ! 67: NAMESERVER_TIMEOUT); ! 68: else printf("\n"); ! 69: } ! 70: endhostent(); ! 71: } ! 72: #ifdef DNETCONN ! 73: /* ! 74: * Get all the DECnet node names ! 75: */ ! 76: nlist = XGetNodes(&nhosts); ! 77: if (nhosts != 0) { ! 78: setnodeent(1); /* keep the database accessed */ ! 79: for (i = 0; i < nhosts; i++ ) { ! 80: printf("%s::\n", dnet_htoa(&nlist[i])); ! 81: } ! 82: } ! 83: #endif ! 84: exit(0); ! 85: } ! 86: ! 87: for (i = 1; i < argc; i++) { ! 88: arg = argv[i]; ! 89: if (*arg == '-') { ! 90: arg++; ! 91: #ifdef DNETCONN ! 92: if ((cp = index(arg, ':')) && (*(cp + 1) == ':')) { ! 93: *cp = '\0'; ! 94: if (dnaddrp = dnet_addr(arg)) { ! 95: XRemoveNode(dnaddrp); ! 96: } else { ! 97: if ((np = getnodebyname (arg)) == NULL) { ! 98: printf("xhost:bad node: %s::\n", arg); ! 99: } else { ! 100: dnaddr.a_len = np->n_length; ! 101: bcopy (np->n_addr, dnaddr.a_addr, np->n_length); ! 102: XRemoveNode(&dnaddr); ! 103: } ! 104: } ! 105: } else { ! 106: #endif ! 107: if ((address = get_address(arg)) == NULL) ! 108: fprintf(stderr, "%s: bad host: %s\n", argv[0], arg); ! 109: else XRemoveHost(address); ! 110: #ifdef DNETCONN ! 111: } ! 112: #endif ! 113: } else { ! 114: if (*arg == '+') arg++; ! 115: #ifdef DNETCONN ! 116: if ((cp = index(arg, ':')) && (*(cp + 1) == ':')) { ! 117: *cp = '\0'; ! 118: if (dnaddrp = dnet_addr(arg)) { ! 119: XAddNode(dnaddrp); ! 120: } else { ! 121: if ((np = getnodebyname (arg)) == NULL) { ! 122: printf("xhost:bad node: %s::\n", arg); ! 123: } else { ! 124: dnaddr.a_len = np->n_length; ! 125: bcopy (np->n_addr, dnaddr.a_addr, np->n_length); ! 126: XAddNode(&dnaddr); ! 127: } ! 128: } ! 129: } else { ! 130: #endif ! 131: if ((address = get_address(arg)) == NULL) ! 132: fprintf(stderr, "%s: bad host: %s\n", argv[0], arg); ! 133: else XAddHost(address); ! 134: #ifdef DNETCONN ! 135: } ! 136: #endif ! 137: } ! 138: } ! 139: XSync(0); ! 140: exit(0); ! 141: } ! 142: ! 143: ! 144: ! 145: /* ! 146: * get_address - return a pointer to an internet address given ! 147: * either a name (CHARON.MIT.EDU) or a string with the raw ! 148: * address (18.58.0.13) ! 149: */ ! 150: ! 151: char *get_address (name) ! 152: char *name; ! 153: { ! 154: struct hostent *hp; ! 155: static unsigned long address; ! 156: ! 157: /* ! 158: * First see if inet_addr() can grok the name; if so, then use it. ! 159: */ ! 160: if ((address = inet_addr(name)) != -1) { ! 161: return((char *)&address); /* Found it */ ! 162: } ! 163: /* ! 164: * Is it in the namespace? ! 165: */ ! 166: else if (((hp = gethostbyname(name)) == (struct hostent *)NULL) ! 167: || hp->h_addrtype != AF_INET) { ! 168: return (NULL); /* Sorry, you lose */ ! 169: } else { ! 170: return (hp->h_addr); /* Found it. */ ! 171: } ! 172: } ! 173: ! 174: ! 175: /* ! 176: * get_hostname - Given an internet address, return a name (CHARON.MIT.EDU) ! 177: * or a string representing the address (18.58.0.13) if the name cannot ! 178: * be found. ! 179: */ ! 180: ! 181: jmp_buf env; ! 182: ! 183: char *get_hostname (address) ! 184: struct in_addr *address; ! 185: { ! 186: struct hostent *hp = NULL; ! 187: int nameserver_lost(); ! 188: char *inet_ntoa(); ! 189: /* gethostbyaddr can take a LONG time if the host does not exist. ! 190: Assume that if it does not respond in NAMESERVER_TIMEOUT seconds ! 191: that something is wrong and do not make the user wait. ! 192: gethostbyaddr will continue after a signal, so we have to ! 193: jump out of it. ! 194: */ ! 195: nameserver_timedout = 0; ! 196: signal(SIGALRM, nameserver_lost); ! 197: alarm(4); ! 198: if (setjmp(env) == 0) { ! 199: hp = gethostbyaddr (&address, sizeof(struct in_addr), AF_INET); ! 200: } ! 201: alarm(0); ! 202: if (hp) ! 203: return (hp->h_name); ! 204: else return (inet_ntoa(address)); ! 205: } ! 206: ! 207: nameserver_lost() ! 208: { ! 209: nameserver_timedout = 1; ! 210: longjmp(env, -1); ! 211: } ! 212: ! 213: /* ! 214: * local_xerror - local non-fatal error handling routine. If the error was ! 215: * that an X_GetHosts request for an unknown address format was received, just ! 216: * return, otherwise call the default error handler _XError. ! 217: */ ! 218: local_xerror (dpy, rep) ! 219: Display *dpy; ! 220: XErrorEvent *rep; ! 221: { ! 222: if ((rep->error_code == BadValue) && (rep->request_code == X_GetHosts)) { ! 223: return; ! 224: } else { ! 225: _XError(dpy, rep); ! 226: } ! 227: } ! 228:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.