/** * @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; }
/** * Create a RAW PCB for IPv6. * * @return The RAW PCB which was created. NULL if the PCB data structure * could not be allocated. * * @param proto the protocol number (next header) of the IPv6 packet payload * (e.g. IP6_NEXTH_ICMP6) * * @see raw_remove() */ struct raw_pcb * raw_new_ip6(u8_t proto) { struct raw_pcb *pcb; pcb = raw_new(proto); #if LWIP_IPV4 IP_SET_TYPE_VAL(pcb->local_ip, IPADDR_TYPE_V6); IP_SET_TYPE_VAL(pcb->remote_ip, IPADDR_TYPE_V6); #endif /* LWIP_IPV4 */ return pcb; }
/** * Create a RAW PCB for IPv6. * * @return The RAW PCB which was created. NULL if the PCB data structure * could not be allocated. * * @param type IP address type, see IPADDR_TYPE_XX definitions. * @param proto the protocol number (next header) of the IPv6 packet payload * (e.g. IP6_NEXTH_ICMP6) * * @see raw_remove() */ struct raw_pcb * raw_new_ip_type(u8_t type, u8_t proto) { struct raw_pcb *pcb; pcb = raw_new(proto); #if LWIP_IPV4 && LWIP_IPV6 if(pcb != NULL) { IP_SET_TYPE_VAL(pcb->local_ip, type); IP_SET_TYPE_VAL(pcb->remote_ip, type); } #else /* LWIP_IPV4 && LWIP_IPV6 */ LWIP_UNUSED_ARG(type); #endif /* LWIP_IPV4 && LWIP_IPV6 */ return pcb; }
/** * Change the IP address of a network interface * * @param netif the network interface to change * @param ipaddr the new IP address * * @note call netif_set_addr() if you also want to change netmask and * default gateway */ void netif_set_ipaddr(struct netif *netif, const ip4_addr_t *ipaddr) { ip4_addr_t new_addr = (ipaddr ? *ipaddr : *IP4_ADDR_ANY); /* address is actually being changed? */ if (ip4_addr_cmp(&new_addr, netif_ip4_addr(netif)) == 0) { LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: netif address being changed\n")); #if LWIP_TCP tcp_netif_ipv4_addr_changed(netif_ip4_addr(netif), ipaddr); #endif /* LWIP_TCP */ #if LWIP_UDP udp_netif_ipv4_addr_changed(netif_ip4_addr(netif), ipaddr); #endif /* LWIP_UDP */ mib2_remove_ip4(netif); mib2_remove_route_ip4(0, netif); /* set new IP address to netif */ ip4_addr_set(ip_2_ip4(&netif->ip_addr), ipaddr); IP_SET_TYPE_VAL(netif->ip_addr, IPADDR_TYPE_V4); mib2_add_ip4(netif); mib2_add_route_ip4(0, netif); netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV4); NETIF_STATUS_CALLBACK(netif); } LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("netif: IP address of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", netif->name[0], netif->name[1], ip4_addr1_16(netif_ip4_addr(netif)), ip4_addr2_16(netif_ip4_addr(netif)), ip4_addr3_16(netif_ip4_addr(netif)), ip4_addr4_16(netif_ip4_addr(netif)))); }
static int open_dataconnection(struct tcp_pcb *pcb, struct ftpd_msgstate *fsm) { if (fsm->passive) return 0; /* Allocate memory for the structure that holds the state of the connection. */ fsm->datafs = malloc(sizeof(struct ftpd_datastate)); if (fsm->datafs == NULL) { send_msg(pcb, fsm, msg451); return 1; } memset(fsm->datafs, 0, sizeof(struct ftpd_datastate)); fsm->datafs->msgfs = fsm; fsm->datafs->msgpcb = pcb; sfifo_init(&fsm->datafs->fifo, 2000); fsm->datapcb = tcp_new(); /* Tell TCP that this is the structure we wish to be passed for our callbacks. */ tcp_arg(fsm->datapcb, fsm->datafs); ip_addr_t dataip; IP_SET_TYPE_VAL(dataip, IPADDR_TYPE_V4); ip4_addr_copy(*ip_2_ip4(&dataip), fsm->dataip); tcp_connect(fsm->datapcb, &dataip, fsm->dataport, ftpd_dataconnected); return 0; }
/** * Change the default gateway for a network interface * * @param netif the network interface to change * @param gw the new default gateway * * @note call netif_set_addr() if you also want to change ip address and netmask */ void netif_set_gw(struct netif *netif, const ip4_addr_t *gw) { ip4_addr_set(ip_2_ip4(&netif->gw), gw); IP_SET_TYPE_VAL(netif->gw, IPADDR_TYPE_V4); LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("netif: GW address of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", netif->name[0], netif->name[1], ip4_addr1_16(netif_ip4_gw(netif)), ip4_addr2_16(netif_ip4_gw(netif)), ip4_addr3_16(netif_ip4_gw(netif)), ip4_addr4_16(netif_ip4_gw(netif)))); }
/** * Change the netmask of a network interface * * @param netif the network interface to change * @param netmask the new netmask * * @note call netif_set_addr() if you also want to change ip address and * default gateway */ void netif_set_netmask(struct netif *netif, const ip4_addr_t *netmask) { mib2_remove_route_ip4(0, netif); /* set new netmask to netif */ ip4_addr_set(ip_2_ip4(&netif->netmask), netmask); IP_SET_TYPE_VAL(netif->netmask, IPADDR_TYPE_V4); mib2_add_route_ip4(0, netif); LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("netif: netmask of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", netif->name[0], netif->name[1], ip4_addr1_16(netif_ip4_netmask(netif)), ip4_addr2_16(netif_ip4_netmask(netif)), ip4_addr3_16(netif_ip4_netmask(netif)), ip4_addr4_16(netif_ip4_netmask(netif)))); }
/* * Change an IPv6 address of a network interface (internal version taking 4 * u32_t) * * @param netif the network interface to change * @param addr_idx index of the IPv6 address * @param i0 word0 of the new IPv6 address * @param i1 word1 of the new IPv6 address * @param i2 word2 of the new IPv6 address * @param i3 word3 of the new IPv6 address */ void netif_ip6_addr_set_parts(struct netif *netif, s8_t addr_idx, u32_t i0, u32_t i1, u32_t i2, u32_t i3) { const ip6_addr_t *old_addr; LWIP_ASSERT("netif != NULL", netif != NULL); LWIP_ASSERT("invalid index", addr_idx < LWIP_IPV6_NUM_ADDRESSES); old_addr = netif_ip6_addr(netif, addr_idx); /* address is actually being changed? */ if ((old_addr->addr[0] != i0) || (old_addr->addr[1] != i1) || (old_addr->addr[2] != i2) || (old_addr->addr[3] != i3)) { LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_ip6_addr_set: netif address being changed\n")); if (netif_ip6_addr_state(netif, addr_idx) & IP6_ADDR_VALID) { #if LWIP_TCP || LWIP_UDP ip_addr_t new_ipaddr; IP_ADDR6(&new_ipaddr, i0, i1, i2, i3); #endif /* LWIP_TCP || LWIP_UDP */ #if LWIP_TCP tcp_netif_ip_addr_changed(netif_ip_addr6(netif, addr_idx), &new_ipaddr); #endif /* LWIP_TCP */ #if LWIP_UDP udp_netif_ip_addr_changed(netif_ip_addr6(netif, addr_idx), &new_ipaddr); #endif /* LWIP_UDP */ #if LWIP_RAW raw_netif_ip_addr_changed(netif_ip_addr6(netif, addr_idx), &new_ipaddr); #endif /* LWIP_RAW */ } /* @todo: remove/readd mib2 ip6 entries? */ IP6_ADDR(ip_2_ip6(&(netif->ip6_addr[addr_idx])), i0, i1, i2, i3); IP_SET_TYPE_VAL(netif->ip6_addr[addr_idx], IPADDR_TYPE_V6); if (netif_ip6_addr_state(netif, addr_idx) & IP6_ADDR_VALID) { netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV6); NETIF_STATUS_CALLBACK(netif); } } LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("netif: IPv6 address %d of interface %c%c set to %s/0x%"X8_F"\n", addr_idx, netif->name[0], netif->name[1], ip6addr_ntoa(netif_ip6_addr(netif, addr_idx)), netif_ip6_addr_state(netif, addr_idx))); }
int dnsclient_main(int argc, FAR char *argv[]) #endif { struct hostent *shost = NULL; ip_addr_t dns_addr; if (argc < 2) { show_usage(argv[0]); return 0; } if (argc == 3 && argv[2] != NULL) { printf("dnsclient : dns_add_nameserver : %s\n", argv[2]); IP_SET_TYPE_VAL(dns_addr, IPADDR_TYPE_V4); #ifdef CONFIG_NET_IPv6 dns_addr.u_addr.ip4.addr = inet_addr(argv[2]); #else dns_addr.addr = inet_addr(argv[2]); #endif /* CONFIG_NET_IPv6 */ dns_setserver(0, &dns_addr); } memset(hostname, 0x00, DNS_NAME_MAXSIZE); if (strlen(argv[1]) > DNS_NAME_MAXSIZE) { printf("dnsclient : length of hostname has to lower than %d\n", DNS_NAME_MAXSIZE); return -1; } strncpy(hostname, argv[1], DNS_NAME_MAXSIZE); printf("\nHostname : %s [len %d]\n", hostname, strlen(hostname)); if ((shost = gethostbyname(hostname)) == NULL || shost->h_addr_list == NULL) { printf("dnsclient : failed to resolve host's IP address, shost %p\n", shost); return -1; } else { printf("DNS results\n"); printf("IP Address : %s\n", ip_ntoa((ip_addr_t *)shost->h_addr_list[0])); } return 0; }
/**************************************************************************** * Name: dhcpc_request ****************************************************************************/ static int dhcpc_request(void *handle, struct dhcpc_state *presult) { if (!handle) { ndbg("ERROR : handle must not be null\n"); return -100; } if (!presult) { ndbg("ERROR : presult must not be null\n"); return -100; } struct dhcpc_state_s *pdhcpc = (struct dhcpc_state_s *)handle; g_pResult = presult; struct in_addr oldaddr; struct in_addr newaddr; uint8_t msgtype; int retries = 0, result = 0; char *intf; #define CNT_MAX_DISCOVER 5 #define CNT_MAX_REQUEST 5 /* Save the currently assigned IP address (should be INADDR_ANY) */ oldaddr.s_addr = 0; intf = pdhcpc->nic; int ret = netlib_get_ipv4addr(intf, &oldaddr); if (ret == -1) { printf("Set IPv4 fail\n"); } /* Loop until we receive the lease (or an error occurs) */ /* Set the IP address to INADDR_ANY. */ newaddr.s_addr = INADDR_ANY; if (netlib_set_ipv4addr(intf, &newaddr) == ERROR) { ndbg("netlib_set_ipv4addr failed\n"); } /* Loop sending DISCOVER until we receive an OFFER from a DHCP * server. We will lock on to the first OFFER and decline any * subsequent offers (which will happen if there are more than one * DHCP servers on the network. */ g_dhcpc_state = STATE_INITIAL; do { /* Get the DHCPOFFER response */ if (++retries > CNT_MAX_DISCOVER) { /* reach max try */ return -2; } ndbg("Send Discover Packet\n"); result = dhcpc_sendmsg(pdhcpc, g_pResult, DHCPDISCOVER); usleep(100000); if (result < 0) { if (get_errno() == EAGAIN) { continue; } ndbg("sendDiscover Error\n"); return -1; } ndbg("Waiting Offer...(%d)\n", pdhcpc->sockfd); result = recv(pdhcpc->sockfd, &pdhcpc->packet, sizeof(struct dhcp_msg), 0); ndbg("Received...(%d)\n", result); if (result <= 0) { ndbg("receive fail\n"); continue; } msgtype = dhcpc_parsemsg(pdhcpc, result, presult); if (msgtype == DHCPOFFER) { /* Save the servid from the presult so that it is not clobbered * by a new OFFER. */ ndbg("Received OFFER from %08x\n", ntohl(presult->serverid.s_addr)); pdhcpc->ipaddr.s_addr = presult->ipaddr.s_addr; pdhcpc->serverid.s_addr = presult->serverid.s_addr; /* Temporarily use the address offered by the server and break * out of the loop. */ ret = netlib_set_ipv4addr(intf, &presult->ipaddr); if (ret == -1) { ndbg("Set IPv4 fail\n"); } g_dhcpc_state = STATE_HAVE_OFFER; break; } else { ndbg("Received AnotherData %d %x\n", result, msgtype); } } while (g_dhcpc_state == STATE_INITIAL); /* Loop sending the REQUEST up to three times (if there is no response) */ retries = 0; /* Send the REQUEST message to obtain the lease that was offered to us. */ ndbg("Send REQUEST\n"); do { if (retries++ > CNT_MAX_REQUEST) break; ndbg("Send Request Packet\n"); result = dhcpc_sendmsg(pdhcpc, g_pResult, DHCPREQUEST); if (result < 0) { ndbg("thread_sendRequest Error\n"); return -3; } usleep(100000); /* Get the ACK/NAK response to the REQUEST (or timeout) */ ndbg("Waiting Ack...\n"); result = recv(pdhcpc->sockfd, &pdhcpc->packet, sizeof(struct dhcp_msg), 0); ndbg("Received...(%d)\n", result); if (result <= 0) { ndbg("recv request error(%d)(%d)\n", result, errno); continue; } /* Parse the response */ ndbg("Data Received\n"); msgtype = dhcpc_parsemsg(pdhcpc, result, presult); /* The ACK response means that the server has accepted our request * and we have the lease. */ if (msgtype == DHCPACK) { ndbg("Received ACK\n"); g_dhcpc_state = STATE_HAVE_LEASE; } /* NAK means the server has refused our request. Break out of * this loop with state == STATE_HAVE_OFFER and send DISCOVER again */ else if (msgtype == DHCPNAK) { ndbg("Received NAK\n"); break; } /* If we get any OFFERs from other servers, then decline them now * and continue waiting for the ACK from the server that we * requested from. */ else if (msgtype == DHCPOFFER) { /* If we get OFFERs from same dhcp server, do not send DECLINE */ if (pdhcpc->serverid.s_addr == presult->serverid.s_addr) { ndbg("Received duplicated OFFER from %08x\n", ntohl(presult->serverid.s_addr)); } else { ndbg("Received another OFFER from %08x, send DECLINE\n", ntohl(presult->serverid.s_addr)); result = dhcpc_sendmsg(pdhcpc, presult, DHCPDECLINE); if (result <= 0) { ndbg("recv request error(%d)(%d)\n", result, errno); } } } /* Otherwise, it is something that we do not recognize */ else { ndbg("Ignoring msgtype=%d %d\n", msgtype, result); } usleep(100000L); /* An error has occurred. If this was a timeout error (meaning * that nothing was received on this socket for a long period of time). * Then break out and send the DISCOVER command again (at most * 3 times). */ } while (g_dhcpc_state == STATE_HAVE_OFFER); ndbg("Got IP address %d.%d.%d.%d\n", (presult->ipaddr.s_addr) & 0xff, (presult->ipaddr.s_addr >> 8) & 0xff, (presult->ipaddr.s_addr >> 16) & 0xff, (presult->ipaddr.s_addr >> 24) & 0xff); ndbg("Got netmask %d.%d.%d.%d\n", (presult->netmask.s_addr) & 0xff, (presult->netmask.s_addr >> 8) & 0xff, (presult->netmask.s_addr >> 16) & 0xff, (presult->netmask.s_addr >> 24) & 0xff); ndbg("Got DNS server %d.%d.%d.%d\n", (presult->dnsaddr.s_addr) & 0xff, (presult->dnsaddr.s_addr >> 8) & 0xff, (presult->dnsaddr.s_addr >> 16) & 0xff, (presult->dnsaddr.s_addr >> 24) & 0xff); ndbg("Got default router %d.%d.%d.%d\n", (presult->default_router.s_addr) & 0xff, (presult->default_router.s_addr >> 8) & 0xff, (presult->default_router.s_addr >> 16) & 0xff, (presult->default_router.s_addr >> 24) & 0xff); ndbg("Lease expires in %d seconds\n", presult->lease_time); #if defined CONFIG_NET_LWIP // this is temporal fix. it should be modified later ip_addr_t dns_addr; IP_SET_TYPE_VAL(dns_addr, IPADDR_TYPE_V4); #ifdef CONFIG_NET_IPv6 dns_addr.u_addr.ip4.addr = presult->dnsaddr.s_addr; #else dns_addr.addr = presult->dnsaddr.s_addr; #endif dns_setserver(0, &dns_addr); #endif /* CONFIG_NET_LWIP */ #if defined(CONFIG_NETDB_DNSCLIENT) && defined(CONFIG_NETDB_DNSSERVER_BY_DHCP) struct sockaddr_in dns; if (presult->dnsaddr.s_addr != 0) { ndbg("Set DNS IP address via dns_add_nameserver\n"); dns.sin_addr.s_addr = presult->dnsaddr.s_addr; dns.sin_family = AF_INET; dns.sin_port = htons(DNS_DEFAULT_PORT); dns_add_nameserver((FAR struct sockaddr *)&dns, sizeof(struct sockaddr_in)); } #endif return OK; }
/** * Convert the Simba inet ip address to a lwip ip address. */ static inline void inet_ip_to_lwip_ip(ip_addr_t *dst_p, const struct inet_ip_addr_t *src_p) { ip4_addr_set_u32(ip_2_ip4(dst_p), src_p->number); IP_SET_TYPE_VAL(*dst_p, IPADDR_TYPE_V4); }