Convert all uses of uint8/16/32 to _t in nmbd and the include file.
[samba.git] / source3 / nmbd / nmbd_subnetdb.c
index 02a91f27606026434d1a0087d3299c30d9600041..5c9e4c7eb26524541b84fba2193112a1e2fc7589 100644 (file)
@@ -7,7 +7,7 @@
    
    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 2 of the License, or
+   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,
    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, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
    Revision History:
 
 */
 
 #include "includes.h"
+#include "nmbd/nmbd.h"
 
-extern int ClientNMB;
-extern int ClientDGRAM;
 extern int global_nmb_port;
 
 /* This is the broadcast subnets database. */
@@ -39,7 +37,7 @@ struct subnet_record *unicast_subnet = NULL;
 struct subnet_record *remote_broadcast_subnet = NULL;
 struct subnet_record *wins_server_subnet = NULL;
 
-extern uint16 samba_nb_type; /* Samba's NetBIOS name type. */
+extern uint16_t samba_nb_type; /* Samba's NetBIOS name type. */
 
 /****************************************************************************
   Add a subnet into the list.
@@ -50,34 +48,6 @@ static void add_subnet(struct subnet_record *subrec)
        DLIST_ADD(subnetlist, subrec);
 }
 
-/* ************************************************************************** **
- * Comparison routine for ordering the splay-tree based namelists assoicated
- * with each subnet record.
- *
- *  Input:  Item  - Pointer to the comparison key.
- *          Node  - Pointer to a node the splay tree.
- *
- *  Output: The return value will be <0 , ==0, or >0 depending upon the
- *          ordinal relationship of the two keys.
- *
- * ************************************************************************** **
- */
-static int namelist_entry_compare( ubi_trItemPtr Item, ubi_trNodePtr Node )
-{
-       struct name_record *NR = (struct name_record *)Node;
-
-       if( DEBUGLVL( 10 ) ) {
-               struct nmb_name *Iname = (struct nmb_name *)Item;
-
-               Debug1( "nmbd_subnetdb:namelist_entry_compare()\n" );
-               Debug1( "%d == memcmp( \"%s\", \"%s\", %d )\n",
-                       memcmp( Item, &(NR->name), sizeof(struct nmb_name) ),
-                       nmb_namestr(Iname), nmb_namestr(&NR->name), (int)sizeof(struct nmb_name) );
-       }
-
-       return( memcmp( Item, &(NR->name), sizeof(struct nmb_name) ) ); 
-}
-
 /****************************************************************************
 stop listening on a subnet
 we don't free the record as we don't have proper reference counting for it
@@ -86,16 +56,24 @@ yet and it may be in use by a response record
 
 void close_subnet(struct subnet_record *subrec)
 {
-       DLIST_REMOVE(subnetlist, subrec);
-
+       if (subrec->nmb_sock != -1) {
+               close(subrec->nmb_sock);
+               subrec->nmb_sock = -1;
+       }
+       if (subrec->nmb_bcast != -1) {
+               close(subrec->nmb_bcast);
+               subrec->nmb_bcast = -1;
+       }
        if (subrec->dgram_sock != -1) {
                close(subrec->dgram_sock);
                subrec->dgram_sock = -1;
        }
-       if (subrec->nmb_sock != -1) {
-               close(subrec->nmb_sock);
-               subrec->nmb_sock = -1;
+       if (subrec->dgram_bcast != -1) {
+               close(subrec->dgram_bcast);
+               subrec->dgram_bcast = -1;
        }
+
+       DLIST_REMOVE(subnetlist, subrec);
 }
 
 /****************************************************************************
@@ -107,66 +85,96 @@ static struct subnet_record *make_subnet(const char *name, enum subnet_type type
                                         struct in_addr mask_ip)
 {
        struct subnet_record *subrec = NULL;
-       int nmb_sock, dgram_sock;
+       int nmb_sock = -1;
+       int dgram_sock = -1;
+       int nmb_bcast = -1;
+       int dgram_bcast = -1;
+       bool bind_bcast = lp_nmbd_bind_explicit_broadcast();
 
        /* Check if we are creating a non broadcast subnet - if so don't create
                sockets.  */
 
