DIRECTORY IPDefs USING [DByte, Datagram, DatagramRec, Address, SendSpecific, TCPProtocol], List USING [Nconc1], TCP USING [Error], TCPLogging USING [PrintTCPPacket], TCPOps USING [Flip, Flop, maxTCPDataLength, ourLocalAddress, pktsRexmitted, pktsSent, sendBufferLength, SetTimeout, TCPChecksum, TCPHandle, tcpHdrByteLength, tcpHdrWordLength, TCPHeaderP, tcpSegmentLife, TCPSendBuffer], TCPTransmit; TCPTransmitImpl: CEDAR PROGRAM IMPORTS IPDefs, List, TCP, TCPLogging, TCPOps EXPORTS TCPTransmit = BEGIN OPEN TCPOps, TCPTransmit; TCPSend: PROC [handle: TCPHandle, data: IPDefs.Datagram] ~ { data.inHdr.timeToLive _ tcpSegmentLife; data.inHdr.protocol _ IPDefs.TCPProtocol; data.inHdr.source _ ourLocalAddress; IF handle # NIL THEN data.inHdr.destination _ handle.foreignAddr; data.optionLength _ 0; data.dataLength _ data.dataLength + tcpHdrByteLength; TRUSTED { tcpHdrPtr: TCPHeaderP _ LOOPHOLE[@data.data]; IF handle # NIL THEN { tcpHdrPtr.sourcePort _ handle.localPort; tcpHdrPtr.dstnPort _ handle.foreignPort; }; tcpHdrPtr.unused _ 0; tcpHdrPtr.dataWordOffset _ tcpHdrWordLength; tcpHdrPtr.checksum _ TCPChecksum[data]; }; pktsSent _ pktsSent + 1; IPDefs.SendSpecific[data]; data.dataLength _ data.dataLength - tcpHdrByteLength; }; SendSegmentToNet: PROC [handle: TCPHandle, sendDatagram: IPDefs.Datagram, dataSegment: BOOL] ~ TRUSTED { tcpHdrPtr: TCPHeaderP; -- TCP header to send tcpSendBufferPtr: REF TCPSendBuffer; -- information about sent segment tcpHdrPtr _ LOOPHOLE[@sendDatagram.data]; tcpHdrPtr.seqNumber _ Flop[handle.sndNxt]; -- set sequence number tcpHdrPtr.window _ handle.rcvWnd; -- send latest window IF ~(handle.state = listen OR handle.state = synSent) THEN { -- if in synced state tcpHdrPtr.ack _ TRUE; -- send ack tcpHdrPtr.ackNumber _ Flop[handle.rcvNxt]; } ELSE { tcpHdrPtr.ack _ FALSE; tcpHdrPtr.ackNumber _ Flop[0]; }; handle.sndNxt _ handle.sndNxt + sendDatagram.dataLength; IF tcpHdrPtr.syn THEN -- adjust seq number for SYN and FIN handle.sndNxt _ handle.sndNxt + 1; IF tcpHdrPtr.fin THEN { handle.sndNxt _ handle.sndNxt + 1; handle.finSequence _ Flip[tcpHdrPtr.seqNumber]; }; IF handle.sndUrgent THEN { tcpHdrPtr.urg _ TRUE; tcpHdrPtr.urgentPtr _ handle.sndUp - handle.sndNxt; } ELSE { tcpHdrPtr.urg _ FALSE; tcpHdrPtr.urgentPtr _ 0; }; TCPSend[handle, sendDatagram]; TCPLogging.PrintTCPPacket[handle, sendDatagram, toNet]; IF dataSegment THEN { -- if didn't send ack alone tcpSendBufferPtr _ NEW[TCPSendBuffer]; tcpSendBufferPtr.dataByteCount _ sendDatagram.dataLength; tcpSendBufferPtr.datagram _ sendDatagram; tcpSendBufferPtr.rexmitTime _ SetTimeout[rexmitInterval]; tcpSendBufferPtr.timeoutTime _ SetTimeout[rexmitTimeout]; IF tcpHdrPtr.syn THEN tcpSendBufferPtr.timeoutTime _ SetTimeout[initialTimeout]; handle.rexmitQueue _ List.Nconc1[handle.rexmitQueue, tcpSendBufferPtr]; }; }; -- SendSegmentToNet TryToSend: PUBLIC PROC [handle: TCPHandle] = { sendDatagram: IPDefs.Datagram; -- datagram to send sendData: BOOL; -- set if sending data, not ack only IF handle.state = listen OR handle.state = synSent THEN -- if in unsynced state RETURN WITH ERROR TCP.Error[handle.reason]; -- Trying to send data in unsynced state IF handle.sndWnd = 0 THEN-- in synced state, if send window is zero IF handle.rexmitQueue = NIL THEN sendData _ TRUE -- if no pkts sent, send pkt to probe window ELSE sendData _ FALSE -- if pkts sent and unacked, send ack ELSE IF handle.sndNxt >= handle.sndUna + handle.sndWnd THEN sendData _ FALSE -- if non-zero window is full, send ack ELSE sendData _ TRUE; -- else send data IF -- if can send data, but no data to send sendData AND handle.toNetQueue = NIL THEN sendData _ FALSE; -- just send an ack IF NOT sendData THEN TRUSTED { -- if sending only an ack tcpHdrPtr: TCPHeaderP; -- TCP header to send sendDatagram _ NEW[IPDefs.DatagramRec]; tcpHdrPtr _ LOOPHOLE[@sendDatagram.data]; tcpHdrPtr.psh _ FALSE; tcpHdrPtr.syn _ FALSE; tcpHdrPtr.fin _ FALSE; tcpHdrPtr.rst _ FALSE; IF handle.sndUrgent THEN { tcpHdrPtr.urg _ TRUE; tcpHdrPtr.urgentPtr _ handle.sndUp - handle.sndNxt; } ELSE { tcpHdrPtr.urg _ FALSE; tcpHdrPtr.urgentPtr _ 0; }; sendDatagram.dataLength _ 0; SendSegmentToNet[handle, sendDatagram, FALSE]; -- send to net RETURN; -- finished trying to send }; DO -- send data until send window is full or no more data to send IF handle.toNetQueue = NIL THEN RETURN; -- out of data sendDatagram _ NARROW[handle.toNetQueue.first]; handle.toNetQueue _ handle.toNetQueue.rest; SendSegmentToNet[handle, sendDatagram, TRUE]; -- send segment to net IF handle.sndNxt >= handle.sndUna + handle.sndWnd THEN EXIT ENDLOOP }; -- TryToSend TryToSendData: PUBLIC PROC [handle: TCPHandle] = { sendData: BOOL; IF handle.state = listen OR handle.state = synSent THEN -- if in unsynced state RETURN WITH ERROR TCP.Error[handle.reason]; -- Trying to send data in unsynced state IF handle.sndWnd = 0 THEN -- if send window is zero IF handle.rexmitQueue = NIL THEN sendData _ TRUE -- if no pkts sent, send pkt to probe window ELSE sendData _ FALSE -- if pkts sent and unacked, send ack ELSE IF handle.sndNxt >= handle.sndUna + handle.sndWnd THEN sendData _ FALSE -- if non-zero window is full, send ack ELSE sendData _ TRUE; -- else send data IF sendData AND handle.toNetQueue # NIL THEN -- if allowed to send data and data to send TryToSend[handle] -- then send it }; -- TryToSendData SendSYN: PUBLIC PROC [handle: TCPHandle] = TRUSTED { sendDatagram: IPDefs.Datagram _ NEW[IPDefs.DatagramRec]; tcpHdrPtr: TCPHeaderP _ LOOPHOLE[@sendDatagram.data]; tcpHdrPtr.urg _ FALSE; tcpHdrPtr.urgentPtr _ 0; tcpHdrPtr.psh _ FALSE; tcpHdrPtr.rst _ FALSE; tcpHdrPtr.syn _ FALSE; tcpHdrPtr.fin _ FALSE; tcpHdrPtr.ack _ FALSE; sendDatagram.dataLength _ 0; -- set data length tcpHdrPtr.syn _ TRUE; tcpHdrPtr.seqNumber _ Flop[handle.iss]; handle.sndNxt _ handle.iss; SendSegmentToNet[handle, sendDatagram, TRUE]; }; SendFIN: PUBLIC PROC [handle: TCPHandle] = TRUSTED { sendDatagram: IPDefs.Datagram _ NEW[IPDefs.DatagramRec]; tcpHdrPtr: TCPHeaderP _ LOOPHOLE[@sendDatagram.data]; tcpHdrPtr.urg _ FALSE; tcpHdrPtr.urgentPtr _ 0; tcpHdrPtr.psh _ FALSE; tcpHdrPtr.rst _ FALSE; tcpHdrPtr.syn _ FALSE; tcpHdrPtr.fin _ TRUE; tcpHdrPtr.ack _ FALSE; sendDatagram.dataLength _ 0; handle.toNetQueue _ List.Nconc1[handle.toNetQueue, sendDatagram]; TryToSend[handle] -- try to send data and fin on to net queue }; -- SendFIN SendReset: PUBLIC PROC [handle: TCPHandle, sourcePort, dstnPort: IPDefs.DByte, Dstn: IPDefs.Address, seq, ack: INT] = TRUSTED { sendDatagram: IPDefs.Datagram; -- segment to send tcpHdrPtr: TCPHeaderP; -- header to send sendDatagram _ NEW[IPDefs.DatagramRec]; sendDatagram.dataLength _ 0; sendDatagram.inHdr.destination _ Dstn; tcpHdrPtr _ LOOPHOLE[@sendDatagram.data]; tcpHdrPtr.sourcePort _ sourcePort; tcpHdrPtr.dstnPort _ dstnPort; tcpHdrPtr.seqNumber _ Flop[seq]; tcpHdrPtr.ackNumber _ Flop[ack]; tcpHdrPtr.urg _ FALSE; tcpHdrPtr.psh _ FALSE; tcpHdrPtr.syn _ FALSE; tcpHdrPtr.fin _ FALSE; tcpHdrPtr.rst _ TRUE; IF ack # 0 THEN tcpHdrPtr.ack _ TRUE ELSE tcpHdrPtr.ack _ FALSE; tcpHdrPtr.window _ 0; tcpHdrPtr.checksum _ 0; tcpHdrPtr.urgentPtr _ 0; TCPSend[handle, sendDatagram]; TCPLogging.PrintTCPPacket[handle, sendDatagram, toNet]; }; -- SendReset RemoveAckedSegments: PUBLIC PROC [handle: TCPHandle] ~ TRUSTED { IF handle.sndUna >= handle.sndUp THEN { handle.sndUrgent _ FALSE; handle.sndUp _ 0; }; WHILE handle.rexmitQueue # NIL DO tcpSendBufferPtr: REF TCPSendBuffer _ NARROW[handle.rexmitQueue.first]; tcpHdrPtr: TCPHeaderP _ LOOPHOLE[@tcpSendBufferPtr.datagram.data]; lastSeqNumber: INT _ Flip[tcpHdrPtr.seqNumber] + tcpSendBufferPtr.dataByteCount - 1; IF tcpHdrPtr.syn THEN lastSeqNumber _ lastSeqNumber + 1; IF tcpHdrPtr.fin THEN lastSeqNumber _ lastSeqNumber + 1; IF handle.sndUna > lastSeqNumber THEN { beNiceToGC: LIST OF REF ANY _ handle.rexmitQueue; handle.rexmitQueue _ handle.rexmitQueue.rest; beNiceToGC.first _ NIL; beNiceToGC.rest _ NIL; } ELSE RETURN; -- segment at head of queue not acked ENDLOOP; }; -- RemoveAckedSegments RepacketizeandRexmit: PUBLIC PROC [handle: TCPHandle, tcpSendBufferPtr: REF TCPSendBuffer] ~ TRUSTED { sendDatagram: IPDefs.Datagram; tcpHdrPtr: TCPHeaderP; i, j: INT; bytesToSend: INT; bytesRexmitted: INT; tcpSendBufferPtr.rexmitTime _ SetTimeout[rexmitInterval]; IF handle.state = synSent OR handle.state = synRcvd THEN { sendDatagram _ tcpSendBufferPtr.datagram; pktsRexmitted _ pktsRexmitted + 1; tcpHdrPtr _ LOOPHOLE[@sendDatagram.data]; IF NOT (handle.state = listen OR handle.state = synSent) THEN IF NOT tcpHdrPtr.ack OR Flip[tcpHdrPtr.ackNumber] # handle.rcvNxt THEN { tcpHdrPtr.ack _ TRUE; tcpHdrPtr.ackNumber _ Flop[handle.rcvNxt]; }; TCPSend[handle, sendDatagram]; TCPLogging.PrintTCPPacket[handle, sendDatagram, rexmitToNet]; RETURN }; IF handle.nBytesToSend = 0 AND NOT (SELECT handle.state FROM finWait1, closing, lastAck => TRUE ENDCASE => FALSE) THEN RETURN; -- if no data or fin to send, then exit bytesToSend _ handle.nBytesToSend; j _ handle.sendSlot; bytesRexmitted _ 0; DO -- allocate buffers to send and fill them sendDatagram _ NEW[IPDefs.DatagramRec]; i _ 0; WHILE i # bytesToSend AND i # maxTCPDataLength DO sendDatagram.data[i+tcpHdrByteLength] _ handle.sendBuffer[j]; j _ (j + 1) MOD sendBufferLength; i _ i + 1 ENDLOOP; bytesToSend _ bytesToSend - i; tcpHdrPtr _ LOOPHOLE[@sendDatagram.data]; tcpHdrPtr.urg _ FALSE; tcpHdrPtr.urgentPtr _ 0; tcpHdrPtr.psh _ FALSE; tcpHdrPtr.rst _ FALSE; tcpHdrPtr.syn _ FALSE; IF handle.sndUna + i = handle.finSequence THEN tcpHdrPtr.fin _ TRUE ELSE tcpHdrPtr.fin _ FALSE; tcpHdrPtr.checksum _ 0; sendDatagram^.dataLength _ i; -- set data length tcpHdrPtr.seqNumber _ Flop[handle.sndUna + bytesRexmitted]; bytesRexmitted _ bytesRexmitted + i; -- to calculate sequence numbers tcpHdrPtr.window _ handle.rcvWnd; -- send latest window tcpHdrPtr.ack _ TRUE; tcpHdrPtr.ackNumber _ Flop[handle.rcvNxt]; pktsRexmitted _ pktsRexmitted + 1; TCPSend[handle, sendDatagram]; TCPLogging.PrintTCPPacket[handle, sendDatagram, rexmitToNet]; IF bytesToSend = 0 THEN EXIT ENDLOOP -- end filling send buffers }; -- RepacketizeandRexmit Rexmit: PUBLIC PROC [handle: TCPHandle, tcpSendBufferPtr: REF TCPSendBuffer] ~ TRUSTED { sendDatagram: IPDefs.Datagram; tcpHdrPtr: TCPHeaderP; tcpSendBufferPtr.rexmitTime _ SetTimeout[rexmitInterval]; sendDatagram _ tcpSendBufferPtr.datagram; pktsRexmitted _ pktsRexmitted + 1; tcpHdrPtr _ LOOPHOLE[@sendDatagram.data]; IF NOT (handle.state = listen OR handle.state = synSent) THEN -- update ack field IF NOT tcpHdrPtr.ack OR Flip[tcpHdrPtr.ackNumber] # handle.rcvNxt THEN { tcpHdrPtr.ack _ TRUE; tcpHdrPtr.ackNumber _ Flop[handle.rcvNxt]; }; TCPSend[handle, sendDatagram]; TCPLogging.PrintTCPPacket[handle, sendDatagram, rexmitToNet]; }; END. Copyright (C) 1983, 1985 by Xerox Corporation. All rights reserved. The following program was created in 1983 but has not been published within the meaning of the copyright law, is furnished under license, and may not be used, copied and/or disclosed except in accordance with the terms of said license. TCPTransmitImpl.mesa Last Edited by: Nichols, August 25, 1983 4:37 pm Last Edited by: Taft, January 4, 1984 12:01 pm Last Edited by: HGM, April 18, 1985 11:07:00 pm PST Hal Murray June 3, 1985 10:05:37 pm PDT John Larson, April 14, 1986 11:30:03 pm PST Fill in the header and miscellaneous stuff in the datagram and send it. This uses IPDefs.SendSpecific since the destination depends on the handle. When packets are given to TCPSend, the dataLength should be the length of the TCP data, not the total data length. Procedure to send data, control or ack only segments to network. The dataSegment argument should be set if there is control or data in the segment which should be acked, i.e. if it is not an ack only segment. If the dataSegment argument is set, the packet is put on the rexmit queue for the connection. common code for sending data or ack only segments update next seq number to send then put entry on rexmit queue Called to send as much data as possible from the ToNet queue. Checks window to determine if data can be sent; if data can not be sent, sends an ack only segment. Calls SendSegmentToNet to actually send the data. allocate new buffer for ack and fill in header Called to determine if data can be sent; if so, it calls TryToSend. This routine is called when you want to send a segment if and only if data exists; thus it is called when an empty ack packet has been received and more data may be sent or when the user supplies more data to send via the TCPSend routine. TryToSend can't be called directly because it always generates an ack and we don't need acks for the acks. This procedure allocates a segment to send a SYN. It calls SendSegmentToNet to send the SYN segment. This procedure allocates a segment to send a FIN, queues it on the end of the ToNet queue and then calls TryToSend to send any data on the ToNet queue and the FIN. This procedure is called to send a reset; it sends directly to the network by calling the TCPSend routine; it does not use the TryToSend or SendSegmentToNet routines because resets can be sent when no TCB exists. This procedure is called to repacketize and retransmit data on the retransmit queue. It is called from CheckRexmitQueues when repacketizing is enabled. if syn unacked, just rexmit users's packet fill send buffer until all bytes are sent or send buffer is full send the segment to the internet This procedure is called to retransmit a segment on the retransmit queue when Repacketizing is not enabled. It is called from CheckRexmitQueues. Κ γ– "cedar" style˜Icode2šœ―™―head™J™0J™.J™3J™'Icode™+J™šΟk ˜ KšœœD˜PKšœœ ˜Kšœœ ˜Kšœ œ˜"KšœœΟ˜ΫK˜ ——šœœ˜Kšœ&˜-Kšœ˜Kš˜Kšœ˜šΟnœœ/˜KšœŸ˜"K˜—šœŸ?˜BKšœœ œŸ˜6Kšœœ˜/K˜+Kšœ'œŸ˜EKšœ0œ˜;Kš˜—KšœŸ ˜—šž œœœ˜2Kšœž™žKšœ œ˜K˜šœœŸ˜PKšœœœŸ(˜TK˜—šœŸ˜4Kš œœœ œŸ-˜^Kšœ œŸ&˜<—šœ˜Kšœ0œ œŸ(˜pKšœ œŸ˜(—š œ œœœŸ+˜XKšœŸ˜"—KšœŸ˜—šžœœœœ˜4Kšœe™eKšœ œŸ˜9Kšœœ˜5Kšœœ˜Kšœ˜Kšœœ˜Kšœœ˜Kšœœ˜Kšœœ˜Kšœœ˜KšœŸ˜0Kšœœ˜Kšœ(˜(Kšœ˜Kšœ'œŸ˜1—šžœœœœ˜4Kšœ€™€Kšœ œŸ˜9Kšœœ˜5Kšœœ˜Kšœ˜Kšœœ˜Kšœœ˜Kšœœ˜Kšœœ˜Kšœœ˜Kšœ˜K˜AKšœŸ+˜=KšœŸ ˜—š ž œœœYœœ˜KšœΥ™ΥKšœŸ˜2KšœŸ˜)K˜Kšœœ˜(K˜K˜&Kšœ œ˜)Kšœ"˜"Kšœ˜Kšœ!˜!Kšœ!˜!Kšœœ˜Kšœœ˜Kšœœ˜Kšœœ˜Kšœœ˜Kšœ œœ˜%Kšœœ˜Kšœ˜Kšœ˜Kšœ˜Kšœ˜K˜7KšœŸ ˜—šžœœœœ˜@šœœ˜'Kšœœ˜K˜—šœœ˜!Kšœœœ˜GKšœœ#˜CKšœœC˜UKšœœ$˜9Kšœœ$˜9šœ˜'Kš œ œœœœ˜1Kšœ-˜-Kšœœ˜Kšœœ˜—KšœœŸ%˜2KšŸ˜ —KšœŸ˜—š žœœœ'œœ˜fKšœ˜™˜Kšœ˜Kšœ˜Kšœœ˜ Kšœ œ˜Kšœœ˜K˜šœ9˜9K˜—šœœœ˜:K™*Kšœ*˜*Kšœ#˜#Kšœ œ˜*šœœœ˜=šœœœ+œ˜HKšœœ˜Kšœ-˜-——K˜Kšœ>˜>KšœŸ˜ —šœœ˜šœœœ˜Kšœ˜"Kšœœœ˜—KšœŸ(˜0—Kšœ#˜#Kšœ˜Kšœ˜šœŸ*˜-Kšœœ˜(Kšœ˜KšœA™Ašœœœ˜2Kšœ>˜>Kšœ œ˜"Kšœ ˜ Kšœ˜ —Kšœ˜Kšœ œ˜*Kšœœ˜Kšœ˜Kšœœ˜Kšœœ˜Kšœœ˜Kšœ(œœ˜DKšœœ˜Kšœ˜KšœŸ˜1Kšœ<˜˜>Kšœœ˜KšœŸ˜$—KšœŸ˜—š žœœœ'œœ˜XKšœ‘™‘Kšœ˜Kšœ˜Kšœ9˜9Kšœ*˜*Kšœ#˜#Kšœ œ˜*š œœœœŸ˜Ršœœœ+œ˜HKšœœ˜Kšœ-˜-——K˜Kšœ@Ÿ˜A—Kšœ˜——…—*pAq