net.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include <stdlib.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <fcntl.h>
  11. #include <sys/socket.h>
  12. #include <sys/un.h>
  13. #include <arpa/inet.h>
  14. #include <time.h>
  15. #include <signal.h>
  16. #include "faux/faux.h"
  17. #include "faux/str.h"
  18. #include "faux/net.h"
  19. #include "private.h"
  20. static faux_net_t *faux_net_allocate(void)
  21. {
  22. faux_net_t *faux_net = NULL;
  23. faux_net = faux_zmalloc(sizeof(*faux_net));
  24. assert(faux_net);
  25. if (!faux_net)
  26. return NULL;
  27. faux_net->fd = -1;
  28. faux_net->isbreak_func = NULL;
  29. faux_net_sigmask_fill(faux_net);
  30. faux_net_set_timeout(faux_net, NULL);
  31. return faux_net;
  32. }
  33. faux_net_t *faux_net_new_by_fd(int fd)
  34. {
  35. faux_net_t *faux_net = NULL;
  36. faux_net = faux_net_allocate();
  37. assert(faux_net);
  38. if (!faux_net)
  39. return NULL;
  40. faux_net->fd = fd;
  41. return faux_net;
  42. }
  43. void faux_net_free(faux_net_t *faux_net)
  44. {
  45. if (!faux_net)
  46. return;
  47. faux_free(faux_net);
  48. }
  49. void faux_net_set_send_timeout(faux_net_t *faux_net, struct timespec *send_timeout)
  50. {
  51. assert(faux_net);
  52. if (!faux_net)
  53. return;
  54. if (!send_timeout) {
  55. faux_net->send_timeout = NULL;
  56. } else {
  57. faux_net->send_timeout_val = *send_timeout;
  58. faux_net->send_timeout = &faux_net->send_timeout_val;
  59. }
  60. }
  61. void faux_net_set_recv_timeout(faux_net_t *faux_net, struct timespec *recv_timeout)
  62. {
  63. assert(faux_net);
  64. if (!faux_net)
  65. return;
  66. if (!recv_timeout) {
  67. faux_net->recv_timeout = NULL;
  68. } else {
  69. faux_net->recv_timeout_val = *recv_timeout;
  70. faux_net->recv_timeout = &faux_net->recv_timeout_val;
  71. }
  72. }
  73. void faux_net_set_timeout(faux_net_t *faux_net, struct timespec *timeout)
  74. {
  75. assert(faux_net);
  76. if (!faux_net)
  77. return;
  78. faux_net_set_send_timeout(faux_net, timeout);
  79. faux_net_set_recv_timeout(faux_net, timeout);
  80. }
  81. void faux_net_set_isbreak_func(faux_net_t *faux_net, int (*isbreak_func)(void))
  82. {
  83. assert(faux_net);
  84. if (!faux_net)
  85. return;
  86. faux_net->isbreak_func = isbreak_func;
  87. }
  88. void faux_net_sigmask_empty(faux_net_t *faux_net)
  89. {
  90. assert(faux_net);
  91. if (!faux_net)
  92. return;
  93. sigemptyset(&faux_net->sigmask);
  94. }
  95. void faux_net_sigmask_fill(faux_net_t *faux_net)
  96. {
  97. assert(faux_net);
  98. if (!faux_net)
  99. return;
  100. sigfillset(&faux_net->sigmask);
  101. }
  102. void faux_net_sigmask_add(faux_net_t *faux_net, int signum)
  103. {
  104. assert(faux_net);
  105. if (!faux_net)
  106. return;
  107. sigaddset(&faux_net->sigmask, signum);
  108. }
  109. void faux_net_sigmask_del(faux_net_t *faux_net, int signum)
  110. {
  111. assert(faux_net);
  112. if (!faux_net)
  113. return;
  114. sigdelset(&faux_net->sigmask, signum);
  115. }
  116. ssize_t faux_net_send(faux_net_t *faux_net, const void *buf, size_t n)
  117. {
  118. return faux_send_block(faux_net->fd, buf, n, faux_net->send_timeout,
  119. &faux_net->sigmask, faux_net->isbreak_func);
  120. }
  121. ssize_t faux_net_sendv(faux_net_t *faux_net,
  122. const struct iovec *iov, int iovcnt)
  123. {
  124. return faux_sendv_block(faux_net->fd, iov, iovcnt, faux_net->send_timeout,
  125. &faux_net->sigmask, faux_net->isbreak_func);
  126. }
  127. ssize_t faux_net_recv(faux_net_t *faux_net, void *buf, size_t n)
  128. {
  129. return faux_recv_block(faux_net->fd, buf, n, faux_net->recv_timeout,
  130. &faux_net->sigmask, faux_net->isbreak_func);
  131. }
  132. ssize_t faux_net_recvv(faux_net_t *faux_net, struct iovec *iov, int iovcnt)
  133. {
  134. return faux_recvv_block(faux_net->fd, iov, iovcnt, faux_net->recv_timeout,
  135. &faux_net->sigmask, faux_net->isbreak_func);
  136. }