tcp.c File Reference

#include "nip_init.h"
#include "dispatcher.h"
#include "inet.h"
#include "os_core.h"
#include "nip_error.h"
#include "net/net_if.h"
#include "net/tcp.h"
#include "net/numbers.h"

Include dependency graph for tcp.c:

Go to the source code of this file.

Defines

#define NIP_TCP_SOCKATTR(sockid, attr)   nip_tcp_sockets[sockid].attr

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_del_conn (nip_tcp_sock_id_t connid)
 Remove connection TCB and entry in socket list.
struct nip_tcbnip_tcp_find_conn (nip_tcp_sock_id_t sockid, uint8_t *rem_ip, uint16_t rem_port)
 Find transmission control block (tcb) for connection with matching address.
struct nip_tcbnip_tcp_create_conn (nip_tcp_sock_id_t sockid, uint8_t *rem_ip, uint16_t rem_port, nip_tcp_stat_t init_status)
 Create new transmission control block (tcb) with given parameters.
void nip_tcp_close (nip_tcp_sock_id_t connid)
 Close connection or remove socket if not connected.
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_write (nip_tcp_sock_id_t connid, uint8_t *buf, nip_mem_size_t size)
 Write data to connection.
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.
uint16_t nip_tcp_checksum (nip_net_if_trans_t *trans)
 Calculate TCP checksum.
void nip_tcp_disp_send (void)
 Dispatcher function to send TCP segment.
void nip_tcp_disp_receive (void)
 Dispatcher function to receive TCP segment.
void nip_tcp_disp_close (void)
 dispatcher function to remove closed connection
void nip_tcp_disp_check (void)
 Dispatcher function to check TCP connection states/retransmissions/.

Variables

struct nip_tcp_sock nip_tcp_sockets [NIP_TCP_MAX_SOCKETS]
nip_tcp_sock_cnt_t nip_tcp_unaccepted_cnt
nip_tcp_sock_id_t nip_tcp_unaccepted [NIP_TCP_MAX_UNACCEPTED]
nip_tcp_sock_cnt_t nip_tcp_conn_cnt
 number of connections in nip_tcp_conns
nip_mem_handle_t nip_tcp_conns = NIP_MEM_NULL
 dynamic memory for connection array


Define Documentation

#define NIP_TCP_SOCKATTR ( sockid,
attr   )     nip_tcp_sockets[sockid].attr

Definition at line 49 of file tcp.c.

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


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:

uint16_t nip_tcp_checksum ( nip_net_if_trans_t trans  ) 

Calculate TCP checksum.

If trans->header is set the checksum will be inserted there. Make sure neither trans->header nor trans->payload are locked

Returns:
checksum in network byte order

Definition at line 547 of file tcp.c.

References nip_tcp_header::checksum, nip_net_if_trans_params_t::dst_nw_addr, nip_net_if_trans_t::header, nip_net_if_trans_t::header_size, htons, nip_net_if_trans_params_t::ip, NIP_IP_PROTO_TCP, nip_mem_obtain_ptr(), nip_mem_release_block(), ntohs, NULL, nip_net_if_trans_t::params, nip_net_if_trans_t::payload, nip_net_if_trans_t::payload_size, and nip_net_if_trans_params_t::src_nw_addr.

Referenced by nip_tcp_disp_receive(), and nip_tcp_disp_send().

Here is the call graph for this function:

Here is the caller graph for this function:

void nip_tcp_close ( nip_tcp_sock_id_t  connid  ) 

struct nip_tcb* nip_tcp_create_conn ( nip_tcp_sock_id_t  sockid,
uint8_t *  rem_ip,
uint16_t  rem_port,
nip_tcp_stat_t  init_status 
) [read]

Create new transmission control block (tcb) with given parameters.

Returns:
pointer to tcb or NULL if there were no ressources to setup connection.
Note:
You _have_ to release the given pointer after using it by calling nip_mem_release_block( nip_tcp_conns );

Todo:
make iss random!

Definition at line 228 of file tcp.c.

