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

/opentcp/include/inet/tcp_ip.h File Reference


Detailed Description

Author:
Version:
1.0
Date:
1.2.2002
OpenTCP TCP and UDP protocol function declarations, constants, etc.

Definition in file tcp_ip.h.

Go to the source code of this file.

Data Structures

struct  udp_frame
 UDP header information. More...

struct  ucb
 UDP control block. More...

struct  tcp_frame
 TCP header information. More...

struct  tcb
 TCP transmission control block. More...


Defines

#define NO_OF_TCPSOCKETS   8
 Defines number of TCP sockets available.

#define NO_OF_UDPSOCKETS   4
 Defines number of UDP sockets available.

#define TCP_PORTS_END   1023
 Define reserved-ports space.

#define UDP_PORTS_END   1023
 Define reserved-ports space.

#define UDP_OPT_NONE   0
 Disable checksum calculation for UDP socket.

#define UDP_OPT_SEND_CS   1
 Enable checksum calculation for outgoing UDP packets.

#define UDP_OPT_CHECK_CS   2
 Enable checksum checking for received UDP packets.

#define UDP_HLEN   8
#define TCP_DEF_RETRIES   7
 Number of attempted TCP retransmissions before giving up.

#define TCP_DEF_KEEPALIVE   4
 Defines a number of seconds after which an empty ACK packet is sent.

#define TCP_DEF_RETRY_TOUT   4
 Default data-retransmission period (in seconds).

#define TCP_INIT_RETRY_TOUT   1
 Initial retransmission period (in seconds).

#define TCP_SYN_RETRY_TOUT   2
 Retranmission period for SYN packet.

#define TCP_TOS_NORMAL   0
 Defines normal type of service for TCP socket.

#define TCP_DEF_TOUT   120
 Default idle timeout.

#define UDP_STATE_FREE   1
 Defines that UDP socket is free and available.

#define UDP_STATE_CLOSED   2
 Defines that UDP socket is allocated but closed.

#define UDP_STATE_OPENED   3
 Defines that UDP socket is allocated and opened.

#define UDP_EVENT_DATA   64
 Only UDP event notified to UDP socket event listener.

#define TCP_TYPE_NONE   0x00
 TCP socket is nor a client nor a server.

#define TCP_TYPE_SERVER   0x01
 TCP socket represents a server application.

#define TCP_TYPE_CLIENT   0x02
 TCP socket represents a client application.

#define TCP_TYPE_CLIENT_SERVER   0x03
 TCP socket can act as client or as server.

#define TCP_STATE_FREE   1
#define TCP_STATE_RESERVED   2
#define TCP_STATE_CLOSED   3
#define TCP_STATE_LISTENING   4
#define TCP_STATE_SYN_RECEIVED   5
#define TCP_STATE_SYN_SENT   6
#define TCP_STATE_FINW1   7
#define TCP_STATE_FINW2   8
#define TCP_STATE_CLOSING   9
#define TCP_STATE_LAST_ACK   10
#define TCP_STATE_TIMED_WAIT   11
#define TCP_STATE_CONNECTED   12
#define TCP_EVENT_CONREQ   1
 Connection request event.

#define TCP_EVENT_CONNECTED   2
 Connection established event.

#define TCP_EVENT_CLOSE   4
 Connection closed event.

#define TCP_EVENT_ABORT   8
 Connection aborted event.

#define TCP_EVENT_ACK   16
 Data acknowledged event.

#define TCP_EVENT_REGENERATE   32
 Regenerate data event.

#define TCP_EVENT_DATA   64
 Data arrival event.

#define TCP_APP_OFFSET   MIN_TCP_HLEN
 Transmit buffer offset for TCP applications.

#define UDP_APP_OFFSET   UDP_HLEN
 Transmit buffer offset for UDP applications.


Functions

INT16 process_icmp_in (struct ip_frame *, UINT16)
 Process recieved ICMP datagram.

INT8 udp_init (void)
 Initialize UDP socket pool.

