mirror of
https://github.com/Zeal-Operating-System/ZealOS.git
synced 2025-01-01 02:06:32 +00:00
397 lines
35 KiB
HTML
Executable file
397 lines
35 KiB
HTML
Executable file
<!DOCTYPE HTML>
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html;charset=US-ASCII">
|
|
<meta name="generator" content="ZealOS V1.07">
|
|
<style type="text/css">
|
|
body {background-color:#1f1f1f;}
|
|
.cF0{color:#e3e3e3;background-color:#1f1f1f;}
|
|
.cF1{color:#4f84a6;background-color:#1f1f1f;}
|
|
.cF2{color:#73a255;background-color:#1f1f1f;}
|
|
.cF3{color:#297582;background-color:#1f1f1f;}
|
|
.cF4{color:#b34f4b;background-color:#1f1f1f;}
|
|
.cF5{color:#8a52c3;background-color:#1f1f1f;}
|
|
.cF6{color:#b7822f;background-color:#1f1f1f;}
|
|
.cF7{color:#444444;background-color:#1f1f1f;}
|
|
.cF8{color:#6d6d6d;background-color:#1f1f1f;}
|
|
.cF9{color:#94bfde;background-color:#1f1f1f;}
|
|
.cFA{color:#a1ce97;background-color:#1f1f1f;}
|
|
.cFB{color:#6db4be;background-color:#1f1f1f;}
|
|
.cFC{color:#e88e88;background-color:#1f1f1f;}
|
|
.cFD{color:#ca94e8;background-color:#1f1f1f;}
|
|
.cFE{color:#d4b475;background-color:#1f1f1f;}
|
|
.cFF{color:#1f1f1f;background-color:#1f1f1f;}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<pre style="font-family:monospace;font-size:12pt">
|
|
<a name="l1"></a><span class=cF1>Bool</span><span class=cF0> TCPHandleValidSEQ(CTCPSocket *tcp_socket, CTCPHeader *header, </span><span class=cF9>U32</span><span class=cF0> segment_seq_num, </span><span class=cF9>I64</span><span class=cF0> length, </span><span class=cF1>U8</span><span class=cF0> *data)
|
|
<a name="l2"></a>{</span><span class=cF2>// returns the value of must_ack, used later in TCPHandleSocket. Copies data to receive buffer.</span><span class=cF0>
|
|
<a name="l3"></a> </span><span class=cF1>Bool</span><span class=cF0> must_ack = </span><span class=cF3>FALSE</span><span class=cF0>;
|
|
<a name="l4"></a> </span><span class=cF9>I64</span><span class=cF0> write_position;
|
|
<a name="l5"></a> </span><span class=cF9>I64</span><span class=cF0> next_position;
|
|
<a name="l6"></a> </span><span class=cF9>I64</span><span class=cF0> i;
|
|
<a name="l7"></a>
|
|
<a name="l8"></a></span><span class=cF2>//</span><span class=cF0> </span><span class=cF2>tcp_socket->send_window = header->window_size;</span><span class=cF0>
|
|
<a name="l9"></a> tcp_socket->send_window = </span><span class=cF5>EndianU16</span><span class=cF0>(header->window_size);
|
|
<a name="l10"></a> </span><span class=cF2>// Shrine doesn't use EndianU16 (ntohs)? are these all being stored network byte order? ...</span><span class=cF0>
|
|
<a name="l11"></a>
|
|
<a name="l12"></a> </span><span class=cF1>switch</span><span class=cF0> (tcp_socket->state)
|
|
<a name="l13"></a> </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l14"></a> </span><span class=cF1>case</span><span class=cF0> TCP_STATE_ESTABLISHED:
|
|
<a name="l15"></a> </span><span class=cF1>case</span><span class=cF0> TCP_STATE_FIN_WAIT1:
|
|
<a name="l16"></a> </span><span class=cF1>case</span><span class=cF0> TCP_STATE_FIN_WAIT2: </span><span class=cF2>// FIN2 check is ommitted in Shrine, yet used in below logic. Adding.</span><span class=cF0>
|
|
<a name="l17"></a> </span><span class=cF1>case</span><span class=cF0> TCP_STATE_CLOSE_WAIT: </span><span class=cF2>// allowing data to be pushed to receive buffer during closing state</span><span class=cF0>
|
|
<a name="l18"></a>
|
|
<a name="l19"></a> NetDebug(</span><span class=cF6>"TCP HANDLE VALID SEQ: Updating data in receive buffer."</span><span class=cF0>);
|
|
<a name="l20"></a>
|
|
<a name="l21"></a> </span><span class=cF2>// TODO: review while loops, make sure we DO NOT HANG INTERRUPT HANDLER.</span><span class=cF0>
|
|
<a name="l22"></a> write_position = tcp_socket->write_position;
|
|
<a name="l23"></a>
|
|
<a name="l24"></a> </span><span class=cF1>while</span><span class=cF0> (length && segment_seq_num != tcp_socket->next_recv_seq_num)
|
|
<a name="l25"></a> {
|
|
<a name="l26"></a> segment_seq_num = (segment_seq_num + </span><span class=cFE>1</span><span class=cF0>) & </span><span class=cFE>0xFFFFFFFF</span><span class=cF0>;
|
|
<a name="l27"></a> data++;
|
|
<a name="l28"></a> length--;
|
|
<a name="l29"></a> }
|
|
<a name="l30"></a>
|
|
<a name="l31"></a> </span><span class=cF1>for</span><span class=cF0> (i = </span><span class=cFE>0</span><span class=cF0>; i < length; i++)
|
|
<a name="l32"></a> {
|
|
<a name="l33"></a></span><span class=cF2>//</span><span class=cF0> </span><span class=cF2>next_position = (write_position + 1) & (tcp_socket->receive_buffer_size - 1);</span><span class=cF0>
|
|
<a name="l34"></a> next_position = (write_position + </span><span class=cFE>1</span><span class=cF0>) % tcp_socket->receive_buffer_size;
|
|
<a name="l35"></a>
|
|
<a name="l36"></a> </span><span class=cF1>if</span><span class=cF0> (next_position == tcp_socket->read_position)
|
|
<a name="l37"></a> </span><span class=cF1>break</span><span class=cF0>; </span><span class=cF2>// ...?</span><span class=cF0>
|
|
<a name="l38"></a>
|
|
<a name="l39"></a> tcp_socket->receive_buffer[write_position] = data[i];
|
|
<a name="l40"></a> write_position = next_position;
|
|
<a name="l41"></a> }
|
|
<a name="l42"></a>
|
|
<a name="l43"></a> tcp_socket->write_position = write_position;
|
|
<a name="l44"></a> tcp_socket->next_recv_seq_num += i;
|
|
<a name="l45"></a>
|
|
<a name="l46"></a> </span><span class=cF1>if</span><span class=cF0> (i > </span><span class=cFE>0</span><span class=cF0>)
|
|
<a name="l47"></a> must_ack = </span><span class=cF3>TRUE</span><span class=cF0>;
|
|
<a name="l48"></a>
|
|
<a name="l49"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cF5>Bt</span><span class=cF7>(</span><span class=cF0>&header->flags, TCPf_FIN</span><span class=cF7>)</span><span class=cF0>)
|
|
<a name="l50"></a> {
|
|
<a name="l51"></a> must_ack = </span><span class=cF3>TRUE</span><span class=cF0>;
|
|
<a name="l52"></a> tcp_socket->next_recv_seq_num++;
|
|
<a name="l53"></a>
|
|
<a name="l54"></a> </span><span class=cF1>switch</span><span class=cF0> (tcp_socket->state)
|
|
<a name="l55"></a> </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l56"></a> </span><span class=cF1>case</span><span class=cF0> TCP_STATE_ESTABLISHED:
|
|
<a name="l57"></a> tcp_socket->state = TCP_STATE_CLOSE_WAIT;
|
|
<a name="l58"></a> </span><span class=cF1>break</span><span class=cF0>;
|
|
<a name="l59"></a> </span><span class=cF1>case</span><span class=cF0> TCP_STATE_FIN_WAIT1:
|
|
<a name="l60"></a> </span><span class=cF1>case</span><span class=cF0> TCP_STATE_FIN_WAIT2:
|
|
<a name="l61"></a> tcp_socket->state = TCP_STATE_TIME_WAIT;
|
|
<a name="l62"></a> </span><span class=cF1>break</span><span class=cF0>;
|
|
<a name="l63"></a> </span><span class=cF7>}</span><span class=cF0> </span><span class=cF2>// review RFC, whether more state checks needed here.</span><span class=cF0>
|
|
<a name="l64"></a> }
|
|
<a name="l65"></a>
|
|
<a name="l66"></a>
|
|
<a name="l67"></a> </span><span class=cF1>break</span><span class=cF0>;
|
|
<a name="l68"></a>
|
|
<a name="l69"></a> </span><span class=cF1>default</span><span class=cF0>:
|
|
<a name="l70"></a> </span><span class=cF1>break</span><span class=cF0>;
|
|
<a name="l71"></a> </span><span class=cF7>}</span><span class=cF0>
|
|
<a name="l72"></a>
|
|
<a name="l73"></a> </span><span class=cF1>return</span><span class=cF0> must_ack;
|
|
<a name="l74"></a>}
|
|
<a name="l75"></a>
|
|
<a name="l76"></a>
|
|
<a name="l77"></a></span><span class=cF1>Bool</span><span class=cF0> TCPHandleACK(CTCPSocket *tcp_socket, CIPV4Packet *packet, CTCPHeader *header,
|
|
<a name="l78"></a> </span><span class=cF9>U32</span><span class=cF0> segment_seq_num, </span><span class=cF9>U32</span><span class=cF0> segment_ack_num, </span><span class=cF9>U32</span><span class=cF0> segment_length)
|
|
<a name="l79"></a>{ </span><span class=cF2>// returns the value of must_ack, used later in TCPHandleSocket</span><span class=cF0>
|
|
<a name="l80"></a> </span><span class=cF9>I64</span><span class=cF0> ack_relative;
|
|
<a name="l81"></a> </span><span class=cF9>I64</span><span class=cF0> ack_next_relative;
|
|
<a name="l82"></a>
|
|
<a name="l83"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cF5>Bt</span><span class=cF7>(</span><span class=cF0>&header->flags, TCPf_ACK</span><span class=cF7>)</span><span class=cF0>)
|
|
<a name="l84"></a> </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l85"></a> ack_relative = (segment_ack_num - tcp_socket->first_unacked_seq) & </span><span class=cFE>0xFFFFFFFF</span><span class=cF0>;
|
|
<a name="l86"></a> ack_next_relative = (tcp_socket->next_send_seq_num - tcp_socket->first_unacked_seq) & </span><span class=cFE>0xFFFFFFFF</span><span class=cF0>;
|
|
<a name="l87"></a>
|
|
<a name="l88"></a> </span><span class=cF2>// Shrine has comments here about RFC poor wording,</span><span class=cF0>
|
|
<a name="l89"></a> </span><span class=cF2>// TODO: review RFC and implement more refined approach.</span><span class=cF0>
|
|
<a name="l90"></a> </span><span class=cF1>if</span><span class=cF0> (ack_relative <= ack_next_relative)
|
|
<a name="l91"></a> {
|
|
<a name="l92"></a> TCPAcknowledgePacket(tcp_socket, segment_ack_num);
|
|
<a name="l93"></a>
|
|
<a name="l94"></a> </span><span class=cF2>// "Accept ACK"</span><span class=cF0>
|
|
<a name="l95"></a> tcp_socket->first_unacked_seq = segment_ack_num;
|
|
<a name="l96"></a>
|
|
<a name="l97"></a> </span><span class=cF1>switch</span><span class=cF0> (tcp_socket->state)
|
|
<a name="l98"></a> </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l99"></a> </span><span class=cF1>case</span><span class=cF0> TCP_STATE_SYN_SENT:
|
|
<a name="l100"></a> NetDebug(</span><span class=cF6>"TCP HANDLE ACK: Acceptable ACK; state: SYN_SENT"</span><span class=cF0>);
|
|
<a name="l101"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF5>Bt</span><span class=cF7>(</span><span class=cF0>&header->flags, TCPf_SYN</span><span class=cF7>)</span><span class=cF0>)
|
|
<a name="l102"></a> </span><span class=cF1>break</span><span class=cF0>;
|
|
<a name="l103"></a> </span><span class=cF2>// else, fall-through</span><span class=cF0>
|
|
<a name="l104"></a>
|
|
<a name="l105"></a> </span><span class=cF1>case</span><span class=cF0> TCP_STATE_SYN_RECEIVED:
|
|
<a name="l106"></a> tcp_socket->state = TCP_STATE_ESTABLISHED;
|
|
<a name="l107"></a> tcp_socket->srtt = </span><span class=cF5>tS</span><span class=cF0> - tcp_socket->connection_time;
|
|
<a name="l108"></a> </span><span class=cF1>break</span><span class=cF0>;
|
|
<a name="l109"></a>
|
|
<a name="l110"></a> </span><span class=cF1>default</span><span class=cF0>:
|
|
<a name="l111"></a> </span><span class=cF1>break</span><span class=cF0>;
|
|
<a name="l112"></a> </span><span class=cF7>}</span><span class=cF0>
|
|
<a name="l113"></a> NetDebug(</span><span class=cF6>"TCP HANDLE ACK: Acceptable ACK returning; state: 0x%0X"</span><span class=cF0>, tcp_socket->state);
|
|
<a name="l114"></a> }
|
|
<a name="l115"></a> </span><span class=cF1>else</span><span class=cF0>
|
|
<a name="l116"></a> {
|
|
<a name="l117"></a> NetWarn(</span><span class=cF6>"TCP HANDLE SOCKET: Invalid ACK."</span><span class=cF0>);
|
|
<a name="l118"></a>
|
|
<a name="l119"></a> </span><span class=cF1>switch</span><span class=cF0> (tcp_socket->state)
|
|
<a name="l120"></a> </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l121"></a> </span><span class=cF1>case</span><span class=cF0> TCP_STATE_LISTEN:
|
|
<a name="l122"></a> </span><span class=cF1>case</span><span class=cF0> TCP_STATE_SYN_SENT:
|
|
<a name="l123"></a> </span><span class=cF1>case</span><span class=cF0> TCP_STATE_SYN_RECEIVED:
|
|
<a name="l124"></a> TCPSend(packet->destination_ip_address,
|
|
<a name="l125"></a> </span><span class=cF5>EndianU16</span><span class=cF7>(</span><span class=cF0>header->destination_port</span><span class=cF7>)</span><span class=cF0>,
|
|
<a name="l126"></a> packet->source_ip_address,
|
|
<a name="l127"></a> </span><span class=cF5>EndianU16</span><span class=cF7>(</span><span class=cF0>header->source_port</span><span class=cF7>)</span><span class=cF0>,
|
|
<a name="l128"></a> segment_ack_num,
|
|
<a name="l129"></a> segment_seq_num + segment_length,
|
|
<a name="l130"></a> TCPF_ACK | TCPF_RST);
|
|
<a name="l131"></a> </span><span class=cF1>break</span><span class=cF0>;
|
|
<a name="l132"></a>
|
|
<a name="l133"></a> </span><span class=cF1>default</span><span class=cF0>:
|
|
<a name="l134"></a> </span><span class=cF1>if</span><span class=cF0> (IsTCPStateSync</span><span class=cF7>(</span><span class=cF0>tcp_socket</span><span class=cF7>)</span><span class=cF0>)
|
|
<a name="l135"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>TRUE</span><span class=cF0>; </span><span class=cF2>// must ACK the packet.</span><span class=cF0>
|
|
<a name="l136"></a>
|
|
<a name="l137"></a> </span><span class=cF1>break</span><span class=cF0>;
|
|
<a name="l138"></a> </span><span class=cF7>}</span><span class=cF0>
|
|
<a name="l139"></a> }
|
|
<a name="l140"></a> </span><span class=cF7>}</span><span class=cF0>
|
|
<a name="l141"></a>
|
|
<a name="l142"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>FALSE</span><span class=cF0>; </span><span class=cF2>// do not need to ACK the packet.</span><span class=cF0>
|
|
<a name="l143"></a>}
|
|
<a name="l144"></a>
|
|
<a name="l145"></a></span><span class=cF1>Bool</span><span class=cF0> TCPHandleReset(CTCPSocket *tcp_socket, CTCPHeader *header, </span><span class=cF1>Bool</span><span class=cF0> is_seq_valid)
|
|
<a name="l146"></a>{ </span><span class=cF2>// returns whether or not to stop overall TCP procedure.</span><span class=cF0>
|
|
<a name="l147"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cF5>Bt</span><span class=cF7>(</span><span class=cF0>&header->flags, TCPf_RST</span><span class=cF7>)</span><span class=cF0>)
|
|
<a name="l148"></a> </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l149"></a> </span><span class=cF1>switch</span><span class=cF0> (tcp_socket->state)
|
|
<a name="l150"></a> {
|
|
<a name="l151"></a> </span><span class=cF1>case</span><span class=cF0> TCP_STATE_SYN_SENT:
|
|
<a name="l152"></a> </span><span class=cF1>if</span><span class=cF0> (tcp_socket->first_unacked_seq == tcp_socket->next_send_seq_num)
|
|
<a name="l153"></a> </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l154"></a> NetWarn(</span><span class=cF6>"TCP HANDLE SOCKET: Got RST, socket state SYN_SENT. Connection refused."</span><span class=cF0>);
|
|
<a name="l155"></a> tcp_socket->state = TCP_STATE_CLOSED;
|
|
<a name="l156"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>TRUE</span><span class=cF0>;
|
|
<a name="l157"></a> </span><span class=cF7>}</span><span class=cF0>
|
|
<a name="l158"></a> </span><span class=cF1>break</span><span class=cF0>;
|
|
<a name="l159"></a>
|
|
<a name="l160"></a> </span><span class=cF1>default</span><span class=cF0>:
|
|
<a name="l161"></a> </span><span class=cF1>if</span><span class=cF0> (is_seq_valid)
|
|
<a name="l162"></a> </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l163"></a> NetWarn(</span><span class=cF6>"TCP HANDLE SOCKET: Got RST, connection refused by remote host."</span><span class=cF0>);
|
|
<a name="l164"></a> tcp_socket->state = TCP_STATE_CLOSED;
|
|
<a name="l165"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>TRUE</span><span class=cF0>;
|
|
<a name="l166"></a> </span><span class=cF7>}</span><span class=cF0>
|
|
<a name="l167"></a> }
|
|
<a name="l168"></a> </span><span class=cF7>}</span><span class=cF0>
|
|
<a name="l169"></a>
|
|
<a name="l170"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>FALSE</span><span class=cF0>;
|
|
<a name="l171"></a>}
|
|
<a name="l172"></a>
|
|
<a name="l173"></a></span><span class=cF1>U0</span><span class=cF0> TCPHandleSocketListen(CTCPSocket *tcp_socket, CIPV4Packet *packet, CTCPHeader *header, </span><span class=cF9>U32</span><span class=cF0> segment_seq_num)
|
|
<a name="l174"></a>{ </span><span class=cF2>// if SYN and socket listening, queue up the connection in the socket's accept queue.</span><span class=cF0>
|
|
<a name="l175"></a> CTCPAcceptQueue *new_connection;
|
|
<a name="l176"></a>
|
|
<a name="l177"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cF5>Bt</span><span class=cF7>(</span><span class=cF0>&header->flags, TCPf_SYN</span><span class=cF7>)</span><span class=cF0> && </span><span class=cF5>QueueSize</span><span class=cF7>(</span><span class=cF0>tcp_socket->accept_queue</span><span class=cF7>)</span><span class=cF0> < tcp_socket->accept_queue_limit)
|
|
<a name="l178"></a> </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l179"></a> NetDebug(</span><span class=cF6>"TCP HANDLE SOCKET LISTEN: Adding new connection to Socket accept queue"</span><span class=cF0>);
|
|
<a name="l180"></a> new_connection = </span><span class=cF5>CAlloc</span><span class=cF0>(</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF0>CTCPAcceptQueue</span><span class=cF7>)</span><span class=cF0>);
|
|
<a name="l181"></a>
|
|
<a name="l182"></a> new_connection->segment_seq_num = segment_seq_num;
|
|
<a name="l183"></a> new_connection->ipv4_address = packet->source_ip_address;
|
|
<a name="l184"></a> new_connection->port = header->source_port;
|
|
<a name="l185"></a>
|
|
<a name="l186"></a> </span><span class=cF5>QueueInsertRev</span><span class=cF0>(new_connection, tcp_socket->accept_queue);
|
|
<a name="l187"></a> </span><span class=cF7>}</span><span class=cF0>
|
|
<a name="l188"></a> </span><span class=cF1>else</span><span class=cF0>
|
|
<a name="l189"></a> </span><span class=cF7>{</span><span class=cF0> </span><span class=cF2>// refuse</span><span class=cF0>
|
|
<a name="l190"></a> NetDebug(</span><span class=cF6>"TCP HANDLE SOCKET LISTEN: Header flags not SYN or Queue full, REFUSING CONNECTION"</span><span class=cF0>);
|
|
<a name="l191"></a> TCPSend(packet->destination_ip_address,
|
|
<a name="l192"></a> </span><span class=cF5>EndianU16</span><span class=cF7>(</span><span class=cF0>header->destination_port</span><span class=cF7>)</span><span class=cF0>,
|
|
<a name="l193"></a> packet->source_ip_address,
|
|
<a name="l194"></a> </span><span class=cF5>EndianU16</span><span class=cF7>(</span><span class=cF0>header->source_port</span><span class=cF7>)</span><span class=cF0>,
|
|
<a name="l195"></a> segment_seq_num + </span><span class=cFE>1</span><span class=cF0>,
|
|
<a name="l196"></a> segment_seq_num + </span><span class=cFE>1</span><span class=cF0>,
|
|
<a name="l197"></a> TCPF_ACK | TCPF_RST);
|
|
<a name="l198"></a> </span><span class=cF7>}</span><span class=cF0>
|
|
<a name="l199"></a>}
|
|
<a name="l200"></a>
|
|
<a name="l201"></a></span><span class=cF1>U0</span><span class=cF0> TCPHandleSocket(CTCPSocket *tcp_socket, CIPV4Packet *packet, CTCPHeader *header, </span><span class=cF1>U8</span><span class=cF0> *data, </span><span class=cF9>I64</span><span class=cF0> length)
|
|
<a name="l202"></a>{
|
|
<a name="l203"></a> </span><span class=cF9>U32</span><span class=cF0> segment_length = length;
|
|
<a name="l204"></a> </span><span class=cF9>U32</span><span class=cF0> segment_seq_num = </span><span class=cF5>EndianU32</span><span class=cF0>(header->seq_num);
|
|
<a name="l205"></a> </span><span class=cF9>U32</span><span class=cF0> segment_ack_num = </span><span class=cF5>EndianU32</span><span class=cF0>(header->ack_num);
|
|
<a name="l206"></a> </span><span class=cF1>Bool</span><span class=cF0> must_ack = </span><span class=cF3>FALSE</span><span class=cF0>;
|
|
<a name="l207"></a> </span><span class=cF1>Bool</span><span class=cF0> is_seq_valid = </span><span class=cF3>FALSE</span><span class=cF0>;
|
|
<a name="l208"></a> </span><span class=cF9>I64</span><span class=cF0> seq_relative;
|
|
<a name="l209"></a> </span><span class=cF9>I64</span><span class=cF0> seq_end_relative;
|
|
<a name="l210"></a>
|
|
<a name="l211"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cF5>Bt</span><span class=cF7>(</span><span class=cF0>&header->flags, TCPf_FIN</span><span class=cF7>)</span><span class=cF0>)
|
|
<a name="l212"></a> segment_length++;
|
|
<a name="l213"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cF5>Bt</span><span class=cF7>(</span><span class=cF0>&header->flags, TCPf_SYN</span><span class=cF7>)</span><span class=cF0>)
|
|
<a name="l214"></a> segment_length++;
|
|
<a name="l215"></a>
|
|
<a name="l216"></a> </span><span class=cF1>switch</span><span class=cF0> (tcp_socket->state)
|
|
<a name="l217"></a> </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l218"></a> </span><span class=cF1>case</span><span class=cF0> TCP_STATE_LISTEN:
|
|
<a name="l219"></a> NetDebug(</span><span class=cF6>"TCP HANDLE SOCKET: Running TCP HANDLE SOCKET LISTEN"</span><span class=cF0>);
|
|
<a name="l220"></a> TCPHandleSocketListen(tcp_socket, packet, header, segment_seq_num);
|
|
<a name="l221"></a> </span><span class=cF1>return</span><span class=cF0>;
|
|
<a name="l222"></a>
|
|
<a name="l223"></a> </span><span class=cF1>case</span><span class=cF0> TCP_STATE_CLOSED:
|
|
<a name="l224"></a> NetErr(</span><span class=cF6>"TCP HANDLE SOCKET: Received packet but TCP Socket is CLOSED."</span><span class=cF0>);
|
|
<a name="l225"></a> </span><span class=cF1>return</span><span class=cF0>;
|
|
<a name="l226"></a>
|
|
<a name="l227"></a> </span><span class=cF1>default</span><span class=cF0>:
|
|
<a name="l228"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cF5>Bt</span><span class=cF7>(</span><span class=cF0>&header->flags, TCPf_SYN</span><span class=cF7>)</span><span class=cF0>)
|
|
<a name="l229"></a> {
|
|
<a name="l230"></a> tcp_socket->next_recv_seq_num = ++segment_seq_num;
|
|
<a name="l231"></a>
|
|
<a name="l232"></a> must_ack = </span><span class=cF3>TRUE</span><span class=cF0>;
|
|
<a name="l233"></a> }
|
|
<a name="l234"></a>
|
|
<a name="l235"></a> </span><span class=cF1>if</span><span class=cF0> (segment_length == </span><span class=cFE>0</span><span class=cF0> && tcp_socket->receive_window == </span><span class=cFE>0</span><span class=cF0>)
|
|
<a name="l236"></a> {
|
|
<a name="l237"></a> NetDebug(</span><span class=cF6>"TCP HANDLE SOCKET: segment length == 0 && receive window == 0, SEQ valid if seq == next recv seq"</span><span class=cF0>);
|
|
<a name="l238"></a> is_seq_valid = (segment_seq_num == tcp_socket->next_recv_seq_num);
|
|
<a name="l239"></a> }
|
|
<a name="l240"></a> </span><span class=cF1>else</span><span class=cF0>
|
|
<a name="l241"></a> {
|
|
<a name="l242"></a> seq_relative = (segment_seq_num - tcp_socket->next_recv_seq_num) & </span><span class=cFE>0xFFFFFFFF</span><span class=cF0>;
|
|
<a name="l243"></a> seq_end_relative = (segment_seq_num + segment_length - </span><span class=cFE>1</span><span class=cF0> - tcp_socket->next_recv_seq_num) & </span><span class=cFE>0xFFFFFFFF</span><span class=cF0>;
|
|
<a name="l244"></a>
|
|
<a name="l245"></a> is_seq_valid = (seq_relative < tcp_socket->receive_window ||
|
|
<a name="l246"></a> seq_end_relative < tcp_socket->receive_window);
|
|
<a name="l247"></a> }
|
|
<a name="l248"></a>
|
|
<a name="l249"></a> </span><span class=cF1>if</span><span class=cF0> (!is_seq_valid)
|
|
<a name="l250"></a> NetWarn(</span><span class=cF6>"TCP HANDLE SOCKET: Invalid SEQ."</span><span class=cF0>);
|
|
<a name="l251"></a>
|
|
<a name="l252"></a> must_ack |= TCPHandleACK(tcp_socket, packet, header, segment_seq_num, segment_ack_num, segment_length);
|
|
<a name="l253"></a>
|
|
<a name="l254"></a>
|
|
<a name="l255"></a> </span><span class=cF1>if</span><span class=cF0> (TCPHandleReset</span><span class=cF7>(</span><span class=cF0>tcp_socket, header, is_seq_valid</span><span class=cF7>)</span><span class=cF0>)
|
|
<a name="l256"></a> </span><span class=cF1>return</span><span class=cF0>;
|
|
<a name="l257"></a>
|
|
<a name="l258"></a> </span><span class=cF1>if</span><span class=cF0> (is_seq_valid)
|
|
<a name="l259"></a> must_ack |= TCPHandleValidSEQ(tcp_socket, header, segment_seq_num, length, data);
|
|
<a name="l260"></a>
|
|
<a name="l261"></a> </span><span class=cF1>if</span><span class=cF0> (must_ack)
|
|
<a name="l262"></a> TCPSendFlags(tcp_socket, TCPF_ACK);
|
|
<a name="l263"></a> </span><span class=cF7>}</span><span class=cF0>
|
|
<a name="l264"></a>
|
|
<a name="l265"></a>}
|
|
<a name="l266"></a>
|
|
<a name="l267"></a></span><span class=cF9>I64</span><span class=cF0> TCPHandleRefuse(CIPV4Packet *packet, CTCPHeader *header, </span><span class=cF9>I64</span><span class=cF0> length)
|
|
<a name="l268"></a>{
|
|
<a name="l269"></a> </span><span class=cF9>I64</span><span class=cF0> de_index;
|
|
<a name="l270"></a> </span><span class=cF9>U32</span><span class=cF0> ack_num = </span><span class=cF5>EndianU32</span><span class=cF0>(header->ack_num);
|
|
<a name="l271"></a> </span><span class=cF9>U32</span><span class=cF0> seq_num = </span><span class=cF5>EndianU32</span><span class=cF0>(header->seq_num);
|
|
<a name="l272"></a>
|
|
<a name="l273"></a> </span><span class=cF1>no_warn</span><span class=cF0> length; </span><span class=cF2>// TODO: Will reset generation need length ?</span><span class=cF0>
|
|
<a name="l274"></a>
|
|
<a name="l275"></a> ack_num = ++seq_num;
|
|
<a name="l276"></a> seq_num = </span><span class=cFE>0</span><span class=cF0>;
|
|
<a name="l277"></a>
|
|
<a name="l278"></a> </span><span class=cF2>// review RFC Reset-Generation ...</span><span class=cF0>
|
|
<a name="l279"></a>
|
|
<a name="l280"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cF5>Bt</span><span class=cF7>(</span><span class=cF0>&header->flags, TCPf_RST</span><span class=cF7>)</span><span class=cF0> && </span><span class=cF5>Bt</span><span class=cF7>(</span><span class=cF0>&header->flags, TCPf_ACK</span><span class=cF7>)</span><span class=cF0>)
|
|
<a name="l281"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cFE>0</span><span class=cF0>; </span><span class=cF2>// bail: we probably sent an RST earlier, and this is the acknowledgement</span><span class=cF0>
|
|
<a name="l282"></a>
|
|
<a name="l283"></a> de_index = TCPSend(packet->destination_ip_address,
|
|
<a name="l284"></a> </span><span class=cF5>EndianU16</span><span class=cF7>(</span><span class=cF0>header->destination_port</span><span class=cF7>)</span><span class=cF0>,
|
|
<a name="l285"></a> packet->source_ip_address,
|
|
<a name="l286"></a> </span><span class=cF5>EndianU16</span><span class=cF7>(</span><span class=cF0>header->source_port</span><span class=cF7>)</span><span class=cF0>,
|
|
<a name="l287"></a> seq_num,
|
|
<a name="l288"></a> ack_num,
|
|
<a name="l289"></a> TCPF_RST | TCPF_ACK);
|
|
<a name="l290"></a> </span><span class=cF1>if</span><span class=cF0> (de_index < </span><span class=cFE>0</span><span class=cF0>)
|
|
<a name="l291"></a> </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l292"></a> NetErr(</span><span class=cF6>"TCP Handler Refuse: TCP Send failed."</span><span class=cF0>);
|
|
<a name="l293"></a> </span><span class=cF1>return</span><span class=cF0> de_index;
|
|
<a name="l294"></a> </span><span class=cF7>}</span><span class=cF0>
|
|
<a name="l295"></a>
|
|
<a name="l296"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cFE>0</span><span class=cF0>;
|
|
<a name="l297"></a>}
|
|
<a name="l298"></a>
|
|
<a name="l299"></a></span><span class=cF9>I64</span><span class=cF0> TCPHandler(CIPV4Packet *packet)
|
|
<a name="l300"></a>{
|
|
<a name="l301"></a> CTCPHeader *header;
|
|
<a name="l302"></a> </span><span class=cF9>U16</span><span class=cF0> destination_port;
|
|
<a name="l303"></a> </span><span class=cF1>U8</span><span class=cF0> *data;
|
|
<a name="l304"></a> </span><span class=cF9>I64</span><span class=cF0> length;
|
|
<a name="l305"></a> CTCPTreeNode *head = tcp_globals.bound_socket_tree;
|
|
<a name="l306"></a> CTCPTreeNode *node;
|
|
<a name="l307"></a> CTCPTreeQueue *queue;
|
|
<a name="l308"></a> CTCPSocket *tcp_socket;
|
|
<a name="l309"></a> </span><span class=cF9>I64</span><span class=cF0> error = TCPPacketParse(&header, &data, &length, packet);
|
|
<a name="l310"></a>
|
|
<a name="l311"></a> </span><span class=cF1>if</span><span class=cF0> (error < </span><span class=cFE>0</span><span class=cF0>)
|
|
<a name="l312"></a> </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l313"></a> NetErr(</span><span class=cF6>"TCP HANDLER: Packet Parse Error."</span><span class=cF0>);
|
|
<a name="l314"></a> </span><span class=cF1>return</span><span class=cF0> error;
|
|
<a name="l315"></a> </span><span class=cF7>}</span><span class=cF0>
|
|
<a name="l316"></a>
|
|
<a name="l317"></a> NetDebug(</span><span class=cF6>"TCP HANDLER: Caught packet with dest port of %0X (B.E.)"</span><span class=cF0>, header->destination_port);
|
|
<a name="l318"></a>
|
|
<a name="l319"></a> destination_port = </span><span class=cF5>EndianU16</span><span class=cF0>(header->destination_port); </span><span class=cF2>// B.E. -> L.E.</span><span class=cF0>
|
|
<a name="l320"></a> NetDebug(</span><span class=cF6>"TCP HANDLER: Caught packet with dest port of %0X (L.E.)"</span><span class=cF0>, destination_port);
|
|
<a name="l321"></a>
|
|
<a name="l322"></a> </span><span class=cF1>if</span><span class=cF0> (head)
|
|
<a name="l323"></a> </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l324"></a> node = TCPTreeNodeFind(destination_port, head);
|
|
<a name="l325"></a>
|
|
<a name="l326"></a> </span><span class=cF1>if</span><span class=cF0> (node)
|
|
<a name="l327"></a> {
|
|
<a name="l328"></a> NetDebug(</span><span class=cF6>"TCP HANDLER: Found node for port, looking for address %0X (L.E.)"</span><span class=cF0>, packet->source_ip_address);
|
|
<a name="l329"></a> queue = TCPTreeNodeQueueIPV4Find(packet->source_ip_address, node); </span><span class=cF2>// TODO: make sure bit order is correct here!!</span><span class=cF0>
|
|
<a name="l330"></a> </span><span class=cF1>if</span><span class=cF0> (queue)
|
|
<a name="l331"></a> </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l332"></a> tcp_socket = queue->socket;
|
|
<a name="l333"></a> NetLog(</span><span class=cF6>"TCP HANDLER: Port and Address are in bound tree."</span><span class=cF0>);
|
|
<a name="l334"></a> </span><span class=cF7>}</span><span class=cF0>
|
|
<a name="l335"></a> </span><span class=cF1>else</span><span class=cF0>
|
|
<a name="l336"></a> </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l337"></a> NetWarn(</span><span class=cF6>"TCP HANDLER: Found node for port, but address is not in node queue."</span><span class=cF0>);
|
|
<a name="l338"></a> NetWarn(</span><span class=cF6>" TCP source ip: 0x%0X."</span><span class=cF0>, packet->source_ip_address);
|
|
<a name="l339"></a>
|
|
<a name="l340"></a> NetWarn(</span><span class=cF6>"TCP HANDLER: Sending TCP RST ACK packet. Refusing connection."</span><span class=cF0>);
|
|
<a name="l341"></a> TCPHandleRefuse(packet, header, length);
|
|
<a name="l342"></a> </span><span class=cF1>return</span><span class=cF0> -</span><span class=cFE>1</span><span class=cF0>;
|
|
<a name="l343"></a> </span><span class=cF7>}</span><span class=cF0>
|
|
<a name="l344"></a> }
|
|
<a name="l345"></a> </span><span class=cF1>else</span><span class=cF0>
|
|
<a name="l346"></a> {
|
|
<a name="l347"></a> NetDebug(</span><span class=cF6>"TCP HANDLER: NODE SEARCH FAILURE: PORT %0X"</span><span class=cF0>, destination_port);
|
|
<a name="l348"></a> NetWarn(</span><span class=cF6>"TCP HANDLER: Node for Port is not in tree."</span><span class=cF0>);
|
|
<a name="l349"></a> NetWarn(</span><span class=cF6>"TCP HANDLER: Sending TCP RST ACK packet. Refusing connection."</span><span class=cF0>);
|
|
<a name="l350"></a> TCPHandleRefuse(packet, header, length);
|
|
<a name="l351"></a> </span><span class=cF1>return</span><span class=cF0> -</span><span class=cFE>1</span><span class=cF0>;
|
|
<a name="l352"></a> }
|
|
<a name="l353"></a>
|
|
<a name="l354"></a> </span><span class=cF7>}</span><span class=cF0>
|
|
<a name="l355"></a> </span><span class=cF1>else</span><span class=cF0>
|
|
<a name="l356"></a> </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l357"></a> NetWarn(</span><span class=cF6>"TCP HANDLER: Socket tree is currently empty."</span><span class=cF0>);
|
|
<a name="l358"></a> NetWarn(</span><span class=cF6>"TCP HANDLER: Sending TCP RST ACK packet. Refusing connection."</span><span class=cF0>);
|
|
<a name="l359"></a> TCPHandleRefuse(packet, header, length);
|
|
<a name="l360"></a> </span><span class=cF1>return</span><span class=cF0> -</span><span class=cFE>1</span><span class=cF0>;
|
|
<a name="l361"></a> </span><span class=cF7>}</span><span class=cF0>
|
|
<a name="l362"></a>
|
|
<a name="l363"></a> </span><span class=cF2>// at this point, tcp_socket is set, otherwise has already returned -1.</span><span class=cF0>
|
|
<a name="l364"></a>
|
|
<a name="l365"></a> NetDebug(</span><span class=cF6>"TCP HANDLER: Running TCP HANDLE SOCKET"</span><span class=cF0>);
|
|
<a name="l366"></a> TCPHandleSocket(tcp_socket, packet, header, data, length);
|
|
<a name="l367"></a>
|
|
<a name="l368"></a>}
|
|
</span></pre></body>
|
|
</html>
|