www.pudn.com > Net_apps.rar > smtp.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     : smtp.c 
* Description  : smtp clinet : send jpg to smtp server 
* 
* 
* Version control: 
*  $Revision: 0.1 $    Date: 2004/04/16 12:00:00  gofly 
*      first implemetation 
* 
********************************************************************************************************* 
*/ 
#include "opt.h" 
#if SMTP_OPEN 
 
#include "..\include\L3\net_apps\smtp\smtp.h" 
#include "..\include\L3\net_apps\smtp\smtpdata.h" 
#include "..\include\L3\net_apps\str_function.h" 
#include "..\include\L2\system\timer.h" 
//#include "..\net\api\http_api_video.h" 
 
#define SMTP_AUTO_RUN_TEST  0 
 
char        huge         G_smtp_temp_buf[2048];//2k 
//OS_EVENT		        *SmtpEvenSem; 
struct _TSmtpStat       *debugSmtpstat; 
extern   INT8U  SmtpAuthflag; 
INT8U       pppoeip=0; 
extern INT8U PPPoE_Status; 
/****************************************************************************/ 
/* FUNCTION NAME :   SmtpThread 
/* DESCRIPTION   : 
/* in PARAMETERS : 
/* out PARAMETERS: 
/* RETURN VALUES : 
/* EXAMPLE	 : 
*****************************************************************************/ 
void SmtpThread (void *data) 
{ 
    extern OS_EVENT * PPPoeMbox; 
    struct _TSmtpStat            Smtpstat; 
    INT8U                        ucErr; 
    INT8U  *msg; 
    debugSmtpstat               =    &Smtpstat; 
   // Smtpstat.jpg_buf.jpg_file   =    NULL; 
    //SmtpEvenSem                 =    (OS_EVENT*)OSSemCreate(0); 
    Smtpstat.JpgFileCount       =     0; 
    SmtpLog (NULL, 0, IOINIT); 
    SHOW_EX1(SMTP_DEBUG ,"SMTP Client Start\n"); 
    Smtpstat.pJpg               =      NULL; 
    smtp_replace_law(NULL,NULL, IOINIT , 0); 
 
    while(1) 
    { 
        Smtpstat.stat      =    SMTP_STOP; 
        ucErr =  SmtpAuthflag; 
 
        #if !SMTP_AUTO_RUN_TEST 
        //OSSemPend(SmtpEvenSem, 0, &ucErr); 
        msg = (INT8U *)OSMboxPend(PPPoeMbox,0,&ucErr); 
        if (strcmp(msg,"PPPoe Ok")==0) 
        { 
            pppoeip = 1; 
            OSTimeDlyHMSM(0,0,5,0); 
        } 
        SmtpLog (NULL, 0, IOINIT); 
        #else 
        Smtpstat.pJpg       =       LogoImage + 74; 
        Smtpstat.TotalLen   =       sizeof(LogoImage) - 74; 
       
        #endif 
 
 
        Smtpstat.stat      =    SMTP_NEW; 
        Smtpstat.Socket    =   INVALID_SOCKET; 
        Smtpstat.SendLen   =   0; 
        SHOW_EX1(SMTP_DEBUG ,"SMTP Client Event\n"); 
        SmtpLog ("Start\x0d\x0a", 7, IOW); 
 
        while(2) 
        { 
            switch(Smtpstat.stat) 
            { 
                case SMTP_NEW: 
                    SmtpNew((void*)&Smtpstat); 
                    break; 
                case SMTP_CONNECT: 
                    SmtpConnect((void*)&Smtpstat); 
                    break; 
                case SMTP_HELLO: 
                    SmtpHello((void*)&Smtpstat); 
                    break; 
                case SMTP_AUTH: 
                    SmtpAuth((void*)&Smtpstat); 
                    break; 
                case SMTP_MAIL_FROM_RCPT: 
                    SmtpMailFromRcpt((void*)&Smtpstat); 
                    break; 
                case SMTP_DATA: 
                    SmtpData((void*)&Smtpstat); 
                    break; 
                case SMTP_QUIT: 
                    SmtpQuit((void*)&Smtpstat); 
                    break; 
 
 
 
 
            }/*switch(Smtpstat.stat)*/ 
 
 
 
            if(Smtpstat.stat == SMTP_CLOSE) 
             break; 
 
 
        } /*  while(2)*/ 
 
 
        if(Smtpstat.Socket != INVALID_SOCKET) 
            close(Smtpstat.Socket ); 
 
        Smtpstat.Socket  = INVALID_SOCKET; 
        if(Smtpstat.jpg_buf.jpg_file != NULL) 
        { 
            //printf("@F %d\n",video_image_unlock(Smtpstat.jpg_buf)); 
            /* 
            if( *( debugSmtpstat->pJpg +  debugSmtpstat->TotalLen  - 1 )   != 0xd9) 
            { 
 
                  *( debugSmtpstat->pJpg +  debugSmtpstat->TotalLen  - 1 ) = 0xd9; 
            } 
 
            */ 
                    //--- gofly debug 
            //printf("unlock %p %p %d\n",Smtpstat.jpg_buf,Smtpstat.jpg_buf->jpg_file ,Smtpstat.jpg_buf->jpg_size  ); 
            video_image_unlock(&Smtpstat.jpg_buf); 
 
        } 
 
     //   Smtpstat.jpg_buf.jpg_file   =   NULL; 
        Smtpstat.pJpg               = NULL; 
        Smtpstat.JpgFileCount++; 
 
    } /*  while(1)*/ 
 
 
 
} 
/****************************************************************************/ 
/* FUNCTION NAME :   SmtpQuit 
/* DESCRIPTION   : 
/* in PARAMETERS : 
/* out PARAMETERS: 
/* RETURN VALUES : 
/* EXAMPLE	 : 
*****************************************************************************/ 
 
