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.

155 lines
3.2 KiB

  1. /* $OpenBSD: buffer.c,v 1.4 2004/09/15 00:05:29 henning 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 <errno.h>
  20. #include <limits.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include "ntpd.h"
  26. void buf_enqueue(struct msgbuf *, struct buf *);
  27. void buf_dequeue(struct msgbuf *, struct buf *);
  28. struct buf *
  29. buf_open(ssize_t len)
  30. {
  31. struct buf *buf;
  32. if ((buf = calloc(1, sizeof(struct buf))) == NULL)
  33. return (NULL);
  34. if ((buf->buf = malloc(len)) == NULL) {
  35. free(buf);
  36. return (NULL);
  37. }
  38. buf->size = len;
  39. return (buf);
  40. }
  41. int
  42. buf_add(struct buf *buf, void *data, ssize_t len)
  43. {
  44. if (buf->wpos + len > buf->size)
  45. return (-1);
  46. memcpy(buf->buf + buf->wpos, data, len);
  47. buf->wpos += len;
  48. return (0);
  49. }
  50. int
  51. buf_close(struct msgbuf *msgbuf, struct buf *buf)
  52. {
  53. buf_enqueue(msgbuf, buf);
  54. return (1);
  55. }
  56. void
  57. buf_free(struct buf *buf)
  58. {
  59. free(buf->buf);
  60. free(buf);
  61. }
  62. void
  63. msgbuf_init(struct msgbuf *msgbuf)
  64. {
  65. msgbuf->queued = 0;
  66. msgbuf->fd = -1;
  67. TAILQ_INIT(&msgbuf->bufs);
  68. }
  69. void
  70. msgbuf_clear(struct msgbuf *msgbuf)
  71. {
  72. struct buf *buf;
  73. while ((buf = TAILQ_FIRST(&msgbuf->bufs)) != NULL)
  74. buf_dequeue(msgbuf, buf);
  75. }
  76. int
  77. msgbuf_write(struct msgbuf *msgbuf)
  78. {
  79. /*
  80. * possible race here
  81. * when we cannot write out data completely from a buffer,
  82. * we MUST return and NOT try to write out stuff from later buffers -
  83. * the socket might have become writeable again
  84. */
  85. struct iovec iov[IOV_MAX];
  86. struct buf *buf, *next;
  87. int i = 0;
  88. ssize_t n;
  89. bzero(&iov, sizeof(iov));
  90. TAILQ_FOREACH(buf, &msgbuf->bufs, entries) {
  91. if (i >= IOV_MAX)
  92. break;
  93. iov[i].iov_base = buf->buf + buf->rpos;
  94. iov[i].iov_len = buf->size - buf->rpos;
  95. i++;
  96. }
  97. if ((n = writev(msgbuf->fd, iov, i)) == -1) {
  98. if (errno == EAGAIN) /* cannot write immediately */
  99. return (0);
  100. else
  101. return (-1);
  102. }
  103. if (n == 0) { /* connection closed */
  104. errno = 0;
  105. return (-2);
  106. }
  107. for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0;
  108. buf = next) {
  109. next = TAILQ_NEXT(buf, entries);
  110. if (n >= buf->size - buf->rpos) {
  111. n -= buf->size - buf->rpos;
  112. buf_dequeue(msgbuf, buf);
  113. } else {
  114. buf->rpos += n;
  115. n = 0;
  116. }
  117. }
  118. return (0);
  119. }
  120. void
  121. buf_enqueue(struct msgbuf *msgbuf, struct buf *buf)
  122. {
  123. TAILQ_INSERT_TAIL(&msgbuf->bufs, buf, entries);
  124. msgbuf->queued++;
  125. }
  126. void
  127. buf_dequeue(struct msgbuf *msgbuf, struct buf *buf)
  128. {
  129. TAILQ_REMOVE(&msgbuf->bufs, buf, entries);
  130. msgbuf->queued--;
  131. buf_free(buf);
  132. }