nmbd_browsesync.c
来自「samba-3.0.22.tar.gz 编译smb服务器的源码」· C语言 代码 · 共 680 行 · 第 1/2 页
C
680 行
zero_free(userdata, size);}/**************************************************************************** Function called when a query for a WORKGROUP<1b> name fails. ****************************************************************************/static void find_domain_master_name_query_fail(struct subnet_record *subrec, struct response_record *rrec, struct nmb_name *question_name, int fail_code){ if( DEBUGLVL( 0 ) ) { dbgtext( "find_domain_master_name_query_fail:\n" ); dbgtext( "Unable to find the Domain Master Browser name " ); dbgtext( "%s for the workgroup %s.\n", nmb_namestr(question_name), question_name->name ); dbgtext( "Unable to sync browse lists in this workgroup.\n" ); }}/****************************************************************************As a local master browser for a workgroup find the domain master browsername, announce ourselves as local master browser to it and then pull thefull domain browse lists from it onto the given subnet.**************************************************************************/void announce_and_sync_with_domain_master_browser( struct subnet_record *subrec, struct work_record *work){ /* Only do this if we are using a WINS server. */ if(we_are_a_wins_client() == False) { if( DEBUGLVL( 10 ) ) { dbgtext( "announce_and_sync_with_domain_master_browser:\n" ); dbgtext( "Ignoring, as we are not a WINS client.\n" ); } return; } /* First, query for the WORKGROUP<1b> name from the WINS server. */ query_name(unicast_subnet, work->work_group, 0x1b, find_domain_master_name_query_success, find_domain_master_name_query_fail, NULL);}/**************************************************************************** Function called when a node status query to a domain master browser IP succeeds. This function is only called on query to a Samba 1.9.18 or above WINS server. Note that adding the workgroup name is enough for this workgroup to be browsable by clients, as clients query the WINS server or broadcast nets for the WORKGROUP<1b> name when they want to browse a workgroup they are not in. We do not need to do a sync with this Domain Master Browser in order for our browse clients to see machines in this workgroup. JRA.****************************************************************************/static void get_domain_master_name_node_status_success(struct subnet_record *subrec, struct userdata_struct *userdata, struct res_rec *answers, struct in_addr from_ip){ struct work_record *work; unstring server_name; server_name[0] = 0; if( DEBUGLVL( 3 ) ) { dbgtext( "get_domain_master_name_node_status_success:\n" ); dbgtext( "Success in node status from ip %s\n", inet_ntoa(from_ip) ); } /* * Go through the list of names found at answers->rdata and look for * the first WORKGROUP<0x1b> name. */ if(answers->rdata != NULL) { char *p = answers->rdata; int numnames = CVAL(p, 0); p += 1; while (numnames--) { unstring qname; uint16 nb_flags; int name_type; pull_ascii_nstring(qname, sizeof(qname), p); name_type = CVAL(p,15); nb_flags = get_nb_flags(&p[16]); trim_char(qname,'\0',' '); p += 18; if(!(nb_flags & NB_GROUP) && (name_type == 0x00) && server_name[0] == 0) { /* this is almost certainly the server netbios name */ unstrcpy(server_name, qname); continue; } if(!(nb_flags & NB_GROUP) && (name_type == 0x1b)) { if( DEBUGLVL( 5 ) ) { dbgtext( "get_domain_master_name_node_status_success:\n" ); dbgtext( "%s(%s) ", server_name, inet_ntoa(from_ip) ); dbgtext( "is a domain master browser for workgroup " ); dbgtext( "%s. Adding this name.\n", qname ); } /* * If we don't already know about this workgroup, add it * to the workgroup list on the unicast_subnet. */ if((work = find_workgroup_on_subnet( subrec, qname)) == NULL) { struct nmb_name nmbname; /* * Add it - with an hour in the cache. */ if(!(work= create_workgroup_on_subnet(subrec, qname, 60*60))) return; /* remember who the master is */ unstrcpy(work->local_master_browser_name, server_name); make_nmb_name(&nmbname, server_name, 0x20); work->dmb_name = nmbname; work->dmb_addr = from_ip; } break; } } } else if( DEBUGLVL( 0 ) ) { dbgtext( "get_domain_master_name_node_status_success:\n" ); dbgtext( "Failed to find a WORKGROUP<0x1b> name in reply from IP " ); dbgtext( "%s.\n", inet_ntoa(from_ip) ); }}/**************************************************************************** Function called when a node status query to a domain master browser IP fails.****************************************************************************/static void get_domain_master_name_node_status_fail(struct subnet_record *subrec, struct response_record *rrec){ if( DEBUGLVL( 0 ) ) { dbgtext( "get_domain_master_name_node_status_fail:\n" ); dbgtext( "Doing a node status request to the domain master browser " ); dbgtext( "at IP %s failed.\n", inet_ntoa(rrec->packet->ip) ); dbgtext( "Cannot get workgroup name.\n" ); }}/**************************************************************************** Function called when a query for *<1b> name succeeds.****************************************************************************/static void find_all_domain_master_names_query_success(struct subnet_record *subrec, struct userdata_struct *userdata_in, struct nmb_name *q_name, struct in_addr answer_ip, struct res_rec *rrec){ /* * We now have a list of all the domain master browsers for all workgroups * that have registered with the WINS server. Now do a node status request * to each one and look for the first 1b name in the reply. This will be * the workgroup name that we will add to the unicast subnet as a 'non-local' * workgroup. */ struct nmb_name nmbname; struct in_addr send_ip; int i; if( DEBUGLVL( 5 ) ) { dbgtext( "find_all_domain_master_names_query_succes:\n" ); dbgtext( "Got answer from WINS server of %d ", (rrec->rdlength / 6) ); dbgtext( "IP addresses for Domain Master Browsers.\n" ); } for(i = 0; i < rrec->rdlength / 6; i++) { /* Initiate the node status requests. */ make_nmb_name(&nmbname, "*", 0); putip((char *)&send_ip, (char *)&rrec->rdata[(i*6) + 2]); /* * Don't send node status requests to ourself. */ if(ismyip( send_ip )) { if( DEBUGLVL( 5 ) ) { dbgtext( "find_all_domain_master_names_query_succes:\n" ); dbgtext( "Not sending node status to our own IP " ); dbgtext( "%s.\n", inet_ntoa(send_ip) ); } continue; } if( DEBUGLVL( 5 ) ) { dbgtext( "find_all_domain_master_names_query_success:\n" ); dbgtext( "Sending node status request to IP %s.\n", inet_ntoa(send_ip) ); } node_status( subrec, &nmbname, send_ip, get_domain_master_name_node_status_success, get_domain_master_name_node_status_fail, NULL); }}/**************************************************************************** Function called when a query for *<1b> name fails. ****************************************************************************/static void find_all_domain_master_names_query_fail(struct subnet_record *subrec, struct response_record *rrec, struct nmb_name *question_name, int fail_code){ if( DEBUGLVL( 10 ) ) { dbgtext( "find_domain_master_name_query_fail:\n" ); dbgtext( "WINS server did not reply to a query for name " ); dbgtext( "%s.\nThis means it ", nmb_namestr(question_name) ); dbgtext( "is probably not a Samba 1.9.18 or above WINS server.\n" ); }}/**************************************************************************** If we are a domain master browser on the unicast subnet, do a query to the WINS server for the *<1b> name. This will only work to a Samba WINS server, so ignore it if we fail. If we succeed, contact each of the IP addresses in turn and do a node status request to them. If this succeeds then look for a <1b> name in the reply - this is the workgroup name. Add this to the unicast subnet. This is expensive, so we only do this every 15 minutes.**************************************************************************/void collect_all_workgroup_names_from_wins_server(time_t t){ static time_t lastrun = 0; struct work_record *work; /* Only do this if we are using a WINS server. */ if(we_are_a_wins_client() == False) return; /* Check to see if we are a domain master browser on the unicast subnet. */ if((work = find_workgroup_on_subnet( unicast_subnet, lp_workgroup())) == NULL) { if( DEBUGLVL( 0 ) ) { dbgtext( "collect_all_workgroup_names_from_wins_server:\n" ); dbgtext( "Cannot find my workgroup %s ", lp_workgroup() ); dbgtext( "on subnet %s.\n", unicast_subnet->subnet_name ); } return; } if(!AM_DOMAIN_MASTER_BROWSER(work)) return; if ((lastrun != 0) && (t < lastrun + (15 * 60))) return; lastrun = t; /* First, query for the *<1b> name from the WINS server. */ query_name(unicast_subnet, "*", 0x1b, find_all_domain_master_names_query_success, find_all_domain_master_names_query_fail, NULL);} /**************************************************************************** If we are a domain master browser on the unicast subnet, do a regular sync with all other DMBs that we know of on that subnet.To prevent exponential network traffic with large numbers of workgroupswe use a randomised system where sync probability is inversely proportionalto the number of known workgroups**************************************************************************/void sync_all_dmbs(time_t t){ static time_t lastrun = 0; struct work_record *work; int count=0; /* Only do this if we are using a WINS server. */ if(we_are_a_wins_client() == False) return; /* Check to see if we are a domain master browser on the unicast subnet. */ work = find_workgroup_on_subnet(unicast_subnet, lp_workgroup()); if (!work) return; if (!AM_DOMAIN_MASTER_BROWSER(work)) return; if ((lastrun != 0) && (t < lastrun + (5 * 60))) return; /* count how many syncs we might need to do */ for (work=unicast_subnet->workgrouplist; work; work = work->next) { if (strcmp(lp_workgroup(), work->work_group)) { count++; } } /* sync with a probability of 1/count */ for (work=unicast_subnet->workgrouplist; work; work = work->next) { if (strcmp(lp_workgroup(), work->work_group)) { unstring dmb_name; if (((unsigned)sys_random()) % count != 0) continue; lastrun = t; if (!work->dmb_name.name[0]) { /* we don't know the DMB - assume it is the same as the unicast local master */ make_nmb_name(&work->dmb_name, work->local_master_browser_name, 0x20); } pull_ascii_nstring(dmb_name, sizeof(dmb_name), work->dmb_name.name); DEBUG(3,("Initiating DMB<->DMB sync with %s(%s)\n", dmb_name, inet_ntoa(work->dmb_addr))); sync_browse_lists(work, dmb_name, work->dmb_name.name_type, work->dmb_addr, False, False); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?