tcp.h File Reference

#include "nip_init.h"
#include "mem.h"

Include dependency graph for tcp.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  nip_tcp_sock_addr
struct  nip_tcp_sock
struct  nip_tcb
 Transmission Control Block, any TCP connection has to have one. More...
struct  nip_tcp_header

Defines

#define NIP_TCP_MAX_SOCKETS   2
 Define maximum number of TCP sockets for static socket array.
#define NIP_TCP_MAX_CONNECTIONS   5
 Define maximum number of TCP sockets for dynamic connection array.
#define NIP_TCP_MAX_UNACCEPTED   5
 Define maximum number of unaccepted TCP connections.
#define NIP_TCP_DEFAULT_RCV_WND   150
 Default Receive Window Size.
#define NIP_TCP_MSL   120
 maximum segment lifetime
#define NIP_TCP_RTO_UBOUND   60
 Upper bound on retransmission timeout (RTO).
#define NIP_TCP_RTO_LBOUND   1
 Lower bound on retransmission timeout (RTO).
#define NIP_TCP_RTO_ALPHA   1
 Smoothing factor for round-trip-time.
#define NIP_TCP_RTO_ALPHA_NEG   0
#define NIP_TCP_RTO_BETA   2
 Delay variance factor for RTO.
#define NIP_TCP_NO_SOCKET   0xFF
#define NIP_TCP_SOCK_FLG_USED   0x01
 set to mark socket as used
#define NIP_TCP_SOCK_FLG_NON_BLOCK   0x02
 set to mark non-blocking socket
#define NIP_TCP_SOCK_FLG_LISTENING   0x04
 wait for incoming connections?
#define NIP_TCP_SOCK_FLG_ACCEPTED   0x08
 user accepted connection
#define NIP_TCP_SOCK_FLG_RECV   0x10
 socket ready to receive
#define NIP_TCP_SOCK_FLG_SEND   0x20
 socket ready to send
#define NIP_TCP_SOCK_FLG_SHUT_WR   0x40
 quit writing data on socket
#define NIP_TCP_SOCK_FLG_SHUT_RD   0x80
 quit receiving data on socket
#define NIP_TCP_SOCK_FLG_AUTOCLOSE   (NIP_TCP_SOCK_FLG_SHUT_WR|NIP_TCP_SOCK_FLG_SHUT_RD)
#define NIP_TCP_MIN_HEADER_SIZE   20
 minimum size of TCP header
#define NIP_TCP_MIN_DATA_OFFSET   0x50
#define NIP_TCP_RESET_ACK   0xFFFFFFFF
#define NIP_TCP_SHUT_RD   NIP_TCP_SOCK_FLG_SHUT_RD
 shut down packet receival
#define NIP_TCP_SHUT_WR   NIP_TCP_SOCK_FLG_SHUT_WR
 shut down packet sending
#define NIP_TCP_SHUT_RDWR   (NIP_TCP_SHUT_RD|NIP_TCP_SHUT_WR)
#define NIP_TCP_FIN_FLAG   0x01
#define NIP_TCP_SYN_FLAG   0x02
#define NIP_TCP_RST_FLAG   0x04
#define NIP_TCP_PSH_FLAG   0x08
#define NIP_TCP_ACK_FLAG   0x10
#define NIP_TCP_URG_FLAG   0x20

Typedefs

typedef uint8_t nip_tcp_sock_id_t
typedef uint8_t nip_tcp_sock_cnt_t
typedef enum nip_tcp_stat nip_tcp_stat_t
typedef uint32_t nip_tcp_seq_t

Enumerations

enum  nip_tcp_stat {
  NIP_TCP_STAT_CLOSED = 0, NIP_TCP_STAT_LISTEN = 1, NIP_TCP_STAT_SYN_RECEIVED = 2, NIP_TCP_STAT_SYN_SENT = 3,
  NIP_TCP_STAT_ESTABLISHED = 4, NIP_TCP_STAT_FIN_WAIT1 = 5, NIP_TCP_STAT_FIN_WAIT2 = 6, NIP_TCP_STAT_CLOSE_WAIT = 7,
  NIP_TCP_STAT_CLOSING = 8, NIP_TCP_STAT_LAST_ACK = 9, NIP_TCP_STAT_TIMEWAIT = 10
}
enum  nip_tcp_conn_op_t { NIP_TCP_CONN_OP_ADD, NIP_TCP_CONN_OP_DEL, NIP_TCP_CONN_OP_FIND }

