renamed macro WITH_TDBSAM to work with new configure script.
[Samba.git] / source / nmbd / nmbd_browserdb.c
blob10d22431e9178e4cb3b4af062ee60a1623e05831
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 NBT netbios routines and daemon - version 2
5 Copyright (C) Andrew Tridgell 1994-1998
6 Copyright (C) Luke Kenneth Casson Leighton 1994-1998
7 Copyright (C) Jeremy Allison 1994-1998
8 Copyright (C) Christopher R. Hertel 1998
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 /* -------------------------------------------------------------------------- **
26 * Modified July 1998 by CRH.
27 * I converted this module to use the canned doubly-linked lists. I also
28 * added comments above the functions where possible.
31 #include "includes.h"
33 extern int DEBUGLEVEL;
35 /* -------------------------------------------------------------------------- **
36 * Variables...
38 * lmb_browserlist - This is our local master browser list.
41 ubi_dlNewList( lmb_browserlist );
44 /* -------------------------------------------------------------------------- **
45 * Functions...
48 /* ************************************************************************** **
49 * Remove and free a browser list entry.
51 * Input: browc - A pointer to the entry to be removed from the list and
52 * freed.
53 * Output: none.
55 * ************************************************************************** **
57 static void remove_lmb_browser_entry( struct browse_cache_record *browc )
59 free( (char *)ubi_dlRemThis( lmb_browserlist, browc ) );
60 } /* remove_lmb_browser_entry */
62 /* ************************************************************************** **
63 * Update a browser death time.
65 * Input: browc - Pointer to the entry to be updated.
66 * Output: none.
68 * ************************************************************************** **
70 void update_browser_death_time( struct browse_cache_record *browc )
72 /* Allow the new lmb to miss an announce period before we remove it. */
73 browc->death_time = time(NULL) + ( (CHECK_TIME_MST_ANNOUNCE + 2) * 60 );
74 } /* update_browser_death_time */
76 /* ************************************************************************** **
77 * Create a browser entry and add it to the local master browser list.
79 * Input: work_name
80 * browser_name
81 * ip
83 * Output: Pointer to the new entry, or NULL if malloc() failed.
85 * ************************************************************************** **
87 struct browse_cache_record *create_browser_in_lmb_cache( char *work_name,
88 char *browser_name,
89 struct in_addr ip )
91 struct browse_cache_record *browc;
92 time_t now = time( NULL );
94 browc = (struct browse_cache_record *)malloc( sizeof( *browc ) );
96 if( NULL == browc )
98 DEBUG( 0, ("create_browser_in_lmb_cache: malloc fail !\n") );
99 return( NULL );
102 memset( (char *)browc, '\0', sizeof( *browc ) );
104 /* For a new lmb entry we want to sync with it after one minute. This
105 will allow it time to send out a local announce and build its
106 browse list.
108 browc->sync_time = now + 60;
110 /* Allow the new lmb to miss an announce period before we remove it. */
111 browc->death_time = now + ( (CHECK_TIME_MST_ANNOUNCE + 2) * 60 );
113 StrnCpy( browc->lmb_name, browser_name, sizeof(browc->lmb_name)-1 );
114 StrnCpy( browc->work_group, work_name, sizeof(browc->work_group)-1 );
115 strupper( browc->lmb_name );
116 strupper( browc->work_group );
118 browc->ip = ip;
120 (void)ubi_dlAddTail( lmb_browserlist, browc );
122 if( DEBUGLVL( 3 ) )
124 Debug1( "nmbd_browserdb:create_browser_in_lmb_cache()\n" );
125 Debug1( " Added lmb cache entry for workgroup %s ", browc->work_group );
126 Debug1( "name %s IP %s ", browc->lmb_name, inet_ntoa(ip) );
127 Debug1( "ttl %d\n", (int)browc->death_time );
130 return( browc );
131 } /* create_browser_in_lmb_cache */
133 /* ************************************************************************** **
134 * Find a browser entry in the local master browser list.
136 * Input: browser_name - The name for which to search.
138 * Output: A pointer to the matching entry, or NULL if no match was found.
140 * ************************************************************************** **
142 struct browse_cache_record *find_browser_in_lmb_cache( char *browser_name )
144 struct browse_cache_record *browc;
146 for( browc = (struct browse_cache_record *)ubi_dlFirst( lmb_browserlist );
147 browc;
148 browc = (struct browse_cache_record *)ubi_dlNext( browc ) )
149 if( strequal( browser_name, browc->lmb_name ) )
150 break;
152 return( browc );
153 } /* find_browser_in_lmb_cache */
155 /* ************************************************************************** **
156 * Expire timed out browsers in the browserlist.
158 * Input: t - Expiration time. Entries with death times less than this
159 * value will be removed from the list.
160 * Output: none.
162 * ************************************************************************** **
164 void expire_lmb_browsers( time_t t )
166 struct browse_cache_record *browc;
167 struct browse_cache_record *nextbrowc;
169 for( browc = (struct browse_cache_record *)ubi_dlFirst( lmb_browserlist );
170 browc;
171 browc = nextbrowc )
173 nextbrowc = (struct browse_cache_record *)ubi_dlNext( browc );
175 if( browc->death_time < t )
177 if( DEBUGLVL( 3 ) )
179 Debug1( "nmbd_browserdb:expire_lmb_browsers()\n" );
180 Debug1( " Removing timed out lmb entry %s\n", browc->lmb_name );
182 remove_lmb_browser_entry( browc );
185 } /* expire_lmb_browsers */