www.pudn.com > Net_apps.rar > ftp.c


/* 
********************************************************************************************************* 
* Copyright 2003 Service & Quality Technology CO., LTD. ALL RIGHTS RESERVED. 
* This software is provided under license and contains proprietary and 
* confidential material which is the property of SQ tech. 
* 
* FileName     : ftp.c 
* Description  : ftp clinet : send jpg to ftp server 
* 
* 
* Version control: 
*  $Revision: 0.1 $    Date: 2004/04/07 12:00:00  gofly 
*      first implemetation 
* 
********************************************************************************************************* 
*/ 
#include "opt.h" 
 
 
#include "..\include\L3\net_apps\ftp\ftp.h" 
#if FTP_OPEN 
 
 
 
#define FTP_AUTO_RUN_TEST  0 
//TEMP_OS_EVENT  *OSSemCreate (INT16U cnt); 
 
//TEMP_OS_EVENT		*FtpEvenSem; 
OS_EVENT		*FtpEvenSem; 
//INT8U  huge      Jpg[262144];//256*1024 
/****************************************************************************/ 
/* FUNCTION NAME :   FtpThread 
/* DESCRIPTION   : 
/* in PARAMETERS : 
/* out PARAMETERS: 
/* RETURN VALUES : 
/* EXAMPLE	 : 
*****************************************************************************/ 
struct _TFtpStat     *debugFtpstat; 
void FtpThread (void *data) 
{ 
    struct _TFtpStat            Ftpstat; 
    INT8U                       ucErr; 
    debugFtpstat            =   &Ftpstat; 
    FtpEvenSem              =   (OS_EVENT*)OSSemCreate(0); //OSSemPost(FtpEvenSem); 
    Ftpstat.JpgFileCount    =   0; 
    Ftpstat.pJpg            =   NULL; 
   // Ftpstat.jpg_buf         =   NULL; 
    FtpLog (NULL, 0, IOINIT); 
    SHOW_EX1(FTP_DEBUG ,"FTP Client Start\n"); 
 
 
    while(1) 
    { 
        Ftpstat.stat      =    FTP_STOP; 
        #if !FTP_AUTO_RUN_TEST 
        OSSemPend(FtpEvenSem, 0, &ucErr); 
        FtpLog (NULL, 0, IOINIT); 
        #else           
        Ftpstat.pJpg      =       LogoImage + 74; 
        Ftpstat.TotalLen  =       sizeof(LogoImage) - 74; 
        #endif 
 
        OSTimeDlyHMSM(0, 0, 0, 20); 
        Ftpstat.stat      =    FTP_NEW; 
        Ftpstat.Socket1   =   INVALID_SOCKET; 
        Ftpstat.Socket2   =   INVALID_SOCKET; 
        Ftpstat.SendLen   =   0; 
        SHOW_EX1(FTP_DEBUG ,"FTP Client Event\n"); 
        FtpLog ("Start\n", 7, IOW); 
        while(1) 
        { 
 
            switch(Ftpstat.stat) 
            { 
                case FTP_NEW: 
                    FtpNew((void*)&Ftpstat); 
                    break; 
                case FTP_CONNECT: 
                    FtpConnect((void*)&Ftpstat); 
                    break; 
                case FTP_CLOSE: 
                   // ftp_close(); 
                    break; 
                case FTP_AUTH: 
                    FtpAuth((void*)&Ftpstat); 
                   // ftp_auth(); 
                    break; 
                case FTP_PORT_M: 
                    FtpPort((void*)&Ftpstat); 
                   // ftp_port_m(); 
                    break; 
                case FTP_PASV_M: 
                    FtpPasv((void*)&Ftpstat); 
                   // ftp_pasv_m(); 
                    break; 
 
            }/* switch(Ftpstat.stat)*/ 
            if(Ftpstat.stat == FTP_CLOSE) 
                break; 
        }/*while(1)*/ 
 
        if(Ftpstat.Socket1 != INVALID_SOCKET) close(Ftpstat.Socket1); 
        if(Ftpstat.Socket2 != INVALID_SOCKET) close(Ftpstat.Socket2); 
 
        Ftpstat.Socket1 = INVALID_SOCKET; 
        Ftpstat.Socket2 = INVALID_SOCKET; 
 
        if(Ftpstat.pJpg != NULL) 
            video_image_unlock(&Ftpstat.jpg_buf); 
 
        Ftpstat.pJpg          =   NULL; 
       // Ftpstat.jpg_buf       =   NULL; 
        Ftpstat.JpgFileCount++; 
 
    }/*while(1)*/ 
 
 
} 
/****************************************************************************/ 
/* FUNCTION NAME :   FtpConnect 
/* DESCRIPTION   : 
/* in PARAMETERS : 
/* out PARAMETERS: 
/* RETURN VALUES : 
/* EXAMPLE	 : 
*****************************************************************************/ 
 