Functions

nip_tcp_sock_id_t nip_tcp_socket (struct nip_tcp_sock_addr *addr, uint8_t flags)
 Register TCP socket with given address and flags.
void nip_tcp_close (nip_tcp_sock_id_t connid)
 Close connection or remove socket if not connected.
void nip_tcp_disp_check (void)
 Dispatcher function to check TCP connection states/retransmissions/.
void nip_tcp_disp_receive (void)
 Dispatcher function to receive TCP segment.
nip_tcp_sock_id_t nip_tcp_accept (nip_tcp_sock_id_t sockid)
 Looks for incoming connections on given listening socket.
nip_mem_size_t nip_tcp_read (nip_tcp_sock_id_t connid, uint8_t *buf, nip_mem_size_t size)
 Read data from connection into given buffer.
nip_mem_size_t nip_tcp_write (nip_tcp_sock_id_t connid, uint8_t *buf, nip_mem_size_t size)
 Write data to connection.


Define Documentation

#define NIP_TCP_ACK_FLAG   0x10

Definition at line 220 of file tcp.h.

Referenced by nip_tcp_disp_receive(), and nip_tcp_disp_send().

#define NIP_TCP_DEFAULT_RCV_WND   150

Default Receive Window Size.

Every IP host should be able to receive 576 Byte long IP packets. Assuming standard headers, that results in 536 Byte for TCP payload.

Definition at line 58 of file tcp.h.

Referenced by nip_tcp_create_conn().

#define NIP_TCP_FIN_FLAG   0x01

Definition at line 216 of file tcp.h.

Referenced by nip_tcp_disp_receive(), and nip_tcp_disp_send().

#define NIP_TCP_MAX_CONNECTIONS   5

Define maximum number of TCP sockets for dynamic connection array.

Definition at line 45 of file tcp.h.

#define NIP_TCP_MAX_SOCKETS   2

Define maximum number of TCP sockets for static socket array.

Todo:
check why setting this to 1 leads to massive compile errors

Definition at line 40 of file tcp.h.

Referenced by nip_tcp_create_conn(), nip_tcp_del_conn(), nip_tcp_disp_receive(), and nip_tcp_socket().

#define NIP_TCP_MAX_UNACCEPTED   5

Define maximum number of unaccepted TCP connections.

Definition at line 50 of file tcp.h.

Referenced by nip_tcp_disp_receive().

#define NIP_TCP_MIN_DATA_OFFSET   0x50

Definition at line 116 of file tcp.h.

Referenced by nip_tcp_disp_receive().

#define NIP_TCP_MIN_HEADER_SIZE   20

minimum size of TCP header

Definition at line 115 of file tcp.h.

Referenced by nip_tcp_disp_receive(), and nip_tcp_disp_send().

#define NIP_TCP_MSL   120

maximum segment lifetime

Definition at line 63 of file tcp.h.

Referenced by nip_tcp_disp_receive().

#define NIP_TCP_NO_SOCKET   0xFF

Definition at line 94 of file tcp.h.

Referenced by nip_tcp_accept(), nip_tcp_del_conn(), nip_tcp_disp_receive(), and nip_tcp_socket().

#define NIP_TCP_PSH_FLAG   0x08

Definition at line 219 of file tcp.h.

Referenced by nip_tcp_disp_send().

#define NIP_TCP_RESET_ACK   0xFFFFFFFF

Definition at line 117 of file tcp.h.

Referenced by nip_tcp_disp_receive().

#define NIP_TCP_RST_FLAG   0x04

Definition at line 218 of file tcp.h.

Referenced by nip_tcp_disp_receive().

#define NIP_TCP_RTO_ALPHA   1

Smoothing factor for round-trip-time.

RFC 793 proposes 0.8 or 0.9

Definition at line 81 of file tcp.h.

#define NIP_TCP_RTO_ALPHA_NEG   0

Definition at line 83 of file tcp.h.

#define NIP_TCP_RTO_BETA   2

Delay variance factor for RTO.

RFC 793 proposes 1.3 to 2.0

Definition at line 87 of file tcp.h.

Referenced by nip_tcp_disp_send().

#define NIP_TCP_RTO_LBOUND   1

Lower bound on retransmission timeout (RTO).

This value MUST NOT exceed 255, due to data type restrictions.

