improve robustness in natpmp code a bit

This commit is contained in:
arvidn 2016-05-23 22:46:46 -04:00
parent 3106f6f8a5
commit 0ce5a4c664
2 changed files with 10 additions and 8 deletions

View File

@ -127,7 +127,8 @@ void natpmp::start()
#if defined TORRENT_ASIO_DEBUGGING
add_outstanding_async("natpmp::on_reply");
#endif
m_socket.async_receive_from(boost::asio::buffer(&m_response_buffer, 16)
m_socket.async_receive_from(boost::asio::buffer(&m_response_buffer[0]
, sizeof(m_response_buffer))
, m_remote, boost::bind(&natpmp::on_reply, self(), _1, _2));
send_get_ip_address_request(l);
@ -433,10 +434,11 @@ void natpmp::on_reply(error_code const& e
#endif
// make a copy of the response packet buffer
// to avoid overwriting it in the next receive call
char msg_buf[16];
char msg_buf[sizeof(m_response_buffer)];
memcpy(msg_buf, m_response_buffer, bytes_transferred);
m_socket.async_receive_from(boost::asio::buffer(&m_response_buffer, 16)
m_socket.async_receive_from(boost::asio::buffer(&m_response_buffer[0]
, sizeof(m_response_buffer))
, m_remote, boost::bind(&natpmp::on_reply, self(), _1, _2));
// simulate packet loss
@ -485,7 +487,7 @@ void natpmp::on_reply(error_code const& e
}
if (bytes_transferred < 16)
if (bytes_transferred != 16)
{
char msg[200];
snprintf(msg, sizeof(msg), "received packet of invalid size: %d", int(bytes_transferred));