9523152840345e274cccee948ee1950a01f33140
[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 extern int DEBUGLEVEL;
34
35 /* -------------------------------------------------------------------------- **
36  * Variables...
37  *
38  *  lmb_browserlist - This is our local master browser list. 
39  */
40
41 ubi_dlNewList( lmb_browserlist );
42
43
44 /* -------------------------------------------------------------------------- **
45  * Functions...
46  */
47
48 /* ************************************************************************** **
49  * Remove and free a browser list entry.
50  *
51  *  Input:  browc - A pointer to the entry to be removed from the list and
52  *                  freed.
53  *  Output: none.
54  *
55  * ************************************************************************** **
56  */
57 static void remove_lmb_browser_entry( struct browse_cache_record *browc )
58   {
59   safe_free( ubi_dlRemThis( lmb_browserlist, browc ) );
60   } /* remove_lmb_browser_entry */
61
62 /* ************************************************************************** **
63  * Update a browser death time.
64  *
65  *  Input:  browc - Pointer to the entry to be updated.
66  *  Output: none.
67  *
68  * ************************************************************************** **
69  */
70 void update_browser_death_time( struct browse_cache_record *browc )
71   {
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 */
75
76 /* ************************************************************************** **
77  * Create a browser entry and add it to the local master browser list.
78  *
79  *  Input:  work_name
80  *          browser_name
81  *          ip
82  *
83  *  Output: Pointer to the new entry, or NULL if malloc() failed.
84  *
85  * ************************************************************************** **
86  */
87 struct browse_cache_record *create_browser_in_lmb_cache( char *work_name, 
88                                                          char *browser_name, 
89                                                          struct in_addr ip )
90   {
91   struct browse_cache_record *browc;
92   time_t now = time( NULL );
93
94   browc = (struct browse_cache_record *)malloc( sizeof( *browc ) );
95
96   if( NULL == browc )
97     {
98     DEBUG( 0, ("create_browser_in_lmb_cache: malloc fail !\n") );
99     return( NULL );
100     }
101
102   memset( (char *)browc, '\0', sizeof( *browc ) );
103   
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.
107    */
108   browc->sync_time = now + 60;
109
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 );
112
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 );
117   
118   browc->ip = ip;
119  
120   (void)ubi_dlAddTail( lmb_browserlist, browc );
121
122   if( DEBUGLVL( 3 ) )
123     {
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 );
128     }
129   
130   return( browc );
131   } /* create_browser_in_lmb_cache */
132
133 /* ************************************************************************** **
134  * Find a browser entry in the local master browser list.
135  *
136  *  Input:  browser_name  - The name for which to search.
137  *
138  *  Output: A pointer to the matching entry, or NULL if no match was found.
139  *
140  * ************************************************************************** **
141  */
142 struct browse_cache_record *find_browser_in_lmb_cache( char *browser_name )
143   {
144   struct browse_cache_record *browc;
145
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;
151
152   return( browc );
153   } /* find_browser_in_lmb_cache */
154
155 /* ************************************************************************** **
156  *  Expire timed out browsers in the browserlist.
157  *
158  *  Input:  t - Expiration time.  Entries with death times less than this
159  *              value will be removed from the list.
160  *  Output: none.
161  *
162  * ************************************************************************** **
163  */
164 void expire_lmb_browsers( time_t t )
165   {
166   struct browse_cache_record *browc;
167   struct browse_cache_record *nextbrowc;
168
169   for( browc = (struct browse_cache_record *)ubi_dlFirst( lmb_browserlist );
170        browc;
171        browc = nextbrowc )
172     {
173     nextbrowc = (struct browse_cache_record *)ubi_dlNext( browc );
174
175     if( browc->death_time < t )
176       {
177       if( DEBUGLVL( 3 ) )
178         {
179         Debug1( "nmbd_browserdb:expire_lmb_browsers()\n" );
180         Debug1( "  Removing timed out lmb entry %s\n", browc->lmb_name );
181         }
182       remove_lmb_browser_entry( browc );
183       }
184     }
185   } /* expire_lmb_browsers */