void FtpConnect (void *data) 
{ 
    INT8U Msg[256]; 
    struct sockaddr_in                  S_ip_port_data; 
    TFtpStat                *Ftpstat    =    data; 
    S_ip_port_data.sin_port             =    htons(atoi(IoFtp("FTP_PORT", NULL, IOR, ROOT))); 
    S_ip_port_data.sin_addr.s_addr      =    Ftpstat->ServerIp; 
    Ftpstat->Socket1                    =    socket(0,SOCK_STREAM,0); 
 
    if(Ftpstat->Socket1 == INVALID_SOCKET) 
    { 
        Ftpstat->stat = FTP_CLOSE; 
        sprintf(Msg, "FTP  Create Socket1 ERR\n\x00"); 
        goto GOTOFtpConnectEnd; 
    } 
    if( -1  ==  connect(Ftpstat->Socket1, &S_ip_port_data , 0)) 
    { 
        Ftpstat->stat = FTP_CLOSE; 
        sprintf(Msg, "FTP  connect Socket1 ERR\n\x00"); 
        goto GOTOFtpConnectEnd; 
 
    } 
 
  //  Ftpstat->stat = FTP_CLOSE; 
    sprintf(Msg, "FTP  connect ftp server Socket1 ok\n\x00"); 
 
GOTOFtpConnectEnd: 
    FtpLog (Msg, strlen(Msg), IOW); 
    SHOW_EX1(FTP_DEBUG ,Msg); 
 
    if(FtpOrderRecv(data, 220)) 
    { 
       Ftpstat->stat    =   FTP_AUTH; 
    } 
          
       // test 
     //  Ftpstat->stat = FTP_CLOSE; 
} 
/****************************************************************************/ 
/* FUNCTION NAME :   FtpPasv 
/* DESCRIPTION   : 
/* in PARAMETERS : 
/* out PARAMETERS: 
/* RETURN VALUES : 
/* EXAMPLE	 : 
*****************************************************************************/ 
 
void FtpPasv (void *data) 
{ 
    struct sockaddr_in      PASV_ip_port_data; 
    INT8U                   Msg[256]; 
    TFtpStat               *Ftpstat         =    data; 
    INT8U                   pasvsock        =   INVALID_SOCKET; 
 
 
    sprintf(Msg,"PASV \x0d\x0a\x00"); 
    send(Ftpstat->Socket1 , Msg, strlen(Msg), 0); 
   // FtpLog (Msg, strlen(Msg), IOW); 
   // SHOW_EX1(FTP_DEBUG ,Msg); 
 
    if(!FtpOrderRecv(data, 227)){Msg[0] = 0x00;  goto GoToFtpPasvEnd;} 
 
 
 
    PASV_ip_port_data.sin_port          =    htons(Ftpstat->pasv_portmun); 
    PASV_ip_port_data.sin_addr.s_addr   =    Ftpstat->ServerIp; 
 
    pasvsock    =   socket(0,SOCK_STREAM,0); 
 
    if(pasvsock == INVALID_SOCKET) 
    { 
        sprintf(Msg, "Ftp Creat pasvsock ERR\x0d\x0a\x00"); 
        goto GoToFtpPasvEnd; 
    } 
 
    if( -1 == connect(pasvsock, &PASV_ip_port_data, 0)) 
    { 
        sprintf(Msg, "Ftp  pasvsock connect server ERR\x0d\x0a\x00"); 
        goto GoToFtpPasvEnd; 
 
    } 
 
    sprintf(Msg, "STOR SQ610%06d.jpg\x0d\x0a\x00", Ftpstat->JpgFileCount); 
  //  send(Ftpstat->Socket1 , Msg, strlen(Msg), 0); 
   // FtpLog (Msg, strlen(Msg), IOW); 
   // SHOW_EX1(FTP_DEBUG ,Msg); 
    if( FtpSend(Ftpstat->Socket1 , Msg, strlen(Msg))) 
    { 
        Ftpstat->stat = FTP_CLOSE; Msg[0] = 0x00;  goto GoToFtpPasvEnd; 
    } 
 
    if(!FtpOrderRecv(data, 150)){Msg[0] = 0x00;  goto GoToFtpPasvEnd;} 
 
    //   if( FtpSend(pasvsock , demojpg + 58, sizeof(demojpg) - 58)) 
    if( FtpSend(pasvsock , Ftpstat->pJpg, Ftpstat->TotalLen )) 
    { 
        Ftpstat->stat = FTP_CLOSE; Msg[0] = 0x00;  goto GoToFtpPasvEnd; 
    } 
 
 
 
      
   // send(pasvsock, demojpg + 58, sizeof(demojpg) - 58 ,0); 
 
    //while(socket_check_sendbuf(pasvsock) > 10) 
      //  OSTimeDlyHMSM(0, 0, 0, 10); 
 
    if(pasvsock       != INVALID_SOCKET) 
    { 
        close(pasvsock); 
        pasvsock = INVALID_SOCKET; 
    } 
    if(!FtpOrderRecv(data, 226)) {Msg[0] = 0x00;  goto GoToFtpPasvEnd;} 
 
 
    strcpy(Msg, "QUIT\x0d\x0a\x00"); 
    send(Ftpstat->Socket1 , Msg, strlen(Msg), 0); 
    //FtpLog (Msg, strlen(Msg), IOW); 
    //SHOW_EX1(FTP_DEBUG ,Msg); 
     
 
    if(!FtpOrderRecv(data, 221)) {Msg[0] = 0x00;  goto GoToFtpPasvEnd;} 
 
 
    strcpy(Msg, "FTP SUCCESS\x0d\x0a\x00"); 
 
 
 
 
GoToFtpPasvEnd: 
    if(pasvsock != INVALID_SOCKET) 
    { 
        close(pasvsock); 
        pasvsock       = INVALID_SOCKET; 
    } 
    FtpLog (Msg, strlen(Msg), IOW); 
    SHOW_EX1(FTP_DEBUG ,Msg); 
 
    Ftpstat->stat = FTP_CLOSE; 
 
 
} 
/****************************************************************************/ 
/* FUNCTION NAME :   FtpPort 
/* DESCRIPTION   : 
/* in PARAMETERS : 
/* out PARAMETERS: 
/* RETURN VALUES : 
/* EXAMPLE	 : 
*****************************************************************************/ 
 
