changes from Luke
[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 (80*18+64)
28 #define MIN_DGRAM_SIZE 12
29
30 #define NMB_QUERY  0x20
31 #define NMB_STATUS 0x21
32 #define NMB_REG    0x05
33 #define NMB_REL    0x06
34
35 #define NB_GROUP  0x80
36 #define NB_PERM   0x02
37 #define NB_ACTIVE 0x04
38 #define NB_CONFL  0x08
39 #define NB_DEREG  0x10
40 #define NB_BFLAG  0x00
41 #define NB_PFLAG  0x20
42 #define NB_MFLAG  0x40
43 #define NB__FLAG  0x60
44 #define NB_FLGMSK 0x60
45
46 #define REFRESH_TIME (15*60)
47
48 #define NAME_PERMANENT(p) ((p) & NB_PERM)
49 #define NAME_ACTIVE(p)    ((p) & NB_ACTIVE)
50 #define NAME_CONFLICT(p)  ((p) & NB_CONFL)
51 #define NAME_DEREG(p)     ((p) & NB_DEREG)
52 #define NAME_GROUP(p)     ((p) & NB_GROUP)
53
54 #define NAME_BFLAG(p)     (((p) & NB_FLGMSK) == NB_BFLAG)
55 #define NAME_PFLAG(p)     (((p) & NB_FLGMSK) == NB_PFLAG)
56 #define NAME_MFLAG(p)     (((p) & NB_FLGMSK) == NB_MFLAG)
57 #define NAME__FLAG(p)     (((p) & NB_FLGMSK) == NB__FLAG)
58
59 #define MSBROWSE "\001\002__MSBROWSE__\002"
60
61 enum name_search { FIND_SELF, FIND_GLOBAL };
62 enum name_source {STATUS_QUERY, LMHOSTS, REGISTER, SELF, DNS, DNSFAIL};
63 enum node_type {B_NODE=0, P_NODE=1, M_NODE=2, NBDD_NODE=3};
64 enum packet_type {NMB_PACKET, DGRAM_PACKET};
65 enum cmd_type
66 {
67         NAME_STATUS_MASTER_CHECK,
68         NAME_STATUS_CHECK,
69         MASTER_SERVER_CHECK,
70         SERVER_CHECK,
71         FIND_MASTER,
72         CHECK_MASTER,
73         NAME_REGISTER,
74         NAME_RELEASE,
75         NAME_CONFIRM_QUERY
76 };
77
78 /* a netbios name structure */
79 struct nmb_name {
80   char name[17];
81   char scope[64];
82   int name_type;
83 };
84
85 /* this is the structure used for the local netbios name list */
86 struct name_record
87 {
88   struct name_record *next;
89   struct name_record *prev;
90   struct nmb_name name;
91   time_t death_time;
92   struct in_addr ip;
93   int nb_flags;
94   enum name_source source;
95 };
96
97 /* browse and backup server cache for synchronising browse list */
98 struct browse_cache_record
99 {
100         struct browse_cache_record *next;
101         struct browse_cache_record *prev;
102
103         pstring name;
104         int type;
105         pstring group;
106         struct in_addr ip;
107         time_t sync_time;
108         BOOL synced;
109 };
110
111 /* this is used to hold the list of servers in my domain, and is */
112 /* contained within lists of domains */
113 struct server_record
114 {
115   struct server_record *next;
116   struct server_record *prev;
117
118   struct server_info_struct serv;
119   time_t death_time;  
120 };
121
122 /* a workgroup structure. it contains a list of servers */
123 struct work_record
124 {
125   struct work_record *next;
126   struct work_record *prev;
127
128   struct server_record *serverlist;
129
130   /* work group info */
131   fstring work_group;
132   int     token;        /* used when communicating with backup browsers */
133   int     ServerType;
134
135   /* announce info */
136   time_t lastannounce_time;
137   int announce_interval;
138   BOOL    needannounce;
139
140   /* election info */
141   BOOL    RunningElection;
142   BOOL    needelection;
143   int     ElectionCount;
144   uint32  ElectionCriterion;
145 };
146
147 /* a domain structure. it contains a list of workgroups */
148 struct domain_record
149 {
150   struct domain_record *next;
151   struct domain_record *prev;
152
153   struct work_record *workgrouplist;
154
155   struct in_addr bcast_ip;
156   struct in_addr mask_ip;
157   struct in_addr myip;
158 };
159
160 /* a resource record */
161 struct res_rec {
162   struct nmb_name rr_name;
163   int rr_type;
164   int rr_class;
165   int ttl;
166   int rdlength;
167   char rdata[MAX_DGRAM_SIZE];
168 };
169
170 /* define a nmb packet. */
171 struct nmb_packet
172 {
173   struct {
174     int name_trn_id;
175     int opcode;
176     BOOL response;
177     struct {
178       BOOL bcast;
179       BOOL recursion_available;
180       BOOL recursion_desired;
181       BOOL trunc;
182       BOOL authoritative;
183     } nm_flags;
184     int rcode;
185     int qdcount;
186     int ancount;
187     int nscount;
188     int arcount;
189   } header;
190
191   struct {
192     struct nmb_name question_name;
193     int question_type;
194     int question_class;
195   } question;
196
197   struct res_rec *answers;
198   struct res_rec *nsrecs;
199   struct res_rec *additional;
200 };
201
202
203 /* initiated name queries recorded in this list to track any responses... */
204 struct name_response_record
205 {
206   struct name_response_record *next;
207   struct name_response_record *prev;
208
209   uint16 response_id;
210   enum cmd_type cmd_type;
211
212   int fd;
213   struct nmb_name name;
214   BOOL bcast;
215   BOOL recurse;
216   struct in_addr to_ip;
217
218   time_t start_time;
219   int num_msgs;
220 };
221
222 /* a datagram - this normally contains SMB data in the data[] array */
223 struct dgram_packet {
224   struct {
225     int msg_type;
226     struct {
227       enum node_type node_type;
228       BOOL first;
229       BOOL more;
230     } flags;
231     int dgm_id;
232     struct in_addr source_ip;
233     int source_port;
234     int dgm_length;
235     int packet_offset;
236   } header;
237   struct nmb_name source_name;
238   struct nmb_name dest_name;
239   int datasize;
240   char data[MAX_DGRAM_SIZE];
241 };
242
243 /* define a structure used to queue packets. this will be a linked
244  list of nmb packets */
245 struct packet_struct
246 {
247   struct packet_struct *next;
248   struct packet_struct *prev;
249   struct in_addr ip;
250   int port;
251   int fd;
252   time_t timestamp;
253   enum packet_type packet_type;
254   union {
255     struct nmb_packet nmb;
256     struct dgram_packet dgram;
257   } packet;
258 };
259
260
261 #define AM_MASTER(work) (work->ServerType & SV_TYPE_MASTER_BROWSER)
262 #define AM_BACKUP(work) (work->ServerType & SV_TYPE_BACKUP_BROWSER)
263 #define AM_DOMCTL(work) (work->ServerType & SV_TYPE_DOMAIN_CTRL)
264
265
266 /* ids for netbios packet types */
267 #define ANN_HostAnnouncement         1
268 #define ANN_AnnouncementRequest      2
269 #define ANN_Election                 8
270 #define ANN_GetBackupListReq         9
271 #define ANN_GetBackupListResp       10
272 #define ANN_BecomeBackup            11
273 #define ANN_DomainAnnouncement      12
274 #define ANN_MasterAnnouncement      13
275 #define ANN_ResetBrowserState       14
276 #define ANN_LocalMasterAnnouncement 15
277
278
279 /* broadcast packet announcement intervals, in minutes */
280
281 /* search for master browsers of workgroups samba knows about, 
282    except default */
283 #define CHECK_TIME_MST_BROWSE       5 
284
285 /* request backup browser announcements from other servers */
286 #define CHECK_TIME_ANNOUNCE_BACKUP 15
287
288 /* request host announcements from other servers: min and max of interval */
289 #define CHECK_TIME_MIN_HOST_ANNCE   3
290 #define CHECK_TIME_MAX_HOST_ANNCE  12
291
292 /* announce as master to WINS server and any Primary Domain Controllers */
293 #define CHECK_TIME_MST_ANNOUNCE    15
294