Main Page   Modules   Data Structures   File List   Data Fields   Globals   Related Pages   Examples  

/opentcp/tcp.c File Reference


Detailed Description

Author:
Version:
1.0
Date:
10.7.2002
Bug:
Warning:
Todo:
OpenTCP TCP implementation. All functions necessary for TCP processing are present here. Note that only a small subset of these functions must be used for "normal" applications that are using the TCP for communciation. For function declarations and lots of other usefull stuff see inet/tcp_ip.h.

For examples how to use TCP and write applications that communicate using TCP see inet/demo/main_demo.c, inet/demo/tcp_client_demo.c and inet/demo/tcp_server_demo.c.

Definition in file tcp.c.

Go to the source code of this file.

Functions

INT8 tcp_getsocket (UINT8 soctype, UINT8 tos, UINT16 tout, INT32(*listener)(INT8, UINT8, UINT32, UINT32))
 Allocate a free socket in TCP socket pool.

INT8 tcp_releasesocket (INT8 sochandle)
 Release a TCP socket.

INT8 tcp_listen (INT8 sochandle, UINT16 port)
 Put TCP socket to listen on a given port.

INT8 tcp_connect (INT8 sochandle, UINT32 ip, UINT16 rport, UINT16 myport)
 Initialize connection establishment towards remote IP&port.

INT16 tcp_send (INT8 sockethandle, UINT8 *buf, UINT16 blen, UINT16 dlen)
 Send user data over TCP using given TCP socket.

INT8 tcp_close (INT8 sochandle)
 Initiate TCP connection closing procedure.

INT8 tcp_getstate (INT8 sochandle)
 Get current state of the socket.

INT16 tcp_checksend (INT8 sochandle)
 Checks if it's possible to send data using given socket.

INT8 tcp_abort (INT8 sochandle)
 Reset connection and place socket to closed state.

void tcp_poll (void)
 Poll TCP sockets periodically.

INT8 tcp_init (void)
 Initialize TCP module.

INT16 process_tcp_in (struct ip_frame *frame, UINT16 len)
 Check and process the received TCP frame.

INT16 process_tcp_out (INT8 sockethandle, UINT8 *buf, UINT16 blen, UINT16 dlen)
 Create and send TCP packet.

void tcp_sendcontrol (INT8 sockethandle)
 Send a TCP control packet (no data).

void tcp_sendreset (struct tcp_frame *frame, UINT32 remip)
 Send a reset (RST) packet to remote host.

UINT32 tcp_initseq (void)
 Get and return initial sequence number.

INT8 tcp_mapsocket (struct ip_frame *ipframe, struct tcp_frame *tcpframe)
 Try to match received TCP packet to a socket.

void tcp_newstate (struct tcb *soc, UINT8 nstate)
 Change TCP socket state and reinitialize timers.

UINT16 tcp_getfreeport (void)
 Returns next free (not used) local port number.

UINT8 tcp_check_cs (struct ip_frame *ipframe, UINT16 len)
 Check if TCP checksum check's out.


Variables

tcp_frame received_tcp_packet
 Used for storing field information about the received TCP packet.

tcb tcp_socket [NO_OF_TCPSOCKETS+1]
 TCP table holding connection parameters for every TCP socket.

UINT8 tcp_tempbuf [MIN_TCP_HLEN+1]


Function Documentation

INT8 tcp_getsocket UINT8    soctype,
UINT8    tos,
UINT16    tout,
INT32(*    listener)(INT8, UINT8, UINT32, UINT32)
 

Author:
Date:
21.07.2002
Parameters:
soctype  type of socket wanted. Can take one of the following values:
tos  type of service for socket. For now only TCP_TOS_NORMAL.
tout  Timeout of socket in seconds. Defines after how many seconds of inactivity (application not sending and/or receiving any data over TCP connection) will the TCP socket automatically be closed.
listener  pointer to callback function that will be invoked by the TCP/IP stack to inform socket application of certain events. See tcpc_demo_eventlistener() and tcps_demo_eventlistener() for more information on events and possible actions.
Returns:
  • -1 - Error getting requested socket
  • >=0 - Handle to reserved socket