void SmtpQuit (void *data) 
{ 
    INT8U Msg[256]; 
    TSmtpStat           *Smtpstat    =    data; 
 
    sprintf(Msg,"QUIT\x0d\x0a\x00"); 
 
    if(SmtpSend (Smtpstat->Socket , Msg, strlen(Msg))) 
    {//-- send fail 
        goto GoToSmtpQuitEnd; 
    } 
 
    if(!Smtp_okay(data, 221)) 
    {  //--- recv fail 
        goto GoToSmtpQuitEnd; 
    } 
 
    SHOW_EX1(SMTP_DEBUG ,"SMTP SUCCESS\n"); 
    SmtpLog ("SMTP SUCCESS\x0d\x0a", 14, IOW); 
 
    GoToSmtpQuitEnd: 
    Smtpstat->stat = SMTP_CLOSE; 
 
} 
/****************************************************************************/ 
/* FUNCTION NAME :   SmtpData 
/* DESCRIPTION   : 
/* in PARAMETERS : 
/* out PARAMETERS: 
/* RETURN VALUES : 
/* EXAMPLE	 : 
*****************************************************************************/ 
 
void SmtpData (void *data) 
{ 
    INT8U Msg[256]; 
    TSmtpStat           *Smtpstat    =    data; 
 
    sprintf(Msg,"DATA\x0d\x0a\x00"); 
 
    if(SmtpSend (Smtpstat->Socket , Msg, strlen(Msg))) 
    {//-- send fail 
        goto GoToSmtpDataEnd; 
    } 
 
    if(!Smtp_okay(data, 354)) 
    {  //--- recv fail 
        goto GoToSmtpDataEnd; 
    } 
///------------------------------------------------------------------- 
 
 
 
    //----------------- 傳送data出問題 ---------------------// 
    //---------------- 測試傳小資料量是否 ok ---------------// 
//#if 0 
if (pppoeip == 1) 
    { 
    sprintf(Msg,"SUBJECT:PPPOE IP\x0d\x0a%s\x0d\x0a"".""\x0d\x0a\0x00",IoIpAddress("IP",NULL,IOR,ROOT)); 
    pppoeip = 0; 
    SmtpSend (Smtpstat->Socket , Msg, strlen(Msg)); 
    } 
else 
//#else 
    { 
    /* 
    if(SmtpSend (Smtpstat->Socket , G_smtp_temp_buf, MakeSendData ())) 
    {//-- send fail 
        goto GoToSmtpDataEnd; 
    } 
    */ 
 
 
    //  (1)  send head 
    if(SmtpSend (Smtpstat->Socket , G_smtp_temp_buf, MakeSendHead ())) 
    {//-- send fail 
        goto GoToSmtpOkEnd; 
    } 
   // OSTimeDlyHMSM(0, 0, 1, 0); 
    //  (2) send body 
    /* 
    memset(G_smtp_temp_buf,0xff,sizeof(G_smtp_temp_buf)); 
    if(SmtpSend (Smtpstat->Socket , (const INT8U*)G_smtp_temp_buf, sizeof(G_smtp_temp_buf))) 
    {//-- send fail 
        goto GoToSmtpDataEnd; 
    } 
     */ 
   /* 
    if(SmtpSend (Smtpstat->Socket , (const INT8U*)debugSmtpstat->pJpg , debugSmtpstat->TotalLen)) 
    {//-- send fail 
        goto GoToSmtpDataEnd; 
    } 
    */ 
    //------------------ 送假圖 -----------------------// 
    #if 1 
    if(Smtp_Base64Encode_Send((INT8U * const)G_smtp_temp_buf , 
        (const INT8U*)debugSmtpstat->pJpg ,debugSmtpstat->TotalLen)) 
    {//-- send fail 
        goto GoToSmtpDataEnd; 
    } 
    #else 
    if(Smtp_Base64Encode_Send((INT8U * const)G_smtp_temp_buf , 
        (const INT8U*)demojpg + 58 ,sizeof(demojpg) - 58)) 
    {//-- send fail 
        goto GoToSmtpDataEnd; 
 
    } 
    #endif 
    //OSTimeDlyHMSM(0, 0, 1, 0); 
    //  (3)  send end 
    if(SmtpSend (Smtpstat->Socket , smtp_text_end, strlen(smtp_text_end))) 
    {//-- send fail 
        goto GoToSmtpDataEnd; 
    } 
//#endif 
    } 
 
     
GoToSmtpOkEnd: 
    if(!Smtp_okay(data, 250)) 
    {  //--- recv fail 
        goto GoToSmtpDataEnd; 
    } 
 
    Smtpstat->stat = SMTP_QUIT; 
    return; 
 
GoToSmtpDataEnd: 
    Smtpstat->stat = SMTP_CLOSE; 
 
} 
/****************************************************************************/ 
/* FUNCTION NAME :   SmtpMailFromRcpt 
/* DESCRIPTION   : 
/* in PARAMETERS : 
/* out PARAMETERS: 
/* RETURN VALUES : 
/* EXAMPLE	 : 
*****************************************************************************/ 
 
