1  | 
     | 
     | 
    /*	$OpenBSD: whois.c,v 1.5 2013/10/27 18:49:25 guenther Exp $ */  | 
    
    
    2  | 
     | 
     | 
     | 
    
    
    3  | 
     | 
     | 
    /*  | 
    
    
    4  | 
     | 
     | 
     * Copyright (c) 2007 Henning Brauer <henning@openbsd.org>  | 
    
    
    5  | 
     | 
     | 
     *  | 
    
    
    6  | 
     | 
     | 
     * Permission to use, copy, modify, and distribute this software for any  | 
    
    
    7  | 
     | 
     | 
     * purpose with or without fee is hereby granted, provided that the above  | 
    
    
    8  | 
     | 
     | 
     * copyright notice and this permission notice appear in all copies.  | 
    
    
    9  | 
     | 
     | 
     *  | 
    
    
    10  | 
     | 
     | 
     * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES  | 
    
    
    11  | 
     | 
     | 
     * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF  | 
    
    
    12  | 
     | 
     | 
     * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR  | 
    
    
    13  | 
     | 
     | 
     * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES  | 
    
    
    14  | 
     | 
     | 
     * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN  | 
    
    
    15  | 
     | 
     | 
     * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT  | 
    
    
    16  | 
     | 
     | 
     * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  | 
    
    
    17  | 
     | 
     | 
     */  | 
    
    
    18  | 
     | 
     | 
     | 
    
    
    19  | 
     | 
     | 
    /*  | 
    
    
    20  | 
     | 
     | 
     * Copyright (c) 1980, 1993  | 
    
    
    21  | 
     | 
     | 
     *	The Regents of the University of California.  All rights reserved.  | 
    
    
    22  | 
     | 
     | 
     *  | 
    
    
    23  | 
     | 
     | 
     * Redistribution and use in source and binary forms, with or without  | 
    
    
    24  | 
     | 
     | 
     * modification, are permitted provided that the following conditions  | 
    
    
    25  | 
     | 
     | 
     * are met:  | 
    
    
    26  | 
     | 
     | 
     * 1. Redistributions of source code must retain the above copyright  | 
    
    
    27  | 
     | 
     | 
     *    notice, this list of conditions and the following disclaimer.  | 
    
    
    28  | 
     | 
     | 
     * 2. Redistributions in binary form must reproduce the above copyright  | 
    
    
    29  | 
     | 
     | 
     *    notice, this list of conditions and the following disclaimer in the  | 
    
    
    30  | 
     | 
     | 
     *    documentation and/or other materials provided with the distribution.  | 
    
    
    31  | 
     | 
     | 
     * 3. Neither the name of the University nor the names of its contributors  | 
    
    
    32  | 
     | 
     | 
     *    may be used to endorse or promote products derived from this software  | 
    
    
    33  | 
     | 
     | 
     *    without specific prior written permission.  | 
    
    
    34  | 
     | 
     | 
     *  | 
    
    
    35  | 
     | 
     | 
     * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND  | 
    
    
    36  | 
     | 
     | 
     * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  | 
    
    
    37  | 
     | 
     | 
     * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE  | 
    
    
    38  | 
     | 
     | 
     * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE  | 
    
    
    39  | 
     | 
     | 
     * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL  | 
    
    
    40  | 
     | 
     | 
     * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS  | 
    
    
    41  | 
     | 
     | 
     * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)  | 
    
    
    42  | 
     | 
     | 
     * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT  | 
    
    
    43  | 
     | 
     | 
     * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY  | 
    
    
    44  | 
     | 
     | 
     * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF  | 
    
    
    45  | 
     | 
     | 
     * SUCH DAMAGE.  | 
    
    
    46  | 
     | 
     | 
     */  | 
    
    
    47  | 
     | 
     | 
     | 
    
    
    48  | 
     | 
     | 
    #include <sys/types.h>  | 
    
    
    49  | 
     | 
     | 
    #include <sys/socket.h>  | 
    
    
    50  | 
     | 
     | 
     | 
    
    
    51  | 
     | 
     | 
    #include <netinet/in.h>  | 
    
    
    52  | 
     | 
     | 
    #include <arpa/inet.h>  | 
    
    
    53  | 
     | 
     | 
    #include <netdb.h>  | 
    
    
    54  | 
     | 
     | 
     | 
    
    
    55  | 
     | 
     | 
    #include <ctype.h>  | 
    
    
    56  | 
     | 
     | 
    #include <err.h>  | 
    
    
    57  | 
     | 
     | 
    #include <errno.h>  | 
    
    
    58  | 
     | 
     | 
    #include <stdio.h>  | 
    
    
    59  | 
     | 
     | 
    #include <stdlib.h>  | 
    
    
    60  | 
     | 
     | 
    #include <string.h>  | 
    
    
    61  | 
     | 
     | 
    #include <unistd.h>  | 
    
    
    62  | 
     | 
     | 
     | 
    
    
    63  | 
     | 
     | 
    #include "irrfilter.h"  | 
    
    
    64  | 
     | 
     | 
     | 
    
    
    65  | 
     | 
     | 
    #define WHOIS_STDOPTS	"-r -a"  | 
    
    
    66  | 
     | 
     | 
     | 
    
    
    67  | 
     | 
     | 
    char *qtype_opts[] = { | 
    
    
    68  | 
     | 
     | 
    	"",  | 
    
    
    69  | 
     | 
     | 
    	"-T aut-num",  | 
    
    
    70  | 
     | 
     | 
    	"-K -T as-set",  | 
    
    
    71  | 
     | 
     | 
    	"-K -T route -i origin",  | 
    
    
    72  | 
     | 
     | 
    	"-K -T route6 -i origin"  | 
    
    
    73  | 
     | 
     | 
    };  | 
    
    
    74  | 
     | 
     | 
     | 
    
    
    75  | 
     | 
     | 
    char	*server = "whois.radb.net";  | 
    
    
    76  | 
     | 
     | 
    char	*port = "whois";  | 
    
    
    77  | 
     | 
     | 
     | 
    
    
    78  | 
     | 
     | 
    int  | 
    
    
    79  | 
     | 
     | 
    whois(const char *query, enum qtype qtype)  | 
    
    
    80  | 
     | 
     | 
    { | 
    
    
    81  | 
     | 
     | 
    	FILE		*sfw, *sfr;  | 
    
    
    82  | 
     | 
     | 
    	int		 s, r = -1, error = 0, attempt, ret;  | 
    
    
    83  | 
     | 
     | 
    	struct addrinfo	 hints, *res, *ai;  | 
    
    
    84  | 
     | 
     | 
    	const char	*reason = NULL;  | 
    
    
    85  | 
     | 
     | 
     | 
    
    
    86  | 
     | 
     | 
    	memset(&hints, 0, sizeof(hints));  | 
    
    
    87  | 
     | 
     | 
    	hints.ai_flags = 0;  | 
    
    
    88  | 
     | 
     | 
    	hints.ai_family = AF_UNSPEC;  | 
    
    
    89  | 
     | 
     | 
    	hints.ai_socktype = SOCK_STREAM;  | 
    
    
    90  | 
     | 
     | 
    	error = getaddrinfo(server, port, &hints, &res);  | 
    
    
    91  | 
     | 
     | 
    	if (error) { | 
    
    
    92  | 
     | 
     | 
    		if (error == EAI_SERVICE)  | 
    
    
    93  | 
     | 
     | 
    			warnx("%s: bad port", port); | 
    
    
    94  | 
     | 
     | 
    		else  | 
    
    
    95  | 
     | 
     | 
    			warnx("%s: %s", server, gai_strerror(error)); | 
    
    
    96  | 
     | 
     | 
    		return (1);  | 
    
    
    97  | 
     | 
     | 
    	}  | 
    
    
    98  | 
     | 
     | 
     | 
    
    
    99  | 
     | 
     | 
    	for (s = -1, ai = res; ai != NULL; ai = ai->ai_next) { | 
    
    
    100  | 
     | 
     | 
    		attempt = 0;  | 
    
    
    101  | 
     | 
     | 
    		do { | 
    
    
    102  | 
     | 
     | 
    			attempt++;  | 
    
    
    103  | 
     | 
     | 
    			if (s != -1)  | 
    
    
    104  | 
     | 
     | 
    				close(s);  | 
    
    
    105  | 
     | 
     | 
    			s = socket(ai->ai_family, ai->ai_socktype,  | 
    
    
    106  | 
     | 
     | 
    			    ai->ai_protocol);  | 
    
    
    107  | 
     | 
     | 
    			if (s == -1) { | 
    
    
    108  | 
     | 
     | 
    				error = errno;  | 
    
    
    109  | 
     | 
     | 
    				reason = "socket";  | 
    
    
    110  | 
     | 
     | 
    			} else  | 
    
    
    111  | 
     | 
     | 
    				r = connect(s, ai->ai_addr, ai->ai_addrlen);  | 
    
    
    112  | 
     | 
     | 
    		} while (r == -1 && errno == ETIMEDOUT && attempt <= 3);  | 
    
    
    113  | 
     | 
     | 
     | 
    
    
    114  | 
     | 
     | 
    		if (r == -1) { | 
    
    
    115  | 
     | 
     | 
    			error = errno;  | 
    
    
    116  | 
     | 
     | 
    			reason = "connect";  | 
    
    
    117  | 
     | 
     | 
    			close(s);  | 
    
    
    118  | 
     | 
     | 
    			s = -1;  | 
    
    
    119  | 
     | 
     | 
    			continue;  | 
    
    
    120  | 
     | 
     | 
    		}  | 
    
    
    121  | 
     | 
     | 
    		if (s != -1)  | 
    
    
    122  | 
     | 
     | 
    			break;	/*okay*/  | 
    
    
    123  | 
     | 
     | 
    	}  | 
    
    
    124  | 
     | 
     | 
    	freeaddrinfo(res);  | 
    
    
    125  | 
     | 
     | 
     | 
    
    
    126  | 
     | 
     | 
    	if (s == -1) { | 
    
    
    127  | 
     | 
     | 
    		if (reason) { | 
    
    
    128  | 
     | 
     | 
    			errno = error;  | 
    
    
    129  | 
     | 
     | 
    			warn("%s: %s", server, reason); | 
    
    
    130  | 
     | 
     | 
    		} else  | 
    
    
    131  | 
     | 
     | 
    			warn("unknown error in connection attempt"); | 
    
    
    132  | 
     | 
     | 
    		return (1);  | 
    
    
    133  | 
     | 
     | 
    	}  | 
    
    
    134  | 
     | 
     | 
     | 
    
    
    135  | 
     | 
     | 
    	sfr = fdopen(s, "r");  | 
    
    
    136  | 
     | 
     | 
    	sfw = fdopen(s, "w");  | 
    
    
    137  | 
     | 
     | 
    	if (sfr == NULL || sfw == NULL)  | 
    
    
    138  | 
     | 
     | 
    		err(1, "fdopen");  | 
    
    
    139  | 
     | 
     | 
    	fprintf(sfw, "%s %s %s\r\n", WHOIS_STDOPTS, qtype_opts[qtype], query);  | 
    
    
    140  | 
     | 
     | 
    	fflush(sfw);  | 
    
    
    141  | 
     | 
     | 
     | 
    
    
    142  | 
     | 
     | 
    	if ((ret = parse_response(sfr, qtype)) == -1)  | 
    
    
    143  | 
     | 
     | 
    		warnx("parse error, query=\"%s %s\"", qtype_opts[qtype], query); | 
    
    
    144  | 
     | 
     | 
     | 
    
    
    145  | 
     | 
     | 
    	fclose(sfw);  | 
    
    
    146  | 
     | 
     | 
    	fclose(sfr);  | 
    
    
    147  | 
     | 
     | 
    	close(s);  | 
    
    
    148  | 
     | 
     | 
    	return (ret);  | 
    
    
    149  | 
     | 
     | 
    }  |