OpenCores
URL https://opencores.org/ocsvn/tcp_socket/tcp_socket/trunk

Subversion Repositories tcp_socket

[/] [tcp_socket/] [trunk/] [source/] [server.h] - Diff between revs 2 and 4

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 2 Rev 4
Line 15... Line 15...
 
 
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// TCP-IP User Settings
// TCP-IP User Settings
//
//
 
 
unsigned local_mac_address_hi = 0x0001u;
const unsigned local_mac_address_hi = 0x0001u;
unsigned local_mac_address_med = 0x0203u;
const unsigned local_mac_address_med = 0x0203u;
unsigned local_mac_address_lo = 0x0405u;
const unsigned local_mac_address_lo = 0x0405u;
unsigned local_ip_address_hi = 0xc0A8u;//192/168
const unsigned local_ip_address_hi = 0xc0A8u;//192/168
unsigned local_ip_address_lo = 0x0101u;//1/1
const unsigned local_ip_address_lo = 0x0101u;//1/1
unsigned local_port = 80u;//http
const unsigned local_port = 80u;//http
 
 
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// TCP-IP GLOBALS
// TCP-IP GLOBALS
//
//
 
 
Line 299... Line 299...
        unsigned header_length;
        unsigned header_length;
        unsigned payload_start;
        unsigned payload_start;
        unsigned payload_length;
        unsigned payload_length;
        unsigned i, from, to;
        unsigned i, from, to;
        unsigned payload_end;
        unsigned payload_end;
 
        unsigned number_of_bytes;
 
 
        number_of_bytes = get_ethernet_packet(packet);
        number_of_bytes = get_ethernet_packet(packet);
 
 
        if(number_of_bytes == 0) return 0;
        if(number_of_bytes == 0) return 0;
        if(packet[6] != 0x0800) return 0;
        if(packet[6] != 0x0800) return 0;
