2 Unix SMB/Netbios implementation.
4 NBT netbios header - version 2
5 Copyright (C) Andrew Tridgell 1994-1997
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.
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.
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.
23 #define GET_TTL(ttl) ((ttl)?MIN(ttl,lp_max_ttl()):lp_max_ttl())
25 /* NTAS uses 2, NT uses 1, WfWg uses 0 */
26 #define MAINTAIN_LIST 2
27 #define ELECTION_VERSION 1
29 #define MAX_DGRAM_SIZE (576) /* tcp/ip datagram limit is 576 bytes */
30 #define MIN_DGRAM_SIZE 12
32 #define NMB_QUERY 0x20
33 #define NMB_STATUS 0x21
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? */
41 #define FIND_SELF 0x01
42 #define FIND_WINS 0x02
43 #define FIND_LOCAL 0x04
48 #define NB_ACTIVE 0x04
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
57 #define REFRESH_TIME (15*60)
58 #define NAME_POLL_REFRESH_TIME (5*60)
59 #define NAME_POLL_INTERVAL 15
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)
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)
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)
79 /* microsoft browser NetBIOS name */
80 #define MSBROWSE "\001\002__MSBROWSE__\002"
83 #define BROWSE_MAILSLOT "\\MAILSLOT\\BROWSE"
84 #define NET_LOGON_MAILSLOT "\\MAILSLOT\\NET\\NETLOGON"
86 enum name_source {STATUS_QUERY, LMHOSTS, REGISTER, SELF, DNS, DNSFAIL};
87 enum node_type {B_NODE=0, P_NODE=1, M_NODE=2, NBDD_NODE=3};
88 enum packet_type {NMB_PACKET, DGRAM_PACKET};
114 NAME_STATUS_DOM_SRV_CHK,
116 NAME_REGISTER_CHALLENGE,
120 NAME_QUERY_ANNOUNCE_HOST,
121 NAME_QUERY_SYNC_LOCAL,
122 NAME_QUERY_SYNC_REMOTE,
123 NAME_QUERY_DOM_SRV_CHK,
130 /* a netbios name structure */
137 /* a netbios flags + ip address structure */
138 /* this is used for multi-homed systems and for internet group names */
141 struct in_addr ip; /* ip address of host that owns this name */
142 uint16 nb_flags; /* netbios flags */
145 /* this is the structure used for the local netbios name list */
148 struct name_record *next;
149 struct name_record *prev;
151 struct nmb_name name; /* the netbios name */
152 struct nmb_ip *ip_flgs; /* the ip + flags */
153 int num_ips; /* number of ip+flags entries */
155 enum name_source source; /* where the name came from */
157 time_t death_time; /* time record must be removed (do not remove if 0) */
158 time_t refresh_time; /* time record should be refreshed */
161 struct subnet_record;
163 /* browse and backup server cache for synchronising browse list */
164 struct browse_cache_record
166 struct browse_cache_record *next;
167 struct browse_cache_record *prev;
176 struct subnet_record *subnet;
179 /* this is used to hold the list of servers in my domain, and is */
180 /* contained within lists of domains */
183 struct server_record *next;
184 struct server_record *prev;
186 struct server_info_struct serv;
190 /* a workgroup structure. it contains a list of servers */
193 struct work_record *next;
194 struct work_record *prev;
196 struct server_record *serverlist;
198 /* stage of development from non-local-master up to local-master browser */
199 enum master_state mst_state;
201 /* stage of development from non-domain-master to domain master browser */
202 enum domain_state dom_state;
204 /* stage of development from non-logon-server to logon server */
205 enum logon_state log_state;
207 /* work group info */
209 int token; /* used when communicating with backup browsers */
213 time_t lastannounce_time;
214 int announce_interval;
219 BOOL RunningElection;
222 uint32 ElectionCriterion;
225 /* initiated name queries recorded in this list to track any responses... */
226 /* sadly, we need to group everything together. i suppose that if this
227 gets unwieldy, then a union ought to be considered. oh for c++... */
228 struct response_record
230 struct response_record *next;
231 struct response_record *prev;
234 enum state_type state;
238 struct nmb_name name;
248 struct in_addr send_ip;
249 struct in_addr reply_to_ip;
254 time_t repeat_interval;
258 /* a subnet structure. it contains a list of workgroups and netbios names*/
260 /* note that a subnet of 255.255.255.255 contains all the WINS netbios names.
261 all communication from such nodes are on a non-broadcast basis: they
262 are point-to-point (P nodes) or mixed point-to-point and broadcast
263 (M nodes). M nodes use point-to-point as a preference, and will use
264 broadcasting for certain activities, or will resort to broadcasting as a
265 last resort, if the WINS server fails (users of wfwg will notice that their
266 machine often freezes for 30 seconds at a time intermittently, if the WINS
269 B nodes will have their own, totally separate subnet record, with their
270 own netbios name set. these do NOT interact with other subnet records'
271 netbios names, INCLUDING the WINS one (with an ip "address", so called,
274 there is a separate response list for each subnet record. in the case of
275 the 255.255.255.255 subnet record (WINS), the WINS server will be able to
276 use this to poll (infrequently!) each of its entries, to ensure that the
277 names are still in use.
278 XXXX this polling is a planned feature for a really over-cautious WINS server
283 struct subnet_record *next;
284 struct subnet_record *prev;
286 struct work_record *workgrouplist; /* list of workgroups */
287 struct name_record *namelist; /* list of netbios names */
288 struct response_record *responselist; /* list of responses expected */
290 struct in_addr bcast_ip;
291 struct in_addr mask_ip;
295 /* a resource record */
297 struct nmb_name rr_name;
302 char rdata[MAX_DGRAM_SIZE];
305 /* define a nmb packet. */
314 BOOL recursion_available;
315 BOOL recursion_desired;
327 struct nmb_name question_name;
332 struct res_rec *answers;
333 struct res_rec *nsrecs;
334 struct res_rec *additional;
338 /* a datagram - this normally contains SMB data in the data[] array */
339 struct dgram_packet {
343 enum node_type node_type;
348 struct in_addr source_ip;
353 struct nmb_name source_name;
354 struct nmb_name dest_name;
356 char data[MAX_DGRAM_SIZE];
359 /* define a structure used to queue packets. this will be a linked
360 list of nmb packets */
363 struct packet_struct *next;
364 struct packet_struct *prev;
369 enum packet_type packet_type;
371 struct nmb_packet nmb;
372 struct dgram_packet dgram;
377 /* ids for netbios packet types */
378 #define ANN_HostAnnouncement 1
379 #define ANN_AnnouncementRequest 2
380 #define ANN_Election 8
381 #define ANN_GetBackupListReq 9
382 #define ANN_GetBackupListResp 10
383 #define ANN_BecomeBackup 11
384 #define ANN_DomainAnnouncement 12
385 #define ANN_MasterAnnouncement 13
386 #define ANN_ResetBrowserState 14
387 #define ANN_LocalMasterAnnouncement 15
390 /* broadcast packet announcement intervals, in minutes */
392 /* attempt to add domain logon and domain master names */
393 #define CHECK_TIME_ADD_DOM_NAMES 5
395 /* search for master browsers of workgroups samba knows about,
397 #define CHECK_TIME_MST_BROWSE 5
399 /* request backup browser announcements from other servers */
400 #define CHECK_TIME_ANNOUNCE_BACKUP 15
402 /* request host announcements from other servers: min and max of interval */
403 #define CHECK_TIME_MIN_HOST_ANNCE 3
404 #define CHECK_TIME_MAX_HOST_ANNCE 12
406 /* announce as master to WINS server and any Primary Domain Controllers */
407 #define CHECK_TIME_MST_ANNOUNCE 15
409 /* do all remote announcements this often */
410 #define REMOTE_ANNOUNCE_INTERVAL 180
412 #define DFLT_SERVER_TYPE (SV_TYPE_WORKSTATION | SV_TYPE_SERVER | \
413 SV_TYPE_TIME_SOURCE | SV_TYPE_SERVER_UNIX | \
414 SV_TYPE_PRINTQ_SERVER | SV_TYPE_SERVER_NT | \
417 /* Macro's to enumerate subnets either with or without
420 extern struct subnet_record *subnetlist;
421 extern struct subnet_record *wins_subnet;
423 #define FIRST_SUBNET subnetlist
424 #define NEXT_SUBNET_EXCLUDING_WINS(x) ((x)->next)
425 #define NEXT_SUBNET_INCLUDING_WINS(x) ( ((x) == wins_subnet) ? 0 : \
426 (((x)->next == 0) ? wins_subnet : \