| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | ContainerPortMapping.cpp |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | This file contains the implementation of the WinRT wrapper for the WSLC SDK ContainerPortMapping class. |
| 12 | |
| 13 | --*/ |
| 14 | |
| 15 | #include "precomp.h" |
| 16 | #include "ContainerPortMapping.h" |
| 17 | #include "Microsoft.WSL.Containers.ContainerPortMapping.g.cpp" |
| 18 | |
| 19 | namespace winrt::Microsoft::WSL::Containers::implementation { |
| 20 | |
| 21 | ContainerPortMapping::ContainerPortMapping(uint16_t windowsPort, uint16_t containerPort, winrt::Microsoft::WSL::Containers::PortProtocol const& protocol) : |
| 22 | m_windowsPort(windowsPort), m_containerPort(containerPort), m_protocol(protocol) |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | uint16_t ContainerPortMapping::WindowsPort() |
| 27 | { |
| 28 | return m_windowsPort; |
| 29 | } |
| 30 | |
| 31 | void ContainerPortMapping::WindowsPort(uint16_t value) |
| 32 | { |
| 33 | if (m_containerPortMapping) |
| 34 | { |
| 35 | throw hresult_illegal_state_change(L"Cannot change value after options have been applied"); |
| 36 | } |
| 37 | |
| 38 | m_windowsPort = value; |
| 39 | } |
| 40 | |
| 41 | uint16_t ContainerPortMapping::ContainerPort() |
| 42 | { |
| 43 | return m_containerPort; |
| 44 | } |
| 45 | |
| 46 | void ContainerPortMapping::ContainerPort(uint16_t value) |
| 47 | { |
| 48 | if (m_containerPortMapping) |
| 49 | { |
| 50 | throw hresult_illegal_state_change(L"Cannot change value after options have been applied"); |
| 51 | } |
| 52 | |
| 53 | m_containerPort = value; |
| 54 | } |
| 55 | |
| 56 | winrt::Microsoft::WSL::Containers::PortProtocol ContainerPortMapping::Protocol() |
| 57 | { |
| 58 | return m_protocol; |
| 59 | } |
| 60 | |
| 61 | void ContainerPortMapping::Protocol(winrt::Microsoft::WSL::Containers::PortProtocol const& value) |
| 62 | { |
| 63 | if (m_containerPortMapping) |
| 64 | { |
| 65 | throw hresult_illegal_state_change(L"Cannot change value after options have been applied"); |
| 66 | } |
| 67 | |
| 68 | m_protocol = value; |
| 69 | } |
| 70 | |
| 71 | winrt::Windows::Networking::HostName ContainerPortMapping::WindowsAddress() |
| 72 | { |
| 73 | return m_windowsAddress; |
| 74 | } |
| 75 | |
| 76 | void ContainerPortMapping::WindowsAddress(winrt::Windows::Networking::HostName const& value) |
| 77 | { |
| 78 | if (m_containerPortMapping) |
| 79 | { |
| 80 | throw hresult_illegal_state_change(L"Cannot change value after options have been applied"); |
| 81 | } |
| 82 | |
| 83 | if (value && value.Type() != winrt::Windows::Networking::HostNameType::Ipv4 && value.Type() != winrt::Windows::Networking::HostNameType::Ipv6) |
| 84 | { |
| 85 | throw hresult_invalid_argument(L"Only IP addresses are supported for port mapping"); |
| 86 | } |
| 87 | |
| 88 | m_windowsAddress = value; |
| 89 | } |
| 90 | |
| 91 | WslcContainerPortMapping ContainerPortMapping::ToStruct() |
| 92 | { |
| 93 | if (!m_containerPortMapping) |
| 94 | { |
| 95 | m_containerPortMapping = std::make_unique<WslcContainerPortMapping>(); |
| 96 | m_containerPortMapping->windowsPort = m_windowsPort; |
| 97 | m_containerPortMapping->containerPort = m_containerPort; |
| 98 | m_containerPortMapping->protocol = static_cast<WslcPortProtocol>(m_protocol); |
| 99 | |
| 100 | if (m_windowsAddress) |
| 101 | { |
| 102 | m_windowsAddressStorage = sockaddr_storage{}; |
| 103 | void* addrPtr = &m_windowsAddressStorage.value(); |
| 104 | |
| 105 | auto rawName = winrt::to_string(m_windowsAddress.RawName()); |
| 106 | |
| 107 | if (m_windowsAddress.Type() == winrt::Windows::Networking::HostNameType::Ipv4) |
| 108 | { |
| 109 | auto addr = static_cast<sockaddr_in*>(addrPtr); |
| 110 | addr->sin_family = AF_INET; |
| 111 | if (inet_pton(AF_INET, rawName.c_str(), &addr->sin_addr) != 1) |
| 112 | { |
| 113 | throw winrt::hresult_invalid_argument(L"Invalid IPv4 address format"); |
| 114 | } |
| 115 | } |
| 116 | else if (m_windowsAddress.Type() == winrt::Windows::Networking::HostNameType::Ipv6) |
| 117 | { |
| 118 | auto addr = static_cast<sockaddr_in6*>(addrPtr); |
| 119 | addr->sin6_family = AF_INET6; |
| 120 | if (inet_pton(AF_INET6, rawName.c_str(), &addr->sin6_addr) != 1) |
| 121 | { |
| 122 | throw winrt::hresult_invalid_argument(L"Invalid IPv6 address format"); |
| 123 | } |
| 124 | } |
| 125 | else |
| 126 | { |
| 127 | throw winrt::hresult_invalid_argument(L"Only IP addresses are supported for port mapping"); |
| 128 | } |
| 129 | |
| 130 | m_containerPortMapping->windowsAddress = &m_windowsAddressStorage.value(); |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | m_containerPortMapping->windowsAddress = nullptr; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | return *m_containerPortMapping; |
| 139 | } |
| 140 | |
| 141 | } // namespace winrt::Microsoft::WSL::Containers::implementation |