Commit | Line | Data |
---|---|---|
47a711ee | 1 | /* |
03b1f474 | 2 | * Copyright (c) 1985 Regents of the University of California. |
26b5a830 KB |
3 | * All rights reserved. |
4 | * | |
1c15e888 C |
5 | * Redistribution and use in source and binary forms are permitted provided |
6 | * that: (1) source distributions retain this entire copyright notice and | |
7 | * comment, and (2) distributions including binaries display the following | |
8 | * acknowledgement: ``This product includes software developed by the | |
9 | * University of California, Berkeley and its contributors'' in the | |
10 | * documentation or other materials provided with the distribution and in | |
11 | * all advertising materials mentioning features or use of this software. | |
12 | * Neither the name of the University nor the names of its contributors may | |
13 | * be used to endorse or promote products derived from this software without | |
14 | * specific prior written permission. | |
15 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED | |
16 | * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF | |
17 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
47a711ee RG |
18 | */ |
19 | ||
20 | #ifndef lint | |
1c15e888 | 21 | static char sccsid[] = "@(#)acksend.c 2.7 (Berkeley) 6/1/90"; |
26b5a830 | 22 | #endif /* not lint */ |
47a711ee RG |
23 | |
24 | #include "globals.h" | |
25 | #include <protocols/timed.h> | |
26 | ||
27 | #define RECEIVED 0 | |
28 | #define LOST 1 | |
29 | #define SECFORACK 1 /* seconds */ | |
30 | #define USECFORACK 0 /* microseconds */ | |
31 | #define MAXCOUNT 5 | |
32 | ||
33 | struct tsp *answer; | |
47a711ee RG |
34 | |
35 | /* | |
36 | * Acksend implements reliable datagram transmission by using sequence | |
37 | * numbers and retransmission when necessary. | |
75d5ce8a JB |
38 | * `name' is the name of the destination |
39 | * `addr' is the address to send to | |
47a711ee RG |
40 | * If `name' is ANYADDR, this routine implements reliable broadcast. |
41 | */ | |
42 | ||
75d5ce8a | 43 | struct tsp *acksend(message, addr, name, ack, net) |
47a711ee | 44 | struct tsp *message; |
75d5ce8a | 45 | struct sockaddr_in *addr; |
47a711ee RG |
46 | char *name; |
47 | int ack; | |
75d5ce8a | 48 | struct netinfo *net; |
47a711ee RG |
49 | { |
50 | int count; | |
51 | int flag; | |
52 | extern u_short sequence; | |
53 | struct timeval tout; | |
54 | struct tsp *readmsg(); | |
47a711ee RG |
55 | |
56 | count = 0; | |
57 | ||
47c5ee96 JB |
58 | message->tsp_vers = TSPVERSION; |
59 | message->tsp_seq = sequence; | |
60 | if (trace) { | |
61 | fprintf(fd, "acksend: "); | |
62 | if (name == ANYADDR) | |
63 | fprintf(fd, "broadcast: "); | |
64 | else | |
65 | fprintf(fd, "%s: ", name); | |
9a957f87 | 66 | print(message, addr); |
47c5ee96 JB |
67 | } |
68 | bytenetorder(message); | |
47a711ee | 69 | do { |
75d5ce8a JB |
70 | if (sendto(sock, (char *)message, sizeof(struct tsp), 0, addr, |
71 | sizeof(struct sockaddr_in)) < 0) { | |
72 | syslog(LOG_ERR, "acksend: sendto: %m"); | |
73 | exit(1); | |
47a711ee RG |
74 | } |
75 | tout.tv_sec = SECFORACK; | |
76 | tout.tv_usec = USECFORACK; | |
75d5ce8a | 77 | answer = readmsg(ack, name, &tout, net); |
47a711ee | 78 | if (answer != NULL) { |
557d93cb JB |
79 | if (answer->tsp_seq != sequence) { |
80 | if (trace) | |
81 | fprintf(fd, "acksend: seq # %d != %d\n", | |
82 | answer->tsp_seq, sequence); | |
83 | continue; | |
84 | } | |
47a711ee RG |
85 | flag = RECEIVED; |
86 | } else { | |
87 | flag = LOST; | |
88 | if (++count == MAXCOUNT) { | |
89 | break; | |
90 | } | |
91 | } | |
92 | } while (flag != RECEIVED); | |
03b1f474 | 93 | sequence++; |
47a711ee RG |
94 | return(answer); |
95 | } |