From c1c65777b6cae48f83cd58a73d878379f03116de Mon Sep 17 00:00:00 2001 From: sg Date: Sun, 12 Apr 2015 10:43:46 +0200 Subject: [PATCH] worked on task #13480: added LWIP_IPV4 define - IPv4 can be disabled, leaving an IPv6-only stack (SNMP is still missing) --- CHANGELOG | 6 ++++++ src/api/api_msg.c | 4 ++-- src/api/netifapi.c | 18 ++++++++++++------ src/api/sockets.c | 34 ++++++++++++++++++++++++---------- src/core/ipv6/inet6.c | 2 +- src/core/netif.c | 15 +++++++++++---- src/core/udp.c | 24 +++++++++++++++--------- src/include/lwip/inet.h | 4 ++-- src/include/lwip/ip_addr.h | 5 +++++ src/include/lwip/netif.h | 17 +++++++---------- src/include/lwip/netifapi.h | 27 +++++++++++++-------------- 11 files changed, 98 insertions(+), 58 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index b936f1d1..d166da3f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -6,12 +6,18 @@ HISTORY ++ New features: + 2015-04-10: Simon Goldschmidt + * many files: task #13480: added LWIP_IPV4 define - IPv4 can be disabled, + leaving an IPv6-only stack + + 2015-04-09: Simon Goldschmidt * nearly all files: 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 + 2015-03-24: Simon Goldschmidt * opt.h, ip4_addr.h, ip4.c, ip6.c: loopif is not required for loopback traffic any more but passed through any netif (ENABLE_LOOPBACK has to be enabled) diff --git a/src/api/api_msg.c b/src/api/api_msg.c index a089033a..7035cdba 100644 --- a/src/api/api_msg.c +++ b/src/api/api_msg.c @@ -556,13 +556,13 @@ pcb_new(struct api_msg_msg *msg) if (msg->conn->pcb.ip == NULL) { msg->err = ERR_MEM; } -#if LWIP_IPV6 +#if LWIP_IPV4 && LWIP_IPV6 else { if (NETCONNTYPE_ISIPV6(msg->conn->type)) { ip_set_v6(msg->conn->pcb.ip, 1); } } -#endif /* LWIP_IPV6 */ +#endif /* LWIP_IPV4 && LWIP_IPV6 */ } /** diff --git a/src/api/netifapi.c b/src/api/netifapi.c index 2ff1c6fe..4c3a46e7 100644 --- a/src/api/netifapi.c +++ b/src/api/netifapi.c @@ -51,9 +51,11 @@ static void netifapi_do_netif_add(struct netifapi_msg_msg *msg) { if (!netif_add( msg->netif, +#if LWIP_IPV4 API_EXPR_REF(msg->msg.add.ipaddr), API_EXPR_REF(msg->msg.add.netmask), API_EXPR_REF(msg->msg.add.gw), +#endif /* LWIP_IPV4 */ msg->msg.add.state, msg->msg.add.init, msg->msg.add.input)) { @@ -64,6 +66,7 @@ netifapi_do_netif_add(struct netifapi_msg_msg *msg) TCPIP_NETIFAPI_ACK(msg); } +#if LWIP_IPV4 /** * Call netif_set_addr() inside the tcpip_thread context. */ @@ -77,6 +80,7 @@ netifapi_do_netif_set_addr(struct netifapi_msg_msg *msg) msg->err = ERR_OK; TCPIP_NETIFAPI_ACK(msg); } +#endif /* LWIP_IPV4 */ /** * Call the "errtfunc" (or the "voidfunc" if "errtfunc" is NULL) inside the @@ -102,12 +106,10 @@ netifapi_do_netif_common(struct netifapi_msg_msg *msg) */ err_t netifapi_netif_add(struct netif *netif, - const ip4_addr_t *ipaddr, - const ip4_addr_t *netmask, - const ip4_addr_t *gw, - void *state, - netif_init_fn init, - netif_input_fn input) +#if LWIP_IPV4 + const ip4_addr_t *ipaddr, const ip4_addr_t *netmask, const ip4_addr_t *gw, +#endif /* LWIP_IPV4 */ + void *state, netif_init_fn init, netif_input_fn input) { err_t err; NETIFAPI_VAR_DECLARE(msg); @@ -125,9 +127,11 @@ netifapi_netif_add(struct netif *netif, #endif /* LWIP_MPU_COMPATIBLE */ NETIFAPI_VAR_REF(msg).function = netifapi_do_netif_add; NETIFAPI_VAR_REF(msg).msg.netif = netif; +#if LWIP_IPV4 NETIFAPI_VAR_REF(msg).msg.msg.add.ipaddr = NETIFAPI_VAR_REF(ipaddr); NETIFAPI_VAR_REF(msg).msg.msg.add.netmask = NETIFAPI_VAR_REF(netmask); NETIFAPI_VAR_REF(msg).msg.msg.add.gw = NETIFAPI_VAR_REF(gw); +#endif /* LWIP_IPV4 */ NETIFAPI_VAR_REF(msg).msg.msg.add.state = state; NETIFAPI_VAR_REF(msg).msg.msg.add.init = init; NETIFAPI_VAR_REF(msg).msg.msg.add.input = input; @@ -138,6 +142,7 @@ netifapi_netif_add(struct netif *netif, return err; } +#if LWIP_IPV4 /** * Call netif_set_addr() in a thread-safe way by running that function inside the * tcpip_thread context. @@ -175,6 +180,7 @@ netifapi_netif_set_addr(struct netif *netif, NETIFAPI_VAR_FREE(msg); return err; } +#endif /* LWIP_IPV4 */ /** * call the "errtfunc" (or the "voidfunc" if "errtfunc" is NULL) in a thread-safe diff --git a/src/api/sockets.c b/src/api/sockets.c index 413684d3..93547119 100644 --- a/src/api/sockets.c +++ b/src/api/sockets.c @@ -71,6 +71,7 @@ #define LWIP_NETCONN 0 #endif +#if LWIP_IPV4 #define IP4ADDR_PORT_TO_SOCKADDR(sin, ipaddr, port) do { \ (sin)->sin_len = sizeof(struct sockaddr_in); \ (sin)->sin_family = AF_INET; \ @@ -80,8 +81,21 @@ #define SOCKADDR4_TO_IP4ADDR_PORT(sin, ipaddr, port) do { \ inet_addr_to_ipaddr(ip_2_ip4(ipaddr), &((sin)->sin_addr)); \ (port) = ntohs((sin)->sin_port); }while(0) +#endif /* LWIP_IPV4 */ #if LWIP_IPV6 +#define IP6ADDR_PORT_TO_SOCKADDR(sin6, ipaddr, port) do { \ + (sin6)->sin6_len = sizeof(struct sockaddr_in6); \ + (sin6)->sin6_family = AF_INET6; \ + (sin6)->sin6_port = htons((port)); \ + (sin6)->sin6_flowinfo = 0; \ + inet6_addr_from_ip6addr(&(sin6)->sin6_addr, ipaddr); }while(0) +#define SOCKADDR6_TO_IP6ADDR_PORT(sin6, ipaddr, port) do { \ + inet6_addr_to_ip6addr(ip_2_ip6(ipaddr), &((sin6)->sin6_addr)); \ + (port) = ntohs((sin6)->sin6_port); }while(0) +#endif /* LWIP_IPV6 */ + +#if LWIP_IPV4 && LWIP_IPV6 #define IS_SOCK_ADDR_LEN_VALID(namelen) (((namelen) == sizeof(struct sockaddr_in)) || \ ((namelen) == sizeof(struct sockaddr_in6))) #define IS_SOCK_ADDR_TYPE_VALID(name) (((name)->sa_family == AF_INET) || \ @@ -89,21 +103,12 @@ #define SOCK_ADDR_TYPE_MATCH(name, sock) \ ((((name)->sa_family == AF_INET) && !(NETCONNTYPE_ISIPV6((sock)->conn->type))) || \ (((name)->sa_family == AF_INET6) && (NETCONNTYPE_ISIPV6((sock)->conn->type)))) -#define IP6ADDR_PORT_TO_SOCKADDR(sin6, ipaddr, port) do { \ - (sin6)->sin6_len = sizeof(struct sockaddr_in6); \ - (sin6)->sin6_family = AF_INET6; \ - (sin6)->sin6_port = htons((port)); \ - (sin6)->sin6_flowinfo = 0; \ - inet6_addr_from_ip6addr(&(sin6)->sin6_addr, ipaddr); }while(0) #define IPADDR_PORT_TO_SOCKADDR(sockaddr, ipaddr, port) do { \ if (IP_IS_V6(ipaddr)) { \ IP6ADDR_PORT_TO_SOCKADDR((struct sockaddr_in6*)(void*)(sockaddr), ip_2_ip6(ipaddr), port); \ } else { \ IP4ADDR_PORT_TO_SOCKADDR((struct sockaddr_in*)(void*)(sockaddr), ip_2_ip4(ipaddr), port); \ } } while(0) -#define SOCKADDR6_TO_IP6ADDR_PORT(sin6, ipaddr, port) do { \ - inet6_addr_to_ip6addr(ip_2_ip6(ipaddr), &((sin6)->sin6_addr)); \ - (port) = ntohs((sin6)->sin6_port); }while(0) #define SOCKADDR_TO_IPADDR_PORT(sockaddr, ipaddr, port) do { \ if (((sockaddr)->sa_family) == AF_INET6) { \ SOCKADDR6_TO_IP6ADDR_PORT((struct sockaddr_in6*)(void*)(sockaddr), ipaddr, port); \ @@ -112,7 +117,16 @@ } } while(0) #define DOMAIN_TO_NETCONN_TYPE(domain, type) (((domain) == AF_INET) ? \ (type) : (enum netconn_type)((type) | NETCONN_TYPE_IPV6)) -#else /* LWIP_IPV6 */ +#elif LWIP_IPV6 /* LWIP_IPV4 && LWIP_IPV6 */ +#define IS_SOCK_ADDR_LEN_VALID(namelen) ((namelen) == sizeof(struct sockaddr_in6)) +#define IS_SOCK_ADDR_TYPE_VALID(name) ((name)->sa_family == AF_INET6) +#define SOCK_ADDR_TYPE_MATCH(name, sock) 1 +#define IPADDR_PORT_TO_SOCKADDR(sockaddr, ipaddr, port) \ + IP6ADDR_PORT_TO_SOCKADDR((struct sockaddr_in6*)(void*)(sockaddr), ip_2_ip6(ipaddr), port) +#define SOCKADDR_TO_IPADDR_PORT(sockaddr, ipaddr, port) \ + SOCKADDR6_TO_IP6ADDR_PORT((struct sockaddr_in6*)(void*)(sockaddr), ipaddr, port) +#define DOMAIN_TO_NETCONN_TYPE(domain, netconn_type) (netconn_type) +#else /*-> LWIP_IPV4: LWIP_IPV4 && LWIP_IPV6 */ #define IS_SOCK_ADDR_LEN_VALID(namelen) ((namelen) == sizeof(struct sockaddr_in)) #define IS_SOCK_ADDR_TYPE_VALID(name) ((name)->sa_family == AF_INET) #define SOCK_ADDR_TYPE_MATCH(name, sock) 1 diff --git a/src/core/ipv6/inet6.c b/src/core/ipv6/inet6.c index 1ca59c32..9ed6bb4d 100644 --- a/src/core/ipv6/inet6.c +++ b/src/core/ipv6/inet6.c @@ -48,6 +48,6 @@ /** This variable is initialized by the system to contain the wildcard IPv6 address. */ -const struct ip6_addr in6addr_any = IN6ADDR_ANY_INIT; +const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; #endif /* LWIP_IPV6 */ diff --git a/src/core/netif.c b/src/core/netif.c index 996d9cce..dbf387cc 100644 --- a/src/core/netif.c +++ b/src/core/netif.c @@ -94,7 +94,9 @@ static err_t netif_null_output_ip6(struct netif *netif, struct pbuf *p, const ip #endif /* LWIP_IPV6 */ #if LWIP_HAVE_LOOPIF +#if LWIP_IPV4 static err_t netif_loop_output_ipv4(struct netif *netif, struct pbuf *p, const ip4_addr_t* addr); +#endif #if LWIP_IPV6 static err_t netif_loop_output_ipv6(struct netif *netif, struct pbuf *p, const ip6_addr_t* addr); #endif @@ -119,7 +121,9 @@ netif_loopif_init(struct netif *netif) netif->name[0] = 'l'; netif->name[1] = 'o'; +#if LWIP_IPV4 netif->output = netif_loop_output_ipv4; +#endif #if LWIP_IPV6 netif->output_ip6 = netif_loop_output_ipv6; #endif @@ -131,15 +135,20 @@ void netif_init(void) { #if LWIP_HAVE_LOOPIF +#if LWIP_IPV4 +#define LOOPIF_ADDRINIT &loop_ipaddr, &loop_netmask, &loop_gw, ip4_addr_t loop_ipaddr, loop_netmask, loop_gw; IP4_ADDR(&loop_gw, 127,0,0,1); IP4_ADDR(&loop_ipaddr, 127,0,0,1); IP4_ADDR(&loop_netmask, 255,0,0,0); +#else /* LWIP_IPV4 */ +#define LOOPIF_ADDRINIT +#endif /* LWIP_IPV4 */ #if NO_SYS - netif_add(&loop_netif, &loop_ipaddr, &loop_netmask, &loop_gw, NULL, netif_loopif_init, ip_input); + netif_add(&loop_netif, LOOPIF_ADDRINIT NULL, netif_loopif_init, ip_input); #else /* NO_SYS */ - netif_add(&loop_netif, &loop_ipaddr, &loop_netmask, &loop_gw, NULL, netif_loopif_init, tcpip_input); + netif_add(&loop_netif, LOOPIF_ADDRINIT NULL, netif_loopif_init, tcpip_input); #endif /* NO_SYS */ #if LWIP_IPV6 @@ -173,8 +182,6 @@ struct netif * netif_add(struct netif *netif, #if LWIP_IPV4 const ip4_addr_t *ipaddr, const ip4_addr_t *netmask, const ip4_addr_t *gw, -#else /* LWIP_IPV4 */ - const void *ipaddr, const void *netmask, const void *gw, #endif /* LWIP_IPV4 */ void *state, netif_init_fn init, netif_input_fn input) { diff --git a/src/core/udp.c b/src/core/udp.c index 90fe2fdf..a51a1731 100644 --- a/src/core/udp.c +++ b/src/core/udp.c @@ -238,17 +238,21 @@ udp_input(struct pbuf *p, struct netif *inp) if (pcb->local_port == dest) { if ( #if LWIP_IPV6 - ((PCB_ISIPV6(pcb) && (ip_current_is_v6()) && + (PCB_ISIPV6(pcb) && (ip_current_is_v6()) && (ip6_addr_isany(ip_2_ip6(&pcb->local_ip)) || #if LWIP_IPV6_MLD ip6_addr_ismulticast(ip6_current_dest_addr()) || #endif /* LWIP_IPV6_MLD */ - ip6_addr_cmp(ip_2_ip6(&pcb->local_ip), ip6_current_dest_addr()))) || - (!PCB_ISIPV6(pcb) && - (ip4_current_header() != NULL) && -#else /* LWIP_IPV6 */ - (( + ip6_addr_cmp(ip_2_ip6(&pcb->local_ip), ip6_current_dest_addr()))) #endif /* LWIP_IPV6 */ +#if LWIP_IPV4 && LWIP_IPV6 + || (!PCB_ISIPV6(pcb) && + (ip4_current_header() != NULL) && +#endif /* LWIP_IPV4 && LWIP_IPV6 */ +#if LWIP_IPV4 +#if !LWIP_IPV6 + ( +#endif /* !LWIP_IPV6 */ ((!broadcast && ip_addr_isany(&pcb->local_ip)) || ip_addr_cmp(&pcb->local_ip, ip_current_dest_addr()) || #if LWIP_IGMP @@ -257,12 +261,14 @@ udp_input(struct pbuf *p, struct netif *inp) #if IP_SOF_BROADCAST_RECV (broadcast && ip_get_option(pcb, SOF_BROADCAST) && (ip_addr_isany(&pcb->local_ip) || - ip_addr_netcmp(&pcb->local_ip, ip_current_dest_addr(), &inp->netmask))))))) { + ip_addr_netcmp(&pcb->local_ip, ip_current_dest_addr(), &inp->netmask))))) #else /* IP_SOF_BROADCAST_RECV */ (broadcast && (ip_addr_isany(&pcb->local_ip) || - ip_addr_netcmp(&pcb->local_ip, ip_current_dest_addr(), &inp->netmask))))))) { -#endif /* IP_SOF_BROADCAST_RECV */ + ip_addr_netcmp(&pcb->local_ip, ip_current_dest_addr(), &inp->netmask))))) +#endif /* IP_SOF_BROADCAST_RECV */ +#endif /* LWIP_IPV4 */ + ) { local_match = 1; if ((uncon_pcb == NULL) && ((pcb->flags & UDP_FLAGS_CONNECTED) == 0)) { diff --git a/src/include/lwip/inet.h b/src/include/lwip/inet.h index eb905ac5..e7f976b4 100644 --- a/src/include/lwip/inet.h +++ b/src/include/lwip/inet.h @@ -61,8 +61,8 @@ struct in_addr { struct in6_addr { union { - u8_t u8_addr[16]; u32_t u32_addr[4]; + u8_t u8_addr[16]; } un; #define s6_addr un.u8_addr }; @@ -83,7 +83,7 @@ struct in6_addr { to the IPv6 loopback address. */ #define IN6ADDR_LOOPBACK_INIT {0,0,0,PP_HTONL(1)} /** This variable is initialized by the system to contain the wildcard IPv6 address. */ -extern const struct ip6_addr in6addr_any; +extern const struct in6_addr in6addr_any; /* Definitions of the bits in an (IPv4) Internet address integer. diff --git a/src/include/lwip/ip_addr.h b/src/include/lwip/ip_addr.h index c884eaa0..68b63434 100644 --- a/src/include/lwip/ip_addr.h +++ b/src/include/lwip/ip_addr.h @@ -250,6 +250,11 @@ extern const ip_addr_t ip6_addr_any; */ #define IP6_ADDR_ANY6 ((ip6_addr_t*)ip_2_ip6(&ip6_addr_any)) +#if !LWIP_IPV4 +/** Just a little upgrade-helper for IPv6-only configurations: */ +#define IP_ADDR_ANY IP6_ADDR_ANY +#endif /* !LWIP_IPV4 */ + #endif #ifdef __cplusplus diff --git a/src/include/lwip/netif.h b/src/include/lwip/netif.h index 02a8174b..36e65d52 100644 --- a/src/include/lwip/netif.h +++ b/src/include/lwip/netif.h @@ -322,17 +322,14 @@ extern struct netif *netif_default; void netif_init(void); +struct netif *netif_add(struct netif *netif, #if LWIP_IPV4 -struct netif * -netif_add(struct netif *netif, const ip4_addr_t *ipaddr, const ip4_addr_t *netmask, - const ip4_addr_t *gw, void *state, netif_init_fn init, netif_input_fn input); -void -netif_set_addr(struct netif *netif, const ip4_addr_t *ipaddr, const ip4_addr_t *netmask, - const ip4_addr_t *gw); -#else /* LWIP_IPV4 */ -struct netif * -netif_add(struct netif *netif, const void *ipaddr, const void *netmask, - const void *gw, void *state, netif_init_fn init, netif_input_fn input); + const ip4_addr_t *ipaddr, const ip4_addr_t *netmask, const ip4_addr_t *gw, +#endif /* LWIP_IPV4 */ + void *state, netif_init_fn init, netif_input_fn input); +#if LWIP_IPV4 +void netif_set_addr(struct netif *netif, const ip4_addr_t *ipaddr, const ip4_addr_t *netmask, + const ip4_addr_t *gw); #endif /* LWIP_IPV4 */ void netif_remove(struct netif * netif); diff --git a/src/include/lwip/netifapi.h b/src/include/lwip/netifapi.h index 26c30a34..5a74d9ee 100644 --- a/src/include/lwip/netifapi.h +++ b/src/include/lwip/netifapi.h @@ -58,9 +58,11 @@ struct netifapi_msg_msg { struct netif *netif; union { struct { +#if LWIP_IPV4 NETIFAPI_IPADDR_DEF(ip4_addr_t, ipaddr); NETIFAPI_IPADDR_DEF(ip4_addr_t, netmask); NETIFAPI_IPADDR_DEF(ip4_addr_t, gw); +#endif /* LWIP_IPV4 */ void *state; netif_init_fn init; netif_input_fn input; @@ -79,22 +81,19 @@ struct netifapi_msg { /* API for application */ -err_t netifapi_netif_add ( struct netif *netif, - const ip4_addr_t *ipaddr, - const ip4_addr_t *netmask, - const ip4_addr_t *gw, - void *state, - netif_init_fn init, - netif_input_fn input); +err_t netifapi_netif_add(struct netif *netif, +#if LWIP_IPV4 + const ip4_addr_t *ipaddr, const ip4_addr_t *netmask, const ip4_addr_t *gw, +#endif /* LWIP_IPV4 */ + void *state, netif_init_fn init, netif_input_fn input); -err_t netifapi_netif_set_addr ( struct netif *netif, - const ip4_addr_t *ipaddr, - const ip4_addr_t *netmask, - const ip4_addr_t *gw ); +#if LWIP_IPV4 +err_t netifapi_netif_set_addr(struct netif *netif, const ip4_addr_t *ipaddr, + const ip4_addr_t *netmask, const ip4_addr_t *gw); +#endif /* LWIP_IPV4*/ -err_t netifapi_netif_common ( struct netif *netif, - netifapi_void_fn voidfunc, - netifapi_errt_fn errtfunc); +err_t netifapi_netif_common(struct netif *netif, netifapi_void_fn voidfunc, + netifapi_errt_fn errtfunc); #define netifapi_netif_remove(n) netifapi_netif_common(n, netif_remove, NULL) #define netifapi_netif_set_up(n) netifapi_netif_common(n, netif_set_up, NULL)