as a result of the writing of namework.doc, namework.c has been tidied up,
[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 /* NTAS uses 2, NT uses 1, WfWg uses 0 */
24 #define MAINTAIN_LIST    2
25 #define ELECTION_VERSION 1
26
27 #define MAX_DGRAM_SIZE (576) /* tcp/ip datagram limit is 576 bytes */
28 #define MIN_DGRAM_SIZE 12
29
30 #define NMB_QUERY  0x20
31 #define NMB_STATUS 0x21
32
33 #define NMB_REG         0x05 /* see rfc1002.txt 4.2.2,3,5,6,7,8 */
34 #define NMB_REG_REFRESH 0x09 /* see rfc1002.txt 4.2.4 */
35 #define NMB_REL         0x06 /* see rfc1002.txt 4.2.9,10,11 */
36 #define NMB_WAIT_ACK    0x07 /* see rfc1002.txt 4.2.17 */
37 /* XXXX what about all the other types?? 0x1, 0x2, 0x3, 0x4, 0x8? */
38
39 /* NetBIOS flags */
40 #define NB_GROUP  0x80
41 #define NB_PERM   0x02
42 #define NB_ACTIVE 0x04
43 #define NB_CONFL  0x08
44 #define NB_DEREG  0x10
45 #define NB_BFLAG  0x00
46 #define NB_PFLAG  0x20
47 #define NB_MFLAG  0x40
48 #define NB__FLAG  0x60
49 #define NB_FLGMSK 0x60
50
51 #define REFRESH_TIME (15*60)
52 #define NAME_POLL_REFRESH_TIME (5*60)
53 #define NAME_POLL_INTERVAL 15
54
55 /* NetBIOS flag identifier */
56 #define NAME_PERMANENT(p) ((p) & NB_PERM)
57 #define NAME_ACTIVE(p)    ((p) & NB_ACTIVE)
58 #define NAME_CONFLICT(p)  ((p) & NB_CONFL)
59 #define NAME_DEREG(p)     ((p) & NB_DEREG)
60 #define NAME_GROUP(p)     ((p) & NB_GROUP)
61
62 #define NAME_BFLAG(p)     (((p) & NB_FLGMSK) == NB_BFLAG)
63 #define NAME_PFLAG(p)     (((p) & NB_FLGMSK) == NB_PFLAG)
64 #define NAME_MFLAG(p)     (((p) & NB_FLGMSK) == NB_MFLAG)
65 #define NAME__FLAG(p)     (((p) & NB_FLGMSK) == NB__FLAG)
66
67 /* server type identifiers */
68 #define AM_MASTER(work) (work->ServerType & SV_TYPE_MASTER_BROWSER)
69 #define AM_BACKUP(work) (work->ServerType & SV_TYPE_BACKUP_BROWSER)
70 #define AM_DOMCTL(work) (work->ServerType & SV_TYPE_DOMAIN_CTRL)
71
72 /* microsoft browser NetBIOS name */
73 #define MSBROWSE "\001\002__MSBROWSE__\002"
74
75 /* mail slots */
76 #define BROWSE_MAILSLOT    "\\MAILSLOT\\BROWSE"
77 #define NET_LOGON_MAILSLOT "\\MAILSLOT\\NET\\NETLOGON"
78
79 enum name_source {STATUS_QUERY, LMHOSTS, REGISTER, SELF, DNS, DNSFAIL};
80 enum node_type {B_NODE=0, P_NODE=1, M_NODE=2, NBDD_NODE=3};
81 enum packet_type {NMB_PACKET, DGRAM_PACKET};
82 enum state_type
83 {
84         NAME_STATUS_MASTER_CHECK,
85         NAME_STATUS_CHECK,
86         NAME_REGISTER,
87         NAME_RELEASE,
88         NAME_QUERY_CONFIRM,
89         NAME_QUERY_SYNC,
90         NAME_QUERY_MST_SRV_CHK,
91         NAME_QUERY_SRV_CHK,
92         NAME_QUERY_FIND_MST,
93         NAME_QUERY_MST_CHK
94 };
95
96 /* a netbios name structure */
97 struct nmb_name {
98   char name[17];
99   char scope[64];
100   int name_type;
101 };
102
103 /* this is the structure used for the local netbios name list */
104 struct name_record
105 {
106   struct name_record *next;
107   struct name_record *prev;
108
109   struct nmb_name name;    /* the netbios name */
110   struct in_addr ip;       /* ip address of host that owns this name */
111   int nb_flags;            /* netbios flags */
112
113   enum name_source source; /* where the name came from */
114
115   time_t death_time; /* time record must be removed (do not remove if 0) */
116   time_t refresh_time; /* time record should be refreshed */
117 };
118
119 /* browse and backup server cache for synchronising browse list */
120 struct browse_cache_record
121 {
122         struct browse_cache_record *next;
123         struct browse_cache_record *prev;
124
125         pstring name;
126         int type;
127         pstring group;
128         struct in_addr ip;
129         time_t sync_time;
130         BOOL synced;
131 };
132
133 /* this is used to hold the list of servers in my domain, and is */
134 /* contained within lists of domains */
135 struct server_record
136 {
137   struct server_record *next;
138   struct server_record *prev;
139
140   struct server_info_struct serv;
141   time_t death_time;  
142 };
143
144 /* a workgroup structure. it contains a list of servers */
145 struct work_record
146 {
147   struct work_record *next;
148   struct work_record *prev;
149
150   struct server_record *serverlist;
151
152   /* work group info */
153   fstring work_group;
154   int     token;        /* used when communicating with backup browsers */
155   int     ServerType;
156
157   /* announce info */
158   time_t lastannounce_time;
159   int announce_interval;
160   BOOL    needannounce;
161
162   /* election info */
163   BOOL    RunningElection;
164   BOOL    needelection;
165   int     ElectionCount;
166   uint32  ElectionCriterion;
167 };
168
169 /* initiated name queries recorded in this list to track any responses... */
170 struct response_record
171 {
172   struct response_record *next;
173   struct response_record *prev;
174
175   uint16 response_id;
176   enum state_type state;
177
178   int fd;
179   int quest_type;
180   struct nmb_name name;
181   int nb_flags;
182   time_t ttl;
183
184   BOOL bcast;
185   BOOL recurse;
186   struct in_addr to_ip;
187
188   int num_msgs;
189
190   time_t repeat_time;
191   time_t repeat_interval;
192   int    repeat_count;
193 };
194
195 /* a subnet structure. it contains a list of workgroups and netbios names*/
196
197 /* note that a subnet of 255.255.255.255 contains all the WINS netbios names.
198    all communication from such nodes are on a non-broadcast basis: they
199    are point-to-point (P nodes) or mixed point-to-point and broadcast
200    (M nodes). M nodes use point-to-point as a preference, and will use
201    broadcasting for certain activities, or will resort to broadcasting as a
202    last resort, if the WINS server fails (users of wfwg will notice that their
203    machine often freezes for 30 seconds at a time intermittently, if the WINS
204    server is down).
205
206    B nodes will have their own, totally separate subnet record, with their
207    own netbios name set. these do NOT interact with other subnet records'
208    netbios names, INCLUDING the WINS one (with an ip "address", so called,
209    of 255.255.255.255)
210
211    there is a separate response list for each subnet record. in the case of
212    the 255.255.255.255 subnet record (WINS), the WINS server will be able to
213    use this to poll (infrequently!) each of its entries, to ensure that the
214    names are still in use.
215    XXXX this polling is a planned feature for a really over-cautious WINS server 
216 */
217
218 struct subnet_record
219 {
220   struct subnet_record *next;
221   struct subnet_record *prev;
222
223   struct work_record *workgrouplist; /* list of workgroups */
224   struct name_record *namelist;      /* list of netbios names */
225   struct response_record *responselist; /* list of responses expected */
226
227   struct in_addr bcast_ip;
228   struct in_addr mask_ip;
229   struct in_addr myip;
230
231   BOOL my_interface;
232 };
233
234 /* a resource record */
235 struct res_rec {
236   struct nmb_name rr_name;
237   int rr_type;
238   int rr_class;
239   int ttl;
240   int rdlength;
241   char rdata[MAX_DGRAM_SIZE];
242 };
243
244 /* define a nmb packet. */
245 struct nmb_packet
246 {
247   struct {
248     int name_trn_id;
249     int opcode;
250     BOOL response;
251     struct {
252       BOOL bcast;
253       BOOL recursion_available;
254       BOOL recursion_desired;
255       BOOL trunc;
256       BOOL authoritative;
257     } nm_flags;
258     int rcode;
259     int qdcount;
260     int ancount;
261     int nscount;
262     int arcount;
263   } header;
264
265   struct {
266     struct nmb_name question_name;
267     int question_type;
268     int question_class;
269   } question;
270
271   struct res_rec *answers;
272   struct res_rec *nsrecs;
273   struct res_rec *additional;
274 };
275
276
277 /* a datagram - this normally contains SMB data in the data[] array */
278 struct dgram_packet {
279   struct {
280     int msg_type;
281     struct {
282       enum node_type node_type;
283       BOOL first;
284       BOOL more;
285     } flags;
286     int dgm_id;
287     struct in_addr source_ip;
288     int source_port;
289     int dgm_length;
290     int packet_offset;
291   } header;
292   struct nmb_name source_name;
293   struct nmb_name dest_name;
294   int datasize;
295   char data[MAX_DGRAM_SIZE];
296 };
297
298 /* define a structure used to queue packets. this will be a linked
299  list of nmb packets */
300 struct packet_struct
301 {
302   struct packet_struct *next;
303   struct packet_struct *prev;
304   struct in_addr ip;
305   int port;
306   int fd;
307   time_t timestamp;
308   enum packet_type packet_type;
309   union {
310     struct nmb_packet nmb;
311     struct dgram_packet dgram;
312   } packet;
313 };
314
315
316 /* ids for netbios packet types */
317 #define ANN_HostAnnouncement         1
318 #define ANN_AnnouncementRequest      2
319 #define ANN_Election                 8
320 #define ANN_GetBackupListReq         9
321 #define ANN_GetBackupListResp       10
322 #define ANN_BecomeBackup            11
323 #define ANN_DomainAnnouncement      12
324 #define ANN_MasterAnnouncement      13
325 #define ANN_ResetBrowserState       14
326 #define ANN_LocalMasterAnnouncement 15
327
328
329 /* broadcast packet announcement intervals, in minutes */
330
331 /* search for master browsers of workgroups samba knows about, 
332    except default */
333 #define CHECK_TIME_MST_BROWSE       5 
334
335 /* request backup browser announcements from other servers */
336 #define CHECK_TIME_ANNOUNCE_BACKUP 15
337
338 /* request host announcements from other servers: min and max of interval */
339 #define CHECK_TIME_MIN_HOST_ANNCE   3
340 #define CHECK_TIME_MAX_HOST_ANNCE  12
341
342 /* announce as master to WINS server and any Primary Domain Controllers */
343 #define CHECK_TIME_MST_ANNOUNCE    15
344