示例#1
0
// List disk partitions
static void
do_disks(int argc, char *argv[])
{
    int i, j;
    disk_t *d;
    partition_t *p;
    char name[16];

    for (i = 0, d = disk_table;  i < disk_count;  i++, d++) {
        switch (d->kind) {
          case DISK_IDE_HD:
            for (j = 0, p = d->partitions;
                 j < CYGNUM_REDBOOT_MAX_PARTITIONS;
                 j++, p++) {
                if (p->systype) {
                    diag_sprintf(name, "hd%c%d", 'a' + d->index, j+1);
                    diag_printf("%-8s %s\n", name, systype_name(p->systype));
                }
            }
            break;
          case DISK_IDE_CDROM:
            diag_sprintf(name, "cd%d", d->index);
            diag_printf("%-8s ISO9660\n", name);
            break;
        }
    }
}
示例#2
0
文件: fconfig.c 项目: cilynx/dd-wrt
// Lookup an alias. First try plain string aliases. If that fails try
// other types so allowing access to all configured values. This allows
// for alias (macro) expansion of normal 'fconfig' data, such as the
// board IP address.
char *
flash_lookup_alias(char *alias, char *alias_buf)
{
    char name[80];
    char *val;
    unsigned char * dp;
    void *val_ptr;
    int type;
    bool hold_bool_val;
    long hold_long_val;
#ifdef CYGPKG_REDBOOT_NETWORKING
    int esa_ptr;
#endif

    make_alias(name, alias);
    if (flash_get_config(name, &val, CONFIG_STRING)) {
        return val;
    } else {
        dp = flash_lookup_config(alias);
        if (dp) {
            val_ptr = (void *)CONFIG_OBJECT_VALUE(dp);
            switch (type = CONFIG_OBJECT_TYPE(dp)) {
            case CONFIG_BOOL:
                memcpy(&hold_bool_val, val_ptr, sizeof(bool));
                diag_sprintf(alias_buf, "%s", hold_bool_val ? "true" : "false");
                break;
            case CONFIG_INT:
                memcpy(&hold_long_val, val_ptr, sizeof(unsigned long));
                diag_sprintf(alias_buf,"%ld", hold_long_val);
                break;
#ifdef CYGPKG_REDBOOT_NETWORKING
            case CONFIG_IP:
                diag_sprintf(alias_buf,"%s", inet_ntoa((in_addr_t *)val_ptr));
                break;
            case CONFIG_ESA:
                for (esa_ptr = 0;  esa_ptr < sizeof(enet_addr_t);  esa_ptr++) {
                    diag_sprintf(alias_buf+(3*esa_ptr), "0x%02X", ((unsigned char *)val_ptr)[esa_ptr]);
                    if (esa_ptr < (sizeof(enet_addr_t)-1)) diag_printf(":");
                }
                break;
#endif
            case CONFIG_SCRIPT:
                return (char *) val_ptr;
                break;
            default:
                return (char *)NULL;
            }
            return alias_buf;
        } 
        return (char *)NULL;
    }
}
externC char *
fileio_error(int err)
{
    static char myerr[10];

    diag_sprintf(myerr, "error %d", err);
    return myerr;
}
示例#4
0
// Lookup an alias. First try plain string aliases. If that fails try
// other types so allowing access to all configured values. This allows
// for alias (macro) expansion of normal 'fconfig' data, such as the
// board IP address.
static char *
lookup_alias(char *alias, char *alias_buf)
{
    if (0 == strcasecmp("FREEMEMLO", alias)) {
        diag_sprintf(alias_buf,"0x%x", ((CYG_ADDRWORD)mem_segments[0].start + 0x03ff) & ~0x03ff);
        return alias_buf;
    } else if (0 == strcasecmp("FREEMEMHI", alias)) {
        diag_sprintf(alias_buf,"0x%x", ((CYG_ADDRWORD)mem_segments[0].end) & ~0x03ff);
        return alias_buf;
    }

#ifdef CYGSEM_REDBOOT_FLASH_ALIASES
    return flash_lookup_alias(alias, alias_buf);
#else
    return NULL;
#endif
}
示例#5
0
// Assumes address is in network byte order
char *
inet_ntoa(in_addr_t *addr)
{
    static char str[32];
    unsigned char *ap;

    ap = (unsigned char *)addr;
    diag_sprintf(str, "%d.%d.%d.%d", ap[0], ap[1], ap[2], ap[3]);
    return str;
}
示例#6
0
char *
inet_ntoa_r(struct in_addr ina, char *buf)
{
    unsigned char *ucp = (unsigned char *)&ina;
    diag_sprintf(buf, "%d.%d.%d.%d",
                 ucp[0] & 0xff,
                 ucp[1] & 0xff,
                 ucp[2] & 0xff,
                 ucp[3] & 0xff);
    return buf;
}
示例#7
0
//------------------------------------------------------------------------------
// FUNCTION
//
//
// DESCRIPTION
//
//  
// PARAMETERS
//
//  
// RETURN
//
//  
//------------------------------------------------------------------------------
char *inet_ntoa(struct in_addr ina)
{
	static char buf[4*sizeof "123"];
	unsigned char *ucp = (unsigned char *)&ina;

	diag_sprintf(buf, "%d.%d.%d.%d",
                     ucp[0] & 0xff,
                     ucp[1] & 0xff,
                     ucp[2] & 0xff,
                     ucp[3] & 0xff);
	return buf;
}
示例#8
0
int
http_stream_open(connection_info_t *info, int *err)
{
    int res;
    struct _stream *s = &http_stream;

    info->server->sin_port = 80;  // HTTP port
    if ((res = __tcp_open(&s->sock, info->server, get_port++, 5000, err)) < 0) {
        *err = HTTP_OPEN;
        return -1;
    }
    diag_sprintf(s->data, "GET %s HTTP/1.0\r\n\r\n", info->filename);
    __tcp_write_block(&s->sock, s->data, strlen(s->data));    
    s->avail = 0;
    s->open = true;
    s->pos = 0;
    return 0;
}
示例#9
0
文件: fconfig.c 项目: cilynx/dd-wrt
static void
make_alias(char *alias, char *name)
{
    diag_sprintf(alias, "alias/%s", name);
}
示例#10
0
文件: fconfig.c 项目: cilynx/dd-wrt
static int
get_config(unsigned char *dp, char *title, int list_opt, char *newvalue )
{
    char line[256], hold_line[256], *sp, *lp;
    int ret;
    bool hold_bool_val, new_bool_val, enable;
    unsigned long hold_int_val, new_int_val;
#ifdef CYGPKG_REDBOOT_NETWORKING
    in_addr_t hold_ip_val, new_ip_val;
    enet_addr_t hold_esa_val;
    int esa_ptr;
    char *esp;
#endif
    void *val_ptr;
    int type;

    if (CONFIG_OBJECT_ENABLE_KEYLEN(dp)) {
        flash_get_config(CONFIG_OBJECT_ENABLE_KEY(dp), &enable, CONFIG_BOOL);
        if (((bool)CONFIG_OBJECT_ENABLE_SENSE(dp) && !enable) ||
            (!(bool)CONFIG_OBJECT_ENABLE_SENSE(dp) && enable)) {
            return CONFIG_OK;  // Disabled field
        }
    }
    lp = line;  *lp = '\0';
    val_ptr = (void *)CONFIG_OBJECT_VALUE(dp);
    if (LIST_OPT_NICKNAMES & list_opt)
        diag_printf("%s: ", CONFIG_OBJECT_KEY(dp));
    if (LIST_OPT_FULLNAMES & list_opt) {
        if (title != (char *)NULL) {
            diag_printf("%s: ", title);
        } else {
            diag_printf("%s: ", CONFIG_OBJECT_KEY(dp));
        }
    }
    switch (type = CONFIG_OBJECT_TYPE(dp)) {
    case CONFIG_BOOL:
        memcpy(&hold_bool_val, val_ptr, sizeof(bool));
        lp += diag_sprintf(lp, "%s", hold_bool_val ? "true" : "false");
        break;
    case CONFIG_INT:
        memcpy(&hold_int_val, val_ptr, sizeof(unsigned long));
        lp += diag_sprintf(lp, "%ld", hold_int_val);
        break;
#ifdef CYGPKG_REDBOOT_NETWORKING
    case CONFIG_IP:
        lp += diag_sprintf(lp, "%s", inet_ntoa((in_addr_t *)val_ptr));
        if (0 == strcmp("0.0.0.0", line) && !(LIST_OPT_LIST_ONLY & list_opt)) {
            // then we have a deeply unhelpful starting text - kill it off
            // (unless we are just listing all values)
            lp = line;  *lp = '\0';
        }
        break;
    case CONFIG_ESA:
        for (esa_ptr = 0;  esa_ptr < sizeof(enet_addr_t);  esa_ptr++) {
            lp += diag_sprintf(lp, "0x%02X", ((unsigned char *)val_ptr)[esa_ptr]);
            if (esa_ptr < (sizeof(enet_addr_t)-1)) lp += diag_sprintf(lp, ":");
        }
        break;
#if defined(CYGHWR_NET_DRIVERS) && (CYGHWR_NET_DRIVERS > 1)
    case CONFIG_NETPORT:
        lp += diag_sprintf(lp, "%s", (unsigned char *)val_ptr);
        break;
#endif
#endif
    case CONFIG_STRING:
        lp += diag_sprintf(lp, "%s", (unsigned char *)val_ptr);
        break;
    case CONFIG_SCRIPT:
        diag_printf("\n");
        sp = lp = (unsigned char *)val_ptr;
        while (*sp) {
            while (*lp != '\n') lp++;
            *lp = '\0';
            diag_printf(".. %s\n", sp);
            *lp++ = '\n';
            sp = lp;
        }
        break;
    }
    if (LIST_OPT_LIST_ONLY & list_opt) {
        diag_printf("%s\n", line);
        return CONFIG_OK;
    }
    if (type != CONFIG_SCRIPT) {
        if (NULL != newvalue) {
            ret = strlen(newvalue);
            if (ret > sizeof(line))
                return CONFIG_BAD;
            strcpy(hold_line, line); // Hold the old value for comparison
            strcpy(line, newvalue);
            diag_printf("Setting to %s\n", newvalue);
        } else {
            // read from terminal
            strcpy(hold_line, line);
            if (LIST_OPT_DUMBTERM & list_opt) {
                diag_printf( (CONFIG_STRING == type ?
                              "%s > " :
                              "%s ? " ), line);
                *line = '\0';
            }
            ret = _rb_gets_preloaded(line, sizeof(line), 0);
        }
        if (ret < 0) return CONFIG_ABORT;
        // empty input - leave value untouched (else DNS goes away for a
        // minute to try to look it up) but we must accept empty value for strings.
        if (0 == line[0] && CONFIG_STRING != type) return CONFIG_OK; 
        if (strcmp(line, hold_line) == 0) return CONFIG_OK;  // Just a CR - leave value untouched
        lp = &line[strlen(line)-1];
        if (*lp == '.') return CONFIG_DONE;
        if (*lp == '^') return CONFIG_BACK;
    }
    switch (type) {
    case CONFIG_BOOL:
        memcpy(&hold_bool_val, val_ptr, sizeof(bool));
        if (!parse_bool(line, &new_bool_val)) {
            return CONFIG_BAD;
        }
        if (hold_bool_val != new_bool_val) {
            memcpy(val_ptr, &new_bool_val, sizeof(bool));
            return CONFIG_CHANGED;
        } else {
            return CONFIG_OK;
        }
        break;
    case CONFIG_INT:
        memcpy(&hold_int_val, val_ptr, sizeof(unsigned long));
        if (!parse_num(line, &new_int_val, 0, 0)) {
            return CONFIG_BAD;
        }
        if (hold_int_val != new_int_val) {
            memcpy(val_ptr, &new_int_val, sizeof(unsigned long));
            return CONFIG_CHANGED;
        } else {
            return CONFIG_OK;
        }
        break;
#ifdef CYGPKG_REDBOOT_NETWORKING
    case CONFIG_IP:
        memcpy(&hold_ip_val.s_addr, &((in_addr_t *)val_ptr)->s_addr, sizeof(in_addr_t));
        if (!_gethostbyname(line, &new_ip_val)) {
            return CONFIG_BAD;
        }
        if (hold_ip_val.s_addr != new_ip_val.s_addr) {
            memcpy(val_ptr, &new_ip_val, sizeof(in_addr_t));
            return CONFIG_CHANGED;
        } else {
            return CONFIG_OK;
        }
        break;
    case CONFIG_ESA:
        memcpy(&hold_esa_val, val_ptr, sizeof(enet_addr_t));
        esp = line;
        for (esa_ptr = 0;  esa_ptr < sizeof(enet_addr_t);  esa_ptr++) {
            unsigned long esa_byte;
            if (!parse_num(esp, &esa_byte, &esp, ":")) {
                memcpy(val_ptr, &hold_esa_val, sizeof(enet_addr_t));
                return CONFIG_BAD;
            }
            ((unsigned char *)val_ptr)[esa_ptr] = esa_byte;
        }
#ifdef CYGSEM_REDBOOT_PLF_ESA_VALIDATE
        if (!cyg_plf_redboot_esa_validate(val_ptr)) {
            memcpy(val_ptr, &hold_esa_val, sizeof(enet_addr_t));
            return CONFIG_BAD;
        }
#endif
        return CONFIG_CHANGED;
        break;
#if defined(CYGHWR_NET_DRIVERS) && (CYGHWR_NET_DRIVERS > 1)
    case CONFIG_NETPORT:
	if (strlen(line) >= MAX_STRING_LENGTH || net_devindex(line) < 0) {
	    int index;
	    const char *name;
	    diag_printf("Sorry, Port name must be one of:\n");
	    for (index = 0; (name = net_devname(index)) != NULL; index++)
		diag_printf("    %s\n", name);
            return CONFIG_BAD;
	}
        strcpy((unsigned char *)val_ptr, line);
	break;
#endif
#endif
    case CONFIG_SCRIPT:
        // Assume it always changes
        sp = (unsigned char *)val_ptr;
        diag_printf("Enter script, terminate with empty line\n");
        while (true) {
            *sp = '\0';
            diag_printf(">> ");
            ret = _rb_gets(line, sizeof(line), 0);
            if (ret < 0) return CONFIG_ABORT;
            if (strlen(line) == 0) break;
            lp = line;
            while (*lp) {
                *sp++ = *lp++;
            }
            *sp++ = '\n';
        }
        break;
    case CONFIG_STRING:
        if (strlen(line) >= MAX_STRING_LENGTH) {
            diag_printf("Sorry, value is too long\n");
            return CONFIG_BAD;
        }
        strcpy((unsigned char *)val_ptr, line);
        break;
    }
    return CONFIG_CHANGED;
}
示例#11
0
static void
server_test(struct bootp *bp)
{
    int sock_indx, i, s, socks[MAXSOCK], client, client_len;
    struct sockaddr client_addr;
    char buf[256], addr_buf[256];
    char host_addr_buf[256], host_port_buf[32];
    int one = 1;
    fd_set in_fds, src_fds;
    int num, len;
    struct timeval tv;
    struct addrinfo *ai, *addrs, hints;
    int err, last_sock;

    bzero(&hints, sizeof(hints));
    hints.ai_family = PF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE;
    if ((err = getaddrinfo(NULL, "7734", &hints, &addrs)) != EAI_NONE) {
        diag_printf("can't getaddrinfo(): %s\n", gai_strerror(err));
        pexit("getaddrinfo failed");
    }
    sock_indx = 0;  last_sock = -1;
    ai = addrs;
    while (ai) {
        _inet_ntop(ai->ai_addr, addr_buf, sizeof(addr_buf));
        diag_printf("Family: %d, Socket: %d, Addr: %s\n", ai->ai_family, ai->ai_socktype, addr_buf);
        s = socket(ai->ai_family, ai->ai_socktype, 0);
        if (s < 0) {
            pexit("stream socket");
        }
        if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
            pexit("setsockopt SO_REUSEADDR");
        }
        if (setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one))) {
            pexit("setsockopt SO_REUSEPORT");
        }
        if(bind(s, ai->ai_addr, ai->ai_addr->sa_len) < 0) {
            pexit("bind error");
        }
        listen(s, SOMAXCONN);
        socks[sock_indx++] = s;
        if (sock_indx >= MAXSOCK) {
            pexit("Too many address types");
        }
        ai = ai->ai_next;
        if (s > last_sock) last_sock = s;
    }
    while (true) {
        // Wait for some activity on one of the ports
        FD_ZERO(&src_fds);
        for (s = 0;  s < sock_indx;  s++) {
            FD_SET(socks[s], &src_fds);
        }
        num = select(last_sock+1, &src_fds, 0, 0, 0);
        if (num > 0) {
            // There are 'num' sockets ready to connect
            for (i = 0;  i < sock_indx; i++) {
                s = socks[i];
                if (FD_ISSET(s, &src_fds)) {
                    client_len = sizeof(client_addr);
                    if ((client = accept(s, (struct sockaddr *)&client_addr, &client_len)) < 0) {
                        pexit("accept");
                    }
                    client_len = sizeof(client_addr);
                    getpeername(client, &client_addr, &client_len);
                    if (getnameinfo (&client_addr, client_len, 
                                     host_addr_buf, sizeof(host_addr_buf),
                                     host_port_buf, sizeof(host_port_buf), 
                                     NI_NUMERICHOST) == EAI_NONE) {
                        diag_printf("connection from %s(%s)\n", host_addr_buf, host_port_buf);
                        diag_sprintf(buf, "Hello %s(%s)\n", host_addr_buf, host_port_buf);
                    } else {
                        _inet_ntop(&client_addr, addr_buf, sizeof(addr_buf));
                        diag_printf("connection from %s(%d)\n", addr_buf, _inet_port(&client_addr));
                        diag_sprintf(buf, "Hello %s(%d)\n", addr_buf, _inet_port(&client_addr));
                    }
                    write(client, buf, strlen(buf));
                    tv.tv_sec = 5;
                    tv.tv_usec = 0;
                    FD_ZERO(&in_fds);
                    FD_SET(client, &in_fds);
                    num = select(client+1, &in_fds, 0, 0, &tv);
                    if (num > 0) {
                        len = read(client, buf, sizeof(buf)-1);
                        buf[len-1] = '\0';  // Trim \n
                        diag_printf("buf = '%s'\n", buf);
                    } else if (num == 0) {
                        diag_printf("No reply - timed out\n");
                    } else {
                        perror("select");
                    }
                    close(client);
                }
            }
        } else {
            diag_printf("select returned: %d\n", num);
            pexit("bad select");
        }
    }
}
示例#12
0
文件: net_io.c 项目: cilynx/dd-wrt
void
net_init(void)
{
    cyg_netdevtab_entry_t *t;
    unsigned index;
    struct eth_drv_sc *primary_net = (struct eth_drv_sc *)0;
#if defined(CYGHWR_NET_DRIVERS) && (CYGHWR_NET_DRIVERS > 1)
    char *default_devname;
    int default_index;
#endif
#ifdef CYGDAT_REDBOOT_DEFAULT_BOOTP_SERVER_IP_ADDR
    char ip_addr[16];
#endif

    // Set defaults as appropriate
#ifdef CYGSEM_REDBOOT_DEFAULT_NO_BOOTP
    use_bootp = false;
#else
    use_bootp = true;
#endif
#ifdef CYGDBG_REDBOOT_NET_DEBUG
    net_debug = true;
#else
    net_debug = false;
#endif
    gdb_port = CYGNUM_REDBOOT_NETWORKING_TCP_PORT;
#ifdef CYGSEM_REDBOOT_FLASH_CONFIG
    // Fetch values from saved config data, if available
#if defined(CYGHWR_NET_DRIVERS) && (CYGHWR_NET_DRIVERS > 1)
    flash_get_config("net_device", &default_devname, CONFIG_NETPORT);
#endif
    flash_get_config("net_debug", &net_debug, CONFIG_BOOL);
    flash_get_config("gdb_port", &gdb_port, CONFIG_INT);
    flash_get_config("bootp", &use_bootp, CONFIG_BOOL);
    if (!use_bootp) {
        flash_get_IP("bootp_my_ip", &__local_ip_addr);
#ifdef CYGSEM_REDBOOT_NETWORKING_USE_GATEWAY
        flash_get_IP("bootp_my_ip_mask", &__local_ip_mask);
        flash_get_IP("bootp_my_gateway_ip", &__local_ip_gate);
#endif
    }
#endif
# ifdef CYGDBG_IO_ETH_DRIVERS_DEBUG
    // Don't override if the user has deliberately set something more
    // verbose.
    if (0 == cyg_io_eth_net_debug)
        cyg_io_eth_net_debug = net_debug;
# endif
    have_net = false;
    // Make sure the recv buffers are set up
    eth_drv_buffers_init();
    __pktbuf_init();

    // Initialize network device(s).
#if defined(CYGHWR_NET_DRIVERS) && (CYGHWR_NET_DRIVERS > 1)
    default_index = net_devindex(default_devname);
    if (default_index < 0)
	default_index = 0;
#ifdef CYGSEM_REDBOOT_NETWORK_INIT_ONE_DEVICE
    if ((t = net_devtab_entry(default_index)) != NULL && t->init(t)) {
	t->status = CYG_NETDEVTAB_STATUS_AVAIL;
	primary_net = __local_enet_sc;
    } else
#endif
#endif
    for (index = 0; (t = net_devtab_entry(index)) != NULL; index++) {
#if defined(CYGSEM_REDBOOT_NETWORK_INIT_ONE_DEVICE)
	if (index == default_index)
	    continue;
#endif
	if (t->init(t)) {
            t->status = CYG_NETDEVTAB_STATUS_AVAIL;
            if (primary_net == (struct eth_drv_sc *)0) {
                primary_net = __local_enet_sc;
            }
#if defined(CYGHWR_NET_DRIVERS) && (CYGHWR_NET_DRIVERS > 1)
#if !defined(CYGSEM_REDBOOT_NETWORK_INIT_ONE_DEVICE)
            if (index == default_index) {
                primary_net = __local_enet_sc;
            }
#else
	    break;
#endif
#endif
        }
    }
    __local_enet_sc = primary_net;

    if (!__local_enet_sc) {
        diag_printf("No network interfaces found\n");
        return;
    }    
    // Initialize the network [if present]
    if (use_bootp) {
        if (__bootp_find_local_ip(&my_bootp_info) == 0) {
            have_net = true;
        } else {
            // Is it an unset address, or has it been set to a static addr
            if (__local_ip_addr[0] == 0 && __local_ip_addr[1] == 0 &&
                __local_ip_addr[2] == 0 && __local_ip_addr[3] == 0) {
                show_eth_info();
                diag_printf("Can't get BOOTP info for device!\n");
            } else {
                diag_printf("Can't get BOOTP info, using default IP address\n");
                have_net = true;
            }
        }
    } else {
        enet_addr_t enet_addr;
        have_net = true;  // Assume values in FLASH were OK
        // Tell the world that we are using this fixed IP address
        if (__arp_request((ip_addr_t *)__local_ip_addr, &enet_addr, 1) >= 0) {
            diag_printf("Warning: IP address %s in use\n", inet_ntoa((in_addr_t *)&__local_ip_addr));
        }
    }
    if (have_net) {
        show_eth_info();
#ifdef CYGDAT_REDBOOT_DEFAULT_BOOTP_SERVER_IP_ADDR
        diag_sprintf(ip_addr, "%d.%d.%d.%d", 
                     CYGDAT_REDBOOT_DEFAULT_BOOTP_SERVER_IP_ADDR);
        inet_aton(ip_addr, &my_bootp_info.bp_siaddr);
#endif
#ifdef CYGSEM_REDBOOT_FLASH_CONFIG
        flash_get_IP("bootp_server_ip", (ip_addr_t *)&my_bootp_info.bp_siaddr);
#endif
#ifdef CYGPKG_REDBOOT_NETWORKING_DNS
	redboot_dns_res_init();
#endif
        show_addrs();
        net_io_init();
    }
}
示例#13
0
void
net_test(cyg_addrword_t q)
{
    char oldbuff[128];
    size_t oldbuffsize;
    char pbuff[128], name[64], *p;
    int *new_oid;
    size_t num_elem;
    int ret, i, oid_kind; 

    diag_printf("Start sysctl1 test\n");

    //    init_all_network_interfaces();

    /* Test the OID to name function of sysctl*/
    oldbuffsize = sizeof(oldbuff);
    ret = sysctl(mib_name_debug_name, CYG_NELEM(mib_name_debug_name), 
                 oldbuff, &oldbuffsize, NULL, 0);
    if (ret == -1) CYG_TEST_FAIL("sysclt(mib_name) failed");
    CYG_TEST_INFO(oldbuff);
    CYG_TEST_PASS_FAIL(!strcmp(oldbuff, "sysctl.name"), "sysctl.name");

    /* Test the name to OID function of sysclt */
    oldbuffsize = sizeof(oldbuff);
    ret = sysctl(mib_name2oid, CYG_NELEM(mib_name2oid), oldbuff, &oldbuffsize, 
                 name2oid, sizeof(name2oid));
    if (ret == -1) CYG_TEST_FAIL("sysclt(mib_name) failed");
    CYG_TEST_PASS_FAIL(((ret == 8 ) && 
                        (((int *)&oldbuff)[0] == CTL_DEBUG) &&
                        (((int *)&oldbuff)[1] == 3)), 
                       "sysctl.name2oid");
    
    /* Walk the table using the next function of sysclt */
    num_elem = CYG_NELEM(mib_next)+1;
    new_oid = mib + CYG_NELEM(mib_next);
    mib[2] = 0;
    do {
      memcpy(mib,mib_next,sizeof(mib_next));
      oldbuffsize = sizeof(mib) - sizeof(mib_next);
      ret = sysctl(mib, num_elem, new_oid, &oldbuffsize, NULL, 0);
      if (ret == -1) {
        if (errno != ENOENT) {
          CYG_TEST_FAIL_FINISH("sysclt(mib_name) failed");
        } else {
          break;
        }
      }
      p = pbuff;
      num_elem = CYG_NELEM(mib_next) + (ret / 4);
      i=0;
      while (ret > 0) {
        p+=diag_sprintf(p, "%d ",new_oid[i++]);
        ret -= sizeof(int);
      }
      /* Get the name of this oid */
      oldbuffsize = sizeof(name);
      memcpy(mib,mib_name,sizeof(mib_name));
      ret = sysctl(mib, num_elem, name, &oldbuffsize, NULL, 0);
      if (ret == -1) CYG_TEST_FAIL("sysclt(mib2name) failed");
      name[ret] = 0;
      p += diag_sprintf(p, "= %s (", name);

      /* Get the find and format */
      memcpy(mib,mib_oidfmt,sizeof(mib_oidfmt));
      ret = sysctl(mib, num_elem, name, &oldbuffsize, NULL, 0);
      if (ret == -1) CYG_TEST_FAIL("sysclt(mib2name) failed");
      oid_kind = *(int *)name;
      switch (oid_kind & CTLTYPE) {
      case CTLTYPE_NODE:
        p += diag_sprintf(p, "NODE");
        break;
      case CTLTYPE_INT:
        p += diag_sprintf(p, "INT");
        break;
      case CTLTYPE_STRING:
        p += diag_sprintf(p, "STRING");
        break;
      case CTLTYPE_QUAD:
        p += diag_sprintf(p, "QUAD");
        break;
      case CTLTYPE_STRUCT:
        p += diag_sprintf(p, "STRUCT");
        break;
      case CTLTYPE_UINT:
        p += diag_sprintf(p, "UINT");
        break;
      case CTLTYPE_LONG:
        p += diag_sprintf(p, "LONG");
        break;
      case CTLTYPE_ULONG:
        p += diag_sprintf(p, "ULONG");
        break;
      default:
        p += diag_sprintf(p,"Unknown type! (%d)", oid_kind & CTLTYPE);
      }
      if (oid_kind & CTLFLAG_RD) 
        p += diag_sprintf(p," Read");
      if (oid_kind & CTLFLAG_WR) 
        p += diag_sprintf(p," Write");
      if (oid_kind & CTLFLAG_NOLOCK) 
        p += diag_sprintf(p," Nolock");
      if (oid_kind & CTLFLAG_ANYBODY) 
        p += diag_sprintf(p," Anybody");
      if (oid_kind & CTLFLAG_SECURE) 
        p += diag_sprintf(p," Secure");
      if (oid_kind & CTLFLAG_PRISON) 
        p += diag_sprintf(p," Prison");
      if (oid_kind & CTLFLAG_DYN) 
        p += diag_sprintf(p," Dynamic");
      
      p += diag_sprintf(p," )");

      p += diag_sprintf(p,"{%d}", num_elem);
      CYG_TEST_INFO(pbuff);
    } while (ret != -1);

    /* Tests for sysctlnametomib */
    num_elem = CYG_NELEM(mib);
    ret = sysctlnametomib(name2oid, mib,&num_elem);
    if (ret == -1) CYG_TEST_FAIL("sysctlnametomib failed");
    CYG_TEST_PASS_FAIL(((num_elem == 2 ) && 
                        (((int *)&oldbuff)[0] == CTL_DEBUG) &&
                        (((int *)&oldbuff)[1] == 3)), 
                       "sysctlnametooid1");

    /* This time with too small a results buffer */
    num_elem = 1;
    ret = sysctlnametomib(name2oid, mib,&num_elem);
    CYG_TEST_PASS_FAIL((ret == -1) && (errno = ENOMEM), 
                       "sysctlnametooid2");
    /* This time with an unknown name */
    num_elem = CYG_NELEM(mib);
    ret = sysctlnametomib("unknown.unknown", mib,&num_elem);
    CYG_TEST_PASS_FAIL((ret == -1) && (errno = ENOENT), 
                       "sysctlnametooid3");

    /* Tests for sysctlbyname */
    oldbuffsize = sizeof(oldbuff);
    ret = sysctlbyname("sysctl.name2oid", oldbuff, &oldbuffsize, 
                 name2oid, sizeof(name2oid));
    if (ret == -1) CYG_TEST_FAIL("sysclt(mib_name) failed");
    CYG_TEST_PASS_FAIL(((ret == 8 ) && 
                        (((int *)&oldbuff)[0] == CTL_DEBUG) &&
                        (((int *)&oldbuff)[1] == 3)), 
                       "sysctlbyname");

    CYG_TEST_EXIT ("sysctl1 exit");
}
示例#14
0
static void
server_test(struct bootp *bp)
{
    int s, client;
    socklen_t client_len;
    struct sockaddr_in client_addr, local;
    char buf[256];
    int one = 1;
    fd_set in_fds;
    int num, len;
    struct timeval tv;

    s = socket(AF_INET, SOCK_STREAM, 0);
    if (s < 0) {
        pexit("stream socket");
    }
    if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
        pexit("setsockopt SO_REUSEADDR");
    }
    if (setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one))) {
        pexit("setsockopt SO_REUSEPORT");
    }
    memset(&local, 0, sizeof(local));
    local.sin_family = AF_INET;
    local.sin_len = sizeof(local);
    local.sin_port = htons(7734);
    local.sin_addr.s_addr = INADDR_ANY;
    if(bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
        pexit("bind error");
    }
    listen(s, SOMAXCONN);
    while (true) {
        client_len = sizeof(client_addr);
        if ((client = accept(s, (struct sockaddr *)&client_addr, &client_len)) < 0) {
            pexit("accept");
        }
        client_len = sizeof(client_addr);
        getpeername(client, (struct sockaddr *)&client_addr, &client_len);
        diag_printf("connection from %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));

        diag_sprintf(buf, "Hello %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
        
        write(client, buf, strlen(buf));
        tv.tv_sec = 5;
        tv.tv_usec = 0;
        FD_ZERO(&in_fds);
        FD_SET(client, &in_fds);
        num = select(client+1, &in_fds, 0, 0, &tv);
        if (num > 0) {
            len = read(client, buf, sizeof(buf)-1);
            buf[len] = '\0';
            diag_printf("buf = '%s'\n", buf);
        } else if (num == 0) {
            diag_printf("No reply - timed out\n");
        } else {
            perror("select");
        }
        close(client);
    }
    close(s);
}