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
45246209Scharnierxmit(int type, u_int seq, struct sockaddr_in *addr)
461553Srgrimes{
471553Srgrimes	static struct tsp msg;
481553Srgrimes
491553Srgrimes	msg.tsp_type = type;
501553Srgrimes	msg.tsp_seq = seq;
511553Srgrimes	msg.tsp_vers = TSPVERSION;
5230830Scharnier	(void)strcpy(msg.tsp_name, hostname);
531553Srgrimes	bytenetorder(&msg);
541553Srgrimes	if (sendto(sock, (char *)&msg, sizeof(struct tsp), 0,
551553Srgrimes		   (struct sockaddr*)addr, sizeof(struct sockaddr)) < 0) {
561553Srgrimes		trace_sendto_err(addr->sin_addr);
571553Srgrimes	}
581553Srgrimes}
591553Srgrimes
601553Srgrimes
611553Srgrimes/*
621553Srgrimes * Acksend implements reliable datagram transmission by using sequence
631553Srgrimes * numbers and retransmission when necessary.
641553Srgrimes * If `name' is ANYADDR, this routine implements reliable broadcast.
651553Srgrimes *
661553Srgrimes * Because this function calls readmsg(), none of its args may be in
671553Srgrimes *	a message provided by readmsg().
68246209Scharnier * message		this message
69246209Scharnier * addr			to here
70246209Scharnier * ack			look for this ack
71246209Scharnier * net			receive from this network
72246209Scharnier * bad			1=losing patience
731553Srgrimes */
741553Srgrimesstruct tsp *
75246209Scharnieracksend(struct tsp *message, struct sockaddr_in *addr, char *name,
76246209Scharnier	int ack, struct netinfo *net, int bad)
771553Srgrimes{
781553Srgrimes	struct timeval twait;
791553Srgrimes	int count;
801553Srgrimes	long msec;
811553Srgrimes
821553Srgrimes	message->tsp_vers = TSPVERSION;
831553Srgrimes	message->tsp_seq = sequence;
841553Srgrimes	if (trace) {
851553Srgrimes		fprintf(fd, "acksend: to %s: ",
861553Srgrimes			(name == ANYADDR ? "broadcast" : name));
871553Srgrimes		print(message, addr);
881553Srgrimes	}
891553Srgrimes	bytenetorder(message);
901553Srgrimes
911553Srgrimes	msec = 200;
921553Srgrimes	count = bad ? 1 : 5;	/* 5 packets in 6.4 seconds */
93299707Spfg	answer = NULL;
941553Srgrimes	do {
951553Srgrimes		if (!answer) {
961553Srgrimes			/* do not go crazy transmitting just because the
971553Srgrimes			 * other guy cannot keep our sequence numbers
981553Srgrimes			 * straight.
991553Srgrimes			 */
1001553Srgrimes			if (sendto(sock, (char *)message, sizeof(struct tsp),
1011553Srgrimes				   0, (struct sockaddr*)addr,
1021553Srgrimes				   sizeof(struct sockaddr)) < 0) {
1031553Srgrimes				trace_sendto_err(addr->sin_addr);
1041553Srgrimes				break;
1051553Srgrimes			}
1061553Srgrimes		}
1071553Srgrimes
1081553Srgrimes		mstotvround(&twait, msec);
1091553Srgrimes		answer  = readmsg(ack, name, &twait, net);
110299707Spfg		if (answer != NULL) {
1111553Srgrimes			if (answer->tsp_seq != sequence) {
1121553Srgrimes				if (trace)
1131553Srgrimes					fprintf(fd,"acksend: seq # %u!=%u\n",
1141553Srgrimes						answer->tsp_seq, sequence);
1151553Srgrimes				continue;
1161553Srgrimes			}
1171553Srgrimes			break;
1181553Srgrimes		}
1191553Srgrimes
1201553Srgrimes		msec *= 2;
1211553Srgrimes	} while (--count > 0);
1221553Srgrimes	sequence++;
1231553Srgrimes
1241553Srgrimes	return(answer);
1251553Srgrimes}
126