Definition at line 76 of file tcp.h.

Referenced by nip_tcp_disp_receive(), and nip_tcp_disp_send().

#define NIP_TCP_RTO_UBOUND   60

Upper bound on retransmission timeout (RTO).

This value MUST NOT exceed 255, due to data type restrictions.

Definition at line 70 of file tcp.h.

Referenced by nip_tcp_disp_send().

#define NIP_TCP_SHUT_RD   NIP_TCP_SOCK_FLG_SHUT_RD

shut down packet receival

Definition at line 119 of file tcp.h.

Referenced by nip_tcp_read().

#define NIP_TCP_SHUT_RDWR   (NIP_TCP_SHUT_RD|NIP_TCP_SHUT_WR)

Definition at line 121 of file tcp.h.

#define NIP_TCP_SHUT_WR   NIP_TCP_SOCK_FLG_SHUT_WR

shut down packet sending

Definition at line 120 of file tcp.h.

Referenced by nip_tcp_close().

#define NIP_TCP_SOCK_FLG_ACCEPTED   0x08

user accepted connection

Definition at line 108 of file tcp.h.

Referenced by nip_tcp_accept().

#define NIP_TCP_SOCK_FLG_AUTOCLOSE   (NIP_TCP_SOCK_FLG_SHUT_WR|NIP_TCP_SOCK_FLG_SHUT_RD)

Definition at line 113 of file tcp.h.

Referenced by nip_tcp_del_conn(), and nip_tcp_disp_check().

#define NIP_TCP_SOCK_FLG_LISTENING   0x04

wait for incoming connections?

Definition at line 107 of file tcp.h.

Referenced by nip_tcp_del_conn(), and nip_tcp_disp_receive().

#define NIP_TCP_SOCK_FLG_NON_BLOCK   0x02

set to mark non-blocking socket

Definition at line 106 of file tcp.h.

#define NIP_TCP_SOCK_FLG_RECV   0x10

socket ready to receive

Definition at line 109 of file tcp.h.

Referenced by nip_tcp_disp_receive(), and nip_tcp_read().

#define NIP_TCP_SOCK_FLG_SEND   0x20

socket ready to send

Definition at line 110 of file tcp.h.

Referenced by nip_tcp_close(), nip_tcp_disp_receive(), and nip_tcp_disp_send().

#define NIP_TCP_SOCK_FLG_SHUT_RD   0x80

quit receiving data on socket

Definition at line 112 of file tcp.h.

Referenced by nip_tcp_disp_receive().

#define NIP_TCP_SOCK_FLG_SHUT_WR   0x40

quit writing data on socket

Definition at line 111 of file tcp.h.

#define NIP_TCP_SOCK_FLG_USED   0x01

set to mark socket as used

Definition at line 105 of file tcp.h.

Referenced by nip_tcp_socket().

#define NIP_TCP_SYN_FLAG   0x02

Definition at line 217 of file tcp.h.

Referenced by nip_tcp_disp_receive(), and nip_tcp_disp_send().

#define NIP_TCP_URG_FLAG   0x20

Definition at line 221 of file tcp.h.


Typedef Documentation

typedef uint32_t nip_tcp_seq_t

Definition at line 163 of file tcp.h.

typedef uint8_t nip_tcp_sock_cnt_t

Definition at line 93 of file tcp.h.

typedef uint8_t nip_tcp_sock_id_t

Definition at line 92 of file tcp.h.


Enumeration Type Documentation

Enumerator:
NIP_TCP_CONN_OP_ADD 
NIP_TCP_CONN_OP_DEL 
NIP_TCP_CONN_OP_FIND 

Definition at line 242 of file tcp.h.

Enumerator:
NIP_TCP_STAT_CLOSED 
NIP_TCP_STAT_LISTEN 
NIP_TCP_STAT_SYN_RECEIVED 
NIP_TCP_STAT_SYN_SENT 
NIP_TCP_STAT_ESTABLISHED 
NIP_TCP_STAT_FIN_WAIT1 
NIP_TCP_STAT_FIN_WAIT2 
NIP_TCP_STAT_CLOSE_WAIT 
NIP_TCP_STAT_CLOSING 
NIP_TCP_STAT_LAST_ACK 
NIP_TCP_STAT_TIMEWAIT 

Definition at line 144 of file tcp.h.


Function Documentation

