From 292a51eda152f5e1885f38f3a811e956560f33f0 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Fri, 13 Jun 2003 21:03:15 +0000 Subject: Forward port the app-head changes for dc name cache into 3.0. Jeremy. (This used to be commit 8bcc3116a22ce11b55a35f3363230f54bc5735fc) --- source3/libsmb/namequery_dc.c | 95 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 78 insertions(+), 17 deletions(-) (limited to 'source3/libsmb/namequery_dc.c') diff --git a/source3/libsmb/namequery_dc.c b/source3/libsmb/namequery_dc.c index e98b728963..c162e34027 100644 --- a/source3/libsmb/namequery_dc.c +++ b/source3/libsmb/namequery_dc.c @@ -156,12 +156,11 @@ void flush_negative_conn_cache( void ) } /**************************************************************************** - Utility function to return the name of a DC using RPC. The name is - guaranteed to be valid since we have already done a name_status_find on it - and we have checked our negative connection cache + Utility function to return the name of a DC. The name is guaranteed to be + valid since we have already done a name_status_find on it ***************************************************************************/ - -BOOL rpc_find_dc(const char *domain, fstring srv_name, struct in_addr *ip_out) + +BOOL get_dc_name(const char *domain, fstring srv_name, struct in_addr *ip_out) { struct in_addr *ip_list = NULL, dc_ip, exclude_ip; int count, i; @@ -177,10 +176,12 @@ BOOL rpc_find_dc(const char *domain, fstring srv_name, struct in_addr *ip_out) if ( use_pdc_only && get_pdc_ip(domain, &dc_ip) ) { - DEBUG(10,("rpc_find_dc: Atempting to lookup PDC to avoid sam sync delays\n")); + DEBUG(10,("get_dc_name: Atempting to lookup PDC to avoid sam sync delays\n")); - if (name_status_find(domain, 0x1c, 0x20, dc_ip, srv_name)) { - /* makre we we haven't tried this on previously and failed */ + /* check the connection cache and perform the node status + lookup only if the IP is not found to be bad */ + + if (name_status_find(domain, 0x1c, 0x20, dc_ip, srv_name) ) { result = check_negative_conn_cache( domain, srv_name ); if ( NT_STATUS_IS_OK(result) ) goto done; @@ -205,11 +206,71 @@ BOOL rpc_find_dc(const char *domain, fstring srv_name, struct in_addr *ip_out) } } - /* Pick a nice close server, but only if the list was not ordered */ - if (!list_ordered && (count > 1) ) { - qsort(ip_list, count, sizeof(struct in_addr), QSORT_CAST ip_compare); + if ( !list_ordered ) + { + /* + * Pick a nice close server. Look for DC on local net + * (assuming we don't have a list of preferred DC's) + */ + + for (i = 0; i < count; i++) { + if (is_zero_ip(ip_list[i])) + continue; + + if ( !is_local_net(ip_list[i]) ) + continue; + + if (name_status_find(domain, 0x1c, 0x20, ip_list[i], srv_name)) { + result = check_negative_conn_cache( domain, srv_name ); + if ( NT_STATUS_IS_OK(result) ) { + dc_ip = ip_list[i]; + goto done; + } + } + + zero_ip(&ip_list[i]); + } + + /* + * Try looking in the name status cache for an + * entry we already have. We know that already + * resolved ok. + */ + + for (i = 0; i < count; i++) { + if (is_zero_ip(ip_list[i])) + continue; + + if (namecache_status_fetch(domain, 0x1c, 0x20, + ip_list[i], srv_name)) { + result = check_negative_conn_cache( domain, srv_name ); + if ( NT_STATUS_IS_OK(result) ) { + dc_ip = ip_list[i]; + goto done; + } + } + } + + /* + * Secondly try and contact a random PDC/BDC. + */ + + i = (sys_random() % count); + + if ( !is_zero_ip(ip_list[i]) ) { + if ( name_status_find(domain, 0x1c, 0x20, ip_list[i], srv_name)) { + result = check_negative_conn_cache( domain, srv_name ); + if ( NT_STATUS_IS_OK(result) ) { + dc_ip = ip_list[i]; + goto done; + } + } + zero_ip(&ip_list[i]); /* Tried and failed. */ + } } + /* Finally return first DC that we can contact */ + for (i = 0; i < count; i++) { if (is_zero_ip(ip_list[i])) continue; @@ -220,20 +281,21 @@ BOOL rpc_find_dc(const char *domain, fstring srv_name, struct in_addr *ip_out) dc_ip = ip_list[i]; goto done; } - } + } } - SAFE_FREE(ip_list); - return False; -done: + /* No-one to talk to )-: */ + return False; /* Boo-hoo */ + + done: /* We have the netbios name and IP address of a domain controller. Ideally we should sent a SAMLOGON request to determine whether the DC is alive and kicking. If we can catch a dead DC before performing a cli_connect() we can avoid a 30-second timeout. */ - DEBUG(3, ("rpc_find_dc: Returning DC %s (%s) for domain %s\n", srv_name, + DEBUG(3, ("get_dc_name: Returning DC %s (%s) for domain %s\n", srv_name, inet_ntoa(dc_ip), domain)); *ip_out = dc_ip; @@ -242,4 +304,3 @@ done: return True; } - -- cgit