Removed 'extern int DEBUGLEVEL' as it is now in the smb.h header.
[kai/samba.git] / source3 / nmbd / nmbd_browserdb.c
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
9    
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.
14    
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.
19    
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.
23    
24 */
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.
29  */
30
31 #include "includes.h"
32
33 /* -------------------------------------------------------------------------- **
34  * Variables...
35  *
36  *  lmb_browserlist - This is our local master browser list. 
37  */
38
39 ubi_dlNewList( lmb_browserlist );
40
41
42 /* -------------------------------------------------------------------------- **
43  * Functions...
44  */
45
46 /* ************************************************************************** **
47  * Remove and free a browser list entry.
48  *
49  *  Input:  browc - A pointer to the entry to be removed from the list and
50  *                  freed.
51  *  Output: none.
52  *
53  * ************************************************************************** **
54  */
55 static void remove_lmb_browser_entry( struct browse_cache_record *browc )
56   {
57   safe_free( ubi_dlRemThis( lmb_browserlist, browc ) );
58   } /* remove_lmb_browser_entry */
59
60 /* ************************************************************************** **
61  * Update a browser death time.
62  *
63  *  Input:  browc - Pointer to the entry to be updated.
64  *  Output: none.
65  *
66  * ************************************************************************** **
67  */
68 void update_browser_death_time( struct browse_cache_record *browc )
69   {
70   /* Allow the new lmb to miss an announce period before we remove it. */
71   browc->death_time = time(NULL) + ( (CHECK_TIME_MST_ANNOUNCE + 2) * 60 );
72   } /* update_browser_death_time */
73
74 /* ************************************************************************** **
75  * Create a browser entry and add it to the local master browser list.
76  *
77  *  Input:  work_name
78  *          browser_name
79  *          ip
80  *
81  *  Output: Pointer to the new entry, or NULL if malloc() failed.
82  *
83  * ************************************************************************** **
84  */
85 struct browse_cache_record *create_browser_in_lmb_cache( char *work_name, 
86                                                          char *browser_name, 
87                                                          struct in_addr ip )
88   {
89   struct browse_cache_record *browc;
90   time_t now = time( NULL );
91
92   browc = (struct browse_cache_record *)malloc( sizeof( *browc ) );
93
94   if( NULL == browc )
95     {
96     DEBUG( 0, ("create_browser_in_lmb_cache: malloc fail !\n") );
97     return( NULL );
98     }
99
100   memset( (char *)browc, '\0', sizeof( *browc ) );
101   
102   /* For a new lmb entry we want to sync with it after one minute. This
103      will allow it time to send out a local announce and build its
104      browse list.
105    */
106   browc->sync_time = now + 60;
107
108   /* Allow the new lmb to miss an announce period before we remove it. */
109   browc->death_time = now + ( (CHECK_TIME_MST_ANNOUNCE + 2) * 60 );
110
111   StrnCpy(  browc->lmb_name, browser_name, sizeof(browc->lmb_name)-1 );
112   StrnCpy(  browc->work_group, work_name, sizeof(browc->work_group)-1 );
113   strupper( browc->lmb_name );
114   strupper( browc->work_group );
115   
116   browc->ip = ip;
117  
118   (void)ubi_dlAddTail( lmb_browserlist, browc );
119
120   if( DEBUGLVL( 3 ) )
121     {
122     Debug1( "nmbd_browserdb:create_browser_in_lmb_cache()\n" );
123     Debug1( "  Added lmb cache entry for workgroup %s ", browc->work_group );
124     Debug1( "name %s IP %s ", browc->lmb_name, inet_ntoa(ip) );
125     Debug1( "ttl %d\n", (int)browc->death_time );
126     }
127   
128   return( browc );
129   } /* create_browser_in_lmb_cache */
130
131 /* ************************************************************************** **
132  * Find a browser entry in the local master browser list.
133  *
134  *  Input:  browser_name  - The name for which to search.
135  *
136  *  Output: A pointer to the matching entry, or NULL if no match was found.
137  *
138  * ************************************************************************** **
139  */
140 struct browse_cache_record *find_browser_in_lmb_cache( char *browser_name )
141   {
142   struct browse_cache_record *browc;
143
144   for( browc = (struct browse_cache_record *)ubi_dlFirst( lmb_browserlist );
145        browc;
146        browc = (struct browse_cache_record *)ubi_dlNext( browc ) )
147     if( strequal( browser_name, browc->lmb_name ) )
148       break;
149
150   return( browc );
151   } /* find_browser_in_lmb_cache */
152
153 /* ************************************************************************** **
154  *  Expire timed out browsers in the browserlist.
155  *
156  *  Input:  t - Expiration time.  Entries with death times less than this
157  *              value will be removed from the list.
158  *  Output: none.
159  *
160  * ************************************************************************** **
161  */
162 void expire_lmb_browsers( time_t t )
163   {
164   struct browse_cache_record *browc;
165   struct browse_cache_record *nextbrowc;
166
167   for( browc = (struct browse_cache_record *)ubi_dlFirst( lmb_browserlist );
168        browc;
169        browc = nextbrowc )
170     {
171     nextbrowc = (struct browse_cache_record *)ubi_dlNext( browc );
172
173     if( browc->death_time < t )
174       {
175       if( DEBUGLVL( 3 ) )
176         {
177         Debug1( "nmbd_browserdb:expire_lmb_browsers()\n" );
178         Debug1( "  Removing timed out lmb entry %s\n", browc->lmb_name );
179         }
180       remove_lmb_browser_entry( browc );
181       }
182     }
183   } /* expire_lmb_browsers */