Watt-32 tcp/ip  2.2 dev-rel.10
Functions | Variables
socket.c File Reference

Go to the source code of this file.

Functions

int _fsext_demux (__FSEXT_Fnumber func, int *rv, va_list _args)
 
long _get_osfhandle (int)
 
static __inline Socketsock_hash_get (int fd)
 
static __inline void sock_hash_add (int fd, Socket *sock)
 
void_sock_calloc (size_t size, const char *file, unsigned line)
 Memory allocation; print some info if allocation fails.
 
static int sock_alloc_fd (char **err)
 
BOOL _sock_dos_fd (int s)
 _sock_dos_fd - Return TRUE if `s' is a valid DOS/Win32 handle. More...
 
int _sock_set_rcv_buf (sock_type *s, size_t len)
 Setup a bigger receive buffer, the default in Wattcp is only 2k. More...
 
void _sock_free_rcv_buf (sock_type *s)
 Free receive buffer associated with udp/tcp sockets.
 
static __inline Socketsk_list_del (int s)
 Deletes the list element associated with a socket. More...
 
const sock_type__get_sock_from_s (int s, int proto)
 Hack function if user application needs to use Wattcp core functions for BSD sockets. More...
 
static __inline BOOL other_tcp_listeners (const Socket *_this)
 Traverse socket-list to find other SOCK_STREAM sockets besides '_this' which are also listening.
 
void _sock_set_syn_hook (int(*func)(_tcp_Socket **))
 
static __inline BOOL other_raw_socks (const Socket *_this)
 Traverse socket-list to find other SOCK_RAW sockets besides '_this'. More...
 
BOOL _sock_set_normal_rx_mode (const Socket *_this)
 
BOOL _sock_set_mcast_rx_mode (void)
 
BOOL _sock_set_promisc_rx_mode (void)
 
static void sock_raw_del (Socket *sock)
 Free buffers and hooks used by a SOCK_RAW socket.
 
static BOOL sock_packet_add (Socket *sock)
 Allocate ringbuffer and rx-buffers for a SOCK_PACKET socket.
 
static void sock_packet_del (Socket *sock)
 
static int W32_CALL sock_packet_peek (const union link_Packet *pkt)
 
static char get_pkt_type_eth (const eth_Header *eth)
 Return correct PACKET_xx type.
 
static char get_pkt_type_fddi (const fddi_Header *fddi)
 
static char get_pkt_type_tok (const tok_Header *tok)
 
static char get_pkt_type_arc (const arcnet_Header *arc)
 
static char get_pkt_type (const union link_Packet *pkt)
 
unsigned sock_packet_receive (Socket *sock, void *buf, unsigned len, struct sockaddr *from, size_t *fromlen)
 Called from receive.c for AF_PACKET sockets. More...
 
unsigned sock_packet_rbused (Socket *sock)
 
unsigned sock_packet_transmit (Socket *socket, const void *buf, unsigned len, const struct sockaddr *to, int tolen)
 
static void sock_close_fd (int fd)
 
Socket_sock_del_fd (int fd, const char *file, unsigned line)
 Delete the socket from `inuse' array and all memory associated with it. More...
 
static int sock_find_fd (const Socket *socket)
 Finds the 'fd' associated with pointer 'socket'. More...
 
static Socketsock_find_udp (const _udp_Socket *udp)
 Finds the 'Socket' associated with udp-socket 'udp'. More...
 
static voidsock_find_tcp (const _tcp_Socket *tcp)
 Finds the 'Socket*' associated with tcp-socket '*tcp'.
 
int _sock_chk_sockaddr (Socket *socket, const struct sockaddr *sa, int len)
 Check `sockaddr*' passed to bind/connect.
 
static struct _raw_Socketfind_free_raw_sock (Socket *sock)
 Search in sock->raw?_sock list for an unused raw-buffer.
 
static struct _raw6_Socketfind_free_raw6_sock (Socket *sock)
 
static int sock_raw4_recv (const in_Header *ip)
 Called from _ip4_handler() via `_bsd_socket_hook'. More...
 
static int sock_raw6_recv (const in6_Header *ip)
 
static void do_keepalive (Socket *sock)
 Handle keepalive on STREAM socket. More...
 
static void *MS_CDECL socket_op_demux (enum BSD_SOCKET_OPS op,...)
 The demultiplexer for callback operations called via '_bsd_socket_hook' from functions in the core Wattcp API.
 
static Sockettcp_sock_daemon (Socket *sock)
 Called by sock_daemon() to handle SOCK_STREAM sockets. More...
 
static Socketudp_sock_daemon (Socket *sock)
 Called by sock_daemon() for SOCK_DGRAM sockets. More...
 
