sockets & traceroute€¦ · socket api (generalized) usimple application-layer abstractions...

21
Computer Networks The Socket API, DNS Lookup & more Originally By David Wetherall (djw@), Modified By Qian Yan (qiany7@)

Upload: others

Post on 05-Apr-2020

21 views

Category:

Documents


0 download

TRANSCRIPT

Computer NetworksThe Socket API, DNS Lookup & more

Originally By David Wetherall (djw@), Modified By Qian Yan (qiany7@)

Network-Application Interface

u Defines how apps use the network

u Application Layer APIs

u Lets apps talk to each other

u hides the other layers of the network

Computer Networks2

host

appapp

host

network

Socket API (Generalized)

u Simple application-layer abstractions (APIs) to use the network

u The network service API used to write all Internet applications

u Part of all major OSes and languages; originally Berkeley (Unix) ~1983

u Two kinds of sockets

u Streams (TCP): reliably send a stream of bytes

u Datagrams (UDP): unreliably send separate messagesComputer Networks 3

Socket API (2)

u Sockets let apps attach to the local network at different ports

u Ports are used by OS to distinguish services/apps using internet

Computer Networks 4

SocketPort 1

SocketPort 2

Socket API (3)

Computer Networks 5

Primitive Meaning

SOCKET Create a new communication endpointBIND Associate a local address (port) with a socket

LISTEN Announce willingness to accept connections; (give queue size)

ACCEPT Passively establish an incoming connectionCONNECT Actively attempt to establish a connectionSEND Send some data over the connectionRECEIVE Receive some data from the connectionCLOSE Release the connection

https://docs.oracle.com/javase/8/docs/api/java/net/Socket.htmlhttps://docs.oracle.com/javase/8/docs/api/java/net/ServerSocket.html

Using Sockets

Computer Networks 6

Client (host 1) Server (host 2)Time

Using Sockets (2)

Computer Networks 7

Client (host 1) Server (host 2)Time

request

reply

disconnect

1 1

2

3

44

connect

Using Sockets (3)

Computer Networks 8

Client (host 1) Server (host 2)Time

5: connect*

1: socket 2: (bind)1: socket

3: (listen)

9: send

6: recv*

4: accept*

7: send

8: recv*

10: close 10: close

request

reply

disconnect

connect

*= call blocks

Client Program (outline)

socket() // make socket

getaddrinfo() // server and port name

// www.example.com:80

connect() // connect to server [block]

send() // send request

recv() // await reply [block]

… // do something with data!

close() // done, disconnect

Computer Networks 9

Server Program (outline)

socket() // make socket

getaddrinfo() // for port on this host

bind() // associate port with socket

listen() // prepare to accept connections

accept() // wait for a connection [block]

recv() // wait for request

send() // send the reply

close() // eventually disconnect

Computer Networks 10

Blocking, Non-blocking calls

Socket calls recv() and send() can beblocking/nonblocking.

Default: blocking, can be changed with fcntl() <-modifies the file descriptor.

Computer Networks 11

• https://www.scottklement.com/rpg/socktut/nonblocking.html

• http://www.masterraghu.com/subjects/np/introduction/unix_network_programming_v1.3/ch25lev1sec2.html

Java Examples with Socket & ServerSocket

u Server

Computer Networks 12

• http://cs.lmu.edu/~ray/notes/javanetexamples/

• https://docs.oracle.com/javase/tutorial/networking/datagrams/clientServer.html

• https://docs.oracle.com/javase/tutorial/networking/sockets/index.html

u Client

ServerSocket listener = new ServerSocket(9090);try {

while (true) {Socket socket = listener.accept();try {

socket.getInputStream();} finally {

socket.close();}

}}finally {

listener.close();}

Socket socket = new Socket(server, 9090);out =

new PrintWriter(socket.getOutputStream(), true);socket.close();

Dig & DNS

u Recursively query local/ISP DNS

u Local DNS non-recusively query from top-down

u Use dig to trace the process

u Reverse DNS lookup

u IP -> server domain

• https://www.golinuxhub.com/2014/01/how-does-dns-query-works-when-you-type.html

• https://en.wikipedia.org/wiki/Reverse_DNS_lookup

ping & nmap

u Ping uses ICMP protocol which is on top of Network layer.

u nmap

u Send TCP/UDP packet to specific host and port and examine the response

• https://en.wikibooks.org/wiki/Communication_Networks/Ping

• https://resources.infosecinstitute.com/nmap/

Traceroute

u Apps talk to other apps with no real idea of what is inside the network

u This is good! But you may be curious …

u Peeking inside the Network with Traceroute

host

appapp

host

???

Traceroute

u Widely used command-line tool to let hosts peek inside the network

u On all OSes (tracert on Windows)

u Developed by Van Jacobson ~1987

u Uses a network-network interface (IP) in ways we will explain later

Computer Networks 16

: Credit: Wikipedia (public domain)

Van Jacobson

Traceroute

uProbes successive hops to find network path

uTTL: time-to-live

Computer Networks 17

. . .

LocalHost Remote

Host

Traceroute

Computer Networks 18

. . .

LocalHost Remote

Host

1 hop 2 hops3 hops N-1 hops

N hops

Using Traceroute

Computer Networks 19

Using Traceroute (2)

u ISP names and places are educated guesses

Computer Networks 20

. . .

My computer www.uw.edu(www1.cac.washington.edu)

tde3 hops

Telefonica4 hops

Level36 hops

pnw-gigapop1 hop

UW3 hops

NYC San Jose Seattle

UW

Home1 hop

100 ms180 ms

>200 ms

END

Computer Networks 21

© 2013 D. Wetherall

Slide material from: TANENBAUM, ANDREW S.; WETHERALL, DAVID J., COMPUTER NETWORKS, 5th Edition, © 2011. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle

River, New Jersey