Skip to content

MtProtoKit: fix SOCKS5 proxy IPv6 connect sent garbage address#179

Open
mvsrcdst wants to merge 1 commit into
Swiftgram:masterfrom
mvsrcdst:fix/proxy-ipv6
Open

MtProtoKit: fix SOCKS5 proxy IPv6 connect sent garbage address#179
mvsrcdst wants to merge 1 commit into
Swiftgram:masterfrom
mvsrcdst:fix/proxy-ipv6

Conversation

@mvsrcdst

@mvsrcdst mvsrcdst commented Jul 16, 2026

Copy link
Copy Markdown

Problem


Users on SOCKS5 proxy fail to connect when the resolved DC address is IPv6 connection spins forever, never drops or errors out.

Root cause: requestSocksConnection in MTTcpConnection.m hardcoded req.AddrType = 1 (IPv4) and nevern value. When _scheme.address.ip is an IPv6 literal, inet_aton fails silently, leaving req.DestAddr.IPv4 holding uninitialized stack garbage which still got sent to the proxy tagged as can't route the connect to a real address, so it just hangs indefinitely instead of failing.

Depends on which DC address the client resolves to (IPv4 vs IPv6), not on whether the proxy server itself supports IPv6.

Fix


Detect IPv4 vs IPv6 properly before building the connect request, mirroring the pattern already used in -start for _socksIp/_mtpIp:

  • inet_aton succeeds - IPv4 (ATYP=1)
  • else inet_pton(AF_INET6, ...) succeeds - IPv6 (ATYP=4)
  • else domain name fallback (ATYP=3) per SOCKS5 spec (RFC 1928 part 4), bounds-checked against the 256-byte domain buffer
  • none of these parse - log and close the connection with an error, instead of sending garbage

Impact


SOCKS5 proxy connections no longer hang indefinitely when the resolved DC address is IPv6.

Note


Upstream has PR TelegramMessenger#735 addressing this same issue, currently under discussion.
Part of tracking issue: #182

Testing


  • Tested on iOS Simulator 18.6/26.5
  • SOCKS5 proxy with DC IPv6 address is connecting

@mvsrcdst mvsrcdst marked this pull request as ready for review July 16, 2026 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant