Network interaction

When BASH detects that an argument its command received requires a network connection, the following stages occur:
  1. A corresponding function (fetching, putting, listing, deleting) is being called from module trftp.c.
  2. That function first opens a connection socket to manage the data transfer between hosts.
  3. Then another, data socket is opened, a fork is being spawned and the data is being transferred. The data connection terminates.
  4. Then the connection socket closes, and we return to BASH.

The scheme is below:

Then here follows the description of all these stages, especially 2 and 3. The related source code can be obtained either from source code pages, or from source code appendix of the print.

BASH calls, upon needed action on of the following functions: get_file(ruser, host, pathname, filename, local_filename, to_delete)
put_file(ruser, host, pathname, filename, local_filename)
list_file(ruser, host, pathname, filename)
delete_file(ruser, host, pathname, filename, pass)
All of those functions first call the function connect_file() which establishes the connection and logs the user in. This is done in the following way:

We first check, if the hostname is valid, and if not return back with a fail error code and corresponding output is being printed.

Then we attempt to establish a socket connection, using socket() function of standard C library. If no socket is created, we return back with corresponding error code and message.
When a socket connection is ready, we can communicate through it with the remote server, sending it commands of the known, predefined list and reading back server's output. The output of the server looks like this:
###!{message string}
where ### is the protocol number of the message (contact the RFC for the list of the protocol numbers), and ! is the distinction character, which is space in case of usual message and '-' in case of a commentary message.

We use routine expect.c from module connect.c to read the output from server. It's built on a backbone of already existing function of the same name, from Empire Client((C) Dave Pare, 1989).
The routine reads into a buffer string the output from server until it encounters a new-line character. Then the fourth character is analyzed to be '-' or ' ', and if first, reading continues, else, if second, the first three numbers are being passed to the routine above where analyzed.

If the received protocol number coincides with the expected number, we proceed. The commands are being sent into server using routine sendcmd.c, from the same Empire Client (handy program, isn't it?). If sending a command (which is writing into socket) fails, we return back with the corresponding error code and message.

Thus we communicate until stage of data transfer comes (putting, getting, listing; deleting doesn't require data transfer). Then a new process is being forked. At this moment we have two sockets opened - one old, control connection socket, second new - data transfer. Those things are performed by the routine initcomm() which is taken from standard FTP client ((C) Regents of California, Berkeley, 1985). The child process reads/writes from/to data socket, while parent process manages it. The data transfer should be terminated by a message from server through a control connection socket and contain a specific protocol number. Upon analyzing of this number we may see, if the transfer completed successfully or failed and return the correct message back.

Errors processing and different error protocol codes are discussed in FTP description and Error handling chapters. Also consult Problems chapter for a couple of relevant issues.


romm@empire.tau.ac.il
Last modified: Wed Aug 27 13:37:27 1997