aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/msgpack_rpc/server.c
blob: ab1b04d73f490a2c9e26f82a4eecad0a5500ec75 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

#include <uv.h>

#include "nvim/msgpack_rpc/channel.h"
#include "nvim/msgpack_rpc/server.h"
#include "nvim/os/os.h"
#include "nvim/ascii.h"
#include "nvim/eval.h"
#include "nvim/garray.h"
#include "nvim/vim.h"
#include "nvim/memory.h"
#include "nvim/log.h"
#include "nvim/tempfile.h"
#include "nvim/path.h"

#define MAX_CONNECTIONS 32
#define ADDRESS_MAX_SIZE 256
#define NVIM_DEFAULT_TCP_PORT 7450
#define LISTEN_ADDRESS_ENV_VAR "NVIM_LISTEN_ADDRESS"

typedef enum {
  kServerTypeTcp,
  kServerTypePipe
} ServerType;

typedef struct {
  // Pipe/socket path, or TCP address string
  char addr[ADDRESS_MAX_SIZE];

  // Type of the union below
  ServerType type;

  // TCP server or unix socket (named pipe on Windows)
  union {
    struct {
      uv_tcp_t handle;
      struct sockaddr_in addr;
    } tcp;
    struct {
      uv_pipe_t handle;
    } pipe;
  } socket;
} Server;

static garray_T servers = GA_EMPTY_INIT_VALUE;

#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "msgpack_rpc/server.c.generated.h"
#endif

/// Initializes the module
bool server_init(void)
{
  ga_init(&servers, sizeof(Server *), 1);

  bool must_free = false;
  const char *listen_address = os_getenv(LISTEN_ADDRESS_ENV_VAR);
  if (listen_address == NULL || *listen_address == NUL) {
    must_free = true;
    listen_address = (char *)vim_tempname();
  }

  bool ok = (server_start(listen_address) == 0);
  if (must_free) {
    xfree((char *) listen_address);
  }
  return ok;
}

/// Retrieve the file handle from a server.
static uv_handle_t *server_handle(Server *server)
{
  return server->type == kServerTypeTcp
    ? (uv_handle_t *)&server->socket.tcp.handle
    : (uv_handle_t *) &server->socket.pipe.handle;
}

/// Teardown a single server
static void server_close_cb(Server **server)
{
  uv_close(server_handle(*server), free_server);
}

/// Set v:servername to the first server in the server list, or unset it if no
/// servers are known.
static void set_vservername(garray_T *srvs)
{
  char *default_server = (srvs->ga_len > 0)
    ? ((Server **)srvs->ga_data)[0]->addr
    : NULL;
  set_vim_var_string(VV_SEND_SERVER, (char_u *)default_server, -1);
}

/// Teardown the server module
void server_teardown(void)
{
  GA_DEEP_CLEAR(&servers, Server *, server_close_cb);
}

/// 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.
///
/// @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.
int server_start(const char *endpoint)
  FUNC_ATTR_NONNULL_ALL
{
  char addr[ADDRESS_MAX_SIZE];

  // Trim to `ADDRESS_MAX_SIZE`
  if (xstrlcpy(addr, endpoint, sizeof(addr)) >= sizeof(addr)) {
    // TODO(aktau): since this is not what the user wanted, perhaps we
    // should return an error here
    WLOG("Address was too long, truncated to %s", addr);
  }

  // Check if the server already exists
  for (int i = 0; i < servers.ga_len; i++) {
    if (strcmp(addr, ((Server **)servers.ga_data)[i]->addr) == 0) {
      ELOG("Already listening on %s", addr);
      return 1;
    }
  }

  ServerType server_type = kServerTypeTcp;
  Server *server = xmalloc(sizeof(Server));
  char ip[16], *ip_end = strrchr(addr, ':');

  if (!ip_end) {
    ip_end = strchr(addr, NUL);
  }

  // (ip_end - addr) is always > 0, so convert to size_t
  size_t addr_len = (size_t)(ip_end - addr);

  if (addr_len > sizeof(ip) - 1) {
    // Maximum length of an IPv4 address buffer is 15 (eg: 255.255.255.255)
    addr_len = sizeof(ip) - 1;
  }

  // Extract the address part
  xstrlcpy(ip, addr, addr_len + 1);

  int port = NVIM_DEFAULT_TCP_PORT;

  if (*ip_end == ':') {
    // Extract the port
    long lport = strtol(ip_end + 1, NULL, 10); // NOLINT
    if (lport <= 0 || lport > 0xffff) {
      // Invalid port, treat as named pipe or unix socket
      server_type = kServerTypePipe;
    } else {
      port = (int) lport;
    }
  }

  if (server_type == kServerTypeTcp) {
    // Try to parse ip address
    if (uv_ip4_addr(ip, port, &server->socket.tcp.addr)) {
      // Invalid address, treat as named pipe or unix socket
      server_type = kServerTypePipe;
    }
  }

  int result;
  uv_stream_t *stream = NULL;

  xstrlcpy(server->addr, addr, sizeof(server->addr));

  if (server_type == kServerTypeTcp) {
    // Listen on tcp address/port
    uv_tcp_init(uv_default_loop(), &server->socket.tcp.handle);
    result = uv_tcp_bind(&server->socket.tcp.handle,
                         (const struct sockaddr *)&server->socket.tcp.addr,
                         0);
    stream = (uv_stream_t *)&server->socket.tcp.handle;
  } else {
    // Listen on named pipe or unix socket
    uv_pipe_init(uv_default_loop(), &server->socket.pipe.handle, 0);
    result = uv_pipe_bind(&server->socket.pipe.handle, server->addr);
    stream = (uv_stream_t *)&server->socket.pipe.handle;
  }

  stream->data = server;

  if (result == 0) {
    result = uv_listen((uv_stream_t *)&server->socket.tcp.handle,
                       MAX_CONNECTIONS,
                       connection_cb);
  }

  assert(result <= 0);  // libuv should have returned -errno or zero.
  if (result < 0) {
    if (result == -EACCES) {
      // Libuv converts ENOENT to EACCES for Windows compatibility, but if
      // the parent directory does not exist, ENOENT would be more accurate.
      *path_tail((char_u *) addr) = NUL;
      if (!os_file_exists((char_u *) addr)) {
        result = -ENOENT;
      }
    }
    uv_close((uv_handle_t *)stream, free_server);
    ELOG("Failed to start server: %s", uv_strerror(result));
    return result;
  }

  // Update $NVIM_LISTEN_ADDRESS, if not set.
  const char *listen_address = os_getenv(LISTEN_ADDRESS_ENV_VAR);
  if (listen_address == NULL || *listen_address == NUL) {
    os_setenv(LISTEN_ADDRESS_ENV_VAR, addr, 1);
  }

  server->type = server_type;

  // Add the server to the list.
  ga_grow(&servers, 1);
  ((Server **)servers.ga_data)[servers.ga_len++] = server;

  // Update v:servername, if not set.
  if (STRLEN(get_vim_var_str(VV_SEND_SERVER)) == 0) {
    set_vservername(&servers);
  }

  return 0;
}

/// Stops listening on the address specified by `endpoint`.
///
/// @param endpoint Address of the server.
void server_stop(char *endpoint)
{
  Server *server;
  char addr[ADDRESS_MAX_SIZE];

  // Trim to `ADDRESS_MAX_SIZE`
  xstrlcpy(addr, endpoint, sizeof(addr));

  int i = 0;  // Index of the server whose address equals addr.
  for (; i < servers.ga_len; i++) {
    server = ((Server **)servers.ga_data)[i];
    if (strcmp(addr, server->addr) == 0) {
      break;
    }
  }

  if (i >= servers.ga_len) {
    ELOG("Not listening on %s", addr);
    return;
  }

  // Unset $NVIM_LISTEN_ADDRESS if it is the stopped address.
  const char *listen_address = os_getenv(LISTEN_ADDRESS_ENV_VAR);
  if (listen_address && STRCMP(addr, listen_address) == 0) {
    os_unsetenv(LISTEN_ADDRESS_ENV_VAR);
  }

  uv_close(server_handle(server), free_server);

  // Remove this server from the list by swapping it with the last item.
  if (i != servers.ga_len - 1) {
    ((Server **)servers.ga_data)[i] =
      ((Server **)servers.ga_data)[servers.ga_len - 1];
  }
  servers.ga_len--;

  // If v:servername is the stopped address, re-initialize it.
  if (STRCMP(addr, get_vim_var_str(VV_SEND_SERVER)) == 0) {
    set_vservername(&servers);
  }
}

/// Returns an allocated array of server addresses.
/// @param[out] size The size of the returned array.
char **server_address_list(size_t *size)
  FUNC_ATTR_NONNULL_ALL
{
  if ((*size = (size_t) servers.ga_len) == 0) {
    return NULL;
  }

  char **addrs = xcalloc((size_t) servers.ga_len, sizeof(const char *));
  for (int i = 0; i < servers.ga_len; i++) {
    addrs[i] = xstrdup(((Server **)servers.ga_data)[i]->addr);
  }
  return addrs;
}

static void connection_cb(uv_stream_t *server, int status)
{
  int result;
  uv_stream_t *client;
  Server *srv = server->data;

  if (status < 0) {
    abort();
  }

  if (srv->type == kServerTypeTcp) {
    client = xmalloc(sizeof(uv_tcp_t));
    uv_tcp_init(uv_default_loop(), (uv_tcp_t *)client);
  } else {
    client = xmalloc(sizeof(uv_pipe_t));
    uv_pipe_init(uv_default_loop(), (uv_pipe_t *)client, 0);
  }

  result = uv_accept(server, client);

  if (result) {
    ELOG("Failed to accept connection: %s", uv_strerror(result));
    uv_close((uv_handle_t *)client, free_client);
    return;
  }

  channel_from_stream(client);
}

static void free_client(uv_handle_t *handle)
{
  xfree(handle);
}

static void free_server(uv_handle_t *handle)
{
  xfree(handle->data);
}