#ifndef _SYS_SOCKETVAR_H_
#define _SYS_SOCKETVAR_H_
#include <sys/event.h>
#include <sys/queue.h>
#include <sys/sigio.h>
#include <sys/task.h>
#include <sys/timeout.h>
#include <sys/mutex.h>
#include <sys/rwlock.h>
#include <sys/refcnt.h>
#ifndef _SOCKLEN_T_DEFINED_
#define _SOCKLEN_T_DEFINED_
typedef __socklen_t socklen_t;
#endif
TAILQ_HEAD(soqhead, socket);
struct sosplice {
struct socket *ssp_socket;
struct socket *ssp_soback;
off_t ssp_len;
off_t ssp_max;
struct timeval ssp_idletv;
struct timeout ssp_idleto;
struct task ssp_task;
struct taskq *ssp_queue;
};
struct sockbuf {
struct rwlock sb_lock;
struct mutex sb_mtx;
#define sb_startzero sb_cc
u_long sb_cc;
u_long sb_datacc;
u_long sb_hiwat;
u_long sb_wat;
u_long sb_mbcnt;
u_long sb_mbmax;
long sb_lowat;
struct mbuf *sb_mb;
struct mbuf *sb_mbtail;
struct mbuf *sb_lastrecord;
short sb_flags;
#define sb_endzero sb_flags
short sb_state;
uint64_t sb_timeo_nsecs;
struct klist sb_klist;
};
#define SB_MAX (2*1024*1024)
#define SB_WAIT 0x0001
#define SB_ASYNC 0x0002
#define SB_SPLICE 0x0004
#define SB_NOINTR 0x0008
struct socket {
const struct protosw *so_proto;
struct rwlock so_lock;
struct refcnt so_refcnt;
void *so_pcb;
u_int so_state;
short so_type;
short so_options;
short so_linger;
struct socket *so_head;
struct soqhead *so_onq;
struct soqhead so_q0;
struct soqhead so_q;
struct sigio_ref so_sigio;
TAILQ_ENTRY(socket) so_qe;
short so_q0len;
short so_qlen;
short so_qlimit;
short so_timeo;
u_long so_oobmark;
u_int so_error;
struct sosplice *so_sp;
struct sockbuf so_rcv;
struct sockbuf so_snd;
void (*so_upcall)(struct socket *, caddr_t, int);
caddr_t so_upcallarg;
uid_t so_euid;
uid_t so_ruid;
gid_t so_egid;
gid_t so_rgid;
pid_t so_cpid;
};
#define SS_NOFDREF 0x001
#define SS_ISCONNECTED 0x002
#define SS_ISCONNECTING 0x004
#define SS_ISDISCONNECTING 0x008
#define SS_CANTSENDMORE 0x010
#define SS_CANTRCVMORE 0x020
#define SS_RCVATMARK 0x040
#define SS_ISDISCONNECTED 0x800
#define SS_PRIV 0x080
#define SS_CONNECTOUT 0x1000
#define SS_ISSENDING 0x2000
#define SS_DNS 0x4000
#define SS_YP 0x8000
#ifdef _KERNEL
#include <sys/protosw.h>
#include <lib/libkern/libkern.h>
struct mbuf;
struct sockaddr;
struct proc;
struct msghdr;
struct stat;
struct knote;
void soassertlocked(struct socket *);
void soassertlocked_readonly(struct socket *);
void sbmtxassertlocked(struct sockbuf *);
int soo_read(struct file *, struct uio *, int);
int soo_write(struct file *, struct uio *, int);
int soo_ioctl(struct file *, u_long, caddr_t, struct proc *);
int soo_kqfilter(struct file *, struct knote *);
int soo_close(struct file *, struct proc *);
int soo_stat(struct file *, struct stat *, struct proc *);
void sbappend(struct sockbuf *, struct mbuf *);
void sbappendstream(struct sockbuf *, struct mbuf *);
int sbappendaddr(struct sockbuf *, const struct sockaddr *, struct mbuf *,
struct mbuf *);
int sbappendcontrol(struct sockbuf *, struct mbuf *, struct mbuf *);
void sbappendrecord(struct sockbuf *, struct mbuf *);
void sbcompress(struct sockbuf *, struct mbuf *, struct mbuf *);
struct mbuf *
sbcreatecontrol(const void *, size_t, int, int);
void sbdrop(struct sockbuf *, int);
void sbdroprecord(struct sockbuf *);
void sbflush(struct sockbuf *);
void sbrelease(struct sockbuf *);
int sbcheckreserve(u_long, u_long);
int sbchecklowmem(void);
int sbreserve(struct sockbuf *, u_long);
int sbwait(struct sockbuf *);
void soinit(void);
void soabort(struct socket *);
int soaccept(struct socket *, struct mbuf *);
int sobind(struct socket *, struct mbuf *, struct proc *);
void socantrcvmore(struct socket *);
void socantsendmore(struct socket *);
int soclose(struct socket *, int);
int soconnect(struct socket *, struct mbuf *);
int soconnect2(struct socket *, struct socket *);
int socreate(int, struct socket **, int, int);
int sodisconnect(struct socket *);
struct socket *soalloc(const struct protosw *, int);
void sofree(struct socket *, int);
void sorele(struct socket *);
int sogetopt(struct socket *, int, int, struct mbuf *);
void sohasoutofband(struct socket *);
void soisconnected(struct socket *);
void soisconnecting(struct socket *);
void soisdisconnected(struct socket *);
void soisdisconnecting(struct socket *);
int solisten(struct socket *, int);
struct socket *sonewconn(struct socket *, int, int);
void soqinsque(struct socket *, struct socket *, int);
int soqremque(struct socket *, int);
int soreceive(struct socket *, struct mbuf **, struct uio *,
struct mbuf **, struct mbuf **, int *, socklen_t);
int soreserve(struct socket *, u_long, u_long);
int sosend(struct socket *, struct mbuf *, struct uio *,
struct mbuf *, struct mbuf *, int);
int sosetopt(struct socket *, int, int, struct mbuf *);
int soshutdown(struct socket *, int);
void sowakeup(struct socket *, struct sockbuf *);
void sorwakeup(struct socket *);
void sowwakeup(struct socket *);
int sockargs(struct mbuf **, const void *, size_t, int);
int sosleep_nsec(struct socket *, void *, int, const char *, uint64_t);
void solock(struct socket *);
void solock_shared(struct socket *);
void solock_nonet(struct socket *);
int solock_persocket(struct socket *);
void solock_pair(struct socket *, struct socket *);
void sounlock(struct socket *);
void sounlock_shared(struct socket *);
void sounlock_nonet(struct socket *);
void sounlock_pair(struct socket *, struct socket *);
int sendit(struct proc *, int, struct msghdr *, int, register_t *);
int recvit(struct proc *, int, struct msghdr *, caddr_t, register_t *);
int doaccept(struct proc *, int, struct sockaddr *, socklen_t *, int,
register_t *);
#ifdef SOCKBUF_DEBUG
void sblastrecordchk(struct sockbuf *, const char *);
#define SBLASTRECORDCHK(sb, where) sblastrecordchk((sb), (where))
void sblastmbufchk(struct sockbuf *, const char *);
#define SBLASTMBUFCHK(sb, where) sblastmbufchk((sb), (where))
void sbcheck(struct socket *, struct sockbuf *);
#define SBCHECK(so, sb) sbcheck((so), (sb))
#else
#define SBLASTRECORDCHK(sb, where)
#define SBLASTMBUFCHK(sb, where)
#define SBCHECK(so, sb)
#endif
#define SBL_WAIT 0x01
#define SBL_NOINTR 0x02
int sblock(struct sockbuf *, int);
void sbunlock(struct sockbuf *);
extern u_long sb_max;
extern struct pool socket_pool;
static inline struct socket *
soref(struct socket *so)
{
if (so == NULL)
return NULL;
refcnt_take(&so->so_refcnt);
return so;
}
#define isspliced(so) ((so)->so_sp && (so)->so_sp->ssp_socket)
#define issplicedback(so) ((so)->so_sp && (so)->so_sp->ssp_soback)
static inline int
sb_notify(struct sockbuf *sb)
{
int rv;
mtx_enter(&sb->sb_mtx);
rv = ((sb->sb_flags & (SB_WAIT|SB_ASYNC|SB_SPLICE)) != 0 ||
!klist_empty(&sb->sb_klist));
mtx_leave(&sb->sb_mtx);
return rv;
}
static inline long
sbspace_locked(struct sockbuf *sb)
{
sbmtxassertlocked(sb);
return lmin(sb->sb_hiwat - sb->sb_cc, sb->sb_mbmax - sb->sb_mbcnt);
}
static inline long
sbspace(struct sockbuf *sb)
{
long ret;
mtx_enter(&sb->sb_mtx);
ret = sbspace_locked(sb);
mtx_leave(&sb->sb_mtx);
return ret;
}
#define sosendallatonce(so) \
((so)->so_proto->pr_flags & PR_ATOMIC)
#define soissending(so) \
((so)->so_snd.sb_state & SS_ISSENDING)
static inline int
soreadable(struct socket *so)
{
soassertlocked_readonly(so);
if (isspliced(so))
return 0;
return (so->so_rcv.sb_state & SS_CANTRCVMORE) ||
so->so_error || so->so_rcv.sb_cc >= so->so_rcv.sb_lowat;
}
static inline int
sowriteable(struct socket *so)
{
soassertlocked_readonly(so);
return ((sbspace(&so->so_snd) >= so->so_snd.sb_lowat &&
((so->so_state & SS_ISCONNECTED) ||
(so->so_proto->pr_flags & PR_CONNREQUIRED)==0)) ||
(so->so_snd.sb_state & SS_CANTSENDMORE) || so->so_error);
}
static inline void
sballoc(struct sockbuf *sb, struct mbuf *m)
{
sb->sb_cc += m->m_len;
if (m->m_type != MT_CONTROL && m->m_type != MT_SONAME)
sb->sb_datacc += m->m_len;
sb->sb_mbcnt += MSIZE;
if (m->m_flags & M_EXT)
sb->sb_mbcnt += m->m_ext.ext_size;
}
static inline void
sbfree(struct sockbuf *sb, struct mbuf *m)
{
sb->sb_cc -= m->m_len;
if (m->m_type != MT_CONTROL && m->m_type != MT_SONAME)
sb->sb_datacc -= m->m_len;
sb->sb_mbcnt -= MSIZE;
if (m->m_flags & M_EXT)
sb->sb_mbcnt -= m->m_ext.ext_size;
}
static inline void
sbassertlocked(struct sockbuf *sb)
{
rw_assert_wrlock(&sb->sb_lock);
}
#define SB_EMPTY_FIXUP(sb) do { \
if ((sb)->sb_mb == NULL) { \
(sb)->sb_mbtail = NULL; \
(sb)->sb_lastrecord = NULL; \
} \
} while (0)
#endif
#endif