net: generic code to query the maximum message size
authorGerrit Renker <grenker@cscs.ch>
Sun, 23 May 2010 05:43:04 +0000 (07:43 +0200)
committerAndre Noll <maan@systemlinux.org>
Mon, 7 Jun 2010 21:24:53 +0000 (23:24 +0200)
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
net.h

diff --git a/net.c b/net.c
index 59b7f367fb2f119e338d9411f2060a71b39209fc..fa9575a6e5a6aa7d857777e1ea85d5f52ec05c01 100644 (file)
--- a/net.c
+++ b/net.c
@@ -521,6 +521,14 @@ static socklen_t salen(const struct sockaddr *sa)
                : sizeof(struct sockaddr_in);
 }
 
                : 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.
 /**
  * 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)
 {
 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);
 
        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;
                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;
 }
 
        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).
  *
 /**
  * Print numeric host and port number (beware - uses static char).
  *
diff --git a/net.h b/net.h
index 457c24dc92380fca0e8af10c9e63781246c58604..db6103184b0f77f395af01e6fd959ad6812a6809 100644 (file)
--- 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);
 
 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, ...);
 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, ...);