Make QUICHE compile for MacOS

This fixes all of the compile issues. There are still unit tests that do not pass; those will be addressed separately.

PiperOrigin-RevId: 493000948
diff --git a/quiche/quic/core/quic_udp_socket_posix.cc b/quiche/quic/core/quic_udp_socket_posix.cc
index 248fee3..5335723 100644
--- a/quiche/quic/core/quic_udp_socket_posix.cc
+++ b/quiche/quic/core/quic_udp_socket_posix.cc
@@ -2,6 +2,11 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
+#if defined(__APPLE__) && !defined(__APPLE_USE_RFC_3542)
+// This must be defined before including any system headers.
+#define __APPLE_USE_RFC_3542
+#endif  // defined(__APPLE__) && !defined(__APPLE_USE_RFC_3542)
+
 #include <arpa/inet.h>
 #include <fcntl.h>
 #include <net/if.h>
diff --git a/quiche/quic/masque/masque_utils.cc b/quiche/quic/masque/masque_utils.cc
index b432a7a..548f62b 100644
--- a/quiche/quic/masque/masque_utils.cc
+++ b/quiche/quic/masque/masque_utils.cc
@@ -4,10 +4,12 @@
 
 #include "quiche/quic/masque/masque_utils.h"
 
+#if defined(__linux__)
 #include <fcntl.h>
 #include <linux/if.h>
 #include <linux/if_tun.h>
 #include <sys/ioctl.h>
+#endif  // defined(__linux__)
 
 namespace quic {
 
@@ -48,6 +50,7 @@
   return os;
 }
 
+#if defined(__linux__)
 int CreateTunInterface(const QuicIpAddress& client_address, bool server) {
   if (!client_address.IsIPv4()) {
     QUIC_LOG(ERROR) << "CreateTunInterface currently only supports IPv4";
@@ -136,5 +139,12 @@
   }
   return -1;
 }
+#else
+int CreateTunInterface(const QuicIpAddress& /*client_address*/,
+                       bool /*server*/) {
+  // Unsupported.
+  return -1;
+}
+#endif  // defined(__linux__)
 
 }  // namespace quic
diff --git a/quiche/quic/tools/quic_server.cc b/quiche/quic/tools/quic_server.cc
index aace378..738d8c9 100644
--- a/quiche/quic/tools/quic_server.cc
+++ b/quiche/quic/tools/quic_server.cc
@@ -125,9 +125,8 @@
   overflow_supported_ = socket_api.EnableDroppedPacketCount(fd_);
   socket_api.EnableReceiveTimestamp(fd_);
 
-  sockaddr_storage addr = address.generic_address();
-  int rc = bind(fd_, reinterpret_cast<sockaddr*>(&addr), sizeof(addr));
-  if (rc < 0) {
+  bool success = socket_api.Bind(fd_, address);
+  if (!success) {
     QUIC_LOG(ERROR) << "Bind failed: " << strerror(errno);
     return false;
   }
diff --git a/quiche/quic/tools/quic_server_test.cc b/quiche/quic/tools/quic_server_test.cc
index 26075a1..facd42e 100644
--- a/quiche/quic/tools/quic_server_test.cc
+++ b/quiche/quic/tools/quic_server_test.cc
@@ -146,18 +146,21 @@
           DoAll(testing::Assign(&more_chlos, false), testing::Return(false)));
 
   // Send a packet to trigger epoll event.
-  int fd = socket(
-      AddressFamilyUnderTest() == IpAddressFamily::IP_V4 ? AF_INET : AF_INET6,
-      SOCK_DGRAM | SOCK_NONBLOCK, IPPROTO_UDP);
-  ASSERT_LT(0, fd);
+  QuicUdpSocketApi socket_api;
+  SocketFd fd =
+      socket_api.Create(server_address_.host().AddressFamilyToInt(),
+                        /*receive_buffer_size =*/kDefaultSocketReceiveBuffer,
+                        /*send_buffer_size =*/kDefaultSocketReceiveBuffer);
+  ASSERT_NE(fd, kQuicInvalidSocketFd);
 
   char buf[1024];
   memset(buf, 0, ABSL_ARRAYSIZE(buf));
-  sockaddr_storage storage = server_address_.generic_address();
-  int rc = sendto(fd, buf, ABSL_ARRAYSIZE(buf), 0,
-                  reinterpret_cast<sockaddr*>(&storage), sizeof(storage));
-  if (rc < 0) {
-    QUIC_DLOG(INFO) << errno << " " << strerror(errno);
+  QuicUdpPacketInfo packet_info;
+  packet_info.SetPeerAddress(server_address_);
+  WriteResult result =
+      socket_api.WritePacket(fd, buf, sizeof(buf), packet_info);
+  if (result.status != WRITE_STATUS_OK) {
+    QUIC_LOG(ERROR) << "Write error for UDP packet: " << result.error_code;
   }
 
   while (more_chlos) {