home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.2 (Developer) / NS_dev_3.2.iso / NextDeveloper / Headers / bsd / sys / socketvar.h < prev    next >
Text File  |  1993-10-19  |  6KB  |  190 lines

  1. /* 
  2.  * Mach Operating System
  3.  * Copyright (c) 1989 Carnegie-Mellon University
  4.  * Copyright (c) 1988 Carnegie-Mellon University
  5.  * Copyright (c) 1987 Carnegie-Mellon University
  6.  * All rights reserved.  The CMU software License Agreement specifies
  7.  * the terms and conditions for use and redistribution.
  8.  */
  9. /*
  10.  * HISTORY
  11.  * $Log:    socketvar.h,v $
  12.  * Revision 2.5  89/08/08  21:50:35  jsb
  13.  *     Made sballoc/sbfree only charge for 1k on large mbufs. Strange
  14.  *     as it might look, this is really the only thing that works on
  15.  *     machines with CLBYTES > 1k. Sigh.
  16.  *     [89/07/23            af]
  17.  * 
  18.  * Revision 2.4  89/03/09  22:07:44  rpd
  19.  *     More cleanup.
  20.  * 
  21.  * Revision 2.2  88/08/24  02:45:07  mwyoung
  22.  *     Adjusted include file references.
  23.  *     [88/08/17  02:23:25  mwyoung]
  24.  *
  25.  * 25-Jan-86  Avadis Tevanian (avie) at Carnegie-Mellon University
  26.  *    Upgrade to 4.3.
  27.  *
  28.  */
  29. /*
  30.  * Copyright (c) 1982, 1986 Regents of the University of California.
  31.  * All rights reserved.  The Berkeley software License Agreement
  32.  * specifies the terms and conditions for redistribution.
  33.  *
  34.  *    @(#)socketvar.h    7.1 (Berkeley) 6/4/86
  35.  */
  36.  
  37. #ifndef    _SYS_SOCKETVAR_H_
  38. #define _SYS_SOCKETVAR_H_
  39.  
  40. #import <sys/types.h>
  41. #import <sys/param.h>        /* for MIN */
  42.  
  43. /*
  44.  * Kernel structure per socket.
  45.  * Contains send and receive buffer queues,
  46.  * handle on protocol and pointer to protocol
  47.  * private data and error information.
  48.  */
  49. struct socket {
  50.     short    so_type;        /* generic type, see socket.h */
  51.     short    so_options;        /* from socket call, see socket.h */
  52.     short    so_linger;        /* time to linger while closing */
  53.     short    so_state;        /* internal state flags SS_*, below */
  54.     caddr_t    so_pcb;            /* protocol control block */
  55.     struct    protosw *so_proto;    /* protocol handle */
  56. /*
  57.  * Variables for connection queueing.
  58.  * Socket where accepts occur is so_head in all subsidiary sockets.
  59.  * If so_head is 0, socket is not related to an accept.
  60.  * For head socket so_q0 queues partially completed connections,
  61.  * while so_q is a queue of connections ready to be accepted.
  62.  * If a connection is aborted and it has so_head set, then
  63.  * it has to be pulled out of either so_q0 or so_q.
  64.  * We allow connections to queue up based on current queue lengths
  65.  * and limit on number of queued connections for this socket.
  66.  */
  67.     struct    socket *so_head;    /* back pointer to accept socket */
  68.     struct    socket *so_q0;        /* queue of partial connections */
  69.     short    so_q0len;        /* partials on so_q0 */
  70.     struct    socket *so_q;        /* queue of incoming connections */
  71.     short    so_qlen;        /* number of connections on so_q */
  72.     short    so_qlimit;        /* max number queued connections */
  73. /*
  74.  * Variables for socket buffering.
  75.  */
  76.     struct    sockbuf {
  77.         u_short    sb_cc;        /* actual chars in buffer */
  78.         u_short    sb_hiwat;    /* max actual char count */
  79.         u_short    sb_mbcnt;    /* chars of mbufs used */
  80.         u_short    sb_mbmax;    /* max chars of mbufs to use */
  81.         u_short    sb_lowat;    /* low water mark (not used yet) */
  82.         short    sb_timeo;    /* timeout (not used yet) */
  83.         struct    mbuf *sb_mb;    /* the mbuf chain */
  84.         struct    proc *sb_sel;    /* process selecting read/write */
  85.         short    sb_flags;    /* flags, see below */
  86.     } so_rcv, so_snd;
  87. #define SB_MAX        65535        /* max chars in sockbuf */
  88. #define SB_LOCK        0x01        /* lock on data queue (so_rcv only) */
  89. #define SB_WANT        0x02        /* someone is waiting to lock */
  90. #define SB_WAIT        0x04        /* someone is waiting for data/space */
  91. #define SB_SEL        0x08        /* buffer is selected */
  92. #define SB_COLL        0x10        /* collision selecting */
  93.     short    so_timeo;        /* connection timeout */
  94.     u_short    so_error;        /* error affecting connection */
  95.     u_short    so_oobmark;        /* chars to oob mark */
  96.     short    so_pgrp;        /* pgrp for signals */
  97. };
  98.  
  99. /*
  100.  * Socket state bits.
  101.  */
  102. #define SS_NOFDREF        0x001    /* no file table ref any more */
  103. #define SS_ISCONNECTED        0x002    /* socket connected to a peer */
  104. #define SS_ISCONNECTING        0x004    /* in process of connecting to peer */
  105. #define SS_ISDISCONNECTING    0x008    /* in process of disconnecting */
  106. #define SS_CANTSENDMORE        0x010    /* can't send more data to peer */
  107. #define SS_CANTRCVMORE        0x020    /* can't receive more data from peer */
  108. #define SS_RCVATMARK        0x040    /* at mark on input */
  109.  
  110. #define SS_PRIV            0x080    /* privileged for broadcast, raw... */
  111. #define SS_NBIO            0x100    /* non-blocking ops */
  112. #define SS_ASYNC        0x200    /* async i/o notify */
  113.  
  114.  
  115. /*
  116.  * Macros for sockets and socket buffering.
  117.  */
  118.  
  119. /* how much space is there in a socket buffer (so->so_snd or so->so_rcv) */
  120. #define sbspace(sb)                        \
  121.     (MIN((int)((sb)->sb_hiwat - (sb)->sb_cc),            \
  122.      (int)((sb)->sb_mbmax - (sb)->sb_mbcnt)))
  123.  
  124. /* do we have to send all at once on a socket? */
  125. #define sosendallatonce(so)                    \
  126.     ((so)->so_proto->pr_flags & PR_ATOMIC)
  127.  
  128. /* can we read something from so? */
  129. #define soreadable(so)                        \
  130.     ((so)->so_rcv.sb_cc || ((so)->so_state & SS_CANTRCVMORE) ||    \
  131.     (so)->so_qlen || (so)->so_error)
  132.  
  133. /* can we write something to so? */
  134. #define sowriteable(so)                        \
  135.     ((sbspace(&(so)->so_snd) > 0 &&                \
  136.     (((so)->so_state&SS_ISCONNECTED) ||            \
  137.       ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) ||    \
  138.      ((so)->so_state & SS_CANTSENDMORE) ||            \
  139.      (so)->so_error)
  140.  
  141. #ifdef    KERNEL
  142. #import <kernserv/macro_help.h>
  143.  
  144. /* adjust counters in sb reflecting allocation of m */
  145. #define sballoc(sb, m)                        \
  146. MACRO_BEGIN                            \
  147.     (sb)->sb_cc += (m)->m_len;                \
  148.     (sb)->sb_mbcnt += MSIZE;                \
  149.     if ((m)->m_off > MMAXOFF)                \
  150.         (sb)->sb_mbcnt += 1024;                \
  151. MACRO_END
  152.  
  153. /* adjust counters in sb reflecting freeing of m */
  154. #define sbfree(sb, m)                        \
  155. MACRO_BEGIN                            \
  156.     (sb)->sb_cc -= (m)->m_len;                \
  157.     (sb)->sb_mbcnt -= MSIZE;                \
  158.     if ((m)->m_off > MMAXOFF)                \
  159.         (sb)->sb_mbcnt -= 1024;                \
  160. MACRO_END
  161.  
  162. /* set lock on sockbuf sb */
  163. #define sblock(sb)                        \
  164. MACRO_BEGIN                            \
  165.     while ((sb)->sb_flags & SB_LOCK) {            \
  166.         (sb)->sb_flags |= SB_WANT;            \
  167.         sleep((caddr_t)&(sb)->sb_flags, PZERO+1);    \
  168.     }                            \
  169.     (sb)->sb_flags |= SB_LOCK;                \
  170. MACRO_END
  171.  
  172. /* release lock on sockbuf sb */
  173. #define sbunlock(sb)                        \
  174. MACRO_BEGIN                            \
  175.     (sb)->sb_flags &= ~SB_LOCK;                \
  176.     if ((sb)->sb_flags & SB_WANT) {                \
  177.         (sb)->sb_flags &= ~SB_WANT;            \
  178.         wakeup((caddr_t)&(sb)->sb_flags);        \
  179.     }                            \
  180. MACRO_END
  181.  
  182. extern struct    socket *sonewconn();
  183.  
  184. #define sorwakeup(so)    sowakeup((so), &(so)->so_rcv)
  185. #define sowwakeup(so)    sowakeup((so), &(so)->so_snd)
  186. #endif    KERNEL
  187. #endif    _SYS_SOCKETVAR_H_
  188.  
  189.  
  190.