Unix SMB/Netbios implementation.
Version 1.9.
NBT client - used to lookup netbios names
- Copyright (C) Andrew Tridgell 1994-1995
+ Copyright (C) Andrew Tridgell 1994-1998
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
*/
-#ifdef SYSLOG
-#undef SYSLOG
-#endif
+#define NO_SYSLOG
#include "includes.h"
extern pstring scope;
-extern pstring myhostname;
extern struct in_addr ipzero;
-int ServerFD= -1;
+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)
{
- struct hostent *hp;
-
- /* get host info */
- if ((hp = Get_Hostbyname(myhostname)) == 0)
- {
- DEBUG(0,( "Get_Hostbyname: Unknown host. %s\n",myhostname));
- return False;
- }
-
- ServerFD = open_socket_in(SOCK_DGRAM, NMB_PORT,3);
+ ServerFD = open_socket_in( SOCK_DGRAM,
+ (RootPort ? 137 :0),
+ 3,
+ interpret_addr(lp_socket_address()), True );
if (ServerFD == -1)
return(False);
}
-/****************************************************************************
- initialise connect, service and file structs
-****************************************************************************/
-static BOOL init_structs(void )
-{
- if (!get_myname(myhostname,NULL))
- return(False);
-
- return True;
-}
-
/****************************************************************************
usage on the program
****************************************************************************/
printf("Version %s\n",VERSION);
printf("\t-d debuglevel set the debuglevel\n");
printf("\t-B broadcast address the address to use for broadcasts\n");
+ printf("\t-U unicast address the address to use for unicast\n");
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
****************************************************************************/
extern int optind;
extern char *optarg;
BOOL find_master=False;
- BOOL find_status=False;
int i;
-
+ static pstring servicesf = CONFIGFILE;
+ BOOL lookup_by_ip = False;
+ int commandline_debuglevel = -2;
+
DEBUGLEVEL = 1;
*lookup = 0;
charset_initialise();
- while ((opt = getopt(argc, argv, "p:d:B:i:SMh")) != 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':
+ bcast_addr = *interpret_addr2(optarg);
+ got_bcast = True;
+ use_bcast = False;
+ break;
+ case 'T':
+ translate_addresses = !translate_addresses;
break;
case 'i':
- strcpy(scope,optarg);
+ fstrcpy(scope,optarg);
strupper(scope);
break;
case 'M':
case 'S':
find_status = True;
break;
+ case 'R':
+ recursion_desired = True;
+ break;
case 'd':
- DEBUGLEVEL = atoi(optarg);
+ commandline_debuglevel = DEBUGLEVEL = atoi(optarg);
+ break;
+ case 's':
+ pstrcpy(servicesf, optarg);
break;
+ case 'r':
+ RootPort = -1;
+ break;
case 'h':
usage();
exit(0);
break;
+ case 'A':
+ lookup_by_ip = True;
+ break;
default:
usage();
exit(1);
exit(1);
}
- load_interfaces();
- init_structs();
- if (!open_sockets()) return(1);
+ if (!lp_load(servicesf,True,False,False)) {
+ fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
+ }
- DEBUG(1,("Sending queries to %s\n",inet_ntoa(*iface_bcast(ipzero))));
+ /*
+ * Ensure we reset DEBUGLEVEL if someone specified it
+ * on the command line.
+ */
+ if(commandline_debuglevel != -2)
+ DEBUGLEVEL = commandline_debuglevel;
+
+ load_interfaces();
+ if (!open_sockets()) return(1);
for (i=optind;i<argc;i++)
- {
- BOOL bcast = True;
- int retries = 2;
+ {
char *p;
struct in_addr ip;
- strcpy(lookup,argv[i]);
+ fstrcpy(lookup,argv[i]);
+
+ if(lookup_by_ip)
+ {
+ fstrcpy(lookup,"*");
+ ip = *interpret_addr2(argv[i]);
+ printf("Looking up status of %s\n",inet_ntoa(ip));
+ name_status(ServerFD,lookup,lookup_type,True,ip,NULL,NULL,NULL);
+ printf("\n");
+ continue;
+ }
if (find_master) {
if (*lookup == '-') {
- strcpy(lookup,"\01\02__MSBROWSE__\02");
+ fstrcpy(lookup,"\01\02__MSBROWSE__\02");
lookup_type = 1;
} else {
- lookup_type = 0x1b;
+ lookup_type = 0x1d;
}
}
p = strchr(lookup,'#');
if (p) {
- *p = 0;
- sscanf(p+1,"%x",&lookup_type);
- bcast = False;
- retries = 1;
+ *p = 0;
+ sscanf(p+1,"%x",&lookup_type);
}
- if (name_query(ServerFD,lookup,lookup_type,bcast,True,
- *iface_bcast(ipzero),&ip,NULL))
- {
- printf("%s %s\n",inet_ntoa(ip),lookup);
- if (find_status)
- {
- printf("Looking up status of %s\n",inet_ntoa(ip));
- name_status(ServerFD,lookup,lookup_type,True,ip,NULL,NULL,NULL);
- printf("\n");
- }
- } else {
- printf("couldn't find name %s\n",lookup);
+ if (!query_one(lookup, lookup_type)) {
+ printf("name_query failed to find name %s\n", lookup);
}
- }
-
+ }
+
return(0);
}