MCPcopy Create free account
hub / github.com/OpenRCT2/OpenRCT2 / GetBroadcastAddresses

Function GetBroadcastAddresses

src/openrct2/network/Socket.cpp:923–983  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

921 #endif
922
923 std::vector<std::unique_ptr<INetworkEndpoint>> GetBroadcastAddresses()
924 {
925 std::vector<std::unique_ptr<INetworkEndpoint>> baddresses;
926 #ifdef _WIN32
927 auto interfaces = GetNetworkInterfaces();
928 for (const auto& ifo : interfaces)
929 {
930 if (ifo.iiFlags & IFF_LOOPBACK)
931 continue;
932 if (!(ifo.iiFlags & IFF_BROADCAST))
933 continue;
934
935 // iiBroadcast is unusable, because it always seems to be set to 255.255.255.255.
936 sockaddr_storage address{};
937 memcpy(&address, &ifo.iiAddress.Address, sizeof(sockaddr));
938 (reinterpret_cast<sockaddr_in*>(&address))->sin_addr.s_addr = ifo.iiAddress.AddressIn.sin_addr.s_addr
939 | ~ifo.iiNetmask.AddressIn.sin_addr.s_addr;
940 baddresses.push_back(
941 std::make_unique<NetworkEndpoint>(
942 reinterpret_cast<const sockaddr*>(&address), static_cast<socklen_t>(sizeof(sockaddr))));
943 }
944 #else
945 int sock = socket(AF_INET, SOCK_DGRAM, 0);
946 if (sock == -1)
947 {
948 return baddresses;
949 }
950
951 char buf[4 * 1024]{};
952 ifconf ifconfx{};
953 ifconfx.ifc_len = sizeof(buf);
954 ifconfx.ifc_buf = buf;
955 if (ioctl(sock, SIOCGIFCONF, &ifconfx) == -1)
956 {
957 close(sock);
958 return baddresses;
959 }
960
961 const char* buf_end = buf + ifconfx.ifc_len;
962 for (const char* p = buf; p < buf_end;)
963 {
964 auto req = reinterpret_cast<const ifreq*>(p);
965 if (req->ifr_addr.sa_family == AF_INET)
966 {
967 ifreq r;
968 strcpy(r.ifr_name, req->ifr_name);
969 if (ioctl(sock, SIOCGIFFLAGS, &r) != -1 && (r.ifr_flags & IFF_BROADCAST)
970 && ioctl(sock, SIOCGIFBRDADDR, &r) != -1)
971 {
972 baddresses.push_back(std::make_unique<NetworkEndpoint>(&r.ifr_broadaddr, sizeof(sockaddr)));
973 }
974 }
975 p += sizeof(ifreq);
976 #if defined(AF_LINK) && !defined(SUNOS)
977 p += req->ifr_addr.sa_len - sizeof(struct sockaddr);
978 #endif
979 }
980 close(sock);

Callers 2

Socket.hFile · 0.85

Calls 2

GetNetworkInterfacesFunction · 0.85
push_backMethod · 0.45

Tested by

no test coverage detected