diff --git a/src/core/ipv4/ip4_addr.c b/src/core/ipv4/ip4_addr.c index 318a4835..fe7809f9 100644 --- a/src/core/ipv4/ip4_addr.c +++ b/src/core/ipv4/ip4_addr.c @@ -315,20 +315,4 @@ ip4addr_ntoa_r(const ip4_addr_t *addr, char *buf, int buflen) return buf; } -#if LWIP_IPV6 -/** Convert IPv4 address to generic IP address. - * Since source types do not contain the type field, a target storage need to be supplied. - */ -ip_addr_t* -ip4_2_ip(const ip4_addr_t *ip4addr, ip_addr_t* storage) -{ - if ((ip4addr == NULL) || (storage == NULL)) { - return NULL; - } - ip4_addr_copy(storage->u_addr.ip4, *ip4addr); - IP_SET_TYPE_VAL(*storage, IPADDR_TYPE_V4); - return storage; -} -#endif /* LWIP_IPV6 */ - #endif /* LWIP_IPV4 */ diff --git a/src/core/ipv6/icmp6.c b/src/core/ipv6/icmp6.c index ecc4b520..9d348149 100644 --- a/src/core/ipv6/icmp6.c +++ b/src/core/ipv6/icmp6.c @@ -163,7 +163,7 @@ icmp6_input(struct pbuf *p, struct netif *inp) /* Determine reply source IPv6 address. */ #if LWIP_MULTICAST_PING if (ip6_addr_ismulticast(ip6_current_dest_addr())) { - reply_src = ip6_select_source_address(inp, ip6_current_src_addr()); + reply_src = ip_2_ip6_c(ip6_select_source_address(inp, ip6_current_src_addr())); if (reply_src == NULL) { /* drop */ pbuf_free(p); @@ -323,7 +323,7 @@ icmp6_send_response(struct pbuf *p, u8_t code, u32_t data, u8_t type) reply_dest = ip6_current_src_addr(); /* Select an address to use as source. */ - reply_src = ip6_select_source_address(netif, reply_dest); + reply_src = ip_2_ip6_c(ip6_select_source_address(netif, reply_dest)); if (reply_src == NULL) { /* drop */ pbuf_free(q); diff --git a/src/core/ipv6/ip6.c b/src/core/ipv6/ip6.c index 61971589..fb38f0d9 100644 --- a/src/core/ipv6/ip6.c +++ b/src/core/ipv6/ip6.c @@ -206,10 +206,10 @@ ip6_route(const ip6_addr_t *src, const ip6_addr_t *dest) * @return the most suitable source address to use, or NULL if no suitable * source address is found */ -const ip6_addr_t * +const ip_addr_t * ip6_select_source_address(struct netif *netif, const ip6_addr_t * dest) { - const ip6_addr_t *src = NULL; + const ip_addr_t *src = NULL; u8_t i; /* If dest is link-local, choose a link-local source. */ @@ -217,7 +217,7 @@ ip6_select_source_address(struct netif *netif, const ip6_addr_t * dest) for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) { if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) && ip6_addr_islinklocal(netif_ip6_addr(netif, i))) { - return netif_ip6_addr(netif, i); + return netif_ip_addr6(netif, i); } } } @@ -228,7 +228,7 @@ ip6_select_source_address(struct netif *netif, const ip6_addr_t * dest) if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) && ip6_addr_issitelocal(netif_ip6_addr(netif, i)) && ip6_addr_netcmp(dest, netif_ip6_addr(netif, i))) { - return netif_ip6_addr(netif, i); + return netif_ip_addr6(netif, i); } } } @@ -239,7 +239,7 @@ ip6_select_source_address(struct netif *netif, const ip6_addr_t * dest) if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) && ip6_addr_isuniquelocal(netif_ip6_addr(netif, i)) && ip6_addr_netcmp(dest, netif_ip6_addr(netif, i))) { - return netif_ip6_addr(netif, i); + return netif_ip_addr6(netif, i); } } } @@ -250,14 +250,14 @@ ip6_select_source_address(struct netif *netif, const ip6_addr_t * dest) if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) && ip6_addr_isglobal(netif_ip6_addr(netif, i))) { if (src == NULL) { - src = netif_ip6_addr(netif, i); + src = netif_ip_addr6(netif, i); } else { /* Replace src only if we find a prefix match. */ /* TODO find longest matching prefix. */ - if ((!(ip6_addr_netcmp(src, dest))) && + if ((!(ip6_addr_netcmp(ip_2_ip6_c(src), dest))) && ip6_addr_netcmp(netif_ip6_addr(netif, i), dest)) { - src = netif_ip6_addr(netif, i); + src = netif_ip_addr6(netif, i); } } } @@ -271,7 +271,7 @@ ip6_select_source_address(struct netif *netif, const ip6_addr_t * dest) for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) { if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) && ip6_addr_netcmp(dest, netif_ip6_addr(netif, i))) { - return netif_ip6_addr(netif, i); + return netif_ip_addr6(netif, i); } } @@ -817,7 +817,7 @@ ip6_output_if(struct pbuf *p, const ip6_addr_t *src, const ip6_addr_t *dest, const ip6_addr_t *src_used = src; if (dest != IP_HDRINCL) { if (src != NULL && ip6_addr_isany(src)) { - src = ip6_select_source_address(netif, dest); + src = ip_2_ip6_c(ip6_select_source_address(netif, dest)); if ((src == NULL) || ip6_addr_isany(src)) { /* No appropriate source address was found for this packet. */ LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip6_output: No suitable source address for packet.\n")); diff --git a/src/core/ipv6/ip6_addr.c b/src/core/ipv6/ip6_addr.c index bb3c609d..85823b5e 100644 --- a/src/core/ipv6/ip6_addr.c +++ b/src/core/ipv6/ip6_addr.c @@ -276,20 +276,6 @@ ip6addr_ntoa_r(const ip6_addr_t *addr, char *buf, int buflen) } #if LWIP_IPV4 -/** Convert IPv6 address to generic IP address. - * Since source types do not contain the type field, a target storage needs to be supplied. - */ -ip_addr_t* -ip6_2_ip(const ip6_addr_t *ip6addr, ip_addr_t* storage) -{ - if ((ip6addr == NULL) || (storage == NULL)) { - return NULL; - } - ip6_addr_copy(storage->u_addr.ip6, *ip6addr); - IP_SET_TYPE_VAL(*storage, IPADDR_TYPE_V6); - return storage; -} - /** Convert IP address string (both versions) to numeric. * The version is auto-detected from the string. * diff --git a/src/core/raw.c b/src/core/raw.c index 0f30969d..d61db3b5 100644 --- a/src/core/raw.c +++ b/src/core/raw.c @@ -241,9 +241,6 @@ raw_sendto(struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *ipaddr) struct pbuf *q; /* q will be sent down the stack */ s16_t header_size; const ip_addr_t *dst_ip = ipaddr; -#if LWIP_IPV4 && LWIP_IPV6 - ip_addr_t src_ip_tmp; -#endif /* LWIP_IPV4 && LWIP_IPV6 */ if ((pcb == NULL) || (ipaddr == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, ipaddr)) { return ERR_VAL; @@ -314,7 +311,7 @@ raw_sendto(struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *ipaddr) if (ip_addr_isany(&pcb->local_ip)) { /* use outgoing network interface IP address as source address */ - src_ip = ip_netif_get_local_ip(PCB_ISIPV6(pcb), netif, dst_ip, &src_ip_tmp); + src_ip = ip_netif_get_local_ip(PCB_ISIPV6(pcb), netif, dst_ip); #if LWIP_IPV6 if (src_ip == NULL) { if (q != p) { diff --git a/src/core/snmp/msg_out.c b/src/core/snmp/msg_out.c index 7c534dfe..9593a6ee 100644 --- a/src/core/snmp/msg_out.c +++ b/src/core/snmp/msg_out.c @@ -228,9 +228,6 @@ snmp_send_trap(s8_t generic_trap, const struct snmp_obj_id *eoid, s32_t specific { struct snmp_trap_dst *td; struct netif *dst_if; -#if LWIP_IPV4 && LWIP_IPV6 - ip_addr_t dst_ip_storage; -#endif /* LWIP_IPV4 && LWIP_IPV6 */ const ip_addr_t* dst_ip; struct pbuf *p; u16_t i,tot_len; @@ -244,7 +241,7 @@ snmp_send_trap(s8_t generic_trap, const struct snmp_obj_id *eoid, s32_t specific ip_addr_copy(trap_msg.dip, td->dip); /* lookup current source address for this dst */ ip_route_get_local_ip(PCB_ISIPV6(trap_msg.pcb), &trap_msg.pcb->local_ip, - &td->dip, dst_if, dst_ip, &dst_ip_storage); + &td->dip, dst_if, dst_ip); if ((dst_if != NULL) && (dst_ip != NULL)) { trap_msg.sip_raw_len = (IP_IS_V6_VAL(*dst_ip) ? 16 : 4); memcpy(trap_msg.sip_raw, dst_ip, trap_msg.sip_raw_len); diff --git a/src/core/tcp.c b/src/core/tcp.c index 160e038d..32924bd2 100644 --- a/src/core/tcp.c +++ b/src/core/tcp.c @@ -755,9 +755,6 @@ tcp_connect(struct tcp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port, err_t ret; u32_t iss; u16_t old_local_port; -#if LWIP_IPV4 && LWIP_IPV6 - ip_addr_t local_ip_tmp; -#endif /* LWIP_IPV4 && LWIP_IPV6 */ if ((pcb == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, ipaddr)) { return ERR_VAL; @@ -778,7 +775,7 @@ tcp_connect(struct tcp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port, /* no local IP address set, yet. */ struct netif *netif; const ip_addr_t *local_ip; - ip_route_get_local_ip(PCB_ISIPV6(pcb), &pcb->local_ip, &pcb->remote_ip, netif, local_ip, &local_ip_tmp); + ip_route_get_local_ip(PCB_ISIPV6(pcb), &pcb->local_ip, &pcb->remote_ip, netif, local_ip); if ((netif == NULL) || (local_ip == NULL)) { /* Don't even try to send a SYN packet if we have no route since that will fail. */ diff --git a/src/core/tcp_out.c b/src/core/tcp_out.c index c072d360..91c11275 100644 --- a/src/core/tcp_out.c +++ b/src/core/tcp_out.c @@ -1198,13 +1198,11 @@ tcp_output_segment(struct tcp_seg *seg, struct tcp_pcb *pcb) /* If we don't have a local IP address, we get one from netif */ if (ip_addr_isany(&pcb->local_ip)) { const ip_addr_t *local_ip = ip_netif_get_local_ip(PCB_ISIPV6(pcb), netif, - &pcb->remote_ip, &pcb->local_ip); + &pcb->remote_ip); if (local_ip == NULL) { return ERR_RTE; } -#if !LWIP_IPV4 || !LWIP_IPV6 ip_addr_copy(pcb->local_ip, *local_ip); -#endif /* !LWIP_IPV4 || !LWIP_IPV6 */ } if (pcb->rttest == 0) { diff --git a/src/core/udp.c b/src/core/udp.c index b3cdfe63..1b3aba6e 100644 --- a/src/core/udp.c +++ b/src/core/udp.c @@ -596,7 +596,8 @@ udp_sendto_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip, in pcb->multicast_ip that is used for routing. */ if (!ip4_addr_isany(&pcb->multicast_ip) && !ip4_addr_cmp(&pcb->multicast_ip, IP4_ADDR_BROADCAST)) { - dst_ip_route = ip4_2_ip(&pcb->multicast_ip, &dst_ip_tmp); + ip_addr_copy_from_ip4(dst_ip_tmp, pcb->multicast_ip); + dst_ip_route = &dst_ip_tmp; } #endif /* LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS */ } @@ -656,9 +657,6 @@ udp_sendto_if_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_i { #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */ const ip_addr_t *src_ip; -#if LWIP_IPV6 && LWIP_IPV4 - ip_addr_t src_ip_tmp; -#endif /* LWIP_IPV6 && LWIP_IPV4 */ if ((pcb == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) { return ERR_VAL; @@ -668,7 +666,7 @@ udp_sendto_if_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_i #if LWIP_IPV6 if (PCB_ISIPV6(pcb)) { if (ip6_addr_isany(ip_2_ip6(&pcb->local_ip))) { - src_ip = ip6_2_ip(ip6_select_source_address(netif, ip_2_ip6_c(dst_ip)), &src_ip_tmp); + src_ip = ip6_select_source_address(netif, ip_2_ip6_c(dst_ip)); if (src_ip == NULL) { /* No suitable source address was found. */ return ERR_RTE; @@ -1056,7 +1054,7 @@ udp_connect(struct udp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port) /** TODO: this will bind the udp pcb locally, to the interface which is used to route output packets to the remote address. However, we might want to accept incoming packets on any interface! */ - ip_netif_get_local_ip(PCB_ISIPV6(pcb), netif, &pcb->remote_ip, &pcb->local_ip); + ip_addr_set(&pcb->local_ip, ip_netif_get_local_ip(PCB_ISIPV6(pcb), netif, &pcb->remote_ip)); } else if (ip_addr_isany(&pcb->remote_ip)) { ip_addr_set_any(PCB_ISIPV6(pcb), &pcb->local_ip); } diff --git a/src/include/lwip/ip.h b/src/include/lwip/ip.h index 5f439d6b..73861fc4 100644 --- a/src/include/lwip/ip.h +++ b/src/include/lwip/ip.h @@ -263,9 +263,9 @@ extern struct ip_globals ip_data; ((isipv6) ? \ ip6_route(ip_2_ip6_c(src), ip_2_ip6_c(dest)) : \ ip4_route_src(ip_2_ip4_c(dest), ip_2_ip4_c(src))) -#define ip_netif_get_local_ip(isipv6, netif, dest, storage) ((isipv6) ? \ - ip6_2_ip(ip6_netif_get_local_ip(netif, ip_2_ip6_c(dest)), storage) : \ - ip4_2_ip(ip4_netif_get_local_ip(netif), storage)) +#define ip_netif_get_local_ip(isipv6, netif, dest) ((isipv6) ? \ + ip6_netif_get_local_ip(netif, ip_2_ip6_c(dest)) : \ + ip4_netif_get_local_ip(netif)) #define ip_debug_print(is_ipv6, p) ((is_ipv6) ? ip6_debug_print(p) : ip4_debug_print(p)) #elif LWIP_IPV4 /* LWIP_IPV4 && LWIP_IPV6 */ #define ip_output(isipv6, p, src, dest, ttl, tos, proto) \ @@ -278,7 +278,7 @@ extern struct ip_globals ip_data; ip4_output_hinted(p, src, dest, ttl, tos, proto, addr_hint) #define ip_route(isipv6, src, dest) \ ip4_route_src(dest, src) -#define ip_netif_get_local_ip(isipv6, netif, dest, storage) \ +#define ip_netif_get_local_ip(isipv6, netif, dest) \ ip4_netif_get_local_ip(netif) #define ip_debug_print(is_ipv6, p) ip4_debug_print(p) #elif LWIP_IPV6 /* LWIP_IPV4 && LWIP_IPV6 */ @@ -292,14 +292,14 @@ extern struct ip_globals ip_data; ip6_output_hinted(p, src, dest, ttl, tos, proto, addr_hint) #define ip_route(isipv6, src, dest) \ ip6_route(src, dest) -#define ip_netif_get_local_ip(isipv6, netif, dest, storage) \ +#define ip_netif_get_local_ip(isipv6, netif, dest) \ ip6_netif_get_local_ip(netif, dest) #define ip_debug_print(is_ipv6, p) ip6_debug_print(p) #endif /* LWIP_IPV6 */ -#define ip_route_get_local_ip(isipv6, src, dest, netif, ipaddr, storage) do { \ +#define ip_route_get_local_ip(isipv6, src, dest, netif, ipaddr) do { \ (netif) = ip_route(isipv6, src, dest); \ - (ipaddr) = ip_netif_get_local_ip(isipv6, netif, dest, storage); \ + (ipaddr) = ip_netif_get_local_ip(isipv6, netif, dest); \ }while(0) err_t ip_input(struct pbuf *p, struct netif *inp); diff --git a/src/include/lwip/ip4.h b/src/include/lwip/ip4.h index 8a06c4e6..c92e632d 100644 --- a/src/include/lwip/ip4.h +++ b/src/include/lwip/ip4.h @@ -143,7 +143,7 @@ err_t ip4_output_if_opt_src(struct pbuf *p, const ip4_addr_t *src, const ip4_add void ip4_set_default_multicast_netif(struct netif* default_multicast_netif); #endif /* LWIP_MULTICAST_TX_OPTIONS */ -#define ip4_netif_get_local_ip(netif) (((netif) != NULL) ? netif_ip4_addr(netif) : NULL) +#define ip4_netif_get_local_ip(netif) (((netif) != NULL) ? &((netif)->ip_addr) : NULL) #if IP_DEBUG void ip4_debug_print(struct pbuf *p); diff --git a/src/include/lwip/ip6.h b/src/include/lwip/ip6.h index 012302ef..248eb02d 100644 --- a/src/include/lwip/ip6.h +++ b/src/include/lwip/ip6.h @@ -162,7 +162,7 @@ PACK_STRUCT_END struct netif *ip6_route(const ip6_addr_t *src, const ip6_addr_t *dest); -const ip6_addr_t *ip6_select_source_address(struct netif *netif, const ip6_addr_t * dest); +const ip_addr_t *ip6_select_source_address(struct netif *netif, const ip6_addr_t * dest); err_t ip6_input(struct pbuf *p, struct netif *inp); err_t ip6_output(struct pbuf *p, const ip6_addr_t *src, const ip6_addr_t *dest, u8_t hl, u8_t tc, u8_t nexth); diff --git a/src/include/lwip/ip_addr.h b/src/include/lwip/ip_addr.h index 754d33d0..504d842c 100644 --- a/src/include/lwip/ip_addr.h +++ b/src/include/lwip/ip_addr.h @@ -68,10 +68,6 @@ typedef struct _ip_addr { #define IP_ADDR_PCB_VERSION_MATCH(pcb, ipaddr) (PCB_ISIPV6(pcb) == IP_IS_V6(ipaddr)) -/* Convert ipv4/ipv6 address to generic ip address. - Since source types do not contain the type field, a target storage need to be supplied. */ -ip_addr_t* ip4_2_ip(const ip4_addr_t *ip4addr, ip_addr_t* storage); -ip_addr_t* ip6_2_ip(const ip6_addr_t *ip6addr, ip_addr_t* storage); /* Convert generic ip address to specific protocol version */ #define ip_2_ip6(ipaddr) (&((ipaddr)->u_addr.ip6)) #define ip_2_ip6_c(ipaddr) ip_2_ip6(ipaddr) @@ -166,7 +162,6 @@ typedef ip4_addr_t ip_addr_t; #define IP_IS_V6(ipaddr) 0 #define IP_SET_TYPE_VAL(ipaddr, iptype) #define IP_SET_TYPE(ipaddr, iptype) -#define ip4_2_ip(ipaddr, unused) (ipaddr) #define ip_2_ip4(ipaddr) (ipaddr) #define ip_2_ip4_c(ipaddr) (ipaddr) #define IP_ADDR4(ipaddr,a,b,c,d) IP4_ADDR(ipaddr,a,b,c,d) @@ -205,7 +200,6 @@ typedef ip6_addr_t ip_addr_t; #define IP_IS_V6(ipaddr) 1 #define IP_SET_TYPE_VAL(ipaddr, iptype) #define IP_SET_TYPE(ipaddr, iptype) -#define ip6_2_ip(ipaddr, unused) (ipaddr) #define ip_2_ip6(ipaddr) (ipaddr) #define ip_2_ip6_c(ipaddr) (ipaddr) #define IP_ADDR6(ipaddr,i0,i1,i2,i3) IP6_ADDR(ipaddr,i0,i1,i2,i3) diff --git a/src/include/lwip/netif.h b/src/include/lwip/netif.h index 02708856..a10cea91 100644 --- a/src/include/lwip/netif.h +++ b/src/include/lwip/netif.h @@ -398,6 +398,7 @@ void netif_poll_all(void); #endif /* ENABLE_LOOPBACK */ #if LWIP_IPV6 +#define netif_ip_addr6(netif, i) ((const ip_addr_t*)(&((netif)->ip6_addr[i]))) #define netif_ip6_addr(netif, i) (ip_2_ip6_c(&((netif)->ip6_addr[i]))) #define netif_ip6_addr_set(netif, i, addr6) do { ip6_addr_set(ip_2_ip6(&((netif)->ip6_addr[i])), addr6); IP_SET_TYPE_VAL((netif)->ip6_addr[i], IPADDR_TYPE_V6); } while(0) #define netif_ip6_addr_state(netif, i) ((netif)->ip6_addr_state[i])