int connectToRhostPortal( char *rodsHost, int rodsPort, int cookie, int windowSize ) { int status, nbytes; struct sockaddr_in remoteAddr; int sock, myCookie; status = setSockAddr( &remoteAddr, rodsHost, rodsPort ); if ( status < 0 ) { rodsLog( LOG_NOTICE, "connectToRhostPortal: setSockAddr error for %s, errno = %d", rodsHost, errno ); return status; } /* set timeout 11/13/2009 */ sock = connectToRhostWithRaddr( &remoteAddr, windowSize, 1 ); if ( sock < 0 ) { rodsLog( LOG_ERROR, "connectToRhostPortal: connectTo Rhost %s port %d error, status = %d", rodsHost, rodsPort, sock ); return sock; } myCookie = htonl( cookie ); nbytes = myWrite( sock, &myCookie, sizeof( myCookie ), NULL ); if ( nbytes != sizeof( myCookie ) ) { CLOSE_SOCK( sock ); return SYS_PORT_COOKIE_ERR; } return sock; }
int DNSgetAddr(SOCKET commSock, char *hostName, struct in_addr *iNet) { /* Variables for DNS message parsing and creation */ PDNS_HDR pDNShdr; SOCKADDR_IN sockAddr; char buffer[BUFSIZE]; u_char *addr; u_long *aPtr; int rc; /********************** * Get a DGRAM socket * **********************/ sockAddr = setSockAddr(dns_addr, DNS_PORT); rc = send_DNS_Addr_Query(hostName,commSock,sockAddr, buffer); if (rc < 0) return rc; pDNShdr = get_DNS_Response(commSock,sockAddr, buffer); if (pDNShdr == NULL) return -1; addr = processReplyBuffer_Addr(pDNShdr); if (addr == 0) return -1; aPtr = (u_long *) addr; iNet->s_addr = *aPtr; return(0); }
int setRhostInfo( rcComm_t *conn, const char *rodsHost, int rodsPort ) { int status; if ( rodsHost == NULL || strlen( rodsHost ) == 0 ) { return USER_RODS_HOST_EMPTY; } rstrcpy( conn->host, rodsHost, NAME_LEN ); conn->portNum = rodsPort; status = setSockAddr( &conn->remoteAddr, rodsHost, rodsPort ); return status; }
int getAFSServer(const char *service, const char *protocol, const char *cellName, unsigned short afsdbPort, /* network byte order */ int *cellHostAddrs, char cellHostNames[][MAXHOSTCHARS], unsigned short ports[], /* network byte order */ unsigned short ipRanks[], int *numServers, int *ttl) { #ifndef DNSAPI_ENV SOCKET commSock; SOCKADDR_IN sockAddr; PDNS_HDR pDNShdr; char buffer[BUFSIZE]; char query[1024]; int rc; #ifdef DEBUG fprintf(stderr, "getAFSServer: cell %s, cm_dnsEnabled=%d\n", cellName, cm_dnsEnabled); #endif *numServers = 0; *ttl = 0; #if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0500) if (cm_dnsEnabled == -1) { /* not yet initialized, eg when called by klog */ cm_InitDNS(1); /* assume enabled */ } #endif if (cm_dnsEnabled == 0) { /* possibly we failed in cm_InitDNS above */ fprintf(stderr, "DNS initialization failed, disabled\n"); return -1; } if (service == NULL || protocol == NULL || cellName == NULL) { fprintf(stderr, "invalid input\n"); return -1; } sockAddr = setSockAddr(dns_addr, DNS_PORT); commSock = socket( AF_INET, SOCK_DGRAM, 0 ); if ( commSock < 0 ) { /*afsi_log("socket() failed\n");*/ fprintf(stderr, "getAFSServer: socket() failed, errno=%d\n", errno); return (-1); } StringCbCopyA(query, sizeof(query), cellName); if (query[strlen(query)-1] != '.') { StringCbCatA(query, sizeof(query), "."); } rc = send_DNS_AFSDB_Query(query,commSock,sockAddr, buffer); if (rc < 0) { closesocket(commSock); fprintf(stderr,"getAFSServer: send_DNS_AFSDB_Query failed\n"); return -1; } pDNShdr = get_DNS_Response(commSock,sockAddr, buffer); /*printReplyBuffer_AFSDB(pDNShdr);*/ if (pDNShdr) processReplyBuffer_AFSDB(commSock, pDNShdr, cellHostAddrs, cellHostNames, ports, ipRanks, numServers, ttl); closesocket(commSock); if (*numServers == 0) return(-1); else return 0; #else /* DNSAPI_ENV */ PDNS_RECORD pDnsCell, pDnsIter, pDnsVol, pDnsVolIter, pDnsCIter; int i; char query[1024]; *numServers = 0; *ttl = 0; if (service == NULL || protocol == NULL || cellName == NULL) return -1; #ifdef AFS_FREELANCE_CLIENT if ( cm_stricmp_utf8N(cellName, "Freelance.Local.Root") == 0 ) return -1; #endif /* AFS_FREELANCE_CLIENT */ /* query the SRV _afs3-vlserver._udp records of cell */ StringCbPrintf(query, sizeof(query), "_%s._%s.%s", service, protocol, cellName); if (query[strlen(query)-1] != '.') { StringCbCatA(query, sizeof(query), "."); } if (DnsQuery_A(query, DNS_TYPE_SRV, DNS_QUERY_STANDARD, NULL, &pDnsCell, NULL) == ERROR_SUCCESS) { /* go through the returned records */ for (pDnsIter = pDnsCell;pDnsIter; pDnsIter = pDnsIter->pNext) { /* if we find an SRV record, we found the service */ if (pDnsIter->wType == DNS_TYPE_SRV) { StringCbCopyA(cellHostNames[*numServers], sizeof(cellHostNames[*numServers]), pDnsIter->Data.SRV.pNameTarget); ipRanks[*numServers] = pDnsIter->Data.SRV.wPriority; ports[*numServers] = htons(pDnsIter->Data.SRV.wPort); (*numServers)++; if (!*ttl) *ttl = pDnsIter->dwTtl; if (*numServers == AFSMAXCELLHOSTS) break; } } for (i=0;i<*numServers;i++) cellHostAddrs[i] = 0; /* now check if there are any A records in the results */ for (pDnsIter = pDnsCell; pDnsIter; pDnsIter = pDnsIter->pNext) { if(pDnsIter->wType == DNS_TYPE_A) /* check if its for one of the service */ for (i=0;i<*numServers;i++) if(cm_stricmp_utf8(pDnsIter->pName, cellHostNames[i]) == 0) cellHostAddrs[i] = pDnsIter->Data.A.IpAddress; } for (i=0;i<*numServers;i++) { /* if we don't have an IP yet, then we should try resolving the afs3-vlserver hostname in a separate query. */ if (!cellHostAddrs[i]) { if (DnsQuery_A(cellHostNames[i], DNS_TYPE_A, DNS_QUERY_STANDARD, NULL, &pDnsVol, NULL) == ERROR_SUCCESS) { for (pDnsVolIter = pDnsVol; pDnsVolIter; pDnsVolIter=pDnsVolIter->pNext) { /* if we get an A record, keep it */ if (pDnsVolIter->wType == DNS_TYPE_A && cm_stricmp_utf8(cellHostNames[i], pDnsVolIter->pName)==0) { cellHostAddrs[i] = pDnsVolIter->Data.A.IpAddress; break; } /* if we get a CNAME, look for a corresponding A record */ if (pDnsVolIter->wType == DNS_TYPE_CNAME && cm_stricmp_utf8(cellHostNames[i], pDnsVolIter->pName)==0) { for (pDnsCIter=pDnsVolIter; pDnsCIter; pDnsCIter=pDnsCIter->pNext) { if (pDnsCIter->wType == DNS_TYPE_A && cm_stricmp_utf8(pDnsVolIter->Data.CNAME.pNameHost, pDnsCIter->pName)==0) { cellHostAddrs[i] = pDnsCIter->Data.A.IpAddress; break; } } if (cellHostAddrs[i]) break; /* TODO: if the additional section is missing, then do another lookup for the CNAME */ } } /* we are done with the service lookup */ DnsRecordListFree(pDnsVol, DnsFreeRecordListDeep); } } } DnsRecordListFree(pDnsCell, DnsFreeRecordListDeep); } else { /* query the AFSDB records of cell */ StringCbCopyA(query, sizeof(query), cellName); if (query[strlen(query)-1] != '.') { StringCbCatA(query, sizeof(query), "."); } if (DnsQuery_A(query, DNS_TYPE_AFSDB, DNS_QUERY_STANDARD, NULL, &pDnsCell, NULL) == ERROR_SUCCESS) { /* go through the returned records */ for (pDnsIter = pDnsCell;pDnsIter; pDnsIter = pDnsIter->pNext) { /* if we find an AFSDB record with Preference set to 1, we found a service instance */ if (pDnsIter->wType == DNS_TYPE_AFSDB && pDnsIter->Data.Afsdb.wPreference == 1) { StringCbCopyA(cellHostNames[*numServers], sizeof(cellHostNames[*numServers]), pDnsIter->Data.Afsdb.pNameExchange); ipRanks[*numServers] = 0; ports[*numServers] = afsdbPort; (*numServers)++; if (!*ttl) *ttl = pDnsIter->dwTtl; if (*numServers == AFSMAXCELLHOSTS) break; } } for (i=0;i<*numServers;i++) cellHostAddrs[i] = 0; /* now check if there are any A records in the results */ for (pDnsIter = pDnsCell; pDnsIter; pDnsIter = pDnsIter->pNext) { if(pDnsIter->wType == DNS_TYPE_A) /* check if its for one of the service */ for (i=0;i<*numServers;i++) if(cm_stricmp_utf8(pDnsIter->pName, cellHostNames[i]) == 0) cellHostAddrs[i] = pDnsIter->Data.A.IpAddress; } for (i=0;i<*numServers;i++) { /* if we don't have an IP yet, then we should try resolving the service hostname in a separate query. */ if (!cellHostAddrs[i]) { if (DnsQuery_A(cellHostNames[i], DNS_TYPE_A, DNS_QUERY_STANDARD, NULL, &pDnsVol, NULL) == ERROR_SUCCESS) { for (pDnsVolIter = pDnsVol; pDnsVolIter; pDnsVolIter=pDnsVolIter->pNext) { /* if we get an A record, keep it */ if (pDnsVolIter->wType == DNS_TYPE_A && cm_stricmp_utf8(cellHostNames[i], pDnsVolIter->pName)==0) { cellHostAddrs[i] = pDnsVolIter->Data.A.IpAddress; break; } /* if we get a CNAME, look for a corresponding A record */ if (pDnsVolIter->wType == DNS_TYPE_CNAME && cm_stricmp_utf8(cellHostNames[i], pDnsVolIter->pName)==0) { for (pDnsCIter=pDnsVolIter; pDnsCIter; pDnsCIter=pDnsCIter->pNext) { if (pDnsCIter->wType == DNS_TYPE_A && cm_stricmp_utf8(pDnsVolIter->Data.CNAME.pNameHost, pDnsCIter->pName)==0) { cellHostAddrs[i] = pDnsCIter->Data.A.IpAddress; break; } } if (cellHostAddrs[i]) break; /* TODO: if the additional section is missing, then do another lookup for the CNAME */ } } /* we are done with the service lookup */ DnsRecordListFree(pDnsVol, DnsFreeRecordListDeep); } } } DnsRecordListFree(pDnsCell, DnsFreeRecordListDeep); } } if ( *numServers > 0 ) return 0; else return -1; #endif /* DNSAPI_ENV */ }