(sock, data, addr, server_host)
| 242 | |
| 243 | |
| 244 | def _handle_allocate_request(sock, data, addr, server_host): |
| 245 | tid = data[8:20] |
| 246 | key = (addr[0], addr[1]) |
| 247 | attrs = _parse_attrs(data) |
| 248 | print("Allocate request from %s:%d" % (addr[0], addr[1]), flush=True) |
| 249 | |
| 250 | if not _check_turn_auth(sock, data, attrs, tid, addr, MSG_ALLOCATE_ERROR): |
| 251 | return |
| 252 | |
| 253 | if key in _allocations and not _allocations[key].is_expired(): |
| 254 | err = _build_error_attr(437, b'Allocation Mismatch') |
| 255 | sock.sendto(_build_response(MSG_ALLOCATE_ERROR, tid, err), addr) |
| 256 | return |
| 257 | |
| 258 | # Clean up any expired allocation for this client before creating a new one |
| 259 | if key in _allocations: |
| 260 | _delete_allocation(key) |
| 261 | |
| 262 | family = socket.AF_INET6 if ':' in server_host else socket.AF_INET |
| 263 | relay_sock = socket.socket(family, socket.SOCK_DGRAM) |
| 264 | relay_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 265 | if family == socket.AF_INET6: |
| 266 | relay_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1) |
| 267 | relay_sock.bind((server_host, 0)) |
| 268 | relay_host, relay_port = relay_sock.getsockname()[:2] |
| 269 | |
| 270 | alloc = Allocation(relay_sock, relay_host, relay_port, sock, addr, TURN_DEFAULT_LIFETIME) |
| 271 | _allocations[key] = alloc |
| 272 | _relay_sock_map[relay_sock] = alloc |
| 273 | |
| 274 | print(" Relay %s:%d allocated for %s:%d" % (relay_host, relay_port, addr[0], addr[1]), flush=True) |
| 275 | |
| 276 | relayed = _build_xor_addr_attr(ATTR_XOR_RELAYED_ADDRESS, relay_host, relay_port, tid) |
| 277 | mapped = _build_xor_addr_attr(ATTR_XOR_MAPPED_ADDRESS, addr[0], addr[1], tid) |
| 278 | lftime = _build_lifetime_attr(TURN_DEFAULT_LIFETIME) |
| 279 | sock.sendto(_build_response(MSG_ALLOCATE_SUCCESS, tid, relayed, mapped, lftime), addr) |
| 280 | |
| 281 | |
| 282 | def _handle_refresh_request(sock, data, addr): |
no test coverage detected