static void sock_daemon (void)
 A daemon called from tcp_tick(). More...
 
void _sock_crit_start (void)
 Start a critical region. More...
 
void _sock_crit_stop (void)
 Mark the end of a critical region. More...
 
static void sock_fortify_exit (void)
 Exit handler for Fortify. More...
 
static const char * vendor_version (void)
 Return version string of current compiler.
 
static void check_errno_version (void)
 Check that . More...
 
static BOOL init_sockets (void)
 Main initialisation routine for the BSD socket API. More...
 
Socket_socklist_find (int s)
 Returns a pointer to the Socket structure associated with socket 's'. More...
 
static BOOL sock_raw_add (Socket *sock, int family)
 Allocate raw?_Socket buffers for a SOCK_RAW socket.
 
static Socketsocklist_add (int s, int type, int proto, int family)
 Adds a new socket to the sk_list.
 
static int set_proto (int type, BOOL is_ip6, int *proto)
 Select (and check) a suitable protocol for socket-type. More...
 
static const char * proto_name (int proto)
 
int W32_CALL socket (int family, int type, int protocol)
 socket(). More...
 
static int stream_cancel (const _tcp_Socket *tcp, BYTE icmp_type, int error)
 Callback handlers for "ICMP Port/Host/Network Unreachable" or "ICMP Parameter Problem" issued by lower layer (udp_cancel() and tcp_cancel() in pctcp.c) More...
 
static int dgram_cancel (const _udp_Socket *udp, BYTE icmp_type, int error)
 
static __inline const char * ip_proto_str (WORD prot)
 
static int icmp_callback (sock_type *s, BYTE icmp_type, BYTE icmp_code)
 
int _UDP_open (Socket *socket, struct in_addr host, WORD loc_port, WORD rem_port)
 Open and listen routines for SOCK_DGRAM at the socket-level.
 
int _UDP_listen (Socket *socket, struct in_addr host, WORD port)
 
int _TCP_open (Socket *socket, struct in_addr host, WORD loc_port, WORD rem_port)
 Open and listen routines for SOCK_STREAM at the socket-level. More...
 
int _TCP_listen (Socket *socket, struct in_addr host, WORD port)
 
int _TCP6_open (Socket *socket, const void *dst, WORD loc_port, WORD rem_port)
 
int _TCP6_listen (Socket *socket, const void *host, WORD loc_port)
 
int _UDP6_open (Socket *socket, const void *host, WORD loc_port, WORD rem_port)
 
int _UDP6_listen (Socket *socket, const void *host, WORD port)
 
int _sock_half_open (const _tcp_Socket *s)
 _sock_half_open - Return true if peer closed his side. More...
 
int W32_CALL socketpair (int family, int type, int protocol, int usockvec[2])
 socketpair() - Create a pair of connected sockets. More...
 

Variables

static int sk_block = 0
 
static int sk_last = SK_FIRST+1
 
static Socketsk_list = NULL
 
static DWORD raw_seq = 0
 
static int(* tcp_syn_hook )(_tcp_Socket **tcp) = NULL
 
static int(* ip4_raw_hook )(const in_Header *ip) = NULL
 
static int(* ip6_raw_hook )(const in6_Header *ip) = NULL
 
static Socketsk_hashes [SOCK_HASH_SIZE]
 
static fd_set inuse [NUM_SOCK_FDSETS]
 `inuse[]' has a non-zero bit for each socket-descriptor in use. More...
 
static BOOL mcast_set = FALSE
 Switch back to normal rx-mode (RXMODE_BROADCAST) if no multicasts active and no more IPv6/SOCK_PACKET sockets besides '_this'.
 
static BOOL promis_set = FALSE
 

Detailed Description

BSD socket().

Definition in file socket.c.

Function Documentation

const sock_type* __get_sock_from_s ( int  s,
int  proto 
)

Hack function if user application needs to use Wattcp core functions for BSD sockets.

Must not modify return value in any way.

Definition at line 308 of file socket.c.

References _socklist_find().

void _sock_crit_start ( void  )

Start a critical region.

Prevent `sk_list' being destroyed (e.g. in sock_daemon) and thus confusing select_s(), connect() etc.

Definition at line 1296 of file socket.c.

void _sock_crit_stop ( void  )

Mark the end of a critical region.

If blocking-level reached zero, we run our socket-daemon.

Definition at line 1306 of file socket.c.

References sock_daemon().

Socket* _sock_del_fd ( int  fd,
const char *  file,
unsigned  line 
)

