int
Ip::Qos::setSockTos(const Comm::ConnectionPointer &conn, tos_t tos)
{
-#ifdef IP_TOS
- int x = setsockopt(conn->fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos_t));
+#if defined(IP_TOS)
+ // Bug 3731: FreeBSD produces 'invalid option'
+ // unless we pass it a 32-bit variable storing 8-bits of data.
+ // NP: it is documented as 'int' for all systems, even those like Linux which accept 8-bit char
+ // so we convert to a int before setting.
+ int bTos = tos;
+ int x = setsockopt(conn->fd, IPPROTO_IP, IP_TOS, &bTos, sizeof(bTos));
if (x < 0)
debugs(50, 2, "Ip::Qos::setSockTos: setsockopt(IP_TOS) on " << conn << ": " << xstrerror());
return x;