#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//add zyf
#define tftp_debug 1
#define ERROR -1
#define TIMEOUT 2000 /*amount of time to wait for an ACK/Data Packet in 1000microseconds 1000 = 1 second*/
#define MAXDATASIZE 1024 /* Maximum data size allowed */
int datasize = 512;
unsigned short int ackfreq = 1;
#define RETRIES 3 /* Number of times to resend a data OR ack packet beforing giving up */
#define DEFAULT_TFTP_HOST "192.168.1.140"
//end
/*
*This function is called when the client would like to download a file from the server.
*/
void
tget (char *pFilename, struct sockaddr_in server, char *pMode, int sock)
{
/* local variables */
int len, server_len, opcode, i, j, n, tid = 0, flag = 1;
unsigned short int count = 0, rcount = 0;
unsigned char filebuf[MAXDATASIZE + 1];
unsigned char packetbuf[MAXDATASIZE + 12];
extern int errno;
char filename[128], mode[12], *bufindex, ackbuf[512];
struct sockaddr_in data;
FILE *fp;/* pointer to the file we will be getting */
strcpy (filename, pFilename);//copy the pointer to the filename into a real array
strcpy (mode, pMode);//same as above
if (tftp_debug)
printf ("branched to file receive function\n");
fp = fopen (filename, "w");/* open the file for writing */
if (fp == NULL)
{//if the pointer is null then the file can't be opened - Bad perms
if (tftp_debug)
printf ("Client requested bad file: cannot open for writing (%s)\n",
filename);
return;
}
else/* File is open and ready to be written to */
{
if (tftp_debug)
printf ("Getting file... (destination: %s) \n", filename);
}
/* zero the buffer before we begin */
memset (filebuf, 0, sizeof (filebuf));
n = datasize + 4;
do
{
/* zero buffers so if there are any errors only NULLs will be exposed */
memset (packetbuf, 0, sizeof (packetbuf));
memset (ackbuf, 0, sizeof (ackbuf));
printf ("enter %d \n",count);
if (n !&#