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>
13 #include <afs/cellconfig.h>
16 #include "cm_dns_private.h"
19 #include <afs/afsint.h>
20 #if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0500)
26 /*extern void afsi_log(char *pattern, ...);*/
28 static char dns_addr[30];
29 static 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)
49 if (!enabled) { fprintf(stderr, "DNS support disabled\n"); cm_dnsEnabled = 0; return 0; }
51 /* First try AFS_NS environment var. */
52 addr = getenv("AFS_NS");
53 if (addr && inet_addr(addr) != -1) {
54 strcpy(dns_addr, addr);
56 /* Now check for the AFSDNS.INI file */
57 code = GetWindowsDirectory(configpath, sizeof(configpath));
58 if (code == 0 || code > sizeof(configpath)) return -1;
59 strcat(configpath, "\\afsdns.ini");
61 /* Currently we only get (and query) the first nameserver. Getting
62 list of mult. nameservers should be easy to do. */
63 len = GetPrivateProfileString("AFS Domain Name Servers", "ns1", NULL,
64 dns_addr, sizeof(dns_addr),
67 if (len == 0 || inet_addr(dns_addr) == -1) {
68 fprintf(stderr, "No valid name server addresses found, DNS lookup is "
70 cm_dnsEnabled = 0; /* failed */
71 return -1; /* No name servers defined */
73 else fprintf(stderr, "Found DNS server %s\n", dns_addr);
75 #endif /* DNSAPI_ENV */
81 SOCKADDR_IN setSockAddr(char *server, int port)
84 int addrLen = sizeof(SOCKADDR_IN);
86 #ifndef WIN32_LEAN_AND_MEAN
87 bzero(&sockAddr,addrLen);
88 #endif /*WIN32_LEAN_AND_MEAN*/
89 sockAddr.sin_family = AF_INET;
90 sockAddr.sin_port = htons( port );
91 sockAddr.sin_addr.s_addr = inet_addr( server );
92 /*inet_aton(server, &sockAddr.sin_addr.s_addr);*/
97 int getRRCount(PDNS_HDR ptr)
99 return(ntohs(ptr->rr_count));
103 int send_DNS_Addr_Query(char* query,
104 SOCKET commSock, SOCKADDR_IN sockAddr, char *buffer)
107 PDNS_QTAIL pDNS_qtail;
112 #ifndef WIN32_LEAN_AND_MEAN
113 bzero(buffer,BUFSIZE);
114 #endif /*WIN32_LEAN_AND_MEAN*/
116 /*********************************
117 * Build DNS Query Message *
119 * hard-coded Adrress (A) query *
120 *********************************/
122 pDNShdr = (PDNS_HDR)&( buffer[ 0 ] );
123 pDNShdr->id = htons( 0xDADE );
124 pDNShdr->flags = htons( DNS_FLAG_RD ); /* do recurse */
125 pDNShdr->q_count = htons( 1 ); /* one query */
126 pDNShdr->rr_count = 0; /* none in query */
127 pDNShdr->auth_count = 0; /* none in query */
128 pDNShdr->add_count = 0; /* none in query */
130 queryLen = putQName( query, &(buffer[ DNS_HDR_LEN ] ) );
131 queryLen += DNS_HDR_LEN; /* query Length is just after the query name and header */
133 fprintf(stderr, "send_DNS_Addr: query=%s, queryLen=%d\n", query, queryLen);
137 pDNS_qtail = (PDNS_QTAIL) &(buffer[ queryLen ]);
138 pDNS_qtail->qtype = htons(255);/*htons(DNS_RRTYPE_A); */
139 pDNS_qtail->qclass = htons(DNS_RRCLASS_IN);
140 queryLen += DNS_QTAIL_LEN;
142 /**************************
143 * Send DNS Query Message *
144 **************************/
147 res = sendto( commSock,
151 (struct sockaddr *) &sockAddr,
152 sizeof( SOCKADDR_IN ) );
154 /*dumpSbuffer(buffer,queryLen);*/
159 fprintf(stderr, "send_DNS_Addr_Query: error %d, errno %d\n", res, errno);
160 fprintf(stderr, "sendto() failed \n");
166 /*printf( "sendto() succeeded\n");*/
174 int send_DNS_AFSDB_Query(char* query,
175 SOCKET commSock, SOCKADDR_IN sockAddr, char *buffer)
177 /*static char buffer[BUFSIZE];*/
180 PDNS_QTAIL pDNS_qtail;
185 #ifndef WIN32_LEAN_AND_MEAN
186 bzero(buffer,BUFSIZE);
187 #endif /*WIN32_LEAN_AND_MEAN*/
189 /***************************
190 * Build DNS Query Message *
192 * hard-coded AFSDB query *
193 ***************************/
195 pDNShdr = (PDNS_HDR)&( buffer[ 0 ] );
196 pDNShdr->id = htons( 0xDEAD );
197 pDNShdr->flags = htons( DNS_FLAG_RD ); /* do recurse */
198 pDNShdr->q_count = htons( 1 ); /* one query */
199 pDNShdr->rr_count = 0; /* none in query */
200 pDNShdr->auth_count = 0; /* none in query */
201 pDNShdr->add_count = 0; /* none in query */
203 queryLen = putQName( query, &(buffer[ DNS_HDR_LEN ] ) );
204 queryLen += DNS_HDR_LEN; /* query Length is just after the query name and header */
207 pDNS_qtail = (PDNS_QTAIL) &(buffer[ queryLen ]);
208 pDNS_qtail->qtype = htons(DNS_RRTYPE_AFSDB);
209 pDNS_qtail->qclass = htons(DNS_RRCLASS_IN);
210 queryLen += DNS_QTAIL_LEN;
212 /**************************
213 * Send DNS Query Message *
214 **************************/
216 res = sendto( commSock,
220 (struct sockaddr *) &sockAddr,
221 sizeof( SOCKADDR_IN ) );
223 /*dumpSbuffer(buffer,queryLen);*/
228 fprintf(stderr, "send_DNS_AFSDB_Query: error %d, errno %d\n", res, errno);
229 fprintf(stderr, "sendto() failed \n");
235 /*printf( "sendto() succeeded\n");*/
243 PDNS_HDR get_DNS_Response(SOCKET commSock, SOCKADDR_IN sockAddr, char *buffer)
245 /*static char buffer[BUFSIZE];*/
247 int addrLen = sizeof(SOCKADDR_IN);
250 #ifndef WIN32_LEAN_AND_MEAN
251 bzero(buffer,BUFSIZE);
252 #endif /*WIN32_LEAN_AND_MEAN*/
254 /*****************************
255 * Receive DNS Reply Message *
256 *****************************/
258 /*printf( "calling recvfrom() on connected UDP socket\n" );*/
260 size = recvfrom( commSock,
264 (struct sockaddr *) &sockAddr,
266 if (size < 0) { fprintf(stderr, "recvfrom error %d\n", errno); return NULL; }
268 /*dumpRbuffer(buffer,res);*/
271 fprintf(stderr, "recvfrom returned %d bytes from %s: \n",
272 size, inet_ntoa( sockAddr.sin_addr ) );
275 return((PDNS_HDR)&( buffer[ 0 ] ));
280 int putQName( char *pHostName, char *pQName )
287 DNSlowerCase(pHostName);
288 /*printf( "Hostname: [%s]\n", pHostName );*/
290 for ( i = 0; *( pHostName + i ); i++ )
292 c = *( pHostName + i ); /* get next character */
297 /* dot encountered, fill in previous length */
298 if (k!=0){ /*don't process repeated dots*/
299 /*printf( "%c", c );*/
301 j = j+k+1; /* set index to next counter */
302 k = 0; /* reset segment length */
307 /*printf( "%c", c );*/
308 *( pQName + j + k + 1 ) = c; /* assign to QName */
309 k++; /* inc count of seg chars */
313 *(pQName + j ) = k; /* count for final segment */
315 *(pQName + j + k + 1 ) = 0; /* count for trailing NULL segment is 0 */
320 return ( j + k + 1 ); /* return total length of QName */
322 return ( j + k + 2 );
323 } /* end putQName() */
326 u_char * skipRRQName(u_char *pQName)
335 /* skip the 'compression' pointer */
339 /* skip a normal qname segment */
346 /* ptr now pointing at terminating zero of query QName,
347 or the pointer for the previous occurrence
353 } /* end skipRRQName() */
357 u_char * printRRQName( u_char *pQName, PDNS_HDR buffer )
360 u_char *buffPtr = (u_char *) buffer;
369 for ( i = 0; i < BUFSIZE; i++ )
380 for ( k = 1; k <= c; k++ )
382 fprintf(stderr, "%c", *( namePtr + k ) );
388 fprintf(stderr,"\n");
389 namePtr++; /* skip terminating zero */
396 } /* end printRRQName() */
399 u_char * sPrintRRQName( u_char *pQName, PDNS_HDR buffer, char *str )
402 u_char *buffPtr = (u_char *) buffer;
413 for ( i = 0; i < BUFSIZE; i++ )
424 for ( k = 1; k <= c; k++ )
426 sprintf(section,"%c", *( namePtr + k ) );
433 namePtr++; /* skip terminating zero */
440 } /* end sPrintRRQName() */
443 void printReplyBuffer_AFSDB(PDNS_HDR replyBuff)
445 u_char *ptr = (u_char *) replyBuff;
446 int answerCount = ntohs((replyBuff)->rr_count);
453 /* ptr now pointing at start of QName in query field */
454 ptr = skipRRQName(ptr);
457 /* skip the query type and class fields */
460 /* ptr should now be at the start of the answer RR sections */
462 fprintf(stderr,"---------------------------------\n");
463 for (i=0; i<answerCount ; i++){
464 ptr = skipRRQName(ptr);
465 rrPtr = (PDNS_AFSDB_RR_HDR) ptr;
466 ptr+= DNS_AFSDB_RR_HDR_LEN;
467 if ( ntohs(rrPtr->rr_afsdb_class) == 1) {
468 fprintf(stderr,"AFDB class %d -> ",ntohs(rrPtr->rr_afsdb_class));
469 ptr = printRRQName(ptr,replyBuff); }
471 ptr = skipRRQName(ptr);
473 fprintf(stderr,"---------------------------------\n");
478 void processReplyBuffer_AFSDB(SOCKET commSock, PDNS_HDR replyBuff, int *cellHostAddrs, char cellHostNames[][MAXHOSTCHARS], int *numServers, int *ttl)
479 /*PAFS_SRV_LIST (srvList)*/
481 u_char *ptr = (u_char *) replyBuff;
482 int answerCount = ntohs((replyBuff)->rr_count);
493 /* ptr now pointing at start of QName in query field */
494 ptr = skipRRQName(ptr);
497 /* skip the query type and class fields */
500 /* ptr should now be at the start of the answer RR sections */
502 answerCount = MIN(answerCount, AFSMAXCELLHOSTS);
504 fprintf(stderr, "processRep_AFSDB: answerCount=%d\n", answerCount);
507 for (i=0; i<answerCount ; i++){
508 ptr = skipRRQName(ptr);
509 rrPtr = (PDNS_AFSDB_RR_HDR) ptr;
510 ptr+= DNS_AFSDB_RR_HDR_LEN;
511 if ((ntohs(rrPtr->rr_afsdb_class) == 1) &&
512 (srvCount < MAX_AFS_SRVS)) {
513 /*ptr = sPrintRRQName(ptr,replyBuff,srvList->host[srvList->count]);*/
514 ptr = sPrintRRQName(ptr,replyBuff,hostName);
515 /*ptr = printRRQName(ptr,replyBuff);*/
516 *ttl = ntohl(rrPtr->rr_ttl);
519 fprintf(stderr, "resolving name %s\n", hostName);
521 /* resolve name from DNS query */
522 rc = DNSgetAddr(commSock, hostName, &addr);
524 continue; /* skip this entry */
526 fprintf(stderr, "processRep_AFSDB: resolved name %s to addr %x\n", hostName, addr);
528 memcpy(&cellHostAddrs[srvCount], &addr.s_addr, sizeof(addr.s_addr));
529 strncpy(cellHostNames[srvCount], hostName, MAXCELLCHARS);
530 cellHostNames[srvCount][MAXCELLCHARS-1] = '\0';
534 ptr = skipRRQName(ptr);
538 *numServers = srvCount;
543 u_char * processReplyBuffer_Addr(PDNS_HDR replyBuff)
545 u_char *ptr = (u_char *) replyBuff;
546 int answerCount = ntohs((replyBuff)->rr_count);
551 fprintf(stderr, "processReplyBuffer_Addr: answerCount=%d\n", answerCount);
553 if (answerCount == 0) return 0;
557 /* ptr now pointing at start of QName in query field */
558 ptr = skipRRQName(ptr);
561 /* skip the query type and class fields */
564 /* ptr should now be at the start of the answer RR sections */
565 ptr = skipRRQName(ptr);
566 rrPtr = (PDNS_A_RR_HDR) ptr;
569 fprintf(stderr, "type:%d, class:%d, ttl:%d, rdlength:%d\n",
570 ntohs(rrPtr->rr_type),ntohs(rrPtr->rr_class),
571 ntohl(rrPtr->rr_ttl),ntohs(rrPtr->rr_rdlength));
572 fprintf(stderr, "Count %d\tand Answer %8x\n",answerCount,rrPtr->rr_addr);
575 ptr += DNS_A_RR_HDR_LEN;
581 int DNSgetAddr(SOCKET commSock, char *hostName, struct in_addr *iNet)
583 /* Variables for DNS message parsing and creation */
586 SOCKADDR_IN sockAddr;
587 char buffer[BUFSIZE];
592 /**********************
593 * Get a DGRAM socket *
594 **********************/
596 sockAddr = setSockAddr(dns_addr, DNS_PORT);
598 rc = send_DNS_Addr_Query(hostName,commSock,sockAddr, buffer);
599 if (rc < 0) return rc;
600 pDNShdr = get_DNS_Response(commSock,sockAddr, buffer);
604 addr = processReplyBuffer_Addr(pDNShdr);
608 aPtr = (u_long *) addr;
610 iNet->s_addr = *aPtr;
614 #endif /* DNSAPI_ENV */
616 int getAFSServer(char *cellName, int *cellHostAddrs, char cellHostNames[][MAXHOSTCHARS],
617 int *numServers, int *ttl)
620 /*static AFS_SRV_LIST srvList;
621 static int ans = 0;*/
623 SOCKADDR_IN sockAddr;
625 char buffer[BUFSIZE];
630 fprintf(stderr, "getAFSServer: cell %s, cm_dnsEnabled=%d\n", cellName, cm_dnsEnabled);
633 #if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0500)
634 if (cm_dnsEnabled == -1) { /* not yet initialized, eg when called by klog */
635 cm_InitDNS(1); /* assume enabled */
638 if (cm_dnsEnabled == 0) { /* possibly we failed in cm_InitDNS above */
639 fprintf(stderr, "DNS initialization failed, disabled\n");
644 sockAddr = setSockAddr(dns_addr, DNS_PORT);
646 commSock = socket( AF_INET, SOCK_DGRAM, 0 );
649 /*afsi_log("socket() failed\n");*/
650 fprintf(stderr, "getAFSServer: socket() failed, errno=%d\n", errno);
655 strncpy(query, cellName, 1024);
657 if (query[strlen(query)-1] != '.') {
658 strncat(query,".",1024);
662 rc = send_DNS_AFSDB_Query(cellName,commSock,sockAddr, buffer);
664 fprintf(stderr,"getAFSServer: send_DNS_AFSDB_Query failed\n");
669 pDNShdr = get_DNS_Response(commSock,sockAddr, buffer);
671 /*printReplyBuffer_AFSDB(pDNShdr);*/
673 processReplyBuffer_AFSDB(commSock, pDNShdr, cellHostAddrs, cellHostNames, numServers, ttl);
677 closesocket(commSock);
678 if (*numServers == 0)
683 #else /* DNSAPI_ENV */
684 PDNS_RECORD pDnsCell, pDnsIter, pDnsVol,pDnsVolIter, pDnsCIter;
686 struct sockaddr_in vlSockAddr;
692 /* query the AFSDB records of cell */
693 strncpy(query, cellName, 1024);
695 if (query[strlen(query)-1] != '.') {
696 strncat(query,".",1024);
700 if (DnsQuery_A(query, DNS_TYPE_AFSDB, DNS_QUERY_STANDARD, NULL, &pDnsCell, NULL) == ERROR_SUCCESS) {
701 memset((void*) &vlSockAddr, 0, sizeof(vlSockAddr));
703 /* go through the returned records */
704 for (pDnsIter = pDnsCell;pDnsIter; pDnsIter = pDnsIter->pNext) {
705 /* if we find an AFSDB record with Preference set to 1, we found a volserver */
706 if (pDnsIter->wType == DNS_TYPE_AFSDB && pDnsIter->Data.Afsdb.wPreference == 1) {
707 strncpy(cellHostNames[*numServers], pDnsIter->Data.Afsdb.pNameExchange, MAXHOSTCHARS);
708 cellHostNames[*numServers][MAXHOSTCHARS-1]='\0';
712 *ttl = pDnsIter->dwTtl;
713 if (*numServers == AFSMAXCELLHOSTS)
718 for (i=0;i<*numServers;i++)
719 cellHostAddrs[i] = 0;
721 /* now check if there are any A records in the results */
722 for (pDnsIter = pDnsCell; pDnsIter; pDnsIter = pDnsIter->pNext) {
723 if(pDnsIter->wType == DNS_TYPE_A)
724 /* check if its for one of the volservers */
725 for (i=0;i<*numServers;i++)
726 if(stricmp(pDnsIter->pName, cellHostNames[i]) == 0)
727 cellHostAddrs[i] = pDnsIter->Data.A.IpAddress;
730 for (i=0;i<*numServers;i++) {
731 /* if we don't have an IP yet, then we should try resolving the volserver hostname
732 in a separate query. */
733 if (!cellHostAddrs[i]) {
734 if (DnsQuery_A(cellHostNames[i], DNS_TYPE_A, DNS_QUERY_STANDARD, NULL, &pDnsVol, NULL) == ERROR_SUCCESS) {
735 for (pDnsVolIter = pDnsVol; pDnsVolIter; pDnsVolIter=pDnsVolIter->pNext) {
736 /* if we get an A record, keep it */
737 if (pDnsVolIter->wType == DNS_TYPE_A && stricmp(cellHostNames[i], pDnsVolIter->pName)==0) {
738 cellHostAddrs[i] = pDnsVolIter->Data.A.IpAddress;
741 /* if we get a CNAME, look for a corresponding A record */
742 if (pDnsVolIter->wType == DNS_TYPE_CNAME && stricmp(cellHostNames[i], pDnsVolIter->pName)==0) {
743 for (pDnsCIter=pDnsVolIter; pDnsCIter; pDnsCIter=pDnsCIter->pNext) {
744 if (pDnsCIter->wType == DNS_TYPE_A && stricmp(pDnsVolIter->Data.CNAME.pNameHost, pDnsCIter->pName)==0) {
745 cellHostAddrs[i] = pDnsCIter->Data.A.IpAddress;
749 if (cellHostAddrs[i])
751 /* TODO: if the additional section is missing, then do another lookup for the CNAME */
754 /* we are done with the volserver lookup */
755 DnsRecordListFree(pDnsVol, DnsFreeRecordListDeep);
759 DnsRecordListFree(pDnsCell, DnsFreeRecordListDeep);
762 if ( *numServers > 0 )
766 #endif /* DNSAPI_ENV */
768 #endif /* AFS_AFSDB_ENV */