socket.h revision 571c90a734400801da973f986190fac9fc5efd0d
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync/*
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync * Copyright (c) 1995 Danny Gasparovski.
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync *
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync * Please read the file COPYRIGHT for the
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync * terms and conditions of the copyright.
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync */
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync/* MINE */
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync#ifndef _SLIRP_SOCKET_H_
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync#define _SLIRP_SOCKET_H_
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync#ifdef VBOX_WITH_SLIRP_MT
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync#include <iprt/critsect.h>
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync#endif
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync#define SO_EXPIRE 240000
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync#define SO_EXPIREFAST 10000
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync/*
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync * Our socket structure
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync */
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsyncstruct socket
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync{
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync struct socket *so_next;
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync struct socket *so_prev; /* For a linked list of sockets */
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync#if !defined(RT_OS_WINDOWS)
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync int s; /* The actual socket */
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync#else
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync union {
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync int s;
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync HANDLE sh;
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync };
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync uint64_t so_icmp_id; /* XXX: hack */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync uint64_t so_icmp_seq; /* XXX: hack */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync#endif
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync /* XXX union these with not-yet-used sbuf params */
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync struct mbuf *so_m; /* Pointer to the original SYN packet,
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync * for non-blocking connect()'s, and
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync * PING reply's */
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync struct tcpiphdr *so_ti; /* Pointer to the original ti within
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync * so_mconn, for non-blocking connections */
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync int so_urgc;
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync struct in_addr so_faddr; /* foreign host table entry */
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync struct in_addr so_laddr; /* local host table entry */
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync u_int16_t so_fport; /* foreign port */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync u_int16_t so_lport; /* local port */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync u_int16_t so_hlport; /* host local port */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync struct in_addr so_hladdr; /* local host addr */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync u_int8_t so_iptos; /* Type of service */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync u_int8_t so_emu; /* Is the socket emulated? */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync u_char so_type; /* Type of socket, UDP or TCP */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync int so_state; /* internal state flags SS_*, below */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync struct tcpcb *so_tcpcb; /* pointer to TCP protocol control block */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync u_int so_expire; /* When the socket will expire */
8867771015571c5542d39e393d7fe6304421a928vboxsync
8867771015571c5542d39e393d7fe6304421a928vboxsync int so_queued; /* Number of packets queued from this socket */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync int so_nqueued; /* Number of packets queued in a row
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync * Used to determine when to "downgrade" a session
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync * from fastq to batchq */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync struct sbuf so_rcv; /* Receive buffer */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync struct sbuf so_snd; /* Send buffer */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync#ifdef VBOX_WITH_SLIRP_MT
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync RTCRITSECT so_mutex;
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync int so_deleted;
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync#endif
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync#ifndef RT_OS_WINDOWS
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync int so_poll_index;
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync#else /* !RT_OS_WINDOWS */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync /*
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync * FD_CLOSE event has been occurred on socket
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync int so_close;
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync#endif /* RT_OS_WINDOWS */
8867771015571c5542d39e393d7fe6304421a928vboxsync
8867771015571c5542d39e393d7fe6304421a928vboxsync void (* so_timeout)(PNATState pData, struct socket *so, void *arg);
8867771015571c5542d39e393d7fe6304421a928vboxsync void *so_timeout_arg;
8867771015571c5542d39e393d7fe6304421a928vboxsync
8867771015571c5542d39e393d7fe6304421a928vboxsync#ifdef VBOX_WITH_NAT_SERVICE
8867771015571c5542d39e393d7fe6304421a928vboxsync /* storage of source ether address */
8867771015571c5542d39e393d7fe6304421a928vboxsync unsigned char so_ethaddr[6];
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync#endif
8867771015571c5542d39e393d7fe6304421a928vboxsync#ifdef VBOX_WITH_SLIRP_ALIAS
8867771015571c5542d39e393d7fe6304421a928vboxsync /* required for port-forwarding */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync struct libalias *so_la;
8867771015571c5542d39e393d7fe6304421a928vboxsync#endif
8867771015571c5542d39e393d7fe6304421a928vboxsync};
8867771015571c5542d39e393d7fe6304421a928vboxsync
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync#ifdef VBOX_WITH_SLIRP_MT
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync# define SOCKET_LOCK(so) \
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync do { \
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync int rc; \
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync /* Assert(strcmp(RTThreadSelfName(), "EMT") != 0); */ \
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync Log2(("lock:%s:%d L on %R[natsock]\n", __FUNCTION__, __LINE__, (so))); \
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync Assert(!RTCritSectIsOwner(&(so)->so_mutex)); \
8867771015571c5542d39e393d7fe6304421a928vboxsync rc = RTCritSectEnter(&(so)->so_mutex); \
8867771015571c5542d39e393d7fe6304421a928vboxsync AssertReleaseRC(rc); \
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync } while (0)
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync# define SOCKET_UNLOCK(so) \
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync do { \
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync int rc; \
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync if ((so) != NULL) Log2(("lock:%s:%d U on %R[natsock]\n", __FUNCTION__, __LINE__, (so))); \
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync rc = RTCritSectLeave(&(so)->so_mutex); \
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync AssertReleaseRC(rc); \
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync } while (0)
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync# define SOCKET_LOCK_CREATE(so) \
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync do { \
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync int rc; \
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync rc = RTCritSectInit(&(so)->so_mutex); \
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync AssertReleaseRC(rc); \
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync } while (0)
8867771015571c5542d39e393d7fe6304421a928vboxsync# define SOCKET_LOCK_DESTROY(so) \
8867771015571c5542d39e393d7fe6304421a928vboxsync do { \
8867771015571c5542d39e393d7fe6304421a928vboxsync int rc = RTCritSectDelete(&(so)->so_mutex); \
8867771015571c5542d39e393d7fe6304421a928vboxsync AssertReleaseRC(rc); \
8867771015571c5542d39e393d7fe6304421a928vboxsync } while (0)
8867771015571c5542d39e393d7fe6304421a928vboxsync#else
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync# define SOCKET_LOCK(so) do {} while (0)
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync# define SOCKET_UNLOCK(so) do {} while (0)
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync# define SOCKET_LOCK_CREATE(so) do {} while (0)
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync# define SOCKET_LOCK_DESTROY(so) do {} while (0)
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync#endif
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync/*
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync * Socket state bits. (peer means the host on the Internet,
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync * local host means the host on the other end of the modem)
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync */
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync#define SS_NOFDREF 0x001 /* No fd reference */
8867771015571c5542d39e393d7fe6304421a928vboxsync
8867771015571c5542d39e393d7fe6304421a928vboxsync#define SS_ISFCONNECTING 0x002 /* Socket is connecting to peer (non-blocking connect()'s) */
8867771015571c5542d39e393d7fe6304421a928vboxsync#define SS_ISFCONNECTED 0x004 /* Socket is connected to peer */
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync#define SS_FCANTRCVMORE 0x008 /* Socket can't receive more from peer (for half-closes) */
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync#define SS_FCANTSENDMORE 0x010 /* Socket can't send more to peer (for half-closes) */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync/* #define SS_ISFDISCONNECTED 0x020*/ /* Socket has disconnected from peer, in 2MSL state */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync#define SS_FWDRAIN 0x040 /* We received a FIN, drain data and set SS_FCANTSENDMORE */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync/* #define SS_CTL 0x080 */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync#define SS_FACCEPTCONN 0x100 /* Socket is accepting connections from a host on the internet */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync#define SS_FACCEPTONCE 0x200 /* If set, the SS_FACCEPTCONN socket will die after one accept */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsyncextern struct socket tcb;
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync
8867771015571c5542d39e393d7fe6304421a928vboxsync#if defined(DECLARE_IOVEC) && !defined(HAVE_READV)
8867771015571c5542d39e393d7fe6304421a928vboxsyncstruct iovec
8867771015571c5542d39e393d7fe6304421a928vboxsync{
8867771015571c5542d39e393d7fe6304421a928vboxsync char *iov_base;
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync size_t iov_len;
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync};
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync#endif
8867771015571c5542d39e393d7fe6304421a928vboxsync
8867771015571c5542d39e393d7fe6304421a928vboxsyncvoid so_init (void);
8867771015571c5542d39e393d7fe6304421a928vboxsyncstruct socket * solookup (struct socket *, struct in_addr, u_int, struct in_addr, u_int);
8867771015571c5542d39e393d7fe6304421a928vboxsyncstruct socket * socreate (void);
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsyncvoid sofree (PNATState, struct socket *);
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync#ifdef VBOX_WITH_SLIRP_MT
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsyncvoid soread_queue (PNATState, struct socket *, int *);
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync#endif
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsyncint soread (PNATState, struct socket *);
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsyncvoid sorecvoob (PNATState, struct socket *);
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsyncint sosendoob (struct socket *);
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsyncint sowrite (PNATState, struct socket *);
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsyncvoid sorecvfrom (PNATState, struct socket *);
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsyncint sosendto (PNATState, struct socket *, struct mbuf *);
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsyncstruct socket * solisten (PNATState, u_int, u_int32_t, u_int, int);
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsyncvoid sorwakeup (struct socket *);
8867771015571c5542d39e393d7fe6304421a928vboxsyncvoid sowwakeup (struct socket *);
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsyncvoid soisfconnecting (register struct socket *);
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsyncvoid soisfconnected (register struct socket *);
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsyncvoid sofcantrcvmore (struct socket *);
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsyncvoid sofcantsendmore (struct socket *);
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsyncvoid soisfdisconnected (struct socket *);
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsyncvoid sofwdrain (struct socket *);
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync#endif /* _SOCKET_H_ */
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync