Changes to allow Samba to be compiled with -Wstrict-prototypes
[kai/samba.git] / source3 / nmbd / nmbd_mynames.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 */
24
25 #include "includes.h"
26
27 extern int DEBUGLEVEL;
28
29 extern char **my_netbios_names;
30 extern pstring myname;
31 extern fstring myworkgroup;
32 extern pstring scope;
33
34 extern uint16 samba_nb_type; /* Samba's NetBIOS type. */
35
36 /****************************************************************************
37  Fail funtion when registering my netbios names.
38   **************************************************************************/
39
40 static void my_name_register_failed(struct subnet_record *subrec,
41                               struct response_record *rrec, struct nmb_name *nmbname)
42 {
43   DEBUG(0,("my_name_register_failed: Failed to register my name %s on subnet %s.\n",
44             namestr(nmbname), subrec->subnet_name));
45 }
46
47 /****************************************************************************
48   Add my workgroup and my given names to the subnet lists.
49   Also add the magic Samba names.
50   **************************************************************************/
51
52 BOOL register_my_workgroup_and_names(void)
53 {
54   struct subnet_record *subrec;
55   struct work_record *work;
56   int i;
57
58   for(subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
59   {
60     /* Create the workgroup on the subnet. */
61     if((work = create_workgroup_on_subnet(subrec, myworkgroup, PERMANENT_TTL)) == NULL)
62     {
63       DEBUG(0,("register_my_workgroup_and_names: Failed to create my workgroup %s on subnet %s. \
64 Exiting.\n", myworkgroup, subrec->subnet_name));
65       return False;
66     }
67
68     /* Each subnet entry, except for the remote_announce_broadcast subnet
69        and the wins_server_subnet has the magic Samba names. */
70     add_samba_names_to_subnet(subrec);
71
72     /* Register all our names including aliases. */
73     for (i=0; my_netbios_names[i]; i++) 
74     {
75       register_name(subrec, my_netbios_names[i],0x20,samba_nb_type,
76                     NULL,
77                     my_name_register_failed, NULL);
78       register_name(subrec, my_netbios_names[i],0x03,samba_nb_type,
79                     NULL,
80                     my_name_register_failed, NULL);
81       register_name(subrec, my_netbios_names[i],0x00,samba_nb_type,
82                     NULL,
83                     my_name_register_failed, NULL);
84     }
85
86     /* Initiate election processing, register the workgroup names etc. */
87     initiate_myworkgroup_startup(subrec, work);
88   }
89
90   /* If we are not a WINS client, we still need to add the magic Samba
91      names and the netbios names to the unicast subnet directly. This is
92      to allow unicast node status requests and queries to still work
93      in a broadcast only environment. */
94
95   if(we_are_a_wins_client() == False)
96   {
97     add_samba_names_to_subnet(unicast_subnet);
98
99     for (i=0; my_netbios_names[i]; i++)
100     {
101       for(subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
102       {
103         /*
104          * Ensure all the IP addresses are added if we are multihomed.
105          */
106         struct nmb_name nmbname;
107
108         make_nmb_name(&nmbname, my_netbios_names[i],0x20, scope);
109         insert_permanent_name_into_unicast(subrec, &nmbname, samba_nb_type);
110
111         make_nmb_name(&nmbname, my_netbios_names[i],0x3, scope);
112         insert_permanent_name_into_unicast(subrec, &nmbname, samba_nb_type);
113
114         make_nmb_name(&nmbname, my_netbios_names[i],0x0, scope);
115         insert_permanent_name_into_unicast(subrec, &nmbname, samba_nb_type);
116       }
117     }
118
119     /*
120      * Add the WORKGROUP<0> and WORKGROUP<1e> group names to the unicast subnet
121      * also for the same reasons.
122      */
123
124     for(subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
125     {
126       /*
127        * Ensure all the IP addresses are added if we are multihomed.
128        */
129       struct nmb_name nmbname;
130
131       make_nmb_name(&nmbname, myworkgroup, 0x0, scope);
132       insert_permanent_name_into_unicast(subrec, &nmbname, samba_nb_type|NB_GROUP);
133
134       make_nmb_name(&nmbname, myworkgroup, 0x1e, scope);
135       insert_permanent_name_into_unicast(subrec, &nmbname, samba_nb_type|NB_GROUP);
136     }
137   }
138
139   return True;
140 }
141
142 /****************************************************************************
143   Remove all the names we registered.
144 **************************************************************************/
145
146 void release_my_names(void)
147 {
148   struct subnet_record *subrec;
149
150   for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
151   {
152     struct name_record *namerec, *nextnamerec;
153
154     for (namerec = subrec->namelist; namerec; namerec = nextnamerec)
155     {
156       nextnamerec = namerec->next;
157       if ((namerec->source == SELF_NAME) && !NAME_IS_DEREGISTERING(namerec))
158         release_name(subrec, namerec, standard_success_release,
159                      NULL, NULL);
160     }
161   }
162 }
163
164 /*******************************************************************
165   Refresh our registered names.
166   ******************************************************************/
167
168 void refresh_my_names(time_t t)
169 {
170   struct subnet_record *subrec;
171
172   for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
173   {
174     struct name_record *namerec;
175           
176     for (namerec = subrec->namelist; namerec; namerec = namerec->next)
177     {
178       /* Each SELF name has an individual time to be refreshed. */
179       if ((namerec->source == SELF_NAME) && (namerec->refresh_time < t) && 
180           (namerec->death_time != PERMANENT_TTL))
181       {
182         /* We cheat here and pretend the refresh is going to be
183            successful & update the refresh times. This stops
184            multiple refresh calls being done. We actually
185            deal with refresh failure in the fail_fn.
186          */
187         if(!is_refresh_already_queued( subrec, namerec))
188           refresh_name(subrec, namerec, NULL, NULL, NULL);
189         namerec->death_time += lp_max_ttl();
190         namerec->refresh_time += lp_max_ttl();
191       }
192     }
193   }
194 }