Removed 'extern int DEBUGLEVEL' as it is now in the smb.h header.
[tprouty/samba.git] / source / nmbd / nmbd_subnetdb.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    
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    Revision History:
24
25 */
26
27 #include "includes.h"
28 #include "smb.h"
29
30 extern int ClientNMB;
31 extern int ClientDGRAM;
32 extern int global_nmb_port;
33
34 extern fstring myworkgroup;
35 extern char **my_netbios_names;
36 extern struct in_addr ipzero;
37
38 /* This is the broadcast subnets database. */
39 struct subnet_record *subnetlist = NULL;
40
41 /* Extra subnets - keep these separate so enumeration code doesn't
42    run onto it by mistake. */
43
44 struct subnet_record *unicast_subnet = NULL;
45 struct subnet_record *remote_broadcast_subnet = NULL;
46 struct subnet_record *wins_server_subnet = NULL;
47
48 extern uint16 samba_nb_type; /* Samba's NetBIOS name type. */
49
50 /****************************************************************************
51   Add a subnet into the list.
52   **************************************************************************/
53
54 static void add_subnet(struct subnet_record *subrec)
55 {
56         DLIST_ADD(subnetlist, subrec);
57 }
58
59 /* ************************************************************************** **
60  * Comparison routine for ordering the splay-tree based namelists assoicated
61  * with each subnet record.
62  *
63  *  Input:  Item  - Pointer to the comparison key.
64  *          Node  - Pointer to a node the splay tree.
65  *
66  *  Output: The return value will be <0 , ==0, or >0 depending upon the
67  *          ordinal relationship of the two keys.
68  *
69  * ************************************************************************** **
70  */
71 static int namelist_entry_compare( ubi_trItemPtr Item, ubi_trNodePtr Node )
72   {
73   struct name_record *NR = (struct name_record *)Node;
74
75   if( DEBUGLVL( 10 ) )
76     {
77     struct nmb_name *Iname = (struct nmb_name *)Item;
78
79     Debug1( "nmbd_subnetdb:namelist_entry_compare()\n" );
80     Debug1( "%d == memcmp( \"%s\", \"%s\", %d )\n",
81             memcmp( Item, &(NR->name), sizeof(struct nmb_name) ),
82             nmb_namestr(Iname), nmb_namestr(&NR->name), (int)sizeof(struct nmb_name) );
83     }
84
85   return( memcmp( Item, &(NR->name), sizeof(struct nmb_name) ) ); 
86   } /* namelist_entry_compare */
87
88
89 /****************************************************************************
90 stop listening on a subnet
91 we don't free the record as we don't have proper reference counting for it
92 yet and it may be in use by a response record
93   ****************************************************************************/
94 void close_subnet(struct subnet_record *subrec)
95 {
96         DLIST_REMOVE(subnetlist, subrec);
97
98         if (subrec->dgram_sock != -1) {
99                 close(subrec->dgram_sock);
100                 subrec->dgram_sock = -1;
101         }
102         if (subrec->nmb_sock != -1) {
103                 close(subrec->nmb_sock);
104                 subrec->nmb_sock = -1;
105         }
106 }
107
108
109
110 /****************************************************************************
111   Create a subnet entry.
112   ****************************************************************************/
113
114 static struct subnet_record *make_subnet(char *name, enum subnet_type type,
115                                          struct in_addr myip, struct in_addr bcast_ip, 
116                                          struct in_addr mask_ip)
117 {
118   struct subnet_record *subrec = NULL;
119   int nmb_sock, dgram_sock;
120
121   /* Check if we are creating a non broadcast subnet - if so don't create
122      sockets.
123    */
124
125   if(type != NORMAL_SUBNET)
126   {
127     nmb_sock = -1;
128     dgram_sock = -1;
129   }
130   else
131   {
132     /*
133      * Attempt to open the sockets on port 137/138 for this interface
134      * and bind them.
135      * Fail the subnet creation if this fails.
136      */
137
138     if((nmb_sock = open_socket_in(SOCK_DGRAM, global_nmb_port,0, myip.s_addr,True)) == -1)
139     {
140       if( DEBUGLVL( 0 ) )
141       {
142         Debug1( "nmbd_subnetdb:make_subnet()\n" );
143         Debug1( "  Failed to open nmb socket on interface %s ", inet_ntoa(myip) );
144         Debug1( "for port %d.  ", global_nmb_port );
145         Debug1( "Error was %s\n", strerror(errno) );
146       }
147       return NULL;
148     }
149
150     if((dgram_sock = open_socket_in(SOCK_DGRAM,DGRAM_PORT,3, myip.s_addr,True)) == -1)
151     {
152       if( DEBUGLVL( 0 ) )
153       {
154         Debug1( "nmbd_subnetdb:make_subnet()\n" );
155         Debug1( "  Failed to open dgram socket on interface %s ", inet_ntoa(myip) );
156         Debug1( "for port %d.  ", DGRAM_PORT );
157         Debug1( "Error was %s\n", strerror(errno) );
158       }
159       return NULL;
160     }
161
162     /* Make sure we can broadcast from these sockets. */
163     set_socket_options(nmb_sock,"SO_BROADCAST");
164     set_socket_options(dgram_sock,"SO_BROADCAST");
165
166   }
167
168   subrec = (struct subnet_record *)malloc(sizeof(*subrec));
169   
170   if (!subrec) 
171   {
172     DEBUG(0,("make_subnet: malloc fail !\n"));
173     close(nmb_sock);
174     close(dgram_sock);
175     return(NULL);
176   }
177   
178   memset( (char *)subrec, '\0', sizeof(*subrec) );
179   (void)ubi_trInitTree( subrec->namelist,
180                         namelist_entry_compare,
181                         ubi_trOVERWRITE );
182
183   if((subrec->subnet_name = strdup(name)) == NULL)
184   {
185     DEBUG(0,("make_subnet: malloc fail for subnet name !\n"));
186     close(nmb_sock);
187     close(dgram_sock);
188     ZERO_STRUCTP(subrec);
189     SAFE_FREE(subrec);
190     return(NULL);
191   }
192
193   DEBUG(2, ("making subnet name:%s ", name ));
194   DEBUG(2, ("Broadcast address:%s ", inet_ntoa(bcast_ip)));
195   DEBUG(2, ("Subnet mask:%s\n", inet_ntoa(mask_ip)));
196  
197   subrec->namelist_changed = False;
198   subrec->work_changed = False;
199  
200   subrec->bcast_ip = bcast_ip;
201   subrec->mask_ip  = mask_ip;
202   subrec->myip = myip;
203   subrec->type = type;
204   subrec->nmb_sock = nmb_sock;
205   subrec->dgram_sock = dgram_sock;
206   
207   return subrec;
208 }
209
210
211 /****************************************************************************
212   Create a normal subnet
213 **************************************************************************/
214 struct subnet_record *make_normal_subnet(struct interface *iface)
215 {
216         struct subnet_record *subrec;
217
218         subrec = make_subnet(inet_ntoa(iface->ip), NORMAL_SUBNET,
219                              iface->ip, iface->bcast, iface->nmask);
220         if (subrec) {
221                 add_subnet(subrec);
222         }
223         return subrec;
224 }
225
226
227 /****************************************************************************
228   Create subnet entries.
229 **************************************************************************/
230
231 BOOL create_subnets(void)
232 {    
233   int num_interfaces = iface_count();
234   int i;
235   struct in_addr unicast_ip;
236   extern struct in_addr loopback_ip;
237
238   if(num_interfaces == 0)
239   {
240     DEBUG(0,("create_subnets: No local interfaces !\n"));
241     return False;
242   }
243
244   /* 
245    * Create subnets from all the local interfaces and thread them onto
246    * the linked list. 
247    */
248
249   for (i = 0 ; i < num_interfaces; i++)
250   {
251     struct interface *iface = get_interface(i);
252
253     /*
254      * We don't want to add a loopback interface, in case
255      * someone has added 127.0.0.1 for smbd, nmbd needs to
256      * ignore it here. JRA.
257      */
258
259     if (ip_equal(iface->ip, loopback_ip)) {
260       DEBUG(2,("create_subnets: Ignoring loopback interface.\n" ));
261       continue;
262     }
263
264     if (!make_normal_subnet(iface)) return False;
265   }
266
267   /* 
268    * If we have been configured to use a WINS server, then try and
269    * get the ip address of it here. If we are the WINS server then
270    * set the unicast subnet address to be the first of our own real
271    * addresses.
272    *
273    * NOTE: I'm not sure of the implications of WINS server failover
274    *       on this bit of code.  Because of failover, the WINS
275    *       server address can change.  crh
276    */
277
278   if( wins_srv_count() )
279   {
280     struct in_addr real_wins_ip;
281     real_wins_ip = wins_srv_ip();
282
283     if (!zero_ip(real_wins_ip))
284     {
285       unicast_ip = real_wins_ip;
286     }
287     else
288     {
289       /* wins_srv_ip() can return a zero IP if all servers are
290        * either down or incorrectly entered in smb.conf.  crh
291        */
292       DEBUG(0,("No 'live' WINS servers found.  Check 'wins server' parameter.\n"));
293       return False;
294     }
295   } 
296   else if(lp_we_are_a_wins_server())
297   {
298     /* Pick the first interface ip address as the WINS server ip. */
299     unicast_ip = *iface_n_ip(0);
300   }
301   else
302   {
303     /* We should not be using a WINS server at all. Set the
304       ip address of the subnet to be zero. */
305     unicast_ip = ipzero;
306   }
307
308   /*
309    * Create the unicast and remote broadcast subnets.
310    * Don't put these onto the linked list.
311    * The ip address of the unicast subnet is set to be
312    * the WINS server address, if it exists, or ipzero if not.
313    */
314
315   unicast_subnet = make_subnet( "UNICAST_SUBNET", UNICAST_SUBNET, 
316                                  unicast_ip, unicast_ip, unicast_ip);
317
318   remote_broadcast_subnet = make_subnet( "REMOTE_BROADCAST_SUBNET",
319                                          REMOTE_BROADCAST_SUBNET,
320                                          ipzero, ipzero, ipzero);
321
322   if((unicast_subnet == NULL) || (remote_broadcast_subnet == NULL))
323     return False;
324
325   /* 
326    * If we are WINS server, create the WINS_SERVER_SUBNET - don't put on
327    * the linked list.
328    */
329
330   if (lp_we_are_a_wins_server())
331   {
332     if( (wins_server_subnet = make_subnet( "WINS_SERVER_SUBNET",
333                                            WINS_SERVER_SUBNET, 
334                                            ipzero, ipzero, ipzero )) == NULL )
335       return False;
336   }
337
338   return True;
339 }
340
341 /*******************************************************************
342 Function to tell us if we can use the unicast subnet.
343 ******************************************************************/
344
345 BOOL we_are_a_wins_client(void)
346 {
347   static int cache_we_are_a_wins_client = -1;
348
349   if(cache_we_are_a_wins_client == -1)
350     cache_we_are_a_wins_client = (ip_equal(ipzero, unicast_subnet->myip) ? 
351                                   False : True);
352
353   return cache_we_are_a_wins_client;
354 }
355
356 /*******************************************************************
357 Access function used by NEXT_SUBNET_INCLUDING_UNICAST
358 ******************************************************************/
359
360 struct subnet_record *get_next_subnet_maybe_unicast(struct subnet_record *subrec)
361 {
362   if(subrec == unicast_subnet)
363     return NULL;
364   else if((subrec->next == NULL) && we_are_a_wins_client())
365     return unicast_subnet;
366   else
367     return subrec->next;
368 }
369
370 /*******************************************************************
371  Access function used by retransmit_or_expire_response_records() in
372  nmbd_packets.c. Patch from Andrey Alekseyev <fetch@muffin.arcadia.spb.ru>
373  Needed when we need to enumerate all the broadcast, unicast and
374  WINS subnets.
375 ******************************************************************/
376
377 struct subnet_record *get_next_subnet_maybe_unicast_or_wins_server(struct subnet_record *subrec)
378 {
379   if(subrec == unicast_subnet)
380   {
381     if(wins_server_subnet)
382       return wins_server_subnet;
383     else
384       return NULL;
385   }
386
387   if(wins_server_subnet && subrec == wins_server_subnet)
388     return NULL;
389
390   if((subrec->next == NULL) && we_are_a_wins_client())
391     return unicast_subnet;
392   else
393     return subrec->next;
394 }