diff options
Diffstat (limited to 'source3/utils/nmblookup.c')
-rw-r--r-- | source3/utils/nmblookup.c | 151 |
1 files changed, 88 insertions, 63 deletions
diff --git a/source3/utils/nmblookup.c b/source3/utils/nmblookup.c index f920106f753..2f9bbc6c539 100644 --- a/source3/utils/nmblookup.c +++ b/source3/utils/nmblookup.c @@ -28,48 +28,37 @@ extern int DEBUGLEVEL; extern pstring scope; -extern pstring myhostname; extern struct in_addr ipzero; -int ServerFD= -1; - -BOOL RootPort = False; +static BOOL use_bcast = True; +static BOOL got_bcast = False; +static struct in_addr bcast_addr; +static BOOL recursion_desired = False; +static BOOL translate_addresses = False; +static int ServerFD= -1; +static int RootPort = 0; +static BOOL find_status=False; /**************************************************************************** open the socket communication **************************************************************************/ static BOOL open_sockets(void) { - if (RootPort) - { - ServerFD = open_socket_in( SOCK_DGRAM, - 137, - 3, - interpret_addr(lp_socket_address()) ); - - if (ServerFD == -1) - { - return(False); - } + ServerFD = open_socket_in( SOCK_DGRAM, + (RootPort ? 137 :0), + 3, + interpret_addr(lp_socket_address()), True ); - set_socket_options(ServerFD,"SO_BROADCAST"); - DEBUG(3, ("Socket opened.\n")); - } - return True; -} - - -/**************************************************************************** - initialise connect, service and file structs -****************************************************************************/ -static BOOL init_structs(void ) -{ - if (!get_myname(myhostname,NULL)) + if (ServerFD == -1) return(False); + set_socket_options(ServerFD,"SO_BROADCAST"); + + DEBUG(3, ("Socket opened.\n")); return True; } + /**************************************************************************** usage on the program ****************************************************************************/ @@ -83,16 +72,71 @@ static void usage(void) printf("\t-M searches for a master browser\n"); printf("\t-R set recursion desired in packet\n"); printf("\t-S lookup node status as well\n"); + printf("\t-T translate IP addresses into names\n"); printf("\t-r Use root port 137 (Win95 only replies to this)\n"); printf("\t-A Do a node status on <name> as an IP Address\n"); printf("\t-i NetBIOS scope Use the given NetBIOS scope for name queries\n"); printf("\t-s smb.conf file Use the given path to the smb.conf file\n"); printf("\t-h Print this help message.\n"); + printf("\n If you specify -M and name is \"-\", nmblookup looks up __MSBROWSE__<01>\n"); printf("\n"); } /**************************************************************************** +send out one query +****************************************************************************/ +static BOOL query_one(char *lookup, unsigned int lookup_type) +{ + int j, count; + struct in_addr *ip_list=NULL; + + if (got_bcast) { + printf("querying %s on %s\n", lookup, inet_ntoa(bcast_addr)); + ip_list = name_query(ServerFD,lookup,lookup_type,use_bcast, + use_bcast?True:recursion_desired, + bcast_addr,&count,NULL); + } else { + struct in_addr *bcast; + for (j=iface_count() - 1; + !ip_list && j >= 0; + j--) { + bcast = iface_n_bcast(j); + printf("querying %s on %s\n", + lookup, inet_ntoa(*bcast)); + ip_list = name_query(ServerFD,lookup,lookup_type, + use_bcast, + use_bcast?True:recursion_desired, + *bcast,&count,NULL); + } + } + + if (!ip_list) return False; + + for (j=0;j<count;j++) { + if (translate_addresses) { + struct hostent *host = gethostbyaddr((char *)&ip_list[j], sizeof(ip_list[j]), AF_INET); + if (host) { + printf("%s, ", host -> h_name); + } + } + printf("%s %s<%02x>\n",inet_ntoa(ip_list[j]),lookup, lookup_type); + } + + /* We can only do find_status if the ip address returned + was valid - ie. name_query returned true. + */ + if (find_status) { + printf("Looking up status of %s\n",inet_ntoa(ip_list[0])); + name_status(ServerFD,lookup,lookup_type,True,ip_list[0],NULL,NULL,NULL); + printf("\n"); + } + + return (ip_list != NULL); +} + + +/**************************************************************************** main program ****************************************************************************/ int main(int argc,char *argv[]) @@ -103,14 +147,10 @@ int main(int argc,char *argv[]) extern int optind; extern char *optarg; BOOL find_master=False; - BOOL find_status=False; int i; static pstring servicesf = CONFIGFILE; - struct in_addr bcast_addr; - BOOL use_bcast = True; - BOOL got_bcast = False; BOOL lookup_by_ip = False; - BOOL recursion_desired = False; + int commandline_debuglevel = -2; DEBUGLEVEL = 1; *lookup = 0; @@ -121,21 +161,22 @@ int main(int argc,char *argv[]) charset_initialise(); - while ((opt = getopt(argc, argv, "d:B:U:i:s:SMrhAR")) != EOF) + while ((opt = getopt(argc, argv, "d:B:U:i:s:SMrhART")) != EOF) switch (opt) { case 'B': - iface_set_default(NULL,optarg,NULL); bcast_addr = *interpret_addr2(optarg); got_bcast = True; use_bcast = True; break; case 'U': - iface_set_default(NULL,optarg,NULL); bcast_addr = *interpret_addr2(optarg); got_bcast = True; use_bcast = False; break; + case 'T': + translate_addresses = !translate_addresses; + break; case 'i': fstrcpy(scope,optarg); strupper(scope); @@ -150,13 +191,13 @@ int main(int argc,char *argv[]) recursion_desired = True; break; case 'd': - DEBUGLEVEL = atoi(optarg); + commandline_debuglevel = DEBUGLEVEL = atoi(optarg); break; case 's': pstrcpy(servicesf, optarg); break; case 'r': - RootPort = True; + RootPort = -1; break; case 'h': usage(); @@ -175,27 +216,25 @@ int main(int argc,char *argv[]) exit(1); } - init_structs(); - if (!lp_load(servicesf,True,False,False)) { fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf); } - load_interfaces(); - if (!open_sockets()) return(1); - - if (!got_bcast) - bcast_addr = *iface_bcast(ipzero); + /* + * Ensure we reset DEBUGLEVEL if someone specified it + * on the command line. + */ - DEBUG(1,("Sending queries to %s\n",inet_ntoa(bcast_addr))); + if(commandline_debuglevel != -2) + DEBUGLEVEL = commandline_debuglevel; + load_interfaces(); + if (!open_sockets()) return(1); for (i=optind;i<argc;i++) { - int j, count; char *p; struct in_addr ip; - struct in_addr *ip_list; fstrcpy(lookup,argv[i]); @@ -225,21 +264,7 @@ int main(int argc,char *argv[]) sscanf(p+1,"%x",&lookup_type); } - if ((ip_list = name_query(ServerFD,lookup,lookup_type,use_bcast, - use_bcast?True:recursion_desired, - bcast_addr,&count,NULL))) { - for (j=0;j<count;j++) - printf("%s %s<%02x>\n",inet_ntoa(ip_list[j]),lookup, lookup_type); - - /* We can only do find_status if the ip address returned - was valid - ie. name_query returned true. - */ - if (find_status) { - printf("Looking up status of %s\n",inet_ntoa(ip_list[0])); - name_status(ServerFD,lookup,lookup_type,True,ip_list[0],NULL,NULL,NULL); - printf("\n"); - } - } else { + if (!query_one(lookup, lookup_type)) { printf("name_query failed to find name %s\n", lookup); } } |