00001 /* 00002 Unix SMB/CIFS implementation. 00003 NBT netbios routines and daemon - version 2 00004 Copyright (C) Andrew Tridgell 1994-1998 00005 Copyright (C) Luke Kenneth Casson Leighton 1994-1998 00006 Copyright (C) Jeremy Allison 1994-1998 00007 Copyright (C) Christopher R. Hertel 1998 00008 00009 This program is free software; you can redistribute it and/or modify 00010 it under the terms of the GNU General Public License as published by 00011 the Free Software Foundation; either version 2 of the License, or 00012 (at your option) any later version. 00013 00014 This program is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 GNU General Public License for more details. 00018 00019 You should have received a copy of the GNU General Public License 00020 along with this program; if not, write to the Free Software 00021 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00022 00023 */ 00024 /* -------------------------------------------------------------------------- ** 00025 * Modified July 1998 by CRH. 00026 * I converted this module to use the canned doubly-linked lists. I also 00027 * added comments above the functions where possible. 00028 */ 00029 00030 #include "includes.h" 00031 00032 /* -------------------------------------------------------------------------- ** 00033 * Variables... 00034 * 00035 * lmb_browserlist - This is our local master browser list. 00036 */ 00037 00038 struct browse_cache_record *lmb_browserlist; 00039 00040 /* -------------------------------------------------------------------------- ** 00041 * Functions... 00042 */ 00043 00044 /* ************************************************************************** ** 00045 * Remove and free a browser list entry. 00046 * 00047 * Input: browc - A pointer to the entry to be removed from the list and 00048 * freed. 00049 * Output: none. 00050 * 00051 * ************************************************************************** ** 00052 */ 00053 static void remove_lmb_browser_entry( struct browse_cache_record *browc ) 00054 { 00055 DLIST_REMOVE(lmb_browserlist, browc); 00056 SAFE_FREE(browc); 00057 } 00058 00059 /* ************************************************************************** ** 00060 * Update a browser death time. 00061 * 00062 * Input: browc - Pointer to the entry to be updated. 00063 * Output: none. 00064 * 00065 * ************************************************************************** ** 00066 */ 00067 void update_browser_death_time( struct browse_cache_record *browc ) 00068 { 00069 /* Allow the new lmb to miss an announce period before we remove it. */ 00070 browc->death_time = time(NULL) + ( (CHECK_TIME_MST_ANNOUNCE + 2) * 60 ); 00071 } 00072 00073 /* ************************************************************************** ** 00074 * Create a browser entry and add it to the local master browser list. 00075 * 00076 * Input: work_name 00077 * browser_name 00078 * ip 00079 * 00080 * Output: Pointer to the new entry, or NULL if malloc() failed. 00081 * 00082 * ************************************************************************** ** 00083 */ 00084 struct browse_cache_record *create_browser_in_lmb_cache( const char *work_name, 00085 const char *browser_name, 00086 struct in_addr ip ) 00087 { 00088 struct browse_cache_record *browc; 00089 time_t now = time( NULL ); 00090 00091 browc = SMB_MALLOC_P(struct browse_cache_record); 00092 00093 if( NULL == browc ) { 00094 DEBUG( 0, ("create_browser_in_lmb_cache: malloc fail !\n") ); 00095 return( NULL ); 00096 } 00097 00098 memset( (char *)browc, '\0', sizeof( *browc ) ); 00099 00100 /* For a new lmb entry we want to sync with it after one minute. This 00101 will allow it time to send out a local announce and build its 00102 browse list. 00103 */ 00104 00105 browc->sync_time = now + 60; 00106 00107 /* Allow the new lmb to miss an announce period before we remove it. */ 00108 browc->death_time = now + ( (CHECK_TIME_MST_ANNOUNCE + 2) * 60 ); 00109 00110 unstrcpy( browc->lmb_name, browser_name); 00111 unstrcpy( browc->work_group, work_name); 00112 strupper_m( browc->lmb_name ); 00113 strupper_m( browc->work_group ); 00114 00115 browc->ip = ip; 00116 00117 DLIST_ADD_END(lmb_browserlist, browc, struct browse_cache_record *); 00118 00119 if( DEBUGLVL( 3 ) ) { 00120 Debug1( "nmbd_browserdb:create_browser_in_lmb_cache()\n" ); 00121 Debug1( " Added lmb cache entry for workgroup %s ", browc->work_group ); 00122 Debug1( "name %s IP %s ", browc->lmb_name, inet_ntoa(ip) ); 00123 Debug1( "ttl %d\n", (int)browc->death_time ); 00124 } 00125 00126 return( browc ); 00127 } 00128 00129 /* ************************************************************************** ** 00130 * Find a browser entry in the local master browser list. 00131 * 00132 * Input: browser_name - The name for which to search. 00133 * 00134 * Output: A pointer to the matching entry, or NULL if no match was found. 00135 * 00136 * ************************************************************************** ** 00137 */ 00138 struct browse_cache_record *find_browser_in_lmb_cache( const char *browser_name ) 00139 { 00140 struct browse_cache_record *browc; 00141 00142 for( browc = lmb_browserlist; browc; browc = browc->next ) { 00143 if( strequal( browser_name, browc->lmb_name ) ) { 00144 break; 00145 } 00146 } 00147 00148 return browc; 00149 } 00150 00151 /* ************************************************************************** ** 00152 * Expire timed out browsers in the browserlist. 00153 * 00154 * Input: t - Expiration time. Entries with death times less than this 00155 * value will be removed from the list. 00156 * Output: none. 00157 * 00158 * ************************************************************************** ** 00159 */ 00160 void expire_lmb_browsers( time_t t ) 00161 { 00162 struct browse_cache_record *browc; 00163 struct browse_cache_record *nextbrowc; 00164 00165 for( browc = lmb_browserlist; browc; browc = nextbrowc) { 00166 nextbrowc = browc->next; 00167 00168 if( browc->death_time < t ) { 00169 if( DEBUGLVL( 3 ) ) { 00170 Debug1( "nmbd_browserdb:expire_lmb_browsers()\n" ); 00171 Debug1( " Removing timed out lmb entry %s\n", browc->lmb_name ); 00172 } 00173 remove_lmb_browser_entry( browc ); 00174 } 00175 } 00176 }