tsend.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27. /*
  28. * send with timeout for stream and datagram sockets
  29. *
  30. * History:
  31. * --------
  32. * 2004-02-26 created by andrei
  33. * 2003-03-03 switched to heavy macro use, added tsend_dgram_ev (andrei)
  34. * 2006-02-03 tsend* will wait forever if timeout==-1 (andrei)
  35. */
  36. /*!
  37. * \file
  38. * \brief SIP-router core ::
  39. * \ingroup core
  40. * Module: \ref core
  41. */
  42. #include <string.h>
  43. #include <errno.h>
  44. #include <sys/poll.h>
  45. #include <sys/types.h>
  46. #include <sys/socket.h>
  47. #include <sys/uio.h>
  48. #include "dprint.h"
  49. #include "timer.h"
  50. #include "timer_ticks.h"
  51. /* the functions below are very similar => some generic macros */
  52. #define TSEND_INIT \
  53. int n; \
  54. struct pollfd pf; \
  55. ticks_t expire; \
  56. s_ticks_t diff; \
  57. expire=get_ticks_raw()+MS_TO_TICKS((ticks_t)timeout); \
  58. pf.fd=fd; \
  59. pf.events=POLLOUT
  60. #define TSEND_POLL(f_name) \
  61. poll_loop: \
  62. while(1){ \
  63. if (timeout==-1) \
  64. n=poll(&pf, 1, -1); \
  65. else{ \
  66. diff=expire-get_ticks_raw(); \
  67. if (diff<=0){ \
  68. LOG(L_ERR, "ERROR: " f_name ": send timeout (%d)\n", timeout);\
  69. goto error; \
  70. } \
  71. n=poll(&pf, 1, TICKS_TO_MS((ticks_t)diff)); \
  72. } \
  73. if (n<0){ \
  74. if (errno==EINTR) continue; /* signal, ignore */ \
  75. LOG(L_ERR, "ERROR: " f_name ": poll failed: %s [%d]\n", \
  76. strerror(errno), errno); \
  77. goto error; \
  78. }else if (n==0){ \
  79. /* timeout */ \
  80. LOG(L_ERR, "ERROR: " f_name ": send timeout (p %d)\n", timeout); \
  81. goto error; \
  82. } \
  83. if (pf.revents&POLLOUT){ \
  84. /* we can write again */ \
  85. goto again; \
  86. }else if (pf.revents&(POLLERR|POLLHUP|POLLNVAL)){ \
  87. LOG(L_ERR, "ERROR: " f_name ": bad poll flags %x\n", \
  88. pf.revents); \
  89. goto error; \
  90. } \
  91. /* if POLLIN or POLLPRI or other non-harmful events happened, \
  92. * continue ( although poll should never signal them since we're \
  93. * not interested in them => we should never reach this point) */ \
  94. }
  95. #define TSEND_ERR_CHECK(f_name)\
  96. if (n<0){ \
  97. if (errno==EINTR) goto again; \
  98. else if (errno!=EAGAIN && errno!=EWOULDBLOCK){ \
  99. LOG(L_ERR, "ERROR: " f_name ": failed to send: (%d) %s\n", \
  100. errno, strerror(errno)); \
  101. goto error; \
  102. }else goto poll_loop; \
  103. }
  104. /* sends on fd (which must be O_NONBLOCK if you want a finite timeout); if it
  105. * cannot send any data
  106. * in timeout milliseconds it will return ERROR
  107. * if timeout==-1, it waits forever
  108. * returns: -1 on error, or number of bytes written
  109. * (if less than len => couldn't send all)
  110. * bugs: signals will reset the timer
  111. */
  112. int tsend_stream(int fd, const char* buf, unsigned int len, int timeout)
  113. {
  114. int written;
  115. TSEND_INIT;
  116. written=0;
  117. again:
  118. n=send(fd, buf, len,
  119. #ifdef HAVE_MSG_NOSIGNAL
  120. MSG_NOSIGNAL
  121. #else
  122. 0
  123. #endif
  124. );
  125. TSEND_ERR_CHECK("tsend_stream");
  126. written+=n;
  127. if (n<(int)len){
  128. /* partial write */
  129. buf+=n;
  130. len-=n;
  131. }else{
  132. /* successful full write */
  133. return written;
  134. }
  135. TSEND_POLL("tsend_stream");
  136. error:
  137. return -1;
  138. }
  139. /* sends on dgram fd (which must be O_NONBLOCK); if it cannot send any data
  140. * in timeout milliseconds it will return ERROR
  141. * returns: -1 on error, or number of bytes written
  142. * (if less than len => couldn't send all)
  143. * bugs: signals will reset the timer
  144. */
  145. int tsend_dgram(int fd, const char* buf, unsigned int len,
  146. const struct sockaddr* to, socklen_t tolen, int timeout)
  147. {
  148. TSEND_INIT;
  149. again:
  150. n=sendto(fd, buf, len, 0, to, tolen);
  151. TSEND_ERR_CHECK("tsend_dgram");
  152. /* we don't care about partial writes: they shouldn't happen on
  153. * a datagram socket */
  154. return n;
  155. TSEND_POLL("tsend_datagram");
  156. error:
  157. return -1;
  158. }
  159. /* sends on connected datagram fd (which must be O_NONBLOCK);
  160. * if it cannot send any data in timeout milliseconds it will return ERROR
  161. * returns: -1 on error, or number of bytes written
  162. * (if less than len => couldn't send all)
  163. * bugs: signals will reset the timer
  164. */
  165. int tsend_dgram_ev(int fd, const struct iovec* v, int count, int timeout)
  166. {
  167. TSEND_INIT;
  168. again:
  169. n=writev(fd, v, count);
  170. TSEND_ERR_CHECK("tsend_datagram_ev");
  171. return n;
  172. TSEND_POLL("tsend_datagram_ev");
  173. error:
  174. return -1;
  175. }