% lec 17: network programming # today's plan DNS & HTTP programming abstraction: socket (today: client-side socket) (later this week: server-side socket & case study - node.js) # big picture what happens when browsing . . . protocol: HTTP domain name: `www.cs.uw.edu` (IP address: `128.208.3.118`) port: 80 (default) q: can we directly use `128.208.3.118` in browser? # Internet protocol suite (TCP/IP) ``` {.ditaa #l17/tcp-ip} +-------------+ +-------------+ | application | | application | +-------------+ +-------------+ | ^ v | +-------------+ +-------------+ | transport | | transport | +-------------+ +-------------+ | ^ v | +-------------+ +-------------+ | internet | | internet | +-------------+ +-------------+ | ^ v | +-------------+ +-------------+ | link | | link | +-------------+ +-------------+ | ^ v | ------------------------------------------- ethernet/wifi/fiber/satellite/... ``` [see also: OSI model] # protocol examples application layer: DNS, HTTP, SSH, ... transport layer: TCP, UDP, ... internet layer: IPv4, IPv6, ... # overview translate `www.cs.uw.edu` to `128.208.3.118` (DNS) connect to `128.208.3.118` (TCP) send request to fetch data (HTTP) # DNS a naming system (recall file system) map domain names to IP addresses example: `www.cs.uw.edu` → `128.208.3.118` . . . q: can one domain name map to multiple IP addresses? q: can multiple domain names map to the same IP address? # DNS resolution a single file (`HOSTS.TXT` or `/etc/hosts`)? scalability? . . . hierarchy root: `.` generic top-level domain (gTLD): `edu` [draw google.com, mit.edu] # root servers [13 logical root servers](http://www.root-servers.org/) run: `dig . ns` demo: resolve `www.cs.uw.edu` . . . q: where's `mit.edu` (from Seattle)? # programming interface manpage: `getaddrinfo`, `freeaddrinfo`, `getnameinfo` [example: dns.c](l17/dns.c.html) # more on DNS MIT 6.033's [DNS hands-on](http://web.mit.edu/6.033/www/assignments/handson-dns.html) take [CSE 461](https://courses.cs.washington.edu/courses/cse461/) # what's next now we have the IP address how to connect to the server and fetch data? # socket programming ``` {.ditaa #l17/syscall} +---------------+ +---------------+ | application | | application | +---------------+ <---socket syscalls---> +---------------+ | OS kernel | | OS kernel | +---------------+ +---------------+ | NIC | | NIC | +-------+-------+ +-------+-------+ | | +-----------------------------------------+ ``` # client-side socket ```c // return a socket (file descriptor) int socket(int domain, int type, int protocol); // connect to a server int connect(int fd, const struct sockaddr *addr, socklen_t addrlen); ``` use `socket`/`connect` to start a client-side socket (not `open`) you can use `read`/`write` on sockets (or `recv`/`send`) `close` when done [example: http.c](l17/http.c.html) # HTTP protocol request/response protocol, over TCP, default port 80 client (e.g., browser) sends a request server produces a response [HTTP/1.1 textual](http://www.w3.org/Protocols/rfc2616/rfc2616.html) (HTTP/2 binary) # request format ```c [method] [uri] HTTP/[version]\r\n [name]: [value]\r\n ... [name]: [value]\r\n \r\n [body] ``` methods: GET, POST, HEAD, ... . . . demo (chrome & httpie): `http -v www.cs.uw.edu` # response format ```c HTTP/[version] [status code] [reasons]\r\n [name]: [value]\r\n ... [name]: [value]\r\n \r\n [body] ``` status code 1xx: some kind of informational message 2xx: success of some kind 3xx: redirects the client to a different URL 4xx: the client’s request contained some error 5xx: the server experienced an error # see you on Wednesday server-side socket