void SmtpMailFromRcpt (void *data) 
{ 
    INT8U Msg[256]; 
    TSmtpStat           *Smtpstat    =    data; 
 
 
    sprintf(Msg,"MAIL FROM:<%s>\x0d\x0a\x00", IoSmtp("SMTP_MAIL_FROM", NULL, IOR, ROOT)); 
    if(SmtpSend (Smtpstat->Socket , Msg, strlen(Msg))) 
    {//-- send fail 
        goto GoToSmtpMailFromRcptEnd; 
    } 
 
    if(!Smtp_okay(data, 250)) 
    {  //--- recv fail 
        goto GoToSmtpMailFromRcptEnd; 
    } 
 
 
    sprintf(Msg,"RCPT TO:<%s>\x0d\x0a\x00", IoSmtp("SMTP_RCPT_TO", NULL, IOR, ROOT)); 
    if(SmtpSend (Smtpstat->Socket , Msg, strlen(Msg))) 
    {//-- send fail 
        goto GoToSmtpMailFromRcptEnd; 
    } 
 
    if(!Smtp_okay(data, 250)) 
    {  //--- recv fail 
        goto GoToSmtpMailFromRcptEnd; 
    } 
 
    Smtpstat->stat = SMTP_DATA; 
    return; 
 
GoToSmtpMailFromRcptEnd: 
    Smtpstat->stat = SMTP_CLOSE; 
 
} 
/****************************************************************************/ 
/* FUNCTION NAME :   SmtpHello 
/* DESCRIPTION   : 
/* in PARAMETERS : 
/* out PARAMETERS: 
/* RETURN VALUES : 
/* EXAMPLE	 : 
*****************************************************************************/ 
 
void SmtpHello (void *data) 
{ 
    INT8U Msg[256]; 
    TSmtpStat           *Smtpstat    =    data; 
 
 
    if( SmtpAuthflag) 
    { 
        sprintf(Msg,"EHLO %s\x0d\x0a\x00", IoSmtp("SMTP_SERVER", NULL, IOR, ROOT)); 
    } 
    else 
    { 
        sprintf(Msg,"HELO %s\x0d\x0a\x00", IoSmtp("SMTP_SERVER", NULL, IOR, ROOT)); 
    } 
 
 
    if(SmtpSend (Smtpstat->Socket , Msg, strlen(Msg))) 
    {//-- send fail 
        goto GoToSmtpHelloEnd; 
    } 
 
    if(Smtp_okay(data, 250)) 
    { 
        if(SmtpAuthflag) 
        { 
            Smtpstat->stat    =   Smtpstat->stat+1; 
        } 
        else 
        { 
            Smtpstat->stat    =   Smtpstat->stat+2; 
        } 
       return; 
    } 
 
    GoToSmtpHelloEnd: 
    Smtpstat->stat = SMTP_QUIT; 
 
} 
/****************************************************************************/ 
/* FUNCTION NAME :   SmtpAuthLogin 
/* DESCRIPTION   : 
/* in PARAMETERS : 
/* out PARAMETERS: 
/* RETURN VALUES : 
/* EXAMPLE	 : 
*****************************************************************************/ 
 