void FtpPort (void *data) 
{ 
    TFtpStat           *Ftpstat                      =    data; 
    INT8U               Msg[256]; 
    struct   	 		sockaddr_in		cliaddr; 
    INT16U    	 		clilen					           =	0; 
    struct           sockaddr_in    S_ip_port_data; 
    INT16U                          clientopenport; 
    INT8U     newsocknum                             =   INVALID_SOCKET; 
    INT8U     clientopensocket                       =   INVALID_SOCKET; 
 
 
    clientopensocket                        =   socket(0,SOCK_STREAM,0); 
 
    if(clientopensocket == INVALID_SOCKET) 
    { 
      sprintf(Msg, "FTP prot creat clientopensocket err\x0d\x0a\x00"); 
      goto  GoToFtpPortEnd; 
    } 
    //---- gofly add test 
 
      /* 
         sprintf(Msg,"QUIT\x0d\x0a\x00"); 
        if( FtpSend(Ftpstat->Socket1 , Msg, strlen(Msg))); 
            SHOW_EX1(FTP_DEBUG ,Msg); 
   FtpLog (Msg, strlen(Msg), IOW); 
        if(!FtpOrderRecv(data, 221)) {Msg[0] = 0x00;  goto GoToFtpPortEnd;} 
      Msg[0] = 0x00;  goto GoToFtpPortEnd; 
 
           */ 
    //---------------------- 
 
    clientopenport                          =   tcp_new_port(); 
 
 
    S_ip_port_data.sin_family     	= AF_INET; 
    S_ip_port_data.sin_addr.s_addr 	= htonl(INADDR_ANY); 
    S_ip_port_data.sin_port 			= htons( clientopenport) ; 
 
    if(bind(clientopensocket,&S_ip_port_data,0) == INVALID_SOCKET) 
       goto  GoToFtpPortEnd; 
 
    if(listen(clientopensocket,1) == INVALID_SOCKET) 
       goto  GoToFtpPortEnd; 
 
    sprintf(Msg, "PORT %s,%d,%d\x0d\x0a\x00", FTPTuneip(IoIpAddress("IP", NULL, IOR, ROOT)), 
            clientopenport/256, clientopenport-256*(clientopenport/256) ); 
 
 
 
    if( FtpSend(Ftpstat->Socket1 , Msg, strlen(Msg))) 
    { 
        Ftpstat->stat = FTP_CLOSE; Msg[0] = 0x00;  goto GoToFtpPortEnd; 
    } 
 
 
 
 
    if(!FtpOrderRecv(data, 200)){Msg[0] = 0x00;  goto GoToFtpPortEnd;} 
 
    sprintf(Msg, "STOR SQ610%06d.jpg\x0d\x0a\x00", Ftpstat->JpgFileCount); 
 
    //SHOW_EX1(FTP_DEBUG ,Msg); 
    //FtpLog (Msg, strlen(Msg), IOW); 
    if( FtpSend(Ftpstat->Socket1 , Msg, strlen(Msg))) 
    { 
     	   Ftpstat->stat = FTP_CLOSE; Msg[0] = 0x00;  goto GoToFtpPortEnd; 
    } 
 //   send(Ftpstat->Socket1 , Msg, strlen(Msg), 0); 
 
 
 
 
         //---- wait ftp server connect not care ftp server err need modify 
 
    //newsocknum  = accept(clientopensocket, &S_ip_port_data, 0); 
 
 
 
    if( FtpAcceptRecv (clientopensocket) == R_OK) 
    { 
      newsocknum  = accept(clientopensocket, &S_ip_port_data, &clilen); 
      //newsocknum  =  accept(clientopensocket, &cliaddr, &clilen); 
 
    } 
 
 
    if(newsocknum == INVALID_SOCKET) 
    { 
      sprintf(Msg, "FTP prot creat newsocknum err\x0d\x0a\x00"); 
      goto  GoToFtpPortEnd; 
    } 
 
    if(!FtpOrderRecv(data, 150)) {Msg[0] = 0x00;  goto GoToFtpPortEnd;} 
 
    #if FTP_DEBUG 
    printf("QQQ\n"); 
    #endif 
   //  if( FtpSend(newsocknum , demojpg + 58, sizeof(demojpg) - 58)) 
     if( FtpSend(newsocknum , Ftpstat->pJpg, Ftpstat->TotalLen )) 
     { 
     Ftpstat->stat = FTP_CLOSE; Msg[0] = 0x00;  goto GoToFtpPortEnd; 
     }  //send(newsocknum,demojpg + 58, sizeof(demojpg) - 58 ,0); 
    // send(newsocknum, "HELLO", 5, 0); 
  //  while(socket_check_sendbuf(newsocknum)> 10) 
    //    OSTimeDlyHMSM(0, 0, 1, 0); 
    OSTimeDlyHMSM(0, 0, 0, 30); 
    #if FTP_DEBUG 
    printf("***\n"); 
    #endif 
//        OSTimeDlyHMSM(0, 0, 0, 300); 
    if(newsocknum       != INVALID_SOCKET) 
    { 
        close(newsocknum); 
        newsocknum       = INVALID_SOCKET; 
    } 
    if(!FtpOrderRecv(data, 226)) {Msg[0] = 0x00;  goto GoToFtpPortEnd;} 
 
 
    strcpy(Msg, "QUIT\x0d\x0a\x00"); 
    //SHOW_EX1(FTP_DEBUG ,Msg); 
    //FtpLog (Msg, strlen(Msg), IOW); 
    if( FtpSend(Ftpstat->Socket1 , Msg, strlen(Msg))) 
    { 
     Ftpstat->stat = FTP_CLOSE; Msg[0] = 0x00;  goto GoToFtpPortEnd; 
     } 
    //send(Ftpstat->Socket1 , Msg, strlen(Msg), 0); 
 
 
 
    if(!FtpOrderRecv(data, 221)) {Msg[0] = 0x00;  goto GoToFtpPortEnd;} 
 
 
    strcpy(Msg, "FTP SUCCESS\x0d\x0a\x00"); 
 
GoToFtpPortEnd: 
    FtpLog (Msg, strlen(Msg), IOW); 
    SHOW_EX1(FTP_DEBUG ,Msg); 
    if(clientopensocket != INVALID_SOCKET)   close(clientopensocket); 
    if(newsocknum       != INVALID_SOCKET)   close(newsocknum); 
    clientopensocket    =    INVALID_SOCKET; 
    newsocknum          =    INVALID_SOCKET; 
 
    Ftpstat->stat = FTP_CLOSE; 
 
 
 
} 
/****************************************************************************/ 
/* FUNCTION NAME :   FtpAuth 
/* DESCRIPTION   : 
/* in PARAMETERS : 
/* out PARAMETERS: 
/* RETURN VALUES : 
/* EXAMPLE	 : 
*****************************************************************************/ 
 