Line 381... Line 382...
void put_tcp_packet(unsigned tx_packet [], unsigned tx_length){
void put_tcp_packet(unsigned tx_packet [], unsigned tx_length){
 
 
        unsigned payload_start = 17;
        unsigned payload_start = 17;
        unsigned packet_length;
        unsigned packet_length;
        unsigned index;
        unsigned index;
 
        unsigned i;
 
 
        //encode TCP header
        //encode TCP header
        tx_packet[payload_start + 0] = tx_source;
        tx_packet[payload_start + 0] = tx_source;
        tx_packet[payload_start + 1] = tx_dest;
        tx_packet[payload_start + 1] = tx_dest;
        tx_packet[payload_start + 2] = tx_seq[1];
        tx_packet[payload_start + 2] = tx_seq[1];
Line 509... Line 511...
        unsigned resend_wait;
        unsigned resend_wait;
        unsigned bytes;
        unsigned bytes;
        unsigned last_state;
        unsigned last_state;
        unsigned new_rx_data;
        unsigned new_rx_data;
 
 
        unsigned listen           = 0;
        const unsigned listen           = 0;
        unsigned open             = 1;
        const unsigned open             = 1;
        unsigned send             = 2;
        const unsigned send             = 2;
        unsigned wait_acknowledge = 3;
        const unsigned wait_acknowledge = 3;
        unsigned close            = 4;
        const unsigned close            = 4;
        unsigned state = listen;
        unsigned state = listen;
 
 
        tx_seq[0] = 0;
        tx_seq[0] = 0;
        tx_seq[1] = 0;
        tx_seq[1] = 0;
 
 
Line 535... Line 537...
                        put_tcp_packet(tx_packet, 0);//send reset packet
                        put_tcp_packet(tx_packet, 0);//send reset packet
                }
                }
 
 
                // (optionaly) send something
                // (optionaly) send something
                switch(state){
                switch(state){
                    case 0:
                    case listen:
                        tx_rst_flag = 0;
                        tx_rst_flag = 0;
                        tx_syn_flag = 0;
                        tx_syn_flag = 0;
                        tx_fin_flag = 0;
                        tx_fin_flag = 0;
                        tx_ack_flag = 0;
                        tx_ack_flag = 0;
                        break;
                        break;
                    case 1:
                    case open:
                        // set remote ip/port
                        // set remote ip/port
                        remote_ip_hi = rx_packet[13];
                        remote_ip_hi = rx_packet[13];
                        remote_ip_lo = rx_packet[14];
                        remote_ip_lo = rx_packet[14];
                        tx_dest = rx_source;
                        tx_dest = rx_source;
                        tx_source = local_port;
                        tx_source = local_port;
Line 553... Line 555...
                        calc_ack(tx_ack, rx_seq, 1);
                        calc_ack(tx_ack, rx_seq, 1);
                        tx_syn_flag = 1;
                        tx_syn_flag = 1;
                        tx_ack_flag = 1;
                        tx_ack_flag = 1;
                        put_tcp_packet(tx_packet, 0);
                        put_tcp_packet(tx_packet, 0);
                        break;
                        break;
                    case 2:
                    case send:
                        // application -> tcp
                        // application -> tcp
                        tx_length = application_get_data(tx_packet, tx_start);
                        tx_length = application_get_data(tx_packet, tx_start);
                        tx_seq[0] = next_tx_seq[0];
                        tx_seq[0] = next_tx_seq[0];
                        tx_seq[1] = next_tx_seq[1];
                        tx_seq[1] = next_tx_seq[1];
                        calc_ack(next_tx_seq, tx_seq, tx_length);
                        calc_ack(next_tx_seq, tx_seq, tx_length);
                        tx_syn_flag = 0;
                        tx_syn_flag = 0;
                        tx_ack_flag = 1;
                        tx_ack_flag = 1;
                        put_tcp_packet(tx_packet, tx_length);
                        put_tcp_packet(tx_packet, tx_length);
                        break;
                        break;
                    case 3:
                    case wait_acknowledge:
                        // resend until acknowledge recieved
                        // resend until acknowledge recieved
                        put_tcp_packet(tx_packet, tx_length);
                        put_tcp_packet(tx_packet, tx_length);
                        break;
                        break;
                    case 4:
                    case close:
                        // send fin ack
                        // send fin ack
                        tx_fin_flag = 1;
                        tx_fin_flag = 1;
                        tx_ack_flag = 1;
                        tx_ack_flag = 1;
                        calc_ack(tx_ack, rx_seq, 1);
                        calc_ack(tx_ack, rx_seq, 1);
                        put_tcp_packet(tx_packet, 0);
                        put_tcp_packet(tx_packet, 0);
Line 587... Line 589...
                                new_rx_data = 0;
                                new_rx_data = 0;
                                last_state = state;
                                last_state = state;
                                switch(state){
                                switch(state){
 
 
                                    // If a syn packet is recieved, wait for an ack
                                    // If a syn packet is recieved, wait for an ack
                                    case 0:
                                    case listen:
                                        if(rx_syn_flag) state = open;
                                        if(rx_syn_flag) state = open;
                                        else{
                                        else{
                                                tx_rst_flag = 1;
                                                tx_rst_flag = 1;
                                                put_tcp_packet(tx_packet, 0);//send reset packet
                                                put_tcp_packet(tx_packet, 0);//send reset packet
                                        }
                                        }
                                        break;
                                        break;
 
 
                                    // If an ack is recieved the connection is established
                                    // If an ack is recieved the connection is established
                                    case 1:
                                    case open:
                                        if(rx_ack_flag){
                                        if(rx_ack_flag){
                                                tx_seq[1] = rx_ack[1];
                                                tx_seq[1] = rx_ack[1];
                                                tx_seq[0] = rx_ack[0];
                                                tx_seq[0] = rx_ack[0];
                                                next_tx_seq[1] = rx_ack[1];
                                                next_tx_seq[1] = rx_ack[1];
                                                next_tx_seq[0] = rx_ack[0];
                                                next_tx_seq[0] = rx_ack[0];
                                                state = send;
                                                state = send;
                                        }
                                        }
                                        break;
                                        break;
 
 
                                    // Send some data
                                    // Send some data
                                    case 2:
                                    case send:
                                        new_rx_data = calc_ack(tx_ack, rx_seq, rx_length);
                                        new_rx_data = calc_ack(tx_ack, rx_seq, rx_length);
                                        if(rx_fin_flag){
                                        if(rx_fin_flag){
                                                state = close;
                                                state = close;
                                        } else if( tx_length ){
                                        } else if( tx_length ){
                                                state = wait_acknowledge;
                                                state = wait_acknowledge;
                                        }
                                        }
                                        break;
                                        break;
 
 
                                    // Wait until data is acknowledged before sending some more.
                                    // Wait until data is acknowledged before sending some more.
                                    case 3:
                                    case wait_acknowledge:
 
 
                                        new_rx_data = calc_ack(tx_ack, rx_seq, rx_length);
                                        new_rx_data = calc_ack(tx_ack, rx_seq, rx_length);
                                        if(rx_fin_flag){
                                        if(rx_fin_flag){
                                                state = close;
                                                state = close;
                                        } else if(  rx_ack_flag &&
                                        } else if(  rx_ack_flag &&
Line 631... Line 633...
                                        }
                                        }
 
 
                                        break;
                                        break;
 
 
                                    // wait for fin/ack.
                                    // wait for fin/ack.
                                    case 4:
                                    case close:
                                        if(rx_ack_flag) state = listen;
                                        if(rx_ack_flag) state = listen;
                                        break;
                                        break;
                                }
                                }
 
 
                                if(rx_rst_flag) state = listen;
                                if(rx_rst_flag) state = listen;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.