11553Srgrimes/*-
21553Srgrimes * Copyright (c) 1985, 1993
31553Srgrimes *	The Regents of the University of California.  All rights reserved.
41553Srgrimes *
51553Srgrimes * Redistribution and use in source and binary forms, with or without
61553Srgrimes * modification, are permitted provided that the following conditions
71553Srgrimes * are met:
81553Srgrimes * 1. Redistributions of source code must retain the above copyright
91553Srgrimes *    notice, this list of conditions and the following disclaimer.
101553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111553Srgrimes *    notice, this list of conditions and the following disclaimer in the
121553Srgrimes *    documentation and/or other materials provided with the distribution.
131553Srgrimes * 4. Neither the name of the University nor the names of its contributors
141553Srgrimes *    may be used to endorse or promote products derived from this software
151553Srgrimes *    without specific prior written permission.
161553Srgrimes *
171553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201553Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271553Srgrimes * SUCH DAMAGE.
281553Srgrimes */
291553Srgrimes
301553Srgrimes#ifndef lint
3130642Scharnier#if 0
321553Srgrimesstatic char sccsid[] = "@(#)acksend.c	8.1 (Berkeley) 6/6/93";
3330642Scharnier#endif
3430642Scharnierstatic const char rcsid[] =
3550479Speter  "$FreeBSD$";
361553Srgrimes#endif /* not lint */
371553Srgrimes
381553Srgrimes#include "globals.h"
391553Srgrimes
401553Srgrimesstruct tsp *answer;
411553Srgrimes
421553Srgrimesextern u_short sequence;
431553Srgrimes
441553Srgrimesvoid
451553Srgrimesxmit(type, seq, addr)
461553Srgrimes	int type;
471553Srgrimes	u_int seq;
481553Srgrimes	struct sockaddr_in *addr;
491553Srgrimes{
501553Srgrimes	static struct tsp msg;
511553Srgrimes
521553Srgrimes	msg.tsp_type = type;
531553Srgrimes	msg.tsp_seq = seq;
541553Srgrimes	msg.tsp_vers = TSPVERSION;
5530830Scharnier	(void)strcpy(msg.tsp_name, hostname);
561553Srgrimes	bytenetorder(&msg);
571553Srgrimes	if (sendto(sock, (char *)&msg, sizeof(struct tsp), 0,
581553Srgrimes		   (struct sockaddr*)addr, sizeof(struct sockaddr)) < 0) {
591553Srgrimes		trace_sendto_err(addr->sin_addr);
601553Srgrimes	}
611553Srgrimes}
621553Srgrimes
631553Srgrimes
641553Srgrimes/*
651553Srgrimes * Acksend implements reliable datagram transmission by using sequence
661553Srgrimes * numbers and retransmission when necessary.
671553Srgrimes * If `name' is ANYADDR, this routine implements reliable broadcast.
681553Srgrimes *
691553Srgrimes * Because this function calls readmsg(), none of its args may be in
701553Srgrimes *	a message provided by readmsg().
711553Srgrimes */
721553Srgrimesstruct tsp *
731553Srgrimesacksend(message, addr, name, ack, net, bad)
741553Srgrimes	struct tsp *message;			/* this message */
751553Srgrimes	struct sockaddr_in *addr;		/* to here */
761553Srgrimes	char *name;
771553Srgrimes	int ack;				/* look for this ack */
781553Srgrimes	struct netinfo *net;			/* receive from this network */
791553Srgrimes	int bad;				/* 1=losing patience */
801553Srgrimes{
811553Srgrimes	struct timeval twait;
821553Srgrimes	int count;
831553Srgrimes	long msec;
841553Srgrimes
851553Srgrimes	message->tsp_vers = TSPVERSION;
861553Srgrimes	message->tsp_seq = sequence;
871553Srgrimes	if (trace) {
881553Srgrimes		fprintf(fd, "acksend: to %s: ",
891553Srgrimes			(name == ANYADDR ? "broadcast" : name));
901553Srgrimes		print(message, addr);
911553Srgrimes	}
921553Srgrimes	bytenetorder(message);
931553Srgrimes
941553Srgrimes	msec = 200;
951553Srgrimes	count = bad ? 1 : 5;	/* 5 packets in 6.4 seconds */
961553Srgrimes	answer = 0;
971553Srgrimes	do {
981553Srgrimes		if (!answer) {
991553Srgrimes			/* do not go crazy transmitting just because the
1001553Srgrimes			 * other guy cannot keep our sequence numbers
1011553Srgrimes			 * straight.
1021553Srgrimes			 */
1031553Srgrimes			if (sendto(sock, (char *)message, sizeof(struct tsp),
1041553Srgrimes				   0, (struct sockaddr*)addr,
1051553Srgrimes				   sizeof(struct sockaddr)) < 0) {
1061553Srgrimes				trace_sendto_err(addr->sin_addr);
1071553Srgrimes				break;
1081553Srgrimes			}
1091553Srgrimes		}
1101553Srgrimes
1111553Srgrimes		mstotvround(&twait, msec);
1121553Srgrimes		answer  = readmsg(ack, name, &twait, net);
1131553Srgrimes		if (answer != 0) {
1141553Srgrimes			if (answer->tsp_seq != sequence) {
1151553Srgrimes				if (trace)
1161553Srgrimes					fprintf(fd,"acksend: seq # %u!=%u\n",
1171553Srgrimes						answer->tsp_seq, sequence);
1181553Srgrimes				continue;
1191553Srgrimes			}
1201553Srgrimes			break;
1211553Srgrimes		}
1221553Srgrimes
1231553Srgrimes		msec *= 2;
1241553Srgrimes	} while (--count > 0);
1251553Srgrimes	sequence++;
1261553Srgrimes
1271553Srgrimes	return(answer);
1281553Srgrimes}
129