|
|
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.9 86/02/01 15:59:16 tony 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 connect to display!\n", argv[0]); ! 48: exit(1); ! 49: } ! 50: ! 51: XErrorHandler(local_xerror); ! 52: ! 53: ! 54: if (argc == 1) { ! 55: /* ! 56: * Get all the INET host names ! 57: */ ! 58: list = XGetHosts(&nhosts); ! 59: if (nhosts != 0) { ! 60: sethostent(1); /* don't close the data base each time */ ! 61: for (i = 0; i < nhosts; i++ ) { ! 62: hostname = get_hostname(list[i]); ! 63: printf("%s\t", hostname); ! 64: if (nameserver_timedout) ! 65: printf("(nameserver did not respond in %d seconds)\n", ! 66: NAMESERVER_TIMEOUT); ! 67: else printf("\n"); ! 68: } ! 69: endhostent(); ! 70: } ! 71: #ifdef DNETCONN ! 72: /* ! 73: * Get all the DECnet node names ! 74: */ ! 75: nlist = XGetNodes(&nhosts); ! 76: if (nhosts != 0) { ! 77: setnodeent(1); /* keep the database accessed */ ! 78: for (i = 0; i < nhosts; i++ ) { ! 79: printf("%s::\n", dnet_htoa(&nlist[i])); ! 80: } ! 81: } ! 82: #endif ! 83: exit(0); ! 84: } ! 85: ! 86: for (i = 1; i < argc; i++) { ! 87: arg = argv[i]; ! 88: if (*arg == '-') { ! 89: arg++; ! 90: #ifdef DNETCONN ! 91: if ((cp = index(arg, ':')) && (*(cp + 1) == ':')) { ! 92: *cp = '\0'; ! 93: if (dnaddrp = dnet_addr(arg)) { ! 94: XRemoveNode(dnaddrp); ! 95: } else { ! 96: if ((np = getnodebyname (arg)) == NULL) { ! 97: printf("xhost:bad node: %s::\n", arg); ! 98: } else { ! 99: dnaddr.a_len = np->n_length; ! 100: bcopy (np->n_addr, dnaddr.a_addr, np->n_length); ! 101: XRemoveNode(&dnaddr); ! 102: } ! 103: } ! 104: } else { ! 105: #endif ! 106: if ((address = get_address(arg)) == NULL) ! 107: fprintf(stderr, "%s: bad host: %s\n", argv[0], arg); ! 108: else XRemoveHost(address); ! 109: #ifdef DNETCONN ! 110: } ! 111: #endif ! 112: } else { ! 113: if (*arg == '+') arg++; ! 114: #ifdef DNETCONN ! 115: if ((cp = index(arg, ':')) && (*(cp + 1) == ':')) { ! 116: *cp = '\0'; ! 117: if (dnaddrp = dnet_addr(arg)) { ! 118: XAddNode(dnaddrp); ! 119: } else { ! 120: if ((np = getnodebyname (arg)) == NULL) { ! 121: printf("xhost:bad node: %s::\n", arg); ! 122: } else { ! 123: dnaddr.a_len = np->n_length; ! 124: bcopy (np->n_addr, dnaddr.a_addr, np->n_length); ! 125: XAddNode(&dnaddr); ! 126: } ! 127: } ! 128: } else { ! 129: #endif ! 130: if ((address = get_address(arg)) == NULL) ! 131: fprintf(stderr, "%s: bad host: %s\n", argv[0], arg); ! 132: else XAddHost(address); ! 133: #ifdef DNETCONN ! 134: } ! 135: #endif ! 136: } ! 137: } ! 138: XSync(0); ! 139: exit(0); ! 140: } ! 141: ! 142: ! 143: ! 144: /* ! 145: * get_address - return a pointer to an internet address given ! 146: * either a name (CHARON.MIT.EDU) or a string with the raw ! 147: * address (18.58.0.13) ! 148: */ ! 149: ! 150: char *get_address (name) ! 151: char *name; ! 152: { ! 153: struct hostent *hp; ! 154: static unsigned long address; ! 155: ! 156: /* Raw addresses are the ones that begin with a digit (do this for speed) */ ! 157: if (isdigit(*name)) { ! 158: address = inet_addr (name); ! 159: if ((int)address == -1) return(NULL); ! 160: else return((char *)&address); ! 161: } ! 162: hp = gethostbyname (name); ! 163: if (hp) { ! 164: if (hp->h_addrtype != AF_INET) return (NULL); ! 165: else return(hp->h_addr); ! 166: } else return(NULL); ! 167: } ! 168: ! 169: ! 170: /* ! 171: * get_hostname - Given an internet address, return a name (CHARON.MIT.EDU) ! 172: * or a string representing the address (18.58.0.13) if the name cannot ! 173: * be found. ! 174: */ ! 175: ! 176: jmp_buf env; ! 177: ! 178: char *get_hostname (address) ! 179: struct in_addr *address; ! 180: { ! 181: struct hostent *hp = NULL; ! 182: int nameserver_lost(); ! 183: char *inet_ntoa(); ! 184: /* gethostbyaddr can take a LONG time if the host does not exist. ! 185: Assume that if it does not respond in NAMESERVER_TIMEOUT seconds ! 186: that something is wrong and do not make the user wait. ! 187: gethostbyaddr will continue after a signal, so we have to ! 188: jump out of it. ! 189: */ ! 190: nameserver_timedout = 0; ! 191: signal(SIGALRM, nameserver_lost); ! 192: alarm(4); ! 193: if (setjmp(env) == 0) { ! 194: hp = gethostbyaddr (&address, sizeof(struct in_addr), AF_INET); ! 195: } ! 196: alarm(0); ! 197: if (hp) ! 198: return (hp->h_name); ! 199: else return (inet_ntoa(address)); ! 200: } ! 201: ! 202: nameserver_lost() ! 203: { ! 204: nameserver_timedout = 1; ! 205: longjmp(env, -1); ! 206: } ! 207: ! 208: /* ! 209: * local_xerror - local non-fatal error handling routine. If the error was ! 210: * that an X_GetHosts request for an unknown address format was received, just ! 211: * return, otherwise call the default error handler _XError. ! 212: */ ! 213: local_xerror (dpy, rep) ! 214: Display *dpy; ! 215: XErrorEvent *rep; ! 216: { ! 217: if ((rep->error_code == BadValue) && (rep->request_code == X_GetHosts)) { ! 218: return; ! 219: } else { ! 220: _XError(dpy, rep); ! 221: } ! 222: } ! 223:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.