INT8 udp_getsocket (UINT8, INT32(*)(INT8, UINT8, UINT32, UINT16, UINT16, UINT16), UINT8)
 Allocate a free socket in UDP socket pool.

INT8 udp_releasesocket (INT8)
 Release a given socket.

INT8 udp_open (INT8, UINT16)
 Open a given UDP socket for communication.

INT8 udp_close (INT8)
 Close given socket for communication.

INT16 udp_send (INT8, UINT32, UINT16, UINT8 *, UINT16, UINT16)
 Send data to remote host using given UDP socket.

INT16 process_udp_in (struct ip_frame *, UINT16)
 Process received UDP frame.

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

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

INT16 process_tcp_out (INT8, UINT8 *, UINT16, UINT16)
 Create and send TCP packet.

INT8 tcp_init (void)
 Initialize TCP module.

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

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

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

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

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

void tcp_poll (void)
 Poll TCP sockets periodically.

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

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

INT8 tcp_releasesocket (INT8)
 Release a TCP socket.

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

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

INT8 tcp_close (INT8)
 Initiate TCP connection closing procedure.

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

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

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

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

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


Define Documentation

#define UDP_OPT_NONE   0
 

By choosing only this option for UDP socket, checksum calculation will be disabled for both incoming/outgoing UDP packets. This will make UDP extremely fast, but also more prone to errors. This is usually not a big limitation considering that checksum is not so good in error-detection anyway.

Definition at line 122 of file tcp_ip.h.

#define UDP_OPT_SEND_CS   1
 

When only this option is chosen, checksum is calculated for outgoing UDP packets. This is sometimes neccessary since certain applications can disable reception of UDP packets without a calculated checksum.

Definition at line 132 of file tcp_ip.h.

Referenced by bootpc_init(), dhcpc_init(), dns_init(), tftps_init(), udp_getsocket(), udp_init(), udp_releasesocket(), and udp_send().

#define UDP_OPT_CHECK_CS   2
 

When this option is enabled, checksum is checked for all received UDP packets to check for transmission errors.

Definition at line 140 of file tcp_ip.h.

Referenced by bootpc_init(), dhcpc_init(), dns_init(), process_udp_in(), tftps_init(), udp_getsocket(), udp_init(), and udp_releasesocket().

#define UDP_HLEN   8
 

UDP Header Length

Definition at line 144 of file tcp_ip.h.

Referenced by process_udp_in(), and udp_send().

#define TCP_TOS_NORMAL   0
 

This defines normal (and for now the only one implemented) type of service for the TCP socket.

Definition at line 214 of file tcp_ip.h.

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

#define UDP_STATE_FREE   1
 

If an entry of ucb type has this as a state value, then it is free and can be allocated by the udp_getsocket().

Definition at line 249 of file tcp_ip.h.

Referenced by udp_getsocket(), udp_init(), and udp_releasesocket().

#define UDP_STATE_CLOSED   2
 

Entries of ucb type that have this as their state value were allocated by udp_getsocket() but they are in the closed state, so no data can be received/sent through the socket.

Definition at line 258 of file tcp_ip.h.

Referenced by udp_close(), udp_getfreeport(), and udp_getsocket().

#define UDP_STATE_OPENED   3
 

Corresponding UDP socket was allocated and opened so data can be transmitted/received through it.

Definition at line 266 of file tcp_ip.h.

Referenced by process_udp_in(), udp_open(), and udp_send().

#define UDP_EVENT_DATA   64
 

For now, this is the only UDP event that is notified to the UDP sockets' event listener.

Definition at line 276 of file tcp_ip.h.

Referenced by dhcpc_eventlistener(), dns_eventlistener(), and process_udp_in().

#define TCP_TYPE_NONE   0x00
 

If TCP socket entry is of this type it can not be used for anything. This may only be used for testing, debugging, etc. purposes or if application is not sure what it wants to be it can reserve a TCP socket by getting a socket of type TCP_TYPE_NONE.

