BUG/MEDIUM: lua: Fix IPv6 with separate port support for Socket.connect
authorTim Duesterhus <tim@bastelstu.be>
Sat, 6 Jan 2018 18:04:45 +0000 (19:04 +0100)
committerWilly Tarreau <w@1wt.eu>
Tue, 9 Jan 2018 14:22:55 +0000 (15:22 +0100)
commit6edab865f661edf732d30232808118585cc2a1c7
tree132ae3b0175a0d62d44fb7f31c3763bfd3b14cc6
parentb33754ce86cdcc877344ccedfb3a336b63154c2e
BUG/MEDIUM: lua: Fix IPv6 with separate port support for Socket.connect

The `socket.tcp.connect` method of Lua requires at least two parameters:
The host and the port. The `Socket.connect` method of haproxy requires
only one when a host with a combined port is provided. This stems from
the fact that `str2sa_range` is used internally in `hlua_socket_connect`.
This very fact unfortunately causes a diversion in the behaviour of
Lua's socket class and haproxy's for IPv6 addresses:

  sock:connect("::1", "80")

works fine with Lua, but fails with:

  connect: cannot parse destination address '::1'

in haproxy, because `str2sa_range` parses the trailing `:1` as the port.

This patch forcefully adds a `:` to the end of the address iff a port
number greater than `0` is given as the second parameter.

Technically this breaks backwards compatibility, because the docs state:

> The syntax "127.0.0.1:1234" is valid. in this case, the
> parameter *port* is ignored.

But: The connect() call can only succeed if the second parameter is left
out (which causes no breakage) or if the second parameter is an integer
or a numeric string.

It seems unlikely that someone would provide an address with a port number
and would also provide a second parameter containing a number other than
zero. Thus I feel this breakage is warranted to fix the mismatch between
haproxy's socket class and Lua's one.

This commit should be backported to haproxy 1.8 only, because of the
possible breakage of existing Lua scripts.
doc/lua-api/index.rst
src/hlua.c