[BACK]Return to whois.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / whois

Annotation of src/usr.bin/whois/whois.c, Revision 1.6

1.6     ! deraadt     1: /*     $OpenBSD: whois.c,v 1.5 1999/08/16 20:24:36 art Exp $   */
1.1       deraadt     2: /*     $NetBSD: whois.c,v 1.5 1994/11/14 05:13:25 jtc Exp $    */
                      3:
                      4: /*
                      5:  * Copyright (c) 1980, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *     This product includes software developed by the University of
                     19:  *     California, Berkeley and its contributors.
                     20:  * 4. Neither the name of the University nor the names of its contributors
                     21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  */
                     36:
                     37: #ifndef lint
                     38: static char copyright[] =
                     39: "@(#) Copyright (c) 1980, 1993\n\
                     40:        The Regents of the University of California.  All rights reserved.\n";
                     41: #endif /* not lint */
                     42:
                     43: #ifndef lint
                     44: #if 0
                     45: static char sccsid[] = "@(#)whois.c    8.1 (Berkeley) 6/6/93";
                     46: #endif
1.6     ! deraadt    47: static char rcsid[] = "$OpenBSD: whois.c,v 1.5 1999/08/16 20:24:36 art Exp $";
1.1       deraadt    48: #endif /* not lint */
                     49:
                     50: #include <sys/types.h>
                     51: #include <sys/socket.h>
                     52: #include <netinet/in.h>
1.5       art        53: #include <arpa/inet.h>
1.1       deraadt    54: #include <netdb.h>
                     55: #include <stdio.h>
                     56: #include <stdlib.h>
                     57: #include <string.h>
1.4       deraadt    58: #include <sysexits.h>
1.1       deraadt    59: #include <unistd.h>
1.5       art        60: #include <err.h>
1.1       deraadt    61:
1.4       deraadt    62: #define        NICHOST         "whois.internic.net"
                     63: #define        DNICHOST        "nic.ddn.mil"
                     64: #define        ANICHOST        "whois.arin.net"
                     65: #define        RNICHOST        "whois.ripe.net"
                     66: #define        PNICHOST        "whois.apnic.net"
1.6     ! deraadt    67: #define        MNICHOST        "whois.ra.net"
        !            68: #define        QNICHOST_TAIL   ".whois-servers.net"
1.4       deraadt    69: #define        WHOIS_PORT      43
1.1       deraadt    70:
                     71: static void usage();
                     72:
                     73: int
                     74: main(argc, argv)
                     75:        int argc;
                     76:        char **argv;
                     77: {
                     78:        extern char *optarg;
                     79:        extern int optind;
                     80:        register FILE *sfi, *sfo;
                     81:        register int ch;
                     82:        struct sockaddr_in sin;
                     83:        struct hostent *hp;
                     84:        struct servent *sp;
                     85:        int s;
                     86:        char *host;
1.6     ! deraadt    87:        char *qnichost;
        !            88:        int use_qnichost;
        !            89:        int i, j;
1.1       deraadt    90:
                     91:        host = NICHOST;
1.6     ! deraadt    92:        use_qnichost = 0;
        !            93:        while ((ch = getopt(argc, argv, "adh:pmqr")) != -1)
1.1       deraadt    94:                switch((char)ch) {
1.4       deraadt    95:                case 'a':
                     96:                        host = ANICHOST;
                     97:                        break;
                     98:                case 'd':
                     99:                        host = DNICHOST;
                    100:                        break;
1.1       deraadt   101:                case 'h':
                    102:                        host = optarg;
                    103:                        break;
1.6     ! deraadt   104:                case 'm':
        !           105:                        host = MNICHOST;
        !           106:                        break;
1.4       deraadt   107:                case 'p':
                    108:                        host = PNICHOST;
                    109:                        break;
1.6     ! deraadt   110:                case 'q':
        !           111:                        use_qnichost = 1;
        !           112:                        break;
1.4       deraadt   113:                case 'r':
                    114:                        host = RNICHOST;
                    115:                        break;
1.1       deraadt   116:                case '?':
                    117:                default:
                    118:                        usage();
                    119:                }
                    120:        argc -= optind;
                    121:        argv += optind;
                    122:
                    123:        if (!argc)
                    124:                usage();
                    125:
1.6     ! deraadt   126:        if (use_qnichost != 0) {
        !           127:                if (argc == 1) {
        !           128:                        for (i = j = 0; (*argv)[i]; i++)
        !           129:                                if ((*argv)[i] == '.') j = i;
        !           130:                        if (j != 0) {
        !           131:                                qnichost = (char *) calloc(i - j + 1 + \
        !           132:                                    strlen(QNICHOST_TAIL), sizeof(char));
        !           133:                                if (!qnichost)
        !           134:                                        err(1, "malloc");
        !           135:                                strcpy(qnichost, *argv + j + 1);
        !           136:                                strcat(qnichost, QNICHOST_TAIL);
        !           137:                                host = qnichost;
        !           138:                        }
        !           139:                }
        !           140:        }
        !           141:
1.4       deraadt   142:        s = socket(PF_INET, SOCK_STREAM, 0);
                    143:        if (s < 0)
                    144:                err(EX_OSERR, "socket");
                    145:
                    146:        memset(&sin, 0, sizeof sin);
                    147:        sin.sin_len = sizeof sin;
                    148:        sin.sin_family = AF_INET;
                    149:
                    150:        if (inet_aton(host, &sin.sin_addr) == 0) {
                    151:                hp = gethostbyname2(host, AF_INET);
                    152:                if (hp == NULL)
                    153:                        errx(EX_NOHOST, "%s: %s", host, hstrerror(h_errno));
                    154:                host = hp->h_name;
                    155:                sin.sin_addr = *(struct in_addr *)hp->h_addr_list[0];
1.1       deraadt   156:        }
1.4       deraadt   157:
1.1       deraadt   158:        sp = getservbyname("whois", "tcp");
1.4       deraadt   159:        if (sp == NULL)
                    160:                sin.sin_port = htons(WHOIS_PORT);
                    161:        else
                    162:                sin.sin_port = sp->s_port;
                    163:
                    164:        if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)
                    165:                err(EX_OSERR, "connect");
                    166:
1.1       deraadt   167:        sfi = fdopen(s, "r");
                    168:        sfo = fdopen(s, "w");
1.4       deraadt   169:        if (sfi == NULL || sfo == NULL)
                    170:                err(EX_OSERR, "fdopen");
1.1       deraadt   171:        while (argc-- > 1)
                    172:                (void)fprintf(sfo, "%s ", *argv++);
                    173:        (void)fprintf(sfo, "%s\r\n", *argv);
                    174:        (void)fflush(sfo);
                    175:        while ((ch = getc(sfi)) != EOF)
                    176:                putchar(ch);
                    177:        exit(0);
                    178: }
                    179:
                    180: static void
                    181: usage()
                    182: {
1.6     ! deraadt   183:        (void)fprintf(stderr, "usage: whois [-admpqr] [-h hostname] name ...\n");
1.4       deraadt   184:        exit(EX_USAGE);
1.1       deraadt   185: }