|
|
1.1 root 1: /* ip2name.c - ip2name */
2:
3: #include <conf.h>
4: #include <kernel.h>
5: #include <network.h>
6:
7: /*------------------------------------------------------------------------
8: * ip2name - return DARPA Domain name for a host given its IP address
9: *------------------------------------------------------------------------
10: */
11: SYSCALL ip2name(ip, nam)
12: IPaddr ip;
13: char *nam;
14: {
15: char tmpstr[20]; /* temporary string buffer */
16: char *buf; /* buffer to hold domain query */
17: int dg, i;
18: register char *p;
19: register struct dn_mesg *dnptr;
20:
21: dnptr = (struct dn_mesg *) (buf = (char *) getmem(DN_MLEN));
22: *nam = NULLCH;
23: dnptr->dn_id = 0;
24: dnptr->dn_opparm = hs2net(DN_RD);
25: dnptr->dn_qcount = hs2net(1);
26: dnptr->dn_acount = dnptr->dn_ncount = dnptr->dn_dcount = 0;
27: p = dnptr->dn_qaaa;
28:
29: /* Fill in question with ip[3].ip[2].ip[1].ip[0].in-addr.arpa */
30:
31: for (i=3 ; i >= 0 ; i--) {
32: sprintf(tmpstr, "%d", ip[i] & LOWBYTE);
33: dn_cat(p, tmpstr);
34: }
35: dn_cat(p, "in-addr");
36: dn_cat(p, "arpa");
37: *p++ = NULLCH; /* terminate name */
38:
39: /* Add query type and query class fields to name */
40:
41: ( (struct dn_qsuf *)p )->dn_type = hs2net(DN_QTPR);
42: ( (struct dn_qsuf *)p )->dn_clas = hs2net(DN_QCIN);
43: p += sizeof(struct dn_qsuf);
44:
45: /* Broadcast query */
46:
47: dg = open(INTERNET, DSERVER, ANYLPORT);
48: control(dg, DG_SETMODE, DG_DMODE | DG_TMODE);
49: write (dg, buf, p - buf);
50: if ( (i = read(dg, buf, DN_MLEN)) == SYSERR || i == TIMEOUT)
51: panic("No response from name server");
52: close(dg);
53: if (net2hs(dnptr->dn_opparm) & DN_RESP ||
54: net2hs(dnptr->dn_acount) <= 0) {
55: freemem(buf, DN_MLEN);
56: return(SYSERR);
57: }
58:
59: /* In answer, skip name and remainder of resource record header */
60:
61: while (*p != NULLCH)
62: if (*p & DN_CMPRS) /* compressed section of name */
63: *++p = NULLCH;
64: else
65: p += *p + 1;
66: p += DN_RLEN + 1;
67:
68: /* Copy name to user */
69:
70: *nam = NULLCH;
71:
72: while (*p != NULLCH) {
73: if (*p & DN_CMPRS)
74: p = buf + (net2hs(*(int *)p) & DN_CPTR);
75: else {
76: strncat(nam, p+1, *p);
77: strcat(nam, ".");
78: p += *p + 1;
79: }
80: }
81: if (strlen(nam)) /* remove trailing dot */
82: nam[strlen(nam) - 1] = NULLCH;
83: freemem(buf, DN_MLEN);
84: return(OK);
85: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.