| 1 | /* 
 | 
|---|
| 2 |    Unix SMB/CIFS implementation.
 | 
|---|
| 3 |    NBT netbios routines and daemon - version 2
 | 
|---|
| 4 |    Copyright (C) Andrew Tridgell 1994-1998
 | 
|---|
| 5 |    Copyright (C) Luke Kenneth Casson Leighton 1994-1998
 | 
|---|
| 6 |    Copyright (C) Jeremy Allison 1994-1998
 | 
|---|
| 7 |    Copyright (C) Christopher R. Hertel 1998
 | 
|---|
| 8 |    
 | 
|---|
| 9 |    This program is free software; you can redistribute it and/or modify
 | 
|---|
| 10 |    it under the terms of the GNU General Public License as published by
 | 
|---|
| 11 |    the Free Software Foundation; either version 3 of the License, or
 | 
|---|
| 12 |    (at your option) any later version.
 | 
|---|
| 13 |    
 | 
|---|
| 14 |    This program is distributed in the hope that it will be useful,
 | 
|---|
| 15 |    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 16 |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 17 |    GNU General Public License for more details.
 | 
|---|
| 18 |    
 | 
|---|
| 19 |    You should have received a copy of the GNU General Public License
 | 
|---|
| 20 |    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| 21 |    
 | 
|---|
| 22 | */
 | 
|---|
| 23 | /* -------------------------------------------------------------------------- **
 | 
|---|
| 24 |  * Modified July 1998 by CRH.
 | 
|---|
| 25 |  *  I converted this module to use the canned doubly-linked lists.  I also
 | 
|---|
| 26 |  *  added comments above the functions where possible.
 | 
|---|
| 27 |  */
 | 
|---|
| 28 | 
 | 
|---|
| 29 | #include "includes.h"
 | 
|---|
| 30 | 
 | 
|---|
| 31 | /* -------------------------------------------------------------------------- **
 | 
|---|
| 32 |  * Variables...
 | 
|---|
| 33 |  *
 | 
|---|
| 34 |  *  lmb_browserlist - This is our local master browser list. 
 | 
|---|
| 35 |  */
 | 
|---|
| 36 | 
 | 
|---|
| 37 | struct browse_cache_record *lmb_browserlist;
 | 
|---|
| 38 | 
 | 
|---|
| 39 | /* -------------------------------------------------------------------------- **
 | 
|---|
| 40 |  * Functions...
 | 
|---|
| 41 |  */
 | 
|---|
| 42 | 
 | 
|---|
| 43 | /* ************************************************************************** **
 | 
|---|
| 44 |  * Remove and free a browser list entry.
 | 
|---|
| 45 |  *
 | 
|---|
| 46 |  *  Input:  browc - A pointer to the entry to be removed from the list and
 | 
|---|
| 47 |  *                  freed.
 | 
|---|
| 48 |  *  Output: none.
 | 
|---|
| 49 |  *
 | 
|---|
| 50 |  * ************************************************************************** **
 | 
|---|
| 51 |  */
 | 
|---|
| 52 | static void remove_lmb_browser_entry( struct browse_cache_record *browc )
 | 
|---|
| 53 | {
 | 
|---|
| 54 |         DLIST_REMOVE(lmb_browserlist, browc);
 | 
|---|
| 55 |         SAFE_FREE(browc);
 | 
|---|
| 56 | }
 | 
|---|
| 57 | 
 | 
|---|
| 58 | /* ************************************************************************** **
 | 
|---|
| 59 |  * Update a browser death time.
 | 
|---|
| 60 |  *
 | 
|---|
| 61 |  *  Input:  browc - Pointer to the entry to be updated.
 | 
|---|
| 62 |  *  Output: none.
 | 
|---|
| 63 |  *
 | 
|---|
| 64 |  * ************************************************************************** **
 | 
|---|
| 65 |  */
 | 
|---|
| 66 | void update_browser_death_time( struct browse_cache_record *browc )
 | 
|---|
| 67 | {
 | 
|---|
| 68 |         /* Allow the new lmb to miss an announce period before we remove it. */
 | 
|---|
| 69 |         browc->death_time = time(NULL) + ( (CHECK_TIME_MST_ANNOUNCE + 2) * 60 );
 | 
|---|
| 70 | }
 | 
|---|
| 71 | 
 | 
|---|
| 72 | /* ************************************************************************** **
 | 
|---|
| 73 |  * Create a browser entry and add it to the local master browser list.
 | 
|---|
| 74 |  *
 | 
|---|
| 75 |  *  Input:  work_name
 | 
|---|
| 76 |  *          browser_name
 | 
|---|
| 77 |  *          ip
 | 
|---|
| 78 |  *
 | 
|---|
| 79 |  *  Output: Pointer to the new entry, or NULL if malloc() failed.
 | 
|---|
| 80 |  *
 | 
|---|
| 81 |  * ************************************************************************** **
 | 
|---|
| 82 |  */
 | 
|---|
| 83 | struct browse_cache_record *create_browser_in_lmb_cache( const char *work_name, 
 | 
|---|
| 84 |                                                          const char *browser_name, 
 | 
|---|
| 85 |                                                          struct in_addr ip )
 | 
