Server: use uv_getaddrinfo() for $NVIM_LISTEN_ADDRESS

This change implicitly adds IPv6 support.

If the address contains ":", we try to use a TCP socket instead of a Unix domain
socket. Everything in front of the last occurrence of ":" is the hostname and
everything after it the port.

If the hostname lookup fails, we fall back to using a Unix domain socket.

If the port is empty ("localhost:"), a random port will be assigned.

Examples:

  NVIM_LISTEN_ADDRESS=localhost:12345 -> TCP (IPv4 or IPv6), port: 12345
  NVIM_LISTEN_ADDRESS=localhost:      -> TCP (IPv4 or IPv6), port: random (> 1024)
  NVIM_LISTEN_ADDRESS=localhost:0     -> TCP (IPv4 or IPv6), port: random (> 1024)
  NVIM_LISTEN_ADDRESS=localhost       -> Unix domain socket "localhost" in current dir
This commit is contained in:
Marco Hinz
2017-05-04 17:59:51 +02:00
parent 9cc10c69f2
commit 3efc82cbb2
4 changed files with 81 additions and 65 deletions

View File

@@ -97,31 +97,36 @@ char *server_address_new(void)
#endif
}
/// Starts listening for API calls on the TCP address or pipe path `endpoint`.
/// The socket type is determined by parsing `endpoint`: If it's a valid IPv4
/// address in 'ip[:port]' format, then it will be TCP socket. The port is
/// optional and if omitted defaults to NVIM_DEFAULT_TCP_PORT. Otherwise it
/// will be a unix socket or named pipe.
/// Starts listening for API calls.
///
/// @param endpoint Address of the server. Either a 'ip[:port]' string or an
/// arbitrary identifier (trimmed to 256 bytes) for the unix socket or
/// named pipe.
/// The socket type is determined by parsing `endpoint`: If it's a valid IPv4
/// or IPv6 address in 'ip:[port]' format, then it will be a TCP socket.
/// Otherwise it will be a Unix socket or named pipe (Windows).
///
/// If no port is given, a random one will be assigned.
///
/// @param endpoint Address of the server. Either a 'ip:[port]' string or an
/// arbitrary identifier (trimmed to 256 bytes) for the Unix
/// socket or named pipe.
/// @returns 0 on success, 1 on a regular error, and negative errno
/// on failure to bind or connect.
/// on failure to bind or listen.
int server_start(const char *endpoint)
{
if (endpoint == NULL) {
ELOG("Attempting to start server on NULL endpoint");
if (endpoint == NULL || endpoint[0] == '\0') {
ELOG("Empty or NULL endpoint");
return 1;
}
SocketWatcher *watcher = xmalloc(sizeof(SocketWatcher));
socket_watcher_init(&main_loop, watcher, endpoint, NULL);
socket_watcher_init(&main_loop, watcher, endpoint);
// Check if a watcher for the endpoint already exists
for (int i = 0; i < watchers.ga_len; i++) {
if (!strcmp(watcher->addr, ((SocketWatcher **)watchers.ga_data)[i]->addr)) {
ELOG("Already listening on %s", watcher->addr);
if (watcher->stream->type == UV_TCP) {
uv_freeaddrinfo(watcher->uv.tcp.addrinfo);
}
socket_watcher_close(watcher, free_server);
return 1;
}