1996-05-04 11:50:46 +04:00
/*
Unix SMB / Netbios implementation .
Version 1.9 .
NBT client - used to lookup netbios names
1997-05-08 05:14:17 +04:00
Copyright ( C ) Andrew Tridgell 1994 - 1997
1996-05-04 11:50:46 +04:00
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
the Free Software Foundation ; either version 2 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program ; if not , write to the Free Software
Foundation , Inc . , 675 Mass Ave , Cambridge , MA 0213 9 , USA .
*/
# ifdef SYSLOG
# undef SYSLOG
# endif
# include "includes.h"
extern int DEBUGLEVEL ;
extern pstring scope ;
extern pstring myhostname ;
1996-06-06 15:43:09 +04:00
extern struct in_addr ipzero ;
1996-05-04 11:50:46 +04:00
int ServerFD = - 1 ;
1997-10-04 05:07:47 +04:00
int RootPort = 0 ;
1996-05-04 11:50:46 +04:00
/****************************************************************************
open the socket communication
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static BOOL open_sockets ( void )
{
1997-10-04 05:07:47 +04:00
ServerFD = open_socket_in ( SOCK_DGRAM ,
( RootPort ? 137 : 0 ) ,
3 ,
interpret_addr ( lp_socket_address ( ) ) ) ;
1996-05-04 11:50:46 +04:00
if ( ServerFD = = - 1 )
return ( False ) ;
set_socket_options ( ServerFD , " SO_BROADCAST " ) ;
DEBUG ( 3 , ( " Socket opened. \n " ) ) ;
return True ;
}
/****************************************************************************
initialise connect , service and file structs
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static BOOL init_structs ( void )
{
1996-06-06 15:43:09 +04:00
if ( ! get_myname ( myhostname , NULL ) )
1996-05-04 11:50:46 +04:00
return ( False ) ;
return True ;
}
/****************************************************************************
usage on the program
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static void usage ( void )
{
printf ( " Usage: nmblookup [-M] [-B bcast address] [-d debuglevel] name \n " ) ;
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 " ) ;
1997-11-20 02:50:02 +03:00
printf ( " \t -U unicast address the address to use for unicast \n " ) ;
1996-05-04 11:50:46 +04:00
printf ( " \t -M searches for a master browser \n " ) ;
1997-11-20 02:50:02 +03:00
printf ( " \t -R set recursion desired in packet \n " ) ;
1996-05-04 11:50:46 +04:00
printf ( " \t -S lookup node status as well \n " ) ;
1997-10-10 05:32:26 +04:00
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 " ) ;
1996-05-04 11:50:46 +04:00
printf ( " \n " ) ;
}
/****************************************************************************
main program
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int main ( int argc , char * argv [ ] )
{
int opt ;
1996-06-29 22:49:20 +04:00
unsigned int lookup_type = 0x0 ;
1996-05-04 11:50:46 +04:00
pstring lookup ;
extern int optind ;
extern char * optarg ;
BOOL find_master = False ;
BOOL find_status = False ;
int i ;
1996-10-02 18:09:22 +04:00
static pstring servicesf = CONFIGFILE ;
1996-10-07 15:06:34 +04:00
struct in_addr bcast_addr ;
1997-10-26 10:50:28 +03:00
BOOL use_bcast = True ;
1996-10-07 15:06:34 +04:00
BOOL got_bcast = False ;
1997-10-10 05:32:26 +04:00
BOOL lookup_by_ip = False ;
1997-11-20 02:50:02 +03:00
BOOL recursion_desired = False ;
1996-05-04 11:50:46 +04:00
DEBUGLEVEL = 1 ;
* lookup = 0 ;
TimeInit ( ) ;
setup_logging ( argv [ 0 ] , True ) ;
1997-07-19 00:21:32 +04:00
charset_initialise ( ) ;
1996-05-04 11:50:46 +04:00
1997-11-20 02:50:02 +03:00
while ( ( opt = getopt ( argc , argv , " d:B:U:i:s:SMrhAR " ) ) ! = EOF )
1996-05-04 11:50:46 +04:00
switch ( opt )
{
case ' B ' :
1996-06-06 15:43:09 +04:00
iface_set_default ( NULL , optarg , NULL ) ;
1996-10-07 15:06:34 +04:00
bcast_addr = * interpret_addr2 ( optarg ) ;
got_bcast = True ;
1997-10-26 10:50:28 +03:00
use_bcast = True ;
break ;
case ' U ' :
iface_set_default ( NULL , optarg , NULL ) ;
bcast_addr = * interpret_addr2 ( optarg ) ;
got_bcast = True ;
use_bcast = False ;
1996-05-04 11:50:46 +04:00
break ;
case ' i ' :
1997-09-26 22:55:29 +04:00
fstrcpy ( scope , optarg ) ;
1996-05-04 11:50:46 +04:00
strupper ( scope ) ;
break ;
case ' M ' :
find_master = True ;
break ;
case ' S ' :
find_status = True ;
break ;
1997-11-20 02:50:02 +03:00
case ' R ' :
recursion_desired = True ;
break ;
1996-05-04 11:50:46 +04:00
case ' d ' :
DEBUGLEVEL = atoi ( optarg ) ;
break ;
1996-10-02 18:09:22 +04:00
case ' s ' :
1997-09-26 22:55:29 +04:00
pstrcpy ( servicesf , optarg ) ;
1996-10-02 18:09:22 +04:00
break ;
1997-10-04 05:07:47 +04:00
case ' r ' :
RootPort = - 1 ;
break ;
1996-05-04 11:50:46 +04:00
case ' h ' :
usage ( ) ;
exit ( 0 ) ;
break ;
1997-10-10 05:32:26 +04:00
case ' A ' :
lookup_by_ip = True ;
break ;
1996-05-04 11:50:46 +04:00
default :
usage ( ) ;
exit ( 1 ) ;
}
if ( argc < 2 ) {
usage ( ) ;
exit ( 1 ) ;
}
1996-10-02 18:09:22 +04:00
if ( ! lp_load ( servicesf , True ) ) {
fprintf ( stderr , " Can't load %s - run testparm to debug it \n " , servicesf ) ;
}
1996-06-06 15:43:09 +04:00
load_interfaces ( ) ;
1996-05-04 11:50:46 +04:00
init_structs ( ) ;
if ( ! open_sockets ( ) ) return ( 1 ) ;
1996-10-07 15:06:34 +04:00
if ( ! got_bcast )
bcast_addr = * iface_bcast ( ipzero ) ;
DEBUG ( 1 , ( " Sending queries to %s \n " , inet_ntoa ( bcast_addr ) ) ) ;
1996-05-04 11:50:46 +04:00
for ( i = optind ; i < argc ; i + + )
1996-08-01 21:49:40 +04:00
{
1997-12-13 17:16:07 +03:00
int j , count , retries = 2 ;
1996-05-04 11:50:46 +04:00
char * p ;
struct in_addr ip ;
1997-12-13 17:16:07 +03:00
struct in_addr * ip_list ;
1996-05-04 11:50:46 +04:00
1997-09-26 22:55:29 +04:00
fstrcpy ( lookup , argv [ i ] ) ;
1996-05-04 11:50:46 +04:00
1997-10-10 05:32:26 +04:00
if ( lookup_by_ip )
{
strcpy ( 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 ;
}
1996-05-04 11:50:46 +04:00
if ( find_master ) {
if ( * lookup = = ' - ' ) {
strcpy ( lookup , " \01 \02 __MSBROWSE__ \02 " ) ;
lookup_type = 1 ;
} else {
1996-11-06 23:14:24 +03:00
lookup_type = 0x1d ;
1996-05-04 11:50:46 +04:00
}
}
p = strchr ( lookup , ' # ' ) ;
if ( p ) {
* p = 0 ;
sscanf ( p + 1 , " %x " , & lookup_type ) ;
retries = 1 ;
}
1997-12-13 17:16:07 +03:00
if ( ( ip_list = name_query ( ServerFD , lookup , lookup_type , use_bcast , 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 {
printf ( " name_query failed to find name %s \n " , lookup ) ;
1997-09-12 00:17:32 +04:00
}
1996-08-01 21:49:40 +04:00
}
1997-12-13 17:16:07 +03:00
1996-05-04 11:50:46 +04:00
return ( 0 ) ;
}