?? host.c
字號:
* returns the appropriate status code and if the ip number is available,
* copies it into mip
*/
static longword ddextract(qp,mip)
struct useek *qp;
unsigned char *mip;
{
word i,j,nans,rcode;
struct rrpart *rrp;
byte *p,space[260];
nans = intel16(qp->h.ancount); /* number of answers */
rcode = DRCODE & intel16(qp->h.flags); /* return code for this message*/
if (rcode > 0)
return(rcode);
if (nans > 0 && /* at least one answer */
(intel16(qp->h.flags) & DQR)) { /* response flag is set */
p = (byte *)&qp->x; /* where question starts */
i = unpackdom(space,p,qp); /* unpack question name */
/* spec defines name then QTYPE + QCLASS = 4 bytes */
p += i+4;
printf(" NAME : ? %s\n", space );
/*
* at this point, there may be several answers. We will take the first
* one which has an IP number. There may be other types of answers that
* we want to support later.
*/
while (nans-- > 0) { /* look at each answer */
i = unpackdom(space,p,qp); /* answer name to unpack */
/* n_puts(space);*/
p += i; /* account for string */
rrp = (struct rrpart *)p; /* resource record here */
/*
* check things which might not align on 68000 chip one byte at a time
*/
if (!*p && *(p+1) == DTYPEA && /* correct type and class */
!*(p+2) && *(p+3) == DIN) {
movmem(rrp->rdata,mip,4); /* save IP # */
return(0); /* successful return */
}
movmem(&rrp->rdlength,&j,2); /* 68000 alignment */
p += 10+intel16(j); /* length of rest of RR */
}
}
return(-1); /* generic failed to parse */
}
/*********************************************************************/
/* getdomain
* Look at the results to see if our DOMAIN request is ready.
* It may be a timeout, which requires another query.
*/
static longword udpdom()
{
int i,uret;
longword desired;
uret = sock_fastread(dom_sock, question, sizeof(struct useek ));
/* this does not happen */
if (uret < 0) {
/* netputevent(USERCLASS,DOMFAIL,-1); */
return(-1);
}
/* num = intel16(question->h.ident); */ /* get machine number */
/*
* check to see if the necessary information was in the UDP response
*/
i = ddextract(question, &desired);
switch (i) {
case 3: return(0); /* name does not exist */
case 0: return(intel(desired)); /* we found the IP number */
case -1:return( 0 ); /* strange return code from ddextract */
default:return( 0 ); /* dunno */
}
}
/**************************************************************************/
/* Sdomain
* DOMAIN based name lookup
* query a domain name server to get an IP number
* Returns the machine number of the machine record for future reference.
* Events generated will have this number tagged with them.
* Returns various negative numbers on error conditions.
*
* if adddom is nonzero, add default domain
*/
static longword Sdomain(mname, adddom, nameserver, timedout )
char *mname;
int adddom;
longword nameserver;
int *timedout; /* set to 1 on timeout */
{
char namebuff[512];
int isaname;
int domainsremaining;
int status, i;
longword response;
isaname = !isaddr( mname );
response = 0;
*timedout = 1;
if (!nameserver) { /* no nameserver, give up now */
outs("No nameserver defined!\n\r");
return(0);
}
while (*mname && *mname < 33) mname ++; /* kill leading spaces */
if (!(*mname))
return(0L);
question->h.flags = intel16(DRD); /* use recursion */
question->h.qdcount = 0;
question->h.ancount = 0;
question->h.nscount = 0;
question->h.arcount = 0;
qinit(); /* initialize some flag fields */
strcpy( namebuff, mname );
if ( isaname ) {
/* forward lookup */
/* only add these things if we are doing a real name */
question->h.qdcount = intel16(1);
if ( adddom ) {
if(namebuff[strlen(namebuff)-1] != '.') { /* if no trailing dot */
if(loc_domain) { /* there is a search list */
domainsremaining = countpaths( loc_domain );
strcat(namebuff,".");
strcat(namebuff,getpath(loc_domain,1));
}
} else
namebuff[ strlen(namebuff)-1] = 0; /* kill trailing dot */
}
} else {
/* reverse lookup */
question->h.ancount = intel16(1);
}
/*
* This is not terribly good, but it attempts to use a binary
* exponentially increasing delays.
*/
for ( i = 2; i < 17; i *= 2) {
if ( isaname ) sendom(namebuff,nameserver, 0xf001); /* try UDP */
else sendrdom( namebuff, nameserver, 0xf001 );
ip_timer_init( dom_sock, i );
do {
kbhit();
tcp_tick( dom_sock );
if (ip_timer_expired( dom_sock )) break;
if ( watcbroke ) {
break;
}
if (chk_timeout( timeoutwhen ))
break;
if ( sock_dataready( dom_sock )) *timedout = 0;
} while ( *timedout );
if ( !*timedout ) break; /* got an answer */
}
if ( !*timedout )
response = udpdom(); /* process the received data */
sock_close( dom_sock );
return( response );
}
/*
* nextdomain - given domain and count = 0,1,2,..., return next larger
* domain or NULL when no more are available
*/
static char *nextdomain( char *domain, int count )
{
char *p;
int i;
p = domain;
for (i = 0; i < count; ++i) {
p = strchr( p, '.' );
if (!p) return( NULL );
++p;
}
return( p );
}
/*
* newresolve()
* convert domain name -> address resolution.
* returns 0 if name is unresolvable right now
*/
longword newresolve(name)
char *name;
{
longword ip_address, temp;
int count, i;
byte timeout[ MAX_NAMESERVERS ];
struct useek qp; /* temp buffer */
udp_Socket ds; /* temp buffer */
word oldhndlcbrk;
question = &qp;
dom_sock = &ds;
if (!name) return( 0 );
rip( name );
if (!_domaintimeout) _domaintimeout = sock_delay << 2;
timeoutwhen = set_timeout( _domaintimeout );
count = 0;
memset( &timeout, 0, sizeof( timeout ));
oldhndlcbrk = wathndlcbrk;
wathndlcbrk = 1; /* enable special interrupt mode */
watcbroke = 0;
do {
if (!(loc_domain = nextdomain( def_domain, count )))
count = -1; /* use default name */
for ( i = 0; i < _last_nameserver ; ++i ) {
if (!timeout[i])
if (ip_address = Sdomain( name , count != -1 ,
def_nameservers[i], &timeout[i] ))
break; /* got name, bail out of loop */
}
if (count == -1) break;
count++;
} while (!ip_address);
watcbroke = 0; /* always clean up */
wathndlcbrk = oldhndlcbrk;
return( ip_address );
}
main(int argc, char **argv )
{
char *name;
longword host;
int status,i;
char buffer[ 20];
if (argc < 2) {
puts("HOST hostname");
exit(3);
}
sock_init();
for ( i = 1 ; i < argc ; ++i ) {
if ((*argv[i] == '/') || (*argv[i] == '-' )) {
}
else {
name = strdup( argv[i] );
printf("resolving \"%s\"...", name );
host = newresolve( name );
printf("\r");
clreol();
}
}
if ( host ) {
printf("\r");
clreol();
printf("%s : %s\n", name, inet_ntoa( buffer , host ));
}
exit( host ? 0 : 1 );
}
/* udpdom && ddextract(question, &desired); */
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -