This is it ! The mega-merge of the JRA_NMBD_REWRITE branch
[samba.git] / source / include / nameserv.h
1 #ifndef _NAMESERV_H_
2 #define _NAMESERV_H_
3 /* 
4    Unix SMB/Netbios implementation.
5    Version 1.9.
6    NBT netbios header - version 2
7    Copyright (C) Andrew Tridgell 1994-1997
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 #define PERMANENT_TTL 0
26
27 /* NTAS uses 2, NT uses 1, WfWg uses 0 */
28 #define MAINTAIN_LIST    2
29 #define ELECTION_VERSION 1
30
31 #define MAX_DGRAM_SIZE (576) /* tcp/ip datagram limit is 576 bytes */
32 #define MIN_DGRAM_SIZE 12
33
34 /*********************************************************
35  Types of reply packet.
36 **********************************************************/
37
38 enum netbios_reply_type_code { NMB_QUERY, NMB_STATUS, NMB_REG, NMB_REG_REFRESH,
39                                NMB_REL, NMB_WAIT_ACK, NMB_MULTIHOMED_REG,
40                                WINS_REG, WINS_QUERY };
41
42 /* From rfc1002, 4.2.1.2 */
43 /* Question types. */
44 #define QUESTION_TYPE_NB_QUERY  0x20
45 #define QUESTION_TYPE_NB_STATUS 0x21
46
47 /* Question class */
48 #define QUESTION_CLASS_IN  0x1
49
50 /* Opcode definitions */
51 #define NMB_NAME_QUERY_OPCODE       0x0
52 #define NMB_NAME_REG_OPCODE         0x05 /* see rfc1002.txt 4.2.2,3,5,6,7,8 */
53 #define NMB_NAME_RELEASE_OPCODE     0x06 /* see rfc1002.txt 4.2.9,10,11 */
54 #define NMB_WACK_OPCODE             0x07 /* see rfc1002.txt 4.2.16 */
55 /* Ambiguity in rfc1002 about which of these is correct. */
56 /* WinNT uses 8 by default but can be made to use 9. */
57 #define NMB_NAME_REFRESH_OPCODE_8   0x08 /* see rfc1002.txt 4.2.4 */
58 #define NMB_NAME_REFRESH_OPCODE_9   0x09 /* see rfc1002.txt 4.2.4 */
59 #define NMB_NAME_MULTIHOMED_REG_OPCODE 0x0F /* Invented by Microsoft. */
60
61 /* XXXX what about all the other types?? 0x1, 0x2, 0x3, 0x4, 0x8? */
62
63 /* Resource record types. rfc1002 4.2.1.3 */
64 #define RR_TYPE_A                  0x1
65 #define RR_TYPE_NS                 0x2
66 #define RR_TYPE_NULL               0xA
67 #define RR_TYPE_NB                0x20
68 #define RR_TYPE_NBSTAT            0x21
69
70 /* Resource record class. */
71 #define RR_CLASS_IN                0x1
72
73 /* NetBIOS flags */
74 #define NB_GROUP  0x80
75 #define NB_PERM   0x02
76 #define NB_ACTIVE 0x04
77 #define NB_CONFL  0x08
78 #define NB_DEREG  0x10
79 #define NB_BFLAG  0x00 /* Broadcast node type. */
80 #define NB_PFLAG  0x20 /* Point-to-point node type. */
81 #define NB_MFLAG  0x40 /* Mixed bcast & p-p node type. */
82 #define NB_HFLAG  0x60 /* Microsoft 'hybrid' node type. */
83 #define NB_NODETYPEMASK 0x60
84 /* Mask applied to outgoing NetBIOS flags. */
85 #define NB_FLGMSK 0xE0
86
87 /* NetBIOS flag identifier. */
88 #define NAME_GROUP(p)     ((p)->nb_flags & NB_GROUP)
89 #define NAME_BFLAG(p)     (((p)->nb_flags & NB_NODETYPEMASK) == NB_BFLAG)
90 #define NAME_PFLAG(p)     (((p)->nb_flags & NB_NODETYPEMASK) == NB_PFLAG)
91 #define NAME_MFLAG(p)     (((p)->nb_flags & NB_NODETYPEMASK) == NB_MFLAG)
92 #define NAME_HFLAG(p)     (((p)->nb_flags & NB_NODETYPEMASK) == NB_HFLAG)
93
94 /* Samba name state for a name in a namelist. */
95 #define NAME_IS_ACTIVE(p) ((p)->nb_flags & NB_ACTIVE)
96 #define NAME_IN_CONFLICT(p)  ((p)->nb_flags & NB_CONFL)
97 #define NAME_IS_DEREGISTERING(p)     ((p)->nb_flags & NB_DEREG)
98
99 /* Error codes for NetBIOS requests. */
100 #define FMT_ERR   0x1       /* Packet format error. */
101 #define SRV_ERR   0x2       /* Internal server error. */
102 #define NAM_ERR   0x3       /* Name does not exist. */
103 #define IMP_ERR   0x4       /* Request not implemented. */
104 #define RFS_ERR   0x5       /* Request refused. */
105 #define ACT_ERR   0x6       /* Active error - name owned by another host. */
106 #define CFT_ERR   0x7       /* Name in conflict error. */
107
108 #define REFRESH_TIME (15*60)
109 #define NAME_POLL_REFRESH_TIME (5*60)
110 #define NAME_POLL_INTERVAL 15
111
112 /* Workgroup state identifiers. */
113 #define AM_POTENTIAL_MASTER_BROWSER(work) ((work)->mst_state == MST_POTENTIAL)
114 #define AM_LOCAL_MASTER_BROWSER(work) ((work)->mst_state == MST_BROWSER)
115 #define AM_DOMAIN_MASTER_BROWSER(work) ((work)->dom_state == DOMAIN_MST)
116 #define AM_DOMAIN_MEMBER(work) ((work)->log_state == LOGON_SRV)
117
118 /* Microsoft browser NetBIOS name. */
119 #define MSBROWSE "\001\002__MSBROWSE__\002"
120
121 /* Mail slots. */
122 #define BROWSE_MAILSLOT    "\\MAILSLOT\\BROWSE"
123 #define NET_LOGON_MAILSLOT "\\MAILSLOT\\NET\\NETLOGON"
124 #define NT_LOGON_MAILSLOT  "\\MAILSLOT\\NET\\NTLOGON"
125
126 /* Samba definitions for find_name_on_subnet(). */
127 #define FIND_ANY_NAME   0
128 #define FIND_SELF_NAME  1
129
130 /*
131  * The different name types that can be in namelists.
132  *
133  * SELF_NAME should only be on the broadcast and unicast subnets.
134  * LMHOSTS_NAME should only be in the remote_broadcast_subnet.
135  * REGISTER_NAME, DNS_NAME, DNSFAIL_NAME should only be in the wins_server_subnet.
136  * WINS_PROXY_NAME should only be on the broadcast subnets.
137  * PERMANENT_NAME can be on all subnets except remote_broadcast_subnet.
138  *
139  */
140
141 enum name_source {LMHOSTS_NAME, REGISTER_NAME, SELF_NAME, DNS_NAME, 
142                   DNSFAIL_NAME, PERMANENT_NAME, WINS_PROXY_NAME};
143 enum node_type {B_NODE=0, P_NODE=1, M_NODE=2, NBDD_NODE=3};
144 enum packet_type {NMB_PACKET, DGRAM_PACKET};
145
146 enum master_state
147 {
148   MST_NONE,
149   MST_POTENTIAL,
150   MST_BACKUP,
151   MST_MSB,
152   MST_BROWSER,
153   MST_UNBECOMING_MASTER
154 };
155
156 enum domain_state
157 {
158   DOMAIN_NONE,
159   DOMAIN_WAIT,
160   DOMAIN_MST
161 };
162
163 enum logon_state
164 {
165   LOGON_NONE,
166   LOGON_WAIT,
167   LOGON_SRV
168 };
169
170 /* A netbios name structure. */
171 struct nmb_name {
172   char name[17];
173   char scope[64];
174   unsigned int name_type;
175 };
176
177 /* This is the structure used for the local netbios name list. */
178 struct name_record
179 {
180   struct name_record *next;
181   struct name_record *prev;
182
183   struct subnet_record *subnet;
184
185   struct nmb_name name;    /* The netbios name. */
186   uint16 nb_flags;         /* Netbios flags. */
187   int num_ips;             /* Number of ip entries. */
188   struct in_addr *ip;      /* The ip list for this name. */
189
190   enum name_source source; /* Where the name came from. */
191
192   time_t death_time; /* The time the record must be removed (do not remove if 0). */
193   time_t refresh_time; /* The time the record should be refreshed. */
194 };
195
196 struct subnet_record;
197
198 /* Browser cache for synchronising browse lists. */
199 struct browse_cache_record
200 {
201   struct browse_cache_record *next;
202   struct browse_cache_record *prev;
203
204   pstring lmb_name;
205   pstring work_group;
206   struct in_addr ip;
207   time_t sync_time;
208   time_t death_time; /* The time the record must be removed. */
209 };
210
211 /* This is used to hold the list of servers in my domain, and is
212    contained within lists of domains. */
213
214 struct server_record
215 {
216   struct server_record *next;
217   struct server_record *prev;
218
219   struct subnet_record *subnet;
220
221   struct server_info_struct serv;
222   time_t death_time;  
223 };
224
225 /* A workgroup structure. It contains a list of servers. */
226 struct work_record
227 {
228   struct work_record *next;
229   struct work_record *prev;
230
231   struct subnet_record *subnet;
232
233   struct server_record *serverlist;
234
235   /* Stage of development from non-local-master up to local-master browser. */
236   enum master_state mst_state;
237
238   /* Stage of development from non-domain-master to domain-master browser. */
239   enum domain_state dom_state;
240
241   /* Stage of development from non-logon-server to logon server. */
242   enum logon_state log_state;
243
244   /* Work group info. */
245   fstring work_group;
246   int     token;        /* Used when communicating with backup browsers. */
247   fstring local_master_browser_name;      /* Current local master browser. */
248
249   /* Announce info. */
250   time_t lastannounce_time;
251   int announce_interval;
252   BOOL    needannounce;
253
254   /* Timeout time for this workgroup. 0 means permanent. */
255   time_t death_time;  
256
257   /* Election info */
258   BOOL    RunningElection;
259   BOOL    needelection;
260   int     ElectionCount;
261   uint32  ElectionCriterion;
262
263   /* Domain master browser info. Used for efficient syncs. */
264   struct nmb_name dmb_name;
265   struct in_addr dmb_addr;
266 };
267
268 /* typedefs needed to define copy & free functions for userdata. */
269 struct userdata_struct;
270
271 typedef struct userdata_struct * (*userdata_copy_fn)(struct userdata_struct *);
272 typedef void (*userdata_free_fn)(struct userdata_struct *);
273
274 /* Structure to define any userdata passed around. */
275
276 struct userdata_struct {
277   userdata_copy_fn copy_fn;
278   userdata_free_fn free_fn;
279   unsigned int userdata_len;
280   char data[1];
281 };
282
283 struct response_record;
284 struct packet_struct;
285 struct res_rec;
286
287 /* typedef to define the function called when this response packet comes in. */
288 typedef void (*response_function)(struct subnet_record *, struct response_record *,
289                                   struct packet_struct *);
290
291 /* typedef to define the function called when this response record times out. */
292 typedef void (*timeout_response_function)(struct subnet_record *,
293                                           struct response_record *);
294
295 /* typedef to define the function called when the request that caused this
296    response record to be created is successful. */
297 typedef void (*success_function)(struct subnet_record *, struct userdata_struct *, ...);
298
299 /* typedef to define the function called when the request that caused this
300    response record to be created is unsuccessful. */
301 typedef void (*fail_function)(struct subnet_record *, struct response_record *, ...);
302
303 /* List of typedefs for success and fail functions of the different query
304    types. Used to catch any compile time prototype errors. */
305
306 typedef void (*register_name_success_function)( struct subnet_record *,
307                                                 struct userdata_struct *,
308                                                 struct nmb_name *,
309                                                 uint16,
310                                                 int,
311                                                 struct in_addr);
312 typedef void (*register_name_fail_function)( struct subnet_record *,
313                                              struct response_record *,
314                                              struct nmb_name *);
315
316 typedef void (*release_name_success_function)( struct subnet_record *,
317                                                struct userdata_struct *, 
318                                                struct nmb_name *,
319                                                struct in_addr);
320 typedef void (*release_name_fail_function)( struct subnet_record *,
321                                             struct response_record *, 
322                                             struct nmb_name *);
323
324 typedef void (*refresh_name_success_function)( struct subnet_record *,
325                                                struct userdata_struct *, 
326                                                struct nmb_name *,
327                                                uint16,
328                                                int,
329                                                struct in_addr);
330 typedef void (*refresh_name_fail_function)( struct subnet_record *,
331                                             struct response_record *,
332                                             struct nmb_name *);
333
334 typedef void (*query_name_success_function)( struct subnet_record *,
335                                              struct userdata_struct *,
336                                              struct nmb_name *,
337                                              struct in_addr,
338                                              struct res_rec *answers);
339
340 typedef void (*query_name_fail_function)( struct subnet_record *,
341                                           struct response_record *,    
342                                           struct nmb_name *,
343                                           int);  
344
345 typedef void (*node_status_success_function)( struct subnet_record *,
346                                               struct userdata_struct *,
347                                               struct res_rec *,
348                                               struct in_addr);
349 typedef void (*node_status_fail_function)( struct subnet_record *,
350                                            struct response_record *);
351
352 /* Initiated name queries are recorded in this list to track any responses. */
353
354 struct response_record
355 {
356   struct response_record *next;
357   struct response_record *prev;
358
359   uint16 response_id;
360
361   /* Callbacks for packets received or not. */ 
362   response_function resp_fn;
363   timeout_response_function timeout_fn;
364
365   /* Callbacks for the request succeeding or not. */
366   success_function success_fn;
367   fail_function fail_fn;
368  
369   struct packet_struct *packet;
370
371   struct userdata_struct *userdata;
372
373   int num_msgs;
374
375   time_t repeat_time;
376   time_t repeat_interval;
377   int    repeat_count;
378 };
379
380 /* A subnet structure. It contains a list of workgroups and netbios names. */
381
382 /*
383    B nodes will have their own, totally separate subnet record, with their
384    own netbios name set. These do NOT interact with other subnet records'
385    netbios names.
386 */
387
388 enum subnet_type {
389   NORMAL_SUBNET              = 0,  /* Subnet listed in interfaces list. */
390   UNICAST_SUBNET             = 1,  /* Subnet for unicast packets. */
391   REMOTE_BROADCAST_SUBNET    = 2,  /* Subnet for remote broadcasts. */
392   WINS_SERVER_SUBNET         = 3   /* Only created if we are a WINS server. */
393 };
394
395 struct subnet_record
396 {
397   struct subnet_record *next;
398   struct subnet_record *prev;
399
400   char  *subnet_name;      /* For Debug identification. */
401   enum subnet_type type;   /* To catagorize the subnet. */
402
403   struct work_record *workgrouplist; /* List of workgroups. */
404   struct name_record *namelist;      /* List of netbios names. */
405   struct response_record *responselist; /* List of responses expected. */
406
407   BOOL namelist_changed;
408   BOOL work_changed;
409
410   struct in_addr bcast_ip;
411   struct in_addr mask_ip;
412   struct in_addr myip;
413   int nmb_sock;               /* socket to listen for unicast 137. */
414   int dgram_sock;             /* socket to listen for unicast 138. */
415 };
416
417 /* A resource record. */
418 struct res_rec {
419   struct nmb_name rr_name;
420   int rr_type;
421   int rr_class;
422   int ttl;
423   int rdlength;
424   char rdata[MAX_DGRAM_SIZE];
425 };
426
427 /* An nmb packet. */
428 struct nmb_packet
429 {
430   struct {
431     int name_trn_id;
432     int opcode;
433     BOOL response;
434     struct {
435       BOOL bcast;
436       BOOL recursion_available;
437       BOOL recursion_desired;
438       BOOL trunc;
439       BOOL authoritative;
440     } nm_flags;
441     int rcode;
442     int qdcount;
443     int ancount;
444     int nscount;
445     int arcount;
446   } header;
447
448   struct {
449     struct nmb_name question_name;
450     int question_type;
451     int question_class;
452   } question;
453
454   struct res_rec *answers;
455   struct res_rec *nsrecs;
456   struct res_rec *additional;
457 };
458
459
460 /* A datagram - this normally contains SMB data in the data[] array. */
461
462 struct dgram_packet {
463   struct {
464     int msg_type;
465     struct {
466       enum node_type node_type;
467       BOOL first;
468       BOOL more;
469     } flags;
470     int dgm_id;
471     struct in_addr source_ip;
472     int source_port;
473     int dgm_length;
474     int packet_offset;
475   } header;
476   struct nmb_name source_name;
477   struct nmb_name dest_name;
478   int datasize;
479   char data[MAX_DGRAM_SIZE];
480 };
481
482 /* Define a structure used to queue packets. This will be a linked
483  list of nmb packets. */
484
485 struct packet_struct
486 {
487   struct packet_struct *next;
488   struct packet_struct *prev;
489   BOOL locked;
490   struct in_addr ip;
491   int port;
492   int fd;
493   time_t timestamp;
494   enum packet_type packet_type;
495   union {
496     struct nmb_packet nmb;
497     struct dgram_packet dgram;
498   } packet;
499 };
500
501 /* NETLOGON opcodes */
502
503 #define QUERYFORPDC      7 /* Query for PDC. */
504 #define QUERYFORPDC_R   12 /* Response to Query for PDC. */
505 #define SAMLOGON        18
506 #define SAMLOGON_R      19
507
508
509 /* Ids for netbios packet types. */
510
511 #define ANN_HostAnnouncement         1
512 #define ANN_AnnouncementRequest      2
513 #define ANN_Election                 8
514 #define ANN_GetBackupListReq         9
515 #define ANN_GetBackupListResp       10
516 #define ANN_BecomeBackup            11
517 #define ANN_DomainAnnouncement      12
518 #define ANN_MasterAnnouncement      13
519 #define ANN_ResetBrowserState       14
520 #define ANN_LocalMasterAnnouncement 15
521
522
523 /* Broadcast packet announcement intervals, in minutes. */
524
525 /* Attempt to add domain logon and domain master names. */
526 #define CHECK_TIME_ADD_DOM_NAMES 5 
527
528 /* Search for master browsers of workgroups samba knows about, 
529    except default. */
530 #define CHECK_TIME_MST_BROWSE       5 
531
532 /* Request backup browser announcements from other servers. */
533 #define CHECK_TIME_ANNOUNCE_BACKUP 15
534
535 /* Request host announcements from other servers: min and max of interval. */
536 #define CHECK_TIME_MIN_HOST_ANNCE   3
537 #define CHECK_TIME_MAX_HOST_ANNCE  12
538
539 /* Announce as master to WINS server and any Primary Domain Controllers. */
540 #define CHECK_TIME_MST_ANNOUNCE    15
541
542 /* Time between syncs from domain master browser to local master browsers. */
543 #define CHECK_TIME_DMB_TO_LMB_SYNC    15
544
545 /* Do all remote announcements this often. */
546 #define REMOTE_ANNOUNCE_INTERVAL 180
547
548 /* Types of machine we can announce as. */
549 #define ANNOUNCE_AS_NT 1
550 #define ANNOUNCE_AS_WIN95 2
551 #define ANNOUNCE_AS_WFW 3
552
553 /* Macro's to enumerate subnets either with or without
554    the UNICAST subnet. */
555
556 extern struct subnet_record *subnetlist;
557 extern struct subnet_record *unicast_subnet;
558 extern struct subnet_record *wins_server_subnet;
559 extern struct subnet_record *remote_broadcast_subnet;
560
561 #define FIRST_SUBNET subnetlist
562 #define NEXT_SUBNET_EXCLUDING_UNICAST(x) ((x)->next)
563 #define NEXT_SUBNET_INCLUDING_UNICAST(x) (get_next_subnet_maybe_unicast((x)))
564
565 /* To be removed. */
566 enum state_type { TEST };
567 #endif /* _NAMESERV_H_ */