nip_tcp_sock_id_t nip_tcp_accept ( nip_tcp_sock_id_t  sockid  ) 

Looks for incoming connections on given listening socket.

Definition at line 392 of file tcp.c.

References nip_tcb::connid, nip_tcb::flags, nip_mem_release_block(), nip_memmove, nip_tcp_conns, nip_tcp_find_conn(), NIP_TCP_NO_SOCKET, NIP_TCP_SOCK_FLG_ACCEPTED, nip_tcp_unaccepted, nip_tcp_unaccepted_cnt, NULL, and nip_tcb::sockid.

Here is the call graph for this function:

void nip_tcp_close ( nip_tcp_sock_id_t  connid  ) 

void nip_tcp_disp_check ( void   ) 

void nip_tcp_disp_receive ( void   ) 

Dispatcher function to receive TCP segment.

Requires nip_disp.next.common.trans to point to valid transmission

Todo:
check checksum

Todo:
delete top of queue. Current implementation has no queue and therefor assumes next item to be snd_nxt, which is not the case if there have been multiple transmissions since. Also there is nothing known about the original transmission time of the next segment, to assume the current time is wrong. We're doing that to save program memory, as TCP will also work this way. It might just perform slower, as retransmissions of subsequent segments may be delayed.

Todo:
perform roundtriptime calculation here

update receive window?

Todo:
check options
Todo:
tcb->snd_maxsegsize = ?

Definition at line 804 of file tcp.c.

References nip_tcp_header::ack, nip_disp_params_t::common, nip_tcb::connid, nip_tcp_header::dataoffset, nip_net_if_trans_params_t::dst_nw_addr, nip_net_if_trans_params_t::dst_port, nip_tcp_header::dstport, nip_tcb::flags, nip_disp_params_t::flags, nip_net_if_trans_t::flags, nip_tcp_header::flags, nip_net_if_trans_flags_t::free_payload, nip_net_if_trans_t::header, nip_net_if_trans_t::header_size, htonl, nip_net_if_trans_params_t::ip, nip_tcb::irs, nip_disp_state_t::next, NIP_CURR_CMD, nip_disp, NIP_E_OK, nip_ip_disp_send(), nip_ip_null, NIP_IP_PROTO_TCP, nip_ip_route(), nip_mem_alloc(), nip_mem_buf_used(), nip_mem_move(), NIP_MEM_NULL, nip_mem_obtain_ptr(), nip_mem_read(), nip_mem_read_at_pos(), nip_mem_release_block(), nip_mem_set_used(), nip_mem_write(), nip_memcmp, nip_memcpy(), NIP_NET_IF_DONE, NIP_NET_IF_RESOLV_ADDR, NIP_NET_NO_IF, nip_reset_trans(), NIP_TCP_ACK_FLAG, nip_tcp_checksum(), nip_tcp_conns, nip_tcp_create_conn(), nip_tcp_disp_send(), NIP_TCP_FIN_FLAG, nip_tcp_find_conn(), NIP_TCP_MAX_SOCKETS, NIP_TCP_MAX_UNACCEPTED, NIP_TCP_MIN_DATA_OFFSET, NIP_TCP_MIN_HEADER_SIZE, NIP_TCP_MSL, NIP_TCP_NO_SOCKET, NIP_TCP_RESET_ACK, NIP_TCP_RST_FLAG, NIP_TCP_RTO_LBOUND, NIP_TCP_SOCK_FLG_LISTENING, NIP_TCP_SOCK_FLG_RECV, NIP_TCP_SOCK_FLG_SEND, NIP_TCP_SOCK_FLG_SHUT_RD, NIP_TCP_SOCKATTR, NIP_TCP_STAT_CLOSE_WAIT, NIP_TCP_STAT_CLOSED, NIP_TCP_STAT_CLOSING, NIP_TCP_STAT_ESTABLISHED, NIP_TCP_STAT_FIN_WAIT1, NIP_TCP_STAT_FIN_WAIT2, NIP_TCP_STAT_LAST_ACK, NIP_TCP_STAT_LISTEN, NIP_TCP_STAT_SYN_RECEIVED, NIP_TCP_STAT_SYN_SENT, NIP_TCP_STAT_TIMEWAIT, NIP_TCP_SYN_FLAG, nip_tcp_unaccepted, nip_tcp_unaccepted_cnt, nip_tickcount, ntohl, ntohs, NULL, nip_net_if_trans_t::params, nip_net_if_trans_t::payload, nip_net_if_trans_t::payload_size, nip_net_if_trans_params_t::protocol, nip_tcb::rcv_buf, nip_tcb::rcv_nxt, nip_tcb::rcv_wnd, nip_tcb::rt_una, nip_tcb::rto, nip_tcp_header::seq, nip_tcb::snd_buf, nip_tcb::snd_nxt, nip_tcb::snd_una, nip_tcb::snd_wl1, nip_tcb::snd_wl2, nip_tcb::snd_wnd, nip_disp_params_t::sockid, nip_tcb::sockid, nip_net_if_trans_params_t::src_nw_addr, nip_net_if_trans_params_t::src_port, nip_tcp_header::srcport, nip_net_if_trans_t::status, nip_tcb::status, nip_disp_params_t::tcp, nip_net_if_trans_params_t::tcp, nip_tcb::timeout, nip_disp_params_t::trans, and nip_tcp_header::window.

