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