/* program to send a file to remote machine via udp  */
/* very very simple, just supply the local file name */
/* and the destination file name as the cmdline      */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <dos.h>
#include	<sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

unsigned long on=1;

int ack=1234;

int sendwithack(int s,char *buf, int length,struct sockaddr_in *sa)
	{
	int n,m,len,bytes;
	char realbuf[512];

	memcpy(realbuf+2,buf,length);

	for (m=0;m<5;m++)
		{

		*((short *)realbuf) = ack;
		sendto(s,realbuf,length+2,0,(struct sockaddr *)sa,sizeof(struct sockaddr_in));

		for (n=0;n<3000;n++)
			{
			apiPause();
			len = sizeof(struct sockaddr_in);
			bytes = recvfrom(s,(char *)&realbuf,sizeof(realbuf),0,(struct sockaddr *)sa,&len);
			if (bytes>=0)
				{
				if (*((short *)realbuf) == ack)
					{
					ack++;
					return 1;
					}
				}
			}
		}

	return 0;
	}


main(int argc, char *argv[])
	{
	int s,n;
	FILE *fp;
	FILE *sin,*sout;
	struct sockaddr_in servaddr,hostaddr;
	struct hostent *he;
	int bit,writebit,readbit,exceptbit=0,iobit;
	char *hostname;
	char string[256];
	unsigned char buff[1024],*b;
	int bytes;
	struct timeval t;
	char *username;
	int portnum,len;
	int sendlength,total;

	if (argc!=4)
		{
		printf("QFTP local-file remote-file host\n");
		exit(1);
		}


	hostname = strdup(argv[3];

	if (!so_init()) printf("Socket interface failed to initialise.\n");

/* create the socket */

	s = socket(AF_INET, SOCK_DGRAM, 0);

/* set up receive address */

	hostaddr.sin_family = AF_INET;
	hostaddr.sin_port = htons(4000);
	hostaddr.sin_addr.s_addr = htonl(INADDR_ANY);

/* set up domain name server address */

	sethostent();

	if (!(he = gethostbyname(hostname)))
		{
		endhostent();
		printf("Unable to resolve host %s.\n",hostname);
		exit(30);
		}

	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(1234);	/* qftp port */
	memcpy(&servaddr.sin_addr,he->h_addr_list[0],he->h_length);

	endhostent();

/* allocate the local port (fixed at 4000 'cos I'm a lazy bugger) */

	if (bind(s, (struct sockaddr *)&hostaddr,sizeof(hostaddr)) == -1)
		{
		exit(1);
		}

/* this is to enable polling of the socket */

	if (ioctl(s, FIONBIO, (long *)&on) < 0)
		{
		printf("Unable to unblock socket.\n");
		exit(1);
		}

/* poll the socket */

	while (1)
		{
		if (fp = fopen(argv[1],"rb"))
			{
			if (!sendwithack(s,argv[2],strlen(argv[2])+1,&servaddr))
				{
				printf("Unable to establish connect.\n");
				break;
				}

			printf("%s=>%s ",argv[1],argv[2]);
			fflush(stdout);

			total = 0;
			while (!feof(fp))
				{
				n = fread(buff+2,1,450,fp);
				total += n;
				*((short *)buff) = n;
				if (n)
					if (!sendwithack(s,buff,n+2,&servaddr))
						{
						printf("Connection broken!\n");
						break;
						}
				printf("#");
				fflush(stdout);
				}
			printf(" %d bytes ok!\n",total);

			if (feof(fp))
				{
				*((int *)buff) = 0;
				sendwithack(s,buff,2,&servaddr);
				}

			fclose(fp);
			}
		else printf("File not found.\n");
		break;
		}

	so_close(s);

	so_exit();

	exit(0);
	}