Here is the call graph for this function:

nip_mem_size_t nip_tcp_read ( nip_tcp_sock_id_t  connid,
uint8_t *  buf,
nip_mem_size_t  size 
)

Read data from connection into given buffer.

Returns:
number of bytes copied. If 0 was returned, nip_error will contain one of the following error codes
  • NIP_E_EOF socket has been closed by remote peer
  • NIP_E_AGAIN no data available or ressource locked
  • NIP_E_INVALID_SOCK given sockid did not match any open connection

Definition at line 500 of file tcp.c.

References nip_tcb::flags, NIP_E_AGAIN, NIP_E_EOF, nip_error, nip_mem_read(), nip_mem_release_block(), nip_tcp_conns, nip_tcp_find_conn(), NIP_TCP_SHUT_RD, NIP_TCP_SOCK_FLG_RECV, NIP_TCP_STAT_ESTABLISHED, NULL, nip_tcb::rcv_buf, and nip_tcb::status.

Here is the call graph for this function:

nip_tcp_sock_id_t nip_tcp_socket ( struct nip_tcp_sock_addr addr,
uint8_t  flags 
)

Register TCP socket with given address and flags.

Parameters:
addr structure to be filled with address and port. If all address bytes are set to Zero, the socket will listen on all interfaces. If port is set to Zero, the implementation will choose a random port, which should be the usual way to go for sockets with the NIP_TCP_SOCK_FLG_LISTENING-flag set.
flags possible flags are:
  • NIP_TCP_SOCK_FLG_NON_BLOCK
  • NIP_TCP_SOCK_FLG_LISTENING
Returns:
NIP_TCP_NO_SOCKET on error. Global nip_error variable will be set to NIP_E_OUT_OF_RESSOURCES in that case.

Definition at line 70 of file tcp.c.

References NIP_E_OUT_OF_RESSOURCES, nip_error, nip_memcpy(), NIP_TCP_MAX_SOCKETS, NIP_TCP_NO_SOCKET, NIP_TCP_SOCK_FLG_USED, NIP_TCP_SOCKATTR, and nip_tcp_sock::sockid.

Here is the call graph for this function:

nip_mem_size_t nip_tcp_write ( nip_tcp_sock_id_t  connid,
uint8_t *  buf,
nip_mem_size_t  size 
)

Write data to connection.

Returns:
number of bytes written. If 0 was returned, nip_error will contain one of the following error codes
  • NIP_E_EOF socket has been closed and is now read-only
  • NIP_E_AGAIN not enough space in send buffer or ressource locked
  • NIP_E_INVALID_SOCK given sockid did not match any open connection

Definition at line 441 of file tcp.c.

References NIP_DISP_CHECK_TCP, nip_disp_notify, nip_dispatcher(), NIP_E_AGAIN, NIP_E_EOF, NIP_E_OK, nip_error, nip_mem_release_block(), nip_mem_write(), nip_tcp_conns, nip_tcp_find_conn(), NIP_TCP_STAT_CLOSING, NIP_TCP_STAT_FIN_WAIT1, NIP_TCP_STAT_FIN_WAIT2, NIP_TCP_STAT_LAST_ACK, NIP_TCP_STAT_TIMEWAIT, NULL, nip_tcb::snd_buf, and nip_tcb::status.

Here is the call graph for this function:


Generated on Thu Jul 10 01:09:48 2008 for NIP by  doxygen 1.5.5