📄 namequery.c
字号:
*********************************************************/FILE *startlmhosts(char *fname){ FILE *fp = sys_fopen(fname,"r"); if (!fp) { DEBUG(4,("startlmhosts: Cannot 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( FILE *fp, pstring name, int *name_type, struct in_addr *ipaddr){ pstring line; while(!feof(fp) && !ferror(fp)) { pstring ip,flags,extra; char *ptr; 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(flags,'G') || strchr(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((ptr = strchr(name, '#')) != NULL) { char *endptr; ptr++; *name_type = (int)strtol(ptr, &endptr, 16); if(!*ptr || (endptr == ptr)) { DEBUG(0,("getlmhostsent: invalid name %s containing '#'.\n", name)); continue; } *(--ptr) = '\0'; /* Truncate at the '#' */ } return True; } return False;}/******************************************************** Finish parsing the lmhosts file.*********************************************************/void endlmhosts(FILE *fp){ fclose(fp);}/********************************************************resolve via "bcast" method*********************************************************/static BOOL resolve_bcast(const char *name, struct in_addr *return_ip, int name_type){ int sock, i; /* * "bcast" means do a broadcast lookup on all the local interfaces. */ DEBUG(3,("resolve_name: 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) { struct in_addr *iplist = NULL; int count; int num_interfaces = iface_count(); set_socket_options(sock,"SO_BROADCAST"); /* * Lookup the name on all the interfaces, return on * the first successful match. */ for( i = 0; i < num_interfaces; i++) { struct in_addr sendto_ip; /* Done this way to fix compiler error on IRIX 5.x */ sendto_ip = *iface_bcast(*iface_n_ip(i)); iplist = name_query(sock, name, name_type, True, True, sendto_ip, &count, NULL); if(iplist != NULL) { *return_ip = iplist[0]; free((char *)iplist); close(sock); return True; } } close(sock); } return False;}/********************************************************resolve via "wins" method*********************************************************/static BOOL resolve_wins(const char *name, struct in_addr *return_ip, int name_type){ int sock; struct in_addr wins_ip; BOOL wins_ismyip; /* * "wins" means do a unicast lookup to the WINS server. * Ignore if there is no WINS server specified or if the * WINS server is one of our interfaces (if we're being * called from within nmbd - we can't do this call as we * would then block). */ DEBUG(3,("resolve_name: Attempting wins lookup for name %s<0x%x>\n", name, name_type)); if(!*lp_wins_server()) { DEBUG(3,("resolve_name: WINS server resolution selected and no WINS server present.\n")); return False; } wins_ip = *interpret_addr2(lp_wins_server()); wins_ismyip = ismyip(wins_ip); if((wins_ismyip && !global_in_nmbd) || !wins_ismyip) { sock = open_socket_in( SOCK_DGRAM, 0, 3, interpret_addr(lp_socket_address()), True ); if (sock != -1) { struct in_addr *iplist = NULL; int count; iplist = name_query(sock, name, name_type, False, True, wins_ip, &count, NULL); if(iplist != NULL) { *return_ip = iplist[0]; free((char *)iplist); close(sock); return True; } close(sock); } } return False;}/********************************************************resolve via "lmhosts" method*********************************************************/static BOOL resolve_lmhosts(const char *name, struct in_addr *return_ip, int name_type){ /* * "lmhosts" means parse the local lmhosts file. */ FILE *fp; pstring lmhost_name; int name_type2; DEBUG(3,("resolve_name: Attempting lmhosts lookup for name %s<0x%x>\n", name, name_type)); fp = startlmhosts( LMHOSTSFILE ); if(fp) { while (getlmhostsent(fp, lmhost_name, &name_type2, return_ip)) { if (strequal(name, lmhost_name) && ((name_type2 == -1) || (name_type == name_type2)) ) { endlmhosts(fp); return True; } } endlmhosts(fp); } return False;}/********************************************************resolve via "hosts" method*********************************************************/static BOOL resolve_hosts(const char *name, struct in_addr *return_ip){ /* * "host" means do a localhost, or dns lookup. */ struct hostent *hp; DEBUG(3,("resolve_name: Attempting host lookup for name %s<0x20>\n", name)); if (((hp = Get_Hostbyname(name)) != NULL) && (hp->h_addr != NULL)) { putip((char *)return_ip,(char *)hp->h_addr); return True; } return False;}/******************************************************** Resolve a name into an IP address. Use this function if the string is either an IP address, DNS or host name or NetBIOS name. This uses the name switch in the smb.conf to determine the order of name resolution.*********************************************************/BOOL resolve_name(const char *name, struct in_addr *return_ip, int name_type){ int i; BOOL pure_address = True; pstring name_resolve_list; fstring tok; char *ptr; if (strcmp(name,"0.0.0.0") == 0) { return_ip->s_addr = 0; return True; } if (strcmp(name,"255.255.255.255") == 0) { return_ip->s_addr = 0xFFFFFFFF; return True; } for (i=0; pure_address && name[i]; i++) if (!(isdigit((int)name[i]) || name[i] == '.')) pure_address = False; /* if it's in the form of an IP address then get the lib to interpret it */ if (pure_address) { return_ip->s_addr = inet_addr(name); return True; } pstrcpy(name_resolve_list, lp_name_resolve_order()); ptr = name_resolve_list; if (!ptr || !*ptr) ptr = "host"; while (next_token(&ptr, tok, LIST_SEP, sizeof(tok))) { if((strequal(tok, "host") || strequal(tok, "hosts"))) { if (name_type == 0x20 && resolve_hosts(name, return_ip)) { return True; } } else if(strequal( tok, "lmhosts")) { if (resolve_lmhosts(name, return_ip, name_type)) { return True; } } else if(strequal( tok, "wins")) { /* don't resolve 1D via WINS */ if (name_type != 0x1D && resolve_wins(name, return_ip, name_type)) { return True; } } else if(strequal( tok, "bcast")) { if (resolve_bcast(name, return_ip, name_type)) { return True; } } else { DEBUG(0,("resolve_name: unknown name switch type %s\n", tok)); } } return False;}/********************************************************find the IP address of the master browser or DMB for a workgroup*********************************************************/BOOL find_master_ip(char *group, struct in_addr *master_ip){ if (resolve_name(group, master_ip, 0x1D)) return True; return resolve_name(group, master_ip, 0x1B);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -