
# Network

> Give the guest's TCP/IP stack a way out.

```js
import { createKernel, directBackend, wispBackend } from "unkernel";

await createKernel({ net: directBackend() }); // Node: host sockets
await createKernel({ net: wispBackend("wss://wisp.example/") }); // anywhere: your Wisp proxy
```

Networking is off unless you pass `net`. The guest runs Linux's own TCP/IP stack, with DHCP and DNS; the backend carries its TCP connections and UDP flows.

- **`directBackend()`** (Node) opens real TCP and UDP sockets on the host. With `hostLoopback` in the network spec, the gateway (10.0.2.2) reaches the host's 127.0.0.1.
- **`wispBackend(servers)`** goes through a [Wisp](https://github.com/MercuryWorkshop/wisp-protocol) proxy that you run or choose (one URL, or several to race), with UDP where the server carries it. Browsers can't open raw TCP sockets, so this is the backend for the web. The proxy sees the host names and ports the guest connects to and plain-HTTP content; TLS from the guest is end to end. In Node, pass `headers` (e.g. `{ Origin: "https://your.site" }`) if your proxy checks the Origin.

Any object with `connect` and `lookup` methods can carry the guest's TCP and DNS, and with a `udp` method its UDP, so you can write your own backend. To join a tailnet, see [Tailscale](/sdk/tailscale).

## Wisp limits

Wisp carries streams and nothing else, and a guest program can tell:

- **No half-close.** When the guest shuts down its sending side (`shutdown(SHUT_WR)`, which `nc` does at the end of its input), the server is not told. A client that waits for the server to see end of file before it answers hangs, such as `echo hi | nc HOST PORT` against a server that reads to the end, or an HTTP/1.0 request with a body and no Content-Length.
- **No DNS.** Names resolve to placeholder addresses in 198.18.0.0/15, and the server resolves the name when the guest connects. Every name resolves (a bad one fails at connect time, as a reset), the address the guest sees is not the host's, and each name has one address.
- **No connect acknowledgement.** A connect succeeds at once; a refused or unreachable host shows up as a reset.

For real addresses, pass a resolver of your own, such as DNS over HTTPS. It takes a name and returns IPv4 addresses, or rejects with `{ code: "ENOTFOUND" }` for a name that does not exist:

```js
await createKernel({ net: wispBackend(servers, { lookup: (name) => resolveOverHttps(name) }) });
```

For half-close, use a backend of your own over a transport that has it.

::note
No IPv6, no ICMP beyond the gateway, and no inbound connections (except from tailnet peers with [Tailscale](/sdk/tailscale)). To reach a server in the guest, use [`vm.connect()` and `vm.fetch()`](/sdk/vsock#reach-guest-servers), which go over vsock.
::

## Answer HTTP from the host

```js
import { fetchRoutes, wispBackend } from "unkernel";

const net = fetchRoutes(wispBackend("wss://wisp.example/"), {
  "deb.debian.org": "https://debian-mirror.example", // http://deb.debian.org/x → https://debian-mirror.example/x
  "api.local": (req) => Response.json({ path: new URL(req.url).pathname }), // your own handler
});
await createKernel({ net });
```

`fetchRoutes(inner, routes)` answers the guest's plain-HTTP requests (port 80, GET and HEAD) to the named hosts with the host's `fetch()`, and passes everything else to `inner`. Pass `undefined` for `inner` to allow only the routes. A string route rewrites the URL's prefix and forwards only the range and cache headers; in a browser the target must allow them through CORS. A function gets a `Request` with the guest's headers. Wisp specs take string routes as `routes`. [unkernel.dev](https://unkernel.dev/) uses this to fetch Debian packages from a CORS mirror instead of through its proxy.
