1 /* Copyright 2000, International Business Machines Corporation and others.
4 * This software has been released under the terms of the IBM Public
5 * License. For details, see the LICENSE file in the top-level source
6 * directory or online at http://www.openafs.org/dl/license10.html
11 #include <afs/param.h>
17 #include "cm_dns_private.h"
20 #include <afs/afsint.h>
22 /*extern void afsi_log(char *pattern, ...);*/
25 static char dns_addr[30];
27 extern char cm_confDir[];
29 int cm_dnsEnabled = -1;
31 void DNSlowerCase(char *str)
35 for (i=0; i<strlen(str); i++)
36 /*str[i] = tolower(str[i]);*/
37 if (str[i] >= 'A' && str[i] <= 'Z')
41 int cm_InitDNS(int enabled)
48 if (!enabled) { fprintf(stderr, "DNS support disabled\n"); cm_dnsEnabled = 0; return 0; }
50 /* First try AFS_NS environment var. */
51 addr = getenv("AFS_NS");
52 if (addr && inet_addr(addr) != -1) {
53 strcpy(dns_addr, addr);
55 /* Now check for the AFSDNS.INI file */
57 strcpy(configpath, cm_confDir);
58 #elif defined(AFS_WIN95_ENV)
59 char *path = getenv("AFSCONF");
60 if (path) strcpy(configpath, path);
61 else strcpy(configpath, "c:\\afscli");
63 code = GetWindowsDirectory(configpath, sizeof(configpath));
64 if (code == 0 || code > sizeof(configpath)) return -1;
66 strcat(configpath, "\\afsdns.ini");
68 /* Currently we only get (and query) the first nameserver. Getting
69 list of mult. nameservers should be easy to do. */
70 len = GetPrivateProfileString("AFS Domain Name Servers", "ns1", NULL,
71 dns_addr, sizeof(dns_addr),
74 if (len == 0 || inet_addr(dns_addr) == -1) {
75 fprintf(stderr, "No valid name server addresses found, DNS lookup is "
77 cm_dnsEnabled = 0; /* failed */
78 return -1; /* No name servers defined */
80 else fprintf(stderr, "Found DNS server %s\n", dns_addr);
87 SOCKADDR_IN setSockAddr(char *server, int port)
90 int addrLen = sizeof(SOCKADDR_IN);
92 #ifndef WIN32_LEAN_AND_MEAN
93 bzero(&sockAddr,addrLen);
94 #endif /*WIN32_LEAN_AND_MEAN*/
95 sockAddr.sin_family = AF_INET;
96 sockAddr.sin_port = htons( port );
97 sockAddr.sin_addr.s_addr = inet_addr( server );
98 /*inet_aton(server, &sockAddr.sin_addr.s_addr);*/
103 int getRRCount(PDNS_HDR ptr)
105 return(ntohs(ptr->rr_count));
109 int send_DNS_Addr_Query(char* query,
110 SOCKET commSock, SOCKADDR_IN sockAddr, char *buffer)
113 PDNS_QTAIL pDNS_qtail;
118 #ifndef WIN32_LEAN_AND_MEAN
119 bzero(buffer,BUFSIZE);
120 #endif /*WIN32_LEAN_AND_MEAN*/
122 /*********************************
123 * Build DNS Query Message *
125 * hard-coded Adrress (A) query *
126 *********************************/
128 pDNShdr = (PDNS_HDR)&( buffer[ 0 ] );
129 pDNShdr->id = htons( 0xDADE );
130 pDNShdr->flags = htons( DNS_FLAG_RD ); /* do recurse */
131 pDNShdr->q_count = htons( 1 ); /* one query */
132 pDNShdr->rr_count = 0; /* none in query */
133 pDNShdr->auth_count = 0; /* none in query */
134 pDNShdr->add_count = 0; /* none in query */
136 queryLen = putQName( query, &(buffer[ DNS_HDR_LEN ] ) );
137 queryLen += DNS_HDR_LEN; /* query Length is just after the query name and header */
139 fprintf(stderr, "send_DNS_Addr: query=%s, queryLen=%d\n", query, queryLen);
143 pDNS_qtail = (PDNS_QTAIL) &(buffer[ queryLen ]);
144 pDNS_qtail->qtype = htons(255);/*htons(DNS_RRTYPE_A); */
145 pDNS_qtail->qclass = htons(DNS_RRCLASS_IN);
146 queryLen += DNS_QTAIL_LEN;
148 /**************************
149 * Send DNS Query Message *
150 **************************/
153 res = sendto( commSock,
157 (struct sockaddr *) &sockAddr,
158 sizeof( SOCKADDR_IN ) );
160 /*dumpSbuffer(buffer,queryLen);*/
165 fprintf(stderr, "send_DNS_Addr_Query: error %d, errno %d\n", res, errno);
166 fprintf(stderr, "sendto() failed \n");
172 /*printf( "sendto() succeeded\n");*/
180 int send_DNS_AFSDB_Query(char* query,
181 SOCKET commSock, SOCKADDR_IN sockAddr, char *buffer)
183 /*static char buffer[BUFSIZE];*/
186 PDNS_QTAIL pDNS_qtail;
191 #ifndef WIN32_LEAN_AND_MEAN
192 bzero(buffer,BUFSIZE);
193 #endif /*WIN32_LEAN_AND_MEAN*/
195 /***************************
196 * Build DNS Query Message *
198 * hard-coded AFSDB query *
199 ***************************/
201 pDNShdr = (PDNS_HDR)&( buffer[ 0 ] );
202 pDNShdr->id = htons( 0xDEAD );
203 pDNShdr->flags = htons( DNS_FLAG_RD ); /* do recurse */
204 pDNShdr->q_count = htons( 1 ); /* one query */
205 pDNShdr->rr_count = 0; /* none in query */
206 pDNShdr->auth_count = 0; /* none in query */
207 pDNShdr->add_count = 0; /* none in query */
209 queryLen = putQName( query, &(buffer[ DNS_HDR_LEN ] ) );
210 queryLen += DNS_HDR_LEN; /* query Length is just after the query name and header */
213 pDNS_qtail = (PDNS_QTAIL) &(buffer[ queryLen ]);
214 pDNS_qtail->qtype = htons(DNS_RRTYPE_AFSDB);
215 pDNS_qtail->qclass = htons(DNS_RRCLASS_IN);
216 queryLen += DNS_QTAIL_LEN;
218 /**************************
219 * Send DNS Query Message *
220 **************************/
222 res = sendto( commSock,
226 (struct sockaddr *) &sockAddr,
227 sizeof( SOCKADDR_IN ) );
229 /*dumpSbuffer(buffer,queryLen);*/
234 fprintf(stderr, "send_DNS_AFSDB_Query: error %d, errno %d\n", res, errno);
235 fprintf(stderr, "sendto() failed \n");
241 /*printf( "sendto() succeeded\n");*/
249 PDNS_HDR get_DNS_Response(SOCKET commSock, SOCKADDR_IN sockAddr, char *buffer)
251 /*static char buffer[BUFSIZE];*/
253 int addrLen = sizeof(SOCKADDR_IN);
256 #ifndef WIN32_LEAN_AND_MEAN
257 bzero(buffer,BUFSIZE);
258 #endif /*WIN32_LEAN_AND_MEAN*/
260 /*****************************
261 * Receive DNS Reply Message *
262 *****************************/
264 /*printf( "calling recvfrom() on connected UDP socket\n" );*/
266 size = recvfrom( commSock,
270 (struct sockaddr *) &sockAddr,
272 if (size < 0) { fprintf(stderr, "recvfrom error %d\n", errno); return NULL; }
274 /*dumpRbuffer(buffer,res);*/
277 fprintf(stderr, "recvfrom returned %d bytes from %s: \n",
278 size, inet_ntoa( sockAddr.sin_addr ) );
281 return((PDNS_HDR)&( buffer[ 0 ] ));
286 int putQName( char *pHostName, char *pQName )
293 DNSlowerCase(pHostName);
294 /*printf( "Hostname: [%s]\n", pHostName );*/
296 for ( i = 0; *( pHostName + i ); i++ )
298 c = *( pHostName + i ); /* get next character */
303 /* dot encountered, fill in previous length */
304 if (k!=0){ /*don't process repeated dots*/
305 /*printf( "%c", c );*/
307 j = j+k+1; /* set index to next counter */
308 k = 0; /* reset segment length */
313 /*printf( "%c", c );*/
314 *( pQName + j + k + 1 ) = c; /* assign to QName */
315 k++; /* inc count of seg chars */
319 *(pQName + j ) = k; /* count for final segment */
321 *(pQName + j + k + 1 ) = 0; /* count for trailing NULL segment is 0 */
326 return ( j + k + 1 ); /* return total length of QName */
328 return ( j + k + 2 );
329 } /* end putQName() */
332 u_char * skipRRQName(u_char *pQName)
341 /* skip the 'compression' pointer */
345 /* skip a normal qname segment */
352 /* ptr now pointing at terminating zero of query QName,
353 or the pointer for the previous occurrence
359 } /* end skipRRQName() */
363 u_char * printRRQName( u_char *pQName, PDNS_HDR buffer )
366 u_char *buffPtr = (u_char *) buffer;
375 for ( i = 0; i < BUFSIZE; i++ )
386 for ( k = 1; k <= c; k++ )
388 fprintf(stderr, "%c", *( namePtr + k ) );
394 fprintf(stderr,"\n");
395 namePtr++; /* skip terminating zero */
402 } /* end printRRQName() */
405 u_char * sPrintRRQName( u_char *pQName, PDNS_HDR buffer, char *str )
408 u_char *buffPtr = (u_char *) buffer;
419 for ( i = 0; i < BUFSIZE; i++ )
430 for ( k = 1; k <= c; k++ )
432 sprintf(section,"%c", *( namePtr + k ) );
439 namePtr++; /* skip terminating zero */
446 } /* end sPrintRRQName() */
449 void printReplyBuffer_AFSDB(PDNS_HDR replyBuff)
451 u_char *ptr = (u_char *) replyBuff;
452 int answerCount = ntohs((replyBuff)->rr_count);
459 /* ptr now pointing at start of QName in query field */
460 ptr = skipRRQName(ptr);
463 /* skip the query type and class fields */
466 /* ptr should now be at the start of the answer RR sections */
468 fprintf(stderr,"---------------------------------\n");
469 for (i=0; i<answerCount ; i++){
470 ptr = skipRRQName(ptr);
471 rrPtr = (PDNS_AFSDB_RR_HDR) ptr;
472 ptr+= DNS_AFSDB_RR_HDR_LEN;
473 if ( ntohs(rrPtr->rr_afsdb_class) == 1) {
474 fprintf(stderr,"AFDB class %d -> ",ntohs(rrPtr->rr_afsdb_class));
475 ptr = printRRQName(ptr,replyBuff); }
477 ptr = skipRRQName(ptr);
479 fprintf(stderr,"---------------------------------\n");
484 void processReplyBuffer_AFSDB(SOCKET commSock, PDNS_HDR replyBuff, int *cellHosts, int *numServers, int *ttl)
485 /*PAFS_SRV_LIST (srvList)*/
487 u_char *ptr = (u_char *) replyBuff;
488 int answerCount = ntohs((replyBuff)->rr_count);
499 /* ptr now pointing at start of QName in query field */
500 ptr = skipRRQName(ptr);
503 /* skip the query type and class fields */
506 /* ptr should now be at the start of the answer RR sections */
508 answerCount = MIN(answerCount, AFSMAXCELLHOSTS);
510 fprintf(stderr, "processRep_AFSDB: answerCount=%d\n", answerCount);
513 for (i=0; i<answerCount ; i++){
514 ptr = skipRRQName(ptr);
515 rrPtr = (PDNS_AFSDB_RR_HDR) ptr;
516 ptr+= DNS_AFSDB_RR_HDR_LEN;
517 if ((ntohs(rrPtr->rr_afsdb_class) == 1) &&
518 (srvCount < MAX_AFS_SRVS)) {
519 /*ptr = sPrintRRQName(ptr,replyBuff,srvList->host[srvList->count]);*/
520 ptr = sPrintRRQName(ptr,replyBuff,hostName);
521 /*ptr = printRRQName(ptr,replyBuff);*/
522 *ttl = ntohl(rrPtr->rr_ttl);
525 fprintf(stderr, "resolving name %s\n", hostName);
527 /* resolve name from DNS query */
528 rc = DNSgetAddr(commSock, hostName, &addr);
530 continue; /* skip this entry */
532 fprintf(stderr, "processRep_AFSDB: resolved name %s to addr %x\n", hostName, addr);
534 memcpy(&cellHosts[srvCount], &addr.s_addr, sizeof(addr.s_addr));
538 ptr = skipRRQName(ptr);
542 *numServers = srvCount;
547 u_char * processReplyBuffer_Addr(PDNS_HDR replyBuff)
549 u_char *ptr = (u_char *) replyBuff;
550 int answerCount = ntohs((replyBuff)->rr_count);
555 fprintf(stderr, "processReplyBuffer_Addr: answerCount=%d\n", answerCount);
557 if (answerCount == 0) return 0;
561 /* ptr now pointing at start of QName in query field */
562 ptr = skipRRQName(ptr);
565 /* skip the query type and class fields */
568 /* ptr should now be at the start of the answer RR sections */
569 ptr = skipRRQName(ptr);
570 rrPtr = (PDNS_A_RR_HDR) ptr;
573 fprintf(stderr, "type:%d, class:%d, ttl:%d, rdlength:%d\n",
574 ntohs(rrPtr->rr_type),ntohs(rrPtr->rr_class),
575 ntohl(rrPtr->rr_ttl),ntohs(rrPtr->rr_rdlength));
576 fprintf(stderr, "Count %d\tand Answer %8x\n",answerCount,rrPtr->rr_addr);
579 ptr += DNS_A_RR_HDR_LEN;
585 int getAFSServer(char *cellName, int *cellHosts, int *numServers, int *ttl)
587 /*static AFS_SRV_LIST srvList;
588 static int ans = 0;*/
590 SOCKADDR_IN sockAddr;
592 char buffer[BUFSIZE];
596 fprintf(stderr, "getAFSServer: cell %s, cm_dnsEnabled=%d\n", cellName, cm_dnsEnabled);
599 if (cm_dnsEnabled == -1) { /* not yet initialized, eg when called by klog */
600 cm_InitDNS(1); /* assume enabled */
602 if (cm_dnsEnabled == 0) { /* possibly we failed in cm_InitDNS above */
603 fprintf(stderr, "DNS initialization failed, disabled\n");
608 sockAddr = setSockAddr(dns_addr, DNS_PORT);
610 commSock = socket( AF_INET, SOCK_DGRAM, 0 );
613 /*afsi_log("socket() failed\n");*/
614 fprintf(stderr, "getAFSServer: socket() failed, errno=%d\n", errno);
620 /* the win95 sock.vxd will not allow sendto for unbound sockets,
621 * so just bind to nothing and it works */
623 __djgpp_set_socket_blocking_mode(commSock, 0);
624 bind(commSock,0,sizeof( SOCKADDR_IN ) );
627 rc = send_DNS_AFSDB_Query(cellName,commSock,sockAddr, buffer);
629 fprintf(stderr,"getAFSServer: send_DNS_AFSDB_Query failed\n");
634 pDNShdr = get_DNS_Response(commSock,sockAddr, buffer);
636 /*printReplyBuffer_AFSDB(pDNShdr);*/
638 processReplyBuffer_AFSDB(commSock, pDNShdr, cellHosts, numServers, ttl);
642 closesocket(commSock);
643 if (*numServers == 0)
650 int DNSgetAddr(SOCKET commSock, char *hostName, struct in_addr *iNet)
652 /* Variables for DNS message parsing and creation */
655 SOCKADDR_IN sockAddr;
656 char buffer[BUFSIZE];
661 /**********************
662 * Get a DGRAM socket *
663 **********************/
665 sockAddr = setSockAddr(dns_addr, DNS_PORT);
667 rc = send_DNS_Addr_Query(hostName,commSock,sockAddr, buffer);
668 if (rc < 0) return rc;
669 pDNShdr = get_DNS_Response(commSock,sockAddr, buffer);
673 addr = processReplyBuffer_Addr(pDNShdr);
677 aPtr = (u_long *) addr;
679 iNet->s_addr = *aPtr;
684 #endif /* AFS_AFSDB_ENV */