void FtpAuth (void *data) 
{ 
    TFtpStat           *Ftpstat    =    data; 
    INT8U               Msg[256]; 
 
    sprintf(Msg, "USER %s\x0d\x0a\x00", IoFtp("FTP_USER", NULL, IOR, ROOT) ); 
   // SHOW_EX1(FTP_DEBUG ,Msg); 
   // FtpLog (Msg, strlen(Msg), IOW); 
    if( FtpSend(Ftpstat->Socket1 , Msg, strlen(Msg))){Ftpstat->stat = FTP_CLOSE; return;} 
    //    send(Ftpstat->Socket1 , Msg, strlen(Msg), 0); 
 
 
 
    if(!FtpOrderRecv(data, 331)) return; 
 
    sprintf(Msg, "Pass %s\x0d\x0a\x00", IoFtp("GET_FTP_PASS", NULL, IOR, ROOT) ); 
    //SHOW_EX1(FTP_DEBUG ,Msg); 
    //FtpLog (Msg, strlen(Msg), IOW); 
    if( FtpSend(Ftpstat->Socket1 , Msg, strlen(Msg))){Ftpstat->stat = FTP_CLOSE; return;} 
   // send(Ftpstat->Socket1 , Msg, strlen(Msg), 0); 
 
    if(!FtpOrderRecv(data, 230)) return; 
 
 
    //sprintf(Msg,"CWD 610ftp\x0d\x0a\x00"); 
    //if( FtpSend(Ftpstat->Socket1 , Msg, strlen(Msg))) 
    //{ 
    // 	   Ftpstat->stat = FTP_CLOSE; return; 
    //} 
 
    //if(FtpOrderRecv(data, 250)) return; 
 
    sprintf(Msg, "TYPE I\x0d\x0a\x00"); 
    //SHOW_EX1(FTP_DEBUG ,Msg); 
    //FtpLog (Msg, strlen(Msg), IOW); 
    if( FtpSend(Ftpstat->Socket1 , Msg, strlen(Msg))){Ftpstat->stat = FTP_CLOSE; return;} 
    //    send(Ftpstat->Socket1 , Msg, strlen(Msg), 0); 
 
 
    if(!FtpOrderRecv(data, 200)) return; 
 
 
    if(!atoi(IoFtp("GET_FTP_MODE", NULL, IOR, ROOT))) // 0 port 1 pasv 
    { 
       Ftpstat->stat = FTP_PORT_M; 
    } 
    else 
    { 
       Ftpstat->stat = FTP_PASV_M; 
 
    } 
     //--- gofly test --------------------------------------------- 
   /* sprintf(Msg,"QUIT\x0d\x0a\x00"); 
    if( FtpSend(Ftpstat->Socket1 , Msg, strlen(Msg))); 
        SHOW_EX1(FTP_DEBUG ,Msg); 
 
    FtpLog (Msg, strlen(Msg), IOW); 
 
    if(!FtpOrderRecv(data, 221)); 
 
    Msg[0] = 0x00;   Ftpstat->stat = FTP_CLOSE;  */ 
      //------------------------------------------------------------- 
 
      //  Ftpstat->stat = FTP_CLOSE; 
 
} 
/****************************************************************************/ 
/* FUNCTION NAME :   FtpNew 
/* DESCRIPTION   : 
/* in PARAMETERS : 
/* out PARAMETERS: 
/* RETURN VALUES : 
/* EXAMPLE	 : 
*****************************************************************************/ 
 
