jra@cygnus.com: Changed CHECK_TIME_MST_ANNOUNCE back to 15 from 1 (from test).
[samba.git] / source3 / include / nameserv.h
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    NBT netbios header - version 2
5    Copyright (C) Andrew Tridgell 1994-1995
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20    
21 */
22
23 #define GET_TTL(ttl) ((ttl)?MIN(ttl,lp_max_ttl()):lp_max_ttl())
24
25 /* NTAS uses 2, NT uses 1, WfWg uses 0 */
26 #define MAINTAIN_LIST    2
27 #define ELECTION_VERSION 1
28
29 #define MAX_DGRAM_SIZE (576) /* tcp/ip datagram limit is 576 bytes */
30 #define MIN_DGRAM_SIZE 12
31
32 #define NMB_QUERY  0x20
33 #define NMB_STATUS 0x21
34
35 #define NMB_REG         0x05 /* see rfc1002.txt 4.2.2,3,5,6,7,8 */
36 #define NMB_REG_REFRESH 0x09 /* see rfc1002.txt 4.2.4 */
37 #define NMB_REL         0x06 /* see rfc1002.txt 4.2.9,10,11 */
38 #define NMB_WAIT_ACK    0x07 /* see rfc1002.txt 4.2.16 */
39 /* XXXX what about all the other types?? 0x1, 0x2, 0x3, 0x4, 0x8? */
40
41 #define FIND_SELF  0x01
42 #define FIND_WINS  0x02
43 #define FIND_LOCAL 0x04
44
45 /* NetBIOS flags */
46 #define NB_GROUP  0x80
47 #define NB_PERM   0x02
48 #define NB_ACTIVE 0x04
49 #define NB_CONFL  0x08
50 #define NB_DEREG  0x10
51 #define NB_BFLAG  0x00 /* broadcast node type */
52 #define NB_PFLAG  0x20 /* point-to-point node type */
53 #define NB_MFLAG  0x40 /* mixed bcast & p-p node type */
54 #define NB_HFLAG  0x60 /* microsoft 'hybrid' node type */
55 #define NB_FLGMSK 0x60
56
57 #define REFRESH_TIME (15*60)
58 #define NAME_POLL_REFRESH_TIME (5*60)
59 #define NAME_POLL_INTERVAL 15
60
61 /* NetBIOS flag identifier */
62 #define NAME_PERMANENT(p) ((p) & NB_PERM)
63 #define NAME_ACTIVE(p)    ((p) & NB_ACTIVE)
64 #define NAME_CONFLICT(p)  ((p) & NB_CONFL)
65 #define NAME_DEREG(p)     ((p) & NB_DEREG)
66 #define NAME_GROUP(p)     ((p) & NB_GROUP)
67
68 #define NAME_BFLAG(p)     (((p) & NB_FLGMSK) == NB_BFLAG)
69 #define NAME_PFLAG(p)     (((p) & NB_FLGMSK) == NB_PFLAG)
70 #define NAME_MFLAG(p)     (((p) & NB_FLGMSK) == NB_MFLAG)
71 #define NAME_HFLAG(p)     (((p) & NB_FLGMSK) == NB_HFLAG)
72
73 /* server type identifiers */
74 #define AM_MASTER(work) (work->ServerType & SV_TYPE_MASTER_BROWSER)
75 #define AM_BACKUP(work) (work->ServerType & SV_TYPE_BACKUP_BROWSER)
76 #define AM_DOMMST(work) (work->ServerType & SV_TYPE_DOMAIN_MASTER)
77 #define AM_DOMMEM(work) (work->ServerType & SV_TYPE_DOMAIN_MEMBER)
78 #define AM_ANY_MASTER(work) (check_work_servertype(work->work_group, \
79 SV_TYPE_MASTER_BROWSER|SV_TYPE_DOMAIN_MASTER))
80
81 /* microsoft browser NetBIOS name */
82 #define MSBROWSE "\001\002__MSBROWSE__\002"
83
84 /* mail slots */
85 #define BROWSE_MAILSLOT    "\\MAILSLOT\\BROWSE"
86 #define NET_LOGON_MAILSLOT "\\MAILSLOT\\NET\\NETLOGON"
87
88 enum name_source {STATUS_QUERY, LMHOSTS, REGISTER, SELF, DNS, DNSFAIL};
89 enum node_type {B_NODE=0, P_NODE=1, M_NODE=2, NBDD_NODE=3};
90 enum packet_type {NMB_PACKET, DGRAM_PACKET};
91
92 enum master_state
93 {
94    MST_POTENTIAL,
95    MST_BACK,
96    MST_MSB,
97    MST_BROWSER
98 };
99
100 enum domain_state
101 {
102    DOMAIN_NONE,
103    DOMAIN_WAIT,
104    DOMAIN_MST
105 };
106
107 enum logon_state
108 {
109    LOGON_NONE,
110    LOGON_WAIT,
111    LOGON_SRV
112 };
113
114 enum state_type
115 {
116         NAME_STATUS_DOM_SRV_CHK,
117         NAME_STATUS_SRV_CHK,
118         NAME_REGISTER_CHALLENGE,
119         NAME_REGISTER,
120         NAME_RELEASE,
121         NAME_QUERY_CONFIRM,
122         NAME_QUERY_ANNOUNCE_HOST,
123         NAME_QUERY_SYNC_LOCAL,
124         NAME_QUERY_SYNC_REMOTE,
125         NAME_QUERY_DOM_SRV_CHK,
126         NAME_QUERY_SRV_CHK,
127         NAME_QUERY_FIND_MST,
128         NAME_QUERY_MST_CHK,
129         NAME_QUERY_DOMAIN
130 };
131
132 /* a netbios name structure */
133 struct nmb_name {
134   char name[17];
135   char scope[64];
136   int name_type;
137 };
138
139 /* a netbios flags + ip address structure */
140 /* this is used for multi-homed systems and for internet group names */
141 struct nmb_ip
142 {
143   struct in_addr ip; /* ip address of host that owns this name */
144   uint16 nb_flags;      /* netbios flags */
145 };
146
147 /* this is the structure used for the local netbios name list */
148 struct name_record
149 {
150   struct name_record *next;
151   struct name_record *prev;
152
153   struct nmb_name name;    /* the netbios name */
154   struct nmb_ip *ip_flgs;  /* the ip + flags */
155   int num_ips;             /* number of ip+flags entries */
156
157   enum name_source source; /* where the name came from */
158
159   time_t death_time; /* time record must be removed (do not remove if 0) */
160   time_t refresh_time; /* time record should be refreshed */
161 };
162
163 /* browse and backup server cache for synchronising browse list */
164 struct browse_cache_record
165 {
166         struct browse_cache_record *next;
167         struct browse_cache_record *prev;
168
169         pstring name;
170         int type;
171         pstring group;
172         struct in_addr ip;
173         time_t sync_time;
174         BOOL synced;
175         BOOL local;
176 };
177
178 /* this is used to hold the list of servers in my domain, and is */
179 /* contained within lists of domains */
180 struct server_record
181 {
182   struct server_record *next;
183   struct server_record *prev;
184
185   struct server_info_struct serv;
186   time_t death_time;  
187 };
188
189 /* a workgroup structure. it contains a list of servers */
190 struct work_record
191 {
192   struct work_record *next;
193   struct work_record *prev;
194
195   struct server_record *serverlist;
196
197   /* stage of development from non-local-master up to local-master browser */
198   enum master_state mst_state;
199
200   /* stage of development from non-domain-master to domain master browser */
201   enum domain_state dom_state;
202
203   /* stage of development from non-logon-server to logon server */
204   enum logon_state log_state;
205
206   /* work group info */
207   fstring work_group;
208   int     token;        /* used when communicating with backup browsers */
209   int     ServerType;
210
211   /* announce info */
212   time_t lastannounce_time;
213   int announce_interval;
214   BOOL    needannounce;
215
216
217   /* election info */
218   BOOL    RunningElection;
219   BOOL    needelection;
220   int     ElectionCount;
221   uint32  ElectionCriterion;
222 };
223
224 /* initiated name queries recorded in this list to track any responses... */
225 /* sadly, we need to group everything together. i suppose that if this
226    gets unwieldy, then a union ought to be considered. oh for c++... */
227 struct response_record
228 {
229   struct response_record *next;
230   struct response_record *prev;
231
232   uint16 response_id;
233   enum state_type state;
234
235   int fd;
236   int quest_type;
237   struct nmb_name name;
238   int nb_flags;
239   time_t ttl;
240
241   int server_type;
242   fstring my_name;
243   fstring my_comment;
244
245   BOOL bcast;
246   BOOL recurse;
247   struct in_addr send_ip;
248   struct in_addr reply_to_ip;
249
250   int num_msgs;
251
252   time_t repeat_time;
253   time_t repeat_interval;
254   int    repeat_count;
255 };
256
257 /* a subnet structure. it contains a list of workgroups and netbios names*/
258
259 /* note that a subnet of 255.255.255.255 contains all the WINS netbios names.
260    all communication from such nodes are on a non-broadcast basis: they
261    are point-to-point (P nodes) or mixed point-to-point and broadcast
262    (M nodes). M nodes use point-to-point as a preference, and will use
263    broadcasting for certain activities, or will resort to broadcasting as a
264    last resort, if the WINS server fails (users of wfwg will notice that their
265    machine often freezes for 30 seconds at a time intermittently, if the WINS
266    server is down).
267
268    B nodes will have their own, totally separate subnet record, with their
269    own netbios name set. these do NOT interact with other subnet records'
270    netbios names, INCLUDING the WINS one (with an ip "address", so called,
271    of 255.255.255.255)
272
273    there is a separate response list for each subnet record. in the case of
274    the 255.255.255.255 subnet record (WINS), the WINS server will be able to
275    use this to poll (infrequently!) each of its entries, to ensure that the
276    names are still in use.
277    XXXX this polling is a planned feature for a really over-cautious WINS server 
278 */
279
280 struct subnet_record
281 {
282   struct subnet_record *next;
283   struct subnet_record *prev;
284
285   struct work_record *workgrouplist; /* list of workgroups */
286   struct name_record *namelist;      /* list of netbios names */
287   struct response_record *responselist; /* list of responses expected */
288
289   struct in_addr bcast_ip;
290   struct in_addr mask_ip;
291   struct in_addr myip;
292 };
293
294 /* a resource record */
295 struct res_rec {
296   struct nmb_name rr_name;
297   int rr_type;
298   int rr_class;
299   int ttl;
300   int rdlength;
301   char rdata[MAX_DGRAM_SIZE];
302 };
303
304 /* define a nmb packet. */
305 struct nmb_packet
306 {
307   struct {
308     int name_trn_id;
309     int opcode;
310     BOOL response;
311     struct {
312       BOOL bcast;
313       BOOL recursion_available;
314       BOOL recursion_desired;
315       BOOL trunc;
316       BOOL authoritative;
317     } nm_flags;
318     int rcode;
319     int qdcount;
320     int ancount;
321     int nscount;
322     int arcount;
323   } header;
324
325   struct {
326     struct nmb_name question_name;
327     int question_type;
328     int question_class;
329   } question;
330
331   struct res_rec *answers;
332   struct res_rec *nsrecs;
333   struct res_rec *additional;
334 };
335
336
337 /* a datagram - this normally contains SMB data in the data[] array */
338 struct dgram_packet {
339   struct {
340     int msg_type;
341     struct {
342       enum node_type node_type;
343       BOOL first;
344       BOOL more;
345     } flags;
346     int dgm_id;
347     struct in_addr source_ip;
348     int source_port;
349     int dgm_length;
350     int packet_offset;
351   } header;
352   struct nmb_name source_name;
353   struct nmb_name dest_name;
354   int datasize;
355   char data[MAX_DGRAM_SIZE];
356 };
357
358 /* define a structure used to queue packets. this will be a linked
359  list of nmb packets */
360 struct packet_struct
361 {
362   struct packet_struct *next;
363   struct packet_struct *prev;
364   struct in_addr ip;
365   int port;
366   int fd;
367   time_t timestamp;
368   enum packet_type packet_type;
369   union {
370     struct nmb_packet nmb;
371     struct dgram_packet dgram;
372   } packet;
373 };
374
375
376 /* ids for netbios packet types */
377 #define ANN_HostAnnouncement         1
378 #define ANN_AnnouncementRequest      2
379 #define ANN_Election                 8
380 #define ANN_GetBackupListReq         9
381 #define ANN_GetBackupListResp       10
382 #define ANN_BecomeBackup            11
383 #define ANN_DomainAnnouncement      12
384 #define ANN_MasterAnnouncement      13
385 #define ANN_ResetBrowserState       14
386 #define ANN_LocalMasterAnnouncement 15
387
388
389 /* broadcast packet announcement intervals, in minutes */
390
391 /* attempt to add domain logon and domain master names */
392 #define CHECK_TIME_ADD_DOM_NAMES 5 
393
394 /* search for master browsers of workgroups samba knows about, 
395    except default */
396 #define CHECK_TIME_MST_BROWSE       5 
397
398 /* request backup browser announcements from other servers */
399 #define CHECK_TIME_ANNOUNCE_BACKUP 15
400
401 /* request host announcements from other servers: min and max of interval */
402 #define CHECK_TIME_MIN_HOST_ANNCE   3
403 #define CHECK_TIME_MAX_HOST_ANNCE  12
404
405 /* announce as master to WINS server and any Primary Domain Controllers */
406 #define CHECK_TIME_MST_ANNOUNCE    15
407
408 /* do all remote announcements this often */
409 #define REMOTE_ANNOUNCE_INTERVAL 180
410
411 #define DFLT_SERVER_TYPE (SV_TYPE_WORKSTATION | SV_TYPE_SERVER | \
412                           SV_TYPE_TIME_SOURCE | SV_TYPE_SERVER_UNIX | \
413                           SV_TYPE_PRINTQ_SERVER | SV_TYPE_SERVER_NT | \
414                           SV_TYPE_NT )
415