Invoke this function to try to obtain a free socket from TCP socket pool. Function returns a handle to the free socket that is later used for accessing the allocated socket (opening, connecting, sending data, closing, aborting, etc.).

Definition at line 155 of file tcp.c.

References tcb::event_listener, tcb::flags, tcb::locport, tcb::rem_ip, tcb::remport, tcb::state, TCP_STATE_FREE, TCP_STATE_RESERVED, TCP_TYPE_CLIENT, TCP_TYPE_CLIENT_SERVER, TCP_TYPE_NONE, TCP_TYPE_SERVER, TIMERTIC, tcb::tos, tcb::tout, and tcb::type.

Referenced by https_init(), pop3c_init(), and smtpc_init().

INT8 tcp_releasesocket INT8    sochandle
 

Author:
Date:
21.07.2002
Parameters:
sochandle  handle to socket to be released
Returns:
  • -1 - Error releasing the socket (Wrong socket handle or socket not in proper state to be released)
  • >=0 - handle of the released socket (can not be used any more untill allocated again with tcp_getsocket()).
Once the application does not need the TCP socket any more it can invoke this function in order to release it. This is usefull if there is a very limited number of sockets (in order to save some memory) shared among several applications.

Definition at line 228 of file tcp.c.

References tcb::event_listener, tcb::flags, tcb::locport, tcb::rem_ip, tcb::remport, tcb::state, TCP_STATE_CLOSED, TCP_STATE_FREE, TCP_STATE_RESERVED, TCP_TYPE_NONE, tcb::tos, and tcb::type.

INT8 tcp_listen INT8    sochandle,
UINT16    port
 

Author:
Date:
21.07.2002
Parameters:
sochandle  handle to socket to be placed to listen state
port  TCP port number on which it should listen
Returns:
  • -1 - Error
  • >=0 - OK (Socket put to listening state. Handle to socket returned)
This function will attempt to put socket to listening state. This is only possible if socket was defined as either TCP_TYPE_SERVER or TCP_TYPE_CLIENT_SERVER. If basic correctness checks pass, socket is put to listening mode and corresponding tcb entry is initialized.

Definition at line 290 of file tcp.c.

References tcb::event_listener, tcb::flags, tcb::locport, tcb::myflags, tcb::receive_next, tcb::rem_ip, tcb::remport, tcb::retries_left, tcb::send_mtu, tcb::send_next, tcb::send_unacked, tcb::state, TCP_STATE_CLOSED, TCP_STATE_LISTENING, TCP_STATE_RESERVED, TCP_STATE_TIMED_WAIT, TCP_TYPE_SERVER, and tcb::type.

Referenced by https_init().

INT8 tcp_connect INT8    sochandle,
UINT32    ip,
UINT16    rport,
UINT16    myport
 

Author:
Date:
21.07.2002
Parameters:
sochandle  handle to socket to be used for connection establishment
ip  remote IP address to connect to
rport  remote port number to connect to
myport  local port to use for connection. This value can be specified directly or, if a value of 0 is given, TCP module will determine local TCP port automatically.
Returns:
  • -1 - Error
  • >=0 - OK (Connection establishment procedure started. Socket handle returned.)
Invoke this function to start connection establishment procedure towards remote host over some socket. This is only possible if socket was defined as either TCP_TYPE_CLIENT or TCP_TYPE_CLIENT_SERVER. Function will make some basic checks and if everything is OK, corresponding tcb socket entry will be initialized and connection procedure started.

Definition at line 377 of file tcp.c.

References tcb::event_listener, tcb::flags, tcb::locport, tcb::myflags, tcb::rem_ip, tcb::remport, tcb::send_mtu, tcb::send_next, tcb::send_unacked, tcb::state, tcp_getfreeport(), tcp_initseq(), tcp_newstate(), tcp_sendcontrol(), TCP_STATE_CLOSED, TCP_STATE_LISTENING, TCP_STATE_RESERVED, TCP_STATE_SYN_SENT, TCP_TYPE_CLIENT, and tcb::type.