void FtpNew (void *data) 
{ 
    TFtpStat           *Ftpstat    =    data; 
    INT8U               ip[4]; 
    INT8U               Msg[256]; 
    INT8U              *pFqdn; 
    INT32U             *pIpaddressStr; 
    INT8U               count; 
    struct  ip_addr     addr; 
    pFqdn = IoFtp("FTP_SERVER", NULL, IOR, ROOT); 
    memset(ip, 0x00,sizeof(ip)); 
    count = 0; 
 
    if(atoi(pFqdn) != 0) 
    { 
        inet_aton((const char*)pFqdn, &addr); 
        Ftpstat->ServerIp = addr.addr; 
        sprintf(Msg,"%s <= server\x0d\x0a\x00", pFqdn); 
        Ftpstat->stat = FTP_CONNECT; 
        goto GoToFtpNewEnd; 
    } 
 
    memset(G_dns_table,0x00,sizeof(G_dns_table));///< add by jjl 20050121 ---->修正 dns 不會更新 bug 
 
    while(1) 
    { 
        switch(dns_table(0, pFqdn , ip, DNS_LOOK_UP)) 
        { 
             case R_ER:  break; 
             case R_RU:  break; 
             case R_OK: 
                pIpaddressStr = ip; 
                Ftpstat->stat = FTP_CONNECT; 
                sprintf(Msg,"%s <= %s\n",inet_ntoa(*pIpaddressStr), pFqdn); 
                Ftpstat->ServerIp = *pIpaddressStr; 
                break; 
        } 
 
        count++ ; 
 
        if(count >= FTP_DNS_REQUEST_TIMEOUT) 
        { 
            Ftpstat->stat = FTP_CLOSE; 
            sprintf(Msg, "FTP DNS REQUEST TIMEOUT\n"); 
            break; 
 
        } 
 
        if(Ftpstat->stat == FTP_CONNECT && ip[0] == 0xff )// dns fail 
        { 
            Ftpstat->stat = FTP_CLOSE; 
            strcpy(Msg, "FTP DNS Fail\n"); 
            break; 
        } 
        if(Ftpstat->stat == FTP_CONNECT) 
        { 
            break; 
        } 
 
        OSTimeDlyHMSM(0, 0, 1, 0); 
    } 
GoToFtpNewEnd: 
   FtpLog (Msg, strlen(Msg), IOW); 
   SHOW_EX1(FTP_DEBUG ,Msg); 
 
} 
 