References nip_tcb::connid, nip_tcp_sock_addr::ip, nip_tcb::iss, NIP_E_OK, nip_mem_alloc(), nip_mem_free(), NIP_MEM_NULL, nip_mem_obtain_ptr(), nip_mem_release_block(), nip_mem_set_used(), nip_mem_write(), nip_memcpy(), nip_memset, nip_tcp_conn_cnt, nip_tcp_conns, NIP_TCP_DEFAULT_RCV_WND, NIP_TCP_MAX_SOCKETS, NIP_TCP_STAT_LISTEN, nip_tickcount, NULL, nip_tcp_sock_addr::port, nip_tcb::rcv_buf, nip_tcb::rcv_wnd, nip_tcb::rem_addr, nip_tcb::snd_buf, nip_tcb::snd_nxt, nip_tcb::snd_una, nip_tcb::sockid, and nip_tcb::status.

Referenced by nip_tcp_disp_receive().

Here is the call graph for this function:

Here is the caller graph for this function:

void nip_tcp_del_conn ( nip_tcp_sock_id_t  connid  ) 

Remove connection TCB and entry in socket list.

Definition at line 99 of file tcp.c.

References nip_tcb::connid, nip_tcb::flags, nip_mem_free(), nip_mem_obtain_ptr(), nip_mem_release_block(), nip_mem_set_used(), nip_memcpy(), nip_tcp_conn_cnt, nip_tcp_conns, NIP_TCP_MAX_SOCKETS, NIP_TCP_NO_SOCKET, NIP_TCP_SOCK_FLG_AUTOCLOSE, NIP_TCP_SOCK_FLG_LISTENING, NIP_TCP_SOCKATTR, NULL, nip_tcb::rcv_buf, nip_tcb::snd_buf, and nip_tcb::sockid.

Referenced by nip_tcp_close(), and nip_tcp_disp_close().

Here is the call graph for this function:

Here is the caller graph for this function:

void nip_tcp_disp_check ( void   ) 

void nip_tcp_disp_close ( void   ) 

dispatcher function to remove closed connection

Definition at line 1251 of file tcp.c.

References nip_disp_state_t::next, nip_disp, nip_tcp_del_conn(), nip_disp_params_t::sockid, and nip_disp_params_t::tcp.

Referenced by nip_tcp_disp_check().

Here is the call graph for this function:

Here is the caller graph for this function:

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:

void nip_tcp_disp_send ( void   ) 

Dispatcher function to send TCP segment.

The following dispatcher parameters are expected to be configured:

  • nip_disp.next.tcp.sockid
  • nip_disp.next.tcp.flags

make sure it's multiples of 4

Todo:
if SYN flag is set and NO_ROUTE_TO_HOST error occured -> cancel

Todo:
Recognize failed connection attempts when ARP fails? Don't do automatic reset of transmission status. You have to implement some check function then, to reset transmission status later on.

Todo:
implement check, that TCP segments don't become too large --> mind tcb->snd_wnd and default-segment-size

Todo:
check if it's ok to set seq to snd_nxt for the default case?! If not: what other default value would seq get?

Todo:
adjust window size??

Todo:
implement urgent pointer

Definition at line 620 of file tcp.c.