INT16 tcp_send INT8    sockethandle,
UINT8 *    buf,
UINT16    blen,
UINT16    dlen
 

Author:
Date:
25.07.2002
Parameters:
sockethandle  handle to TCP socket to be used for sending data
buf  pointer to data buffer (start of user data)
blen  buffer length in bytes (without space reserved at the beginning of buffer for headers)
dlen  length of user data to be sent (in bytes)
Returns:
  • -1 - Error
  • >0 - OK (number represents number of bytes actually sent)
Warning:
  • buf parameter is a pointer to data to be sent in user buffer. But note that there MUST be sufficient free buffer space before that data for TCP header (of MIN_TCP_HLEN size).
Invoke this function to initiate data sending over TCP connection established over a TCP socket. Since data is not buffered (in order to reduce RAM memory consumption) new data can not be sent until data that was previously sent is acknowledged. So, application knows when it can send new data either by:
  • waiting for TCP_EVENT_ACK in event_listener function
  • invoking tcp_check_send() function to check if it is possible to send data

Definition at line 481 of file tcp.c.

References tcb::myflags, process_tcp_out(), tcb::send_mtu, tcb::send_next, tcb::send_unacked, tcb::state, and TCP_STATE_CONNECTED.

INT8 tcp_close INT8    sochandle
 

Author:
Date:
21.07.2002
Parameters:
sochandle  handle to socket on which TCP connection is to be closed
Returns:
  • -2 - there is unacked data on this socket. Try again later.
  • -1 - Error
  • >=0 - OK (connection closing procedure started. Handle to socket returned)
Invoke this function to start connetion closing procedure over a given socket. Note that connection is not immediately closed. It may take some time for that to happen. Event_listener function will be invoked with appropriate event when that really happens.

Definition at line 549 of file tcp.c.

References tcb::flags, tcb::myflags, tcb::send_next, tcb::send_unacked, tcb::state, tcp_newstate(), tcp_sendcontrol(), TCP_STATE_CLOSED, TCP_STATE_CLOSING, TCP_STATE_CONNECTED, TCP_STATE_FINW1, TCP_STATE_FINW2, TCP_STATE_LAST_ACK, TCP_STATE_LISTENING, TCP_STATE_SYN_RECEIVED, TCP_STATE_SYN_SENT, and TCP_STATE_TIMED_WAIT.

INT8 tcp_getstate INT8    sochandle
 

Author:
Date:
21.07.2002
Parameters:
sochandle  handle to the socket to be queried
Returns:
  • -1 - Error
  • >0 - Socket state
Use this function for querying socket state. This is usually not needed directly, but could be usefull for some special purposes.

Definition at line 648 of file tcp.c.

References tcb::state.

INT16 tcp_checksend INT8    sochandle
 

Author:
Date:
23.07.2002
Parameters:
sochandle  handle to the socket to be inspected
Returns:
  • -1 - not possible to send over a socket (previously sent data is still not akcnowledged)
  • >0 - it is possible to send data over a socket
Invoke this function to get information whether it is possible to send data or not. This may, sometimes, be preffered way of getting this type of information to waiting for TCP_EVENT_ACK in event_listener function.

Definition at line 690 of file tcp.c.

References tcb::send_mtu, tcb::send_next, tcb::send_unacked, tcb::state, and TCP_STATE_CONNECTED.

INT8 tcp_abort INT8    sochandle
 

Author:
Date:
21.07.2002
Parameters:
sochandle  handle to socket to be aborted
Returns:
  • -1 - error
  • >=0 - OK (value represents handle to aborted socket)
Use this function in cases when TCP connection must be immediately closed. Note that the preffered (more elegant) way of closing the TCP connection is to invoke tcp_close() which starts a proper closing procedure. tcp_abort should be used only in cases when it is really necessary to immediately and quickly close the connection.

