version 1.1.1.3, 1998/03/01 02:13:08 |
version 1.6.2.2, 1993/11/06 00:07:55 |
|
|
/* |
/* |
* Copyright (c) 1982, 1986, 1988, 1990, 1993 |
* Copyright (c) 1982, 1986, 1988, 1990 Regents of the University of California. |
* The Regents of the University of California. All rights reserved. |
* All rights reserved. |
* |
* |
* Redistribution and use in source and binary forms, with or without |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* modification, are permitted provided that the following conditions |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
* SUCH DAMAGE. |
* SUCH DAMAGE. |
* |
* |
* @(#)uipc_socket.c 8.6 (Berkeley) 5/2/95 |
* from: @(#)uipc_socket.c 7.28 (Berkeley) 5/4/91 |
|
* $Id$ |
*/ |
*/ |
|
|
#include <sys/param.h> |
#include <sys/param.h> |
|
|
#include <sys/mbuf.h> |
#include <sys/mbuf.h> |
#include <sys/domain.h> |
#include <sys/domain.h> |
#include <sys/kernel.h> |
#include <sys/kernel.h> |
|
#include <sys/select.h> |
#include <sys/protosw.h> |
#include <sys/protosw.h> |
#include <sys/socket.h> |
#include <sys/socket.h> |
#include <sys/socketvar.h> |
#include <sys/socketvar.h> |
#include <sys/resourcevar.h> |
#include <sys/resourcevar.h> |
|
|
|
#include <machine/cpu.h> |
|
|
/* |
/* |
* Socket operation routines. |
* Socket operation routines. |
* These routines are called by the routines in |
* These routines are called by the routines in |
|
|
/*ARGSUSED*/ |
/*ARGSUSED*/ |
int |
int |
socreate(dom, aso, type, proto) |
socreate(dom, aso, type, proto) |
int dom; |
|
struct socket **aso; |
struct socket **aso; |
register int type; |
register int type; |
int proto; |
int proto; |
Line 70 socreate(dom, aso, type, proto) |
|
Line 73 socreate(dom, aso, type, proto) |
|
prp = pffindproto(dom, proto, type); |
prp = pffindproto(dom, proto, type); |
else |
else |
prp = pffindtype(dom, type); |
prp = pffindtype(dom, type); |
if (prp == 0 || prp->pr_usrreq == 0) |
if (!prp || !prp->pr_usrreq) |
return (EPROTONOSUPPORT); |
return (EPROTONOSUPPORT); |
if (prp->pr_type != type) |
if (prp->pr_type != type) |
return (EPROTOTYPE); |
return (EPROTOTYPE); |
Line 80 socreate(dom, aso, type, proto) |
|
Line 83 socreate(dom, aso, type, proto) |
|
if (p->p_ucred->cr_uid == 0) |
if (p->p_ucred->cr_uid == 0) |
so->so_state = SS_PRIV; |
so->so_state = SS_PRIV; |
so->so_proto = prp; |
so->so_proto = prp; |
error = (*prp->pr_usrreq)(so, PRU_ATTACH, (struct mbuf *)0, |
error = |
(struct mbuf *)(long)proto, (struct mbuf *)0); |
(*prp->pr_usrreq)(so, PRU_ATTACH, |
|
(struct mbuf *)0, (struct mbuf *)proto, (struct mbuf *)0); |
if (error) { |
if (error) { |
so->so_state |= SS_NOFDREF; |
so->so_state |= SS_NOFDREF; |
sofree(so); |
sofree(so); |
|
|
goto drop; |
goto drop; |
while (so->so_state & SS_ISCONNECTED) |
while (so->so_state & SS_ISCONNECTED) |
if (error = tsleep((caddr_t)&so->so_timeo, |
if (error = tsleep((caddr_t)&so->so_timeo, |
PSOCK | PCATCH, netcls, so->so_linger * hz)) |
PSOCK | PCATCH, netcls, so->so_linger)) |
break; |
break; |
} |
} |
} |
} |
|
|
return (error); |
return (error); |
} |
} |
|
|
#define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK) |
|
/* |
/* |
* Send on a socket. |
* Send on a socket. |
* If send must go all at once and message is larger than |
* If send must go all at once and message is larger than |
Line 349 sosend(so, addr, uio, top, control, flag |
|
Line 352 sosend(so, addr, uio, top, control, flag |
|
#define snderr(errno) { error = errno; splx(s); goto release; } |
#define snderr(errno) { error = errno; splx(s); goto release; } |
|
|
restart: |
restart: |
if (error = sblock(&so->so_snd, SBLOCKWAIT(flags))) |
if (error = sblock(&so->so_snd)) |
goto out; |
goto out; |
do { |
do { |
s = splnet(); |
s = splnet(); |
|
|
space = sbspace(&so->so_snd); |
space = sbspace(&so->so_snd); |
if (flags & MSG_OOB) |
if (flags & MSG_OOB) |
space += 1024; |
space += 1024; |
if (atomic && resid > so->so_snd.sb_hiwat || |
if (space < resid + clen && |
clen > so->so_snd.sb_hiwat) |
|
snderr(EMSGSIZE); |
|
if (space < resid + clen && uio && |
|
(atomic || space < so->so_snd.sb_lowat || space < clen)) { |
(atomic || space < so->so_snd.sb_lowat || space < clen)) { |
|
if (atomic && resid > so->so_snd.sb_hiwat || |
|
clen > so->so_snd.sb_hiwat) |
|
snderr(EMSGSIZE); |
if (so->so_state & SS_NBIO) |
if (so->so_state & SS_NBIO) |
snderr(EWOULDBLOCK); |
snderr(EWOULDBLOCK); |
sbunlock(&so->so_snd); |
sbunlock(&so->so_snd); |
|
|
MGET(m, M_WAIT, MT_DATA); |
MGET(m, M_WAIT, MT_DATA); |
mlen = MLEN; |
mlen = MLEN; |
} |
} |
if (resid >= MINCLSIZE && space >= MCLBYTES) { |
if (resid >= MINCLSIZE) { |
MCLGET(m, M_WAIT); |
MCLGET(m, M_WAIT); |
if ((m->m_flags & M_EXT) == 0) |
if ((m->m_flags & M_EXT) == 0) |
goto nopages; |
goto nopages; |
mlen = MCLBYTES; |
mlen = MCLBYTES; |
#ifdef MAPPED_MBUFS |
len = min(min(mlen, resid), space); |
len = min(MCLBYTES, resid); |
|
#else |
|
if (atomic && top == 0) { |
|
len = min(MCLBYTES - max_hdr, resid); |
|
m->m_data += max_hdr; |
|
} else |
|
len = min(MCLBYTES, resid); |
|
#endif |
|
space -= MCLBYTES; |
|
} else { |
} else { |
nopages: |
nopages: |
len = min(min(mlen, resid), space); |
len = min(min(mlen, resid), space); |
space -= len; |
|
/* |
/* |
* For datagram protocols, leave room |
* For datagram protocols, leave room |
* for protocol headers in first mbuf. |
* for protocol headers in first mbuf. |
|
|
if (atomic && top == 0 && len < mlen) |
if (atomic && top == 0 && len < mlen) |
MH_ALIGN(m, len); |
MH_ALIGN(m, len); |
} |
} |
|
space -= len; |
error = uiomove(mtod(m, caddr_t), (int)len, uio); |
error = uiomove(mtod(m, caddr_t), (int)len, uio); |
resid = uio->uio_resid; |
resid = uio->uio_resid; |
m->m_len = len; |
m->m_len = len; |
Line 496 soreceive(so, paddr, uio, mp0, controlp, |
|
Line 490 soreceive(so, paddr, uio, mp0, controlp, |
|
struct mbuf **controlp; |
struct mbuf **controlp; |
int *flagsp; |
int *flagsp; |
{ |
{ |
|
struct proc *p = curproc; /* XXX */ |
register struct mbuf *m, **mp; |
register struct mbuf *m, **mp; |
register int flags, len, error, s, offset; |
register int flags, len, error, s, offset; |
struct protosw *pr = so->so_proto; |
struct protosw *pr = so->so_proto; |
Line 514 soreceive(so, paddr, uio, mp0, controlp, |
|
Line 509 soreceive(so, paddr, uio, mp0, controlp, |
|
flags = 0; |
flags = 0; |
if (flags & MSG_OOB) { |
if (flags & MSG_OOB) { |
m = m_get(M_WAIT, MT_DATA); |
m = m_get(M_WAIT, MT_DATA); |
error = (*pr->pr_usrreq)(so, PRU_RCVOOB, m, |
error = (*pr->pr_usrreq)(so, PRU_RCVOOB, |
(struct mbuf *)(long)(flags & MSG_PEEK), (struct mbuf *)0); |
m, (struct mbuf *)(flags & MSG_PEEK), (struct mbuf *)0); |
if (error) |
if (error) |
goto bad; |
goto bad; |
do { |
do { |
|
|
(struct mbuf *)0, (struct mbuf *)0); |
(struct mbuf *)0, (struct mbuf *)0); |
|
|
restart: |
restart: |
if (error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) |
if (error = sblock(&so->so_rcv)) |
return (error); |
return (error); |
s = splnet(); |
s = splnet(); |
|
|
|
|
* (subject to any timeout) if: |
* (subject to any timeout) if: |
* 1. the current count is less than the low water mark, or |
* 1. the current count is less than the low water mark, or |
* 2. MSG_WAITALL is set, and it is possible to do the entire |
* 2. MSG_WAITALL is set, and it is possible to do the entire |
* receive operation at once if we block (resid <= hiwat), or |
* receive operation at once if we block (resid <= hiwat). |
* 3. MSG_DONTWAIT is not set. |
|
* If MSG_WAITALL is set but resid is larger than the receive buffer, |
* If MSG_WAITALL is set but resid is larger than the receive buffer, |
* we have to do the receive in sections, and thus risk returning |
* we have to do the receive in sections, and thus risk returning |
* a short count if a timeout or signal occurs after we start. |
* a short count if a timeout or signal occurs after we start. |
*/ |
*/ |
if (m == 0 || ((flags & MSG_DONTWAIT) == 0 && |
while (m == 0 || so->so_rcv.sb_cc < uio->uio_resid && |
so->so_rcv.sb_cc < uio->uio_resid) && |
|
(so->so_rcv.sb_cc < so->so_rcv.sb_lowat || |
(so->so_rcv.sb_cc < so->so_rcv.sb_lowat || |
((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) && |
((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) && |
m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0) { |
m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0) { |
|
|
#endif |
#endif |
if (so->so_error) { |
if (so->so_error) { |
if (m) |
if (m) |
goto dontblock; |
break; |
error = so->so_error; |
error = so->so_error; |
if ((flags & MSG_PEEK) == 0) |
if ((flags & MSG_PEEK) == 0) |
so->so_error = 0; |
so->so_error = 0; |
|
|
} |
} |
if (so->so_state & SS_CANTRCVMORE) { |
if (so->so_state & SS_CANTRCVMORE) { |
if (m) |
if (m) |
goto dontblock; |
break; |
else |
else |
goto release; |
goto release; |
} |
} |
|
|
} |
} |
if (uio->uio_resid == 0) |
if (uio->uio_resid == 0) |
goto release; |
goto release; |
if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) { |
if (so->so_state & SS_NBIO) { |
error = EWOULDBLOCK; |
error = EWOULDBLOCK; |
goto release; |
goto release; |
} |
} |
|
|
goto restart; |
goto restart; |
} |
} |
dontblock: |
dontblock: |
if (uio->uio_procp) |
p->p_stats->p_ru.ru_msgrcv++; |
uio->uio_procp->p_stats->p_ru.ru_msgrcv++; |
|
nextrecord = m->m_nextpkt; |
nextrecord = m->m_nextpkt; |
if (pr->pr_flags & PR_ADDR) { |
if (pr->pr_flags & PR_ADDR) { |
#ifdef DIAGNOSTIC |
#ifdef DIAGNOSTIC |
|
|
so->so_rcv.sb_mb = nextrecord; |
so->so_rcv.sb_mb = nextrecord; |
if (pr->pr_flags & PR_WANTRCVD && so->so_pcb) |
if (pr->pr_flags & PR_WANTRCVD && so->so_pcb) |
(*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0, |
(*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0, |
(struct mbuf *)(long)flags, (struct mbuf *)0, |
(struct mbuf *)flags, (struct mbuf *)0, |
(struct mbuf *)0); |
(struct mbuf *)0); |
} |
} |
if (orig_resid == uio->uio_resid && orig_resid && |
if (orig_resid == uio->uio_resid && orig_resid && |
|
|
return (error); |
return (error); |
} |
} |
|
|
int |
|
soshutdown(so, how) |
soshutdown(so, how) |
register struct socket *so; |
register struct socket *so; |
register int how; |
register int how; |
Line 802 soshutdown(so, how) |
|
Line 793 soshutdown(so, how) |
|
return (0); |
return (0); |
} |
} |
|
|
void |
|
sorflush(so) |
sorflush(so) |
register struct socket *so; |
register struct socket *so; |
{ |
{ |
|
|
struct sockbuf asb; |
struct sockbuf asb; |
|
|
sb->sb_flags |= SB_NOINTR; |
sb->sb_flags |= SB_NOINTR; |
(void) sblock(sb, M_WAITOK); |
(void) sblock(sb); |
s = splimp(); |
s = splimp(); |
socantrcvmore(so); |
socantrcvmore(so); |
sbunlock(sb); |
sbunlock(sb); |
|
|
sbrelease(&asb); |
sbrelease(&asb); |
} |
} |
|
|
int |
|
sosetopt(so, level, optname, m0) |
sosetopt(so, level, optname, m0) |
register struct socket *so; |
register struct socket *so; |
int level, optname; |
int level, optname; |
Line 855 sosetopt(so, level, optname, m0) |
|
Line 844 sosetopt(so, level, optname, m0) |
|
case SO_USELOOPBACK: |
case SO_USELOOPBACK: |
case SO_BROADCAST: |
case SO_BROADCAST: |
case SO_REUSEADDR: |
case SO_REUSEADDR: |
case SO_REUSEPORT: |
|
case SO_OOBINLINE: |
case SO_OOBINLINE: |
if (m == NULL || m->m_len < sizeof (int)) { |
if (m == NULL || m->m_len < sizeof (int)) { |
error = EINVAL; |
error = EINVAL; |
Line 907 sosetopt(so, level, optname, m0) |
|
Line 895 sosetopt(so, level, optname, m0) |
|
goto bad; |
goto bad; |
} |
} |
tv = mtod(m, struct timeval *); |
tv = mtod(m, struct timeval *); |
if (tv->tv_sec * hz + tv->tv_usec / tick > SHRT_MAX) { |
if (tv->tv_sec > SHRT_MAX / hz - hz) { |
error = EDOM; |
error = EDOM; |
goto bad; |
goto bad; |
} |
} |
Line 929 sosetopt(so, level, optname, m0) |
|
Line 917 sosetopt(so, level, optname, m0) |
|
error = ENOPROTOOPT; |
error = ENOPROTOOPT; |
break; |
break; |
} |
} |
if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) { |
|
(void) ((*so->so_proto->pr_ctloutput) |
|
(PRCO_SETOPT, so, level, optname, &m0)); |
|
m = NULL; /* freed by protocol */ |
|
} |
|
} |
} |
bad: |
bad: |
if (m) |
if (m) |
|
|
return (error); |
return (error); |
} |
} |
|
|
int |
|
sogetopt(so, level, optname, mp) |
sogetopt(so, level, optname, mp) |
register struct socket *so; |
register struct socket *so; |
int level, optname; |
int level, optname; |
Line 973 sogetopt(so, level, optname, mp) |
|
Line 955 sogetopt(so, level, optname, mp) |
|
case SO_DEBUG: |
case SO_DEBUG: |
case SO_KEEPALIVE: |
case SO_KEEPALIVE: |
case SO_REUSEADDR: |
case SO_REUSEADDR: |
case SO_REUSEPORT: |
|
case SO_BROADCAST: |
case SO_BROADCAST: |
case SO_OOBINLINE: |
case SO_OOBINLINE: |
*mtod(m, int *) = so->so_options & optname; |
*mtod(m, int *) = so->so_options & optname; |
Line 1013 sogetopt(so, level, optname, mp) |
|
Line 994 sogetopt(so, level, optname, mp) |
|
m->m_len = sizeof(struct timeval); |
m->m_len = sizeof(struct timeval); |
mtod(m, struct timeval *)->tv_sec = val / hz; |
mtod(m, struct timeval *)->tv_sec = val / hz; |
mtod(m, struct timeval *)->tv_usec = |
mtod(m, struct timeval *)->tv_usec = |
(val % hz) * tick; |
(val % hz) / tick; |
break; |
break; |
} |
} |
|
|
Line 1026 sogetopt(so, level, optname, mp) |
|
Line 1007 sogetopt(so, level, optname, mp) |
|
} |
} |
} |
} |
|
|
void |
|
sohasoutofband(so) |
sohasoutofband(so) |
register struct socket *so; |
register struct socket *so; |
{ |
{ |