2N/A * The contents of this file are subject to the terms of the 2N/A * Common Development and Distribution License (the "License"). 2N/A * You may not use this file except in compliance with the License. 2N/A * See the License for the specific language governing permissions 2N/A * and limitations under the License. 2N/A * When distributing Covered Code, include this CDDL HEADER in each 2N/A * If applicable, add the following below this CDDL HEADER, with the 2N/A * fields enclosed by brackets "[]" replaced with your own identifying 2N/A * information: Portions Copyright [yyyy] [name of copyright owner] 2N/A * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. 2N/A * NetBIOS name resolution node types. 2N/A * A B-node (broadcast node) uses broadcasts for name registration 2N/A * and resolution. Routers typically do not forward broadcasts and 2N/A * only computers on the local subnet will respond. 2N/A * A P-node (peer-to-peer node) uses a NetBIOS name server (WINS) 2N/A * to resolve NetBIOS names, which allows it to work across routers. 2N/A * In order to function in a P-node environment, all computers must 2N/A * be configured to use the NetBIOS name server because P-nodes do 2N/A * not broadcast on the network. 2N/A * A mixed node (M-node) behaves as a B-node by default. If it cannot 2N/A * resolve the name via broadcast then it tries a NetBIOS name server 2N/A * A hybrid node (H-node) behaves as a P-node by default. If it cannot 2N/A * resolve the name using a NetBIOS name server then it resorts to 2N/A * broadcasts (B-node). 2N/A * NetBIOS Name Service Protocols 2N/A * A REQUEST packet is always sent to the well known UDP port 137. 2N/A * The destination address is normally either the IP broadcast address or 2N/A * the address of the NAME - the address of the NAME server it set up at 2N/A * initialization time. In rare cases, a request packet will be sent to 2N/A * an end node, e.g. a NAME QUERY REQUEST sent to "challenge" a node. 2N/A * A RESPONSE packet is always sent to the source UDP port and source IP 2N/A * address of the request packet. 2N/A * A DEMAND packet must always be sent to the well known UDP port 137. 2N/A * There is no restriction on the target IP address. 2N/A * A transaction ID is a value composed from the requestor's IP address and 2N/A * a unique 16 bit value generated by the originator of the transaction. 2N/A * RFC 1002 4.2.1.1. HEADER 2N/A * RFC 1002 4.2.1.3. RESOURCE RECORD 2N/A * RESOURCE RECORD RR_CLASS field definitions 2N/A * NB_FLAGS field of the RESOURCE RECORD RDATA field for RR_TYPE of NB. 2N/A * Allocate a transaction id. 2N/A * The response packet has in it the address of the presumed owner 2N/A * of the name. Challenge that owner. If owner either does not 2N/A * respond or indicates that he no longer owns the name, claim the 2N/A * name. Otherwise, the name cannot be claimed. 2N/A /* Presumably nobody is waiting any more... */ 2N/A return (0);
/* No reply: retry */ 2N/A * Make sure that remote name is not 2N/A * a name in the state "conflict 2N/A * detected" does not "logically" exist 2N/A * on that node. No further session 2N/A * will be accepted on that name. 2N/A * No datagrams can be sent against 2N/A * Such an entry will not be used for 2N/A * purposes of processing incoming 2N/A * The only valid user NetBIOS operation 2N/A * against such a name is DELETE NAME. 2N/A "nbns: name conflict: %15.15s",
2N/A * name can be added: 2N/A * adjust refresh timeout value, 2N/A * TTL, for this name 2N/A * The response packet has in it the 2N/A * address of the presumed owner of the 2N/A * name. Challenge that owner. If 2N/A * owner either does not respond or 2N/A * indicates that he no longer owns the 2N/A * name, claim the name. Otherwise, 2N/A * the name cannot be claimed. 2N/A * smb_name_buf_from_packet 2N/A * Convert a NetBIOS Name Server Packet Block (npb) 2N/A * into the bits and bytes destined for the wire. 2N/A * The "buf" is used as a heap. 2N/A * char * buf -> Buffer, from the wire 2N/A * unsigned n_buf -> Length of 'buf' 2N/A * name_packet *npb -> Packet block, decode into 2N/A * unsigned n_npb -> Max bytes in 'npb' 2N/A * >0 -> Encode successful, value is length of packet in "buf" 2N/A * -1 -> Hard error, can not possibly encode 2N/A * -2 -> Need more memory in buf -- it's too small 2N/A return (-
1);
/* no header, impossible */ 2N/A /* truly ugly, but saves code copying */ 2N/A }
else {
/* step == 3 */ 2N/A for (i = 0; i < n; i++) {
2N/A * Lookup for character 'c' in first 'n' chars of string 's'. 2N/A * Returns pointer to the found char, otherwise returns 0. 2N/A char *
es = (
char *)s + n;
2N/A if (*
ps ==
'\0' && c ==
'\0')
2N/A * smb_netbios_getname 2N/A * Get the Netbios name part of the given record. 2N/A * Does some boundary checks. 2N/A * Returns the name length on success, otherwise 2N/A /* no room for a NB name */ 2N/A /* not a valid NB name */ 2N/A * smb_name_buf_to_packet 2N/A * Convert the bits and bytes that came from the wire into a NetBIOS 2N/A * Name Server Packet Block (npb). The "block" is used as a heap. 2N/A * Returns a pointer to a name packet on success. Otherwise, returns 2N/A /* truncated header */ 2N/A /* scan is in position for question entries */ 2N/A * Measure the space needed for the tables 2N/A /* LINTED - E_BAD_PTR_CAST_ALIGN */ 2N/A /* LINTED - E_BAD_PTR_CAST_ALIGN */ 2N/A /* LINTED - E_BAD_PTR_CAST_ALIGN */ 2N/A /* LINTED - E_BAD_PTR_CAST_ALIGN */ 2N/A /* LINTED - E_BAD_PTR_CAST_ALIGN */ 2N/A /* LINTED - E_BAD_PTR_CAST_ALIGN */ 2N/A * Populate each resource_record's .name field. 2N/A * Done as a second pass so that all resource records 2N/A * (answer, authority, additional) are consecutive via nrr[i]. 2N/A /* LINTED - E_BAD_PTR_CAST_ALIGN */ 2N/A /* Couldn't decode the question name */ 2N/A /* no room for Question Type(2) and Class(2) fields */ 2N/A * Cheat. Remaining sections are of the same resource_record 2N/A * format. Table space is consecutive. 2N/A /* Namebuf is reused... */ 2N/A * no room for RR_TYPE (2), RR_CLASS (2), TTL (4) and 2N/A * RDLENGTH (2) fields. 2N/A /* no room for RDATA */ 2N/A /* not enough memory */ 2N/A * smb_send_name_service_packet 2N/A * Send out a name service packet to proper destination. 2N/A * struct netbios_name *dest -> NETBIOS name of destination 2N/A * struct name_packet *packet -> Packet to send 2N/A * smb_netbios_send_rcv 2N/A * This function sends the given NetBIOS packet to the given 2N/A * address and get back the response. If send operation is not 2N/A * successful, it's repeated 'retries' times. 2N/A * 0 Unsuccessful send operation; no reply 2N/A * RFC 1002 4.2.2. NAME REGISTRATION REQUEST 2N/A * Only register with the Primary WINS server, 2N/A * unless we got no reply. 2N/A * RFC 1002 4.2.4. NAME REFRESH REQUEST 2N/A * the value of addr_num is irrelvant here, because 2N/A * the code is going to do special_process so it doesn't 2N/A * need the addr_num. We set a value here just to avoid 2N/A * RFC 1002 4.2.5. POSITIVE NAME REGISTRATION RESPONSE 2N/A * RFC 1002 4.2.6. NEGATIVE NAME REGISTRATION RESPONSE 2N/A * RFC 1002 4.2.9. NAME RELEASE REQUEST & DEMAND 2N/A * RFC 1002 4.2.10. POSITIVE NAME RELEASE RESPONSE 2N/A * RFC 1002 4.2.11. NEGATIVE NAME RELEASE RESPONSE 2N/A/* LINTED - E_STATIC_UNUSED */ 2N/A * RFC 1002 4.2.12. NAME QUERY REQUEST 2N/A * RFC 1002 4.2.13. POSITIVE NAME QUERY RESPONSE 2N/A * RFC 1002 4.2.14. NEGATIVE NAME QUERY RESPONSE 2N/A * RFC 1002 4.2.18. NODE STATUS RESPONSE 2N/A /* LINTED - E_BAD_PTR_CAST_ALIGN */ 2N/A /* build name service packet */ 2N/A * question.name->attributes |= NAME_NB_FLAGS_ONT_B; 2N/A * This is commented because NAME_NB_FLAGS_ONT_B is 0 2N/A * Host initiated processing for a P node 2N/A return (
1);
/* No name server configured */ 2N/A return (
1);
/* No name server configured */ 2N/A return (-
1);
/* No name server configured */ 2N/A /* Guard against malformed packets */ 2N/A * This opcode covers both NAME_QUERY_REQUEST and 2N/A * NODE_STATUS_REQUEST. They can be distinguished 2N/A * based on the type of question entry. 2N/A /* All query requests have to have question entry */ 2N/A * Name of "*" may be used to force node to 2N/A * divulge status for administrative purposes 2N/A * send only those names that are 2N/A * in the same scope as the scope 2N/A * field in the request packet 2N/A * always ignore UDP broadcast packets 2N/A /* Guard against malformed packets */ 2N/A * This opcode covers both NAME_QUERY_REQUEST and 2N/A * NODE_STATUS_REQUEST. They can be distinguished 2N/A * based on the type of question entry. 2N/A /* All query requests have to have question entry */ 2N/A * send response to the IP address and port 2N/A * number from which the request was received. 2N/A * send response to the requestor 2N/A * Name of "*" may be used to force node to 2N/A * divulge status for administrative purposes 2N/A * send only those names that are 2N/A * in the same scope as the scope 2N/A * field in the request packet 2N/A * smb_netbios_name_tick 2N/A * Called once a second to handle name server timeouts. 2N/A * smb_name_find_name 2N/A * Lookup name cache for the given name. 2N/A * If it's not in the cache it'll send a 2N/A * name query request and then lookup the 2N/A * cache again. Note that if a name is 2N/A * returned it's locked and called MUST 2N/A * unlock it by calling smb_name_unlock_name() 2N/A * smb_netbios_worker 2N/A * name service (on port 138). 2N/A /* Request packet */ 2N/A * Configure the node type. If a WINS server has been specified, 2N/A * act like an H-node. Otherwise, behave like a B-node. 2N/A * Sends name registration requests on all configured network links. 2N/A * Name registration requests are sent to claim ownership of a name. 2N/A * If WINS server(s) are configured, the request is directly sent 2N/A * to those servers, if not the request is broadcasted. 2N/A * Currently the code cannot scale well to handle hunderds of network 2N/A * links (which can be created using VLANs) so a maximum is enforced. 2N/A * If the number of configured links is greater than a threshold then 2N/A * name registration broadcast will not be done on any links. 2N/A * Note that the node configuration must be setup before calling 2N/A * smb_init_name_struct(). 2N/A * NetBIOS Name Service (port 137) 2N/A * Initialize reply_queue 2N/A /* Sleep for 10 seconds and try again */ 2N/A /* Sleep for 10 seconds and try again */ 2N/A /* Ignore any incoming packets from myself... */ 2N/A * Launch a netbios worker to process the received packet.