|---|
| 86 | {
 | 
|---|
| 87 |         struct browse_cache_record *browc;
 | 
|---|
| 88 |         time_t now = time( NULL );
 | 
|---|
| 89 | 
 | 
|---|
| 90 |         browc = SMB_MALLOC_P(struct browse_cache_record);
 | 
|---|
| 91 | 
 | 
|---|
| 92 |         if( NULL == browc ) {
 | 
|---|
| 93 |                 DEBUG( 0, ("create_browser_in_lmb_cache: malloc fail !\n") );
 | 
|---|
| 94 |                 return( NULL );
 | 
|---|
| 95 |         }
 | 
|---|
| 96 | 
 | 
|---|
| 97 |         memset( (char *)browc, '\0', sizeof( *browc ) );
 | 
|---|
| 98 |   
 | 
|---|
| 99 |         /* For a new lmb entry we want to sync with it after one minute. This
 | 
|---|
| 100 |          will allow it time to send out a local announce and build its
 | 
|---|
| 101 |          browse list.
 | 
|---|
| 102 |         */
 | 
|---|
| 103 | 
 | 
|---|
| 104 |         browc->sync_time = now + 60;
 | 
|---|
| 105 | 
 | 
|---|
| 106 |         /* Allow the new lmb to miss an announce period before we remove it. */
 | 
|---|
| 107 |         browc->death_time = now + ( (CHECK_TIME_MST_ANNOUNCE + 2) * 60 );
 | 
|---|
| 108 | 
 | 
|---|
| 109 |         unstrcpy( browc->lmb_name, browser_name);
 | 
|---|
| 110 |         unstrcpy( browc->work_group, work_name);
 | 
|---|
| 111 |         strupper_m( browc->lmb_name );
 | 
|---|
| 112 |         strupper_m( browc->work_group );
 | 
|---|
| 113 |   
 | 
|---|
| 114 |         browc->ip = ip;
 | 
|---|
| 115 |  
 | 
|---|
| 116 |         DLIST_ADD_END(lmb_browserlist, browc, struct browse_cache_record *);
 | 
|---|
| 117 | 
 | 
|---|
| 118 |         if( DEBUGLVL( 3 ) ) {
 | 
|---|
| 119 |                 Debug1( "nmbd_browserdb:create_browser_in_lmb_cache()\n" );
 | 
|---|
| 120 |                 Debug1( "  Added lmb cache entry for workgroup %s ", browc->work_group );
 | 
|---|
| 121 |                 Debug1( "name %s IP %s ", browc->lmb_name, inet_ntoa(ip) );
 | 
|---|
| 122 |                 Debug1( "ttl %d\n", (int)browc->death_time );
 | 
|---|
| 123 |         }
 | 
|---|
| 124 |   
 | 
|---|
| 125 |         return( browc );
 | 
|---|
| 126 | }
 | 
|---|
| 127 | 
 | 
|---|
| 128 | /* ************************************************************************** **
 | 
|---|
| 129 |  * Find a browser entry in the local master browser list.
 | 
|---|
| 130 |  *
 | 
|---|
| 131 |  *  Input:  browser_name  - The name for which to search.
 | 
|---|
| 132 |  *
 | 
|---|
| 133 |  *  Output: A pointer to the matching entry, or NULL if no match was found.
 | 
|---|
| 134 |  *
 | 
|---|
| 135 |  * ************************************************************************** **
 | 
|---|
| 136 |  */
 | 
|---|
| 137 | struct browse_cache_record *find_browser_in_lmb_cache( const char *browser_name )
 | 
|---|
| 138 | {
 | 
|---|
| 139 |         struct browse_cache_record *browc;
 | 
|---|
| 140 | 
 | 
|---|
| 141 |         for( browc = lmb_browserlist; browc; browc = browc->next ) {
 | 
|---|
| 142 |                 if( strequal( browser_name, browc->lmb_name ) ) {
 | 
|---|
| 143 |                         break;
 | 
|---|
| 144 |                 }
 | 
|---|
| 145 |         }
 | 
|---|
| 146 | 
 | 
|---|
| 147 |         return browc;
 | 
|---|
| 148 | }
 | 
|---|
| 149 | 
 | 
|---|
| 150 | /* ************************************************************************** **
 | 
|---|
| 151 |  *  Expire timed out browsers in the browserlist.
 | 
|---|
| 152 |  *
 | 
|---|
| 153 |  *  Input:  t - Expiration time.  Entries with death times less than this
 | 
|---|
| 154 |  *              value will be removed from the list.
 | 
|---|
| 155 |  *  Output: none.
 | 
|---|
| 156 |  *
 | 
|---|
| 157 |  * ************************************************************************** **
 | 
|---|
| 158 |  */
 | 
|---|
| 159 | void expire_lmb_browsers( time_t t )
 | 
|---|
| 160 | {
 | 
|---|
| 161 |         struct browse_cache_record *browc;
 | 
|---|
| 162 |         struct browse_cache_record *nextbrowc;
 | 
|---|
| 163 | 
 | 
|---|
| 164 |         for( browc = lmb_browserlist; browc; browc = nextbrowc) {
 | 
|---|
| 165 |                 nextbrowc = browc->next;
 | 
|---|
| 166 | 
 | 
|---|
| 167 |                 if( browc->death_time < t ) {
 | 
|---|
| 168 |                         if( DEBUGLVL( 3 ) ) {
 | 
|---|
| 169 |                                 Debug1( "nmbd_browserdb:expire_lmb_browsers()\n" );
 | 
|---|
| 170 |                                 Debug1( "  Removing timed out lmb entry %s\n", browc->lmb_name );
 | 
|---|
| 171 |                         }
 | 
|---|
| 172 |                         remove_lmb_browser_entry( browc );
 | 
|---|
| 173 |                 }
 | 
|---|
| 174 |         }
 | 
|---|
| 175 | }
 | 
|---|