-       if(type != NORMAL_SUBNET) {
-               nmb_sock = -1;
-               dgram_sock = -1;
-       } else {
+       if (type == NORMAL_SUBNET) {
+               struct sockaddr_storage ss;
+               struct sockaddr_storage ss_bcast;
+
+               in_addr_to_sockaddr_storage(&ss, myip);
+               in_addr_to_sockaddr_storage(&ss_bcast, bcast_ip);
+
                /*
                 * Attempt to open the sockets on port 137/138 for this interface
                 * and bind them.
                 * Fail the subnet creation if this fails.
                 */
 
-               if((nmb_sock = open_socket_in(SOCK_DGRAM, global_nmb_port,0, myip.s_addr,True)) == -1) {
-                       if( DEBUGLVL( 0 ) ) {
-                               Debug1( "nmbd_subnetdb:make_subnet()\n" );
-                               Debug1( "  Failed to open nmb socket on interface %s ", inet_ntoa(myip) );
-                               Debug1( "for port %d.  ", global_nmb_port );
-                               Debug1( "Error was %s\n", strerror(errno) );
+               nmb_sock = open_socket_in(SOCK_DGRAM, global_nmb_port,
+                                         0, &ss, true);
+               if (nmb_sock == -1) {
+                       DEBUG(0,   ("nmbd_subnetdb:make_subnet()\n"));
+                       DEBUGADD(0,("  Failed to open nmb socket on interface %s ",
+                                   inet_ntoa(myip)));
+                       DEBUGADD(0,("for port %d.  ", global_nmb_port));
+                       DEBUGADD(0,("Error was %s\n", strerror(errno)));
+                       goto failed;
+               }
+               set_socket_options(nmb_sock,"SO_BROADCAST");
+               set_blocking(nmb_sock, false);
+
+               if (bind_bcast) {
+                       nmb_bcast = open_socket_in(SOCK_DGRAM, global_nmb_port,
+                                                  0, &ss_bcast, true);
+                       if (nmb_bcast == -1) {
+                               DEBUG(0,   ("nmbd_subnetdb:make_subnet()\n"));
+                               DEBUGADD(0,("  Failed to open nmb bcast socket on interface %s ",
+                                           inet_ntoa(bcast_ip)));
+                               DEBUGADD(0,("for port %d.  ", global_nmb_port));
+                               DEBUGADD(0,("Error was %s\n", strerror(errno)));
+                               goto failed;
                        }
-                       return NULL;
+                       set_socket_options(nmb_bcast, "SO_BROADCAST");
+                       set_blocking(nmb_bcast, false);
                }
 
-               if((dgram_sock = open_socket_in(SOCK_DGRAM,DGRAM_PORT,3, myip.s_addr,True)) == -1) {
-                       if( DEBUGLVL( 0 ) ) {
-                               Debug1( "nmbd_subnetdb:make_subnet()\n" );
-                               Debug1( "  Failed to open dgram socket on interface %s ", inet_ntoa(myip) );
-                               Debug1( "for port %d.  ", DGRAM_PORT );
-                               Debug1( "Error was %s\n", strerror(errno) );
+               dgram_sock = open_socket_in(SOCK_DGRAM, DGRAM_PORT,
+                                           3, &ss, true);
+               if (dgram_sock == -1) {
+                       DEBUG(0,   ("nmbd_subnetdb:make_subnet()\n"));
+                       DEBUGADD(0,("  Failed to open dgram socket on interface %s ",
+                                   inet_ntoa(myip)));
+                       DEBUGADD(0,("for port %d.  ", DGRAM_PORT));
+                       DEBUGADD(0,("Error was %s\n", strerror(errno)));
+                       goto failed;
+               }
+               set_socket_options(dgram_sock, "SO_BROADCAST");
+               set_blocking(dgram_sock, false);
+
+               if (bind_bcast) {
+                       dgram_bcast = open_socket_in(SOCK_DGRAM, DGRAM_PORT,
+                                                    3, &ss_bcast, true);
+                       if (dgram_bcast == -1) {
+                               DEBUG(0,   ("nmbd_subnetdb:make_subnet()\n"));
+                               DEBUGADD(0,("  Failed to open dgram bcast socket on interface %s ",
+                                           inet_ntoa(bcast_ip)));
+                               DEBUGADD(0,("for port %d.  ", DGRAM_PORT));
+                               DEBUGADD(0,("Error was %s\n", strerror(errno)));
+                               goto failed;
                        }
-                       return NULL;
+                       set_socket_options(dgram_bcast, "SO_BROADCAST");
+                       set_blocking(dgram_bcast, false);
                }
-
-               /* Make sure we can broadcast from these sockets. */
-               set_socket_options(nmb_sock,"SO_BROADCAST");
-               set_socket_options(dgram_sock,"SO_BROADCAST");
        }
 
-       subrec = (struct subnet_record *)malloc(sizeof(*subrec));
+       subrec = SMB_MALLOC_P(struct subnet_record);
        if (!subrec) {
                DEBUG(0,("make_subnet: malloc fail !\n"));
-               close(nmb_sock);
-               close(dgram_sock);
-               return(NULL);
+               goto failed;
        }
   
-       memset( (char *)subrec, '\0', sizeof(*subrec) );
-       (void)ubi_trInitTree( subrec->namelist,
-                       namelist_entry_compare,
-                       ubi_trOVERWRITE );
+       ZERO_STRUCTP(subrec);
 
-       if((subrec->subnet_name = strdup(name)) == NULL) {
+       if((subrec->subnet_name = SMB_STRDUP(name)) == NULL) {
                DEBUG(0,("make_subnet: malloc fail for subnet name !\n"));
-               close(nmb_sock);
-               close(dgram_sock);
-               ZERO_STRUCTP(subrec);
-               SAFE_FREE(subrec);
-               return(NULL);
+               goto failed;
        }
 
        DEBUG(2, ("making subnet name:%s ", name ));
@@ -181,21 +189,43 @@ static struct subnet_record *make_subnet(const char *name, enum subnet_type type
        subrec->myip = myip;
        subrec->type = type;
        subrec->nmb_sock = nmb_sock;
+       subrec->nmb_bcast = nmb_bcast;
        subrec->dgram_sock = dgram_sock;
-  
+       subrec->dgram_bcast = dgram_bcast;
+
        return subrec;
+
+failed:
+       SAFE_FREE(subrec);
+       if (nmb_sock != -1) {
+               close(nmb_sock);
+       }
+       if (nmb_bcast != -1) {
+               close(nmb_bcast);
+       }
+       if (dgram_sock != -1) {
+               close(dgram_sock);
+       }
+       if (dgram_bcast != -1) {
+               close(dgram_bcast);
+       }
+       return NULL;
 }
 
 /****************************************************************************
   Create a normal subnet
 **************************************************************************/
 
-struct subnet_record *make_normal_subnet(struct interface *iface)
+struct subnet_record *make_normal_subnet(const struct interface *iface)
 {
+
        struct subnet_record *subrec;
+       const struct in_addr *pip = &((const struct sockaddr_in *)&iface->ip)->sin_addr;
+       const struct in_addr *pbcast = &((const struct sockaddr_in *)&iface->bcast)->sin_addr;
+       const struct in_addr *pnmask = &((const struct sockaddr_in *)&iface->netmask)->sin_addr;
 
-       subrec = make_subnet(inet_ntoa(iface->ip), NORMAL_SUBNET,
-                            iface->ip, iface->bcast, iface->nmask);
+       subrec = make_subnet(inet_ntoa(*pip), NORMAL_SUBNET,
+                            *pip, *pbcast, *pnmask);
        if (subrec) {
                add_subnet(subrec);
        }
@@ -206,31 +236,70 @@ struct subnet_record *make_normal_subnet(struct interface *iface)
   Create subnet entries.
 **************************************************************************/
 
-BOOL create_subnets(void)
-{    
-       int num_interfaces = iface_count();
+bool create_subnets(void)
+{
+       /* We only count IPv4 interfaces whilst we're waiting. */
+       int num_interfaces;
        int i;
        struct in_addr unicast_ip, ipzero;
-       extern struct in_addr loopback_ip;
-
-       if(num_interfaces == 0) {
-               DEBUG(0,("create_subnets: No local interfaces !\n"));
-               DEBUG(0,("create_subnets: Waiting for an interface to appear ...\n"));
-               while (iface_count() == 0) {
-                       sleep(5);
-                       load_interfaces();
-               }
+
+  try_interfaces_again:
+
+       /* Only count IPv4, non-loopback interfaces. */
+       if (iface_count_v4_nl() == 0) {
+               daemon_status("nmbd",
+                             "No local IPv4 non-loopback interfaces "
+                             "available, waiting for interface ...");
+               DEBUG(0,("NOTE: NetBIOS name resolution is not supported for "
+                        "Internet Protocol Version 6 (IPv6).\n"));
+       }
+
+       /* We only count IPv4, non-loopback interfaces here. */
+       while (iface_count_v4_nl() == 0) {
+               void (*saved_handler)(int);
+
+               /*
+                * Whilst we're waiting for an interface, allow SIGTERM to
+                * cause us to exit.
+                */
+
+               saved_handler = CatchSignal(SIGTERM, SIG_DFL);
+
+               sleep(5);
+               load_interfaces();
+
+               /*
+                * We got an interface, restore our normal term handler.
+                */
+
+               CatchSignal(SIGTERM, saved_handler);
        }
 
+       /*
+        * Here we count v4 and v6 - we know there's at least one
+        * IPv4 interface and we filter on it below.
+        */
        num_interfaces = iface_count();
 
-       /* 
+       /*
         * Create subnets from all the local interfaces and thread them onto
-        * the linked list. 
+        * the linked list.
         */
 
        for (i = 0 ; i < num_interfaces; i++) {
-               struct interface *iface = get_interface(i);
+               const struct interface *iface = get_interface(i);
+
+               if (!iface) {
+                       DEBUG(2,("create_subnets: can't get interface %d.\n", i ));
+                       continue;
+               }
+
+               /* Ensure we're only dealing with IPv4 here. */
+               if (iface->ip.ss_family != AF_INET) {
+                       DEBUG(2,("create_subnets: "
+                               "ignoring non IPv4 interface.\n"));
+                       continue;
+               }
 
                /*
                 * We don't want to add a loopback interface, in case
@@ -238,7 +307,7 @@ BOOL create_subnets(void)
                 * ignore it here. JRA.
                 */
 
-               if (ip_equal(iface->ip, loopback_ip)) {
+               if (is_loopback_addr((const struct sockaddr *)&iface->ip)) {
                        DEBUG(2,("create_subnets: Ignoring loopback interface.\n" ));
                        continue;
                }
@@ -247,14 +316,38 @@ BOOL create_subnets(void)
                        return False;
        }
 
+        /* We must have at least one subnet. */
+       if (subnetlist == NULL) {
+               void (*saved_handler)(int);
+
+               DEBUG(0,("create_subnets: Unable to create any subnet from "
+                               "given interfaces. Is your interface line in "
+                               "smb.conf correct ?\n"));
+
+               saved_handler = CatchSignal(SIGTERM, SIG_DFL);
+
+               sleep(5);
+               load_interfaces();
+
+               CatchSignal(SIGTERM, saved_handler);
+               goto try_interfaces_again;
+       }
+
        if (lp_we_are_a_wins_server()) {
-               /* Pick the first interface ip address as the WINS server ip. */
-               unicast_ip = *iface_n_ip(0);
+               /* Pick the first interface IPv4 address as the WINS server
+                * ip. */
+               const struct in_addr *nip = first_ipv4_iface();
+
+               if (!nip) {
+                       return False;
+               }
+
+               unicast_ip = *nip;
        } else {
                /* note that we do not set the wins server IP here. We just
                        set it at zero and let the wins registration code cope
                        with getting the IPs right for each packet */
-               zero_ip(&unicast_ip);
+               zero_ip_v4(&unicast_ip);
        }
 
        /*
@@ -267,7 +360,7 @@ BOOL create_subnets(void)
        unicast_subnet = make_subnet( "UNICAST_SUBNET", UNICAST_SUBNET, 
                                unicast_ip, unicast_ip, unicast_ip);
 
-       zero_ip(&ipzero);
+       zero_ip_v4(&ipzero);
 
        remote_broadcast_subnet = make_subnet( "REMOTE_BROADCAST_SUBNET",
                                REMOTE_BROADCAST_SUBNET,
@@ -295,7 +388,7 @@ BOOL create_subnets(void)
 Function to tell us if we can use the unicast subnet.
 ******************************************************************/
 
-BOOL we_are_a_wins_client(void)
+bool we_are_a_wins_client(void)
 {
        if (wins_srv_count() > 0) {
                return True;