first pass at updating head branch to be to be the same as the SAMBA_2_0 branch
[ira/wip.git] / source3 / utils / nmblookup.c
index 59d5771c5d2797e8fb8e9db77b930ea3fc7d4065..2f9bbc6c5395ab0b179236b713c01cc9c5acf613 100644 (file)
@@ -20,9 +20,7 @@
    
 */
 
-#ifdef SYSLOG
-#undef SYSLOG
-#endif
+#define NO_SYSLOG
 
 #include "includes.h"
 
@@ -30,12 +28,16 @@ extern int DEBUGLEVEL;
 
 extern pstring scope;
 
-extern pstring myhostname;
 extern struct in_addr ipzero;
 
-int ServerFD= -1;
-
-int RootPort = 0;
+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
@@ -45,7 +47,7 @@ static BOOL open_sockets(void)
   ServerFD = open_socket_in( SOCK_DGRAM,
                              (RootPort ? 137 :0),
                              3,
-                             interpret_addr(lp_socket_address()) );
+                             interpret_addr(lp_socket_address()), True );
 
   if (ServerFD == -1)
     return(False);
@@ -57,17 +59,6 @@ static BOOL open_sockets(void)
 }
 
 
-/****************************************************************************
-  initialise connect, service and file structs
-****************************************************************************/
-static BOOL init_structs(void )
-{
-  if (!get_myname(myhostname,NULL))
-    return(False);
-
-  return True;
-}
-
 /****************************************************************************
 usage on the program
 ****************************************************************************/
@@ -81,12 +72,70 @@ 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
 ****************************************************************************/
@@ -98,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;
@@ -116,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);
@@ -145,7 +191,7 @@ 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);
@@ -170,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);
+  /*
+   * Ensure we reset DEBUGLEVEL if someone specified it
+   * on the command line.
+   */
 
-  if (!got_bcast)
-    bcast_addr = *iface_bcast(ipzero);
-
-  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, retries = 2;
       char *p;
       struct in_addr ip;
-      struct in_addr *ip_list;
 
       fstrcpy(lookup,argv[i]);
 
@@ -216,25 +260,11 @@ int main(int argc,char *argv[])
       p = strchr(lookup,'#');
 
       if (p) {
-       *p = 0;
-       sscanf(p+1,"%x",&lookup_type);
-       retries = 1;
+        *p = 0;
+        sscanf(p+1,"%x",&lookup_type);
       }
 
-      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 {
+      if (!query_one(lookup, lookup_type)) {
              printf("name_query failed to find name %s\n", lookup);
       }
   }