b2db7443707916e5468b43bf941be2d4fa27e3f3
[samba.git] / source / 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-1997
6    Copyright (C) Luke Kenneth Casson Leighton 1994-1997 
7    Copyright (C) Jeremy Allison 1994-1997
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 2 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, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22    
23 */
24
25 #include "includes.h"
26 #include "smb.h"
27
28 extern int DEBUGLEVEL;
29
30 /* This is our local master browser list database. */
31 struct browse_cache_record *lmb_browserlist = NULL;
32
33 /***************************************************************************
34 Add a browser into the lmb list.
35 **************************************************************************/
36
37 static void add_to_lmb_browse_cache(struct browse_cache_record *browc)
38 {
39   struct browse_cache_record *browc2;
40
41   if (lmb_browserlist == NULL)
42   {
43     lmb_browserlist = browc;
44     browc->prev = NULL;
45     browc->next = NULL;
46     return;
47   }
48   
49   for (browc2 = lmb_browserlist; browc2->next; browc2 = browc2->next)
50     ;
51   
52   browc2->next = browc;
53   browc->next = NULL;
54   browc->prev = browc2;
55 }
56
57 /*******************************************************************
58 Remove a lmb browser entry.
59 ******************************************************************/
60
61 void remove_lmb_browser_entry(struct browse_cache_record *browc)
62 {
63   if (browc->prev)
64     browc->prev->next = browc->next;
65   if (browc->next)
66     browc->next->prev = browc->prev;
67
68   if (lmb_browserlist == browc)
69     lmb_browserlist = browc->next; 
70           
71   free((char *)browc);
72 }
73
74 /****************************************************************************
75 Update a browser death time.
76 ****************************************************************************/
77
78 void update_browser_death_time(struct browse_cache_record *browc)
79 {
80   /* Allow the new lmb to miss an announce period before we remove it. */
81   browc->death_time = time(NULL) + (CHECK_TIME_MST_ANNOUNCE + 2)*60;
82 }
83
84 /****************************************************************************
85 Create a browser entry.
86 ****************************************************************************/
87
88 struct browse_cache_record *create_browser_in_lmb_cache(char *work_name, 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 (browc == NULL)
97   {
98     DEBUG(0,("create_browser_in_lmb_cache: malloc fail !\n"));
99     return(NULL);
100   }
101
102   bzero((char *)browc,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   add_to_lmb_browse_cache(browc);
121       
122   DEBUG(3,("create_browser_in_lmb_cache: Added lmb cache entry for workgroup %s name %s IP %s ttl %d\n",
123             browc->work_group, browc->lmb_name, inet_ntoa(ip), browc->death_time));
124   
125   return(browc);
126 }
127
128 /****************************************************************************
129 Find a browser entry.
130 ****************************************************************************/
131
132 struct browse_cache_record *find_browser_in_lmb_cache( char *browser_name )
133 {
134   struct browse_cache_record *browc = NULL;
135
136   for( browc = lmb_browserlist; browc; browc = browc->next)
137     if(strequal( browser_name, browc->lmb_name))
138       break;
139
140   return browc;
141 }
142
143 /*******************************************************************
144   Expire timed out browsers in the browserlist.
145 ******************************************************************/
146
147 void expire_lmb_browsers(time_t t)
148 {
149   struct browse_cache_record *browc;
150   struct browse_cache_record *nextbrowc;
151
152   for (browc = lmb_browserlist; browc; browc = nextbrowc)
153   {
154     nextbrowc = browc->next;
155
156     if (browc->death_time < t)
157     {
158       DEBUG(3,("expire_lmb_browsers: Removing timed out lmb entry %s\n",browc->lmb_name));
159       remove_lmb_browser_entry(browc);
160     }
161   }
162 }
163
164 /*******************************************************************
165   Remove browsers from a named workgroup in the browserlist.
166 ******************************************************************/
167
168 void remove_workgroup_lmb_browsers(char *work_group)
169 {
170   struct browse_cache_record *browc;
171   struct browse_cache_record *nextbrowc;
172
173   for (browc = lmb_browserlist; browc; browc = nextbrowc)
174   {
175     nextbrowc = browc->next;
176
177     if (strequal(work_group, browc->work_group))
178     {
179       DEBUG(3,("remove_workgroup_browsers: Removing lmb entry %s\n",browc->lmb_name));
180       remove_lmb_browser_entry(browc);
181     }
182   }
183 }
184