| 121 | } |
| 122 | |
| 123 | int Bind(SOCKET s, ui16 mode) const { |
| 124 | Y_UNUSED(mode); |
| 125 | int ret = 0; |
| 126 | // 1. open file |
| 127 | TFileHandle f(Path, CreateAlways | WrOnly); |
| 128 | if (!f.IsOpen()) { |
| 129 | return -errno; |
| 130 | } |
| 131 | |
| 132 | // 2. find port and bind to it |
| 133 | in.sin_port = 0; |
| 134 | ret = bind(s, SockAddr(), Len()); |
| 135 | if (ret != 0) { |
| 136 | return -WSAGetLastError(); |
| 137 | } |
| 138 | |
| 139 | int size = Size(); |
| 140 | ret = getsockname(s, (struct sockaddr*)(&in), &size); |
| 141 | if (ret != 0) { |
| 142 | return -WSAGetLastError(); |
| 143 | } |
| 144 | |
| 145 | // 3. write port to file |
| 146 | ret = f.Write(&(in.sin_port), sizeof(in.sin_port)); |
| 147 | if (ret != sizeof(in.sin_port)) { |
| 148 | return -errno; |
| 149 | } |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | static constexpr size_t PathSize = 128; |
| 155 | mutable struct sockaddr_in in; |