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