net.c revision 1.29 1 1.29 christos /* $NetBSD: net.c,v 1.29 2005/12/11 12:24:46 christos Exp $ */
2 1.3 cgd
3 1.1 brezak /*
4 1.1 brezak * Copyright (c) 1992 Regents of the University of California.
5 1.1 brezak * All rights reserved.
6 1.1 brezak *
7 1.1 brezak * This software was developed by the Computer Systems Engineering group
8 1.1 brezak * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 1.1 brezak * contributed to Berkeley.
10 1.1 brezak *
11 1.1 brezak * Redistribution and use in source and binary forms, with or without
12 1.1 brezak * modification, are permitted provided that the following conditions
13 1.1 brezak * are met:
14 1.1 brezak * 1. Redistributions of source code must retain the above copyright
15 1.1 brezak * notice, this list of conditions and the following disclaimer.
16 1.1 brezak * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 brezak * notice, this list of conditions and the following disclaimer in the
18 1.1 brezak * documentation and/or other materials provided with the distribution.
19 1.1 brezak * 3. All advertising materials mentioning features or use of this software
20 1.1 brezak * must display the following acknowledgement:
21 1.1 brezak * This product includes software developed by the University of
22 1.1 brezak * California, Lawrence Berkeley Laboratory and its contributors.
23 1.1 brezak * 4. Neither the name of the University nor the names of its contributors
24 1.1 brezak * may be used to endorse or promote products derived from this software
25 1.1 brezak * without specific prior written permission.
26 1.1 brezak *
27 1.1 brezak * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 1.1 brezak * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 1.1 brezak * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 1.1 brezak * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 1.1 brezak * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 1.1 brezak * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 1.1 brezak * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 1.1 brezak * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 1.1 brezak * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 1.1 brezak * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 1.1 brezak * SUCH DAMAGE.
38 1.1 brezak *
39 1.3 cgd * @(#) Header: net.c,v 1.9 93/08/06 19:32:15 leres Exp (LBL)
40 1.1 brezak */
41 1.1 brezak
42 1.1 brezak #include <sys/param.h>
43 1.1 brezak #include <sys/socket.h>
44 1.1 brezak
45 1.25 thorpej #ifdef _STANDALONE
46 1.25 thorpej #include <lib/libkern/libkern.h>
47 1.25 thorpej #else
48 1.25 thorpej #include <string.h>
49 1.25 thorpej #endif
50 1.25 thorpej
51 1.1 brezak #include <net/if.h>
52 1.17 drochner #include <net/if_ether.h>
53 1.1 brezak
54 1.1 brezak #include <netinet/in.h>
55 1.1 brezak #include <netinet/in_systm.h>
56 1.1 brezak #include <netinet/ip.h>
57 1.1 brezak
58 1.1 brezak #include "stand.h"
59 1.1 brezak #include "net.h"
60 1.1 brezak
61 1.1 brezak /*
62 1.1 brezak * Send a packet and wait for a reply, with exponential backoff.
63 1.1 brezak *
64 1.20 scottr * The send routine must return the actual number of bytes written,
65 1.20 scottr * or -1 on error.
66 1.1 brezak *
67 1.1 brezak * The receive routine can indicate success by returning the number of
68 1.1 brezak * bytes read; it can return 0 to indicate EOF; it can return -1 with a
69 1.1 brezak * non-zero errno to indicate failure; finally, it can return -1 with a
70 1.1 brezak * zero errno to indicate it isn't done yet.
71 1.1 brezak */
72 1.6 pk ssize_t
73 1.1 brezak sendrecv(d, sproc, sbuf, ssize, rproc, rbuf, rsize)
74 1.26 augustss struct iodesc *d;
75 1.26 augustss ssize_t (*sproc)(struct iodesc *, void *, size_t);
76 1.26 augustss void *sbuf;
77 1.26 augustss size_t ssize;
78 1.26 augustss ssize_t (*rproc)(struct iodesc *, void *, size_t, time_t);
79 1.26 augustss void *rbuf;
80 1.26 augustss size_t rsize;
81 1.1 brezak {
82 1.26 augustss ssize_t cc;
83 1.26 augustss time_t t, tmo, tlast;
84 1.12 pk long tleft;
85 1.1 brezak
86 1.1 brezak #ifdef NET_DEBUG
87 1.1 brezak if (debug)
88 1.14 christos printf("sendrecv: called\n");
89 1.1 brezak #endif
90 1.4 mycroft
91 1.1 brezak tmo = MINTMO;
92 1.1 brezak tlast = tleft = 0;
93 1.1 brezak t = getsecs();
94 1.1 brezak for (;;) {
95 1.1 brezak if (tleft <= 0) {
96 1.12 pk if (tmo >= MAXTMO) {
97 1.8 pk errno = ETIMEDOUT;
98 1.8 pk return -1;
99 1.8 pk }
100 1.1 brezak cc = (*sproc)(d, sbuf, ssize);
101 1.28 fvdl if (cc != -1 && (size_t)cc < ssize)
102 1.1 brezak panic("sendrecv: short write! (%d < %d)",
103 1.1 brezak cc, ssize);
104 1.1 brezak
105 1.1 brezak tleft = tmo;
106 1.1 brezak tmo <<= 1;
107 1.1 brezak if (tmo > MAXTMO)
108 1.1 brezak tmo = MAXTMO;
109 1.20 scottr
110 1.20 scottr if (cc == -1) {
111 1.20 scottr /* Error on transmit; wait before retrying */
112 1.20 scottr while ((getsecs() - t) < tmo);
113 1.20 scottr tleft = 0;
114 1.20 scottr continue;
115 1.20 scottr }
116 1.20 scottr
117 1.1 brezak tlast = t;
118 1.1 brezak }
119 1.1 brezak
120 1.4 mycroft /* Try to get a packet and process it. */
121 1.4 mycroft cc = (*rproc)(d, rbuf, rsize, tleft);
122 1.4 mycroft /* Return on data, EOF or real error. */
123 1.4 mycroft if (cc != -1 || errno != 0)
124 1.4 mycroft return (cc);
125 1.4 mycroft
126 1.1 brezak /* Timed out or didn't get the packet we're waiting for */
127 1.1 brezak t = getsecs();
128 1.1 brezak tleft -= t - tlast;
129 1.1 brezak tlast = t;
130 1.1 brezak }
131 1.1 brezak }
132