Definition at line 300 of file tcp_ip.h.

Referenced by tcp_getsocket(), tcp_init(), and tcp_releasesocket().

#define TCP_TYPE_SERVER   0x01
 

If TCP socket entry is of server type, application using it can only listen on a given socket for incoming connections. No connections can be opened towards some outside host unless the outside host initiates the connection.

Definition at line 310 of file tcp_ip.h.

Referenced by https_init(), process_tcp_in(), tcp_getsocket(), tcp_listen(), and tcp_poll().

#define TCP_TYPE_CLIENT   0x02
 

If TCP socket entry is of client type, application using it can establish connection through it towards other Internet hosts but can not accept any incoming connections on the port (execute tcp_listen() on it that is).

Definition at line 320 of file tcp_ip.h.

Referenced by pop3c_init(), smtpc_init(), tcp_connect(), and tcp_getsocket().

#define TCP_TYPE_CLIENT_SERVER   0x03
 

If TCP socket entry is of this type, application using it can both listen on a given socket or establish connection towards an outside host.

Definition at line 329 of file tcp_ip.h.

Referenced by tcp_getsocket().

#define TCP_STATE_FREE   1
 

Entry is free and unused

Definition at line 333 of file tcp_ip.h.

Referenced by process_tcp_in(), tcp_abort(), tcp_getsocket(), tcp_init(), tcp_poll(), and tcp_releasesocket().

#define TCP_STATE_RESERVED   2
 

Entry is reserved for use

Definition at line 334 of file tcp_ip.h.

Referenced by tcp_abort(), tcp_connect(), tcp_getsocket(), tcp_listen(), tcp_poll(), and tcp_releasesocket().

#define TCP_STATE_CLOSED   3
 

Entry allocated, socket still closed

Definition at line 335 of file tcp_ip.h.

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

#define TCP_STATE_LISTENING   4
 

Socket in listening state, waiting for incoming connections

Definition at line 336 of file tcp_ip.h.

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

#define TCP_STATE_SYN_RECEIVED   5
 

SYN packet received (either first SYN packet or response to SYN that we have previously sent)

Definition at line 339 of file tcp_ip.h.

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

#define TCP_STATE_SYN_SENT   6
 

SYN packet sent as an attempt to establish a connection

Definition at line 343 of file tcp_ip.h.

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

#define TCP_STATE_FINW1   7
 

User issued tcp_close request issued so FIN packet was sent

Definition at line 346 of file tcp_ip.h.

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

#define TCP_STATE_FINW2   8
 

Received ACK of our FIN, now waiting for other side to send FIN

Definition at line 349 of file tcp_ip.h.

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

#define TCP_STATE_CLOSING   9
 

Received FIN independently of our FIN

Definition at line 353 of file tcp_ip.h.

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

#define TCP_STATE_LAST_ACK   10
 

Waiting for last ACK packet as a response to our FIN

Definition at line 356 of file tcp_ip.h.

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

#define TCP_STATE_TIMED_WAIT   11
 

Waiting for 2MSL to prevent erroneous connection duplication

Definition at line 359 of file tcp_ip.h.

Referenced by process_tcp_in(), tcp_abort(), tcp_close(), tcp_listen(), tcp_newstate(), and tcp_poll().

#define TCP_STATE_CONNECTED   12
 

Connection established and data flowing freely to both sides :-)

Definition at line 362 of file tcp_ip.h.

Referenced by process_tcp_in(), tcp_abort(), tcp_checksend(), tcp_close(), tcp_newstate(), tcp_poll(), and tcp_send().

#define TCP_EVENT_CONREQ   1
 

Connection request event is notified to TCP server applications' event listener when SYN packet is received for it's socket. Event listener can then, if it wants to, inspect IP addres and port number of the remote host, or some other internal parameters, to decide whether it should allow connection establishment or not. One of the following values must then be returned from the event listener:

  • -1 - do not allow connection to be established. RST packet will be sent to remote host.
  • -2 - do not respond to this particular SYN packet (keep quiet). This may be used if device is somehow busy and not yet ready to establish a connection, but doesn't wan't to forcefully reject the connection with a RST packet.
  • 1 - allow connection to be established

