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