* Copyright (c) 1985 Regents of the University of California.
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of California, Berkeley. The name of the
* University may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
static char sccsid
[] = "@(#)subr.c 5.12 (Berkeley) 7/23/88";
*******************************************************************************
* 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.
* If sockFD is valid, it is closed.
* If filePtr is valid, it is closed.
* Flow of control returns to the main() routine.
*******************************************************************************
if (filePtr
!= NULL
&& filePtr
!= stdout
) {
*******************************************************************************
* 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
);
saveMask
= sigblock(sigmask(SIGINT
));
ptr
= malloc((unsigned) size
);
(void) sigsetmask(saveMask
);
fprintf(stderr
, "malloc failed\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
= index(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 NO_INFO
: return("No information"); break;
case ERROR
: return("Unspecified error"); break;
case TIME_OUT
: return("Timed out"); break;
case NONAUTH
: return("Non-authoritative answer"); break;
return("BAD ERROR VALUE");
StringToClass(class, dflt
)
if (strcasecmp(class, "IN") == 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
, "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 */
fprintf(stderr
, "unknown query type: %s\n", type
);
*******************************************************************************
* Converts a query type to a descriptive name.
* (A more verbose form of p_type.)
*******************************************************************************
return("mail exchanger");
return("cannonical name");
return("start of authority zone");
return("mailbox domain name");
return("mail group member");
return("mail rename name");
return("null resource record");
return("well known service");
return("domain name pointer");
return("mailbox (MINFO)");
(void) sprintf(nbuf
, "%d", type
);