Definition at line 736 of file tcp.c.

References tcb::myflags, tcb::state, tcp_newstate(), tcp_sendcontrol(), TCP_STATE_CLOSED, TCP_STATE_CLOSING, TCP_STATE_CONNECTED, TCP_STATE_FINW1, TCP_STATE_FINW2, TCP_STATE_FREE, TCP_STATE_LAST_ACK, TCP_STATE_LISTENING, TCP_STATE_RESERVED, TCP_STATE_SYN_RECEIVED, TCP_STATE_SYN_SENT, and TCP_STATE_TIMED_WAIT.

void tcp_poll void   
 

Author:
Date:
19.07.2002
Warning:
  • This function must be invoked periodically from the main loop. See main_demo.c for an example.
This function checks all TCP sockets and performs various actions if timeouts occur. What kind of action is performed is defined by the state of the TCP socket.

Definition at line 808 of file tcp.c.

References check_timer(), tcb::event_listener, tcb::flags, init_timer(), tcb::myflags, tcb::persist_timerh, tcb::rem_ip, tcb::remport, tcb::retransmit_timerh, tcb::retries_left, tcb::send_mtu, tcb::send_next, tcb::send_unacked, tcb::state, TCP_DEF_RETRY_TOUT, TCP_EVENT_ABORT, TCP_EVENT_CLOSE, TCP_EVENT_REGENERATE, tcp_newstate(), tcp_sendcontrol(), TCP_STATE_CLOSED, TCP_STATE_CLOSING, TCP_STATE_CONNECTED, TCP_STATE_FINW1, TCP_STATE_FINW2, TCP_STATE_FREE, TCP_STATE_LAST_ACK, TCP_STATE_LISTENING, TCP_STATE_RESERVED, TCP_STATE_SYN_RECEIVED, TCP_STATE_SYN_SENT, TCP_STATE_TIMED_WAIT, TCP_SYN_RETRY_TOUT, TCP_TYPE_SERVER, TIMERTIC, and tcb::type.

INT8 tcp_init void   
 

Author:
Date:
21.07.2002
Returns:
  • -1 - error
  • >0 - number of sockets initialized
Warning:
  • This function must be invoked at startup before any other TCP functions are invoked.
This function initializes all sockets and corresponding tcbs to known state. Timers are also allocated for each socket and everything is brought to a predefined state.

Definition at line 1154 of file tcp.c.

References tcb::event_listener, tcb::flags, get_timer(), init_timer(), tcb::locport, tcb::myflags, tcb::persist_timerh, tcb::rem_ip, tcb::remport, tcb::retransmit_timerh, tcb::retries_left, tcb::send_mtu, tcb::state, TCP_STATE_FREE, TCP_TYPE_NONE, tcb::tos, tcb::tout, and tcb::type.

INT16 process_tcp_in struct ip_frame   frame,
UINT16    len
 

Author:
Date:
12.07.2002
Parameters:
frame  pointer to received ip_frame structure
len  length of data contained in IP datagram (in bytes)
Returns:
  • -1 - Error (packet not OK, or not TCP,or something else)
  • >0 - Packet OK
Invoke this function to process received TCP frames. See main_demo.c for an example on how to accomplish this.

Definition at line 1250 of file tcp.c.

