nmblookup.c:
[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-1997
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 int RootPort = 0;
39
40 /****************************************************************************
41   open the socket communication
42   **************************************************************************/
43 static BOOL open_sockets(void)
44 {
45   struct hostent *hp;
46  
47   /* get host info */
48   if ((hp = Get_Hostbyname(myhostname)) == 0) 
49     {
50       DEBUG(0,( "Get_Hostbyname: Unknown host. %s\n",myhostname));
51       return False;
52     }   
53
54   ServerFD = open_socket_in( SOCK_DGRAM,
55                              (RootPort ? 137 :0),
56                              3,
57                              interpret_addr(lp_socket_address()) );
58
59   if (ServerFD == -1)
60     return(False);
61
62   set_socket_options(ServerFD,"SO_BROADCAST");
63
64   DEBUG(3, ("Socket opened.\n"));
65   return True;
66 }
67
68
69 /****************************************************************************
70   initialise connect, service and file structs
71 ****************************************************************************/
72 static BOOL init_structs(void )
73 {
74   if (!get_myname(myhostname,NULL))
75     return(False);
76
77   return True;
78 }
79
80 /****************************************************************************
81 usage on the program
82 ****************************************************************************/
83 static void usage(void)
84 {
85   printf("Usage: nmblookup [-M] [-B bcast address] [-d debuglevel] name\n");
86   printf("Version %s\n",VERSION);
87   printf("\t-d debuglevel         set the debuglevel\n");
88   printf("\t-B broadcast address  the address to use for broadcasts\n");
89   printf("\t-M                    searches for a master browser\n");
90   printf("\t-S                    lookup node status as well\n");
91   printf("\n");
92 }
93
94
95 /****************************************************************************
96   main program
97 ****************************************************************************/
98 int main(int argc,char *argv[])
99 {
100   int opt;
101   unsigned int lookup_type = 0x0;
102   pstring lookup;
103   extern int optind;
104   extern char *optarg;
105   BOOL find_master=False;
106   BOOL find_status=False;
107   int i;
108   static pstring servicesf = CONFIGFILE;
109   struct in_addr bcast_addr;
110   BOOL got_bcast = False;
111   
112   DEBUGLEVEL = 1;
113   *lookup = 0;
114
115   TimeInit();
116
117   setup_logging(argv[0],True);
118
119   charset_initialise();
120
121   while ((opt = getopt(argc, argv, "d:B:i:s:SMrh")) != EOF)
122     switch (opt)
123       {
124       case 'B':
125         iface_set_default(NULL,optarg,NULL);
126         bcast_addr = *interpret_addr2(optarg);
127         got_bcast = True;
128         break;
129       case 'i':
130         fstrcpy(scope,optarg);
131         strupper(scope);
132         break;
133       case 'M':
134         find_master = True;
135         break;
136       case 'S':
137         find_status = True;
138         break;
139       case 'd':
140         DEBUGLEVEL = atoi(optarg);
141         break;
142       case 's':
143         pstrcpy(servicesf, optarg);
144         break;
145       case 'r':
146         RootPort = -1;
147         break;
148       case 'h':
149         usage();
150         exit(0);
151         break;
152       default:
153         usage();
154         exit(1);
155       }
156
157   if (argc < 2) {
158     usage();
159     exit(1);
160   }
161
162   if (!lp_load(servicesf,True)) {
163     fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
164   }
165
166   load_interfaces();
167   init_structs();
168   if (!open_sockets()) return(1);
169
170   if (!got_bcast)
171     bcast_addr = *iface_bcast(ipzero);
172
173   DEBUG(1,("Sending queries to %s\n",inet_ntoa(bcast_addr)));
174
175
176   for (i=optind;i<argc;i++)
177   {
178       BOOL bcast = True;
179       int retries = 2;
180       char *p;
181       struct in_addr ip;
182
183       fstrcpy(lookup,argv[i]);
184
185       if (find_master) {
186         if (*lookup == '-') {
187           strcpy(lookup,"\01\02__MSBROWSE__\02");
188           lookup_type = 1;
189         } else {
190           lookup_type = 0x1d;
191         }
192       }
193
194       p = strchr(lookup,'#');
195
196       if (p) {
197         *p = 0;
198         sscanf(p+1,"%x",&lookup_type);
199         bcast = False;
200         retries = 1;
201       }
202
203       if (name_query(ServerFD,lookup,lookup_type,bcast,True,
204                    bcast_addr,&ip,NULL)) 
205       {
206         printf("%s %s\n",inet_ntoa(ip),lookup);
207
208         /* We can only do find_status if the ip address returned
209            was valid - ie. name_query returned true.
210          */
211         if (find_status) 
212         {
213               printf("Looking up status of %s\n",inet_ntoa(ip));
214               name_status(ServerFD,lookup,lookup_type,True,ip,NULL,NULL,NULL);
215               printf("\n");
216         }
217       }
218       else
219       {
220         printf("name_query failed to find name %s\n", lookup);
221       }
222   }
223
224   return(0);
225 }