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

📄 tftpcli.c

📁 在ARM7和UC/OSII的平台上实现了GPS自动报站的功能,涉及GPS模块LEA_4S的驱动,位置速寻算法,语音芯片ISD4004的录放音驱动,LED页面管理等等.从启动代码到操作系统的移植以及到业
💻 C
字号:
/* tftpcli.c

   Copyright 1998 by InterNiche Technologies Inc. All rights reserved.
   Copyright 1995 by NetPort Software.
   Copyright 1986 by Carnegie Mellon
   Copyright 1984 by the Massachusetts Institute of Technology

   TFTP client code - uses common code in tftputil.c and udp
interface in tftpudp.c

*/

#include "tftpport.h"

#ifdef TFTP_CLIENT

#include "tftp.h"


/* tftpuse() - Entry point for tftp transfers.

   Called from app to initiate a tftp transfer.

Retuns NULL if transer started without errors, else returns a pointer 
to text describing the error.
*/

char * 
tftpuse(
   ip_addr fhost,   /* address of foriegn tftp host */
   char *fname,   /* name of file on this machine */
   char *rmfile,   /* name of file on other machine */
   unsigned dir,   /* transfer direction, either PUT or GET */
   unsigned mode,   /* binary or ascii file transfer */
   int (*callback)(int, struct tfconn*, char*) )   /* callback */
{
struct tfconn * cn;

   cn = tfmkcn();
   if(cn == NULL)
      return("alloc failed");

   cn->tf_mode = mode;
   cn->tf_start = cticks;
   cn->tf_fhost = fhost;
   cn->tf_dir = dir;
   cn->tf_mode = mode;
   cn->tf_fport = TFTPPORT;
   cn->tf_lport = 0;          /* let tftp_udplisten() assign port */
   cn->tf_fhost = fhost;
   cn->callback = callback;

   /* Open client tftp connection. Note that foreign port is wildcard
   since server will select it's own when it replys */
   cn->tf_conn = tftp_udplisten(cn->tf_fhost, 0, &cn->tf_lport, 
                  (void*)cn);
   if(!cn->tf_conn)
      return("Couldn't open UDP connection");

   if(dir == GET) 
   {
      if(mode == ASCII)
         cn->tf_fd = vfopen(fname, "w");
      else if(mode == OCTET)
         cn->tf_fd = vfopen(fname, "wb");
      else 
         return("Invalid mode");

      if(cn->tf_fd == 0)
      {
         cn->tf_state = DEAD;
         return("Couldn't open local file");
      }
      if(tfsndreq(cn, rmfile) != 0)
         return "Initial request failed";
      cn->tf_expected = 1;
      cn->tf_state = DATAWAIT;
   }
   else if(dir == PUT)
   {
      if(mode == ASCII)
         cn->tf_fd = vfopen(fname, "r");
      else if(mode == OCTET)
         cn->tf_fd = vfopen(fname, "rb");
      else 
         return("Invalid mode");

      if(cn->tf_fd == 0)
         return("Couldn't open file to send");

      if(tfsndreq(cn, rmfile) != 0)
         return "Initial request failed";
      cn->tf_expected = 0;
   }
   else   /* not tftp PUT or GET? */
   {
      dtrap("tftpcli 1\n");   /* prog error */
      return ("bad direction parm");
   }
   return NULL;
}

/* Format up and send out an initial request for a tftp transfer. */

int
tfsndreq(struct tfconn * cn, char * fname)
{
int err;
int reqlen;    /* length of tftp request data */
TFTPBUF p;
struct tfreq *ptreq;

   reqlen = sizeof(struct tfreq) + strlen(fname);
   tftp_udpbuffer(cn, reqlen);
   if(cn->tf_outbuf.data == NULL)
      return ENP_NOBUFFER;
   p = &cn->tf_outbuf;
   ptreq = (struct tfreq *)p->data;
   strcpy((char*)ptreq->tf_name, fname);

   if(cn->tf_dir == GET) 
      ptreq->tf_op = TF_RRQ;
   else if(cn->tf_dir == PUT) 
      ptreq->tf_op = TF_WRQ;
   else 
   {
      dtrap("tftpcli 2\n");
      return FALSE;
   }

   if(cn->tf_mode == OCTET)
      strcpy((char*)ptreq->tf_name+strlen(fname)+1, "octet");
   else if(cn->tf_mode == ASCII)
      strcpy((char*)ptreq->tf_name+strlen(fname)+1, "netascii");
   else
   {
      dtrap("tftpcli 3\n");   /* bad mode; prog error */
      return FALSE;
   }
   err = tf_write(cn, reqlen);
   if(err == 0)
      cn->tf_state = ACKWAIT;
   else
   {
      dtrap("tftpcli 4\n");   /* Initial request failed? */
      tfkill(cn);
   }
   return err;
}


#endif   /* TFTP_CLIENT */

⌨️ 快捷键说明

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