References tcp_frame::ackno, tcp_frame::buf_index, ip_frame::buf_index, tcp_frame::checksum, tcp_frame::dport, tcb::event_listener, tcb::flags, tcp_frame::hlen_flags, IP_TCP, tcb::myflags, NETWORK_RECEIVE_INITIALIZE, tcp_frame::opt, ip_frame::protocol, RECEIVE_NETWORK_B, tcb::receive_next, tcb::rem_ip, tcb::remport, RESET_SYSTEM, tcb::send_next, tcb::send_unacked, tcp_frame::seqno, ip_frame::sip, tcp_frame::sport, tcb::state, tcp_check_cs(), TCP_EVENT_ABORT, TCP_EVENT_ACK, TCP_EVENT_CLOSE, TCP_EVENT_CONNECTED, TCP_EVENT_CONREQ, TCP_EVENT_DATA, tcp_initseq(), tcp_mapsocket(), tcp_newstate(), tcp_sendcontrol(), tcp_sendreset(), TCP_STATE_CLOSED, TCP_STATE_CLOSING, TCP_STATE_CONNECTED, TCP_STATE_FINW1, TCP_STATE_FINW2, TCP_STATE_FREE, TCP_STATE_LAST_ACK, TCP_STATE_LISTENING, TCP_STATE_SYN_RECEIVED, TCP_STATE_SYN_SENT, TCP_STATE_TIMED_WAIT, TCP_TYPE_SERVER, tcb::type, tcp_frame::urgent, and tcp_frame::window.

INT16 process_tcp_out INT8    sockethandle,
UINT8 *    buf,
UINT16    blen,
UINT16    dlen
 

Author:
Date:
16.07.2002
Parameters:
sockethandle  handle to processed socket
buf  pointer to data buffer (where TCP header will be stored)
blen  buffer length in bytes
dlen  length of data in bytes
Returns:
  • -1 - Error
  • >0 - Packet OK
Based on data supplied as function parameters and data stored in socket's tcb, TCP header is created in buffer, checksum is calculated and packet is forwarded to lower layers (IP).

Definition at line 2119 of file tcp.c.

References ip_checksum(), ip_checksum_buf(), IP_TCP, tcb::locport, tcb::myflags, process_ip_out(), tcb::receive_next, tcb::rem_ip, tcb::remport, tcb::send_mtu, tcb::send_unacked, and tcb::tos.

Referenced by tcp_send(), and tcp_sendcontrol().

void tcp_sendcontrol INT8    sockethandle
 

Author:
Date:
17.07.2002
Parameters:
sockethandle  handle to socket
This function is used to initiate sending of a control (no data) TCP packet. Important thing in these packets are the flags and sequence numbers they carry.

Definition at line 2253 of file tcp.c.

References process_tcp_out(), and tcp_tempbuf.

Referenced by process_tcp_in(), tcp_abort(), tcp_close(), tcp_connect(), tcp_poll(), and tcp_sendreset().

void tcp_sendreset struct tcp_frame   frame,
UINT32    remip
 

Author:
Date:
20.08.2002
Parameters:
frame  pointer to received TCP packet
remip  remote IP address of packet
Uses socket NO_OF_TCPSOCKETS to send a RESET packet to peer. This function is used when we are establishing connection but we receive something else than SYN or SYN+ACK when it's possible that the peer has still old connection on which needs to be resetted without canceling the connection establishment on process.

Definition at line 2294 of file tcp.c.

References tcp_frame::ackno, tcp_frame::dport, tcp_frame::hlen_flags, tcb::locport, tcb::myflags, tcb::receive_next, tcb::rem_ip, tcb::remport, tcb::send_mtu, tcb::send_unacked, tcp_frame::seqno, tcp_frame::sport, tcp_sendcontrol(), and tcb::tos.

Referenced by process_tcp_in().

UINT32 tcp_initseq void   
 

Author:
Date:
17.07.2002
Returns:
UINT32 number containing initial sequence number to be used
This function returns initial sequence number to be used in a TCP connection. For now, initial sequence number is selected based on base_timer value, which should be solid enough choice.

Definition at line 2346 of file tcp.c.

Referenced by process_tcp_in(), and tcp_connect().

INT8 tcp_mapsocket struct ip_frame   ipframe,
struct tcp_frame   tcpframe
 

Author:
Date:
12.07.2002
Parameters:
ipframe  pointer to received IP frame
tcpframe  pointer to received TCP frame to be mapped
Returns:
  • -1 - Error (no resources or no socket found)
  • >=0 - Handle to mapped socket