/****************************************************************************/ 
/* FUNCTION NAME :   FtpLog 
/* DESCRIPTION   : 
/* in PARAMETERS : 
/* out PARAMETERS: 
/* RETURN VALUES : 
/* EXAMPLE	 : 
*****************************************************************************/ 
 
void FtpLog (INT8U *data,INT32U Size,INT8U op) 
{ 
 static INT8U Log[1024]; 
 
 debugFtpstat->pLog = Log; 
 asm {PUSHF; CLI} 
 switch(op) 
 { 
    case IOR: 
        memset(data, 0x00, Size); 
        Size = min(Size, sizeof(Log)); 
        memcpy(data, Log,Size); 
 
        break; 
    case IOW: 
        if(Size  > sizeof(Log)) break; 
 
        if(Size + strlen(Log)  >=  sizeof(Log)) 
            memset(Log, 0x00, sizeof(Log)); 
        strcat(Log, data); 
 
        break; 
    case IOINIT: 
        memset(Log, 0x00, sizeof(Log)); 
        break; 
 } 
 
 
 asm  POPF 
} 
 
 
/****************************************************************************/ 
/* FUNCTION NAME :   FtpSend 
/* DESCRIPTION   : 
/* in PARAMETERS : 
/* out PARAMETERS: 
/* RETURN VALUES : 
/* EXAMPLE	 : 
*****************************************************************************/ 
INT8U FtpSend (INT8U Sock, INT8U huge *pFile, INT32U TotalLen) 
{ 
 
    fd_set                  writeset; 
    INT8U                   Msg[256]; 
    INT8U                   count; 
    INT8U                   maxfdp1; 
    INT32U                  SentLen; 
    //INT32U                  len; 
    INT32S                  len; 
    INT8U                   flag; 
 
    struct  timeval         timeout={FTP_ORDER_RECV_TIMEOUT, 0}; 
 
 
 
    maxfdp1 =   Sock + 1; 
    count   =   0; 
    SentLen =   0; 
    len     =   0; 
    flag    =   0; 
 
    memset(Msg,0x00,sizeof(Msg)); 
    if(TotalLen < 100) 
     strcpy(Msg, pFile); 
    else 
     sprintf(Msg,"send length  1024*%d+%d \n\x00",TotalLen/1024,TotalLen%1024 ); 
  /* 
    while(0) 
    { 
         len    =    min(TotalLen - SentLen,1460*2); 
         len    =    send(Sock , pFile + SentLen, len, 0); 
 
        if(len > 0) 
        { 
            SentLen += len; 
 
        } 
        else 
        { 
            sprintf(Msg + strlen(Msg), ":FTp SEND remote close\n\x00"); 
           // flag = 1;// remote close 
            goto GoToFtpSendEnd; 
        } 
 
        if(TotalLen  == SentLen) 
        { 
            sprintf(Msg+ strlen(Msg), ":FTp SEND ok\n\x00"); 
            flag = 0;//no err 
            goto GoToFtpSendEnd; 
 
        } 
        OSTimeDlyHMSM(0, 0, 0, 20); 
    } 
 
   */ 
//---------------------------- 
    while(1) 
    { 
        FD_ZERO(&writeset); 
        FD_SET(Sock, &writeset); 
        select(maxfdp1, 0, &writeset, 0,&timeout); 
        if(FD_ISSET(Sock, &writeset) ) 
        { 
            len    =   min(TotalLen - SentLen,1460*2); 
            len    =   send(Sock , pFile + SentLen, len, 0); 
            //----- gofly debug print size > 64 k 
          /*  if(TotalLen > 65536) 
            { 
 
                printf("%p\n", pFile + SentLen); 
 
            } */ 
            //--------------------------------------- 
 
            count  =   0; 
            if(len > 0) 
            { 
                SentLen += len; 
 
            } 
            else 
            { 
               sprintf(Msg, "FTp SEND remote close\n\x00"); 
               flag = 1;// remote close 
               goto GoToFtpSendEnd; 
            } 
 
            if(TotalLen  == SentLen) 
            { 
               sprintf(Msg, "FTp SEND ok\n\x00"); 
               flag = 0;//no err 
               goto GoToFtpSendEnd; 
 
            } 
 
 
        } 
        else 
        {//timeout 
            count++; 
            // timeout == 15 sec and send not ok 
            if(count >= 15/FTP_ORDER_RECV_TIMEOUT) 
            { 
               sprintf(Msg, "FTp SEND timeout\n\x00"); 
               flag = 2;// timeout 
               goto GoToFtpSendEnd; 
            } 
 
 
        } 
 
 
 
 
    }   /* while(1)*/ 
GoToFtpSendEnd: 
        FtpLog (Msg, strlen(Msg), IOW); 
        SHOW_EX1(FTP_DEBUG ,Msg); 
 
        return flag; // 0 ok 1 remoteclose 2 timeout 
 
} 
 
