📄 namequery.c
字号:
if (!tmp_ip_list) { DEBUG(0,("name_query: Realloc failed.\n")); SAFE_FREE(ip_list); } ip_list = tmp_ip_list; if (ip_list) { DEBUG(2,("Got a positive name query response from %s ( ", inet_ntoa(p2->ip))); for (i=0;i<nmb2->answers->rdlength/6;i++) { putip((char *)&ip_list[(*count)],&nmb2->answers->rdata[2+i*6]); DEBUGADD(2,("%s ",inet_ntoa(ip_list[(*count)]))); (*count)++; } DEBUGADD(2,(")\n")); } found=True; retries=0; /* We add the flags back ... */ if (nmb2->header.response) (*flags) |= NM_FLAGS_RS; if (nmb2->header.nm_flags.authoritative) (*flags) |= NM_FLAGS_AA; if (nmb2->header.nm_flags.trunc) (*flags) |= NM_FLAGS_TC; if (nmb2->header.nm_flags.recursion_desired) (*flags) |= NM_FLAGS_RD; if (nmb2->header.nm_flags.recursion_available) (*flags) |= NM_FLAGS_RA; if (nmb2->header.nm_flags.bcast) (*flags) |= NM_FLAGS_B; free_packet(p2); /* * If we're doing a unicast lookup we only * expect one reply. Don't wait the full 2 * seconds if we got one. JRA. */ if(!bcast && found) break; } } /* only set timed_out if we didn't fund what we where looking for*/ if ( !found && timed_out ) { *timed_out = True; } /* sort the ip list so we choose close servers first if possible */ sort_ip_list(ip_list, *count); return ip_list;}/******************************************************** Start parsing the lmhosts file.*********************************************************/XFILE *startlmhosts(char *fname){ XFILE *fp = x_fopen(fname,O_RDONLY, 0); if (!fp) { DEBUG(4,("startlmhosts: Can't open lmhosts file %s. Error was %s\n", fname, strerror(errno))); return NULL; } return fp;}/******************************************************** Parse the next line in the lmhosts file.*********************************************************/BOOL getlmhostsent( XFILE *fp, pstring name, int *name_type, struct in_addr *ipaddr){ pstring line; while(!x_feof(fp) && !x_ferror(fp)) { pstring ip,flags,extra; const char *ptr; char *ptr1; int count = 0; *name_type = -1; if (!fgets_slash(line,sizeof(pstring),fp)) { continue; } if (*line == '#') { continue; } pstrcpy(ip,""); pstrcpy(name,""); pstrcpy(flags,""); ptr = line; if (next_token(&ptr,ip ,NULL,sizeof(ip))) ++count; if (next_token(&ptr,name ,NULL, sizeof(pstring))) ++count; if (next_token(&ptr,flags,NULL, sizeof(flags))) ++count; if (next_token(&ptr,extra,NULL, sizeof(extra))) ++count; if (count <= 0) continue; if (count > 0 && count < 2) { DEBUG(0,("getlmhostsent: Ill formed hosts line [%s]\n",line)); continue; } if (count >= 4) { DEBUG(0,("getlmhostsent: too many columns in lmhosts file (obsolete syntax)\n")); continue; } DEBUG(4, ("getlmhostsent: lmhost entry: %s %s %s\n", ip, name, flags)); if (strchr_m(flags,'G') || strchr_m(flags,'S')) { DEBUG(0,("getlmhostsent: group flag in lmhosts ignored (obsolete)\n")); continue; } *ipaddr = *interpret_addr2(ip); /* Extra feature. If the name ends in '#XX', where XX is a hex number, then only add that name type. */ if((ptr1 = strchr_m(name, '#')) != NULL) { char *endptr; ptr1++; *name_type = (int)strtol(ptr1, &endptr, 16); if(!*ptr1 || (endptr == ptr1)) { DEBUG(0,("getlmhostsent: invalid name %s containing '#'.\n", name)); continue; } *(--ptr1) = '\0'; /* Truncate at the '#' */ } return True; } return False;}/******************************************************** Finish parsing the lmhosts file.*********************************************************/void endlmhosts(XFILE *fp){ x_fclose(fp);}/******************************************************** convert an array if struct in_addrs to struct ip_service return False on failure. Port is set to PORT_NONE;*********************************************************/static BOOL convert_ip2service( struct ip_service **return_iplist, struct in_addr *ip_list, int count ){ int i; if ( count==0 || !ip_list ) return False; /* copy the ip address; port will be PORT_NONE */ if ( (*return_iplist = SMB_MALLOC_ARRAY(struct ip_service, count)) == NULL ) { DEBUG(0,("convert_ip2service: malloc failed for %d enetries!\n", count )); return False; } for ( i=0; i<count; i++ ) { (*return_iplist)[i].ip = ip_list[i]; (*return_iplist)[i].port = PORT_NONE; } return True;} /******************************************************** Resolve via "bcast" method.*********************************************************/BOOL name_resolve_bcast(const char *name, int name_type, struct ip_service **return_iplist, int *return_count){ int sock, i; int num_interfaces = iface_count(); struct in_addr *ip_list; BOOL ret; if (lp_disable_netbios()) { DEBUG(5,("name_resolve_bcast(%s#%02x): netbios is disabled\n", name, name_type)); return False; } *return_iplist = NULL; *return_count = 0; /* * "bcast" means do a broadcast lookup on all the local interfaces. */ DEBUG(3,("name_resolve_bcast: Attempting broadcast lookup for name %s<0x%x>\n", name, name_type)); sock = open_socket_in( SOCK_DGRAM, 0, 3, interpret_addr(lp_socket_address()), True ); if (sock == -1) return False; set_socket_options(sock,"SO_BROADCAST"); /* * Lookup the name on all the interfaces, return on * the first successful match. */ for( i = num_interfaces-1; i >= 0; i--) { struct in_addr sendto_ip; int flags; /* Done this way to fix compiler error on IRIX 5.x */ sendto_ip = *iface_n_bcast(i); ip_list = name_query(sock, name, name_type, True, True, sendto_ip, return_count, &flags, NULL); if( ip_list ) goto success; } /* failed - no response */ close(sock); return False; success: ret = True; if ( !convert_ip2service(return_iplist, ip_list, *return_count) ) ret = False; SAFE_FREE( ip_list ); close(sock); return ret;}/******************************************************** Resolve via "wins" method.*********************************************************/BOOL resolve_wins(const char *name, int name_type, struct ip_service **return_iplist, int *return_count){ int sock, t, i; char **wins_tags; struct in_addr src_ip, *ip_list = NULL; BOOL ret; if (lp_disable_netbios()) { DEBUG(5,("resolve_wins(%s#%02x): netbios is disabled\n", name, name_type)); return False; } *return_iplist = NULL; *return_count = 0; DEBUG(3,("resolve_wins: Attempting wins lookup for name %s<0x%x>\n", name, name_type)); if (wins_srv_count() < 1) { DEBUG(3,("resolve_wins: WINS server resolution selected and no WINS servers listed.\n")); return False; } /* we try a lookup on each of the WINS tags in turn */ wins_tags = wins_srv_tags(); if (!wins_tags) { /* huh? no tags?? give up in disgust */ return False; } /* the address we will be sending from */ src_ip = *interpret_addr2(lp_socket_address()); /* in the worst case we will try every wins server with every tag! */ for (t=0; wins_tags && wins_tags[t]; t++) { int srv_count = wins_srv_count_tag(wins_tags[t]); for (i=0; i<srv_count; i++) { struct in_addr wins_ip; int flags; BOOL timed_out; wins_ip = wins_srv_ip_tag(wins_tags[t], src_ip); if (global_in_nmbd && ismyip(wins_ip)) { /* yikes! we'll loop forever */ continue; } /* skip any that have been unresponsive lately */ if (wins_srv_is_dead(wins_ip, src_ip)) { continue; } DEBUG(3,("resolve_wins: using WINS server %s and tag '%s'\n", inet_ntoa(wins_ip), wins_tags[t])); sock = open_socket_in(SOCK_DGRAM, 0, 3, src_ip.s_addr, True); if (sock == -1) { continue; } ip_list = name_query(sock,name,name_type, False, True, wins_ip, return_count, &flags, &timed_out); /* exit loop if we got a list of addresses */ if ( ip_list ) goto success; close(sock); if (timed_out) { /* Timed out wating for WINS server to respond. Mark it dead. */ wins_srv_died(wins_ip, src_ip); } else { /* The name definately isn't in this group of WINS servers. goto the next group */ break; } } } wins_srv_tags_free(wins_tags); return False;success: ret = True; if ( !convert_ip2service( return_iplist, ip_list, *return_count ) ) ret = False; SAFE_FREE( ip_list ); wins_srv_tags_free(wins_tags); close(sock); return ret;}/******************************************************** Resolve via "lmhosts" method.*********************************************************/static BOOL resolve_lmhosts(const char *name, int name_type, struct ip_service **return_iplist, int *return_count){ /* * "lmhosts" means parse the local lmhosts file. */ XFILE *fp; pstring lmhost_name; int name_type2; struct in_addr return_ip; BOOL result = False; *return_iplist = NULL; *return_count = 0; DEBUG(3,("resolve_lmhosts: Attempting lmhosts lookup for name %s<0x%x>\n", name, name_type)); fp = startlmhosts(dyn_LMHOSTSFILE); if ( fp == NULL ) return False; while (getlmhostsent(fp, lmhost_name, &name_type2, &return_ip)) { if (!strequal(name, lmhost_name)) continue; if ((name_type2 != -1) && (name_type != name_type2)) continue; *return_iplist = SMB_REALLOC_ARRAY((*return_iplist), struct ip_service, (*return_count)+1); if ((*return_iplist) == NULL) { DEBUG(3,("resolve_lmhosts: malloc fail !\n")); return False; } (*return_iplist)[*return_count].ip = return_ip; (*return_iplist)[*return_count].port = PORT_NONE; *return_count += 1; /* we found something */ result = True; /* Multiple names only for DC lookup */ if (name_type != 0x1c) break; } endlmhosts(fp); return result;}/******************************************************** Resolve via "hosts" method.*********************************************************/static BOOL resolve_hosts(const char *name, int name_type, struct ip_service **return_iplist, int *return_count){ /* * "host" means do a localhost, or dns lookup. */ struct hostent *hp; if ( name_type != 0x20 && name_type != 0x0) { DEBUG(5, ("resolve_hosts: not appropriate for name type <0x%x>\n", name_type)); return False; } *return_iplist = NULL; *return_count = 0; DEBUG(3,("resolve_hosts: Attempting host lookup for name %s<0x%x>\n", name, name_type)); if (((hp = sys_gethostbyname(name)) != NULL) && (hp->h_addr != NULL)) { struct in_addr return_ip; putip((char *)&return_ip,(char *)hp->h_addr); *return_iplist = SMB_MALLOC_P(struct ip_service); if(*return_iplist == NULL) { DEBUG(3,("resolve_hosts: malloc fail !\n")); return False; } (*return_iplist)->ip = return_ip; (*return_iplist)->port = PORT_NONE; *return_count = 1; return True; } return False;}/******************************************************** Resolve via "ADS" method.*********************************************************/static BOOL resolve_ads(const char *name, int name_type, struct ip_service **return_iplist, int *return_count){ #ifdef HAVE_ADS if ( name_type == 0x1c ) { int count, i = 0; char *list = NULL; const char *ptr; pstring tok; /* try to lookup the _ldap._tcp.<domain> if we are using ADS */ if ( lp_security() != SEC_ADS ) return False; DEBUG(5,("resolve_hosts: Attempting to resolve DC's for %s using DNS\n", name)); if (ldap_domain2hostlist(name, &list) != LDAP_SUCCESS) return False; count = count_chars(list, ' ') + 1; if ( (*return_iplist = SMB_MALLOC_ARRAY(struct ip_service, count)) == NULL ) { DEBUG(0,("resolve_hosts: malloc failed for %d entries\n", count )); return False; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -