# C library

Source: https://tiltas.openpanel.sh/docs/sdks/c

> libtiltas and tiltas.h, the C core under the Python and Node.js packages, for bindings in other languages.

`libtiltas` is the Go package built as a shared library with a small C interface, in the spirit of Tailscale's
`libtailscale`. The Python and Node.js packages are thin layers over it; bind it the same way from any language that can
call C.

## Build

From a copy of the Tiltas repository, with Go 1.27.4 and a C compiler:

```sh
sdk/c/build.sh
```

It writes `sdk/c/dist/<os>-<arch>/` with `libtiltas.so` (Linux), `libtiltas.dylib` (macOS) or `libtiltas.dll`
(Windows), `tiltas.h`, the licenses and `MODULES.txt`, the list of every Go module inside. Give a target to cross-build,
with `CC` set to a cross compiler: `CC=aarch64-linux-gnu-gcc sdk/c/build.sh linux/arm64`. With `ENGINE_DIR` set to a
copy of the Tiltas engine, the network code is built from it, at that commit.

## Conventions

- Handles are `int`s greater than 0. Functions return `0` on success and `-1` on failure; `tiltas_errmsg` says why.
  Errors never contain a Join key, a Device token or a Coordinator key.
- Strings are UTF-8 and end with a NUL.
- Functions that return text write it into your buffer like `snprintf`: they return the full length, and if that is
  not less than your buffer's size, call again with a larger one.
- Every function may be called from any thread. `tiltas_up` and `tiltas_close` block: keep them off a UI or event-loop
  thread.
- The Join key is read from `TILTAS_JOIN_KEY` or a file, never passed as an argument. The environment is read when
  `tiltas_up` runs, so a program may set it first.

## Functions

| Function | What it does |
|---|---|
| `int tiltas_version(char *buf, size_t len)` | The library version. |
| `tiltas tiltas_new(void)` | A new Server, not yet connected. |
| `int tiltas_set_name(tiltas sd, const char *name)` | Options, all before `tiltas_up`. Empty strings keep the default. Also `tiltas_set_panel`, `tiltas_set_join_key_file`, `tiltas_set_dir`, `tiltas_set_persistent(sd, int)`, `tiltas_set_app_version`, `tiltas_set_log_stderr(sd, int)`. |
| `int tiltas_up(tiltas sd, int timeout_ms)` | Joins on the first start and connects; `timeout_ms` 0 means 60 s. |
| `int tiltas_close(tiltas sd)` | Disconnects and frees `sd`; an ephemeral Device leaves the Network. On failure, `tiltas_errmsg(0)` says why. |
| `int tiltas_listen(tiltas sd, const char *network, const char *addr, const char *forward_to, tiltas_listener *out)` | Listens on the Network at `addr` (`":80"`) and forwards each connection to `forward_to`, a loopback address such as `"127.0.0.1:8080"` where your own server listens. TCP only. |
| `int tiltas_listener_close(tiltas_listener ln)` | Stops a listener. |
| `int tiltas_proxy(tiltas sd, char *buf, size_t len)` | Starts a SOCKS5 proxy on loopback for dialing into the Network and returns `{"address","username","password"}` as JSON. Names are resolved on the Network. |
| `int tiltas_whois(tiltas sd, const char *addr, char *buf, size_t len)` | The identity behind an address of the Network, or behind the loopback peer of a forwarded connection, as JSON. |
| `int tiltas_info(tiltas sd, char *buf, size_t len)` | This Device: `{"device_id","domain","addresses","version","engine"}`. |
| `int tiltas_errmsg(tiltas sd, char *buf, size_t len)` | The last error on `sd`; `sd` 0 for calls that had no valid handle. |

## Why forward and proxy

`tiltas_listen` hands connections to a loopback port, and dialing goes through a loopback SOCKS5 proxy. Every language
can listen on and connect to loopback with its own socket API, on every system, so a binding needs no file descriptor
passing and no blocking call per connection. `tiltas_whois` knows which Network caller is behind each forwarded
connection, by the loopback port it came from. Programs on the same computer can reach these loopback ports too: the
proxy needs its password, and `tiltas_whois` refuses connections that the library did not forward.

## Example

```c
#include <stdio.h>
#include "tiltas.h"

int main(void) {
	char buf[512];
	tiltas sd = tiltas_new();
	tiltas_set_name(sd, "c-worker");
	if (tiltas_up(sd, 60000) != TILTAS_OK) {
		tiltas_errmsg(sd, buf, sizeof buf);
		fprintf(stderr, "%s\n", buf);
		return 1;
	}
	tiltas_info(sd, buf, sizeof buf);
	printf("%s\n", buf);
	tiltas_close(sd);
	return 0;
}
```

Link it with `-ltiltas` and the directory of the library. The ABI's own test, `sdk/c/test/smoke.c`, runs on every change.
