LCOV - code coverage report
Current view: top level - sys - socketvar.h (source / functions) Hit Total Coverage
Test: 6.4 Lines: 0 15 0.0 %
Date: 2018-10-19 03:25:38 Functions: 0 3 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*      $OpenBSD: socketvar.h,v 1.87 2018/08/20 16:00:22 mpi Exp $      */
       2             : /*      $NetBSD: socketvar.h,v 1.18 1996/02/09 18:25:38 christos Exp $  */
       3             : 
       4             : /*-
       5             :  * Copyright (c) 1982, 1986, 1990, 1993
       6             :  *      The Regents of the University of California.  All rights reserved.
       7             :  *
       8             :  * Redistribution and use in source and binary forms, with or without
       9             :  * modification, are permitted provided that the following conditions
      10             :  * are met:
      11             :  * 1. Redistributions of source code must retain the above copyright
      12             :  *    notice, this list of conditions and the following disclaimer.
      13             :  * 2. Redistributions in binary form must reproduce the above copyright
      14             :  *    notice, this list of conditions and the following disclaimer in the
      15             :  *    documentation and/or other materials provided with the distribution.
      16             :  * 3. Neither the name of the University nor the names of its contributors
      17             :  *    may be used to endorse or promote products derived from this software
      18             :  *    without specific prior written permission.
      19             :  *
      20             :  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
      21             :  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      22             :  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
      23             :  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
      24             :  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
      25             :  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
      26             :  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
      27             :  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
      28             :  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
      29             :  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
      30             :  * SUCH DAMAGE.
      31             :  *
      32             :  *      @(#)socketvar.h 8.1 (Berkeley) 6/2/93
      33             :  */
      34             : 
      35             : #include <sys/selinfo.h>                  /* for struct selinfo */
      36             : #include <sys/queue.h>
      37             : #include <sys/task.h>
      38             : #include <sys/timeout.h>
      39             : 
      40             : #ifndef _SOCKLEN_T_DEFINED_
      41             : #define _SOCKLEN_T_DEFINED_
      42             : typedef __socklen_t     socklen_t;      /* length type for network syscalls */
      43             : #endif
      44             : 
      45             : TAILQ_HEAD(soqhead, socket);
      46             : 
      47             : /*
      48             :  * Kernel structure per socket.
      49             :  * Contains send and receive buffer queues,
      50             :  * handle on protocol and pointer to protocol
      51             :  * private data and error information.
      52             :  */
      53             : struct socket {
      54             :         const struct protosw *so_proto; /* protocol handle */
      55             :         void    *so_pcb;                /* protocol control block */
      56             :         u_int   so_state;               /* internal state flags SS_*, below */
      57             :         short   so_type;                /* generic type, see socket.h */
      58             :         short   so_options;             /* from socket call, see socket.h */
      59             :         short   so_linger;              /* time to linger while closing */
      60             : /*
      61             :  * Variables for connection queueing.
      62             :  * Socket where accepts occur is so_head in all subsidiary sockets.
      63             :  * If so_head is 0, socket is not related to an accept.
      64             :  * For head socket so_q0 queues partially completed connections,
      65             :  * while so_q is a queue of connections ready to be accepted.
      66             :  * If a connection is aborted and it has so_head set, then
      67             :  * it has to be pulled out of either so_q0 or so_q.
      68             :  * We allow connections to queue up based on current queue lengths
      69             :  * and limit on number of queued connections for this socket.
      70             :  */
      71             :         struct  socket  *so_head;       /* back pointer to accept socket */
      72             :         struct  soqhead *so_onq;        /* queue (q or q0) that we're on */
      73             :         struct  soqhead so_q0;          /* queue of partial connections */
      74             :         struct  soqhead so_q;           /* queue of incoming connections */
      75             :         TAILQ_ENTRY(socket) so_qe;      /* our queue entry (q or q0) */
      76             :         short   so_q0len;               /* partials on so_q0 */
      77             :         short   so_qlen;                /* number of connections on so_q */
      78             :         short   so_qlimit;              /* max number queued connections */
      79             :         short   so_timeo;               /* connection timeout */
      80             :         u_int   so_error;               /* error affecting connection */
      81             :         pid_t   so_pgid;                /* pgid for signals */
      82             :         uid_t   so_siguid;              /* uid of process who set so_pgid */
      83             :         uid_t   so_sigeuid;             /* euid of process who set so_pgid */
      84             :         u_long  so_oobmark;             /* chars to oob mark */
      85             : /*
      86             :  * Variables for socket splicing, allocated only when needed.
      87             :  */
      88             :         struct sosplice {
      89             :                 struct  socket *ssp_socket;     /* send data to drain socket */
      90             :                 struct  socket *ssp_soback;     /* back ref to source socket */
      91             :                 off_t   ssp_len;                /* number of bytes spliced */
      92             :                 off_t   ssp_max;                /* maximum number of bytes */
      93             :                 struct  timeval ssp_idletv;     /* idle timeout */
      94             :                 struct  timeout ssp_idleto;
      95             :                 struct  task ssp_task;          /* task for somove */
      96             :         } *so_sp;
      97             : /*
      98             :  * Variables for socket buffering.
      99             :  */
     100             :         struct  sockbuf {
     101             : /* The following fields are all zeroed on flush. */
     102             : #define sb_startzero    sb_cc
     103             :                 u_long  sb_cc;          /* actual chars in buffer */
     104             :                 u_long  sb_datacc;      /* data only chars in buffer */
     105             :                 u_long  sb_hiwat;       /* max actual char count */
     106             :                 u_long  sb_wat;         /* default watermark */
     107             :                 u_long  sb_mbcnt;       /* chars of mbufs used */
     108             :                 u_long  sb_mbmax;       /* max chars of mbufs to use */
     109             :                 long    sb_lowat;       /* low water mark */
     110             :                 struct mbuf *sb_mb;     /* the mbuf chain */
     111             :                 struct mbuf *sb_mbtail; /* the last mbuf in the chain */
     112             :                 struct mbuf *sb_lastrecord;/* first mbuf of last record in
     113             :                                               socket buffer */
     114             :                 u_short sb_timeo;       /* timeout for read/write */
     115             :                 short   sb_flags;       /* flags, see below */
     116             : /* End area that is zeroed on flush. */
     117             : #define sb_endzero      sb_flags
     118             :                 int     sb_flagsintr;   /* flags, changed atomically */
     119             :                 struct  selinfo sb_sel; /* process selecting read/write */
     120             :         } so_rcv, so_snd;
     121             : #define SB_MAX          (2*1024*1024)   /* default for max chars in sockbuf */
     122             : #define SB_LOCK         0x01            /* lock on data queue */
     123             : #define SB_WANT         0x02            /* someone is waiting to lock */
     124             : #define SB_WAIT         0x04            /* someone is waiting for data/space */
     125             : #define SB_SEL          0x08            /* someone is selecting */
     126             : #define SB_ASYNC        0x10            /* ASYNC I/O, need signals */
     127             : #define SB_SPLICE       0x20            /* buffer is splice source or drain */
     128             : #define SB_NOINTR       0x40            /* operations not interruptible */
     129             : #define SB_KNOTE        0x80            /* kernel note attached */
     130             : 
     131             :         void    (*so_upcall)(struct socket *so, caddr_t arg, int waitf);
     132             :         caddr_t so_upcallarg;           /* Arg for above */
     133             :         uid_t   so_euid, so_ruid;       /* who opened the socket */
     134             :         gid_t   so_egid, so_rgid;
     135             :         pid_t   so_cpid;                /* pid of process that opened socket */
     136             : };
     137             : 
     138             : /*
     139             :  * Socket state bits.
     140             :  */
     141             : #define SS_NOFDREF              0x001   /* no file table ref any more */
     142             : #define SS_ISCONNECTED          0x002   /* socket connected to a peer */
     143             : #define SS_ISCONNECTING         0x004   /* in process of connecting to peer */
     144             : #define SS_ISDISCONNECTING      0x008   /* in process of disconnecting */
     145             : #define SS_CANTSENDMORE         0x010   /* can't send more data to peer */
     146             : #define SS_CANTRCVMORE          0x020   /* can't receive more data from peer */
     147             : #define SS_RCVATMARK            0x040   /* at mark on input */
     148             : #define SS_ISDISCONNECTED       0x800   /* socket disconnected from peer */
     149             : 
     150             : #define SS_PRIV                 0x080   /* privileged for broadcast, raw... */
     151             : #define SS_ASYNC                0x200   /* async i/o notify */
     152             : #define SS_CONNECTOUT           0x1000  /* connect, not accept, at this end */
     153             : #define SS_ISSENDING            0x2000  /* hint for lower layer */
     154             : #define SS_DNS                  0x4000  /* created using SOCK_DNS socket(2) */
     155             : 
     156             : #ifdef _KERNEL
     157             : 
     158             : #include <lib/libkern/libkern.h>
     159             : 
     160             : /*
     161             :  * Values for sounlock()/sofree().
     162             :  */
     163             : #define SL_NOUNLOCK     0x00
     164             : #define SL_LOCKED       0x42
     165             : 
     166             : void    soassertlocked(struct socket *);
     167             : 
     168             : /*
     169             :  * Macros for sockets and socket buffering.
     170             :  */
     171             : 
     172             : #define isspliced(so)           ((so)->so_sp && (so)->so_sp->ssp_socket)
     173             : #define issplicedback(so)       ((so)->so_sp && (so)->so_sp->ssp_soback)
     174             : 
     175             : /*
     176             :  * Do we need to notify the other side when I/O is possible?
     177             :  */
     178             : static inline int
     179           0 : sb_notify(struct socket *so, struct sockbuf *sb)
     180             : {
     181           0 :         int flags = (sb->sb_flags | sb->sb_flagsintr);
     182             : 
     183           0 :         KASSERT(sb == &so->so_rcv || sb == &so->so_snd);
     184           0 :         soassertlocked(so);
     185           0 :         return ((flags & (SB_WAIT|SB_SEL|SB_ASYNC|SB_SPLICE|SB_KNOTE)) != 0);
     186             : }
     187             : 
     188             : /*
     189             :  * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
     190             :  * This is problematical if the fields are unsigned, as the space might
     191             :  * still be negative (cc > hiwat or mbcnt > mbmax).  Should detect
     192             :  * overflow and return 0.
     193             :  */
     194             : static inline long
     195           0 : sbspace(struct socket *so, struct sockbuf *sb)
     196             : {
     197           0 :         KASSERT(sb == &so->so_rcv || sb == &so->so_snd);
     198           0 :         return lmin(sb->sb_hiwat - sb->sb_cc, sb->sb_mbmax - sb->sb_mbcnt);
     199             : }
     200             : 
     201             : /* do we have to send all at once on a socket? */
     202             : #define sosendallatonce(so) \
     203             :     ((so)->so_proto->pr_flags & PR_ATOMIC)
     204             : 
     205             : /* are we sending on this socket? */
     206             : #define soissending(so) \
     207             :     ((so)->so_state & SS_ISSENDING)
     208             : 
     209             : /* can we read something from so? */
     210             : static inline int
     211           0 : soreadable(struct socket *so)
     212             : {
     213           0 :         soassertlocked(so);
     214           0 :         if (isspliced(so))
     215           0 :                 return 0;
     216           0 :         return (so->so_state & SS_CANTRCVMORE) || so->so_qlen || so->so_error ||
     217           0 :             so->so_rcv.sb_cc >= so->so_rcv.sb_lowat;
     218           0 : }
     219             : 
     220             : /* can we write something to so? */
     221             : #define sowriteable(so) \
     222             :     ((sbspace((so), &(so)->so_snd) >= (so)->so_snd.sb_lowat && \
     223             :         (((so)->so_state & SS_ISCONNECTED) || \
     224             :           ((so)->so_proto->pr_flags & PR_CONNREQUIRED)==0)) || \
     225             :     ((so)->so_state & SS_CANTSENDMORE) || (so)->so_error)
     226             : 
     227             : /* adjust counters in sb reflecting allocation of m */
     228             : #define sballoc(sb, m) do {                                             \
     229             :         (sb)->sb_cc += (m)->m_len;                                        \
     230             :         if ((m)->m_type != MT_CONTROL && (m)->m_type != MT_SONAME)        \
     231             :                 (sb)->sb_datacc += (m)->m_len;                            \
     232             :         (sb)->sb_mbcnt += MSIZE;                                     \
     233             :         if ((m)->m_flags & M_EXT)                                        \
     234             :                 (sb)->sb_mbcnt += (m)->m_ext.ext_size;                    \
     235             : } while (/* CONSTCOND */ 0)
     236             : 
     237             : /* adjust counters in sb reflecting freeing of m */
     238             : #define sbfree(sb, m) do {                                              \
     239             :         (sb)->sb_cc -= (m)->m_len;                                        \
     240             :         if ((m)->m_type != MT_CONTROL && (m)->m_type != MT_SONAME)        \
     241             :                 (sb)->sb_datacc -= (m)->m_len;                            \
     242             :         (sb)->sb_mbcnt -= MSIZE;                                     \
     243             :         if ((m)->m_flags & M_EXT)                                        \
     244             :                 (sb)->sb_mbcnt -= (m)->m_ext.ext_size;                    \
     245             : } while (/* CONSTCOND */ 0)
     246             : 
     247             : /*
     248             :  * Set lock on sockbuf sb; sleep if lock is already held.
     249             :  * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
     250             :  * Returns error without lock if sleep is interrupted.
     251             :  */
     252             : int sblock(struct socket *, struct sockbuf *, int);
     253             : 
     254             : /* release lock on sockbuf sb */
     255             : void sbunlock(struct socket *, struct sockbuf *);
     256             : 
     257             : #define SB_EMPTY_FIXUP(sb) do {                                         \
     258             :         if ((sb)->sb_mb == NULL) {                                   \
     259             :                 (sb)->sb_mbtail = NULL;                                      \
     260             :                 (sb)->sb_lastrecord = NULL;                          \
     261             :         }                                                               \
     262             : } while (/*CONSTCOND*/0)
     263             : 
     264             : extern u_long sb_max;
     265             : 
     266             : extern struct pool      socket_pool;
     267             : 
     268             : struct mbuf;
     269             : struct sockaddr;
     270             : struct proc;
     271             : struct msghdr;
     272             : struct stat;
     273             : struct knote;
     274             : 
     275             : /*
     276             :  * File operations on sockets.
     277             :  */
     278             : int     soo_read(struct file *, struct uio *, int);
     279             : int     soo_write(struct file *, struct uio *, int);
     280             : int     soo_ioctl(struct file *, u_long, caddr_t, struct proc *);
     281             : int     soo_poll(struct file *, int events, struct proc *);
     282             : int     soo_kqfilter(struct file *, struct knote *);
     283             : int     soo_close(struct file *, struct proc *);
     284             : int     soo_stat(struct file *, struct stat *, struct proc *);
     285             : void    sbappend(struct socket *, struct sockbuf *, struct mbuf *);
     286             : void    sbappendstream(struct socket *, struct sockbuf *, struct mbuf *);
     287             : int     sbappendaddr(struct socket *, struct sockbuf *,
     288             :             const struct sockaddr *, struct mbuf *, struct mbuf *);
     289             : int     sbappendcontrol(struct socket *, struct sockbuf *, struct mbuf *,
     290             :             struct mbuf *);
     291             : void    sbappendrecord(struct socket *, struct sockbuf *, struct mbuf *);
     292             : void    sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n);
     293             : struct mbuf *
     294             :         sbcreatecontrol(caddr_t p, int size, int type, int level);
     295             : void    sbdrop(struct socket *, struct sockbuf *, int);
     296             : void    sbdroprecord(struct sockbuf *sb);
     297             : void    sbflush(struct socket *, struct sockbuf *);
     298             : void    sbinsertoob(struct sockbuf *sb, struct mbuf *m0);
     299             : void    sbrelease(struct socket *, struct sockbuf *);
     300             : int     sbcheckreserve(u_long cnt, u_long defcnt);
     301             : int     sbchecklowmem(void);
     302             : int     sbreserve(struct socket *, struct sockbuf *, u_long);
     303             : int     sbwait(struct socket *, struct sockbuf *sb);
     304             : int     sb_lock(struct sockbuf *sb);
     305             : void    soinit(void);
     306             : int     soabort(struct socket *so);
     307             : int     soaccept(struct socket *so, struct mbuf *nam);
     308             : int     sobind(struct socket *so, struct mbuf *nam, struct proc *p);
     309             : void    socantrcvmore(struct socket *so);
     310             : void    socantsendmore(struct socket *so);
     311             : int     soclose(struct socket *, int);
     312             : int     soconnect(struct socket *so, struct mbuf *nam);
     313             : int     soconnect2(struct socket *so1, struct socket *so2);
     314             : int     socreate(int dom, struct socket **aso, int type, int proto);
     315             : int     sodisconnect(struct socket *so);
     316             : void    sofree(struct socket *so, int);
     317             : int     sogetopt(struct socket *so, int level, int optname, struct mbuf *m);
     318             : void    sohasoutofband(struct socket *so);
     319             : void    soisconnected(struct socket *so);
     320             : void    soisconnecting(struct socket *so);
     321             : void    soisdisconnected(struct socket *so);
     322             : void    soisdisconnecting(struct socket *so);
     323             : int     solisten(struct socket *so, int backlog);
     324             : struct socket *sonewconn(struct socket *head, int connstatus);
     325             : void    soqinsque(struct socket *head, struct socket *so, int q);
     326             : int     soqremque(struct socket *so, int q);
     327             : int     soreceive(struct socket *so, struct mbuf **paddr, struct uio *uio,
     328             :             struct mbuf **mp0, struct mbuf **controlp, int *flagsp,
     329             :             socklen_t controllen);
     330             : int     soreserve(struct socket *so, u_long sndcc, u_long rcvcc);
     331             : void    sorflush(struct socket *so);
     332             : int     sosend(struct socket *so, struct mbuf *addr, struct uio *uio,
     333             :             struct mbuf *top, struct mbuf *control, int flags);
     334             : int     sosetopt(struct socket *so, int level, int optname, struct mbuf *m);
     335             : int     soshutdown(struct socket *so, int how);
     336             : void    sowakeup(struct socket *so, struct sockbuf *sb);
     337             : void    sorwakeup(struct socket *);
     338             : void    sowwakeup(struct socket *);
     339             : int     sockargs(struct mbuf **, const void *, size_t, int);
     340             : 
     341             : int     sosleep(struct socket *, void *, int, const char *, int);
     342             : int     solock(struct socket *);
     343             : void    sounlock(struct socket *, int);
     344             : 
     345             : int     sendit(struct proc *, int, struct msghdr *, int, register_t *);
     346             : int     recvit(struct proc *, int, struct msghdr *, caddr_t,
     347             :                     register_t *);
     348             : int     doaccept(struct proc *, int, struct sockaddr *, socklen_t *, int,
     349             :             register_t *);
     350             : 
     351             : #ifdef SOCKBUF_DEBUG
     352             : void    sblastrecordchk(struct sockbuf *, const char *);
     353             : #define SBLASTRECORDCHK(sb, where)      sblastrecordchk((sb), (where))
     354             : 
     355             : void    sblastmbufchk(struct sockbuf *, const char *);
     356             : #define SBLASTMBUFCHK(sb, where)        sblastmbufchk((sb), (where))
     357             : void    sbcheck(struct sockbuf *sb);
     358             : #define SBCHECK(sb)                     sbcheck(sb)
     359             : #else
     360             : #define SBLASTRECORDCHK(sb, where)      /* nothing */
     361             : #define SBLASTMBUFCHK(sb, where)        /* nothing */
     362             : #define SBCHECK(sb)                     /* nothing */
     363             : #endif /* SOCKBUF_DEBUG */
     364             : 
     365             : #endif /* _KERNEL */

Generated by: LCOV version 1.13