add support for arbitrary number of utmp entries
[unix-history] / usr / src / usr.bin / talk / ctl_transact.c
CommitLineData
963ce42e
MK
1#ifndef lint
2static char sccsid[] = "@(#)ctl_transact.c 1.2 (Berkeley) %G%";
3#endif
4
0328946e
MK
5#include "talk_ctl.h"
6#include <sys/time.h>
7
963ce42e 8#define CTL_WAIT 2 /* time to wait for a response, in seconds */
0328946e 9
963ce42e
MK
10/*
11 * SOCKDGRAM is unreliable, so we must repeat messages if we have
12 * not recieved an acknowledgement within a reasonable amount
13 * of time
14 */
0328946e 15ctl_transact(target, msg, type, response)
963ce42e
MK
16 struct in_addr target;
17 CTL_MSG msg;
18 int type;
19 CTL_RESPONSE *response;
0328946e 20{
963ce42e
MK
21 struct sockaddr junk;
22 int read_mask;
23 int ctl_mask;
24 int nready;
25 int cc;
26 int junk_size;
27 struct timeval wait;
28
29 wait.tv_sec = CTL_WAIT;
30 wait.tv_usec = 0;
31 msg.type = type;
32 daemon_addr.sin_addr = target;
33 daemon_addr.sin_port = daemon_port;
34 ctl_mask = 1 << ctl_sockt;
0328946e
MK
35
36 /*
37 * keep sending the message until a response of the right
38 * type is obtained
39 */
0328946e 40 do {
963ce42e
MK
41 /* keep sending the message until a response is obtained */
42 do {
43 cc = sendto(ctl_sockt, (char *)&msg, sizeof(CTL_MSG), 0,
44 &daemon_addr, sizeof(daemon_addr));
45 if (cc != sizeof(CTL_MSG)) {
46 if (errno == EINTR)
47 continue;
48 p_error("Error on write to talk daemon");
49 }
50 read_mask = ctl_mask;
51 nready = select(32, &read_mask, 0, 0, &wait);
52 while (nready < 0) {
53 if (errno == EINTR)
54 continue;
55 p_error("Error waiting for daemon response");
56 }
57 } while (nready == 0);
58 /* keep reading while there are queued messages
59 (this is not necessary, it just saves extra
60 request/acknowledgements being sent)
61 */
62 do {
63 junk_size = sizeof(junk);
64 cc = recvfrom(ctl_sockt, (char *)response,
65 sizeof (CTL_RESPONSE), 0, &junk, &junk_size);
66 if (cc < 0) {
67 if (errno == EINTR)
68 continue;
69 p_error("Error on read from talk daemon");
70 }
71 read_mask = ctl_mask;
72 /* an immediate poll */
73 timerclear(&wait);
74 nready = select(32, &read_mask, 0, 0, &wait);
75 } while (nready > 0 && response->type != type);
76 } while (response->type != type);
0328946e 77}