utils/nmblookup.c

説明を見る。
00001 /* 
00002    Unix SMB/CIFS implementation.
00003    NBT client - used to lookup netbios names
00004    Copyright (C) Andrew Tridgell 1994-1998
00005    Copyright (C) Jelmer Vernooij 2003 (Conversion to popt)
00006    
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2 of the License, or
00010    (at your option) any later version.
00011    
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016    
00017    You should have received a copy of the GNU General Public License
00018    along with this program; if not, write to the Free Software
00019    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020    
00021 */
00022 
00023 #include "includes.h"
00024 
00025 extern BOOL AllowDebugChange;
00026 
00027 static BOOL give_flags = False;
00028 static BOOL use_bcast = True;
00029 static BOOL got_bcast = False;
00030 static struct in_addr bcast_addr;
00031 static BOOL recursion_desired = False;
00032 static BOOL translate_addresses = False;
00033 static int ServerFD= -1;
00034 static int RootPort = False;
00035 static BOOL find_status=False;
00036 
00037 /****************************************************************************
00038   open the socket communication
00039   **************************************************************************/
00040 static BOOL open_sockets(void)
00041 {
00042   ServerFD = open_socket_in( SOCK_DGRAM,
00043                              (RootPort ? 137 : 0),
00044                              (RootPort ?   0 : 3),
00045                              interpret_addr(lp_socket_address()), True );
00046 
00047   if (ServerFD == -1)
00048     return(False);
00049 
00050   set_socket_options( ServerFD, "SO_BROADCAST" );
00051 
00052   DEBUG(3, ("Socket opened.\n"));
00053   return True;
00054 }
00055 
00056 /****************************************************************************
00057 turn a node status flags field into a string
00058 ****************************************************************************/
00059 static char *node_status_flags(unsigned char flags)
00060 {
00061         static fstring ret;
00062         fstrcpy(ret,"");
00063         
00064         fstrcat(ret, (flags & 0x80) ? "<GROUP> " : "        ");
00065         if ((flags & 0x60) == 0x00) fstrcat(ret,"B ");
00066         if ((flags & 0x60) == 0x20) fstrcat(ret,"P ");
00067         if ((flags & 0x60) == 0x40) fstrcat(ret,"M ");
00068         if ((flags & 0x60) == 0x60) fstrcat(ret,"H ");
00069         if (flags & 0x10) fstrcat(ret,"<DEREGISTERING> ");
00070         if (flags & 0x08) fstrcat(ret,"<CONFLICT> ");
00071         if (flags & 0x04) fstrcat(ret,"<ACTIVE> ");
00072         if (flags & 0x02) fstrcat(ret,"<PERMANENT> ");
00073         
00074         return ret;
00075 }
00076 
00077 /****************************************************************************
00078 turn the NMB Query flags into a string
00079 ****************************************************************************/
00080 static char *query_flags(int flags)
00081 {
00082         static fstring ret1;
00083         fstrcpy(ret1, "");
00084 
00085         if (flags & NM_FLAGS_RS) fstrcat(ret1, "Response ");
00086         if (flags & NM_FLAGS_AA) fstrcat(ret1, "Authoritative ");
00087         if (flags & NM_FLAGS_TC) fstrcat(ret1, "Truncated ");
00088         if (flags & NM_FLAGS_RD) fstrcat(ret1, "Recursion_Desired ");
00089         if (flags & NM_FLAGS_RA) fstrcat(ret1, "Recursion_Available ");
00090         if (flags & NM_FLAGS_B)  fstrcat(ret1, "Broadcast ");
00091 
00092         return ret1;
00093 }
00094 
00095 /****************************************************************************
00096 do a node status query
00097 ****************************************************************************/
00098 static void do_node_status(int fd, const char *name, int type, struct in_addr ip)
00099 {
00100         struct nmb_name nname;
00101         int count, i, j;
00102         NODE_STATUS_STRUCT *status;
00103         struct node_status_extra extra;
00104         fstring cleanname;
00105 
00106         d_printf("Looking up status of %s\n",inet_ntoa(ip));
00107         make_nmb_name(&nname, name, type);
00108         status = node_status_query(fd,&nname,ip, &count, &extra);
00109         if (status) {
00110                 for (i=0;i<count;i++) {
00111                         pull_ascii_fstring(cleanname, status[i].name);
00112                         for (j=0;cleanname[j];j++) {
00113                                 if (!isprint((int)cleanname[j])) cleanname[j] = '.';
00114                         }
00115                         d_printf("\t%-15s <%02x> - %s\n",
00116                                cleanname,status[i].type,
00117                                node_status_flags(status[i].flags));
00118                 }
00119                 d_printf("\n\tMAC Address = %02X-%02X-%02X-%02X-%02X-%02X\n",
00120                                 extra.mac_addr[0], extra.mac_addr[1],
00121                                 extra.mac_addr[2], extra.mac_addr[3],
00122                                 extra.mac_addr[4], extra.mac_addr[5]);
00123                 d_printf("\n");
00124                 SAFE_FREE(status);
00125         } else {
00126                 d_printf("No reply from %s\n\n",inet_ntoa(ip));
00127         }
00128 }
00129 
00130 
00131 /****************************************************************************
00132 send out one query
00133 ****************************************************************************/
00134 static BOOL query_one(const char *lookup, unsigned int lookup_type)
00135 {
00136         int j, count, flags = 0;
00137         struct in_addr *ip_list=NULL;
00138 
00139         if (got_bcast) {
00140                 d_printf("querying %s on %s\n", lookup, inet_ntoa(bcast_addr));
00141                 ip_list = name_query(ServerFD,lookup,lookup_type,use_bcast,
00142                                      use_bcast?True:recursion_desired,
00143                                      bcast_addr,&count, &flags, NULL);
00144         } else {
00145                 struct in_addr *bcast;
00146                 for (j=iface_count() - 1;
00147                      !ip_list && j >= 0;
00148                      j--) {
00149                         bcast = iface_n_bcast(j);
00150                         d_printf("querying %s on %s\n", 
00151                                lookup, inet_ntoa(*bcast));
00152                         ip_list = name_query(ServerFD,lookup,lookup_type,
00153                                              use_bcast,
00154                                              use_bcast?True:recursion_desired,
00155                                              *bcast,&count, &flags, NULL);
00156                 }
00157         }
00158 
00159         if (!ip_list) return False;
00160 
00161         if (give_flags)
00162           d_printf("Flags: %s\n", query_flags(flags));
00163 
00164         for (j=0;j<count;j++) {
00165                 if (translate_addresses) {
00166                         struct hostent *host = gethostbyaddr((char *)&ip_list[j], sizeof(ip_list[j]), AF_INET);
00167                         if (host) {
00168                                 d_printf("%s, ", host -> h_name);
00169                         }
00170                 }
00171                 d_printf("%s %s<%02x>\n",inet_ntoa(ip_list[j]),lookup, lookup_type);
00172                 /* We can only do find_status if the ip address returned
00173                    was valid - ie. name_query returned true.
00174                  */
00175                 if (find_status) {
00176                         do_node_status(ServerFD, lookup, lookup_type, ip_list[j]);
00177                 }
00178         }
00179 
00180         safe_free(ip_list);
00181 
00182         return (ip_list != NULL);
00183 }
00184 
00185 
00186 /****************************************************************************
00187   main program
00188 ****************************************************************************/
00189 int main(int argc,char *argv[])
00190 {
00191   int opt;
00192   unsigned int lookup_type = 0x0;
00193   fstring lookup;
00194   static BOOL find_master=False;
00195   static BOOL lookup_by_ip = False;
00196   poptContext pc;
00197 
00198   struct poptOption long_options[] = {
00199           POPT_AUTOHELP
00200           { "broadcast", 'B', POPT_ARG_STRING, NULL, 'B', "Specify address to use for broadcasts", "BROADCAST-ADDRESS" },
00201           { "flags", 'f', POPT_ARG_VAL, &give_flags, True, "List the NMB flags returned" },
00202           { "unicast", 'U', POPT_ARG_STRING, NULL, 'U', "Specify address to use for unicast" },
00203           { "master-browser", 'M', POPT_ARG_VAL, &find_master, True, "Search for a master browser" },
00204           { "recursion", 'R', POPT_ARG_VAL, &recursion_desired, True, "Set recursion desired in package" },
00205           { "status", 'S', POPT_ARG_VAL, &find_status, True, "Lookup node status as well" },
00206           { "translate", 'T', POPT_ARG_NONE, NULL, 'T', "Translate IP addresses into names" },
00207           { "root-port", 'r', POPT_ARG_VAL, &RootPort, True, "Use root port 137 (Win95 only replies to this)" },
00208           { "lookup-by-ip", 'A', POPT_ARG_VAL, &lookup_by_ip, True, "Do a node status on <name> as an IP Address" },
00209           POPT_COMMON_SAMBA
00210           POPT_COMMON_CONNECTION
00211           { 0, 0, 0, 0 }
00212   };
00213         
00214   *lookup = 0;
00215 
00216   load_case_tables();
00217 
00218   setup_logging(argv[0],True);
00219 
00220   pc = poptGetContext("nmblookup", argc, (const char **)argv, long_options, 
00221                                           POPT_CONTEXT_KEEP_FIRST);
00222 
00223   poptSetOtherOptionHelp(pc, "<NODE> ...");
00224 
00225   while ((opt = poptGetNextOpt(pc)) != -1) {
00226           switch (opt) {
00227           case 'B':
00228                   bcast_addr = *interpret_addr2(poptGetOptArg(pc));
00229                   got_bcast = True;
00230                   use_bcast = True;
00231                   break;
00232           case 'U':
00233                   bcast_addr = *interpret_addr2(poptGetOptArg(pc));
00234                   got_bcast = True;
00235                   use_bcast = False;
00236                   break;
00237           case 'T':
00238                   translate_addresses = !translate_addresses;
00239                   break;
00240           }
00241   }
00242 
00243   poptGetArg(pc); /* Remove argv[0] */
00244 
00245   if(!poptPeekArg(pc)) { 
00246           poptPrintUsage(pc, stderr, 0);
00247           exit(1);
00248   }
00249 
00250   if (!lp_load(dyn_CONFIGFILE,True,False,False,True)) {
00251           fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
00252   }
00253 
00254   load_interfaces();
00255   if (!open_sockets()) return(1);
00256 
00257   while(poptPeekArg(pc))
00258   {
00259           char *p;
00260           struct in_addr ip;
00261 
00262           fstrcpy(lookup,poptGetArg(pc));
00263 
00264           if(lookup_by_ip)
00265           {
00266                   ip = *interpret_addr2(lookup);
00267                   fstrcpy(lookup,"*");
00268                   do_node_status(ServerFD, lookup, lookup_type, ip);
00269                   continue;
00270           }
00271 
00272           if (find_master) {
00273                   if (*lookup == '-') {
00274                           fstrcpy(lookup,"\01\02__MSBROWSE__\02");
00275                           lookup_type = 1;
00276                   } else {
00277                           lookup_type = 0x1d;
00278                   }
00279           }
00280 
00281           p = strchr_m(lookup,'#');
00282           if (p) {
00283                   *p = '\0';
00284                   sscanf(++p,"%x",&lookup_type);
00285           }
00286 
00287           if (!query_one(lookup, lookup_type)) {
00288                   d_printf( "name_query failed to find name %s", lookup );
00289                   if( 0 != lookup_type )
00290                           d_printf( "#%02x", lookup_type );
00291                   d_printf( "\n" );
00292           }
00293   }
00294 
00295   poptFreeContext(pc);
00296 
00297   return(0);
00298 }

Sambaに対してSat Aug 29 21:23:30 2009に生成されました。  doxygen 1.4.7