⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tftpc.c

📁 TFTP server and client source code
💻 C
📖 第 1 页 / 共 2 页
字号:
	         ip_port(server); */	    }	  else	    {	      if (debug)		printf ("Client: Remote host successfully ACK'd (#%d)\n",			rcount);	      break;	    }	}			//end of else      if (debug)	printf ("Client: Ack(s) lost. Resending complete.\n");    }/* The ack sending 'for' loop ends here */  memset (filebuf, 0, sizeof (filebuf));	//clear the filebuf  while (1)			/* our break statement will escape us when we are done */    {      acked = 0;      ssize = fread (filebuf, 1, datasize, fp);      if (debug)	{	  printf	    ("The first data block has been read from the file and will be sent to the server\n");	  printf ("The size read from the file is: %d\n", ssize);	}      count++;			/* count number of datasize byte portions we read from the file */      if (count == 1)		/* we always look for an ack on the FIRST packet */	bcount = 0;      else if (count == 2)	/* The second packet will always start our counter at zreo. This special case needs to exist to avoid a DBZ when count = 2 - 2 = 0 */	bcount = 0;      else	bcount = (count - 2) % ackfreq;      sprintf ((char *) packetbuf[bcount], "%c%c%c%c", 0x00, 0x03, 0x00, 0x00);	/* build data packet but write out the count as zero */      memcpy ((char *) packetbuf[bcount] + 4, filebuf, ssize);      len = 4 + ssize;      packetbuf[bcount][2] = (count & 0xFF00) >> 8;	//fill in the count (top number first)      packetbuf[bcount][3] = (count & 0x00FF);	//fill in the lower part of the count      if (debug)	printf ("Client: Sending packet # %04d (length: %d file chunk: %d)\n",		count, len, ssize);      /* send the data packet */      if (sendto	  (sock, packetbuf[bcount], len, 0, (struct sockaddr *) &server,	   sizeof (server)) != len)	{	  if (debug)	    printf ("Client: Mismatch in number of sent bytes\n");	  return;	}      if (debug)	{	  ip_port (server);	  printf ("==count: %d  bcount: %d  ssize: %d  datasize: %d\n", count,		  bcount, ssize, datasize);	}      //if ((count - 1) == 0 || ((count - 1) % ackfreq) == 0 || ssize != datasize)      if (((count) % ackfreq) == 0 || ssize != datasize)	{	  if (debug)	    printf ("-- I will get an ACK\n");/* The following 'for' loop is used to recieve/timeout ACKs */	  for (j = 0; j < RETRIES; j++)	    {	      server_len = sizeof (ack);	      errno = EAGAIN;	      n = -1;	      for (i = 0; errno == EAGAIN && i <= TIMEOUT && n < 0; i++)		{		  n =		    recvfrom (sock, recvbuf, sizeof (recvbuf), MSG_DONTWAIT,			      (struct sockaddr *) &ack,			      (socklen_t *) & server_len);		  /* if (debug)		     ip_port(ack); */		  usleep (1000);		}	      if (n < 0 && errno != EAGAIN)		{		  if (debug)		    printf		      ("Client: could not receive from the server (errno: %d n: %d)\n",		       errno, n);		  //resend packet		}	      else if (n < 0 && errno == EAGAIN)		{		  if (debug)		    printf		      ("Client: Timeout waiting for ack (errno: %d n: %d)\n",		       errno, n);		  //resend packet		}	      else		{		/* checks to ensure send to ip is same from ACK IP */		  if (server.sin_addr.s_addr != ack.sin_addr.s_addr)		    {		      if (debug)			printf			  ("Client: Error recieving ACK (ACK from invalid address)\n");		      /* in this case someone else connected to our port. Ignore this fact and retry getting the ack */		      j--;		      continue;		    }		  if (tid != ntohs (server.sin_port))	/* checks to ensure get from the correct TID */		    {		      if (debug)			printf			  ("Client: Error recieving file (data from invalid tid)\n");		      len = err_packet (5, err_msg[5], buf);		      /* send the data packet */		      if (sendto			  (sock, buf, len, 0, (struct sockaddr *) &server,			   sizeof (server)) != len)			{			  printf			    ("Client: Mismatch in number of sent bytes while trying to send mode error packet\n");			}		      /*if (debug)		         ip_port(server);  */		      j--;		      continue;	/* we aren't going to let another connection spoil our first connection */		    }/* this formatting code is just like the code in the main function */		  bufindex = (char *) recvbuf;	//start our pointer going		  if (bufindex++[0] != 0x00)		    printf ("Client: bad first nullbyte!\n");		  opcode = *bufindex++;		  rcount = *bufindex++ << 8;		  rcount &= 0xff00;		  rcount += (*bufindex++ & 0x00ff);		  if (opcode != 4 || rcount != count)	/* ack packet should have code 4 (ack) and should be acking the packet we just sent */		    {		      if (debug)			printf			  ("Client: Remote host failed to ACK proper data packet # %d (got OP: %d Block: %d)\n",			   count, opcode, rcount);		      /* sending error message */		      if (opcode > 5)			{			  len = err_packet (4, err_msg[4], buf);			  if (sendto (sock, buf, len, 0, (struct sockaddr *) &server, sizeof (server)) != len)	/* send the data packet */			    {			      printf				("Client: Mismatch in number of sent bytes while trying to send mode error packet\n");			    }			  /*if (debug)			     ip_port(server); */			}		      /* from here we will loop back and resend */		    }		  else		    {		      if (debug)			printf			  ("Client: Remote host successfully ACK'd (#%d)\n",			   rcount);		      break;		    }		}	      for (i = 0; i <= bcount; i++)		{		  if (sendto (sock, packetbuf[i], len, 0, (struct sockaddr *) &server, sizeof (server)) != len)	/* resend the data packet */		    {		      if (debug)			printf ("Client: Mismatch in number of sent bytes\n");		      return;		    }		  if (debug)		    {		      printf ("Client: Ack(s) lost. Resending: %d\n",			      count - bcount + i);		      ip_port (server);		    }		}	      if (debug)		printf ("Client: Ack(s) lost. Resending complete.\n");	    }/* The ack sending 'for' loop ends here */	}      else if (debug)	{	  printf	    ("Client: Not attempting to recieve ack. Not required. count: %d\n",	     count);	  n = recvfrom (sock, recvbuf, sizeof (recvbuf), MSG_DONTWAIT, (struct sockaddr *) &ack, (socklen_t *) & server_len);	/* just do a quick check incase the remote host is trying with ackfreq = 1 */	  /*if (debug)	     ip_port(ack); */	}      if (j == RETRIES)	{	  if (debug)	    printf ("Client: Ack Timeout. Aborting transfer\n");	  fclose (fp);	  return;	}      if (ssize != datasize)	break;      memset (filebuf, 0, sizeof (filebuf));	/* fill the filebuf with zeros so that when the fread fills it, it is a null terminated string */    }  fclose (fp);  if (debug)    printf ("Client: File sent successfully\n");  return;}				//end of tsend function/**This function is called when the client would like to download a file from the server.*/voidtget (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 (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 (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 (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));      if (n != (datasize + 4))	/* remember if our datasize is less than a full packet this was the last packet to be received */	{	  if (debug)	    printf	      ("Last chunk detected (file chunk size: %d). exiting while loop\n",	       n - 4);	  len = sprintf (ackbuf, "%c%c%c%c", 0x00, 0x04, 0x00, 0x00);	  ackbuf[2] = (count & 0xFF00) >> 8;	//fill in the count (top number first)	  ackbuf[3] = (count & 0x00FF);	//fill in the lower part of the count	  if (debug)	    printf ("Sending ack # %04d (length: %d)\n", count, len);	  if (sendto	      (sock, ackbuf, len, 0, (struct sockaddr *) &server,	       sizeof (server)) != len)	    {	      if (debug)		printf ("Mismatch in number of sent bytes\n");	      return;	    }	  if (debug)	    printf ("The Client has sent an ACK for packet\n");	  goto done;		/* gotos are not optimal, but a good solution when exiting a multi-layer loop */	}      count++;      for (j = 0; j < RETRIES; j++)	/* this allows us to loop until we either break out by getting the correct ack OR time out because we've looped more than RETRIES times */	{	  server_len = sizeof (data);	  errno = EAGAIN;	/* this allows us to enter the loop */	  n = -1;	  for (i = 0; errno == EAGAIN && i <= TIMEOUT && n < 0; i++)	/* this for loop will just keep checking the non-blocking socket until timeout */	    {	      n =		recvfrom (sock, packetbuf, sizeof (packetbuf) - 1,			  MSG_DONTWAIT, (struct sockaddr *) &data,			  (socklen_t *) & server_len);	      usleep (1000);	    }	  /*if(debug)	     ip_port (data);  print the vlaue recived from the server */	  if (!tid)	    {	      tid = ntohs (data.sin_port);	//get the tid of the server.	      server.sin_port = htons (tid);	//set the tid for rest of the transfer 	    }	  if (n < 0 && errno != EAGAIN)	/* this will be true when there is an error that isn't the WOULD BLOCK error */	    {	      if (debug)		printf		  ("The server could not receive from the client (errno: %d n: %d)\n",		   errno, n);	      //resend packet	    }	  else if (n < 0 && errno == EAGAIN)	/* this is true when the error IS would block. This means we timed out */	    {	      if (debug)		printf ("Timeout waiting for data (errno: %d == %d n: %d)\n",			errno, EAGAIN, n);	      //resend packet	    }	  else	    {	      if (server.sin_addr.s_addr != data.sin_addr.s_addr)	/* checks to ensure get from ip is same from ACK IP */		{		  if (debug)		    printf		      ("Error recieving file (data from invalid address)\n");		  j--;		  continue;	/* we aren't going to let another connection spoil our first connection */		}	      if (tid != ntohs (server.sin_port))	/* checks to ensure get from the correct TID */		{		  if (debug)		    printf ("Error recieving file (data from invalid tid)\n");		  len = sprintf ((char *) packetbuf,				 "%c%c%c%cBad/Unknown TID%c",				 0x00, 0x05, 0x00, 0x05, 0x00);		  if (sendto (sock, packetbuf, len, 0, (struct sockaddr *) &server, sizeof (server)) != len)	/* send the data packet */		    {		      printf			("Mismatch in number of sent bytes while trying to send mode error packet\n");		    }		  j--;		  continue;	/* we aren't going to let another connection spoil our first connection */		}/* this formatting code is just like the code in the main function */	      bufindex = (char *) packetbuf;	//start our pointer going	      if (bufindex++[0] != 0x00)		printf ("bad first nullbyte!\n");	      opcode = *bufindex++;	      rcount = *bufindex++ << 8;	      rcount &= 0xff00;	      rcount += (*bufindex++ & 0x00ff);	      memcpy ((char *) filebuf, bufindex, n - 4);	/* copy the rest of the packet (data portion) into our data array */	      if (debug)		printf		  ("Remote host sent data packet #%d (Opcode: %d packetsize: %d filechunksize: %d)\n",		   rcount, opcode, n, n - 4);	      if (flag)		{		  if (n > 516)		    datasize = n - 4;		  else if (n < 516)		    datasize = 512;		  flag = 0;		}	      if (opcode != 3)	/* ack packet should have code 3 (data) and should be ack+1 the packet we just sent */		{		  if (debug)		    printf		      ("Badly ordered/invalid data packet (Got OP: %d Block: %d) (Wanted Op: 3 Block: %d)\n",		       opcode, rcount, count);/* sending error message */		  if (opcode > 5)		    {		      len = sprintf ((char *) packetbuf,				     "%c%c%c%cIllegal operation%c",				     0x00, 0x05, 0x00, 0x04, 0x00);		      if (sendto (sock, packetbuf, len, 0, (struct sockaddr *) &server, sizeof (server)) != len)	/* send the data packet */			{			  printf			    ("Mismatch in number of sent bytes while trying to send mode error packet\n");			}		    }		}	      else		{		  len = sprintf (ackbuf, "%c%c%c%c", 0x00, 0x04, 0x00, 0x00);		  ackbuf[2] = (count & 0xFF00) >> 8;	//fill in the count (top number first)		  ackbuf[3] = (count & 0x00FF);	//fill in the lower part of the count		  if (debug)		    printf ("Sending ack # %04d (length: %d)\n", count, len);		  if (((count - 1) % ackfreq) == 0)		    {		      if (sendto			  (sock, ackbuf, len, 0, (struct sockaddr *) &server,			   sizeof (server)) != len)			{			  if (debug)			    printf ("Mismatch in number of sent bytes\n");			  return;			}		      if (debug)			printf ("The client has sent an ACK for packet %d\n",				count);		    }		//check for ackfreq		  else if (count == 1)		    {		      if (sendto			  (sock, ackbuf, len, 0, (struct sockaddr *) &server,			   sizeof (server)) != len)			{			  if (debug)			    printf ("Mismatch in number of sent bytes\n");			  return;			}		      if (debug)			printf ("The Client has sent an ACK for packet 1\n");		    }		  break;		}		//end of else	    }	}      if (j == RETRIES)	{	  if (debug)	    printf ("Data recieve Timeout. Aborting transfer\n");	  fclose (fp);	  return;	}    }  while (fwrite (filebuf, 1, n - 4, fp) == n - 4);	/* if it doesn't write the file the length of the packet received less 4 then it didn't work */  fclose (fp);  sync ();  if (debug)    printf      ("fclose and sync unsuccessful. File failed to recieve properly\n");  return;done:  fclose (fp);  sync ();  if (debug)    printf ("fclose and sync successful. File received successfully\n");  return;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -