ftp_var_example.c

来自「开放源码实时操作系统源码.」· C语言 代码 · 共 567 行 · 第 1/2 页

C
567
字号
#include <pkgconf/system.h>
#define _config_OK
#if !defined(CYGPKG_NET_FTPCLIENT)
#error "Configuration is missing: CYGPKG_NET_FTPCLIENT"
#undef _config_OK
#endif
#if !defined(CYGPKG_COMPRESS_ZLIB)
#error "Configuration is missing: CYGPKG_COMPRESS_ZLIB"
#undef _config_OK
#else
#include <pkgconf/compress_zlib.h>
#if !defined(CYGSEM_COMPRESS_ZLIB_DEFLATE_MAKES_GZIP)
#error "Configuration is missing: CYGSEM_COMPRESS_ZLIB_DEFLATE_MAKES_GZIP"
#undef _config_OK
#endif
#endif
#if !defined(CYGPKG_CRC)
#error "Configuration is missing: CYGPKG_CRC"
#undef _config_OK
#endif
 
#ifdef _config_OK
#include <stdio.h>
#include <stdlib.h>
#include <ftpclient.h>
#include <cyg/compress/zlib.h>

#include <pkgconf/net.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/errno.h>

#include <net/if.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <net/route.h>

#include <cyg/infra/diag.h>
#include <cyg/kernel/kapi.h>

#include <bootp.h>
#include <network.h>
#include <arpa/inet.h>

#include "nvram.h"
#include "lkvm.h"
#include "getopt.h"

#define FTP_SERV "192.168.1.125"
#define FTP_USER "george"
#define FTP_PASS "allen"

#define MAX_ARGV 32

#ifdef CYGHWR_NET_DRIVER_ETH0
static struct bootp eth0_bootp_data;
static const char  *eth0_name = "eth0";
#endif
#ifdef CYGHWR_NET_DRIVER_ETH1
static struct bootp eth1_bootp_data;
static const char  *eth1_name = "eth1";
#endif

// TEMP for debug
core_info_t test_core_info = {
    .fence0 = KCORE_FENCE0_PAT,
    .fence1 = KCORE_FENCE1_PAT,
    .fence2 = KCORE_FENCE2_PAT,
    .fence3 = KCORE_FENCE3_PAT,
    .crash = KCORE_CRASH_PANIC,
    .panicstr = "This is a panic string",
    .ncpu = 1,
    .nsegs = 2,
    .physical_banks[0] = { 0x700000, 0x40000 },
    .physical_banks[1] = { 0x740000, 0x40000 },
    .sr[0] = 0xB00B0000,
    .sr[1] = 0xB00B0001,
    .sr[2] = 0xB00B0002,
    .sr[3] = 0xB00B0003,
    .sr[4] = 0xB00B0004,
    .sr[5] = 0xB00B0005,
    .sr[6] = 0xB00B0006,
    .sr[7] = 0xB00B0007,
    .dbatl[0] = 0xDEAD0000, .dbatu[0] = 0xDEAD1000,
    .dbatl[1] = 0xDEAD0001, .dbatu[1] = 0xDEAD1001,
    .dbatl[2] = 0xDEAD0002, .dbatu[2] = 0xDEAD1002,
    .dbatl[3] = 0xDEAD0003, .dbatu[3] = 0xDEAD1003,
    .dbatl[4] = 0xDEAD0004, .dbatu[4] = 0xDEAD1004,
    .dbatl[5] = 0xDEAD0005, .dbatu[5] = 0xDEAD1005,
    .dbatl[6] = 0xDEAD0006, .dbatu[6] = 0xDEAD1006,
    .dbatl[7] = 0xDEAD2007, .dbatu[7] = 0xDEAD3007,
    .ibatl[0] = 0xDEAD2000, .ibatu[0] = 0xDEAD3000,
    .ibatl[1] = 0xDEAD2001, .ibatu[1] = 0xDEAD3001,
    .ibatl[2] = 0xDEAD2002, .ibatu[2] = 0xDEAD3002,
    .ibatl[3] = 0xDEAD2003, .ibatu[3] = 0xDEAD3003,
    .ibatl[4] = 0xDEAD2004, .ibatu[4] = 0xDEAD3004,
    .ibatl[5] = 0xDEAD2005, .ibatu[5] = 0xDEAD3005,
    .ibatl[6] = 0xDEAD2006, .ibatu[6] = 0xDEAD3006,
    .ibatl[7] = 0xDEAD2007, .ibatu[7] = 0xDEAD3007,
};
// TEMP

#define FTP_BUFSIZE 4096
struct ftp_data {
    z_stream stream;
    char     buf[FTP_BUFSIZE], *bufp;
    int      len;
    int      src_len;
    int      first;
    int    (*get_data)(char **ptr, void *param);
    void    *get_param;
};

