1*10497fd2Schristos /* $NetBSD: net.c,v 1.36 2019/03/31 20:08:45 christos Exp $ */
26668f51cScgd
3e03842d0Sbrezak /*
4e03842d0Sbrezak * Copyright (c) 1992 Regents of the University of California.
5e03842d0Sbrezak * All rights reserved.
6e03842d0Sbrezak *
7e03842d0Sbrezak * This software was developed by the Computer Systems Engineering group
8e03842d0Sbrezak * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9e03842d0Sbrezak * contributed to Berkeley.
10e03842d0Sbrezak *
11e03842d0Sbrezak * Redistribution and use in source and binary forms, with or without
12e03842d0Sbrezak * modification, are permitted provided that the following conditions
13e03842d0Sbrezak * are met:
14e03842d0Sbrezak * 1. Redistributions of source code must retain the above copyright
15e03842d0Sbrezak * notice, this list of conditions and the following disclaimer.
16e03842d0Sbrezak * 2. Redistributions in binary form must reproduce the above copyright
17e03842d0Sbrezak * notice, this list of conditions and the following disclaimer in the
18e03842d0Sbrezak * documentation and/or other materials provided with the distribution.
19e03842d0Sbrezak * 3. All advertising materials mentioning features or use of this software
20e03842d0Sbrezak * must display the following acknowledgement:
21e03842d0Sbrezak * This product includes software developed by the University of
22e03842d0Sbrezak * California, Lawrence Berkeley Laboratory and its contributors.
23e03842d0Sbrezak * 4. Neither the name of the University nor the names of its contributors
24e03842d0Sbrezak * may be used to endorse or promote products derived from this software
25e03842d0Sbrezak * without specific prior written permission.
26e03842d0Sbrezak *
27e03842d0Sbrezak * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28e03842d0Sbrezak * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29e03842d0Sbrezak * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30e03842d0Sbrezak * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31e03842d0Sbrezak * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32e03842d0Sbrezak * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33e03842d0Sbrezak * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34e03842d0Sbrezak * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35e03842d0Sbrezak * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36e03842d0Sbrezak * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37e03842d0Sbrezak * SUCH DAMAGE.
38e03842d0Sbrezak *
396668f51cScgd * @(#) Header: net.c,v 1.9 93/08/06 19:32:15 leres Exp (LBL)
40e03842d0Sbrezak */
41e03842d0Sbrezak
42e03842d0Sbrezak #include <sys/param.h>
43e03842d0Sbrezak #include <sys/socket.h>
44e03842d0Sbrezak
4536ff5d93Sthorpej #ifdef _STANDALONE
4636ff5d93Sthorpej #include <lib/libkern/libkern.h>
4736ff5d93Sthorpej #else
4836ff5d93Sthorpej #include <string.h>
4936ff5d93Sthorpej #endif
5036ff5d93Sthorpej
51e03842d0Sbrezak #include <net/if.h>
5264cd693aSdrochner #include <net/if_ether.h>
53e03842d0Sbrezak
54e03842d0Sbrezak #include <netinet/in.h>
55e03842d0Sbrezak #include <netinet/in_systm.h>
56e03842d0Sbrezak #include <netinet/ip.h>
57e03842d0Sbrezak
5805b5a4b7Sdsl #ifdef _STANDALONE
59e03842d0Sbrezak #include "stand.h"
6005b5a4b7Sdsl #define delay()
6105b5a4b7Sdsl #else
6205b5a4b7Sdsl #include <errno.h>
6305b5a4b7Sdsl #include <stdio.h>
6405b5a4b7Sdsl #include <unistd.h>
6505b5a4b7Sdsl #define panic printf
6605b5a4b7Sdsl #define delay() usleep(100000)
6705b5a4b7Sdsl #endif
6805b5a4b7Sdsl
6980a35507Sisaki #include "net.h"
70e03842d0Sbrezak
71e03842d0Sbrezak /*
72e03842d0Sbrezak * Send a packet and wait for a reply, with exponential backoff.
73e03842d0Sbrezak *
74d8f02788Sscottr * The send routine must return the actual number of bytes written,
75d8f02788Sscottr * or -1 on error.
76e03842d0Sbrezak *
77e03842d0Sbrezak * The receive routine can indicate success by returning the number of
78e03842d0Sbrezak * bytes read; it can return 0 to indicate EOF; it can return -1 with a
79e03842d0Sbrezak * non-zero errno to indicate failure; finally, it can return -1 with a
80e03842d0Sbrezak * zero errno to indicate it isn't done yet.
81e03842d0Sbrezak */
8284c517c1Spk ssize_t
sendrecv(struct iodesc * d,ssize_t (* sproc)(struct iodesc *,void *,size_t),void * sbuf,size_t ssize,ssize_t (* rproc)(struct iodesc *,void *,size_t,saseconds_t),void * rbuf,size_t rsize)831c038e68Sisaki sendrecv(struct iodesc *d,
841c038e68Sisaki ssize_t (*sproc)(struct iodesc *, void *, size_t),
851c038e68Sisaki void *sbuf, size_t ssize,
8669cf32a7Stsutsui ssize_t (*rproc)(struct iodesc *, void *, size_t, saseconds_t),
871c038e68Sisaki void *rbuf, size_t rsize)
88e03842d0Sbrezak {
891279e67bSaugustss ssize_t cc;
9069cf32a7Stsutsui satime_t t, tlast;
9169cf32a7Stsutsui saseconds_t tmo, tleft;
92e03842d0Sbrezak
93e03842d0Sbrezak #ifdef NET_DEBUG
94e03842d0Sbrezak if (debug)
95*10497fd2Schristos printf("%s: called\n", __func__);
96e03842d0Sbrezak #endif
97258efc53Smycroft
98e03842d0Sbrezak tmo = MINTMO;
9969cf32a7Stsutsui tlast = 0;
10069cf32a7Stsutsui tleft = 0;
101e03842d0Sbrezak t = getsecs();
102e03842d0Sbrezak for (;;) {
103e03842d0Sbrezak if (tleft <= 0) {
104284c0cd0Spk if (tmo >= MAXTMO) {
105d9124da4Spk errno = ETIMEDOUT;
106d9124da4Spk return -1;
107d9124da4Spk }
108e03842d0Sbrezak cc = (*sproc)(d, sbuf, ssize);
109e03842d0Sbrezak
110e03842d0Sbrezak tleft = tmo;
111e03842d0Sbrezak tmo <<= 1;
112e03842d0Sbrezak if (tmo > MAXTMO)
113e03842d0Sbrezak tmo = MAXTMO;
114d8f02788Sscottr
115d8f02788Sscottr if (cc == -1) {
116d8f02788Sscottr /* Error on transmit; wait before retrying */
11705b5a4b7Sdsl while ((getsecs() - t) < tmo)
11805b5a4b7Sdsl delay();
119d8f02788Sscottr tleft = 0;
120d8f02788Sscottr continue;
121d8f02788Sscottr }
12294ec558cSlukem if ((size_t)cc < ssize)
123*10497fd2Schristos panic("%s: short write! (%zd < %zu)", __func__,
12405b5a4b7Sdsl cc, ssize);
125d8f02788Sscottr
126e03842d0Sbrezak tlast = t;
127e03842d0Sbrezak }
128e03842d0Sbrezak
129258efc53Smycroft /* Try to get a packet and process it. */
130258efc53Smycroft cc = (*rproc)(d, rbuf, rsize, tleft);
131258efc53Smycroft /* Return on data, EOF or real error. */
132258efc53Smycroft if (cc != -1 || errno != 0)
1331c038e68Sisaki return cc;
134258efc53Smycroft
135e03842d0Sbrezak /* Timed out or didn't get the packet we're waiting for */
136e03842d0Sbrezak t = getsecs();
137e03842d0Sbrezak tleft -= t - tlast;
138e03842d0Sbrezak tlast = t;
139e03842d0Sbrezak }
140e03842d0Sbrezak }
141