10.1 Interpreting the Server Identifier

The SMB URL format supports the use of three different identifier types in the host field. We went over them briefly before. They are the IP address, DNS name, or NetBIOS name of the destination. Our next task is to figure out which is which.

Presentation is everything, and it turns out that the code for interpreting the Server Identifier is verbose and tedious . Most of the busywork for handling NetBIOS names was covered in Part I, and there are plenty of tools for dealing with IP addresses and DNS names , so to save time we will describe how to interpret and resolve the address (and let you write the code yourself). [2]

[2] Additional source code is available at http://ubiqx.org/libcifs/.

It could be an IP address.

Check the syntax of the input to determine whether it is a valid representation of an IP address. Do this test first. It is quick, and does not involve sending any queries out over the network. The inet_aton() function, common on Unix-like operating systems, does the job nicely for the fourbyte IPv4 addresses used today.

IP version 6 (IPv6) addresses are different. They are longer, harder for a human to read, and potentially more complicated to parse out.

Fortunately, when used in URLs they are always contained within square brackets, as in the following example:

 smb://[fe80::240:f4ff:fe1f:8243]/ 

The square brackets are reserved characters , used specifically for this purpose. [3] They make it easy to identify an IPv6 IP address. Once identified, the IPv6 address can be converted into its internal format by the inet_pton() function, which is now supported by many systems.

[3] See RFC 2732 for information on the use of IPv6 addresses in URLs.

Note that it is, in theory, possible to register a NetBIOS name that looks exactly like an IP address. What's worse is that it might not be the same as the IP address of the node that registered it. That's nasty. Anyone who would do such a thing should have their keyboard taken away. It is probably not important to handle such situations. Defensive programming practices would suggest being prepared, but in this case the perpetrators deserve the troubles they cause for themselves .

It could be a NetBIOS Name.

If the Server Identifier isn't an IP address, it could be a NetBIOS name. To see if this is the case, the first step is to look for a dot ('.'). The SMB URL format does not allow unescaped dots to appear in the NetBIOS name itself, so if there is a dot character in the raw string then consider the rest of the string to be a Scope ID. For example:

 smb://my%2Enode.scope/ 

is made up of the NetBIOS name " MY.NODE " and the Scope ID " SCOPE ". (The URL escape sequence for encoding a dot is %2E .)

Once the string has been parsed into its NetBIOS Name and Scope ID components , the next thing to do is to send an NBT Name Query. Always use a suffix value of 0x20 , which is the prescribed suffix for SMB services. The handling of the query depends, of course, on whether the client is a B, P, M, or H node. For anything other than a B node, the IP address of the NBNS is required. Most client implementations keep such information in some form of configuration file or database.

If a positive response is received, keep track of the NetBIOS name and returned IP address. You will need them in order to connect to the server.

It could be a DNS name.

If the Server Identifier is neither an IP address nor a NetBIOS name, try DNS name resolution. The gethostbyname() function is commonly used to resolve DNS names to IP addresses, but be warned that this is a blocking function. It may take quite a while for it to do its job, and your program will do nothing in the meantime. [4] That is one reason why it is typically the last thing to try.

[4] Samba's nmbd daemon spawns a separate process to handle DNS queries, just to get around this very problem.

That is how to go about determining which kind of Server Identifier you've been given. Isn't overloading fun? Now you see why the code for handling all of this is tedious and verbose. It really is not very difficult, though, it's just that it takes a bit of work to get it all coded up.



Implementing CIFS. The Common Internet File System
Implementing CIFS: The Common Internet File System
ISBN: 013047116X
EAN: 2147483647
Year: 2002
Pages: 210

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net