/****************************************************************************/ 
/* FUNCTION NAME :   FtpOrderRecv 
/* DESCRIPTION   : 
/* in PARAMETERS : 
/* out PARAMETERS: 
/* RETURN VALUES : 
/* EXAMPLE	 : 
*****************************************************************************/ 
 
INT8U FtpOrderRecv (void *data, INT32U op) 
{ 
    TFtpStat           *Ftpstat    =    data; 
    INT8U               Msg[256]; 
    fd_set              readset; 
    struct  timeval     timeout={FTP_ORDER_RECV_TIMEOUT,0}; 
    INT8U               maxfdp1; 
    INT32S              len; 
    INT8U               flag; 
    INT8U               count; 
    INT8U               i; 
    INT8U              *point; 
 
    if(Ftpstat->stat == FTP_CLOSE) return 0; 
    maxfdp1 =  Ftpstat->Socket1 + 1; 
 
    flag    =   0; 
    len     =   0; 
    count   =   0; 
    memset(Msg,0x00,sizeof(Msg)); 
 
 
 
    while(1) 
    { 
 
 
        FD_ZERO(&readset); 
        FD_SET(Ftpstat->Socket1, &readset); 
        select(maxfdp1, &readset, 0, 0,&timeout); 
        if(FD_ISSET(Ftpstat->Socket1, &readset) ) 
        { 
            len    =    recv(Ftpstat->Socket1 , Msg, sizeof(Msg), 0); 
            if(len > 0 && atoi(Msg) == op)flag++; 
            if(len <= 0) break; 
 
        } 
        else 
        { 
            if(flag) // timeout & recv ok 
            { 
               break; 
            } 
            else 
            { 
               count++; 
               // timeout == 15 sec and recv not ok 
               if(count >= 15/FTP_ORDER_RECV_TIMEOUT) 
               { 
                    break; 
               } 
 
            } 
 
        } 
 
    }  /* while(1)*/ 
 
 
       if(op == 227) 
       { 
            for(i=0, point = Msg; i < 4 && (point - Msg) < 50 ; point = strchr(point,0x2c) + 1,i++); 
                Ftpstat->pasv_portmun   =   atoi(point)*256; 
                point                   =   strchr(point,0x2c)+1; 
                Ftpstat->pasv_portmun  +=   atoi(point); 
 
 
       } 
 
       if(!flag) 
       { 
           sprintf(Msg + strlen(Msg), ":FTP   recv order %d ERR\n\x00", op); 
           Ftpstat->stat = FTP_CLOSE; 
       } 
       else 
       { 
 
        sprintf(Msg+strlen(Msg), ":FTP   recv order %d Ok\n\x00", op); 
 
       } 
 
        FtpLog (Msg, strlen(Msg), IOW); 
        SHOW_EX1(FTP_DEBUG ,Msg); 
 
    #if FTP_DEBUG 
    printf("FtpOrderRecv ==\n"); 
    #endif 
    return flag; 
 
 
} 
 
 
/****************************************************************************/ 
/* FUNCTION NAME :   FtpAcceptRecv 
/* DESCRIPTION   : 
/* in PARAMETERS : 
/* out PARAMETERS: 
/* RETURN VALUES : 
/* EXAMPLE	 : 
*****************************************************************************/ 
 
