/** * @ingroup ipaddr * Convert IP address string (both versions) to numeric. * The version is auto-detected from the string. * * @param cp IP address string to convert * @param addr conversion result is stored here * @return 1 on success, 0 on error */ int ipaddr_aton(const char *cp, ip_addr_t *addr) { if (cp != NULL) { const char* c; for (c = cp; *c != 0; c++) { if (*c == ':') { /* contains a colon: IPv6 address */ if (addr) { IP_SET_TYPE_VAL(*addr, IPADDR_TYPE_V6); } return ip6addr_aton(cp, ip_2_ip6(addr)); } else if (*c == '.') { /* contains a dot: IPv4 address */ break; } } /* call ip4addr_aton as fallback or if IPv4 was found */ if (addr) { IP_SET_TYPE_VAL(*addr, IPADDR_TYPE_V4); } return ip4addr_aton(cp, ip_2_ip4(addr)); } return 0; }
/** * Ascii internet address interpretation routine. * The value returned is in network order. * * @param cp IP address in ascii representation (e.g. "127.0.0.1") * @return ip address in network order */ u32_t ipaddr_addr(const char *cp) { ip4_addr_t val; if (ip4addr_aton(cp, &val)) { return ip4_addr_get_u32(&val); } return (IPADDR_NONE); }
static void fsip_or_default(struct ip4_addr *d, char *key, int i1, int i2, int i3, int i4) { int r; #if (defined CSR_SPIFLASH_BASE && defined SPIFLASH_PAGE_SIZE) char cp[32]; #endif IP4_ADDR(d, i1, i2, i3, i4); #if (defined CSR_SPIFLASH_BASE && defined SPIFLASH_PAGE_SIZE) r = fs_read(key, cp, sizeof(cp) - 1, NULL); if(r <= 0) return; cp[r] = 0; if(!ip4addr_aton(cp, d)) return; #endif }
void lwip_network_init(uint8_t opmode) { lwip_tcpip_config_t tcpip_config = {{0}, {0}, {0}, {0}, {0}, {0}}; ip4addr_aton(STA_IPADDR, &(tcpip_config.sta_addr)); ip4addr_aton(STA_NETMASK, &tcpip_config.sta_mask); ip4addr_aton(STA_GATEWAY, &tcpip_config.sta_gateway); ip4addr_aton(AP_IPADDR, &(tcpip_config.ap_addr)); ip4addr_aton(AP_NETMASK, &tcpip_config.ap_mask); ip4addr_aton(AP_GATEWAY, &tcpip_config.ap_gateway); wifi_connected = xSemaphoreCreateBinary(); #if USE_DHCP ip_ready = xSemaphoreCreateBinary(); #endif lwip_tcpip_init(&tcpip_config, opmode); }
/** * Check whether "cp" is a valid ascii representation * of an IPv6 address and convert to a binary address. * Returns 1 if the address is valid, 0 if not. * * @param cp IPv6 address in ascii representation (e.g. "FF01::1") * @param addr pointer to which to save the ip address in network order * @return 1 if cp could be converted to addr, 0 on failure */ int ip6addr_aton(const char *cp, ip6_addr_t *addr) { u32_t addr_index, zero_blocks, current_block_index, current_block_value; const char *s; #if LWIP_IPV4 int check_ipv4_mapped = 0; #endif /* LWIP_IPV4 */ /* Count the number of colons, to count the number of blocks in a "::" sequence zero_blocks may be 1 even if there are no :: sequences */ zero_blocks = 8; for (s = cp; *s != 0; s++) { if (*s == ':') { zero_blocks--; #if LWIP_IPV4 } else if (*s == '.') { if ((zero_blocks == 5) ||(zero_blocks == 2)) { check_ipv4_mapped = 1; /* last block could be the start of an IPv4 address */ zero_blocks--; } else { /* invalid format */ return 0; } break; #endif /* LWIP_IPV4 */ } else if (!lwip_isxdigit(*s)) { break; } } /* parse each block */ addr_index = 0; current_block_index = 0; current_block_value = 0; for (s = cp; *s != 0; s++) { if (*s == ':') { if (addr) { if (current_block_index & 0x1) { addr->addr[addr_index++] |= current_block_value; } else { addr->addr[addr_index] = current_block_value << 16; } } current_block_index++; #if LWIP_IPV4 if (check_ipv4_mapped) { if (current_block_index == 6) { ip4_addr_t ip4; int ret = ip4addr_aton(s + 1, &ip4); if (ret) { if (addr) { addr->addr[3] = lwip_htonl(ip4.addr); current_block_index++; goto fix_byte_order_and_return; } return 1; } } } #endif /* LWIP_IPV4 */ current_block_value = 0; if (current_block_index > 7) { /* address too long! */ return 0; } if (s[1] == ':') { if (s[2] == ':') { /* invalid format: three successive colons */ return 0; } s++; /* "::" found, set zeros */ while (zero_blocks > 0) { zero_blocks--; if (current_block_index & 0x1) { addr_index++; } else { if (addr) { addr->addr[addr_index] = 0; } } current_block_index++; if (current_block_index > 7) { /* address too long! */ return 0; } } } } else if (lwip_isxdigit(*s)) { /* add current digit */ current_block_value = (current_block_value << 4) + (lwip_isdigit(*s) ? (u32_t)(*s - '0') : (u32_t)(10 + (lwip_islower(*s) ? *s - 'a' : *s - 'A'))); } else { /* unexpected digit, space? CRLF? */ break; } } if (addr) { if (current_block_index & 0x1) { addr->addr[addr_index++] |= current_block_value; } else { addr->addr[addr_index] = current_block_value << 16; } #if LWIP_IPV4 fix_byte_order_and_return: #endif /* convert to network byte order. */ for (addr_index = 0; addr_index < 4; addr_index++) { addr->addr[addr_index] = lwip_htonl(addr->addr[addr_index]); } ip6_addr_clear_zone(addr); } if (current_block_index != 7) { return 0; } return 1; }
/*-----------------------------------------------------------------------------------*/ int main(int argc, char **argv) { int ch; char ip_str[16] = {0}, nm_str[16] = {0}, gw_str[16] = {0}; /* startup defaults (may be overridden by one or more opts) */ IP4_ADDR(&gw, 192,168,0,1); IP4_ADDR(&netmask, 255,255,255,0); IP4_ADDR(&ipaddr, 192,168,0,2); ping_flag = 0; /* use debug flags defined by debug.h */ debug_flags = LWIP_DBG_OFF; while ((ch = getopt_long(argc, argv, "dhg:i:m:p:", longopts, NULL)) != -1) { switch (ch) { case 'd': debug_flags |= (LWIP_DBG_ON|LWIP_DBG_TRACE|LWIP_DBG_STATE|LWIP_DBG_FRESH|LWIP_DBG_HALT); break; case 'h': usage(); exit(0); break; case 'g': ip4addr_aton(optarg, &gw); break; case 'i': ip4addr_aton(optarg, &ipaddr); break; case 'm': ip4addr_aton(optarg, &netmask); break; case 'p': ping_flag = !0; ip4addr_aton(optarg, &ping_addr); strncpy(ip_str,ip4addr_ntoa(&ping_addr),sizeof(ip_str)); printf("Using %s to ping\n", ip_str); break; default: usage(); break; } } argc -= optind; argv += optind; strncpy(ip_str,ip4addr_ntoa(&ipaddr),sizeof(ip_str)); strncpy(nm_str,ip4addr_ntoa(&netmask),sizeof(nm_str)); strncpy(gw_str,ip4addr_ntoa(&gw),sizeof(gw_str)); printf("Host at %s mask %s gateway %s\n", ip_str, nm_str, gw_str); #ifdef PERF perf_init("/tmp/simhost.perf"); #endif /* PERF */ printf("System initialized.\n"); sys_thread_new("main_thread", main_thread, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO); pause(); return 0; }
int main(int argc, char *argv[]) { int c; int keep_alive; int non_local; struct netif *netif; int dns_count; char *str; char *endptr; int mtu; int fd_in; int fd_out; ip4_addr_t ipaddr; ip4_addr_t netmask; ip4_addr_t gateway; ip4_addr_t dns; struct event_base *base; char *pcap_file; struct conn_info *local; struct conn_info *remote; struct conn_info *socks; struct conn_info *info; ip_addr_set_zero(&ipaddr); ip_addr_set_zero(&netmask); ip_addr_set_zero(&gateway); local = remote = socks = NULL; dns_count = 0; keep_alive = 0; fd_in = 0; fd_out = 1; mtu = 0; pcap_file = NULL; non_local = 0; signal(SIGPIPE, SIG_IGN); base = event_base_new(); lwip_init(); libevent_timeouts_init(base); if ((str = getenv("INTERNAL_IP4_ADDRESS"))) ip4addr_aton(str, &ipaddr); if ((str = getenv("INTERNAL_IP4_MTU"))) mtu = strtoul(str, NULL, 0); if ((str = getenv("VPNFD"))) fd_in = fd_out = strtoul(str, NULL, 0); if ((str = getenv("CISCO_DEF_DOMAIN"))) { endptr = str; while ((str = tokenize(endptr, ", ", &endptr))) host_add_search(str); } if ((str = getenv("INTERNAL_IP4_DNS"))) { endptr = str; while ((str = tokenize(endptr, ", ", &endptr))) { ip4addr_aton(str, &dns); dns_setserver(dns_count++, &dns); free(str); } } while ((c = getopt(argc, argv, "L:D:R:k:m:s:d:i:n:G:p:gh")) != -1) { switch (c) { case 'L': info = parse_conn_info(optarg, 4); if (!info) print_usage(argv[0]); info->next = local; local = info; break; case 'D': info = parse_conn_info(optarg, 2); if (!info) print_usage(argv[0]); info->next = socks; socks = info; break; case 'R': info = parse_conn_info(optarg, 4); if (!info) print_usage(argv[0]); info->next = remote; remote = info; break; case 'k': keep_alive = strtoul(optarg, &endptr, 0); if (*endptr) print_usage(argv[0]); keep_alive *= 1000; break; case 'm': mtu = strtoul(optarg, &endptr, 0); if (*endptr) print_usage(argv[0]); break; case 's': while ((str = tokenize(optarg, ", ", &optarg))) host_add_search(str); break; case 'd': while ((str = tokenize(optarg, ", ", &optarg))) { ip4addr_aton(str, &dns); dns_setserver(dns_count++, &dns); free(str); } break; case 'i': ip4addr_aton(optarg, &ipaddr); break; case 'n': ip4addr_aton(optarg, &netmask); break; case 'G': ip4addr_aton(optarg, &gateway); break; #ifdef USE_PCAP case 'p': pcap_file = strdup(optarg); break; #endif case 'g': non_local = 1; break; default: print_usage(argv[0]); } } while (local) { info = local; str = info->bind && info->bind[0] ? info->bind : NULL; if (!non_local) str = str ? : "localhost"; if (forward_local(base, str, info->bind_port, info->host, info->host_port, keep_alive) < 0) return -1; local = info->next; free_conn_info(info); } while (socks) { info = socks; str = info->bind && info->bind[0] ? info->bind : NULL; if (!non_local) str = str ? : "localhost"; if (socks_listen(base, str, info->bind_port, keep_alive) < 0) return -1; socks = socks->next; free_conn_info(info); } while (remote) { info = remote; if (forward_remote(base, info->bind_port, info->host, info->host_port, keep_alive) < 0) return -1; remote = info->next; free_conn_info(info); } netif = tunif_add(base, fd_in, fd_out, pcap_file); netif_set_ipaddr(netif, &ipaddr); netif_set_netmask(netif, &netmask); netif_set_gw(netif, &gateway); if (mtu) netif->mtu = mtu; netif_set_up(netif); event_base_dispatch(base); return 0; }