int _ftp_write_gz(char *buf, int len, void *priv)
{
    struct ftp_data *dp = (struct ftp_data *)priv;
    int err;
    int res = 0;
    char *src;

    if (dp->first) {
        // Ugly hack - zlib needs a value for 'avail_in' at initialization
        // time, even though we don't have anything ready yet
        dp->stream.avail_in = 0;
        dp->first = 0;
    }
    while ((dp->len == 0) && (dp->src_len > 0)) {
        // Has input buffer been exhausted?
        if (dp->stream.avail_in == 0) {
            dp->src_len = (*dp->get_data)(&src, dp->get_param);
            dp->stream.next_in = src;
            dp->stream.avail_in = dp->src_len;
        }        
        // See if more data is available to be compressed
        dp->stream.avail_out = FTP_BUFSIZE;
        dp->stream.next_out = dp->buf;
        err = deflate(&dp->stream, dp->src_len ? Z_NO_FLUSH : Z_FINISH);
        dp->len = FTP_BUFSIZE - dp->stream.avail_out;
        dp->bufp = dp->buf;
    }
    // Move up to 'len' bytes from internal buffer
    res = len;
    if (res > dp->len) {
        res = dp->len;
    }
    memcpy(buf, dp->bufp, res);
    dp->bufp += res;
    dp->len -= res;
    return res;
}

struct _simple_get_data_param {
    char *ptr;
    int   len;
};

int
simple_get_data(char **ptr, void *param)
{
    struct _simple_get_data_param *gp = (struct _simple_get_data_param *)param;
    int res;

    *ptr = gp->ptr;
    res = gp->len;
    gp->len = 0;
    return res;
    
}


//
// Parse (scan/copy) network addresses.  If the IP address contains
// a '/', then use that to determine the netmask and ignore the
// specified value.
//
bool
parse_eth_addrs(char *desired_ip, char *desired_netmask, 
                char *ip, char *netmask, char *broadcast)
{
    char *src, *dst;
    bool found_netmask = false;
    int mask_size, mask, ip_addr;

//    diag_printf("parse - ip: '%s', mask: '%s'\n", desired_ip, desired_netmask);
    // IP address
    src = desired_ip;  dst = ip;
    while (*src) {
        if (*src == '/') {
            // Do something with netmask
            src++;
            found_netmask = true;
            mask_size = 0;
            while (*src) {
                mask_size = (mask_size * 10) + (*src++ - '0');
            }
            mask = 0xFFFFFFFF << (32-mask_size);
            break;
        }
        *dst++ = *src++;
    }
    *dst = '\0';
    if (!found_netmask) {
        if (!inet_aton(desired_netmask, (struct in_addr *)&mask)) {
            diag_printf("Error: Invalid netmask '%s'\n", desired_netmask);
            return false;  // Illegal netmask
        }
    }
    strcpy(netmask, inet_ntoa(*(struct in_addr *)&mask));
    if (!inet_aton(ip, (struct in_addr *)&ip_addr)) {
        diag_printf("Error: Invalid IP address '%s'\n", ip);
        return false;
    }
    ip_addr = (ip_addr & mask) | ~mask;
    strcpy(broadcast, inet_ntoa(*(struct in_addr *)&ip_addr));
//    diag_printf("ip: '%s', netmask: '%s', broadcast: '%s'\n", ip, netmask, broadcast);
    return true;
}

struct core_dump_param {
    int             phase;
    int             segno;
    core_info_t    *cp;
    kcore_hdr_t     hdr;
    kcore_seg_t     cpu_hdr, ram_hdr;
    cpu_kcore_hdr_t cpu_info;
    phys_ram_seg_t  cpu_phys_segs[8];
};

#define ALIGN(v,n) ((((v)+((n)-1))/(n))*(n))

int
dump_core(char **ptr, void *param)
{
    struct core_dump_param *dp = (struct core_dump_param *)param;
    int len;

    switch (dp->phase) {
    case 0:
        // File header (kcore_hdr_t)
        dp->phase = 1;
        *ptr = (char *)&dp->hdr;
        return ALIGN(sizeof(dp->hdr), 8);
    case 1:
        // CPU header (kcore_seg_t)
        dp->phase = 2;
        *ptr = (char *)&dp->cpu_hdr;
        return ALIGN(sizeof(dp->cpu_hdr), 8);
    case 2:
        // CPU info (cpu_kcore_hdr_t)
        dp->phase = 3;
        dp->segno = 0;
        *ptr = (char *)&dp->cpu_info;
        return sizeof(dp->cpu_info);
    case 3:
        // Physical RAM segment descriptions (phys_ram_seg_t)
        *ptr = (char *)&dp->cpu_phys_segs[dp->segno];
        if (++dp->segno == dp->cp->nsegs) {
            dp->phase = 4;
        }
        return sizeof(dp->cpu_phys_segs[0]);
    case 4:
        // RAM header (kcore_seg_t)
        dp->phase = 5;
        *ptr = (char *)&dp->ram_hdr;
        dp->segno = 0;
        return ALIGN(sizeof(dp->ram_hdr), 8);
    case 5:
        // RAM segment
        *ptr = (char *)dp->cpu_phys_segs[dp->segno].start;
        len = dp->cpu_phys_segs[dp->segno].size;
        if (++dp->segno == dp->cp->nsegs) {
            dp->phase = 6;
        }
        return len;
    case 6:
        // All done!
        return 0;
    default:
        diag_printf("Bad core dump phase: %d\n", dp->phase);
        return 0;
    }
}

⌨️ 快捷键说明

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