From 64d5980b747d75de0b6bc20f526c81958a37033d Mon Sep 17 00:00:00 2001 From: Dan Rumney Date: Wed, 16 Nov 2022 15:14:27 -0600 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20deal=20with=20OSX=20handl?= =?UTF-8?q?ing=20of=20IPv4=20header=20length?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit FreeBSD does weird things to the length field in the IP header of a packet; this change reverts those changes so that there is a consistent experience across platforms ✅ Closes: #60 --- src/raw.cc | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/raw.cc b/src/raw.cc index b8e4850..4f34b4e 100644 --- a/src/raw.cc +++ b/src/raw.cc @@ -414,7 +414,14 @@ void SocketWrap::HandleIOEvent (int status, int revents) { ** 0.10 and 0.12. So, for now, we will just give you the number. **/ char status_str[32]; +#ifdef __APPLE__ + /** + * OSX deprecates the use of sprintf + */ + snprintf(status_str, sizeof(status_str), "%d", status); +#else sprintf(status_str, "%d", status); +#endif args[1] = Nan::Error(status_str); Nan::Call(Nan::New("emit").ToLocalChecked(), handle(), 1, args); @@ -575,6 +582,26 @@ NAN_METHOD(SocketWrap::Recv) { uv_ip6_name (&sin6_address, addr, 50); else uv_ip4_name (&sin_address, addr, 50); + +#ifdef __APPLE__ + /* + FreeBSD has a bug/feature(?) such that: + + - ip_len does not include the IP header's length. recvfrom() however + returns the packet's true length. To get the true ip_len field do: + iphdr->ip_len += iphdr->ip_hl << 2; + + This code converts this information + */ + if (socket->family_ != AF_INET6) { + char *message = (char *)node::Buffer::Data(buffer); + int header_len = (message[0] & 0x0F) << 2; + long packet_len = message[3] << 8 | message[2] + header_len; + + message[3] = packet_len & 0xFF; + message[2] = (packet_len >> 8) & 0xFF; + } +#endif Local cb = Local::Cast (info[1]); const unsigned argc = 3;