39
EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

Embed Size (px)

Citation preview

Page 1: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

EECS340 Recitation 2: Very helpful to your project

Hongyu Gao

1

Page 2: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

Warming-up Questions

If you don’t know your IP address, dig into the old announcement in

the newsgroup

2

Page 3: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

3

Roadmap

How to run Minet stack Hints for project 2

Page 4: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

4

Need X-server if running remotely

Xming for Windows

Page 5: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

5

Script to run Minetstart_minet.sh somehow doesn’t work properly for me.

Use run_modules.sh instead.

Remember to set variable MINET_IPADDR and MINET_ETHERNETADDR in minet.cfg or run_modules.sh, depending on which script you use.

Page 6: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

6

Connect to tcp_server with nc

Page 7: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

7

Script to stop Minet

./stop_minet.sh

Page 8: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

8

Roadmap

How to run Minet stack Hints for project 2

Page 9: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

9

File to Modify

src/core/tcp_module.cc

Implement your TCP in this file

Page 10: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

10

Refer to udp_module.cc

How to pass data to application layer (sockets)?How to receive data from application layer (sockets)?How to pass data to IP layer?How to receive data from IP layer?

MinetSend() and MinetReceive()

Page 11: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

11

Understand TCP Protocol!

Page 12: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

12

Sneak Peek: Talk with IP Layer

