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