void SmtpAuth (void *data) 
{ 
    INT8U Msg[256]; 
    TSmtpStat           *Smtpstat    =    data; 
 
    sprintf(Msg,"AUTH LOGIN\x0d\x0a\x00"); 
 
    if(SmtpSend (Smtpstat->Socket , Msg, strlen(Msg))) 
    {//-- send fail 
        goto GoToSmtpHelloEnd; 
    } 
 
    if(!Smtp_okay(data, 334)) 
    { 
        goto GoToSmtpHelloEnd; 
    } 
 
    Base64Encode((INT8U * const)(Msg), 
            (const INT8U*) IoSmtp("SMTP_USER", NULL, IOR, ROOT) , 
             (INT32U)strlen(IoSmtp("SMTP_USER", NULL, IOR, ROOT))); 
 
    strcat(Msg,"\x0d\x0a\x00"); 
 
    if(SmtpSend (Smtpstat->Socket , Msg, strlen(Msg))) 
    {//-- send fail 
        goto GoToSmtpHelloEnd; 
    } 
 
    if(!Smtp_okay(data, 334)) 
    { 
        goto GoToSmtpHelloEnd; 
} 
 
    Base64Encode((INT8U * const)(Msg), 
            (const INT8U*) IoSmtp("GET_SMTP_PASS", NULL, IOR, ROOT) , 
             (INT32U)strlen(IoSmtp("GET_SMTP_PASS", NULL, IOR, ROOT))); 
 
 
    strcat(Msg,"\x0d\x0a\x00"); 
    if(SmtpSend (Smtpstat->Socket , Msg, strlen(Msg))) 
    {//-- send fail 
        goto GoToSmtpHelloEnd; 
    } 
 
    if(!Smtp_okay(data, 235)) 
    { 
        goto GoToSmtpHelloEnd; 
    } 
 
       Smtpstat->stat    =   SMTP_MAIL_FROM_RCPT; 
       return; 
 
    GoToSmtpHelloEnd: 
 
    Smtpstat->stat    =   SMTP_CLOSE; 
} 
 
 
/****************************************************************************/ 
/* FUNCTION NAME :   SmtpConnect 
/* DESCRIPTION   : 
/* in PARAMETERS : 
/* out PARAMETERS: 
/* RETURN VALUES : 
/* EXAMPLE	 : 
*****************************************************************************/ 
 
void SmtpConnect (void *data) 
{ 
    INT8U Msg[256]; 
    struct sockaddr_in                  S_ip_port_data; 
    TSmtpStat           *Smtpstat    =    data; 
    S_ip_port_data.sin_port          =    htons(25); 
    S_ip_port_data.sin_addr.s_addr   =    Smtpstat->ServerIp; 
    Smtpstat->Socket                 =    socket(0,SOCK_STREAM,0); 
 
    if(Smtpstat->Socket == INVALID_SOCKET) 
    { 
        Smtpstat->stat = SMTP_CLOSE; 
        sprintf(Msg, "SMTP  Create Socket ERR\n\x00"); 
        goto GOTOSmtpConnectEnd; 
    } 
    if( -1  ==  connect(Smtpstat->Socket, &S_ip_port_data , 0)) 
    { 
        Smtpstat->stat = SMTP_CLOSE; 
        sprintf(Msg, "SMTP  connect Socket ERR\n\x00"); 
        goto GOTOSmtpConnectEnd; 
 
    } 
 
 
    sprintf(Msg, "SMTP  connect Smtp server Socket ok\n\x00"); 
 
    GOTOSmtpConnectEnd: 
    SmtpLog (Msg, strlen(Msg), IOW); 
    SHOW_EX1(SMTP_DEBUG ,Msg); 
 
    if(Smtp_okay(data, 220)) 
    { 
       Smtpstat->stat    =   SMTP_HELLO; 
    } 
 
} 
/****************************************************************************/ 
/* FUNCTION NAME :   SmtpNew 
/* DESCRIPTION   : 
/* in PARAMETERS : 
/* out PARAMETERS: 
/* RETURN VALUES : 
/* EXAMPLE	 : 
*****************************************************************************/ 
 
