* Copyright (c) 1985,1989 Regents of the University of California.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
static char sccsid
[] = "@(#)subr.c 5.24 (Berkeley) 3/2/91";
*******************************************************************************
* Miscellaneous subroutines for the name server
*******************************************************************************
#include <arpa/nameser.h>
*******************************************************************************
* This routine is called whenever a control-C is typed.
* It performs three main functions:
* - closes an open socket connection,
* - closes an open output file (used by LookupHost, et al.),
* - jumps back to the main read-eval loop.
* If a user types a ^C in the middle of a routine that uses a socket,
* the routine would not be able to close the socket. To prevent an
* overflow of the process's open file table, the socket and output
* file descriptors are closed by the interrupt handler.
* Open file descriptors are closed.
* If filePtr is valid, it is closed.
* Flow of control returns to the main() routine.
*******************************************************************************
#if defined(BSD) && BSD >= 199006
extern FILE *yyin
; /* scanner input file */
extern void yyrestart(); /* routine to restart scanner after interrupt */
if (filePtr
!= NULL
&& filePtr
!= stdout
) {
#if defined(BSD) && BSD >= 199006
*******************************************************************************
* Calls the malloc library routine with SIGINT blocked to prevent
* corruption of malloc's data structures. We need to do this because
* a control-C doesn't kill the program -- it causes a return to the
* NOTE: This method doesn't prevent the pointer returned by malloc
* from getting lost, so it is possible to get "core leaks".
* If malloc fails, the program exits.
* (address) - address of new buffer.
*******************************************************************************
ptr
= malloc((unsigned) size
);
old
= signal(SIGINT
, SIG_IGN
);
ptr
= malloc((unsigned) size
);
saveMask
= sigblock(sigmask(SIGINT
));
ptr
= malloc((unsigned) size
);
(void) sigsetmask(saveMask
);
fprintf(stderr
, "*** Can't allocate memory\n");
char *ptr
= Malloc(num
*size
);
*******************************************************************************
* Prints out the HostInfo structure for a host.
*******************************************************************************
PrintHostInfo(file
, title
, hp
)
register ServerInfo
**sp
;
fprintf(file
, "%-7s %s", title
, hp
->name
);
if (hp
->addrList
!= NULL
) {
if (hp
->addrList
[1] != NULL
) {
fprintf(file
, "\nAddresses:");
fprintf(file
, "\nAddress:");
for (cp
= hp
->addrList
; cp
&& *cp
; cp
++) {
fprintf(file
,"%c %s", comma
, inet_ntoa(*(struct in_addr
*)*cp
));
if (hp
->aliases
!= NULL
) {
fprintf(file
, "\nAliases:");
for (cp
= hp
->aliases
; cp
&& *cp
&& **cp
; cp
++) {
fprintf(file
, "%c %s", comma
, *cp
);
if (hp
->servers
!= NULL
) {
fprintf(file
, "\nServed by:\n");
for (sp
= hp
->servers
; *sp
!= NULL
; sp
++) {
fprintf(file
, "- %s\n\t", (*sp
)->name
);
for (cp
= (*sp
)->addrList
; cp
&& *cp
&& **cp
; cp
++) {
"%c %s", comma
, inet_ntoa(*(struct in_addr
*)*cp
));
for (cp
= (*sp
)->domains
; cp
&& *cp
&& **cp
; cp
++) {
fprintf(file
, "%c %s", comma
, *cp
);
*******************************************************************************
* Parses a command string for a file name and opens
* file pointer - the open was successful.
* NULL - there was an error opening the file or
* the input string was invalid.
*******************************************************************************
* Open an output file if we see '>' or >>'.
* Check for overwrite (">") or concatenation (">>").
redirect
= strchr(string
, '>');
if (redirect
[1] == '>') {
sscanf(redirect
, ">> %s", file
);
tmpPtr
= fopen(file
, "a+");
sscanf(redirect
, "> %s", file
);
tmpPtr
= fopen(file
, "w");
*******************************************************************************
* Converts an error code into a character string.
*******************************************************************************
case NOERROR
: return("Success"); break;
case FORMERR
: return("Format error"); break;
case SERVFAIL
: return("Server failed"); break;
case NXDOMAIN
: return("Non-existent domain"); break;
case NOTIMP
: return("Not implemented"); break;
case REFUSED
: return("Query refused"); break;
case NOCHANGE
: return("No change"); break;
case TIME_OUT
: return("Timed out"); break;
case NO_INFO
: return("No information"); break;
case ERROR
: return("Unspecified error"); break;
case NONAUTH
: return("Non-authoritative answer"); break;
case NO_RESPONSE
: return("No response from server"); break;
return("BAD ERROR VALUE");
StringToClass(class, dflt
)
if (strcasecmp(class, "IN") == 0)
if (strcasecmp(class, "HESIOD") == 0 ||
strcasecmp(class, "HS") == 0)
if (strcasecmp(class, "CHAOS") == 0)
if (strcasecmp(class, "ANY") == 0)
fprintf(stderr
, "unknown query class: %s\n", class);
*******************************************************************************
* Converts a string form of a query type name to its
* corresponding integer value.
*******************************************************************************
if (strcasecmp(type
, "A") == 0)
if (strcasecmp(type
, "NS") == 0)
return(T_NS
); /* authoritative server */
if (strcasecmp(type
, "MX") == 0)
return(T_MX
); /* mail exchanger */
if (strcasecmp(type
, "CNAME") == 0)
return(T_CNAME
); /* canonical name */
if (strcasecmp(type
, "SOA") == 0)
return(T_SOA
); /* start of authority zone */
if (strcasecmp(type
, "MB") == 0)
return(T_MB
); /* mailbox domain name */
if (strcasecmp(type
, "MG") == 0)
return(T_MG
); /* mail group member */
if (strcasecmp(type
, "MR") == 0)
return(T_MR
); /* mail rename name */
if (strcasecmp(type
, "WKS") == 0)
return(T_WKS
); /* well known service */
if (strcasecmp(type
, "PTR") == 0)
return(T_PTR
); /* domain name pointer */
if (strcasecmp(type
, "HINFO") == 0)
return(T_HINFO
); /* host information */
if (strcasecmp(type
, "MINFO") == 0)
return(T_MINFO
); /* mailbox information */
if (strcasecmp(type
, "AXFR") == 0)
return(T_AXFR
); /* zone transfer */
if (strcasecmp(type
, "MAILA") == 0)
return(T_MAILA
); /* mail agent */
if (strcasecmp(type
, "MAILB") == 0)
return(T_MAILB
); /* mail box */
if (strcasecmp(type
, "ANY") == 0)
return(T_ANY
); /* matches any type */
if (strcasecmp(type
, "UINFO") == 0)
return(T_UINFO
); /* user info */
if (strcasecmp(type
, "UID") == 0)
return(T_UID
); /* user id */
if (strcasecmp(type
, "GID") == 0)
return(T_GID
); /* group id */
if (strcasecmp(type
, "TXT") == 0)
return(T_TXT
); /* text */
fprintf(stderr
, "unknown query type: %s\n", type
);
*******************************************************************************
* Converts a query type to a descriptive name.
* (A more verbose form of p_type.)
*******************************************************************************
return("canonical name");
return("start of authority");
return("mail group member");
return("well-known service");
return("domain name pointer");
return("host information");
return("mailbox information");
return("mail exchanger");
return("user information");
return("mailbox-related data");
(void) sprintf(nbuf
, "%d", type
);