'The mother of all checkins' :-). Jeremy Allison (jallison@whistle.com)
[Samba.git] / source / utils / nmblookup.c
blob582f4eb6db387a599c4384d1689943e14905fbd6
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
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.
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.
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.
23 #ifdef SYSLOG
24 #undef SYSLOG
25 #endif
27 #include "includes.h"
29 extern int DEBUGLEVEL;
31 extern pstring scope;
33 extern pstring myhostname;
34 extern struct in_addr ipzero;
36 int ServerFD= -1;
38 /****************************************************************************
39 open the socket communication
40 **************************************************************************/
41 static BOOL open_sockets(void)
43 struct hostent *hp;
45 /* get host info */
46 if ((hp = Get_Hostbyname(myhostname)) == 0)
48 DEBUG(0,( "Get_Hostbyname: Unknown host. %s\n",myhostname));
49 return False;
52 ServerFD = open_socket_in(SOCK_DGRAM, 0,3,interpret_addr(lp_socket_address()));
54 if (ServerFD == -1)
55 return(False);
57 set_socket_options(ServerFD,"SO_BROADCAST");
59 DEBUG(3, ("Socket opened.\n"));
60 return True;
64 /****************************************************************************
65 initialise connect, service and file structs
66 ****************************************************************************/
67 static BOOL init_structs(void )
69 if (!get_myname(myhostname,NULL))
70 return(False);
72 return True;
75 /****************************************************************************
76 usage on the program
77 ****************************************************************************/
78 static void usage(void)
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");
90 /****************************************************************************
91 main program
92 ****************************************************************************/
93 int main(int argc,char *argv[])
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 struct in_addr bcast_addr;
105 BOOL got_bcast = False;
107 DEBUGLEVEL = 1;
108 *lookup = 0;
110 TimeInit();
112 setup_logging(argv[0],True);
114 charset_initialise();
116 while ((opt = getopt(argc, argv, "p:d:B:i:s:SMh")) != EOF)
117 switch (opt)
119 case 'B':
120 iface_set_default(NULL,optarg,NULL);
121 bcast_addr = *interpret_addr2(optarg);
122 got_bcast = True;
123 break;
124 case 'i':
125 strcpy(scope,optarg);
126 strupper(scope);
127 break;
128 case 'M':
129 find_master = True;
130 break;
131 case 'S':
132 find_status = True;
133 break;
134 case 'd':
135 DEBUGLEVEL = atoi(optarg);
136 break;
137 case 's':
138 strcpy(servicesf, optarg);
139 break;
140 case 'h':
141 usage();
142 exit(0);
143 break;
144 default:
145 usage();
146 exit(1);
149 if (argc < 2) {
150 usage();
151 exit(1);
154 if (!lp_load(servicesf,True)) {
155 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
158 load_interfaces();
159 init_structs();
160 if (!open_sockets()) return(1);
162 if (!got_bcast)
163 bcast_addr = *iface_bcast(ipzero);
165 DEBUG(1,("Sending queries to %s\n",inet_ntoa(bcast_addr)));
168 for (i=optind;i<argc;i++)
170 BOOL bcast = True;
171 int retries = 2;
172 char *p;
173 struct in_addr ip;
175 strcpy(lookup,argv[i]);
177 if (find_master) {
178 if (*lookup == '-') {
179 strcpy(lookup,"\01\02__MSBROWSE__\02");
180 lookup_type = 1;
181 } else {
182 lookup_type = 0x1d;
186 p = strchr(lookup,'#');
188 if (p) {
189 *p = 0;
190 sscanf(p+1,"%x",&lookup_type);
191 bcast = False;
192 retries = 1;
195 if (name_query(ServerFD,lookup,lookup_type,bcast,True,
196 bcast_addr,&ip,NULL))
198 printf("%s %s\n",inet_ntoa(ip),lookup);
200 if (find_status)
202 printf("Looking up status of %s\n",inet_ntoa(ip));
203 name_status(ServerFD,lookup,lookup_type,True,ip,NULL,NULL,NULL);
204 printf("\n");
208 return(0);