Removed acconfig.h configure configure.in include/config.h.in: Made smbwrapper not...
[amitay/samba.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 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   struct subnet_record *subrec2;
59
60   if (!subnetlist)
61   {
62     subnetlist = subrec;
63     subrec->prev = NULL;
64     subrec->next = NULL;
65     return;
66   }
67
68   for (subrec2 = subnetlist; subrec2->next; subrec2 = subrec2->next)
69     ;
70
71   subrec2->next = subrec;
72   subrec->next = NULL;
73   subrec->prev = subrec2;
74 }
75
76 /* ************************************************************************** **
77  * Comparison routine for ordering the splay-tree based namelists assoicated
78  * with each subnet record.
79  *
80  *  Input:  Item  - Pointer to the comparison key.
81  *          Node  - Pointer to a node the splay tree.
82  *
83  *  Output: The return value will be <0 , ==0, or >0 depending upon the
84  *          ordinal relationship of the two keys.
85  *
86  * ************************************************************************** **
87  */
88 static int namelist_entry_compare( ubi_trItemPtr Item, ubi_trNodePtr Node )
89   {
90   struct name_record *NR = (struct name_record *)Node;
91
92   if( DEBUGLVL( 10 ) )
93     {
94     struct nmb_name *Iname = (struct nmb_name *)Item;
95
96     Debug1( "nmbd_subnetdb:namelist_entry_compare()\n" );
97     Debug1( "%d == memcmp( \"%s\", \"%s\", %d )\n",
98             memcmp( Item, &(NR->name), sizeof(struct nmb_name) ),
99             nmb_namestr(Iname), nmb_namestr(&NR->name), sizeof(struct nmb_name) );
100     }
101
102   return( memcmp( Item, &(NR->name), sizeof(struct nmb_name) ) ); 
103   } /* namelist_entry_compare */
104
105 /****************************************************************************
106   Create a subnet entry.
107   ****************************************************************************/
108
109 static struct subnet_record *make_subnet(char *name, enum subnet_type type,
110                                          struct in_addr myip, struct in_addr bcast_ip, 
111                                          struct in_addr mask_ip)
112 {
113   struct subnet_record *subrec = NULL;
114   int nmb_sock, dgram_sock;
115
116   /* Check if we are creating a non broadcast subnet - if so don't create
117      sockets.
118    */
119
120   if(type != NORMAL_SUBNET)
121   {
122     nmb_sock = -1;
123     dgram_sock = -1;
124   }
125   else
126   {
127     /*
128      * Attempt to open the sockets on port 137/138 for this interface
129      * and bind them.
130      * Fail the subnet creation if this fails.
131      */
132
133     if((nmb_sock = open_socket_in(SOCK_DGRAM, global_nmb_port,0, myip.s_addr)) == -1)
134     {
135       if( DEBUGLVL( 0 ) )
136       {
137         Debug1( "nmbd_subnetdb:make_subnet()\n" );
138         Debug1( "  Failed to open nmb socket on interface %s ", inet_ntoa(myip) );
139         Debug1( "for port %d.  ", global_nmb_port );
140         Debug1( "Error was %s\n", strerror(errno) );
141       }
142       return NULL;
143     }
144
145     if((dgram_sock = open_socket_in(SOCK_DGRAM,DGRAM_PORT,3, myip.s_addr)) == -1)
146     {
147       if( DEBUGLVL( 0 ) )
148       {
149         Debug1( "nmbd_subnetdb:make_subnet()\n" );
150         Debug1( "  Failed to open dgram socket on interface %s ", inet_ntoa(myip) );
151         Debug1( "for port %d.  ", DGRAM_PORT );
152         Debug1( "Error was %s\n", strerror(errno) );
153       }
154       return NULL;
155     }
156
157     /* Make sure we can broadcast from these sockets. */
158     set_socket_options(nmb_sock,"SO_BROADCAST");
159     set_socket_options(dgram_sock,"SO_BROADCAST");
160
161   }
162
163   subrec = (struct subnet_record *)malloc(sizeof(*subrec));
164   
165   if (!subrec) 
166   {
167     DEBUG(0,("make_subnet: malloc fail !\n"));
168     close(nmb_sock);
169     close(dgram_sock);
170     return(NULL);
171   }
172   
173   bzero( (char *)subrec, sizeof(*subrec) );
174   (void)ubi_trInitTree( subrec->namelist,
175                         namelist_entry_compare,
176                         ubi_trOVERWRITE );
177
178   if((subrec->subnet_name = strdup(name)) == NULL)
179   {
180     DEBUG(0,("make_subnet: malloc fail for subnet name !\n"));
181     close(nmb_sock);
182     close(dgram_sock);
183     ZERO_STRUCTP(subrec);
184     free((char *)subrec);
185     return(NULL);
186   }
187
188   DEBUG(2, ("making subnet name:%s ", name ));
189   DEBUG(2, ("Broadcast address:%s ", inet_ntoa(bcast_ip)));
190   DEBUG(2, ("Subnet mask:%s\n", inet_ntoa(mask_ip)));
191  
192   subrec->namelist_changed = False;
193   subrec->work_changed = False;
194  
195   subrec->bcast_ip = bcast_ip;
196   subrec->mask_ip  = mask_ip;
197   subrec->myip = myip;
198   subrec->type = type;
199   subrec->nmb_sock = nmb_sock;
200   subrec->dgram_sock = dgram_sock;
201   
202   return subrec;
203 }
204
205 /****************************************************************************
206   Create subnet entries.
207 **************************************************************************/
208
209 BOOL create_subnets(void)
210 {    
211   int num_interfaces = iface_count();
212   int i;
213   struct in_addr unicast_ip;
214
215   if(num_interfaces == 0)
216   {
217     DEBUG(0,("create_subnets: No local interfaces !\n"));
218     return False;
219   }
220
221   /* 
222    * Create subnets from all the local interfaces and thread them onto
223    * the linked list. 
224    */
225
226   for (i = 0 ; i < num_interfaces; i++)
227   {
228     struct subnet_record *subrec;
229     struct interface *iface = get_interface(i);
230
231     if((subrec = make_subnet(inet_ntoa(iface->ip), NORMAL_SUBNET,
232                  iface->ip, iface->bcast,iface->nmask)) == NULL)
233       return False;
234     add_subnet(subrec);
235   }
236
237   /* 
238    * If we have been configured to use a WINS server, then try and
239    * get the ip address of it here. If we are the WINS server then
240    * set the unicast subnet address to be the first of our own real
241    * addresses.
242    */
243
244   if(*lp_wins_server())
245   {
246     struct in_addr real_wins_ip;
247     real_wins_ip = *interpret_addr2(lp_wins_server());
248
249     if (!zero_ip(real_wins_ip))
250     {
251       unicast_ip = real_wins_ip;
252     }
253     else
254     {
255       /* The smb.conf's wins server parameter MUST be a host_name
256          or an ip_address. */
257       DEBUG(0,("invalid smb.conf parameter 'wins server'\n"));
258       return False;
259     }
260   } 
261   else if(lp_we_are_a_wins_server())
262   {
263     /* Pick the first interface ip address as the WINS server ip. */
264     unicast_ip = *iface_n_ip(0);
265   }
266   else
267   {
268     /* We should not be using a WINS server at all. Set the
269       ip address of the subnet to be zero. */
270     unicast_ip = ipzero;
271   }
272
273   /*
274    * Create the unicast and remote broadcast subnets.
275    * Don't put these onto the linked list.
276    * The ip address of the unicast subnet is set to be
277    * the WINS server address, if it exists, or ipzero if not.
278    */
279
280   unicast_subnet = make_subnet( "UNICAST_SUBNET", UNICAST_SUBNET, 
281                                  unicast_ip, unicast_ip, unicast_ip);
282
283   remote_broadcast_subnet = make_subnet( "REMOTE_BROADCAST_SUBNET",
284                                          REMOTE_BROADCAST_SUBNET,
285                                          ipzero, ipzero, ipzero);
286
287   if((unicast_subnet == NULL) || (remote_broadcast_subnet == NULL))
288     return False;
289
290   /* 
291    * If we are WINS server, create the WINS_SERVER_SUBNET - don't put on
292    * the linked list.
293    */
294
295   if (lp_we_are_a_wins_server())
296   {
297     if((wins_server_subnet = make_subnet("WINS_SERVER_SUBNET",
298                                        WINS_SERVER_SUBNET, 
299                                        ipzero, ipzero, ipzero)) == NULL)
300       return False;
301   }
302
303   return True;
304 }
305
306 /*******************************************************************
307 Function to tell us if we can use the unicast subnet.
308 ******************************************************************/
309
310 BOOL we_are_a_wins_client(void)
311 {
312   static int cache_we_are_a_wins_client = -1;
313
314   if(cache_we_are_a_wins_client == -1)
315     cache_we_are_a_wins_client = (ip_equal(ipzero, unicast_subnet->myip) ? 
316                                   False : True);
317
318   return cache_we_are_a_wins_client;
319 }
320
321 /*******************************************************************
322 Access function used by NEXT_SUBNET_INCLUDING_UNICAST
323 ******************************************************************/
324
325 struct subnet_record *get_next_subnet_maybe_unicast(struct subnet_record *subrec)
326 {
327   if(subrec == unicast_subnet)
328     return NULL;
329   else if((subrec->next == NULL) && we_are_a_wins_client())
330     return unicast_subnet;
331   else
332     return subrec->next;
333 }
334
335 /*******************************************************************
336  Access function used by retransmit_or_expire_response_records() in
337  nmbd_packets.c. Patch from Andrey Alekseyev <fetch@muffin.arcadia.spb.ru>
338  Needed when we need to enumerate all the broadcast, unicast and
339  WINS subnets.
340 ******************************************************************/
341
342 struct subnet_record *get_next_subnet_maybe_unicast_or_wins_server(struct subnet_record *subrec)
343 {
344   if(subrec == unicast_subnet)
345   {
346     if(wins_server_subnet)
347       return wins_server_subnet;
348     else
349       return NULL;
350   }
351
352   if(wins_server_subnet && subrec == wins_server_subnet)
353     return NULL;
354
355   if((subrec->next == NULL) && we_are_a_wins_client())
356     return unicast_subnet;
357   else
358     return subrec->next;
359 }