INT8U FtpAcceptRecv (INT8U sock) 
{ 
 
    INT8U               Msg[256]; 
    fd_set              readset; 
    struct  timeval     timeout={FTP_ORDER_RECV_TIMEOUT,0}; 
    INT8U               maxfdp1; 
    INT8U               count; 
 
 
    maxfdp1 =  sock + 1; 
 
 
    count   =   0; 
 
 
    while(1) 
    { 
 
 
        FD_ZERO(&readset); 
        FD_SET(sock, &readset); 
        select(maxfdp1, &readset, 0, 0,&timeout); 
 
        if(FD_ISSET(sock, &readset) ) 
        { 
           return R_OK;// 
 
        } 
        else 
        { 
 
               count++; 
               // timeout == 15 sec and recv not ok 
               if(count >= 15/FTP_ORDER_RECV_TIMEOUT) 
               { 
                    break; 
               } 
 
 
 
        } 
 
    }  /* while(1)*/ 
 
   return R_ER; 
 
 
} 
/****************************************************************************/ 
/* FUNCTION NAME :   FTPTuneip 
/* DESCRIPTION   :  192.168.3.108 => 192,168,3,108 
/* in PARAMETERS : 
/* out PARAMETERS: 
/* RETURN VALUES : 
/* EXAMPLE	 : 
*****************************************************************************/ 
 
INT8U *FTPTuneip(INT8U *p) 
{ 
    static INT8U ip[16]; 
    INT8U i,j; 
 
    strcpy(ip, p); 
 
    for(i = 0, j =0; j < 3 && i < sizeof(ip) ; i++) 
    { 
       if(ip[i] == '.') 
       { 
            ip[i]=','; 
            j++; 
       } 
 
    } 
 
 
    return ip; 
} 
 
/****************************************************************************/ 
/* FUNCTION NAME :   FtpApi 
/* DESCRIPTION   : 
/* in PARAMETERS : 
/* out PARAMETERS: 
/* RETURN VALUES : 
/* EXAMPLE	 : 
*****************************************************************************/ 
 
void FtpApi (void *data, void *jpg_buf) 
{ 
    if(jpg_buf == NULL) 
    { 
        if(data != NULL) 
        { 
            sprintf(data,"%d FTP EVEN DENY  : jpg_buf == NULL\x00",debugFtpstat->JpgFileCount); 
        } 
        return; 
    } 
    if(debugFtpstat->pJpg != NULL) 
    { 
        if(data != NULL) 
        { 
            sprintf(data,"%d FTP EVEN DENY  :Ftp RUNNING\x00",debugFtpstat->JpgFileCount); 
        } 
        return; 
    } 
   /* 
    video_image_lock((void*)&jpg_buf); 
    //debugFtpstat->jpg_buf  =  jpg_buf; 
    debugFtpstat->TotalLen = ((JPG_BUF*)jpg_buf)->jpg_size; 
    debugFtpstat->pJpg     = ((JPG_BUF*)jpg_buf)->jpg_file; 
   */ 
    debugFtpstat->jpg_buf.jpg_file         =  ((JPG_BUF*)(jpg_buf))->jpg_file; 
    debugFtpstat->jpg_buf.jpg_size         =  ((JPG_BUF*)(jpg_buf))->jpg_size; 
    debugFtpstat->jpg_buf.jpg_queue_num    =  ((JPG_BUF*)(jpg_buf))->jpg_queue_num; 
 
    debugFtpstat->TotalLen =  debugFtpstat->jpg_buf.jpg_size; 
    debugFtpstat->pJpg     =  (INT8U*)debugFtpstat->jpg_buf.jpg_file; 
 
    video_image_lock(&debugFtpstat->jpg_buf); 
 
    if(data != NULL) 
    { 
        sprintf(data, "%d FTP EVEN ALLOW\x00",debugFtpstat->JpgFileCount); 
    } 
 
    OSSemPost(FtpEvenSem); 
} 
 
#endif/*FTP_OPEN*/