pt2DFIPFunction DFIPFuncArray[NUM_TCP_STATES] = { &DFIPNull, // CLOSED &DFIPNull, // LISTEN *DFIPListen is not dispatched from this table* &DFIPSynRcvd, // SYN_RCVD &DFIPSynSent, // SYN_SENT &DFIPSynSent1, // SYN_SENT1 &DFIPEstablished, // ESTABLISHED &DFIPSendData, // SEND_DATA &DFIPCloseWait, // CLOSE_WAIT &DFIPFinWait1, // FIN_WAIT1 &DFIPClosing, // CLOSING &DFIPLastAck, // LAST_ACK &DFIPFinWait2, // FIN_WAIT2 &DFIPTimeWait // TIME_WAIT };

Page 13: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

13

Sneak Peek: Talk with Sockets

pt2DFSOCKFunction DFSOCKFuncArray[NUM_SOCK_TYPES] = { &DFSOCKConnect, // CONNECT &DFSOCKAccept, // ACCEPT &DFSOCKWrite, // WRITE &DFSOCKForward, // FORWARD &DFSOCKClose, // CLOSE &DFSOCKStatus // STATUS };

Page 14: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

14

Please make your code well-commented

Next, some slides that previous TAs gave to the previous students…

Page 15: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

What to do in muxHandler()?

(starting from line 50)

From Anonymous TA1

Page 16: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

Get the headers

Use function MinetReceive to get the packet p.

Get the TCPHeader and IPHeader of p. You can use the function in class

Packet. For example, FindHeader function. You can get the Tcpheader by using p.FindHeader(Headers::TCPHeader);

Page 17: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

Find the connections

Extract information from the IPHeaders and TCPHeaders. For example, src ip & port and dest ip &port. For example, using iph.GetDestIP function.(suppose iph is the ip header)

Use the information above to find the connection the packet belongs in Clist.

Clist is used for keeping all connections built. For example, using ConnectionList<TCPState>::iterator cs = clist.FindMatching(c);// Specify c with src & dest information

Page 18: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

Parse tcp header

Get the state of current connection. You may need to modify it based on the packet received. For example, using (*cs).state.GetState();

Get the flags of TCP header, using tcph.GetFlags(f). Then check whether it is SYN, ACK, FIN.. And do corresponding action based on current state.

Page 19: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1
Page 20: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

For Example

if( IS_ACK(f) && IS_SYN(f) ) { cout<<"SYN ACK packet Received. will have to send ACK packet"<<endl; p.Print(cout); if( state == SYN_SENT ) { connstate.state.SetState(ESTABLISHED); unsigned int newack,last_recvd; tcph.GetAckNum(newack); tcph.GetSeqNum(last_recvd); connstate.state.SetLastAcked( newack ); connstate.state.SetLastRecvd( last_recvd ); connstate.timeout=Time() + ACK_TIMEOUT;

char *data1 = "ACTIVE:in active connection\n"; Buffer b(data1,strlen(data1)); Packet psh; unsigned int old_time; TCPOptions opt; tcph.GetOptions(opt); old_time = ntohl(*(int *)(opt.data+8)); craftSynPacket(&psh,connstate,PSH_ACK_HEADER,0,old_time); psh.Print(cout); MinetSend(mux,psh); connstate.state.SetLastSent(connstate.state.GetLastSent()); status=STATUS; MinetSend(sock,write); } }

Page 21: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

Netprog: TCP Details 21

TCP Details

Introduction to Networking

From Anonymous TA2

Page 22: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

Netprog: TCP Details 22

The TCP ProjectImportant Handouts:

Minet The Minet Technical Report The Minet Socket Interface

TCP Guides RFC 793 http://www.freesoft.org/CIE/RFC/793/ (in

convenient HTML format), RFC 1122 http://www.faqs.org/rfcs/rfc1122.html

A very useful summary and picture of the TCP State diagram (TCP_IP_State_Transition_Diagram.pdf)

TCP, UDP and IP pocket guide (TCP_UDP_IP_packet_guide.pdf) includes header details

Brief overview of TCP (http://www.freesoft.org/CIE/Topics/83.htm) contains a nice summary of the essentials

Here is a page with nice TCP animations (http://www.netbook.cs.purdue.edu/). They explain TCP connection startup, termination, data flow and flow control and cumulative ack concepts.

Page 23: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

Netprog: TCP Details 23

TCP Lingo

When a client requests a connection, it sends a “SYN” segment (a special TCP segment) to the server port.

SYN stands for synchronize. The SYN message includes the client’s ISN.

ISN is Initial Sequence Number.

Page 24: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

Netprog: TCP Details 24

More... Every TCP segment includes a

Sequence Number that refers to the first byte of data included in the segment.

Every TCP segment includes a Request Number (Acknowledgement Number) that indicates the byte number of the next data that is expected to be received. All bytes up through this number have

already been received.

Page 25: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

Netprog: TCP Details 25

And more...

There are a bunch of control flags: URG: urgent data included. ACK: this segment is (among other things)

an acknowledgement. RST: error - abort the session. SYN: synchronize Sequence Numbers

(setup) FIN: polite connection termination.

Page 26: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

Netprog: TCP Details 26

And more...

MSS: Maximum segment size (A TCP option)

Window: Every ACK includes a Window field that tells the sender how many bytes it can send before the receiver will have to toss it away (due to fixed buffer size, “flow control”).

Page 27: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

Netprog: TCP Details 27

TCP Connection Creation

A server accepts a connection. Must be looking for new connections!

A client requests a connection. Must know where the server is!

Page 28: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

Netprog: TCP Details 28

Client Starts

A client starts by sending a SYN segment with the following information: Client’s ISN (generated pseudo-randomly) Maximum Receive Window for client. Optionally (but usually) MSS (largest

datagram accepted). No payload! (Only TCP headers)

Page 29: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

Netprog: TCP Details 29

Sever Response When a waiting server sees a new

connection request, the server sends back a SYN segment with: Server’s ISN (generated pseudo-randomly) Request Number is Client ISN+1 Maximum Receive Window for server. Optionally (but usually) MSS No payload! (Only TCP headers)

Page 30: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

Netprog: TCP Details 30

Finally

When the Server’s SYN is received, the client sends back an ACK with: Request Number is Server’s ISN+1

Page 31: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

Netprog: TCP Details 31

Client Server

SYNISN=X

SYNISN=X

1

SYNISN=Y ACK=X+1

SYNISN=Y ACK=X+1

2

ACK=Y+1ACK=Y+1 3

time

Page 32: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

Netprog: TCP Details 32

Why 3-Way?

Why is the third message necessary?

HINTS: TCP is a reliable service. IP delivers each TCP segment. IP is not reliable.

Page 33: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

Netprog: TCP Details 33

TCP Data and ACK

Once the connection is established, data can be sent.

Each data segment includes a sequence number identifying the first byte in the segment.

Each segment (data or empty) includes a request number indicating what data has been received.

Page 34: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

Netprog: TCP Details 34

TCP Buffers

Both the client and server allocate buffers to hold incoming and outgoing data The TCP layer does this.

Both the client and server announce with every ACK how much buffer space remains (the Window field in a TCP segment).

Page 35: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

Netprog: TCP Details 35

Send Buffers

The application gives the TCP layer some data to send.

The data is put in a send buffer, where it stays until the data is ACK’d. it has to stay, as it might need to be sent

again! The TCP layer won’t accept data from

the application unless (or until) there is buffer space.

Page 36: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

Netprog: TCP Details 36

ACKs

A receiver doesn’t have to ACK every segment (it can ACK many segments with a single ACK segment).

Each ACK can also contain outgoing data (piggybacking).

If a sender doesn’t get an ACK after some time limit (MSL) it resends the data.

Page 37: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

Netprog: TCP Details 37

Termination

The TCP layer can send a RST segment that terminates a connection if something is wrong.

Usually the application tells TCP to terminate the connection politely with a FIN segment.

Page 38: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

Netprog: TCP Details 38

FIN

Either end of the connection can initiate termination.

A FIN is sent, which means the application is done sending data.

The FIN is ACK’d. The other end must now send a FIN. That FIN must be ACK’d.

Page 39: EECS340 Recitation 2: Very helpful to your project Hongyu Gao 1

Netprog: TCP Details 39

App1 App2

FINSN=X

FINSN=X

1

ACK=X+1ACK=X+12

ACK=Y+1ACK=Y+1 4

FINSN=Y

FINSN=Y

3..

.