1/* $NetBSD: acksend.c,v 1.10 2007/01/25 23:25:20 cbiere Exp $ */ 2 3/*- 4 * Copyright (c) 1985, 1993 The Regents of the University of California. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32#include <sys/cdefs.h> 33#ifndef lint 34#if 0 35static char sccsid[] = "@(#)acksend.c 8.1 (Berkeley) 6/6/93"; 36#else 37__RCSID("$NetBSD: acksend.c,v 1.10 2007/01/25 23:25:20 cbiere Exp $"); 38#endif 39#endif /* not lint */ 40 41#include "globals.h" 42 43 44struct tsp *answer; 45 46extern u_short sequence; 47 48void 49xmit(int type, u_short seq, struct sockaddr_in *addr) 50{ 51 static struct tsp msg; 52 53 msg.tsp_type = type; 54 msg.tsp_seq = seq; 55 msg.tsp_vers = TSPVERSION; 56 57 set_tsp_name(&msg, hostname); 58 bytenetorder(&msg); 59 (void)sendtsp(sock, &msg, addr); 60} 61 62int 63sendtsp(int s, struct tsp *msg, struct sockaddr_in *addr) 64{ 65 int error; 66 67 error = sendto(s, msg, sizeof(*msg), 0, 68 (struct sockaddr *)(void *)addr, sizeof(*addr)); 69 if (error == -1) 70 trace_sendto_err(addr->sin_addr); 71 return error; 72} 73 74 75/* 76 * Acksend implements reliable datagram transmission by using sequence 77 * numbers and retransmission when necessary. 78 * If `name' is ANYADDR, this routine implements reliable broadcast. 79 * 80 * Because this function calls readmsg(), none of its args may be in 81 * a message provided by readmsg(). 82 */ 83struct tsp * 84acksend(struct tsp *message, /* this message */ 85 struct sockaddr_in *addr, char *name, /* to here */ 86 int ack, /* look for this ack */ 87 struct netinfo *net, /* receive from this network */ 88 int bad) /* 1=losing patience */ 89{ 90 struct timeval twait; 91 int count; 92 long msec; 93 94 message->tsp_vers = TSPVERSION; 95 message->tsp_seq = sequence; 96 if (trace) { 97 fprintf(fd, "acksend: to %s: ", 98 (name == ANYADDR ? "broadcast" : name)); 99 print(message, addr); 100 } 101 bytenetorder(message); 102 103 msec = 200; 104 count = bad ? 1 : 5; /* 5 packets in 6.4 seconds */ 105 answer = 0; 106 do { 107 if (!answer) { 108 /* do not go crazy transmitting just because the 109 * other guy cannot keep our sequence numbers 110 * straight. 111 */ 112 if (sendtsp(sock, message, addr) == -1) 113 break; 114 } 115 116 mstotvround(&twait, msec); 117 answer = readmsg(ack, name, &twait, net); 118 if (answer != 0) { 119 if (answer->tsp_seq != sequence) { 120 if (trace) 121 fprintf(fd,"acksend: seq # %u!=%u\n", 122 answer->tsp_seq, sequence); 123 continue; 124 } 125 break; 126 } 127 128 msec *= 2; 129 } while (--count > 0); 130 sequence++; 131 132 return(answer); 133} 134