From f88b7a076be74a29a3bf876b4e2705f4a1ecf42b Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 24 Oct 2007 14:16:54 -0700 Subject: This is a large patch (sorry). Migrate from struct in_addr to struct sockaddr_storage in most places that matter (ie. not the nmbd and NetBIOS lookups). This passes make test on an IPv4 box, but I'll have to do more work/testing on IPv6 enabled boxes. This should now give us a framework for testing and finishing the IPv6 migration. It's at the state where someone with a working IPv6 setup should (theorecically) be able to type : smbclient //ipv6-address/share and have it work. Jeremy. (This used to be commit 98e154c3125d5732c37a72d74b0eb5cd7b6155fd) --- source3/utils/nmblookup.c | 400 ++++++++++++++++++++++++++-------------------- 1 file changed, 225 insertions(+), 175 deletions(-) (limited to 'source3/utils/nmblookup.c') diff --git a/source3/utils/nmblookup.c b/source3/utils/nmblookup.c index 1a26e81206e..e68c786ce97 100644 --- a/source3/utils/nmblookup.c +++ b/source3/utils/nmblookup.c @@ -1,55 +1,66 @@ -/* +/* Unix SMB/CIFS implementation. NBT client - used to lookup netbios names Copyright (C) Andrew Tridgell 1994-1998 Copyright (C) Jelmer Vernooij 2003 (Conversion to popt) - + This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - + You should have received a copy of the GNU General Public License along with this program. If not, see . - + */ #include "includes.h" extern bool AllowDebugChange; -static bool give_flags = False; -static bool use_bcast = True; -static bool got_bcast = False; -static struct in_addr bcast_addr; -static bool recursion_desired = False; -static bool translate_addresses = False; +static bool give_flags = false; +static bool use_bcast = true; +static bool got_bcast = false; +static struct sockaddr_storage bcast_addr; +static bool recursion_desired = false; +static bool translate_addresses = false; static int ServerFD= -1; -static bool RootPort = False; -static bool find_status=False; +static bool RootPort = false; +static bool find_status = false; /**************************************************************************** - open the socket communication - **************************************************************************/ + Open the socket communication. +**************************************************************************/ + static bool open_sockets(void) { - ServerFD = open_socket_in( SOCK_DGRAM, - (RootPort ? 137 : 0), - (RootPort ? 0 : 3), - interpret_addr(lp_socket_address()), True ); + struct sockaddr_storage ss; + const char *sock_addr = lp_socket_address(); + + if (!interpret_string_addr(&ss, sock_addr, + AI_NUMERICHOST|AI_PASSIVE)) { + DEBUG(0,("open_sockets: unable to get socket address " + "from string %s", sock_addr)); + return false; + } + ServerFD = open_socket_in( SOCK_DGRAM, + (RootPort ? 137 : 0), + (RootPort ? 0 : 3), + &ss, true ); - if (ServerFD == -1) - return(False); + if (ServerFD == -1) { + return false; + } - set_socket_options( ServerFD, "SO_BROADCAST" ); + set_socket_options( ServerFD, "SO_BROADCAST" ); - DEBUG(3, ("Socket opened.\n")); - return True; + DEBUG(3, ("Socket opened.\n")); + return true; } /**************************************************************************** @@ -59,7 +70,7 @@ static char *node_status_flags(unsigned char flags) { static fstring ret; fstrcpy(ret,""); - + fstrcat(ret, (flags & 0x80) ? " " : " "); if ((flags & 0x60) == 0x00) fstrcat(ret,"B "); if ((flags & 0x60) == 0x20) fstrcat(ret,"P "); @@ -69,13 +80,14 @@ static char *node_status_flags(unsigned char flags) if (flags & 0x08) fstrcat(ret," "); if (flags & 0x04) fstrcat(ret," "); if (flags & 0x02) fstrcat(ret," "); - + return ret; } /**************************************************************************** -turn the NMB Query flags into a string + Turn the NMB Query flags into a string. ****************************************************************************/ + static char *query_flags(int flags) { static fstring ret1; @@ -92,24 +104,32 @@ static char *query_flags(int flags) } /**************************************************************************** -do a node status query + Do a node status query. ****************************************************************************/ -static void do_node_status(int fd, const char *name, int type, struct in_addr ip) + +static void do_node_status(int fd, + const char *name, + int type, + struct sockaddr_storage *pss) { struct nmb_name nname; int count, i, j; NODE_STATUS_STRUCT *status; struct node_status_extra extra; fstring cleanname; + char addr[INET6_ADDRSTRLEN]; - d_printf("Looking up status of %s\n",inet_ntoa(ip)); + print_sockaddr(addr, sizeof(addr), pss); + d_printf("Looking up status of %s\n",addr); make_nmb_name(&nname, name, type); - status = node_status_query(fd,&nname,ip, &count, &extra); + status = node_status_query(fd, &nname, pss, &count, &extra); if (status) { for (i=0;i - %s\n", cleanname,status[i].type, @@ -122,60 +142,80 @@ static void do_node_status(int fd, const char *name, int type, struct in_addr ip d_printf("\n"); SAFE_FREE(status); } else { - d_printf("No reply from %s\n\n",inet_ntoa(ip)); + d_printf("No reply from %s\n\n",addr); } } /**************************************************************************** -send out one query + Send out one query. ****************************************************************************/ + static bool query_one(const char *lookup, unsigned int lookup_type) { int j, count, flags = 0; - struct in_addr *ip_list=NULL; + struct sockaddr_storage *ip_list=NULL; if (got_bcast) { - d_printf("querying %s on %s\n", lookup, inet_ntoa(bcast_addr)); + char addr[INET6_ADDRSTRLEN]; + print_sockaddr(addr, sizeof(addr), &bcast_addr); + d_printf("querying %s on %s\n", lookup, addr); ip_list = name_query(ServerFD,lookup,lookup_type,use_bcast, - use_bcast?True:recursion_desired, - bcast_addr,&count, &flags, NULL); + use_bcast?true:recursion_desired, + &bcast_addr, &count, &flags, NULL); } else { const struct in_addr *bcast; for (j=iface_count() - 1; !ip_list && j >= 0; j--) { + char addr[INET6_ADDRSTRLEN]; + struct sockaddr_storage bcast_ss; + bcast = iface_n_bcast_v4(j); if (!bcast) { continue; } - d_printf("querying %s on %s\n", - lookup, inet_ntoa(*bcast)); + in_addr_to_sockaddr_storage(&bcast_ss, *bcast); + print_sockaddr(addr, sizeof(addr), &bcast_ss); + d_printf("querying %s on %s\n", + lookup, addr); ip_list = name_query(ServerFD,lookup,lookup_type, use_bcast, use_bcast?True:recursion_desired, - *bcast,&count, &flags, NULL); + &bcast_ss,&count, &flags, NULL); } } - if (!ip_list) return False; + if (!ip_list) { + return false; + } - if (give_flags) - d_printf("Flags: %s\n", query_flags(flags)); + if (give_flags) { + d_printf("Flags: %s\n", query_flags(flags)); + } for (j=0;j h_name); + char h_name[HOST_NAME_MAX]; + h_name[0] = '\0'; + if (getnameinfo((const struct sockaddr *)&ip_list[j], + sizeof(struct sockaddr_storage), + h_name, sizeof(h_name), + NULL, 0, + NI_NAMEREQD)) { + continue; } + d_printf("%s, ", h_name); } - d_printf("%s %s<%02x>\n",inet_ntoa(ip_list[j]),lookup, lookup_type); + print_sockaddr(addr, sizeof(addr), &ip_list[j]); + d_printf("%s %s<%02x>\n", addr,lookup, lookup_type); /* We can only do find_status if the ip address returned was valid - ie. name_query returned true. */ if (find_status) { - do_node_status(ServerFD, lookup, lookup_type, ip_list[j]); + do_node_status(ServerFD, lookup, + lookup_type, &ip_list[j]); } } @@ -190,130 +230,140 @@ static bool query_one(const char *lookup, unsigned int lookup_type) ****************************************************************************/ int main(int argc,char *argv[]) { - int opt; - unsigned int lookup_type = 0x0; - fstring lookup; - static bool find_master=False; - static bool lookup_by_ip = False; - poptContext pc; - TALLOC_CTX *frame = talloc_stackframe(); - - struct poptOption long_options[] = { - POPT_AUTOHELP - { "broadcast", 'B', POPT_ARG_STRING, NULL, 'B', "Specify address to use for broadcasts", "BROADCAST-ADDRESS" }, - { "flags", 'f', POPT_ARG_NONE, NULL, 'f', "List the NMB flags returned" }, - { "unicast", 'U', POPT_ARG_STRING, NULL, 'U', "Specify address to use for unicast" }, - { "master-browser", 'M', POPT_ARG_NONE, NULL, 'M', "Search for a master browser" }, - { "recursion", 'R', POPT_ARG_VAL, NULL, 'R', "Set recursion desired in package" }, - { "status", 'S', POPT_ARG_VAL, NULL, 'S', "Lookup node status as well" }, - { "translate", 'T', POPT_ARG_NONE, NULL, 'T', "Translate IP addresses into names" }, - { "root-port", 'r', POPT_ARG_VAL, NULL, 'r', "Use root port 137 (Win95 only replies to this)" }, - { "lookup-by-ip", 'A', POPT_ARG_VAL, NULL, 'A', "Do a node status on as an IP Address" }, - POPT_COMMON_SAMBA - POPT_COMMON_CONNECTION - { 0, 0, 0, 0 } - }; - - *lookup = 0; - - load_case_tables(); - - setup_logging(argv[0],True); - - pc = poptGetContext("nmblookup", argc, (const char **)argv, long_options, - POPT_CONTEXT_KEEP_FIRST); - - poptSetOtherOptionHelp(pc, " ..."); - - while ((opt = poptGetNextOpt(pc)) != -1) { - switch (opt) { - case 'f': - give_flags = true; - break; - case 'M': - find_master = true; - break; - case 'R': - recursion_desired = true; - break; - case 'S': - find_status = true; - break; - case 'r': - RootPort = true; - break; - case 'A': - lookup_by_ip = true; - break; - case 'B': - bcast_addr = *interpret_addr2(poptGetOptArg(pc)); - got_bcast = True; - use_bcast = True; - break; - case 'U': - bcast_addr = *interpret_addr2(poptGetOptArg(pc)); - got_bcast = True; - use_bcast = False; - break; - case 'T': - translate_addresses = !translate_addresses; - break; - } - } - - poptGetArg(pc); /* Remove argv[0] */ - - if(!poptPeekArg(pc)) { - poptPrintUsage(pc, stderr, 0); - exit(1); - } - - if (!lp_load(dyn_CONFIGFILE,True,False,False,True)) { - fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE); - } - - load_interfaces(); - if (!open_sockets()) return(1); - - while(poptPeekArg(pc)) - { - char *p; - struct in_addr ip; - - fstrcpy(lookup,poptGetArg(pc)); - - if(lookup_by_ip) - { - ip = *interpret_addr2(lookup); - fstrcpy(lookup,"*"); - do_node_status(ServerFD, lookup, lookup_type, ip); - continue; - } - - if (find_master) { - if (*lookup == '-') { - fstrcpy(lookup,"\01\02__MSBROWSE__\02"); - lookup_type = 1; - } else { - lookup_type = 0x1d; - } - } - - p = strchr_m(lookup,'#'); - if (p) { - *p = '\0'; - sscanf(++p,"%x",&lookup_type); - } - - if (!query_one(lookup, lookup_type)) { - d_printf( "name_query failed to find name %s", lookup ); - if( 0 != lookup_type ) - d_printf( "#%02x", lookup_type ); - d_printf( "\n" ); - } - } - - poptFreeContext(pc); - TALLOC_FREE(frame); - return(0); + int opt; + unsigned int lookup_type = 0x0; + fstring lookup; + static bool find_master=False; + static bool lookup_by_ip = False; + poptContext pc; + TALLOC_CTX *frame = talloc_stackframe(); + + struct poptOption long_options[] = { + POPT_AUTOHELP + { "broadcast", 'B', POPT_ARG_STRING, NULL, 'B', "Specify address to use for broadcasts", "BROADCAST-ADDRESS" }, + { "flags", 'f', POPT_ARG_NONE, NULL, 'f', "List the NMB flags returned" }, + { "unicast", 'U', POPT_ARG_STRING, NULL, 'U', "Specify address to use for unicast" }, + { "master-browser", 'M', POPT_ARG_NONE, NULL, 'M', "Search for a master browser" }, + { "recursion", 'R', POPT_ARG_VAL, NULL, 'R', "Set recursion desired in package" }, + { "status", 'S', POPT_ARG_VAL, NULL, 'S', "Lookup node status as well" }, + { "translate", 'T', POPT_ARG_NONE, NULL, 'T', "Translate IP addresses into names" }, + { "root-port", 'r', POPT_ARG_VAL, NULL, 'r', "Use root port 137 (Win95 only replies to this)" }, + { "lookup-by-ip", 'A', POPT_ARG_VAL, NULL, 'A', "Do a node status on as an IP Address" }, + POPT_COMMON_SAMBA + POPT_COMMON_CONNECTION + { 0, 0, 0, 0 } + }; + + *lookup = 0; + + load_case_tables(); + + setup_logging(argv[0],True); + + pc = poptGetContext("nmblookup", argc, (const char **)argv, + long_options, POPT_CONTEXT_KEEP_FIRST); + + poptSetOtherOptionHelp(pc, " ..."); + + while ((opt = poptGetNextOpt(pc)) != -1) { + switch (opt) { + case 'f': + give_flags = true; + break; + case 'M': + find_master = true; + break; + case 'R': + recursion_desired = true; + break; + case 'S': + find_status = true; + break; + case 'r': + RootPort = true; + break; + case 'A': + lookup_by_ip = true; + break; + case 'B': + if (interpret_string_addr(&bcast_addr, + poptGetOptArg(pc), + NI_NUMERICHOST)) { + got_bcast = True; + use_bcast = True; + } + break; + case 'U': + if (interpret_string_addr(&bcast_addr, + poptGetOptArg(pc), + 0)) { + got_bcast = True; + use_bcast = False; + } + break; + case 'T': + translate_addresses = !translate_addresses; + break; + } + } + + poptGetArg(pc); /* Remove argv[0] */ + + if(!poptPeekArg(pc)) { + poptPrintUsage(pc, stderr, 0); + exit(1); + } + + if (!lp_load(dyn_CONFIGFILE,True,False,False,True)) { + fprintf(stderr, "Can't load %s - run testparm to debug it\n", + dyn_CONFIGFILE); + } + + load_interfaces(); + if (!open_sockets()) { + return(1); + } + + while(poptPeekArg(pc)) { + char *p; + struct in_addr ip; + + fstrcpy(lookup,poptGetArg(pc)); + + if(lookup_by_ip) { + struct sockaddr_storage ss; + ip = *interpret_addr2(lookup); + in_addr_to_sockaddr_storage(&ss, ip); + fstrcpy(lookup,"*"); + do_node_status(ServerFD, lookup, lookup_type, &ss); + continue; + } + + if (find_master) { + if (*lookup == '-') { + fstrcpy(lookup,"\01\02__MSBROWSE__\02"); + lookup_type = 1; + } else { + lookup_type = 0x1d; + } + } + + p = strchr_m(lookup,'#'); + if (p) { + *p = '\0'; + sscanf(++p,"%x",&lookup_type); + } + + if (!query_one(lookup, lookup_type)) { + d_printf( "name_query failed to find name %s", lookup ); + if( 0 != lookup_type ) { + d_printf( "#%02x", lookup_type ); + } + d_printf( "\n" ); + } + } + + poptFreeContext(pc); + TALLOC_FREE(frame); + return(0); } -- cgit