Welcome to WebBSD!

# This is a minimal WebBSD system. It doesn't even have networking setup.
# Let's explore URLs and their syntax. Then, we'll set things up.

$ ls :
pci tty
$ ls tty://
localhost
$ ls tty://localhost/
bash
$ # Shorter URL syntax is available, but long-form syntax is good for learning.
$ readlink --canonicalize tty:
tty://
$ readlink --canonicalize tty:/
tty://localhost/

# Did you see tty://localhost/bash?
# We're connected to that right now. Cool, eh?
# 
# Opening the resource at that URL launches a new bash shell. After
# we setup networking, we'll use tty:// to SSH into other machines.
# 
# Now, let's connect to the internet.

================================ 
=========== Ethernet =========== 
================================ 

# 1. Find our ethernet card
$ ls pci://
localhost
$ ls pci://localhost/
by-type/ by-id/ by-uuid/
$ ls pci://localhost/by-type/
display/ ethernet/ keyboard/ mouse/ unknown/ wifi/
$ ls pci://localhost/by-type/ethernet/
rtl8168
$ # We can use the `info` command to learn more about a URL.
$ info pci://localhost/by-type/ethernet/rtl8168
02:00.0 Ethernet controller: Realtek Semiconductor Co LTD RTL8111/8168/8411

# 2. Setup a network:// interface using the `hw-rtl8168d` driver.
$ hw-rtl8168d --help
Driver for Realtek Semiconductor rtl8168.
Usage: hw-rtl8168d --import pci:<rtl8168> --export network:<domain name>
$ # The angle brackets don't mean anything; they're just placeholders.
$ # Let's fill them in.
$ hw-rtl8168d --import pci://localhost/by-type/ethernet/rtl8168 \
              --export network://local-ethernet # domain name chosen at random

# 3. Setup more hardware-agnostic protocols using the `translate-net` command.
$ translate-netd --help
Daemon to translate [ tcp: udp: ip: ethernet: ] to network: 
Usage: translate-netd --import network:<domain> \
                      --export tcp:<domain> udp:<domain> ...
$ # Shell expansion syntax can help us be concise
$ echo {ethernet,ip,udp,tcp}://local-ethernet
ethernet://local-ethernet ip://local-ethernet udp://local-ethernet tcp://local-ethernet 
$ translate-netd --import network://local-ethernet --export {ethernet,ip,udp,tcp}://local-ethernet 

# 4. Wrap tcp: with socket: so we can use shell utilities that expect a stream
#    but don't know about the details of tcp: specifically.
$ translate-tcpd --import tcp://local-ethernet --export +socket://tcp.local-ethernet

# `tcp.local-ethernet` is a regular domain, just like `local-ethernet`.
# Behind the scenes, `translate-tcpd` does the following translation:
#     +socket://$subdomains.tcp.local-ethernet -> tcp://$subdomains.local-ethernet

# When we originally created the `local-ethernet` domain, it only supported the
# network: protocol. We used the `translate-netd` command to make that domain
# support ethernet:, ip:, udp:, and  tcp:. Behind the scenes, `translate-netd`
# is translating these new protocols to network://local-ethernet.

# rBSD has three types of protocols:
#  1. +stream://   - used by tcp://, http://, etc
#  2. +datagram:// - used by udp://, ntp://, tftp://, etc
#  3. +block://    - used by file://, disk://, etc


================================ 
===== Explore IP Networking ====
================================ 


# Let's explore the networking setup we have.
$ ls :
ethernet ip network pci socket tcp tty udp
$ info +socket://tcp.local-ethernet
This is a socket for accessing the internet. It has subdomains to 
connect to machines at specific IP addresses.
$ stream-client +socket://93.184.216.34.tcp.local-ethernet \
                --send 'GET / HTTP/1.1\nHost: example.com\n' \
                --num-lines-to-read 3
HTTP/1.1 200 OK
Accept-Ranges: bytes
Age: 517104


================================ 
============== DNS =============
================================ 

# Awesome! Subdomains are super helpful, and we'll soon see some cool things
# we can do with them. Now let's setup world-wide web DNS. That's not too hard.
$ www-dnsd --import ://local-ethernet --export ://www-dns.local-ethernet

# We used the ://domain-name syntax to select that domain for all protocols
# it implements, both now and in the future. Let's try this out:
$ stream-client +socket://example.com.www-dns.tcp.local-ethernet \
                --send 'GET / HTTP/1.1\nHost: example.com\n' \
                --num-lines-to-read 3
HTTP/1.1 200 OK
Accept-Ranges: bytes
Age: 517104

# Perfect! Remember that we have our `translate-tcpd` daemon in the background.
# It translates from +socket://example.com.www-dns.tcp.local-ethernet
#                 to tcp://example.com.www-dns.local-ethernet



================================ 
=========== Clean Up ===========
================================ 

# Let's setup HTTP. Because it's transport-agnostic,
# we can use the `passthrough` command to pass http://local-ethernet data
# to our +socket://tcp host.
$ passthrough --import +socket://tcp.local-ethernet --export http://tcp.local-ethernet 
$ curl http://example.com.www-dns.tcp.local-ethernet | head -n 4
<!doctype html>
<html>
<head>
  <title>Example Domain</title>

# Awesome! Lastly, let's `chroot` so we don't have to deal with
# that long domain suffix:
$ chroot ://www-dns.tcp.local-ethernet
$ curl http://example.com | head -n 4
<!doctype html>
<html>
<head>
  <title>Example Domain</title>