diff options
| -rw-r--r-- | PICA_client.c | 7 | ||||
| -rw-r--r-- | PICA_media.c | 151 | ||||
| -rw-r--r-- | PICA_media.h | 62 | ||||
| -rw-r--r-- | PICA_netconf.c | 121 | ||||
| -rw-r--r-- | PICA_netconf.h | 12 | ||||
| -rw-r--r-- | PICA_nodewait.c | 10 | ||||
| -rw-r--r-- | pica-client/audiovideocallcontroller.cpp | 12 |
7 files changed, 358 insertions, 17 deletions
diff --git a/PICA_client.c b/PICA_client.c index 0339ef7..12dc708 100644 --- a/PICA_client.c +++ b/PICA_client.c @@ -2811,6 +2811,11 @@ static unsigned int procmsg_CALL_RECEPTION_REPORT(unsigned char *buf, unsigned i r.video_octets = get_le32(buf + 16); r.queuing_delay = get_le16(buf + 20); + /* before the callback: a report that makes the datagrams smaller has to + * reach the sending side as a new payload size first, so that it is not + * still producing the old one while acting on the report */ + PICA_media_peer_report(chan, &r); + if (callbacks.call_reception_report_cb) callbacks.call_reception_report_cb(chan->peer_id, &r); @@ -4904,7 +4909,7 @@ static int PICA_send_call_packet(struct PICA_c2c *chn, unsigned int msgid, uint1 * add more delay to every packet than the rest of the call path * costs together. */ - unsigned char pkt[PICA_MEDIA_LINK_MTU]; + unsigned char pkt[PICA_MEDIA_MAX_LINK_MTU]; unsigned int pktlen = 4 + PICA_PROTO_CALL_PACKET_HDRSIZE + len; if (pktlen > sizeof(pkt)) diff --git a/PICA_media.c b/PICA_media.c index afb862f..26e44b8 100644 --- a/PICA_media.c +++ b/PICA_media.c @@ -239,6 +239,7 @@ static struct PICA_media_channel *media_channel_new(struct PICA_c2c *c2c) m->sck = -1; m->started = time(0); m->last_tx = m->started; + m->link_mtu = PICA_MEDIA_LINK_MTU; return m; } @@ -1025,6 +1026,14 @@ int PICA_media_send(struct PICA_c2c *c2c, const unsigned char *buf, unsigned int if (!PICA_media_is_active(c2c)) return PICA_ERRINVARG; + /* Counted before the write rather than after it: a datagram the system + * refuses as too large never leaves this host, which is exactly one of + * the ways a path that cannot carry it shows up, see + * PICA_media_peer_report(). + */ + if (buf[0] == PICA_PROTO_CALL_VIDEO_PACKET) + m->tx_video++; + ret = SSL_write(m->ssl, buf, len); if (ret > 0) @@ -1045,6 +1054,139 @@ int PICA_media_send(struct PICA_c2c *c2c, const unsigned char *buf, unsigned int return PICA_OK; } +/* Sizes the datagrams of an established connection, and with that what one + * media packet can carry: tells OpenSSL and works out what a datagram has + * left over once the DTLS record overhead of the negotiated ciphersuite is + * paid. + * + * SSL_set_mtu() is what works after the handshake - DTLS_set_link_mtu() is + * only converted into the working MTU on the handshake write path, where it + * would sit unused. The value it sets survives whatever the handshake path + * does afterwards, a retransmitted final flight included: the link MTU given + * before the handshake has been consumed by then. + */ +static void media_set_link_mtu(struct PICA_media_channel *m, unsigned int link_mtu) +{ + m->link_mtu = link_mtu; + + SSL_set_mtu(m->ssl, m->link_mtu - PICA_MEDIA_IPUDP_OVERHEAD); + +#if OPENSSL_VERSION_NUMBER >= 0x10100000L + m->max_payload = (unsigned int)DTLS_get_data_mtu(m->ssl); +#else + /* DTLS record header, explicit nonce and authentication tag of the + * ciphersuites Pica Pica uses + */ + m->max_payload = m->link_mtu - PICA_MEDIA_IPUDP_OVERHEAD - 13 - 8 - 16; +#endif +} + +/* The size to run an established connection at. If the peer's address is on + * a private network this host is on, as for the probes of attempt 2 (see + * is_probe_target()), then the two clients are on the same network and + * nothing in between can need the datagrams any smaller than that network + * carries - so the connection uses the MTU of the interface facing it + * instead of the size every path is assumed to carry. It is read from the + * system rather than assumed to be Ethernet's 1500: an interface with a + * private address and a netmask of its own is just as likely to be a VPN + * tunnel, where it is several dozen octets less. + * + * Only ever asked once the handshake is over, never before it: a value too + * large for the path costs media packets, which the call survives and + * PICA_media_peer_report() undoes, while during the handshake it would cost + * the whole media connection, see PICA_MEDIA_LINK_MTU. + */ +static unsigned int media_local_link_mtu(struct PICA_media_channel *m) +{ + in_addr_t ifaddrs[PICA_IFADDRS_MAX], ifmasks[PICA_IFADDRS_MAX]; + in_addr_t peer = m->peer_addr.sin_addr.s_addr; + char addrstr[PICA_MEDIA_ADDRSTRSIZE]; + int ifcount, i, mtu; + + if (!PICA_is_private_addr_ipv4(peer)) + return PICA_MEDIA_LINK_MTU; + + ifcount = PICA_get_private_ifaddrs_ipv4(ifaddrs, ifmasks, PICA_IFADDRS_MAX); + i = PICA_find_subnet_ipv4(peer, ifaddrs, ifmasks, ifcount); + + if (i < 0) + return PICA_MEDIA_LINK_MTU; + + mtu = PICA_get_ifmtu_ipv4(ifaddrs[i]); + + fprintf(stderr, "media: %s is on the local network of %u.%u.%u.%u, interface MTU %i\n", + media_addrstr(&m->peer_addr, addrstr), + (unsigned int)(((const unsigned char*)&ifaddrs[i])[0]), + (unsigned int)(((const unsigned char*)&ifaddrs[i])[1]), + (unsigned int)(((const unsigned char*)&ifaddrs[i])[2]), + (unsigned int)(((const unsigned char*)&ifaddrs[i])[3]), + mtu); + + if (mtu > PICA_MEDIA_MAX_LINK_MTU) + mtu = PICA_MEDIA_MAX_LINK_MTU; + + /* an interface the system reports nothing about, or one carrying less + * than is assumed anyway - the smaller datagrams go through such a link + * as they are */ + if (mtu <= PICA_MEDIA_LINK_MTU) + return PICA_MEDIA_LINK_MTU; + + return (unsigned int)mtu; +} + +void PICA_media_peer_report(struct PICA_c2c *c2c, const struct PICA_call_reception_report *r) +{ + struct PICA_media_channel *m = c2c->media; + unsigned int sent; + unsigned int mtu; + + if (!PICA_media_is_active(c2c)) + return; + + /* the window the next report will be judged by starts here, whatever + * this one says */ + sent = m->tx_video; + m->tx_video = 0; + + /* Nothing arriving at all, or nothing sent to arrive, says nothing + * about the size of the datagrams: for that the small packets have to + * be getting through while the large ones do not. + */ + if (m->link_mtu <= PICA_MEDIA_MIN_LINK_MTU + || r->audio_received == 0 + || sent < PICA_MEDIA_BLACKHOLE_MIN_PACKETS + || (unsigned int)r->video_received * PICA_MEDIA_BLACKHOLE_RATIO >= sent) + { + m->blackhole_periods = 0; + return; + } + + fprintf(stderr, "media: the peer received %u of %u video packets and %u audio ones" + " - the path may not carry %u octet datagrams\n", + (unsigned int)r->video_received, sent, (unsigned int)r->audio_received, m->link_mtu); + + if (++m->blackhole_periods < PICA_MEDIA_BLACKHOLE_PERIODS) + return; + + m->blackhole_periods = 0; + + /* First back to the size every path is expected to carry, which is all + * there is to undo when this connection raised it for a local network; + * only if that is losing the video too does the assumption itself turn + * out to be wrong on this path. + */ + mtu = m->link_mtu > PICA_MEDIA_LINK_MTU ? PICA_MEDIA_LINK_MTU : PICA_MEDIA_MIN_LINK_MTU; + + media_set_link_mtu(m, mtu); + + fprintf(stderr, "media: datagrams of this connection lowered to %u octets\n", m->link_mtu); + + /* what one media packet can carry has changed, and the sending side of + * the call has to hear about it - the same way it heard the size this + * connection started with */ + PICA_media_transport_changed(c2c, PICA_CALL_TRANSPORT_MEDIAC2C, SSL_get_cipher_name(m->ssl)); +} + static void media_activate(struct PICA_media_channel *m) { int ret; @@ -1060,14 +1202,7 @@ static void media_activate(struct PICA_media_channel *m) return; } -#if OPENSSL_VERSION_NUMBER >= 0x10100000L - m->max_payload = (unsigned int)DTLS_get_data_mtu(m->ssl); -#else - /* DTLS record header, explicit nonce and authentication tag of the - * ciphersuites Pica Pica uses - */ - m->max_payload = PICA_MEDIA_LINK_MTU - PICA_MEDIA_IPUDP_OVERHEAD - 13 - 8 - 16; -#endif + media_set_link_mtu(m, media_local_link_mtu(m)); m->state = PICA_MEDIA_STATE_ACTIVE; diff --git a/PICA_media.h b/PICA_media.h index 90276a2..557275e 100644 --- a/PICA_media.h +++ b/PICA_media.h @@ -44,9 +44,27 @@ /* Link MTU assumed for a mediac2c connection until path MTU discovery * (RFC 8899) is implemented. 1200 octets is the value QUIC uses as the size - * every path is expected to carry. + * every path is expected to carry. The DTLS handshake always runs at this + * size, whatever the path turns out to be: a handshake message too large for + * the path is simply never answered, and the connection is lost along with + * the whole attempt. */ #define PICA_MEDIA_LINK_MTU 1200 +/* Largest link MTU a mediac2c connection ever uses, and with that the + * largest datagram one can send: the Ethernet MTU, which is what the local + * networks that get more than PICA_MEDIA_LINK_MTU out of this (see + * media_raise_local_mtu()) carry. An interface offering more than this - + * jumbo frames - is not taken at its word: the frames still have to cross + * switches, and the peer's interface, that may not be configured for them. + */ +#define PICA_MEDIA_MAX_LINK_MTU 1500 +/* Smallest link MTU a mediac2c connection ever uses: where the datagrams end + * up when even PICA_MEDIA_LINK_MTU turns out to be too large for the path, + * see PICA_media_peer_report(). Low enough to get through a tunnel inside a + * tunnel, and still large enough that one video packet carries something + * worth sending. + */ +#define PICA_MEDIA_MIN_LINK_MTU 1000 /* IPv4 + UDP headers */ #define PICA_MEDIA_IPUDP_OVERHEAD 28 @@ -117,6 +135,27 @@ */ #define PICA_MEDIA_DELAY_BASELINE_PERIODS 15 +/* A path that carries the small audio packets but none of the datagram sized + * video ones is one whose MTU is smaller than this connection assumed. It + * cannot be told apart from an ordinary bad link in one report, so the + * datagrams are only made smaller once this many reports in a row say the + * same thing - at PICA_MEDIA_REPORT_INTERVAL each, see + * PICA_media_peer_report(). + */ +#define PICA_MEDIA_BLACKHOLE_PERIODS 2 +/* A report says nothing about the size of the datagrams unless this many + * video packets were sent in the period it covers: at a low enough rate the + * loss of a few packets is the ordinary kind, and the packets are too small + * for their size to be what stops them anyway. + */ +#define PICA_MEDIA_BLACKHOLE_MIN_PACKETS 16 +/* How much of the video has to be missing for the period to count: the peer + * received less than one video packet in this many of those that were sent. + * Not all of them - the last fragment of a frame is whatever is left over + * and can be small enough to get through a path the full sized ones do not. + */ +#define PICA_MEDIA_BLACKHOLE_RATIO 4 + /* transport of the media packets of a call, reported by call_media_transport_cb */ #define PICA_CALL_TRANSPORT_C2C 0 #define PICA_CALL_TRANSPORT_MEDIAC2C 1 @@ -271,8 +310,19 @@ struct PICA_media_channel int dtls_seen; time_t last_tx; + /* what the datagrams of this connection are sized for: the assumed + * PICA_MEDIA_LINK_MTU, the MTU of the local network the peer turned out + * to be on (media_local_link_mtu()), or less than either of them once + * the path turns out not to carry that much + * (PICA_media_peer_report()) */ + unsigned int link_mtu; unsigned int max_payload; + /* video packets sent since the peer's last report arrived, and the + * number of its reports in a row that found none of them arriving */ + unsigned int tx_video; + int blackhole_periods; + unsigned char read_buf[PICA_MEDIA_READBUFSIZE]; unsigned int read_pos; }; @@ -339,6 +389,16 @@ void PICA_media_rx_packet(struct PICA_c2c *c2c, int video, uint16_t seq_num, */ int PICA_media_rx_report(struct PICA_c2c *c2c, struct PICA_call_reception_report *r); +/** + * The other direction: what one of the peer's 0x79 reports says about the + * media sent to it. A report that says the audio arrived while the video did + * not is the one thing an MTU too large for the path looks like from this + * side, the audio packets being small and the video ones datagram sized - so + * enough such reports in a row make the datagrams smaller, down to + * PICA_MEDIA_MIN_LINK_MTU. Never larger: nothing here searches upwards. + */ +void PICA_media_peer_report(struct PICA_c2c *c2c, const struct PICA_call_reception_report *r); + /** Releases everything PICA_media_call_prepare() made. */ void PICA_media_call_free(struct PICA_c2c *c2c); diff --git a/PICA_netconf.c b/PICA_netconf.c index 2ada9c3..c0353fa 100644 --- a/PICA_netconf.c +++ b/PICA_netconf.c @@ -28,8 +28,16 @@ #ifndef WIN32 #include <sys/types.h> +#include <sys/socket.h> +#include <sys/ioctl.h> +#include <unistd.h> #include <ifaddrs.h> #include <net/if.h> +/* SIOCGIFMTU lives here on the BSDs and on Apple's systems, in + * <sys/ioctl.h> on Linux and Android */ +#ifndef __linux__ +#include <sys/sockio.h> +#endif #endif #define PREFIX4 htonl(0xf0000000U) @@ -326,6 +334,119 @@ int PICA_find_subnet_ipv4(in_addr_t addr, const in_addr_t *addrs, const in_addr_ return -1; } +#ifndef WIN32 +int PICA_get_ifmtu_ipv4(in_addr_t ifaddr) +{ + struct ifaddrs *ifaddr_list, *ifa; + int sck; + int mtu = 0; + + /* getifaddrs() reports the MTU on the BSDs only, in the ifa_data of the + * link layer entry of an interface rather than of its IPv4 one - so the + * address is looked up here only to learn the interface name, and the + * MTU is asked for by the one call that answers it everywhere. + */ + if (getifaddrs(&ifaddr_list) == -1) + return 0; + + sck = socket(AF_INET, SOCK_DGRAM, 0); + + if (sck < 0) + { + freeifaddrs(ifaddr_list); + return 0; + } + + for (ifa = ifaddr_list; ifa != NULL; ifa = ifa->ifa_next) + { + struct ifreq ifr; + + if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET || ifa->ifa_name == NULL) + continue; + + if (((struct sockaddr_in*)ifa->ifa_addr)->sin_addr.s_addr != ifaddr) + continue; + + memset(&ifr, 0, sizeof(ifr)); + snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", ifa->ifa_name); + + if (ioctl(sck, SIOCGIFMTU, &ifr) == 0 && ifr.ifr_mtu > 0) + mtu = ifr.ifr_mtu; + + break; + } + + close(sck); + freeifaddrs(ifaddr_list); + + return mtu; +} +#else +int PICA_get_ifmtu_ipv4(in_addr_t ifaddr) +{ + PMIB_IPADDRTABLE table = NULL; + MIB_IFROW row; + ULONG size = 0; + DWORD ret; + DWORD i; + DWORD ifindex = 0; + int found = 0; + int tries; + + /* as in PICA_get_private_ifaddrs_ipv4(): the address table gives the + * interface index, GetIfEntry() the MTU of the interface it belongs to + */ + for (tries = 0; tries < 3; tries++) + { + ret = GetIpAddrTable(table, &size, FALSE); + + if (ret == NO_ERROR) + break; + + free(table); + table = NULL; + + if (ret != ERROR_INSUFFICIENT_BUFFER) + return 0; + + table = (PMIB_IPADDRTABLE)malloc(size); + + if (!table) + return 0; + } + + if (ret != NO_ERROR || !table) + { + free(table); + return 0; + } + + for (i = 0; i < table->dwNumEntries; i++) + if ((in_addr_t)table->table[i].dwAddr == ifaddr) + { + ifindex = table->table[i].dwIndex; + found = 1; + break; + } + + free(table); + + if (!found) + return 0; + + memset(&row, 0, sizeof(row)); + row.dwIndex = ifindex; + + if (GetIfEntry(&row) != NO_ERROR) + return 0; + + if (row.dwMtu == 0 || row.dwMtu > 0xFFFF) + return 0; + + return (int)row.dwMtu; +} +#endif + in_addr_t PICA_guess_listening_addr_ipv4() { in_addr_t addrs[PICA_IFADDRS_MAX]; diff --git a/PICA_netconf.h b/PICA_netconf.h index aad2a5c..d63d9ff 100644 --- a/PICA_netconf.h +++ b/PICA_netconf.h @@ -148,6 +148,18 @@ int PICA_get_private_ifaddrs_ipv4(in_addr_t *addrs, in_addr_t *masks, int maxcou */ int PICA_find_subnet_ipv4(in_addr_t addr, const in_addr_t *addrs, const in_addr_t *masks, int count); +/** + * Link MTU of the network interface that has the given IPv4 address, i.e. + * the largest IP packet that interface sends without fragmenting it. For a + * peer on the same network this is the whole path MTU, there being nothing + * in between to fragment anything. + * \param ifaddr an interface address as reported by PICA_get_ifaddrs_ipv4() + * or PICA_get_private_ifaddrs_ipv4(), network byte order + * \return the MTU in octets, or 0 if there is no such interface or the + * system cannot be asked + */ +int PICA_get_ifmtu_ipv4(in_addr_t ifaddr); + in_addr_t PICA_guess_listening_addr_ipv4(); /** diff --git a/PICA_nodewait.c b/PICA_nodewait.c index 5e4a60b..582aed5 100644 --- a/PICA_nodewait.c +++ b/PICA_nodewait.c @@ -29,6 +29,8 @@ #include <ws2tcpip.h> #include <windows.h> +#define THREADFUNCRET_TYPE DWORD + #else #include <pthread.h> @@ -36,6 +38,8 @@ #include <sys/socket.h> #include <netdb.h> +#define THREADFUNCRET_TYPE void* + #endif struct nodewait *nodewait_list; @@ -166,7 +170,7 @@ static void *nodewait_connect_thread (void *arg) if (!nw->nc.anonssl) { CLOSE(nw->nc.sck); - return (void *)0; + return (THREADFUNCRET_TYPE)0; } if (!SSL_set_fd(nw->nc.anonssl, nw->nc.sck) || 1 != SSL_connect(nw->nc.anonssl)) @@ -187,11 +191,11 @@ static void *nodewait_connect_thread (void *arg) nw->nc.addr = *((struct sockaddr_in*)(ap->ai_addr)); nw->state = PICA_NODEWAIT_CONNECTED; - return (void *)1; + return (THREADFUNCRET_TYPE)1; } nw->state = PICA_NODEWAIT_CONNECT_FAILED; - return (void *)0; + return (THREADFUNCRET_TYPE)0; } void nodewait_start_resolve(struct PICA_nodeaddr *a) diff --git a/pica-client/audiovideocallcontroller.cpp b/pica-client/audiovideocallcontroller.cpp index 5f24e73..d367066 100644 --- a/pica-client/audiovideocallcontroller.cpp +++ b/pica-client/audiovideocallcontroller.cpp @@ -730,12 +730,16 @@ void AudioVideoCallController::media_transport_changed(QByteArray peer_id, bool if (peer_id != m_peer_id) return; - // Fragments stay datagram sized for the whole call (see - // startVideoPipeline()); this only lowers the size further, for when the - // media connection turns out to carry less than was assumed. Direct call, + // Over a media connection the datagram is the hard limit and max_payload + // is exactly what fits into one, whether that is less than was assumed at + // the start of the call (see startVideoPipeline()) or - on a local network + // - more. Back on the c2c connection, where a message can carry far more + // than any datagram, fragments stay datagram sized so that the media can + // move to a datagram again without changing size mid-frame. Direct call, // not invokeMethod()/QueuedConnection - see the comment in // stopAudioPipeline(): cam's thread is blocked inside Capture(). - cam->setMaxFragmentSize(qMin((int)max_payload, (int)PICA_MEDIA_SAFE_PAYLOAD)); + cam->setMaxFragmentSize(direct_udp && max_payload > 0 ? (int)max_payload + : (int)PICA_MEDIA_SAFE_PAYLOAD); if (callwindow) callwindow->setMediaTransport(direct_udp, ciphersuitename); |
