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