2000-10-25 05:49:55 +04:00
|
|
|
/* $NetBSD: net.c,v 1.27 2000/10/25 01:49:55 thorpej Exp $ */
|
1994-10-26 08:44:32 +03:00
|
|
|
|
1994-05-08 20:11:14 +04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1992 Regents of the University of California.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This software was developed by the Computer Systems Engineering group
|
|
|
|
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
|
|
|
|
* contributed to Berkeley.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Lawrence Berkeley Laboratory and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1994-10-26 08:44:32 +03:00
|
|
|
* @(#) Header: net.c,v 1.9 93/08/06 19:32:15 leres Exp (LBL)
|
1994-05-08 20:11:14 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
1999-11-14 00:17:56 +03:00
|
|
|
#ifdef _STANDALONE
|
|
|
|
#include <lib/libkern/libkern.h>
|
|
|
|
#else
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
|
1994-05-08 20:11:14 +04:00
|
|
|
#include <net/if.h>
|
1997-07-07 19:52:49 +04:00
|
|
|
#include <net/if_ether.h>
|
1994-05-08 20:11:14 +04:00
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
|
|
|
|
#include "stand.h"
|
|
|
|
#include "net.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send a packet and wait for a reply, with exponential backoff.
|
|
|
|
*
|
1997-12-27 01:41:30 +03:00
|
|
|
* The send routine must return the actual number of bytes written,
|
|
|
|
* or -1 on error.
|
1994-05-08 20:11:14 +04:00
|
|
|
*
|
|
|
|
* The receive routine can indicate success by returning the number of
|
|
|
|
* bytes read; it can return 0 to indicate EOF; it can return -1 with a
|
|
|
|
* non-zero errno to indicate failure; finally, it can return -1 with a
|
|
|
|
* zero errno to indicate it isn't done yet.
|
|
|
|
*/
|
1995-09-15 03:45:21 +04:00
|
|
|
ssize_t
|
1994-05-08 20:11:14 +04:00
|
|
|
sendrecv(d, sproc, sbuf, ssize, rproc, rbuf, rsize)
|
2000-03-30 16:19:46 +04:00
|
|
|
struct iodesc *d;
|
|
|
|
ssize_t (*sproc)(struct iodesc *, void *, size_t);
|
|
|
|
void *sbuf;
|
|
|
|
size_t ssize;
|
|
|
|
ssize_t (*rproc)(struct iodesc *, void *, size_t, time_t);
|
|
|
|
void *rbuf;
|
|
|
|
size_t rsize;
|
1994-05-08 20:11:14 +04:00
|
|
|
{
|
2000-03-30 16:19:46 +04:00
|
|
|
ssize_t cc;
|
|
|
|
time_t t, tmo, tlast;
|
1995-12-14 02:38:10 +03:00
|
|
|
long tleft;
|
1994-05-08 20:11:14 +04:00
|
|
|
|
|
|
|
#ifdef NET_DEBUG
|
|
|
|
if (debug)
|
1996-10-13 06:28:58 +04:00
|
|
|
printf("sendrecv: called\n");
|
1994-05-08 20:11:14 +04:00
|
|
|
#endif
|
1995-02-20 14:04:00 +03:00
|
|
|
|
1994-05-08 20:11:14 +04:00
|
|
|
tmo = MINTMO;
|
|
|
|
tlast = tleft = 0;
|
|
|
|
t = getsecs();
|
|
|
|
for (;;) {
|
|
|
|
if (tleft <= 0) {
|
1995-12-14 02:38:10 +03:00
|
|
|
if (tmo >= MAXTMO) {
|
1995-09-19 01:19:18 +04:00
|
|
|
errno = ETIMEDOUT;
|
|
|
|
return -1;
|
|
|
|
}
|
1994-05-08 20:11:14 +04:00
|
|
|
cc = (*sproc)(d, sbuf, ssize);
|
1997-12-27 01:41:30 +03:00
|
|
|
if (cc != -1 && cc < ssize)
|
1994-05-08 20:11:14 +04:00
|
|
|
panic("sendrecv: short write! (%d < %d)",
|
|
|
|
cc, ssize);
|
|
|
|
|
|
|
|
tleft = tmo;
|
|
|
|
tmo <<= 1;
|
|
|
|
if (tmo > MAXTMO)
|
|
|
|
tmo = MAXTMO;
|
1997-12-27 01:41:30 +03:00
|
|
|
|
|
|
|
if (cc == -1) {
|
|
|
|
/* Error on transmit; wait before retrying */
|
|
|
|
while ((getsecs() - t) < tmo);
|
|
|
|
tleft = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
1994-05-08 20:11:14 +04:00
|
|
|
tlast = t;
|
|
|
|
}
|
|
|
|
|
1995-02-20 14:04:00 +03:00
|
|
|
/* Try to get a packet and process it. */
|
|
|
|
cc = (*rproc)(d, rbuf, rsize, tleft);
|
|
|
|
/* Return on data, EOF or real error. */
|
|
|
|
if (cc != -1 || errno != 0)
|
|
|
|
return (cc);
|
|
|
|
|
1994-05-08 20:11:14 +04:00
|
|
|
/* Timed out or didn't get the packet we're waiting for */
|
|
|
|
t = getsecs();
|
|
|
|
tleft -= t - tlast;
|
|
|
|
tlast = t;
|
|
|
|
}
|
|
|
|
}
|