Removed version number from file header.
[jra/samba/.git] / source3 / nmbd / nmbd_subnetdb.c
1 /* 
2    Unix SMB/CIFS implementation.
3    NBT netbios routines and daemon - version 2
4    Copyright (C) Andrew Tridgell 1994-1998
5    Copyright (C) Luke Kenneth Casson Leighton 1994-1998
6    Copyright (C) Jeremy Allison 1994-1998
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21    
22    Revision History:
23
24 */
25
26 #include "includes.h"
27 #include "smb.h"
28
29 extern int ClientNMB;
30 extern int ClientDGRAM;
31 extern int global_nmb_port;
32
33 extern fstring myworkgroup;
34 extern char **my_netbios_names;
35
36 /* This is the broadcast subnets database. */
37 struct subnet_record *subnetlist = NULL;
38
39 /* Extra subnets - keep these separate so enumeration code doesn't
40    run onto it by mistake. */
41
42 struct subnet_record *unicast_subnet = NULL;
43 struct subnet_record *remote_broadcast_subnet = NULL;
44 struct subnet_record *wins_server_subnet = NULL;
45
46 extern uint16 samba_nb_type; /* Samba's NetBIOS name type. */
47
48 /****************************************************************************
49   Add a subnet into the list.
50   **************************************************************************/
51
52 static void add_subnet(struct subnet_record *subrec)
53 {
54         DLIST_ADD(subnetlist, subrec);
55 }
56
57 /* ************************************************************************** **
58  * Comparison routine for ordering the splay-tree based namelists assoicated
59  * with each subnet record.
60  *
61  *  Input:  Item  - Pointer to the comparison key.
62  *          Node  - Pointer to a node the splay tree.
63  *
64  *  Output: The return value will be <0 , ==0, or >0 depending upon the
65  *          ordinal relationship of the two keys.
66  *
67  * ************************************************************************** **
68  */
69 static int namelist_entry_compare( ubi_trItemPtr Item, ubi_trNodePtr Node )
70   {
71   struct name_record *NR = (struct name_record *)Node;
72
73   if( DEBUGLVL( 10 ) )
74     {
75     struct nmb_name *Iname = (struct nmb_name *)Item;
76
77     Debug1( "nmbd_subnetdb:namelist_entry_compare()\n" );
78     Debug1( "%d == memcmp( \"%s\", \"%s\", %d )\n",
79             memcmp( Item, &(NR->name), sizeof(struct nmb_name) ),
80             nmb_namestr(Iname), nmb_namestr(&NR->name), (int)sizeof(struct nmb_name) );
81     }
82
83   return( memcmp( Item, &(NR->name), sizeof(struct nmb_name) ) ); 
84   } /* namelist_entry_compare */
85
86
87 /****************************************************************************
88 stop listening on a subnet
89 we don't free the record as we don't have proper reference counting for it
90 yet and it may be in use by a response record
91   ****************************************************************************/
92 void close_subnet(struct subnet_record *subrec)
93 {
94         DLIST_REMOVE(subnetlist, subrec);
95
96         if (subrec->dgram_sock != -1) {
97                 close(subrec->dgram_sock);
98                 subrec->dgram_sock = -1;
99         }
100         if (subrec->nmb_sock != -1) {
101                 close(subrec->nmb_sock);
102                 subrec->nmb_sock = -1;
103         }
104 }
105
106
107
108 /****************************************************************************
109   Create a subnet entry.
110   ****************************************************************************/
111
112 static struct subnet_record *make_subnet(char *name, enum subnet_type type,
113                                          struct in_addr myip, struct in_addr bcast_ip, 
114                                          struct in_addr mask_ip)
115 {
116   struct subnet_record *subrec = NULL;
117   int nmb_sock, dgram_sock;
118
119   /* Check if we are creating a non broadcast subnet - if so don't create
120      sockets.
121    */
122
123   if(type != NORMAL_SUBNET)
124   {
125     nmb_sock = -1;
126     dgram_sock = -1;
127   }
128   else
129   {
130     /*
131      * Attempt to open the sockets on port 137/138 for this interface
132      * and bind them.
133      * Fail the subnet creation if this fails.
134      */
135
136     if((nmb_sock = open_socket_in(SOCK_DGRAM, global_nmb_port,0, myip.s_addr,True)) == -1)
137     {
138       if( DEBUGLVL( 0 ) )
139       {
140         Debug1( "nmbd_subnetdb:make_subnet()\n" );
141         Debug1( "  Failed to open nmb socket on interface %s ", inet_ntoa(myip) );
142         Debug1( "for port %d.  ", global_nmb_port );
143         Debug1( "Error was %s\n", strerror(errno) );
144       }
145       return NULL;
146     }
147
148     if((dgram_sock = open_socket_in(SOCK_DGRAM,DGRAM_PORT,3, myip.s_addr,True)) == -1)
149     {
150       if( DEBUGLVL( 0 ) )
151       {
152         Debug1( "nmbd_subnetdb:make_subnet()\n" );
153         Debug1( "  Failed to open dgram socket on interface %s ", inet_ntoa(myip) );
154         Debug1( "for port %d.  ", DGRAM_PORT );
155         Debug1( "Error was %s\n", strerror(errno) );
156       }
157       return NULL;
158     }
159
160     /* Make sure we can broadcast from these sockets. */
161     set_socket_options(nmb_sock,"SO_BROADCAST");
162     set_socket_options(dgram_sock,"SO_BROADCAST");
163
164   }
165
166   subrec = (struct subnet_record *)malloc(sizeof(*subrec));
167   
168   if (!subrec) 
169   {
170     DEBUG(0,("make_subnet: malloc fail !\n"));
171     close(nmb_sock);
172     close(dgram_sock);
173     return(NULL);
174   }
175   
176   memset( (char *)subrec, '\0', sizeof(*subrec) );
177   (void)ubi_trInitTree( subrec->namelist,
178                         namelist_entry_compare,
179                         ubi_trOVERWRITE );
180
181   if((subrec->subnet_name = strdup(name)) == NULL)
182   {
183     DEBUG(0,("make_subnet: malloc fail for subnet name !\n"));
184     close(nmb_sock);
185     close(dgram_sock);
186     ZERO_STRUCTP(subrec);
187     SAFE_FREE(subrec);
188     return(NULL);
189   }
190
191   DEBUG(2, ("making subnet name:%s ", name ));
192   DEBUG(2, ("Broadcast address:%s ", inet_ntoa(bcast_ip)));
193   DEBUG(2, ("Subnet mask:%s\n", inet_ntoa(mask_ip)));
194  
195   subrec->namelist_changed = False;
196   subrec->work_changed = False;
197  
198   subrec->bcast_ip = bcast_ip;
199   subrec->mask_ip  = mask_ip;
200   subrec->myip = myip;
201   subrec->type = type;
202   subrec->nmb_sock = nmb_sock;
203   subrec->dgram_sock = dgram_sock;
204   
205   return subrec;
206 }
207
208
209 /****************************************************************************
210   Create a normal subnet
211 **************************************************************************/
212 struct subnet_record *make_normal_subnet(struct interface *iface)
213 {
214         struct subnet_record *subrec;
215
216         subrec = make_subnet(inet_ntoa(iface->ip), NORMAL_SUBNET,
217                              iface->ip, iface->bcast, iface->nmask);
218         if (subrec) {
219                 add_subnet(subrec);
220         }
221         return subrec;
222 }
223
224
225 /****************************************************************************
226   Create subnet entries.
227 **************************************************************************/
228
229 BOOL create_subnets(void)
230 {    
231   int num_interfaces = iface_count();
232   int i;
233   struct in_addr unicast_ip, ipzero;
234   extern struct in_addr loopback_ip;
235
236   if(num_interfaces == 0)
237   {
238     DEBUG(0,("create_subnets: No local interfaces !\n"));
239     return False;
240   }
241
242   /* 
243    * Create subnets from all the local interfaces and thread them onto
244    * the linked list. 
245    */
246
247   for (i = 0 ; i < num_interfaces; i++)
248   {
249     struct interface *iface = get_interface(i);
250
251     /*
252      * We don't want to add a loopback interface, in case
253      * someone has added 127.0.0.1 for smbd, nmbd needs to
254      * ignore it here. JRA.
255      */
256
257     if (ip_equal(iface->ip, loopback_ip)) {
258       DEBUG(2,("create_subnets: Ignoring loopback interface.\n" ));
259       continue;
260     }
261
262     if (!make_normal_subnet(iface)) return False;
263   }
264
265   /* 
266    * If we have been configured to use a WINS server, then try and
267    * get the ip address of it here. If we are the WINS server then
268    * set the unicast subnet address to be the first of our own real
269    * addresses.
270    *
271    * NOTE: I'm not sure of the implications of WINS server failover
272    *       on this bit of code.  Because of failover, the WINS
273    *       server address can change.  crh
274    */
275
276   if( wins_srv_count() )
277   {
278     struct in_addr real_wins_ip;
279     real_wins_ip = wins_srv_ip();
280
281     if (!is_zero_ip(real_wins_ip))
282     {
283       unicast_ip = real_wins_ip;
284     }
285     else
286     {
287       /* wins_srv_ip() can return a zero IP if all servers are
288        * either down or incorrectly entered in smb.conf.  crh
289        */
290       DEBUG(0,("No 'live' WINS servers found.  Check 'wins server' parameter.\n"));
291       return False;
292     }
293   } 
294   else if(lp_we_are_a_wins_server())
295   {
296     /* Pick the first interface ip address as the WINS server ip. */
297     unicast_ip = *iface_n_ip(0);
298   }
299   else
300   {
301     /* We should not be using a WINS server at all. Set the
302       ip address of the subnet to be zero. */
303     zero_ip(&unicast_ip);
304   }
305
306   /*
307    * Create the unicast and remote broadcast subnets.
308    * Don't put these onto the linked list.
309    * The ip address of the unicast subnet is set to be
310    * the WINS server address, if it exists, or ipzero if not.
311    */
312
313   unicast_subnet = make_subnet( "UNICAST_SUBNET", UNICAST_SUBNET, 
314                                  unicast_ip, unicast_ip, unicast_ip);
315
316   zero_ip(&ipzero);
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 = (is_zero_ip(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 }