Delete the socket from `inuse' array and all memory associated with it.

Also unlink it from the socket list (sk_list). Clear _tcp_syn_hook is no more SOCK_STREAM listeners. Return pointer to next node in list or NULL if none/error.

Definition at line 701 of file socket.c.

References _bsd_socket_hook(), _pkt_forced_rxmode, _pkt_rxmode, _sock_free_rcv_buf(), _socklist_find(), other_tcp_listeners(), RXMODE_BROADCAST, sk_list_del(), sock_abort(), and sock_raw_del().

BOOL _sock_dos_fd ( int  s)

_sock_dos_fd - Return TRUE if `s' is a valid DOS/Win32 handle.

Used to differentiate EBADF from ENOTSOCK.

Note: for non-djgpp targets `s' may have same value as a DOS-handle. This function should only be used when `s' isn't found in `sk_list'.

Definition at line 222 of file socket.c.

int _sock_half_open ( const _tcp_Socket s)

_sock_half_open - Return true if peer closed his side.

There might still be data to read

Definition at line 2239 of file socket.c.

References tcp_Socket::state.

int _sock_set_rcv_buf ( sock_type s,
size_t  len 
)

Setup a bigger receive buffer, the default in Wattcp is only 2k.

Note
If calloc() fails, sock_setbuf() reverts to default 2kB socket buffer.
Todo:
allow user to define size using SO_RCVBUF/SO_SNDBUF before calling connect().

Definition at line 252 of file socket.c.

Socket* _socklist_find ( int  s)

Returns a pointer to the Socket structure associated with socket 's'.

If socket `s' was not found, NULL is returned.

Definition at line 1534 of file socket.c.

References init_sockets().

int _TCP6_listen ( Socket socket,
const void host,
WORD  loc_port 
)
Todo:
Support TCP listen() for AF_INET6

Definition at line 2202 of file socket.c.

int _TCP6_open ( Socket socket,
const void dst,
WORD  loc_port,
WORD  rem_port 
)
int _TCP_open ( Socket socket,
struct in_addr  host,
WORD  loc_port,
WORD  rem_port 
)

Open and listen routines for SOCK_STREAM at the socket-level.

The parameters to tcp_open() is a bit tricky, but the internal Wattcp socket 's' contains the following elements that must match in the first 'for-loop' of _tcp_handler().

s->hisport != 0 i.e. active (non-listening) port s->myaddr == ip->destination, our IP-address s->hisaddr == ip->source, above 'dest' address s->myport == tcp->dstPort, above 'loc_port' s->hisport == tcp->srcPort, above 'rem_port'

Definition at line 2067 of file socket.c.

References _sock_set_rcv_buf(), tcp_open(), and tcp_recv_win.

int _UDP6_listen ( Socket socket,
const void host,
WORD  port 
)
Todo:
Support UDP listen() for AF_INET6

Definition at line 2222 of file socket.c.

int _UDP6_open ( Socket socket,
const void host,
WORD  loc_port,
WORD  rem_port 
)
Todo:
Support UDP connect() for AF_INET6

Definition at line 2211 of file socket.c.

References _sock_set_rcv_buf().

static void check_errno_version ( void  )
static

Check that .

/util/xx_err.exe is built with same version we're using here. Print a warning if there's a mismatch.

Definition at line 1457 of file socket.c.

References vendor_version().

static void do_keepalive ( Socket sock)
static

Handle keepalive on STREAM socket.

Not complete; doesn't consider tcp_max_idle.

Definition at line 1063 of file socket.c.

References _tcp_keepalive(), chk_timeout(), set_timeout(), and tcp_keep_idle.

static BOOL init_sockets ( void  )
static

Main initialisation routine for the BSD socket API.

Called only once to do:

  • Calls watt_sock_init() w/o exiting if it fails.
  • Checks the generated errno list.
  • Set the limit on max number of sockets.
  • Add our sock_daemon().
  • Initialise Fortify malloc debugger.

Definition at line 1483 of file socket.c.

References _watt_do_exit, check_errno_version(), sock_daemon(), sock_fortify_exit(), and watt_sock_init().

static __inline BOOL other_raw_socks ( const Socket _this)
static

Traverse socket-list to find other SOCK_RAW sockets besides '_this'.

Unhook '_raw_ip?_hook' if none found.

Definition at line 352 of file socket.c.

static int set_proto ( int  type,
BOOL  is_ip6,
int *  proto 
)
static

Select (and check) a suitable protocol for socket-type.

<

Todo:
IPPROTO_XTP

Definition at line 1685 of file socket.c.

References _pkt_errno, pkt_strerror(), and sock_raw4_recv().

static __inline Socket* sk_list_del ( int  s)
static

Deletes the list element associated with a socket.

Return pointer to next node. Return NULL if no next or socket not found.

Definition at line 284 of file socket.c.

static void sock_daemon ( void  )
static

