From 666243e5776fd4609ef81ec6781f9eb9d9999700 Mon Sep 17 00:00:00 2001 From: Gerrit Renker Date: Sun, 23 May 2010 07:43:04 +0200 Subject: [PATCH] net: generic code to query the maximum message size This adds a fallback routine to determine the address-family specific maximum message size (MMS). This value is an over-estimation of the maximum payload size that the network layer can take, i.e. it returns the maximum size for transport-layer header and transport-layer payload. --- net.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++--- net.h | 5 +++++ 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/net.c b/net.c index 59b7f367..fa9575a6 100644 --- a/net.c +++ b/net.c @@ -521,6 +521,14 @@ static socklen_t salen(const struct sockaddr *sa) : sizeof(struct sockaddr_in); } +/** True if @ss holds a v6-mapped-v4 address (RFC 4291, 2.5.5.2) */ +static bool SS_IS_ADDR_V4MAPPED(const struct sockaddr_storage *ss) +{ + const struct sockaddr_in6 *ia6 = (const struct sockaddr_in6 *)ss; + + return ss->ss_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&ia6->sin6_addr); +} + /** * Process IPv4/v6 address, turn v6-mapped-v4 address into normal IPv4 address. * \param ss Container of IPv4/6 address. @@ -531,11 +539,10 @@ static socklen_t salen(const struct sockaddr *sa) static const struct sockaddr * normalize_ip_address(const struct sockaddr_storage *ss) { - const struct sockaddr_in6 *ia6 = (const struct sockaddr_in6 *)ss; - assert(ss->ss_family == AF_INET || ss->ss_family == AF_INET6); - if (ss->ss_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&ia6->sin6_addr)) { + if (SS_IS_ADDR_V4MAPPED(ss)) { + const struct sockaddr_in6 *ia6 = (const struct sockaddr_in6 *)ss; static struct sockaddr_in ia; ia.sin_family = AF_INET; @@ -546,6 +553,46 @@ normalize_ip_address(const struct sockaddr_storage *ss) return (const struct sockaddr *)ss; } +/** + * Generic/fallback MTU values + * + * These are taken from RFC 1122, RFC 2460, and RFC 5405. + * - RFC 1122, 3.3.3 defines EMTU_S ("Effective MTU for sending") and recommends + * to use an EMTU_S size of of 576 bytes if the IPv4 path MTU is unknown; + * - RFC 2460, 5. requires a minimum IPv6 MTU of 1280 bytes; + * - RFC 5405, 3.2 recommends that if path MTU discovery is not done, + * UDP senders should use the respective minimum values of EMTU_S. + */ +static inline int generic_mtu(const int af_type) +{ + return af_type == AF_INET6 ? 1280 : 576; +} + +/** Crude approximation of IP header overhead - neglecting options. */ +static inline int estimated_header_overhead(const int af_type) +{ + return af_type == AF_INET6 ? 40 : 20; +} + +/** + * Maximum transport-layer message size (MMS_S) as per RFC 1122, 3.3.3 + * Socket must be connected. + */ +int generic_max_transport_msg_size(int sockfd) +{ + struct sockaddr_storage ss; + socklen_t sslen = sizeof(ss); + int af_type = AF_INET; + + if (getpeername(sockfd, (struct sockaddr *)&ss, &sslen) < 0) { + PARA_ERROR_LOG("can not determine remote address type: %s\n", + strerror(errno)); + } else if (!SS_IS_ADDR_V4MAPPED(&ss)) { + af_type = ss.ss_family; + } + return generic_mtu(af_type) - estimated_header_overhead(af_type); +} + /** * Print numeric host and port number (beware - uses static char). * diff --git a/net.h b/net.h index 457c24dc..db610318 100644 --- a/net.h +++ b/net.h @@ -132,6 +132,11 @@ static inline int para_listen_simple(unsigned l4type, uint16_t port) extern char *local_name(int sockfd); extern char *remote_name(int sockfd); +/** + * Determining maximum payload (packet) size + */ +extern int generic_max_transport_msg_size(int sockfd); + int send_bin_buffer(int, const char *, size_t); int send_buffer(int, const char *); __printf_2_3 int send_va_buffer(int fd, const char *fmt, ...); -- 2.39.2