///////////////////////////////////////////////////////
| 732 | |
| 733 | //////////////////////////////////////////////////////////// |
| 734 | unsigned short TcpSocket::getLocalPort() const |
| 735 | { |
| 736 | if (getNativeHandle() != priv::SocketImpl::invalidSocket()) |
| 737 | { |
| 738 | // Retrieve information about the local end of the socket |
| 739 | sockaddr_in6 addressV6{}; |
| 740 | priv::SocketImpl::AddrLength size = sizeof(addressV6); |
| 741 | if (getsockname(getNativeHandle(), reinterpret_cast<sockaddr*>(&addressV6), &size) != -1) |
| 742 | { |
| 743 | if (addressV6.sin6_family == PF_INET6) |
| 744 | return ntohs(addressV6.sin6_port); |
| 745 | |
| 746 | if (addressV6.sin6_family == PF_INET) |
| 747 | { |
| 748 | sockaddr_in addressV4{}; |
| 749 | std::memcpy(&addressV4, &addressV6, sizeof(addressV4)); |
| 750 | return ntohs(addressV4.sin_port); |
| 751 | } |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | // We failed to retrieve the port |
| 756 | return 0; |
| 757 | } |
| 758 | |
| 759 | |
| 760 | //////////////////////////////////////////////////////////// |
no outgoing calls
no test coverage detected