A daemon called from tcp_tick().

Called every DAEMON_PERIOD (500 msec) to perform cleanup of bound SOCK_STREAM and SOCK_DGRAM sockets still in use.

Definition at line 1256 of file socket.c.

References tcp_sock_daemon(), and udp_sock_daemon().

static int sock_find_fd ( const Socket socket)
static

Finds the 'fd' associated with pointer 'socket'.

Return -1 if not found.

Definition at line 798 of file socket.c.

static Socket* sock_find_udp ( const _udp_Socket udp)
static

Finds the 'Socket' associated with udp-socket 'udp'.

Return NULL if not found.

Definition at line 812 of file socket.c.

static void sock_fortify_exit ( void  )
static

Exit handler for Fortify.

Traverse all sockets and print some state and memory statistics.

Definition at line 1326 of file socket.c.

References tcp_Socket::state.

unsigned sock_packet_receive ( Socket sock,
void buf,
unsigned  len,
struct sockaddr from,
size_t *  fromlen 
)

Called from receive.c for AF_PACKET sockets.

Todo:
This should loop until some packet is received.
Todo:
should check fromlen is large enough

Definition at line 601 of file socket.c.

References _eth_get_hwtype(), and _pkt_ip_ofs.

static int sock_raw4_recv ( const in_Header ip)
static

Called from _ip4_handler() via `_bsd_socket_hook'.

IP-header is already checked in `_ip4_handler()'. Finds all 'Socket' associated with raw IP-packet 'ip'. Enqueue to 'sock->raw_sock'. Return >=1 if 'ip' is consumed, 0 otherwise.

Todo:
Handle receiving bad IP-packets for SOCK_RAW sockets.

Definition at line 925 of file socket.c.

References find_free_raw_sock(), _raw_Socket::rx_data, _raw_Socket::seq_num, and _raw_Socket::used.

int W32_CALL socket ( int  family,
int  type,
int  protocol 
)

socket().

  • family The protocol family. Supports the AF_INET, AF_INET6, AF_PACKET families.
  • type SOCK_STREAM (tcp), SOCK_DGRAM (udp), SOCK_RAW (ip) or SOCK_PACKET (link).
  • protocol IPPROTO_TCP, IPPROTO_UDP or 0/IPPROTO_IP.
Return values
Thesocket ID number

Definition at line 1794 of file socket.c.

References _bsd_socket_hook(), init_sockets(), set_proto(), socket_op_demux(), and socklist_add().

int W32_CALL socketpair ( int  family,
int  type,
int  protocol,
int  usockvec[2] 
)

socketpair() - Create a pair of connected sockets.

Modified version based on Linux's version.

Definition at line 2254 of file socket.c.

static int stream_cancel ( const _tcp_Socket tcp,
BYTE  icmp_type,
int  error 
)
static

Callback handlers for "ICMP Port/Host/Network Unreachable" or "ICMP Parameter Problem" issued by lower layer (udp_cancel() and tcp_cancel() in pctcp.c)

Note
a single ICMP message may apply to several UDP sockets.

We should drop (i.e. set ECONNREFUSED in lack of a better errno) on receiving "ICMP-param problem" or "ICMP-unreachable" reached stress-level.

Definition at line 1912 of file socket.c.

References socket().

static Socket* tcp_sock_daemon ( Socket sock)
static

Called by sock_daemon() to handle SOCK_STREAM sockets.

Unlink the socket from the linked list if application has read all data and tcp_state has become CLOSED and the linger period has expired.

Definition at line 1169 of file socket.c.

References _sock_free_rcv_buf(), do_keepalive(), sock_fastread(), and tcp_Socket::state.

static Socket* udp_sock_daemon ( Socket sock)
static

Called by sock_daemon() for SOCK_DGRAM sockets.

Unlink the socket from the linked list if application has read all data and "state" is disconnecting.

Note
Setting 'SS_ISDISCONNECTING' is really a mis-nomer, but should indicate socket is closed/aborted with Rx-data remaining.

Definition at line 1236 of file socket.c.

Variable Documentation

fd_set inuse[NUM_SOCK_FDSETS]
static

`inuse[]' has a non-zero bit for each socket-descriptor in use.

There can be max `MAX_SOCKETS' allocated at any time. Dead stream sockets will be unlinked by `sock_daemon()' in due time.

Non-djgpp targets: Allocate a new descriptor (handle) by searching through `inuse' for the first zero bit. Update `sk_last' as needed.

djgpp target: Allocate a descriptior from the "File System Extension" layer. `sk_last' is not used (initialised to MAX_SOCKETS). Thus max # of sockets are limited by max # of DOS handles that can be created.

Definition at line 148 of file socket.c.