: 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.
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;
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).
*
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, ...);