nameserv.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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-1998
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (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.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. #define PERMANENT_TTL 0
  21. /* NTAS uses 2, NT uses 1, WfWg uses 0 */
  22. #define MAINTAIN_LIST 2
  23. #define ELECTION_VERSION 1
  24. #define MAX_DGRAM_SIZE (576) /* tcp/ip datagram limit is 576 bytes */
  25. #define MIN_DGRAM_SIZE 12
  26. /*********************************************************
  27. Types of reply packet.
  28. **********************************************************/
  29. enum netbios_reply_type_code { NMB_QUERY, NMB_STATUS, NMB_REG, NMB_REG_REFRESH,
  30. NMB_REL, NMB_WAIT_ACK, NMB_MULTIHOMED_REG,
  31. WINS_REG, WINS_QUERY };
  32. /* From rfc1002, 4.2.1.2 */
  33. /* Question types. */
  34. #define QUESTION_TYPE_NB_QUERY 0x20
  35. #define QUESTION_TYPE_NB_STATUS 0x21
  36. /* Question class */
  37. #define QUESTION_CLASS_IN 0x1
  38. /* Opcode definitions */
  39. #define NMB_NAME_QUERY_OPCODE 0x0
  40. #define NMB_NAME_REG_OPCODE 0x05 /* see rfc1002.txt 4.2.2,3,5,6,7,8 */
  41. #define NMB_NAME_RELEASE_OPCODE 0x06 /* see rfc1002.txt 4.2.9,10,11 */
  42. #define NMB_WACK_OPCODE 0x07 /* see rfc1002.txt 4.2.16 */
  43. /* Ambiguity in rfc1002 about which of these is correct. */
  44. /* WinNT uses 8 by default but can be made to use 9. */
  45. #define NMB_NAME_REFRESH_OPCODE_8 0x08 /* see rfc1002.txt 4.2.4 */
  46. #define NMB_NAME_REFRESH_OPCODE_9 0x09 /* see rfc1002.txt 4.2.4 */
  47. #define NMB_NAME_MULTIHOMED_REG_OPCODE 0x0F /* Invented by Microsoft. */
  48. /* XXXX what about all the other types?? 0x1, 0x2, 0x3, 0x4, 0x8? */
  49. /* Resource record types. rfc1002 4.2.1.3 */
  50. #define RR_TYPE_A 0x1
  51. #define RR_TYPE_NS 0x2
  52. #define RR_TYPE_NULL 0xA
  53. #define RR_TYPE_NB 0x20
  54. #define RR_TYPE_NBSTAT 0x21
  55. /* Resource record class. */
  56. #define RR_CLASS_IN 0x1
  57. /* NetBIOS flags */
  58. #define NB_GROUP 0x80
  59. #define NB_PERM 0x02
  60. #define NB_ACTIVE 0x04
  61. #define NB_CONFL 0x08
  62. #define NB_DEREG 0x10
  63. #define NB_BFLAG 0x00 /* Broadcast node type. */
  64. #define NB_PFLAG 0x20 /* Point-to-point node type. */
  65. #define NB_MFLAG 0x40 /* Mixed bcast & p-p node type. */
  66. #define NB_HFLAG 0x60 /* Microsoft 'hybrid' node type. */
  67. #define NB_NODETYPEMASK 0x60
  68. /* Mask applied to outgoing NetBIOS flags. */
  69. #define NB_FLGMSK 0xE0
  70. /* NetBIOS flag identifier. */
  71. #define NAME_GROUP(p) ((p)->data.nb_flags & NB_GROUP)
  72. #define NAME_BFLAG(p) (((p)->data.nb_flags & NB_NODETYPEMASK) == NB_BFLAG)
  73. #define NAME_PFLAG(p) (((p)->data.nb_flags & NB_NODETYPEMASK) == NB_PFLAG)
  74. #define NAME_MFLAG(p) (((p)->data.nb_flags & NB_NODETYPEMASK) == NB_MFLAG)
  75. #define NAME_HFLAG(p) (((p)->data.nb_flags & NB_NODETYPEMASK) == NB_HFLAG)
  76. /* Samba name state for a name in a namelist. */
  77. #define NAME_IS_ACTIVE(p) ((p)->data.nb_flags & NB_ACTIVE)
  78. #define NAME_IN_CONFLICT(p) ((p)->data.nb_flags & NB_CONFL)
  79. #define NAME_IS_DEREGISTERING(p) ((p)->data.nb_flags & NB_DEREG)
  80. /* Error codes for NetBIOS requests. */
  81. #define FMT_ERR 0x1 /* Packet format error. */
  82. #define SRV_ERR 0x2 /* Internal server error. */
  83. #define NAM_ERR 0x3 /* Name does not exist. */
  84. #define IMP_ERR 0x4 /* Request not implemented. */
  85. #define RFS_ERR 0x5 /* Request refused. */
  86. #define ACT_ERR 0x6 /* Active error - name owned by another host. */
  87. #define CFT_ERR 0x7 /* Name in conflict error. */
  88. #define REFRESH_TIME (15*60)
  89. #define NAME_POLL_REFRESH_TIME (5*60)
  90. #define NAME_POLL_INTERVAL 15
  91. /* Workgroup state identifiers. */
  92. #define AM_POTENTIAL_MASTER_BROWSER(work) ((work)->mst_state == MST_POTENTIAL)
  93. #define AM_LOCAL_MASTER_BROWSER(work) ((work)->mst_state == MST_BROWSER)
  94. #define AM_DOMAIN_MASTER_BROWSER(work) ((work)->dom_state == DOMAIN_MST)
  95. #define AM_DOMAIN_MEMBER(work) ((work)->log_state == LOGON_SRV)
  96. /* Microsoft browser NetBIOS name. */
  97. #define MSBROWSE "\001\002__MSBROWSE__\002"
  98. /* Mail slots. */
  99. #define BROWSE_MAILSLOT "\\MAILSLOT\\BROWSE"
  100. #define NET_LOGON_MAILSLOT "\\MAILSLOT\\NET\\NETLOGON"
  101. #define NT_LOGON_MAILSLOT "\\MAILSLOT\\NET\\NTLOGON"
  102. #define LANMAN_MAILSLOT "\\MAILSLOT\\LANMAN"
  103. /* Samba definitions for find_name_on_subnet(). */
  104. #define FIND_ANY_NAME 0
  105. #define FIND_SELF_NAME 1
  106. /*
  107. * The different name types that can be in namelists.
  108. *
  109. * SELF_NAME should only be on the broadcast and unicast subnets.
  110. * LMHOSTS_NAME should only be in the remote_broadcast_subnet.
  111. * REGISTER_NAME, DNS_NAME, DNSFAIL_NAME should only be in the wins_server_subnet.
  112. * WINS_PROXY_NAME should only be on the broadcast subnets.
  113. * PERMANENT_NAME can be on all subnets except remote_broadcast_subnet.
  114. *
  115. */
  116. enum name_source {LMHOSTS_NAME, REGISTER_NAME, SELF_NAME, DNS_NAME,
  117. DNSFAIL_NAME, PERMANENT_NAME, WINS_PROXY_NAME};
  118. enum node_type {B_NODE=0, P_NODE=1, M_NODE=2, NBDD_NODE=3};
  119. enum packet_type {NMB_PACKET, DGRAM_PACKET};
  120. enum master_state
  121. {
  122. MST_NONE,
  123. MST_POTENTIAL,
  124. MST_BACKUP,
  125. MST_MSB,
  126. MST_BROWSER,
  127. MST_UNBECOMING_MASTER
  128. };
  129. enum domain_state
  130. {
  131. DOMAIN_NONE,
  132. DOMAIN_WAIT,
  133. DOMAIN_MST
  134. };
  135. enum logon_state
  136. {
  137. LOGON_NONE,
  138. LOGON_WAIT,
  139. LOGON_SRV
  140. };
  141. struct subnet_record;
  142. struct nmb_data
  143. {
  144. uint16 nb_flags; /* Netbios flags. */
  145. int num_ips; /* Number of ip entries. */
  146. struct in_addr *ip; /* The ip list for this name. */
  147. enum name_source source; /* Where the name came from. */
  148. time_t death_time; /* The time the record must be removed (do not remove if 0). */
  149. time_t refresh_time; /* The time the record should be refreshed. */
  150. };
  151. /* This is used to hold the list of servers in my domain, and is
  152. contained within lists of domains. */
  153. struct server_record
  154. {
  155. struct server_record *next;
  156. struct server_record *prev;
  157. struct subnet_record *subnet;
  158. struct server_info_struct serv;
  159. time_t death_time;
  160. };
  161. /* A workgroup structure. It contains a list of servers. */
  162. struct work_record
  163. {
  164. struct work_record *next;
  165. struct work_record *prev;
  166. struct subnet_record *subnet;
  167. struct server_record *serverlist;
  168. /* Stage of development from non-local-master up to local-master browser. */
  169. enum master_state mst_state;
  170. /* Stage of development from non-domain-master to domain-master browser. */
  171. enum domain_state dom_state;
  172. /* Stage of development from non-logon-server to logon server. */
  173. enum logon_state log_state;
  174. /* Work group info. */
  175. fstring work_group;
  176. int token; /* Used when communicating with backup browsers. */
  177. fstring local_master_browser_name; /* Current local master browser. */
  178. /* Announce info. */
  179. time_t lastannounce_time;
  180. int announce_interval;
  181. BOOL needannounce;
  182. /* Timeout time for this workgroup. 0 means permanent. */
  183. time_t death_time;
  184. /* Election info */
  185. BOOL RunningElection;
  186. BOOL needelection;
  187. int ElectionCount;
  188. uint32 ElectionCriterion;
  189. /* Domain master browser info. Used for efficient syncs. */
  190. struct nmb_name dmb_name;
  191. struct in_addr dmb_addr;
  192. };
  193. /* typedefs needed to define copy & free functions for userdata. */
  194. struct userdata_struct;
  195. typedef struct userdata_struct * (*userdata_copy_fn)(struct userdata_struct *);
  196. typedef void (*userdata_free_fn)(struct userdata_struct *);
  197. /* Structure to define any userdata passed around. */
  198. struct userdata_struct {
  199. userdata_copy_fn copy_fn;
  200. userdata_free_fn free_fn;
  201. unsigned int userdata_len;
  202. char data[16]; /* 16 is to ensure alignment/padding on all systems */
  203. };
  204. struct response_record;
  205. struct packet_struct;
  206. struct res_rec;
  207. /* typedef to define the function called when this response packet comes in. */
  208. typedef void (*response_function)(struct subnet_record *, struct response_record *,
  209. struct packet_struct *);
  210. /* typedef to define the function called when this response record times out. */
  211. typedef void (*timeout_response_function)(struct subnet_record *,
  212. struct response_record *);
  213. /* typedef to define the function called when the request that caused this
  214. response record to be created is successful. */
  215. typedef void (*success_function)(struct subnet_record *, struct userdata_struct *, ...);
  216. /* typedef to define the function called when the request that caused this
  217. response record to be created is unsuccessful. */
  218. typedef void (*fail_function)(struct subnet_record *, struct response_record *, ...);
  219. /* List of typedefs for success and fail functions of the different query
  220. types. Used to catch any compile time prototype errors. */
  221. typedef void (*register_name_success_function)( struct subnet_record *,
  222. struct userdata_struct *,
  223. struct nmb_name *,
  224. uint16,
  225. int,
  226. struct in_addr);
  227. typedef void (*register_name_fail_function)( struct subnet_record *,
  228. struct response_record *,
  229. struct nmb_name *);
  230. typedef void (*release_name_success_function)( struct subnet_record *,
  231. struct userdata_struct *,
  232. struct nmb_name *,
  233. struct in_addr);
  234. typedef void (*release_name_fail_function)( struct subnet_record *,
  235. struct response_record *,
  236. struct nmb_name *);
  237. typedef void (*refresh_name_success_function)( struct subnet_record *,
  238. struct userdata_struct *,
  239. struct nmb_name *,
  240. uint16,
  241. int,
  242. struct in_addr);
  243. typedef void (*refresh_name_fail_function)( struct subnet_record *,
  244. struct response_record *,
  245. struct nmb_name *);
  246. typedef void (*query_name_success_function)( struct subnet_record *,
  247. struct userdata_struct *,
  248. struct nmb_name *,
  249. struct in_addr,
  250. struct res_rec *answers);
  251. typedef void (*query_name_fail_function)( struct subnet_record *,
  252. struct response_record *,
  253. struct nmb_name *,
  254. int);
  255. typedef void (*node_status_success_function)( struct subnet_record *,
  256. struct userdata_struct *,
  257. struct res_rec *,
  258. struct in_addr);
  259. typedef void (*node_status_fail_function)( struct subnet_record *,
  260. struct response_record *);
  261. /* Initiated name queries are recorded in this list to track any responses. */
  262. struct response_record
  263. {
  264. struct response_record *next;
  265. struct response_record *prev;
  266. uint16 response_id;
  267. /* Callbacks for packets received or not. */
  268. response_function resp_fn;
  269. timeout_response_function timeout_fn;
  270. /* Callbacks for the request succeeding or not. */
  271. success_function success_fn;
  272. fail_function fail_fn;
  273. struct packet_struct *packet;
  274. struct userdata_struct *userdata;
  275. int num_msgs;
  276. time_t repeat_time;
  277. time_t repeat_interval;
  278. int repeat_count;
  279. /* Recursion protection. */
  280. BOOL in_expiration_processing;
  281. };
  282. /* A subnet structure. It contains a list of workgroups and netbios names. */
  283. /*
  284. B nodes will have their own, totally separate subnet record, with their
  285. own netbios name set. These do NOT interact with other subnet records'
  286. netbios names.
  287. */
  288. enum subnet_type {
  289. NORMAL_SUBNET = 0, /* Subnet listed in interfaces list. */
  290. UNICAST_SUBNET = 1, /* Subnet for unicast packets. */
  291. REMOTE_BROADCAST_SUBNET = 2, /* Subnet for remote broadcasts. */
  292. WINS_SERVER_SUBNET = 3 /* Only created if we are a WINS server. */
  293. };
  294. /* A resource record. */
  295. struct res_rec {
  296. struct nmb_name rr_name;
  297. int rr_type;
  298. int rr_class;
  299. int ttl;
  300. int rdlength;
  301. char rdata[MAX_DGRAM_SIZE];
  302. };
  303. /* An nmb packet. */
  304. struct nmb_packet
  305. {
  306. struct {
  307. int name_trn_id;
  308. int opcode;
  309. BOOL response;
  310. struct {
  311. BOOL bcast;
  312. BOOL recursion_available;
  313. BOOL recursion_desired;
  314. BOOL trunc;
  315. BOOL authoritative;
  316. } nm_flags;
  317. int rcode;
  318. int qdcount;
  319. int ancount;
  320. int nscount;
  321. int arcount;
  322. } header;
  323. struct {
  324. struct nmb_name question_name;
  325. int question_type;
  326. int question_class;
  327. } question;
  328. struct res_rec *answers;
  329. struct res_rec *nsrecs;
  330. struct res_rec *additional;
  331. };
  332. /* A datagram - this normally contains SMB data in the data[] array. */
  333. struct dgram_packet {
  334. struct {
  335. int msg_type;
  336. struct {
  337. enum node_type node_type;
  338. BOOL first;
  339. BOOL more;
  340. } flags;
  341. int dgm_id;
  342. struct in_addr source_ip;
  343. int source_port;
  344. int dgm_length;
  345. int packet_offset;
  346. } header;
  347. struct nmb_name source_name;
  348. struct nmb_name dest_name;
  349. int datasize;
  350. char data[MAX_DGRAM_SIZE];
  351. };
  352. /* Define a structure used to queue packets. This will be a linked
  353. list of nmb packets. */
  354. struct packet_struct
  355. {
  356. struct packet_struct *next;
  357. struct packet_struct *prev;
  358. BOOL locked;
  359. struct in_addr ip;
  360. int port;
  361. int fd;
  362. time_t timestamp;
  363. enum packet_type packet_type;
  364. union {
  365. struct nmb_packet nmb;
  366. struct dgram_packet dgram;
  367. } packet;
  368. };
  369. /* NETLOGON opcodes */
  370. #define QUERYFORPDC 7 /* Query for PDC. */
  371. #define QUERYFORPDC_R 12 /* Response to Query for PDC. */
  372. #define SAMLOGON 18
  373. #define SAMLOGON_R 19
  374. /* Ids for netbios packet types. */
  375. #define ANN_HostAnnouncement 1
  376. #define ANN_AnnouncementRequest 2
  377. #define ANN_Election 8
  378. #define ANN_GetBackupListReq 9
  379. #define ANN_GetBackupListResp 10
  380. #define ANN_BecomeBackup 11
  381. #define ANN_DomainAnnouncement 12
  382. #define ANN_MasterAnnouncement 13
  383. #define ANN_ResetBrowserState 14
  384. #define ANN_LocalMasterAnnouncement 15
  385. /* Broadcast packet announcement intervals, in minutes. */
  386. /* Attempt to add domain logon and domain master names. */
  387. #define CHECK_TIME_ADD_DOM_NAMES 5
  388. /* Search for master browsers of workgroups samba knows about,
  389. except default. */
  390. #define CHECK_TIME_MST_BROWSE 5
  391. /* Request backup browser announcements from other servers. */
  392. #define CHECK_TIME_ANNOUNCE_BACKUP 15
  393. /* Request host announcements from other servers: min and max of interval. */
  394. #define CHECK_TIME_MIN_HOST_ANNCE 3
  395. #define CHECK_TIME_MAX_HOST_ANNCE 12
  396. /* Announce as master to WINS server and any Primary Domain Controllers. */
  397. #define CHECK_TIME_MST_ANNOUNCE 15
  398. /* Time between syncs from domain master browser to local master browsers. */
  399. #define CHECK_TIME_DMB_TO_LMB_SYNC 15
  400. /* Do all remote announcements this often. */
  401. #define REMOTE_ANNOUNCE_INTERVAL 180
  402. /* what is the maximum period between name refreshes. Note that this only
  403. affects non-permanent self names (in seconds) */
  404. #define MAX_REFRESH_TIME (60*20)
  405. /* Macro's to enumerate subnets either with or without
  406. the UNICAST subnet. */
  407. extern struct subnet_record *subnetlist;
  408. extern struct subnet_record *unicast_subnet;
  409. extern struct subnet_record *wins_server_subnet;
  410. extern struct subnet_record *remote_broadcast_subnet;
  411. #define FIRST_SUBNET subnetlist
  412. #define NEXT_SUBNET_EXCLUDING_UNICAST(x) ((x)->next)
  413. #define NEXT_SUBNET_INCLUDING_UNICAST(x) (get_next_subnet_maybe_unicast((x)))
  414. /* To be removed. */
  415. enum state_type { TEST };
  416. #endif /* _NAMESERV_H_ */