Function iterates through socket table trying to find a socket for whom this TCP packet is intended.

Definition at line 2369 of file tcp.c.

References tcp_frame::dport, tcp_frame::hlen_flags, tcb::locport, tcb::rem_ip, tcb::remport, ip_frame::sip, tcp_frame::sport, tcb::state, and TCP_STATE_LISTENING.

Referenced by process_tcp_in().

void tcp_newstate struct tcb   soc,
UINT8    nstate
 

Author:
Date:
18.07.2002
Parameters:
soc  pointer to socket structure we're working with
nstate  new socket state
This function is used for every state-change that occurs in the TCP sockets so as to provide correct timers/retransmittions that ensure TCP connection is lasting.

Definition at line 2455 of file tcp.c.

References init_timer(), tcb::persist_timerh, tcb::retransmit_timerh, tcb::retries_left, tcb::state, TCP_DEF_RETRIES, TCP_DEF_RETRY_TOUT, TCP_INIT_RETRY_TOUT, TCP_STATE_CLOSING, TCP_STATE_CONNECTED, TCP_STATE_FINW1, TCP_STATE_FINW2, TCP_STATE_LAST_ACK, TCP_STATE_SYN_SENT, TCP_STATE_TIMED_WAIT, TIMERTIC, and tcb::tout.

Referenced by process_tcp_in(), tcp_abort(), tcp_close(), tcp_connect(), and tcp_poll().

UINT16 tcp_getfreeport void   
 

Author:
Date:
21.07.2002
Returns:
  • 0 - no free ports!
  • >0 - free local TCP port number
Function attempts to find new local port number that can be used to establish a connection.

Definition at line 2511 of file tcp.c.

References tcb::locport, tcb::state, TCP_PORTS_END, and TCP_STATE_CLOSED.

Referenced by tcp_connect().

UINT8 tcp_check_cs struct ip_frame   ipframe,
UINT16    len
 

Author:
Date:
16.07.2002
Parameters:
ipframe  pointer to IP frame that carried TCP message
len  length of TCP portion
Returns:
  • 0 - checksum corrupted
  • 1 - checksum OK
Function recalculates TCP checksum (pseudoheader+header+data) and compares it to received checksum to see if everything is OK or there is a problem with the checksum.

Definition at line 2567 of file tcp.c.

References ip_frame::dip, ip_checksum(), ip_checksum_buf(), ip_frame::protocol, RECEIVE_NETWORK_B, RECEIVE_NETWORK_BUF, ip_frame::sip, and tcp_tempbuf.

Referenced by process_tcp_in().


Variable Documentation

struct tcp_frame received_tcp_packet
 

Various fields from the TCP packet are stored in this variable. These values are then used to perform the necessary actions as defined by the TCP specification: correctnes of the received TCP packet is checked by analyzing these fields, appropriate socket data is adjusted and/or control packet is sent based on it. See tcp_frame definition for struct information.

Definition at line 102 of file tcp.c.

struct tcb tcp_socket[NO_OF_TCPSOCKETS + 1]
 

TCP table is an array of tcp_socket structures holding all of the necessary information about the state, timers, timeouts and sequence and port numbers of the TCP sockets opened. Number of TCP sockets that can be opened at any given time is defined by the NO_OF_TCPSOCKETS and may be changed in order to change the amount of used RAM memory. See tcb definition for more information about the structure itself.

Note:
As seen in the code, an array size is actually bigger for one than the NO_OF_TCPSOCKETS defines. The last entry is used for sending control packets as answers to incoming TCP packets that do not map to any existing TCP sockets.

Definition at line 118 of file tcp.c.

UINT8 tcp_tempbuf[MIN_TCP_HLEN + 1]
 

Temporary buffer used for sending TCP control packets

Definition at line 120 of file tcp.c.

Referenced by tcp_check_cs(), and tcp_sendcontrol().


Generated on Sun Aug 3 20:33:00 2003 for OpenTCP by doxygen1.2.18