Home | History | Annotate | Line # | Download | only in libsa
net.c revision 1.28.16.1
      1  1.28.16.1      yamt /*	$NetBSD: net.c,v 1.28.16.1 2006/06/21 15:10:23 yamt 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.28.16.1      yamt #ifdef _STANDALONE
     59        1.1    brezak #include "stand.h"
     60  1.28.16.1      yamt #define delay()
     61  1.28.16.1      yamt #else
     62  1.28.16.1      yamt #include <errno.h>
     63  1.28.16.1      yamt #include <stdio.h>
     64  1.28.16.1      yamt #include <unistd.h>
     65  1.28.16.1      yamt #define panic printf
     66  1.28.16.1      yamt #define delay() usleep(100000)
     67  1.28.16.1      yamt #define getsecs() time(NULL)
     68  1.28.16.1      yamt #endif
     69  1.28.16.1      yamt 
     70        1.1    brezak #include "net.h"
     71        1.1    brezak 
     72        1.1    brezak /*
     73        1.1    brezak  * Send a packet and wait for a reply, with exponential backoff.
     74        1.1    brezak  *
     75       1.20    scottr  * The send routine must return the actual number of bytes written,
     76       1.20    scottr  * or -1 on error.
     77        1.1    brezak  *
     78        1.1    brezak  * The receive routine can indicate success by returning the number of
     79        1.1    brezak  * bytes read; it can return 0 to indicate EOF; it can return -1 with a
     80        1.1    brezak  * non-zero errno to indicate failure; finally, it can return -1 with a
     81        1.1    brezak  * zero errno to indicate it isn't done yet.
     82        1.1    brezak  */
     83        1.6        pk ssize_t
     84        1.1    brezak sendrecv(d, sproc, sbuf, ssize, rproc, rbuf, rsize)
     85       1.26  augustss 	struct iodesc *d;
     86       1.26  augustss 	ssize_t (*sproc)(struct iodesc *, void *, size_t);
     87       1.26  augustss 	void *sbuf;
     88       1.26  augustss 	size_t ssize;
     89       1.26  augustss 	ssize_t (*rproc)(struct iodesc *, void *, size_t, time_t);
     90       1.26  augustss 	void *rbuf;
     91       1.26  augustss 	size_t rsize;
     92        1.1    brezak {
     93       1.26  augustss 	ssize_t cc;
     94       1.26  augustss 	time_t t, tmo, tlast;
     95       1.12        pk 	long tleft;
     96        1.1    brezak 
     97        1.1    brezak #ifdef NET_DEBUG
     98        1.1    brezak 	if (debug)
     99       1.14  christos 		printf("sendrecv: called\n");
    100        1.1    brezak #endif
    101        1.4   mycroft 
    102        1.1    brezak 	tmo = MINTMO;
    103        1.1    brezak 	tlast = tleft = 0;
    104        1.1    brezak 	t = getsecs();
    105        1.1    brezak 	for (;;) {
    106        1.1    brezak 		if (tleft <= 0) {
    107       1.12        pk 			if (tmo >= MAXTMO) {
    108        1.8        pk 				errno = ETIMEDOUT;
    109        1.8        pk 				return -1;
    110        1.8        pk 			}
    111        1.1    brezak 			cc = (*sproc)(d, sbuf, ssize);
    112        1.1    brezak 
    113        1.1    brezak 			tleft = tmo;
    114        1.1    brezak 			tmo <<= 1;
    115        1.1    brezak 			if (tmo > MAXTMO)
    116        1.1    brezak 				tmo = MAXTMO;
    117       1.20    scottr 
    118       1.20    scottr 			if (cc == -1) {
    119       1.20    scottr 				/* Error on transmit; wait before retrying */
    120  1.28.16.1      yamt 				while ((getsecs() - t) < tmo)
    121  1.28.16.1      yamt 					delay();
    122       1.20    scottr 				tleft = 0;
    123       1.20    scottr 				continue;
    124       1.20    scottr 			}
    125  1.28.16.1      yamt 			if (cc < ssize)
    126  1.28.16.1      yamt 				panic("sendrecv: short write! (%zd < %zd)",
    127  1.28.16.1      yamt 				    cc, ssize);
    128       1.20    scottr 
    129        1.1    brezak 			tlast = t;
    130        1.1    brezak 		}
    131        1.1    brezak 
    132        1.4   mycroft 		/* Try to get a packet and process it. */
    133        1.4   mycroft 		cc = (*rproc)(d, rbuf, rsize, tleft);
    134        1.4   mycroft 		/* Return on data, EOF or real error. */
    135        1.4   mycroft 		if (cc != -1 || errno != 0)
    136        1.4   mycroft 			return (cc);
    137        1.4   mycroft 
    138        1.1    brezak 		/* Timed out or didn't get the packet we're waiting for */
    139        1.1    brezak 		t = getsecs();
    140        1.1    brezak 		tleft -= t - tlast;
    141        1.1    brezak 		tlast = t;
    142        1.1    brezak 	}
    143        1.1    brezak }
    144