Definition at line 387 of file tcp_ip.h.

Referenced by process_tcp_in().

#define TCP_EVENT_CONNECTED   2
 

Applications' event listener is informed by this event that connection is established and that it may start sending/receiving data.

Definition at line 397 of file tcp_ip.h.

Referenced by process_tcp_in().

#define TCP_EVENT_CLOSE   4
 

TCP connection was properly closed (either by calling tcp_close() by application or remote host initialized closing sequence).

Definition at line 405 of file tcp_ip.h.

Referenced by process_tcp_in(), and tcp_poll().

#define TCP_EVENT_ABORT   8
 

Connection is, for some reason, aborted. This can happen for a number of reasons:

  • Data retransmissions performed sufficient number of times but no acknowledgment was received
  • No response for some time to keep-alive packets
  • Remote host forcefully closed the connection for some reason
  • Application invoked tcp_abort() function

Definition at line 418 of file tcp_ip.h.

Referenced by process_tcp_in(), and tcp_poll().

#define TCP_EVENT_ACK   16
 

TCP/IP stack has received correct acknowledgment packet for the previously sent data and is informing the application about it. After this event, application can send new data packet to remote host.

Definition at line 428 of file tcp_ip.h.

Referenced by process_tcp_in().

#define TCP_EVENT_REGENERATE   32
 

Previously sent data packet was not acknowledged (or the acknowledgment packet did not arrive) so retransmission needs to be peformed. Application must resend the data that was sent in the previous packet.

Definition at line 437 of file tcp_ip.h.

Referenced by tcp_poll().

#define TCP_EVENT_DATA   64
 

TCP received some data from remote host and is informing application that it is available for reading from the Ethernet controller.

Definition at line 445 of file tcp_ip.h.

Referenced by process_tcp_in().

#define TCP_APP_OFFSET   MIN_TCP_HLEN
 

This value defines offset that TCP applications must use when writing to transmit buffer. This many bytes will be used before the first byte of applications data in the transmit buffer to store TCP header.

Definition at line 459 of file tcp_ip.h.

Referenced by smtpc_init().

#define UDP_APP_OFFSET   UDP_HLEN
 

This value defines offset that UDP applications must use when writing to transmit buffer. This many bytes will be used before the first byte of applications data in the transmit buffer to store UDP header.

Definition at line 471 of file tcp_ip.h.

Referenced by bootpc_run(), dhcpc_send_message(), and get_host_by_name().


Function Documentation

INT16 process_icmp_in struct ip_frame   frame,
UINT16    len
 

Author:
Date:
08.07.2002
Parameters:
frame  - pointer to received IP frame structure
len  - length of the received IP datagram (in bytes)
Returns:
  • -1 - packet not OK (not proper ICMP or not ICMP at all)
  • >=0 - packet OK
Invoke process_icmp_in whenever IP datagram containing ICMP message is detected (see main_demo.c for example main loop implementing this).

This function simply checks correctnes of received ICMP message and send ICMP replies when requested.

Definition at line 97 of file icmp.c.

References ip_frame::buf_index, ip_frame::dip, ip_checksum(), ip_checksum_buf(), IP_ICMP, NETWORK_RECEIVE_INITIALIZE, NETWORK_TX_BUFFER_SIZE, process_ip_out(), ip_frame::protocol, RECEIVE_NETWORK_B, RECEIVE_NETWORK_BUF, ip_frame::sip, and TXBUF.

UINT16 udp_getfreeport void   
 

Author:
Date:
19.10.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 684 of file udp.c.

References ucb::locport, ucb::state, UDP_PORTS_END, and UDP_STATE_CLOSED.

Referenced by udp_open().

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().

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().

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().

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().

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().

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().

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().

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().


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