Commit | Line | Data |
---|---|---|
d0aeaf5a DF |
1 | /* |
2 | * Copyright (c) 1983 Regents of the University of California. | |
f9ac90b4 KB |
3 | * All rights reserved. |
4 | * | |
5 | * Redistribution and use in source and binary forms are permitted | |
b36fc510 KB |
6 | * provided that the above copyright notice and this paragraph are |
7 | * duplicated in all such forms and that any documentation, | |
8 | * advertising materials, and other materials related to such | |
9 | * distribution and use acknowledge that the software was developed | |
10 | * by the University of California, Berkeley. The name of the | |
11 | * University may not be used to endorse or promote products derived | |
12 | * from this software without specific prior written permission. | |
13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
15 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
d0aeaf5a DF |
16 | */ |
17 | ||
963ce42e | 18 | #ifndef lint |
b36fc510 | 19 | static char sccsid[] = "@(#)ctl_transact.c 5.4 (Berkeley) %G%"; |
f9ac90b4 | 20 | #endif /* not lint */ |
963ce42e | 21 | |
0328946e MK |
22 | #include "talk_ctl.h" |
23 | #include <sys/time.h> | |
24 | ||
963ce42e | 25 | #define CTL_WAIT 2 /* time to wait for a response, in seconds */ |
0328946e | 26 | |
963ce42e MK |
27 | /* |
28 | * SOCKDGRAM is unreliable, so we must repeat messages if we have | |
29 | * not recieved an acknowledgement within a reasonable amount | |
30 | * of time | |
31 | */ | |
4fa2ac2d | 32 | ctl_transact(target, msg, type, rp) |
963ce42e MK |
33 | struct in_addr target; |
34 | CTL_MSG msg; | |
35 | int type; | |
4fa2ac2d | 36 | CTL_RESPONSE *rp; |
0328946e | 37 | { |
4fa2ac2d | 38 | int read_mask, ctl_mask, nready, cc; |
963ce42e MK |
39 | struct timeval wait; |
40 | ||
963ce42e MK |
41 | msg.type = type; |
42 | daemon_addr.sin_addr = target; | |
43 | daemon_addr.sin_port = daemon_port; | |
44 | ctl_mask = 1 << ctl_sockt; | |
0328946e MK |
45 | |
46 | /* | |
4fa2ac2d KM |
47 | * Keep sending the message until a response of |
48 | * the proper type is obtained. | |
0328946e | 49 | */ |
0328946e | 50 | do { |
3f3b4df0 JB |
51 | wait.tv_sec = CTL_WAIT; |
52 | wait.tv_usec = 0; | |
4fa2ac2d | 53 | /* resend message until a response is obtained */ |
963ce42e | 54 | do { |
4fa2ac2d KM |
55 | cc = sendto(ctl_sockt, (char *)&msg, sizeof (msg), 0, |
56 | &daemon_addr, sizeof (daemon_addr)); | |
57 | if (cc != sizeof (msg)) { | |
963ce42e MK |
58 | if (errno == EINTR) |
59 | continue; | |
60 | p_error("Error on write to talk daemon"); | |
61 | } | |
62 | read_mask = ctl_mask; | |
4fa2ac2d KM |
63 | nready = select(32, &read_mask, 0, 0, &wait); |
64 | if (nready < 0) { | |
963ce42e MK |
65 | if (errno == EINTR) |
66 | continue; | |
67 | p_error("Error waiting for daemon response"); | |
68 | } | |
69 | } while (nready == 0); | |
4fa2ac2d KM |
70 | /* |
71 | * Keep reading while there are queued messages | |
72 | * (this is not necessary, it just saves extra | |
73 | * request/acknowledgements being sent) | |
963ce42e MK |
74 | */ |
75 | do { | |
4fa2ac2d | 76 | cc = recv(ctl_sockt, (char *)rp, sizeof (*rp), 0); |
963ce42e MK |
77 | if (cc < 0) { |
78 | if (errno == EINTR) | |
79 | continue; | |
80 | p_error("Error on read from talk daemon"); | |
81 | } | |
82 | read_mask = ctl_mask; | |
83 | /* an immediate poll */ | |
84 | timerclear(&wait); | |
85 | nready = select(32, &read_mask, 0, 0, &wait); | |
4fa2ac2d KM |
86 | } while (nready > 0 && (rp->vers != TALK_VERSION || |
87 | rp->type != type)); | |
88 | } while (rp->vers != TALK_VERSION || rp->type != type); | |
89 | rp->id_num = ntohl(rp->id_num); | |
90 | rp->addr.sa_family = ntohs(rp->addr.sa_family); | |
0328946e | 91 | } |