void SmtpNew (void *data) 
{ 
    TSmtpStat           *Smtpstat    =    data; 
    INT8U               ip[4]; 
    INT8U               Msg[256]; 
    INT8U              *pFqdn; 
    INT32U             *pIpaddressStr; 
    INT8U               count; 
    struct  ip_addr     addr; 
    pFqdn = IoSmtp("SMTP_SERVER", NULL, IOR, ROOT); 
    memset(ip, 0x00,sizeof(ip)); 
    count = 0; 
 
    if(atoi(pFqdn) != 0) 
    { 
        inet_aton((const char*)pFqdn, &addr); 
        Smtpstat->ServerIp = addr.addr; 
        sprintf(Msg,"%s <= server\x0d\x0a\x00", pFqdn); 
        Smtpstat->stat = SMTP_CONNECT; 
        goto GoToSmtpNewEnd; 
    } 
 
    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; 
                Smtpstat->stat = SMTP_CONNECT; 
                sprintf(Msg,"%s <= %s\n",inet_ntoa(*pIpaddressStr), pFqdn); 
                Smtpstat->ServerIp = *pIpaddressStr; 
                break; 
        } 
 
        count++ ; 
 
        if(count >= SMTP_DNS_REQUEST_TIMEOUT) 
        { 
            Smtpstat->stat = SMTP_CLOSE; 
            sprintf(Msg, "SMTP DNS REQUEST TIMEOUT\n"); 
            break; 
 
        } 
 
        if(Smtpstat->stat == SMTP_CONNECT && ip[0] == 0xff )// dns fail 
        { 
            Smtpstat->stat = SMTP_CLOSE; 
            strcpy(Msg, "SMTP DNS Fail\n"); 
            break; 
        } 
        if(Smtpstat->stat == SMTP_CONNECT) 
        { 
            break; 
        } 
 
        OSTimeDlyHMSM(0, 0, 1, 0); 
    } 
    GoToSmtpNewEnd: 
    SmtpLog (Msg, strlen(Msg), IOW); 
    SHOW_EX1(SMTP_DEBUG ,Msg); 
 
} 
 
/****************************************************************************/ 
/* FUNCTION NAME :   SmtpLog 
/* DESCRIPTION   :   record log, 
/* in PARAMETERS : 
/* out PARAMETERS: 
/* RETURN VALUES : 
/* EXAMPLE	 : 
*****************************************************************************/ 
#define SMTP_LOG_SIZE       1024 
#define SMTP_LOG_LAST_SIZE  256 
void SmtpLog (INT8U *data,INT32U Size,INT8U op) 
{ 
 static INT8U Log[SMTP_LOG_SIZE]; 
 static INT8U Last[SMTP_LOG_LAST_SIZE]; 
 static INT8U count; 
 INT32U IorSize; 
 debugSmtpstat->pLog = Log; 
 
 asm {PUSHF; CLI} 
 switch(op) 
 { 
    case IOR: 
        memset(data, 0x00, Size); 
        IorSize = min(Size, SMTP_LOG_SIZE); 
        memcpy(data, Log, IorSize); 
        if(count != 0 && strlen(data) + 40 < Size) 
        { 
          sprintf(data + strlen(data),"last message repeated %d times\n",count); 
        } 
 
 
        break; 
    case IOW: 
        if(Size  >=  SMTP_LOG_LAST_SIZE ) 
            break; 
        if(Size  !=  strlen(data)       ) 
            break; 
 
        if(!extra_strncmp(data,Last)) 
        { 
           count++; 
           break; 
        } 
 
        if(count != 0 && strlen(Log) + 40 <  SMTP_LOG_SIZE) 
        { 
          sprintf(Log + strlen(Log),"last message repeated %d times\n",count); 
 
        } 
 
        count = 0; 
        strcpy(Last, data); 
 
        if(Size + strlen(Log)  >=  SMTP_LOG_SIZE) 
            memset(Log, 0x00, SMTP_LOG_SIZE); 
 
        strcat(Log, data); 
 
        break; 
    case IOINIT: 
        memset(Log, 0x00, SMTP_LOG_SIZE); 
        memset(Last, 0x00, SMTP_LOG_LAST_SIZE); 
        count = 0; 
        break; 
 } 
 
 
 asm  POPF 
} 
/****************************************************************************/ 
/* FUNCTION NAME :   Smtp_okay 
/* DESCRIPTION   : 
/* in PARAMETERS : 
/* out PARAMETERS: 
/* RETURN VALUES : 
/* EXAMPLE	 : 
*****************************************************************************/ 
 
