nmblookup.c:
[samba.git] / source3 / utils / nmblookup.c
index a543b90762f77c32e277ff47c07e20933db22f4b..36905aa5ae33dd1674e24f78fd9640636fb8114e 100644 (file)
@@ -2,7 +2,7 @@
    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-1997
    
    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
@@ -35,6 +35,8 @@ extern struct in_addr ipzero;
 
 int ServerFD= -1;
 
+int RootPort = 0;
+
 /****************************************************************************
   open the socket communication
   **************************************************************************/
@@ -49,7 +51,10 @@ static BOOL open_sockets(void)
       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()) );
 
   if (ServerFD == -1)
     return(False);
@@ -100,6 +105,9 @@ int main(int argc,char *argv[])
   BOOL find_master=False;
   BOOL find_status=False;
   int i;
+  static pstring servicesf = CONFIGFILE;
+  struct in_addr bcast_addr;
+  BOOL got_bcast = False;
   
   DEBUGLEVEL = 1;
   *lookup = 0;
@@ -110,14 +118,16 @@ int main(int argc,char *argv[])
 
   charset_initialise();
 
-  while ((opt = getopt(argc, argv, "p:d:B:i:SMh")) != EOF)
+  while ((opt = getopt(argc, argv, "d:B:i:s:SMrh")) != EOF)
     switch (opt)
       {
       case 'B':
        iface_set_default(NULL,optarg,NULL);
+       bcast_addr = *interpret_addr2(optarg);
+       got_bcast = True;
        break;
       case 'i':
-       strcpy(scope,optarg);
+       fstrcpy(scope,optarg);
        strupper(scope);
        break;
       case 'M':
@@ -129,6 +139,12 @@ int main(int argc,char *argv[])
       case 'd':
        DEBUGLEVEL = atoi(optarg);
        break;
+      case 's':
+       pstrcpy(servicesf, optarg);
+       break;
+      case 'r':
+        RootPort = -1;
+        break;
       case 'h':
        usage();
        exit(0);
@@ -143,28 +159,35 @@ int main(int argc,char *argv[])
     exit(1);
   }
 
+  if (!lp_load(servicesf,True)) {
+    fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
+  }
+
   load_interfaces();
   init_structs();
   if (!open_sockets()) return(1);
 
-  DEBUG(1,("Sending queries to %s\n",inet_ntoa(*iface_bcast(ipzero))));
+  if (!got_bcast)
+    bcast_addr = *iface_bcast(ipzero);
+
+  DEBUG(1,("Sending queries to %s\n",inet_ntoa(bcast_addr)));
 
 
   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 (find_master) {
        if (*lookup == '-') {
          strcpy(lookup,"\01\02__MSBROWSE__\02");
          lookup_type = 1;
        } else {
-         lookup_type = 0x1b;
+         lookup_type = 0x1d;
        }
       }
 
@@ -178,19 +201,25 @@ int main(int argc,char *argv[])
       }
 
       if (name_query(ServerFD,lookup,lookup_type,bcast,True,
-                    *iface_bcast(ipzero),&ip,NULL)) 
+                  bcast_addr,&ip,NULL)) 
+      {
+        printf("%s %s\n",inet_ntoa(ip),lookup);
+
+        /* We can only do find_status if the ip address returned
+           was valid - ie. name_query returned true.
+         */
+       if (find_status) 
        {
-         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);
+       }
       }
-    }
+      else
+      {
+        printf("name_query failed to find name %s\n", lookup);
+      }
+  }
 
   return(0);
 }