c137b4f93acd48b44c3229a3d535a6280bbdf97a
[samba.git] / source3 / utils / nmblookup.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    NBT client - used to lookup netbios names
5    Copyright (C) Andrew Tridgell 1994-1995
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20    
21 */
22
23 #ifdef SYSLOG
24 #undef SYSLOG
25 #endif
26
27 #include "includes.h"
28
29 extern int DEBUGLEVEL;
30
31 extern pstring scope;
32
33 extern pstring myhostname;
34 extern struct in_addr ipzero;
35
36 int ServerFD= -1;
37
38 /****************************************************************************
39   open the socket communication
40   **************************************************************************/
41 static BOOL open_sockets(void)
42 {
43   struct hostent *hp;
44  
45   /* get host info */
46   if ((hp = Get_Hostbyname(myhostname)) == 0) 
47     {
48       DEBUG(0,( "Get_Hostbyname: Unknown host. %s\n",myhostname));
49       return False;
50     }   
51
52   ServerFD = open_socket_in(SOCK_DGRAM, 0,3,interpret_addr(lp_socket_address()));
53
54   if (ServerFD == -1)
55     return(False);
56
57   set_socket_options(ServerFD,"SO_BROADCAST");
58
59   DEBUG(3, ("Socket opened.\n"));
60   return True;
61 }
62
63
64 /****************************************************************************
65   initialise connect, service and file structs
66 ****************************************************************************/
67 static BOOL init_structs(void )
68 {
69   if (!get_myname(myhostname,NULL))
70     return(False);
71
72   return True;
73 }
74
75 /****************************************************************************
76 usage on the program
77 ****************************************************************************/
78 static void usage(void)
79 {
80   printf("Usage: nmblookup [-M] [-B bcast address] [-d debuglevel] name\n");
81   printf("Version %s\n",VERSION);
82   printf("\t-d debuglevel         set the debuglevel\n");
83   printf("\t-B broadcast address  the address to use for broadcasts\n");
84   printf("\t-M                    searches for a master browser\n");
85   printf("\t-S                    lookup node status as well\n");
86   printf("\n");
87 }
88
89
90 /****************************************************************************
91   main program
92 ****************************************************************************/
93 int main(int argc,char *argv[])
94 {
95   int opt;
96   unsigned int lookup_type = 0x0;
97   pstring lookup;
98   extern int optind;
99   extern char *optarg;
100   BOOL find_master=False;
101   BOOL find_status=False;
102   int i;
103   static pstring servicesf = CONFIGFILE;
104   
105   DEBUGLEVEL = 1;
106   *lookup = 0;
107
108   TimeInit();
109
110   setup_logging(argv[0],True);
111
112   charset_initialise();
113
114   while ((opt = getopt(argc, argv, "p:d:B:i:s:SMh")) != EOF)
115     switch (opt)
116       {
117       case 'B':
118         iface_set_default(NULL,optarg,NULL);
119         break;
120       case 'i':
121         strcpy(scope,optarg);
122         strupper(scope);
123         break;
124       case 'M':
125         find_master = True;
126         break;
127       case 'S':
128         find_status = True;
129         break;
130       case 'd':
131         DEBUGLEVEL = atoi(optarg);
132         break;
133       case 's':
134         strcpy(servicesf, optarg);
135         break;
136       case 'h':
137         usage();
138         exit(0);
139         break;
140       default:
141         usage();
142         exit(1);
143       }
144
145   if (argc < 2) {
146     usage();
147     exit(1);
148   }
149
150   if (!lp_load(servicesf,True)) {
151     fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
152     return (-1);
153   }
154
155   load_interfaces();
156   init_structs();
157   if (!open_sockets()) return(1);
158
159   DEBUG(1,("Sending queries to %s\n",inet_ntoa(*iface_bcast(ipzero))));
160
161
162   for (i=optind;i<argc;i++)
163   {
164       BOOL bcast = True;
165       int retries = 2;
166       char *p;
167       struct in_addr ip;
168
169       strcpy(lookup,argv[i]);
170
171       if (find_master) {
172         if (*lookup == '-') {
173           strcpy(lookup,"\01\02__MSBROWSE__\02");
174           lookup_type = 1;
175         } else {
176           lookup_type = 0x1b;
177         }
178       }
179
180       p = strchr(lookup,'#');
181
182       if (p) {
183         *p = 0;
184         sscanf(p+1,"%x",&lookup_type);
185         bcast = False;
186         retries = 1;
187       }
188
189     if (name_query(ServerFD,lookup,lookup_type,bcast,True,
190                      *iface_bcast(ipzero),&ip,NULL)) 
191         {
192           printf("%s %s\n",inet_ntoa(ip),lookup);
193     }
194         if (find_status) 
195         {
196               printf("Looking up status of %s\n",inet_ntoa(ip));
197               name_status(ServerFD,lookup,lookup_type,True,ip,NULL,NULL,NULL);
198               printf("\n");
199         }
200   }
201
202   return(0);
203 }