Source code pulled from OpenBSD for OpenNTPD. The place to contribute to this code is via the OpenBSD CVS tree.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
3.7 KiB

  1. /* $OpenBSD: buffer.c,v 1.2 2004/07/12 09:22:38 dtucker Exp $ */
  2. /*
  3. * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  15. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/types.h>
  18. #include <sys/uio.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <errno.h>
  22. #include <limits.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include "ntpd.h"
  26. int buf_write(int, struct buf *);
  27. void buf_enqueue(struct msgbuf *, struct buf *);
  28. void buf_dequeue(struct msgbuf *, struct buf *);
  29. struct buf *
  30. buf_open(ssize_t len)
  31. {
  32. struct buf *buf;
  33. if ((buf = calloc(1, sizeof(struct buf))) == NULL)
  34. return (NULL);
  35. if ((buf->buf = malloc(len)) == NULL) {
  36. free(buf);
  37. return (NULL);
  38. }
  39. buf->size = len;
  40. return (buf);
  41. }
  42. int
  43. buf_add(struct buf *buf, void *data, ssize_t len)
  44. {
  45. if (buf->wpos + len > buf->size)
  46. return (-1);
  47. memcpy(buf->buf + buf->wpos, data, len);
  48. buf->wpos += len;
  49. return (0);
  50. }
  51. int
  52. buf_close(struct msgbuf *msgbuf, struct buf *buf)
  53. {
  54. buf_enqueue(msgbuf, buf);
  55. return (1);
  56. }
  57. int
  58. buf_write(int sock, struct buf *buf)
  59. {
  60. ssize_t n;
  61. if ((n = write(sock, buf->buf + buf->rpos,
  62. buf->size - buf->rpos)) == -1) {
  63. if (errno == EAGAIN) /* cannot write immediately */
  64. return (0);
  65. else
  66. return (-1);
  67. }
  68. if (n == 0) { /* connection closed */
  69. errno = 0;
  70. return (-2);
  71. }
  72. if (n < buf->size - buf->rpos) { /* not all data written yet */
  73. buf->rpos += n;
  74. return (0);
  75. } else
  76. return (1);
  77. }
  78. void
  79. buf_free(struct buf *buf)
  80. {
  81. free(buf->buf);
  82. free(buf);
  83. }
  84. void
  85. msgbuf_init(struct msgbuf *msgbuf)
  86. {
  87. msgbuf->queued = 0;
  88. msgbuf->fd = -1;
  89. TAILQ_INIT(&msgbuf->bufs);
  90. }
  91. void
  92. msgbuf_clear(struct msgbuf *msgbuf)
  93. {
  94. struct buf *buf;
  95. while ((buf = TAILQ_FIRST(&msgbuf->bufs)) != NULL)
  96. buf_dequeue(msgbuf, buf);
  97. }
  98. int
  99. msgbuf_write(struct msgbuf *msgbuf)
  100. {
  101. /*
  102. * possible race here
  103. * when we cannot write out data completely from a buffer,
  104. * we MUST return and NOT try to write out stuff from later buffers -
  105. * the socket might have become writeable again
  106. */
  107. struct iovec iov[IOV_MAX];
  108. struct buf *buf, *next;
  109. int i = 0;
  110. ssize_t n;
  111. bzero(&iov, sizeof(iov));
  112. TAILQ_FOREACH(buf, &msgbuf->bufs, entries) {
  113. if (i >= IOV_MAX)
  114. break;
  115. iov[i].iov_base = buf->buf + buf->rpos;
  116. iov[i].iov_len = buf->size - buf->rpos;
  117. i++;
  118. }
  119. if ((n = writev(msgbuf->fd, iov, i)) == -1) {
  120. if (errno == EAGAIN) /* cannot write immediately */
  121. return (0);
  122. else
  123. return (-1);
  124. }
  125. if (n == 0) { /* connection closed */
  126. errno = 0;
  127. return (-2);
  128. }
  129. for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0;
  130. buf = next) {
  131. next = TAILQ_NEXT(buf, entries);
  132. if (n >= buf->size - buf->rpos) {
  133. n -= buf->size - buf->rpos;
  134. buf_dequeue(msgbuf, buf);
  135. } else {
  136. buf->rpos += n;
  137. n = 0;
  138. }
  139. }
  140. return (0);
  141. }
  142. void
  143. buf_enqueue(struct msgbuf *msgbuf, struct buf *buf)
  144. {
  145. TAILQ_INSERT_TAIL(&msgbuf->bufs, buf, entries);
  146. msgbuf->queued++;
  147. }
  148. void
  149. buf_dequeue(struct msgbuf *msgbuf, struct buf *buf)
  150. {
  151. TAILQ_REMOVE(&msgbuf->bufs, buf, entries);
  152. msgbuf->queued--;
  153. buf_free(buf);
  154. }