ctl_transact.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* $NetBSD: ctl_transact.c,v 1.6 2003/06/11 12:00:22 wiz Exp $ */
  2. /*
  3. * Copyright (c) 1983-2003, Regents of the University of California.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * + Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * + Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * + Neither the name of the University of California, San Francisco nor
  16. * the names of its contributors may be used to endorse or promote
  17. * products derived from this software without specific prior written
  18. * permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  21. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  23. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include "bsd.h"
  33. #if defined(TALK_43) || defined(TALK_42)
  34. #include <sys/cdefs.h>
  35. #ifndef lint
  36. #if 0
  37. static char sccsid[] = "@(#)ctl_transact.c 5.2 (Berkeley) 3/13/86";
  38. #else
  39. __RCSID("$NetBSD: ctl_transact.c,v 1.6 2003/06/11 12:00:22 wiz Exp $");
  40. #endif
  41. #endif /* not lint */
  42. #include <sys/time.h>
  43. #include <unistd.h>
  44. #include "hunt.h"
  45. #include "talk_ctl.h"
  46. #define CTL_WAIT 2 /* time to wait for a response, in seconds */
  47. #define MAX_RETRY 5
  48. /*
  49. * SOCKDGRAM is unreliable, so we must repeat messages if we have
  50. * not received an acknowledgement within a reasonable amount
  51. * of time
  52. */
  53. void
  54. ctl_transact(target, msg, type, rp)
  55. struct in_addr target;
  56. CTL_MSG msg;
  57. int type;
  58. CTL_RESPONSE *rp;
  59. {
  60. struct pollfd set[1];
  61. int nready, cc, retries;
  62. nready = 0;
  63. msg.type = type;
  64. daemon_addr.sin_addr = target;
  65. daemon_addr.sin_port = daemon_port;
  66. set[0].fd = ctl_sockt;
  67. set[0].events = POLLIN;
  68. /*
  69. * Keep sending the message until a response of
  70. * the proper type is obtained.
  71. */
  72. do {
  73. /* resend message until a response is obtained */
  74. for (retries = MAX_RETRY; retries > 0; retries -= 1) {
  75. cc = sendto(ctl_sockt, (char *)&msg, sizeof (msg), 0,
  76. &daemon_addr, sizeof (daemon_addr));
  77. if (cc != sizeof (msg)) {
  78. if (errno == EINTR)
  79. continue;
  80. p_error("Error on write to talk daemon");
  81. }
  82. nready = poll(set, 1, CTL_WAIT * 1000);
  83. if (nready < 0) {
  84. if (errno == EINTR)
  85. continue;
  86. p_error("Error waiting for daemon response");
  87. }
  88. if (nready != 0)
  89. break;
  90. }
  91. if (retries <= 0)
  92. break;
  93. /*
  94. * Keep reading while there are queued messages
  95. * (this is not necessary, it just saves extra
  96. * request/acknowledgements being sent)
  97. */
  98. do {
  99. cc = recv(ctl_sockt, (char *)rp, sizeof (*rp), 0);
  100. if (cc < 0) {
  101. if (errno == EINTR)
  102. continue;
  103. p_error("Error on read from talk daemon");
  104. }
  105. /* an immediate poll */
  106. nready = poll(set, 1, 0);
  107. } while (nready > 0 && (
  108. #ifdef TALK_43
  109. rp->vers != TALK_VERSION ||
  110. #endif
  111. rp->type != type));
  112. } while (
  113. #ifdef TALK_43
  114. rp->vers != TALK_VERSION ||
  115. #endif
  116. rp->type != type);
  117. rp->id_num = ntohl(rp->id_num);
  118. #ifdef TALK_43
  119. rp->addr.sa_family = ntohs(rp->addr.sa_family);
  120. # else
  121. rp->addr.sin_family = ntohs(rp->addr.sin_family);
  122. # endif
  123. }
  124. #endif