WARNING: Pure AI slop. Use with care.
A minimal LEELEN (立林) video-intercom <-> SIP converter, written from
scratch against the reverse-engineered protocol notes in
docs/leelen-sip.md.
Only the core call path is implemented — deliberately nothing else:
- LEELEN discovery (UDP 6789 solicitation / advertisement) to resolve
dddd-ddddnumbers to device addresses - LEELEN signaling (UDP 5060, 8-byte little-endian header + text fields), message codes CALL/VIEW/OK/BYE/ACCEPTED
- minimal SIP over UDP (RFC 3261 subset): INVITE / 100 / 180 / 200 / ACK / BYE with SDP (PCMA audio, JPEG video)
- RTP forwarding in both directions (audio + video); RTCP is not forwarded
Not implemented (on purpose): control protocol (17722), cloud platform, SIP auth/TCP/TLS, re-INVITE, call hold/transfer, RTCP, STUN/NAT traversal.
| LEELEN | SIP |
|---|---|
| CALL / VIEW | INVITE |
| ACCEPTED | 200 OK |
| OK | transport ack (retrans stop) |
| BYE | BYE |
- LEELEN -> SIP: a panel CALLs a LEELEN number; the converter INVITEs the SIP client that registered that number (see below). Caller number goes in the From user part. Calls for numbers nobody registered are ignored.
- SIP -> LEELEN: an INVITE whose Request-URI user part is a LEELEN number
(
sip:9999-0002@converteror digit-style99990002) makes the converter discover that number and send it a CALL.
The converter is a minimal SIP registrar (UDP, no auth). A client registers the LEELEN number it wants to receive calls for by putting it in the To header of a REGISTER:
REGISTER sip:converter:5060 SIP/2.0
To: <sip:9999-0001@converter>
Contact: <sip:9999-0001@client:5060>
Expires: 300
Multiple clients may register different numbers at the same time
(latest registration wins for a number; Expires: 0 unregisters;
registrations expire after at most 1 hour). Registered numbers are
bound to the source address of the REGISTER.
There is no static SIP destination: every SIP leg of a call is routed to a registered client only.
Designed as a trusted-LAN appliance; all of the following are deliberate:
- LEELEN signaling is accepted from any source and matched by dialog id only -- any LAN host can forge BYE/ACCEPTED for an ongoing call (the protocol has no authentication at all).
- SIP has no authentication; anyone may REGISTER.
- SIP tag/branch tokens come from a time-based PRNG (predictable). Fine on a LAN; use a proper random source before exposing to untrusted networks.
- No SIP UDP request retransmission on our side (relies on the peer's retransmits), no CANCEL -- a cancelled SETUP waits for its timeout and is torn down with BYE.
- Single-Via user-agent routing: responses follow the top Via only. Header folding is tolerated when reading, but requests we send are unfolded.
- Oversized requests (>1600 bytes) are rejected with 500 instead of being stored.
Number style on the SIP side is plain digits, phone-dialer friendly:
- outbound (From user part) carries digits only:
99990002; - inbound accepts any separators and reads digits only -- exactly 8 digits
(
99990002->9999-0002) or 9 digits (999900021->9999-0002-1) are accepted, anything else is rejected with 488. - Every LEELEN message is answered with OK and retransmitted (500 ms, 4 tries) until acked; unanswered calls fail the SIP side (480/503/408/404).
make # C99; requires libuv (pkg-config libuv)
Style: .clang-format plus tools/fix-defs.py for what clang-format
cannot express (wrapped function definition parameters take one extra
indent level; top-level function definitions are surrounded by exactly
two blank lines):
clang-format -i src/*.c src/*.h && tools/fix-defs.py src/*.c src/*.h
Typical deployment — client on the intranet, gateway facing a dedicated network (back-to-back). The LEELEN side and the SIP side bind and route independently; IPv4/IPv6 in any combination:
./leelen2sip \
--leelen-bind 192.168.1.5 --leelen-port 5060 \
--sip-bind 10.0.0.7 --sip-port 5060 \
--my-number 9999-0001 --media-base 7078
Bind addresses (--leelen-bind / --sip-bind, or --listen to set both):
*(default) listen on IPv4 and IPv6 wildcards- a literal listen on that address only (
0.0.0.0,::,192.168.1.5,2001:db8::5, ...)
The RTP/SDP address advertised to the SIP side is auto-detected per
address family (UDP connect trick) or set with --media-ip. Discovery
is multicast
(224.0.0.1 / ff02::1) on each configured family; use
--discovery-target ADDR[:PORT] for directed solicitation when multicast
does not span the segments (routed networks).
--leelen-port and --sip-port must differ when both sides share one
IP (LEELEN devices expect 5060). Each session occupies 4 UDP ports
starting at --media-base (+8 per session slot).
--media-passthrough writes the LEELEN device's real address and RTP
ports into the SDP, so the SIP side streams media straight to the device
whenever both sides share an address family and can route to each other.
Two caveats, both inherent to the LEELEN protocol (its media fields carry
only a port; a device always sends its return media to its signaling
peer, i.e. the converter):
- the device -> SIP direction still relays through the converter;
- when the two sides use different address families (v4 vs v6) the SDP automatically falls back to the relayed form.
python3 test/e2e.py
Emulates a LEELEN device (discovery + CALL/BYE + RTP) and a SIP softphone (INVITE/200/BYE + RTP) on loopback and exercises both call directions end-to-end. Note: the device emulator binds UDP 6789.
src/leelen.c/.h— number validation, discovery codec, signaling codecsrc/sip.c/.h— minimal SIP message parser/builders + SDPsrc/sock.c/.h— libuv UDP socket layer (bind/send helpers, dual-stack)src/cdefs.h— ATTR_ACCESS/ATTR_NONNULL macros for buffer parameters (plain pointers, never array declarators; GCC-only, empty elsewhere)src/main.c— config, session state machine, poll event loop, LEELEN retransmission, discovery cache, RTP forwarding
MIT — see LICENSE.