INT8U Smtp_okay (void *data, INT32U op) 
{ 
    TSmtpStat           *Smtpstat    =    data; 
    INT8U               Msg[256]; 
    fd_set              readset; 
    struct  timeval     timeout={SMTP_ORDER_RECV_TIMEOUT,0}; 
    INT8U               maxfdp1; 
    INT32S              len; 
    INT8U               count; 
    INT8U               flag; 
 
    if(Smtpstat->stat == FTP_CLOSE) return 0; 
    maxfdp1 =  Smtpstat->Socket + 1; 
 
 
   // Msg[0]  =   0x00; 
    memset(Msg,0x00,sizeof(Msg)); 
    len     =   0; 
    count   =   0; 
    flag    =   0; 
 
 
 
    while(1) 
    { 
 
 
        FD_ZERO(&readset); 
        FD_SET(Smtpstat->Socket , &readset); 
        select(maxfdp1, &readset, 0, 0,&timeout); 
        if(FD_ISSET(Smtpstat->Socket, &readset) ) 
        { 
            len    =    recv(Smtpstat->Socket , 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 >= 60/SMTP_ORDER_RECV_TIMEOUT) ///< up 15 to 60 to prevent smtp ack is too slow  
               { 
                    break; 
               } 
 
            } 
 
        } 
 
    }  /* while(1)*/ 
 
    Msg[100] = 0x00;// skip length err 
    if(!flag) 
    { 
 
        sprintf(Msg+strlen(Msg) , "Smtp  recv order %d ERR\n\x00", op); 
        Smtpstat->stat = SMTP_CLOSE; 
    } 
    else 
    { 
 
         sprintf(Msg+strlen(Msg), ":Smtp  recv order %d ok\n\x00", op); 
    } 
    SmtpLog (Msg, strlen(Msg), IOW); 
    SHOW_EX1(SMTP_DEBUG ,Msg); 
    return   flag; 
 
} 
/****************************************************************************/ 
/* FUNCTION NAME :   SmtpSend 
/* DESCRIPTION   : 
/* in PARAMETERS : 
/* out PARAMETERS: 
/* RETURN VALUES : 
/* EXAMPLE	 : 
*****************************************************************************/ 
INT8U SmtpSend (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={SMTP_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(1) 
    { 
        FD_ZERO(&writeset); 
        FD_SET(Sock, &writeset); 
        select(maxfdp1, 0, &writeset, 0,&timeout); 
        if(FD_ISSET(Sock, &writeset) ) 
        { 
            len    =   min (TotalLen - SentLen,1300);//min (TotalLen - SentLen,1460*2); 
            len    =   send(Sock , pFile + SentLen, len, 0); 
             
            //----- gofly debug 
            /* 
            if(TotalLen == 1520) 
            { 
 
                printf("%d\n", len); 
 
            } 
            */ 
            //--------------------------------------- 
 
            count  =   0; 
 
            if(len > 0) 
            { 
                SentLen += len; 
 
            } 
            else 
            { 
               sprintf(Msg, "Smtp SEND remote close\n\x00"); 
               flag = 1;// remote close 
               goto GoToSmtpSendEnd; 
            } 
 
            if(TotalLen  == SentLen) 
            { 
               sprintf(Msg, "SMTP SEND ok\n\x00"); 
               flag = 0;//no err 
               goto GoToSmtpSendEnd; 
 
            } 
 
 
        } 
        else 
        {//timeout 
            count++; 
            // timeout == 15 sec and send not ok 
            if(count >= 15/SMTP_ORDER_RECV_TIMEOUT) 
            { 
               sprintf(Msg, "SMTP SEND timeout\n\x00"); 
               flag = 2;// timeout 
               goto GoToSmtpSendEnd; 
            } 
 
 
        } 
 
 
 
 
    }   /* while(1)*/ 
GoToSmtpSendEnd: 
        SmtpLog (Msg, strlen(Msg), IOW); 
        SHOW_EX1(SMTP_DEBUG ,Msg); 
      //OSTimeDlyHMSM(0, 0, 0, 20); 
        return flag; // 0 ok 1 remoteclose 2 timeout 
 
} 
 
/****************************************************************************/ 
/* FUNCTION NAME :   smtp_replace_law 
/* DESCRIPTION   : 
/* in PARAMETERS : 
/* out PARAMETERS: 
/* RETURN VALUES : 
/* EXAMPLE	 : 
*****************************************************************************/ 
 
INT8S smtp_replace_law(INT8U *search,INT8U *search_tag, INT8U op , INT16U uid) 
{ 
    static INT8U SmtpSubject[64]; 
    static INT8U SmtpTime[64]; 
    static INT8U SmtpMsg[256]; 
    INT8U  *p = NULL; 
 
   if(op != IOINIT) 
   { 
 
        if(search == NULL || search_tag == NULL) 
        { 
            return R_ER; 
        } 
 
        if(!strncmp((const INT8S*)search,(INT8S*)"SMTP_SUBJECT",strlen((const INT8S*)search))) 
        { 
            p = SmtpSubject; 
        } 
        else if(!strncmp((const INT8S*)search,(INT8S*)"SMTP_TIME",strlen((const INT8S*)search))) 
        { 
 
            p = SmtpTime; 
        } 
        else if(!strncmp((const INT8S*)search,(INT8S*)"MSG",strlen((const INT8S*)search))) 
        { 
            p = SmtpMsg; 
 
        } 
        else return R_ER; 
   } 
 
    switch(op) 
    { 
        case IOINIT: 
//            strcpy(SmtpSubject,"SMTP_SUBJECT"); 
            sprintf(SmtpSubject,"MOTION_DECTION_EVENT_%s",timer_char); 
            strcpy(SmtpTime,"SMTP_TIME"); 
            strcpy(SmtpMsg,"MESSAGE"); 
            break; 
        case IOR: 
           strcpy((char*)search_tag,p); 
            break; 
        case IOW: 
           strcpy((char*)p,search_tag); 
            break; 
    } 
 
 
 
 
   return R_OK; 
 
 
} 
/****************************************************************************/ 
/* FUNCTION NAME :   MakeSendData 
/* DESCRIPTION   : 
/* in PARAMETERS : 
/* out PARAMETERS: 
/* RETURN VALUES : 
/* EXAMPLE	 : 
*****************************************************************************/ 
 
INT32U MakeSendData () 
{ 
    INT32U i; 
    Treplace   s_replace; 
    s_replace.source                =   smtp_text_head; 
    s_replace.tag                   =   G_smtp_temp_buf; 
    s_replace.source_size           =   sizeof(smtp_text_head); 
    s_replace.tag_size              =   sizeof(G_smtp_temp_buf); 
    s_replace.data                  =   NULL; 
    s_replace.replace_law           =   smtp_replace_law; 
 
   //------ gofly debug skip  s_f_replace_k_w 
 
    #if 0 
    s_f_replace_k_w(&s_replace); 
    #else 
    strcpy(G_smtp_temp_buf,smtp_text_head); 
    #endif 
   //---------------------------------------- 
    Base64Encode((INT8U * const)(G_smtp_temp_buf + strlen(G_smtp_temp_buf)), 
        (const INT8U*)debugSmtpstat->pJpg ,debugSmtpstat->TotalLen); 
 
    for(i = 0; i < sizeof(G_smtp_temp_buf);i++) 
    { 
        if(*(G_smtp_temp_buf + i) == 0x00) 
            break; 
    } 
 
    strcpy(G_smtp_temp_buf + i ,smtp_text_end); 
 
    for(i = 0; i < sizeof(G_smtp_temp_buf);i++) 
    { 
        if(*(G_smtp_temp_buf + i) == 0x00) 
            break; 
    } 
 
    return i; 
 
} 
 
 
/****************************************************************************/ 
/* FUNCTION NAME :   Smtp_Base64Encode_Send 
/* DESCRIPTION   :   base64 encode image and send 
/* in PARAMETERS : 
/* out PARAMETERS: 
/* RETURN VALUES :   R_OK R_ER 
/* EXAMPLE	 : 
*****************************************************************************/ 
INT8U Smtp_Base64Encode_Send( INT8U * const huge aDest, INT8U   huge *aSrc, INT32U aLen ) 
{ 
 
	INT8U      huge  * p = aDest; 
    INT32U           i; 
    INT8U t; 
 
    for ( i = 0; i < aLen; i++ ) 
    { 
    	switch ( i % 3 ) 
        { 
        case 0 : 
        	*p++ = Base2Chr( *aSrc >> 2 ); 
            t = ( *aSrc << 4 ) & 0x3F; 
 
            break; 
        case 1 : 
        	*p++ = Base2Chr( t | ( *aSrc >> 4 ) ); 
            t = ( *aSrc << 2 ) & 0x3F; 
 
            break; 
        case 2 : 
        	*p++ = Base2Chr( t | ( *aSrc >> 6 ) ); 
            *p++ = Base2Chr( *aSrc ); 
 
            break; 
        } 
 
        aSrc = (INT8U huge*)aSrc + 1; 
 
        if( p - aDest > 1460) 
        { 
 
            if(SmtpSend (debugSmtpstat->Socket , aDest, p - aDest)) 
            {//-- send fail 
               return R_ER; 
            } 
            p =  aDest; 
        } 
 
 
 
    } 
 
    if ( aLen % 3 != 0 ) 
    { 
    	*p++ = Base2Chr( t ); 
        if ( aLen % 3 == 1 ) 
        	*p++ = '='; 
        *p++ = '='; 
    } 
 
    *p = 0;  //  aDest is an ASCIIZ string 
 
    if(SmtpSend (debugSmtpstat->Socket , aDest, strlen(aDest))) 
    {//-- send fail 
       return R_ER; 
    } 
 
     //printf("%d **\n",strlen(aDest)); 
     return R_OK; 
} 
/****************************************************************************/ 
/* FUNCTION NAME :   MakeSendHead 
/* DESCRIPTION   : 
/* in PARAMETERS : 
/* out PARAMETERS: 
/* RETURN VALUES : 
/* EXAMPLE	 : 
*****************************************************************************/ 
 
INT32U MakeSendHead () 
{ 
    INT32U i; 
    Treplace   s_replace; 
    s_replace.source                =   smtp_text_head; 
    s_replace.tag                   =   G_smtp_temp_buf; 
    s_replace.source_size           =   sizeof(smtp_text_head); 
    s_replace.tag_size              =   sizeof(G_smtp_temp_buf); 
    s_replace.data                  =   NULL; 
    s_replace.replace_law           =   smtp_replace_law; 
 
 
    s_f_replace_k_w(&s_replace); 
 
    return strlen(G_smtp_temp_buf); 
 
} 
/****************************************************************************/ 
/* FUNCTION NAME :   SmtpApi 
/* DESCRIPTION   : 
/* in PARAMETERS : 
/* out PARAMETERS: 
/* RETURN VALUES : 
/* EXAMPLE	 : 
*****************************************************************************/ 
 
void SmtpApi (void *data,void *jpg_buf) 
{ 
    extern OS_EVENT *PPPoeMbox; 
    INT8U msg[]="Event Start"; 
 
    if(jpg_buf == NULL) 
    { 
        if(data != NULL) 
        { 
            sprintf(data,"%d SMTP EVENT DENY  : jpg_buf == NULL\x00",debugSmtpstat->JpgFileCount); 
        } 
 
        return; 
    } 
 
    if(debugSmtpstat->pJpg != NULL) 
    { 
        if(data != NULL) 
        { 
            sprintf(data,"%d SMTP EVENT DENY  :SMTP RUNNING\x00",debugSmtpstat->JpgFileCount); 
        } 
 
        return; 
    } 
 
 
 
 
        //--- gofly debug 
    //printf("lock %p %p %d\n",((JPG_BUF*)jpg_buf),((JPG_BUF*)jpg_buf)->jpg_file,((JPG_BUF*)jpg_buf)->jpg_size); 
 
    debugSmtpstat->jpg_buf.jpg_file         =  ((JPG_BUF*)(jpg_buf))->jpg_file; 
    debugSmtpstat->jpg_buf.jpg_size         =  ((JPG_BUF*)(jpg_buf))->jpg_size; 
    debugSmtpstat->jpg_buf.jpg_queue_num    =  ((JPG_BUF*)(jpg_buf))->jpg_queue_num; 
 
    debugSmtpstat->TotalLen =  debugSmtpstat->jpg_buf.jpg_size; 
    debugSmtpstat->pJpg     =  (INT8U*)debugSmtpstat->jpg_buf.jpg_file; 
 
    video_image_lock(&debugSmtpstat->jpg_buf); 
    /* 
    if( *( debugSmtpstat->pJpg +  debugSmtpstat->TotalLen  - 1 )   != 0xd9) 
    { 
 
          *( debugSmtpstat->pJpg +  debugSmtpstat->TotalLen  - 1 ) = 0xd9; 
    } 
    */ 
    if(data != NULL) 
    { 
        sprintf(data, "%d SMTP EVENT ALLOW \x00",debugSmtpstat->JpgFileCount); 
    } 
 
    smtp_replace_law("MSG", timer_ctime(), IOW , ROOT); 
    //OSSemPost(SmtpEvenSem); 
    OSMboxPost(PPPoeMbox,(void *)msg); 
} 
 
/****************************************************************************/ 
/* FUNCTION NAME :   SmtpSendIp 
/* DESCRIPTION   : 
/* in PARAMETERS : 
/* out PARAMETERS: 
/* RETURN VALUES : 
/* EXAMPLE	 : 
*****************************************************************************/ 
void SmtpSendIp () 
{ 
       pppoeip = 1; 
} 
 
#endif /*SMTP_OPEN*/