mirror of
https://git.savannah.nongnu.org/git/lwip.git
synced 2026-08-10 17:43:39 +08:00
task #12722 (improve IPv4/v6 address handling): renamed ip_addr_t to ip4_addr_t, renamed ipX_addr_t to ip_addr_t and added IP version;
ip_addr_t is used for all generic IP addresses for the API, ip(4/6)_addr_t are only used internally or when initializing netifs or when calling version-related functions
This commit is contained in:
@@ -41,10 +41,10 @@
|
||||
|
||||
#include "lwip/opt.h"
|
||||
|
||||
#if LWIP_IPV6_DHCP6 /* don't build if not configured for use in lwipopts.h */
|
||||
#if LWIP_IPV6 && LWIP_IPV6_DHCP6 /* don't build if not configured for use in lwipopts.h */
|
||||
|
||||
#include "lwip/ip6_addr.h"
|
||||
#include "lwip/def.h"
|
||||
|
||||
|
||||
#endif /* LWIP_IPV6_DHCP6 */
|
||||
#endif /* LWIP_IPV6 && LWIP_IPV6_DHCP6 */
|
||||
|
||||
@@ -44,8 +44,10 @@
|
||||
#if LWIP_IPV6 && LWIP_SOCKET /* don't build if not configured for use in lwipopts.h */
|
||||
|
||||
#include "lwip/def.h"
|
||||
#include "lwip/inet6.h"
|
||||
#include "lwip/inet.h"
|
||||
|
||||
/** @see ip6_addr.c for implementation of functions. */
|
||||
/** This variable is initialized by the system to contain the wildcard IPv6 address.
|
||||
*/
|
||||
const struct ip6_addr in6addr_any = IN6ADDR_ANY_INIT;
|
||||
|
||||
#endif /* LWIP_IPV6 */
|
||||
|
||||
@@ -60,6 +60,10 @@
|
||||
#include "lwip/debug.h"
|
||||
#include "lwip/stats.h"
|
||||
|
||||
#if !LWIP_IPV4
|
||||
/** Global data for IPv6 only */
|
||||
struct ip_globals ip_data;
|
||||
#endif /* !LWIP_IPV4 */
|
||||
|
||||
/**
|
||||
* Finds the appropriate network interface for a given IPv6 address. It tries to select
|
||||
@@ -298,7 +302,7 @@ ip6_forward(struct pbuf *p, struct ip6_hdr *iphdr, struct netif *inp)
|
||||
}
|
||||
|
||||
/* Find network interface where to forward this IP packet to. */
|
||||
netif = ip6_route(IP6_ADDR_ANY, ip6_current_dest_addr());
|
||||
netif = ip6_route(IP6_ADDR_ANY6, ip6_current_dest_addr());
|
||||
if (netif == NULL) {
|
||||
LWIP_DEBUGF(IP6_DEBUG, ("ip6_forward: no route for %"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F"\n",
|
||||
IP6_ADDR_BLOCK1(ip6_current_dest_addr()),
|
||||
@@ -443,8 +447,8 @@ ip6_input(struct pbuf *p, struct netif *inp)
|
||||
pbuf_realloc(p, IP6_HLEN + IP6H_PLEN(ip6hdr));
|
||||
|
||||
/* copy IP addresses to aligned ip6_addr_t */
|
||||
ip6_addr_copy(ip_data.current_iphdr_dest.ip6, ip6hdr->dest);
|
||||
ip6_addr_copy(ip_data.current_iphdr_src.ip6, ip6hdr->src);
|
||||
ip_addr_copy_from_ip6(ip_data.current_iphdr_dest, ip6hdr->dest);
|
||||
ip_addr_copy_from_ip6(ip_data.current_iphdr_src, ip6hdr->src);
|
||||
|
||||
/* current header pointer. */
|
||||
ip_data.current_ip6_header = ip6hdr;
|
||||
@@ -764,8 +768,8 @@ ip6_input_cleanup:
|
||||
ip_data.current_input_netif = NULL;
|
||||
ip_data.current_ip6_header = NULL;
|
||||
ip_data.current_ip_header_tot_len = 0;
|
||||
ip6_addr_set_any(&ip_data.current_iphdr_src.ip6);
|
||||
ip6_addr_set_any(&ip_data.current_iphdr_dest.ip6);
|
||||
ip6_addr_set_zero(ip6_current_src_addr());
|
||||
ip6_addr_set_zero(ip6_current_dest_addr());
|
||||
|
||||
return ERR_OK;
|
||||
}
|
||||
@@ -852,7 +856,7 @@ ip6_output_if_src(struct pbuf *p, const ip6_addr_t *src, const ip6_addr_t *dest,
|
||||
IP6H_PLEN_SET(ip6hdr, p->tot_len - IP6_HLEN);
|
||||
|
||||
if (src == NULL) {
|
||||
src = IP6_ADDR_ANY;
|
||||
src = IP6_ADDR_ANY6;
|
||||
}
|
||||
/* src cannot be NULL here */
|
||||
ip6_addr_copy(ip6hdr->src, *src);
|
||||
@@ -975,7 +979,7 @@ ip6_output(struct pbuf *p, const ip6_addr_t *src, const ip6_addr_t *dest,
|
||||
* see ip_output_if() for more return values
|
||||
*/
|
||||
err_t
|
||||
ip6_output_hinted(struct pbuf *p, ip6_addr_t *src, ip6_addr_t *dest,
|
||||
ip6_output_hinted(struct pbuf *p, const ip6_addr_t *src, const ip6_addr_t *dest,
|
||||
u8_t hl, u8_t tc, u8_t nexth, u8_t *addr_hint)
|
||||
{
|
||||
struct netif *netif;
|
||||
|
||||
@@ -44,11 +44,17 @@
|
||||
|
||||
#if LWIP_IPV6 /* don't build if not configured for use in lwipopts.h */
|
||||
|
||||
#include "lwip/ip6_addr.h"
|
||||
#include "lwip/ip_addr.h"
|
||||
#include "lwip/def.h"
|
||||
|
||||
/* used by IP6_ADDR_ANY in ip6_addr.h */
|
||||
const ip6_addr_t ip6_addr_any = { { 0ul, 0ul, 0ul, 0ul } };
|
||||
/* used by IP6_ADDR_ANY(6) in ip6_addr.h */
|
||||
const ip_addr_t ip6_addr_any = {
|
||||
#if LWIP_IPV4
|
||||
{ 0ul }, IPADDR_TYPE_V6
|
||||
#else
|
||||
0ul, 0ul, 0ul, 0ul
|
||||
#endif
|
||||
};
|
||||
|
||||
#ifndef isprint
|
||||
#define in_range(c, lo, up) ((u8_t)c >= lo && (u8_t)c <= up)
|
||||
@@ -248,4 +254,53 @@ ip6addr_ntoa_r(const ip6_addr_t *addr, char *buf, int buflen)
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
#if LWIP_IPV4
|
||||
/** Convert 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*
|
||||
ip6_2_ip(const ip6_addr_t *ip6addr, ip_addr_t* storage)
|
||||
{
|
||||
if ((ip6addr == NULL) || (storage == NULL)) {
|
||||
return NULL;
|
||||
}
|
||||
ip6_addr_copy(storage->addr.ip6, *ip6addr);
|
||||
IP_SET_TYPE_L(storage, IPADDR_TYPE_V6);
|
||||
return storage;
|
||||
}
|
||||
|
||||
/** Convert IP address string (both versions) to numeric.
|
||||
* The version is auto-detected from the string.
|
||||
*/
|
||||
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 dot: IPv4 address */
|
||||
if (addr) {
|
||||
IP_SET_TYPE_L(addr, IPADDR_TYPE_V4);
|
||||
}
|
||||
return ip4addr_aton(cp, ip_2_ip4(addr));
|
||||
} else if (*c == ':') {
|
||||
/* contains a colon: IPv6 address */
|
||||
if (addr) {
|
||||
IP_SET_TYPE_L(addr, IPADDR_TYPE_V6);
|
||||
}
|
||||
return ip6addr_aton(cp, ip_2_ip6(addr));
|
||||
}
|
||||
}
|
||||
/* nothing found, call ip4addr_aton as fallback */
|
||||
if (addr) {
|
||||
IP_SET_TYPE_L(addr, IPADDR_TYPE_V4);
|
||||
}
|
||||
return ip4addr_aton(cp, ip_2_ip4(addr));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif /* LWIP_IPV4 */
|
||||
|
||||
#endif /* LWIP_IPV6 */
|
||||
|
||||
@@ -551,7 +551,7 @@ mld6_send(struct mld_group *group, u8_t type)
|
||||
if (!ip6_addr_isvalid(netif_ip6_addr_state(group->netif, 0))) {
|
||||
/* This is a special case, when we are performing duplicate address detection.
|
||||
* We must join the multicast group, but we don't have a valid address yet. */
|
||||
src_addr = IP6_ADDR_ANY;
|
||||
src_addr = IP6_ADDR_ANY6;
|
||||
} else {
|
||||
/* Use link-local address as source address. */
|
||||
src_addr = netif_ip6_addr(group->netif, 0);
|
||||
|
||||
@@ -840,7 +840,7 @@ nd6_send_ns(struct netif * netif, const ip6_addr_t * target_addr, u8_t flags)
|
||||
/* Use link-local address as source address. */
|
||||
src_addr = netif_ip6_addr(netif, 0);
|
||||
} else {
|
||||
src_addr = IP6_ADDR_ANY;
|
||||
src_addr = IP6_ADDR_ANY6;
|
||||
}
|
||||
|
||||
/* Allocate a packet. */
|
||||
@@ -882,7 +882,7 @@ nd6_send_ns(struct netif * netif, const ip6_addr_t * target_addr, u8_t flags)
|
||||
|
||||
/* Send the packet out. */
|
||||
ND6_STATS_INC(nd6.xmit);
|
||||
ip6_output_if(p, (src_addr == IP6_ADDR_ANY) ? NULL : src_addr, target_addr,
|
||||
ip6_output_if(p, (src_addr == IP6_ADDR_ANY6) ? NULL : src_addr, target_addr,
|
||||
LWIP_ICMP6_HL, 0, IP6_NEXTH_ICMP6, netif);
|
||||
pbuf_free(p);
|
||||
}
|
||||
@@ -984,14 +984,14 @@ nd6_send_rs(struct netif * netif)
|
||||
src_addr = netif_ip6_addr(netif, 0);
|
||||
}
|
||||
else {
|
||||
src_addr = IP6_ADDR_ANY;
|
||||
src_addr = IP6_ADDR_ANY6;
|
||||
}
|
||||
|
||||
/* Generate the all routers target address. */
|
||||
ip6_addr_set_allrouters_linklocal(&multicast_address);
|
||||
|
||||
/* Allocate a packet. */
|
||||
if (src_addr != IP6_ADDR_ANY) {
|
||||
if (src_addr != IP6_ADDR_ANY6) {
|
||||
lladdr_opt_len = ((netif->hwaddr_len + 2) >> 3) + (((netif->hwaddr_len + 2) & 0x07) ? 1 : 0);
|
||||
}
|
||||
p = pbuf_alloc(PBUF_IP, sizeof(struct rs_header) + (lladdr_opt_len << 3), PBUF_RAM);
|
||||
@@ -1012,7 +1012,7 @@ nd6_send_rs(struct netif * netif)
|
||||
rs_hdr->chksum = 0;
|
||||
rs_hdr->reserved = 0;
|
||||
|
||||
if (src_addr != IP6_ADDR_ANY) {
|
||||
if (src_addr != IP6_ADDR_ANY6) {
|
||||
/* Include our hw address. */
|
||||
lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct rs_header));
|
||||
lladdr_opt->type = ND6_OPTION_TYPE_SOURCE_LLADDR;
|
||||
|
||||
Reference in New Issue
Block a user