示例#1
0
/******************************************************************************
* 函数名称: ftpDownload
* 函数功能: 通过FTP从PC机下载文件到指定的地址
                        
* 创建日期: 2003-04-25
* 作    者: 邱文
* 输入参数: 
            CHAR *fileName: 要下载的文件名
            CHAR *TEMPBUFFER: 存放在系统内存的起始地址
* 输出参数: 无
* 返    回: fileLength(文件长度),失败返回0
* 被调函数:ftpXfer() and ftpCommand()
* 调用函数:bsp_menu_ftpDownload()
*******************************************************************************/
LOCAL DWORD ftpDownload_ALTERA
(
BYTE *fileName,
BYTE *TEMPBUFFER
)
{
    BOOT_PARAMS  params;
    int dataSock;
    int ctrlSock;
    CHAR buf[1024];
    SDWORD nBytes;
    DWORD fileLength = 0;
    SDWORD status;
     
    BYTE *tempBuffer = TEMPBUFFER;

    if ( ( NULL == fileName ) || ( NULL == tempBuffer ) )
    {
        return 0; 
    }
    
    if (usrBootLineCrack (BOOT_LINE_ADRS, &params) != OK)
    {
        printf("根启动参数无效  \n");
        return (0);
    }
 
    if (ftpXfer (params.had, params.usr, params.passwd, "", "RETR %s", "", 
			fileName,&ctrlSock, &dataSock) == ERROR)
    {
        printf("下载文件 \"%s\" 出错 \n", fileName);
        return (0);
    }
  
    while ((nBytes = read (dataSock, buf, sizeof (buf))) > 0)
    {
         
        memcpy (tempBuffer, buf, nBytes);
        fileLength += nBytes;
        tempBuffer += nBytes;

    }
    /*   BSP_MENU_PRINT("     拷贝到缓冲区成功\n");*/

      close (dataSock);
    /*   BSP_MENU_PRINT("     关闭文件成功\n");*/
    if (fileLength > 0)
    {   
        status = OK;
        if (ftpCommand (ctrlSock, "QUIT", 0, 0, 0, 0, 0, 0) != FTP_COMPLETE)
             status = ERROR;
        close (ctrlSock);

        /*printf("\r\nFTP下载成功, 文件长度为 %d",fileLength);*/
        return fileLength;

    }
    else
    {    
        #if 0
        if (nBytes < 0)             /* read error? */
            status = 0;

        if (ftpReplyGet (ctrlSock, TRUE) != FTP_COMPLETE)
            status = 0;
        
        #endif
        if (ftpCommand (ctrlSock, "QUIT", 0, 0, 0, 0, 0, 0) != FTP_COMPLETE)
        {
            status = 0;
        }
        close (ctrlSock);
        printf("FTP下载失败\n");
        return 0;
    }
}
示例#2
0
int ftpOpen(char *host, int family, char *name, char *password,
            char *proxy, int port) {
    static int sock;
    struct in_addr addr;
    struct in6_addr addr6;
    struct sockaddr_in destPort;
    struct sockaddr_in6 destPort6;
    struct passwd * pw;
    char * buf;
    int rc = 0;
    char *userReply;

    if (port < 0) port = IPPORT_FTP;

    if (!name)
        name = "anonymous";

    if (!password) {
        password = "******";
        if (getuid()) {
            pw = getpwuid(getuid());
            if (pw) {
                password = alloca(strlen(pw->pw_name) + 2);
                strcpy(password, pw->pw_name);
                strcat(password, "@");
            }
        }
    }

    if (proxy) {
        if (asprintf(&buf, "%s@%s", name, host) != -1)
            name = buf;
        host = proxy;
    }

    if (family == AF_INET)
        rc = getHostAddress(host, &addr, AF_INET);
    else if (family == AF_INET6)
        rc = getHostAddress(host, &addr6, AF_INET6);

    if (rc)
        return rc;

    sock = socket(family, SOCK_STREAM, IPPROTO_IP);
    if (sock < 0) {
        return FTPERR_FAILED_CONNECT;
    }

    if (family == AF_INET) {
        destPort.sin_family = family;
        destPort.sin_port = htons(port);
        destPort.sin_addr = addr;

        if (connect(sock, (struct sockaddr *) &destPort, sizeof(destPort))) {
            close(sock);
            return FTPERR_FAILED_CONNECT;
        }
    } else if (family == AF_INET6) {
        destPort6.sin6_family = family;
        destPort6.sin6_port = htons(port);
        destPort6.sin6_addr = addr6;

        if (connect(sock, (struct sockaddr *) &destPort6, sizeof(destPort6))) {
            close(sock);
            return FTPERR_FAILED_CONNECT;
        }
    }

    /* ftpCheckResponse() assumes the socket is nonblocking */
    if (fcntl(sock, F_SETFL, O_NONBLOCK)) {
        close(sock);
        return FTPERR_FAILED_CONNECT;
    }

    if ((rc = ftpCheckResponse(sock, NULL))) {
        return rc;     
    }

    if ((rc = ftpCommand(sock, &userReply, "USER", name, NULL))) {
        close(sock);
        return rc;
    }

    /* FTP does not require that USER be followed by PASS.  Anonymous logins
     * in particular do not need any password.
     */
    if (strncmp(userReply, "230", 3) != 0) {
        if ((rc = ftpCommand(sock, NULL, "PASS", password, NULL))) {
            close(sock);
            return rc;
        }
    }

    if ((rc = ftpCommand(sock, NULL, "TYPE", "I", NULL))) {
        close(sock);
        return rc;
    }

    return sock;
}