/*******************************************************************************
        ftp_client.c
        Z-World, 2000

        Demonstration of the use of the ftp_client library to download a
        file.
*******************************************************************************/

/***********************************
 * Configuration                   *
 * -------------                   *
 * All fields in this section must *
 * be altered to match your local  *
 * network settings.               *
 ***********************************/

/*
 * You must change the following values to whatever
 * your local IP address and netmask are. Contact your
 * network administrator for these numbers.
 */
#define MY_IP_ADDRESS   "10.10.6.101"
#define MY_NETMASK      "255.255.255.0"

/*
 * FTP Client configuration
 *
 * The FTP client takes several parameters, to determine
 * where the remote ftp server is, and what file to get/put.
 * This can all be placed directly in the ftp_client_setup()
 * call below, but it is defined here for easy-access.
 */

/* Address of remote FTP server */
#define REMOTE_HOST		"10.10.6.1"

/* Port on the FTP server to connect to. 0 == default FTP port */
#define REMOTE_PORT		0

/* username on the server */
#define REMOTE_USERNAME "anonymous"

/* password on the server */
#define REMOTE_PASSWORD "anon@anon.com"

/* the file to get, including full path */
#define REMOTE_FILE		"bar"

/********************************
 * End of configuration section *
 ********************************/

#memmap xmem
#use "dcrtcp.lib"
#use "ftp_client.lib"

main()
{
	static char buf[1024];
	static int retval, i, j;
	
	printf("Calling sock_init()...\n");	
	sock_init();

	printf("Calling ftp_client_setup()...\n");
	if(ftp_client_setup(resolve(REMOTE_HOST),REMOTE_PORT,REMOTE_USERNAME,
			REMOTE_PASSWORD,FTP_MODE_DOWNLOAD,REMOTE_FILE,
			NULL,buf,sizeof(buf))) {
		printf("FTP setup failed.\n");
		exit(0);
	}

	printf("Looping on ftp_client_tick()...\n");
	while( 0 == (retval = ftp_client_tick()) )
		;

	if( 1 == retval ) {
		printf("FTP completed successfully.\n");

		buf[ftp_client_filesize()] = '\0';
		printf("Data => '%s'\n", buf);
	} else {
		printf("FTP failed: status == %d\n",retval);
	}
}