qnetd-poll-array.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (c) 2015-2016 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <sys/types.h>
  35. #include <arpa/inet.h>
  36. #include <inttypes.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include "qnetd-poll-array.h"
  40. void
  41. qnetd_poll_array_init(struct qnetd_poll_array *poll_array)
  42. {
  43. memset(poll_array, 0, sizeof(*poll_array));
  44. }
  45. void
  46. qnetd_poll_array_destroy(struct qnetd_poll_array *poll_array)
  47. {
  48. free(poll_array->array);
  49. qnetd_poll_array_init(poll_array);
  50. }
  51. void
  52. qnetd_poll_array_clean(struct qnetd_poll_array *poll_array)
  53. {
  54. poll_array->items = 0;
  55. }
  56. static int
  57. qnetd_poll_array_realloc(struct qnetd_poll_array *poll_array,
  58. unsigned int new_array_size)
  59. {
  60. PRPollDesc *new_array;
  61. new_array = realloc(poll_array->array,
  62. sizeof(PRPollDesc) * new_array_size);
  63. if (new_array == NULL) {
  64. return (-1);
  65. }
  66. poll_array->allocated = new_array_size;
  67. poll_array->array = new_array;
  68. return (0);
  69. }
  70. unsigned int
  71. qnetd_poll_array_size(struct qnetd_poll_array *poll_array)
  72. {
  73. return (poll_array->items);
  74. }
  75. PRPollDesc *
  76. qnetd_poll_array_add(struct qnetd_poll_array *poll_array)
  77. {
  78. if (qnetd_poll_array_size(poll_array) >= poll_array->allocated) {
  79. if (qnetd_poll_array_realloc(poll_array, (poll_array->allocated * 2) + 1)) {
  80. return (NULL);
  81. }
  82. }
  83. poll_array->items++;
  84. return (&poll_array->array[qnetd_poll_array_size(poll_array) - 1]);
  85. }
  86. static void
  87. qnetd_poll_array_gc(struct qnetd_poll_array *poll_array)
  88. {
  89. if (poll_array->allocated > (qnetd_poll_array_size(poll_array) * 3) + 1) {
  90. qnetd_poll_array_realloc(poll_array, (qnetd_poll_array_size(poll_array) * 2) + 1);
  91. }
  92. }
  93. PRPollDesc *
  94. qnetd_poll_array_get(const struct qnetd_poll_array *poll_array, unsigned int pos)
  95. {
  96. if (pos >= poll_array->items) {
  97. return (NULL);
  98. }
  99. return (&poll_array->array[pos]);
  100. }
  101. PRPollDesc *
  102. qnetd_poll_array_create_from_client_list(struct qnetd_poll_array *poll_array,
  103. const struct qnetd_client_list *client_list,
  104. PRFileDesc *extra_fd, PRInt16 extra_fd_in_flags)
  105. {
  106. struct qnetd_client *client;
  107. PRPollDesc *poll_desc;
  108. qnetd_poll_array_clean(poll_array);
  109. if (extra_fd != NULL) {
  110. poll_desc = qnetd_poll_array_add(poll_array);
  111. if (poll_desc == NULL) {
  112. return (NULL);
  113. }
  114. poll_desc->fd = extra_fd;
  115. poll_desc->in_flags = extra_fd_in_flags;
  116. poll_desc->out_flags = 0;
  117. }
  118. TAILQ_FOREACH(client, client_list, entries) {
  119. poll_desc = qnetd_poll_array_add(poll_array);
  120. if (poll_desc == NULL) {
  121. return (NULL);
  122. }
  123. poll_desc->fd = client->socket;
  124. poll_desc->in_flags = PR_POLL_READ;
  125. if (!send_buffer_list_empty(&client->send_buffer_list)) {
  126. poll_desc->in_flags |= PR_POLL_WRITE;
  127. }
  128. poll_desc->out_flags = 0;
  129. }
  130. qnetd_poll_array_gc(poll_array);
  131. return (poll_array->array);
  132. }