1997-12-13 17:16:07 +03:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
1997-12-13 17:16:07 +03:00
NBT netbios routines and daemon - version 2
1998-01-22 16:27:43 +03:00
Copyright ( C ) Andrew Tridgell 1994 - 1998
Copyright ( C ) Luke Kenneth Casson Leighton 1994 - 1998
Copyright ( C ) Jeremy Allison 1994 - 1998
1997-12-13 17:16:07 +03:00
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
( 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 , write to the Free Software
Foundation , Inc . , 675 Mass Ave , Cambridge , MA 0213 9 , USA .
Revision History :
*/
# include "includes.h"
extern int ClientNMB ;
extern int ClientDGRAM ;
extern int global_nmb_port ;
/* This is the broadcast subnets database. */
struct subnet_record * subnetlist = NULL ;
/* Extra subnets - keep these separate so enumeration code doesn't
run onto it by mistake . */
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. */
/****************************************************************************
Add a subnet into the list .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static void add_subnet ( struct subnet_record * subrec )
{
1999-12-13 16:27:58 +03:00
DLIST_ADD ( subnetlist , subrec ) ;
1997-12-13 17:16:07 +03:00
}
1998-06-30 02:50:49 +04:00
/* ************************************************************************** **
1998-07-25 19:45:42 +04:00
* 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 .
*
1998-06-30 02:50:49 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
1998-07-02 22:49:08 +04:00
static int namelist_entry_compare ( ubi_trItemPtr Item , ubi_trNodePtr Node )
1998-06-30 02:50:49 +04:00
{
struct name_record * NR = ( struct name_record * ) Node ;
1998-07-25 19:45:42 +04:00
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 ) ) ,
1999-12-13 16:27:58 +03:00
nmb_namestr ( Iname ) , nmb_namestr ( & NR - > name ) , ( int ) sizeof ( struct nmb_name ) ) ;
1998-07-25 19:45:42 +04:00
}
1998-06-30 02:50:49 +04:00
return ( memcmp ( Item , & ( NR - > name ) , sizeof ( struct nmb_name ) ) ) ;
} /* namelist_entry_compare */
1999-12-13 16:27:58 +03:00
/****************************************************************************
stop listening on a subnet
we don ' t free the record as we don ' t have proper reference counting for it
yet and it may be in use by a response record
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void close_subnet ( struct subnet_record * subrec )
{
DLIST_REMOVE ( subnetlist , subrec ) ;
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 ;
}
}
1997-12-13 17:16:07 +03:00
/****************************************************************************
Create a subnet entry .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-01-03 11:28:12 +03:00
static struct subnet_record * make_subnet ( const char * name , enum subnet_type type ,
1999-12-13 16:27:58 +03:00
struct in_addr myip , struct in_addr bcast_ip ,
struct in_addr mask_ip )
1997-12-13 17:16:07 +03:00
{
struct subnet_record * subrec = NULL ;
int nmb_sock , dgram_sock ;
/* 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
{
/*
* Attempt to open the sockets on port 137 / 138 for this interface
* and bind them .
* Fail the subnet creation if this fails .
*/
1999-12-13 16:27:58 +03:00
if ( ( nmb_sock = open_socket_in ( SOCK_DGRAM , global_nmb_port , 0 , myip . s_addr , True ) ) = = - 1 )
1997-12-13 17:16:07 +03:00
{
1998-07-25 19:45:42 +04:00
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 ) ) ;
}
1997-12-13 17:16:07 +03:00
return NULL ;
}
1999-12-13 16:27:58 +03:00
if ( ( dgram_sock = open_socket_in ( SOCK_DGRAM , DGRAM_PORT , 3 , myip . s_addr , True ) ) = = - 1 )
1997-12-13 17:16:07 +03:00
{
1998-07-25 19:45:42 +04:00
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 ) ) ;
}
1997-12-13 17:16:07 +03:00
return NULL ;
}
/* 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 ) ) ;
if ( ! subrec )
{
DEBUG ( 0 , ( " make_subnet: malloc fail ! \n " ) ) ;
close ( nmb_sock ) ;
close ( dgram_sock ) ;
return ( NULL ) ;
}
1999-12-13 16:27:58 +03:00
memset ( ( char * ) subrec , ' \0 ' , sizeof ( * subrec ) ) ;
1998-06-10 23:51:58 +04:00
( void ) ubi_trInitTree ( subrec - > namelist ,
namelist_entry_compare ,
ubi_trOVERWRITE ) ;
1997-12-13 17:16:07 +03:00
if ( ( subrec - > subnet_name = strdup ( name ) ) = = NULL )
{
DEBUG ( 0 , ( " make_subnet: malloc fail for subnet name ! \n " ) ) ;
close ( nmb_sock ) ;
close ( dgram_sock ) ;
1998-08-30 08:27:26 +04:00
ZERO_STRUCTP ( subrec ) ;
2001-09-17 08:35:51 +04:00
SAFE_FREE ( subrec ) ;
1997-12-13 17:16:07 +03:00
return ( NULL ) ;
}
DEBUG ( 2 , ( " making subnet name:%s " , name ) ) ;
DEBUG ( 2 , ( " Broadcast address:%s " , inet_ntoa ( bcast_ip ) ) ) ;
DEBUG ( 2 , ( " Subnet mask:%s \n " , inet_ntoa ( mask_ip ) ) ) ;
subrec - > namelist_changed = False ;
subrec - > work_changed = False ;
subrec - > bcast_ip = bcast_ip ;
subrec - > mask_ip = mask_ip ;
subrec - > myip = myip ;
subrec - > type = type ;
subrec - > nmb_sock = nmb_sock ;
subrec - > dgram_sock = dgram_sock ;
return subrec ;
}
1999-12-13 16:27:58 +03:00
/****************************************************************************
Create a normal subnet
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
struct subnet_record * make_normal_subnet ( struct interface * iface )
{
struct subnet_record * subrec ;
subrec = make_subnet ( inet_ntoa ( iface - > ip ) , NORMAL_SUBNET ,
iface - > ip , iface - > bcast , iface - > nmask ) ;
if ( subrec ) {
add_subnet ( subrec ) ;
}
return subrec ;
}
1997-12-13 17:16:07 +03:00
/****************************************************************************
Create subnet entries .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1998-04-13 23:24:06 +04:00
BOOL create_subnets ( void )
1997-12-13 17:16:07 +03:00
{
int num_interfaces = iface_count ( ) ;
int i ;
2001-11-26 06:11:44 +03:00
struct in_addr unicast_ip , ipzero ;
2000-04-03 21:12:25 +04:00
extern struct in_addr loopback_ip ;
1997-12-13 17:16:07 +03:00
2002-07-15 14:35:28 +04:00
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 ( ) ;
}
1997-12-13 17:16:07 +03:00
}
2002-07-15 14:35:28 +04:00
num_interfaces = iface_count ( ) ;
1997-12-13 17:16:07 +03:00
/*
* Create subnets from all the local interfaces and thread them onto
* the linked list .
*/
for ( i = 0 ; i < num_interfaces ; i + + )
{
struct interface * iface = get_interface ( i ) ;
2000-04-03 21:12:25 +04:00
/*
* We don ' t want to add a loopback interface , in case
* someone has added 127.0 .0 .1 for smbd , nmbd needs to
* ignore it here . JRA .
*/
if ( ip_equal ( iface - > ip , loopback_ip ) ) {
DEBUG ( 2 , ( " create_subnets: Ignoring loopback interface. \n " ) ) ;
continue ;
}
1999-12-13 16:27:58 +03:00
if ( ! make_normal_subnet ( iface ) ) return False ;
1997-12-13 17:16:07 +03:00
}
2002-07-15 14:35:28 +04:00
if ( lp_we_are_a_wins_server ( ) ) {
/* Pick the first interface ip address as the WINS server ip. */
unicast_ip = * iface_n_ip ( 0 ) ;
} 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 ) ;
1997-12-13 17:16:07 +03:00
}
/*
* Create the unicast and remote broadcast subnets .
* Don ' t put these onto the linked list .
* The ip address of the unicast subnet is set to be
* the WINS server address , if it exists , or ipzero if not .
*/
unicast_subnet = make_subnet ( " UNICAST_SUBNET " , UNICAST_SUBNET ,
unicast_ip , unicast_ip , unicast_ip ) ;
2001-11-26 06:11:44 +03:00
zero_ip ( & ipzero ) ;
1997-12-13 17:16:07 +03:00
remote_broadcast_subnet = make_subnet ( " REMOTE_BROADCAST_SUBNET " ,
REMOTE_BROADCAST_SUBNET ,
ipzero , ipzero , ipzero ) ;
if ( ( unicast_subnet = = NULL ) | | ( remote_broadcast_subnet = = NULL ) )
return False ;
/*
* If we are WINS server , create the WINS_SERVER_SUBNET - don ' t put on
* the linked list .
*/
if ( lp_we_are_a_wins_server ( ) )
{
2000-07-19 09:32:43 +04:00
if ( ( wins_server_subnet = make_subnet ( " WINS_SERVER_SUBNET " ,
WINS_SERVER_SUBNET ,
ipzero , ipzero , ipzero ) ) = = NULL )
1997-12-13 17:16:07 +03:00
return False ;
}
return True ;
}
/*******************************************************************
Function to tell us if we can use the unicast subnet .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1998-04-13 23:24:06 +04:00
BOOL we_are_a_wins_client ( void )
1997-12-13 17:16:07 +03:00
{
2002-07-15 14:35:28 +04:00
if ( wins_srv_count ( ) > 0 ) {
return True ;
}
1997-12-13 17:16:07 +03:00
2002-07-15 14:35:28 +04:00
return False ;
1997-12-13 17:16:07 +03:00
}
/*******************************************************************
Access function used by NEXT_SUBNET_INCLUDING_UNICAST
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
struct subnet_record * get_next_subnet_maybe_unicast ( struct subnet_record * subrec )
{
if ( subrec = = unicast_subnet )
return NULL ;
else if ( ( subrec - > next = = NULL ) & & we_are_a_wins_client ( ) )
return unicast_subnet ;
else
return subrec - > next ;
}
1998-02-20 22:48:01 +03:00
/*******************************************************************
Access function used by retransmit_or_expire_response_records ( ) in
nmbd_packets . c . Patch from Andrey Alekseyev < fetch @ muffin . arcadia . spb . ru >
Needed when we need to enumerate all the broadcast , unicast and
WINS subnets .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
struct subnet_record * get_next_subnet_maybe_unicast_or_wins_server ( struct subnet_record * subrec )
{
if ( subrec = = unicast_subnet )
1998-03-12 00:11:04 +03:00
{
1998-02-20 22:48:01 +03:00
if ( wins_server_subnet )
return wins_server_subnet ;
else
return NULL ;
1998-03-12 00:11:04 +03:00
}
1998-02-20 22:48:01 +03:00
if ( wins_server_subnet & & subrec = = wins_server_subnet )
return NULL ;
if ( ( subrec - > next = = NULL ) & & we_are_a_wins_client ( ) )
return unicast_subnet ;
else
return subrec - > next ;
}