References nip_tcp_header::ack, nip_tcp_header::checksum, nip_disp_params_t::common, nip_tcp_header::dataoffset, nip_tcp_header::dstport, nip_tcb::flags, nip_disp_params_t::flags, nip_tcp_header::flags, nip_net_if_trans_t::flags, nip_net_if_trans_flags_t::free_payload, nip_net_if_trans_t::header, nip_net_if_trans_t::header_size, htonl, htons, nip_net_if_trans_params_t::ip, nip_tcp_sock_addr::ip, nip_disp_state_t::next, NIP_CURR_CMD, nip_disp, NIP_E_OK, nip_ip_disp_send(), NIP_IP_PROTO_TCP, nip_ip_route(), nip_mem_alloc(), nip_mem_buf_used(), NIP_MEM_NULL, nip_mem_obtain_ptr(), nip_mem_release_block(), nip_mem_write(), NIP_NET_IF_RESOLV_ADDR, NIP_NET_NO_IF, nip_reset_trans(), NIP_TCP_ACK_FLAG, nip_tcp_checksum(), nip_tcp_conns, NIP_TCP_FIN_FLAG, nip_tcp_find_conn(), NIP_TCP_MIN_HEADER_SIZE, NIP_TCP_PSH_FLAG, NIP_TCP_RTO_BETA, NIP_TCP_RTO_LBOUND, NIP_TCP_RTO_UBOUND, NIP_TCP_SOCK_FLG_SEND, NIP_TCP_SOCKATTR, NIP_TCP_STAT_CLOSING, NIP_TCP_STAT_FIN_WAIT1, NIP_TCP_STAT_LAST_ACK, NIP_TCP_STAT_SYN_RECEIVED, NIP_TCP_STAT_SYN_SENT, NIP_TCP_SYN_FLAG, nip_tickcount, NULL, nip_net_if_trans_t::params, nip_net_if_trans_t::payload, nip_net_if_trans_t::payload_off, nip_net_if_trans_t::payload_size, nip_tcp_sock_addr::port, nip_net_if_trans_params_t::protocol, nip_tcb::push_ptr, nip_tcb::rcv_nxt, nip_tcb::rcv_wnd, nip_tcb::rem_addr, nip_net_if_trans_flags_t::reset_status, nip_tcb::rt_count, 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::sockid, nip_disp_params_t::sockid, nip_tcp_header::srcport, nip_tcb::srtt, nip_net_if_trans_t::status, nip_tcb::status, nip_disp_params_t::tcp, nip_disp_params_t::trans, and nip_tcp_header::window.

Referenced by nip_tcp_disp_check(), and nip_tcp_disp_receive().

Here is the call graph for this function:

Here is the caller graph for this function:

struct nip_tcb* nip_tcp_find_conn ( nip_tcp_sock_id_t  sockid,
uint8_t *  rem_ip,
uint16_t  rem_port 
) [read]

Find transmission control block (tcb) for connection with matching address.

If rem_ip is not NULL the given sockid will be matched against the Ids of the socket-array, otherwise sockid will be matched against connection IDs.

Returns:
pointer to tcb or NULL if tcb was not found. If NULL is returned, nip_error will contain one of the following error codes
  • NIP_E_AGAIN needed ressources were locked
  • NIP_E_INVALID_SOCK given sockid does not belong to any open connection
Note:
You _have_ to release the given pointer after using it by calling nip_mem_release_block( nip_tcp_conns );

Definition at line 182 of file tcp.c.

References nip_tcb::connid, nip_tcp_sock_addr::ip, NIP_E_AGAIN, NIP_E_INVALID_SOCK, nip_error, nip_mem_obtain_ptr(), nip_mem_release_block(), nip_memcmp, nip_tcp_conn_cnt, nip_tcp_conns, NIP_TCP_STAT_CLOSED, NULL, nip_tcp_sock_addr::port, nip_tcb::rem_addr, nip_tcb::sockid, and nip_tcb::status.

Referenced by nip_tcp_accept(), nip_tcp_close(), nip_tcp_disp_receive(), nip_tcp_disp_send(), nip_tcp_read(), and nip_tcp_write().

Here is the call graph for this function:

Here is the caller 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:


Variable Documentation

number of connections in nip_tcp_conns

Definition at line 54 of file tcp.c.

Referenced by nip_tcp_create_conn(), nip_tcp_del_conn(), nip_tcp_disp_check(), and nip_tcp_find_conn().

struct nip_tcp_sock nip_tcp_sockets[NIP_TCP_MAX_SOCKETS]

Definition at line 48 of file tcp.c.

nip_tcp_sock_id_t nip_tcp_unaccepted[NIP_TCP_MAX_UNACCEPTED]

Definition at line 53 of file tcp.c.

Referenced by nip_tcp_accept(), and nip_tcp_disp_receive().

Definition at line 52 of file tcp.c.

Referenced by nip_tcp_accept(), and nip_tcp_disp_receive().


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