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.

395 lines
9.8 KiB

8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
8 years ago
​
​
​
​
8 years ago
​
8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
8 years ago
​
8 years ago
​
​
​
​
8 years ago
​
​
​
8 years ago
​
​
8 years ago
​
8 years ago
​
​
​
​
8 years ago
​
​
​
8 years ago
8 years ago
​
​
​
8 years ago
​
​
​
​
​
​
​
8 years ago
​
​
​
​
​
​
​
​
​
8 years ago
​
​
8 years ago
​
​
8 years ago
​
​
8 years ago
​
​
​
8 years ago
​
​
8 years ago
​
​
​
​
8 years ago
​
​
​
​
8 years ago
​
​
​
​
8 years ago
8 years ago
​
​
8 years ago
​
​
​
8 years ago
​
8 years ago
​
​
​
8 years ago
​
​
​
​
​
​
​
​
​
8 years ago
8 years ago
​
8 years ago
8 years ago
​
​
​
8 years ago
​
​
​
8 years ago
​
​
​
​
​
​
8 years ago
​
8 years ago
​
​
​
​
​
8 years ago
​
​
​
​
​
​
​
​
​
​
8 years ago
​
​
​
8 years ago
​
​
8 years ago
8 years ago
​
​
8 years ago
​
​
​
​
​
​
​
​
​
​
​
8 years ago
​
​
8 years ago
​
8 years ago
8 years ago
8 years ago
​
​
​
​
8 years ago
​
​
​
​
8 years ago
​
​
​
​
8 years ago
​
​
8 years ago
​
8 years ago
​
​
​
​
8 years ago
​
​
​
​
​
​
​
​
​
8 years ago
​
​
​
8 years ago
​
​
​
​
​
8 years ago
​
8 years ago
​
​
​
​
8 years ago
​
​
​
8 years ago
​
​
8 years ago
​
8 years ago
​
​
​
​
8 years ago
​
8 years ago
8 years ago
​
​
​
​
​
​
​
​
​
8 years ago
​
​
​
8 years ago
​
8 years ago
​
​
​
​
​
8 years ago
​
8 years ago
​
​
​
​
​
8 years ago
​
​
​
​
​
  1. /* $Id: asyncsendto.c,v 1.7 2015/09/03 18:19:20 nanard Exp $ */
  2. /* MiniUPnP project
  3. * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
  4. * (c) 2006-2014 Thomas Bernard
  5. * This software is subject to the conditions detailed
  6. * in the LICENCE file provided within the distribution */
  7. #include "asyncsendto.h"
  8. #include "upnputils.h"
  9. #include <errno.h>
  10. #include <inttypes.h>
  11. #include <netinet/in.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <sys/queue.h>
  15. #include <sys/socket.h>
  16. #include <sys/time.h>
  17. #include <sys/types.h>
  18. #include <sys/uio.h>
  19. #include <syslog.h>
  20. #include <unistd.h>
  21. /* state diagram for a packet :
  22. *
  23. * |
  24. * V
  25. * -> ESCHEDULED -> ESENDNOW -> sent
  26. * ^ |
  27. * | V
  28. * EWAITREADY -> sent
  29. */
  30. struct scheduled_send
  31. {
  32. LIST_ENTRY(scheduled_send) entries;
  33. struct timeval ts;
  34. enum
  35. {
  36. ESCHEDULED = 1,
  37. EWAITREADY = 2,
  38. ESENDNOW = 3
  39. } state;
  40. int sockfd;
  41. const void * buf;
  42. size_t len;
  43. int flags;
  44. const struct sockaddr * dest_addr;
  45. socklen_t addrlen;
  46. const struct sockaddr_in6 * src_addr;
  47. char data[];
  48. };
  49. static LIST_HEAD(listhead, scheduled_send) send_list = {NULL};
  50. /*
  51. * ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
  52. * const struct sockaddr *dest_addr, socklen_t addrlen);
  53. */
  54. static ssize_t send_from_to(int sockfd, const void * buf, size_t len, int flags, const struct sockaddr_in6 * src_addr, const struct sockaddr * dest_addr, socklen_t addrlen)
  55. {
  56. #ifdef IPV6_PKTINFO
  57. if (src_addr)
  58. {
  59. struct iovec iov;
  60. struct in6_pktinfo ipi6;
  61. uint8_t c[CMSG_SPACE(sizeof(ipi6))];
  62. struct msghdr msg;
  63. struct cmsghdr * cmsg;
  64. iov.iov_base = (void *) buf;
  65. iov.iov_len = len;
  66. memset(&msg, 0, sizeof(msg));
  67. msg.msg_iov = &iov;
  68. msg.msg_iovlen = 1;
  69. ipi6.ipi6_addr = src_addr->sin6_addr;
  70. ipi6.ipi6_ifindex = src_addr->sin6_scope_id;
  71. msg.msg_control = c;
  72. msg.msg_controllen = sizeof(c);
  73. cmsg = CMSG_FIRSTHDR(&msg);
  74. cmsg->cmsg_level = IPPROTO_IPV6;
  75. cmsg->cmsg_type = IPV6_PKTINFO;
  76. cmsg->cmsg_len = CMSG_LEN(sizeof(ipi6));
  77. memcpy(CMSG_DATA(cmsg), &ipi6, sizeof(ipi6));
  78. msg.msg_name = (void *) dest_addr;
  79. msg.msg_namelen = addrlen;
  80. return sendmsg(sockfd, &msg, flags);
  81. }
  82. else
  83. {
  84. #endif /* IPV6_PKTINFO */
  85. return sendto(sockfd, buf, len, flags, dest_addr, addrlen);
  86. #ifdef IPV6_PKTINFO
  87. }
  88. #endif /* IPV6_PKTINFO */
  89. }
  90. /* delay = milli seconds */
  91. ssize_t sendto_schedule2(int sockfd, const void * buf, size_t len, int flags, const struct sockaddr * dest_addr, socklen_t addrlen, const struct sockaddr_in6 * src_addr, unsigned int delay)
  92. {
  93. enum
  94. {
  95. ESCHEDULED,
  96. EWAITREADY,
  97. ESENDNOW
  98. } state;
  99. ssize_t n;
  100. size_t alloc_len;
  101. struct timeval tv;
  102. struct scheduled_send * elt;
  103. if (delay == 0)
  104. {
  105. /* first try to send at once */
  106. n = send_from_to(sockfd, buf, len, flags, src_addr, dest_addr, addrlen);
  107. if (n >= 0)
  108. {
  109. return n;
  110. }
  111. else if (errno == EAGAIN || errno == EWOULDBLOCK)
  112. {
  113. /* use select() on this socket */
  114. state = EWAITREADY;
  115. }
  116. else if (errno == EINTR)
  117. {
  118. state = ESENDNOW;
  119. }
  120. else
  121. {
  122. /* uncatched error */
  123. return n;
  124. }
  125. }
  126. else
  127. {
  128. state = ESCHEDULED;
  129. }
  130. /* schedule */
  131. if (gettimeofday(&tv, 0) < 0)
  132. {
  133. return -1;
  134. }
  135. /* allocate enough space for structure + buffers */
  136. alloc_len = sizeof(struct scheduled_send) + len + addrlen;
  137. if (src_addr)
  138. {
  139. alloc_len += sizeof(struct sockaddr_in6);
  140. }
  141. elt = malloc(alloc_len);
  142. if (elt == NULL)
  143. {
  144. syslog(LOG_ERR, "malloc failed to allocate %u bytes", (unsigned) alloc_len);
  145. return -1;
  146. }
  147. elt->state = state;
  148. /* time the packet should be sent */
  149. elt->ts.tv_sec = tv.tv_sec + (delay / 1000);
  150. elt->ts.tv_usec = tv.tv_usec + (delay % 1000) * 1000;
  151. if (elt->ts.tv_usec > 1000000)
  152. {
  153. elt->ts.tv_sec++;
  154. elt->ts.tv_usec -= 1000000;
  155. }
  156. elt->sockfd = sockfd;
  157. elt->flags = flags;
  158. memcpy(elt->data, dest_addr, addrlen);
  159. elt->dest_addr = (struct sockaddr *) elt->data;
  160. elt->addrlen = addrlen;
  161. if (src_addr)
  162. {
  163. elt->src_addr = (struct sockaddr_in6 *) (elt->data + addrlen);
  164. memcpy((void *) elt->src_addr, src_addr, sizeof(struct sockaddr_in6));
  165. elt->buf = (void *) (elt->data + addrlen + sizeof(struct sockaddr_in6));
  166. }
  167. else
  168. {
  169. elt->src_addr = NULL;
  170. elt->buf = (void *) (elt->data + addrlen);
  171. }
  172. elt->len = len;
  173. memcpy((void *) elt->buf, buf, len);
  174. /* insert */
  175. LIST_INSERT_HEAD(&send_list, elt, entries);
  176. return 0;
  177. }
  178. /* try to send at once, and queue the packet if needed */
  179. ssize_t sendto_or_schedule(int sockfd, const void * buf, size_t len, int flags, const struct sockaddr * dest_addr, socklen_t addrlen)
  180. {
  181. return sendto_schedule2(sockfd, buf, len, flags, dest_addr, addrlen, NULL, 0);
  182. }
  183. ssize_t sendto_or_schedule2(int sockfd, const void * buf, size_t len, int flags, const struct sockaddr * dest_addr, socklen_t addrlen, const struct sockaddr_in6 * src_addr)
  184. {
  185. return sendto_schedule2(sockfd, buf, len, flags, dest_addr, addrlen, src_addr, 0);
  186. }
  187. /* get_next_scheduled_send() return number of scheduled send in list */
  188. int get_next_scheduled_send(struct timeval * next_send)
  189. {
  190. int n = 0;
  191. struct scheduled_send * elt;
  192. if (next_send == NULL)
  193. {
  194. return -1;
  195. }
  196. for (elt = send_list.lh_first; elt != NULL; elt = elt->entries.le_next)
  197. {
  198. if (n == 0 || (elt->ts.tv_sec < next_send->tv_sec) || (elt->ts.tv_sec == next_send->tv_sec && elt->ts.tv_usec < next_send->tv_usec))
  199. {
  200. next_send->tv_sec = elt->ts.tv_sec;
  201. next_send->tv_usec = elt->ts.tv_usec;
  202. }
  203. n++;
  204. }
  205. return n;
  206. }
  207. /* update writefds for select() call
  208. * return the number of packets to try to send at once */
  209. int get_sendto_fds(fd_set * writefds, int * max_fd, const struct timeval * now)
  210. {
  211. int n = 0;
  212. struct scheduled_send * elt;
  213. for (elt = send_list.lh_first; elt != NULL; elt = elt->entries.le_next)
  214. {
  215. if (elt->state == EWAITREADY)
  216. {
  217. /* last sendto() call returned EAGAIN/EWOULDBLOCK */
  218. FD_SET(elt->sockfd, writefds);
  219. if (elt->sockfd > *max_fd)
  220. {
  221. *max_fd = elt->sockfd;
  222. }
  223. n++;
  224. }
  225. else if ((elt->ts.tv_sec < now->tv_sec) || (elt->ts.tv_sec == now->tv_sec && elt->ts.tv_usec <= now->tv_usec))
  226. {
  227. /* we waited long enough, now send ! */
  228. elt->state = ESENDNOW;
  229. n++;
  230. }
  231. }
  232. return n;
  233. }
  234. /* executed sendto() when needed */
  235. int try_sendto(fd_set * writefds)
  236. {
  237. int ret = 0;
  238. ssize_t n;
  239. struct scheduled_send * elt;
  240. struct scheduled_send * next;
  241. for (elt = send_list.lh_first; elt != NULL; elt = next)
  242. {
  243. next = elt->entries.le_next;
  244. if ((elt->state == ESENDNOW) || (elt->state == EWAITREADY && FD_ISSET(elt->sockfd, writefds)))
  245. {
  246. #ifdef DEBUG
  247. syslog(LOG_DEBUG, "%s: %d bytes on socket %d", "try_sendto", (int) elt->len, elt->sockfd);
  248. #endif
  249. n = send_from_to(elt->sockfd, elt->buf, elt->len, elt->flags, elt->src_addr, elt->dest_addr, elt->addrlen);
  250. /*n = sendto(elt->sockfd, elt->buf, elt->len, elt->flags,
  251. elt->dest_addr, elt->addrlen);*/
  252. if (n < 0)
  253. {
  254. if (errno == EINTR)
  255. {
  256. /* retry at once */
  257. elt->state = ESENDNOW;
  258. continue;
  259. }
  260. else if (errno == EAGAIN || errno == EWOULDBLOCK)
  261. {
  262. /* retry once the socket is ready for writing */
  263. elt->state = EWAITREADY;
  264. continue;
  265. }
  266. else
  267. {
  268. char addr_str[64];
  269. /* uncatched error */
  270. if (sockaddr_to_string(elt->dest_addr, addr_str, sizeof(addr_str)) <= 0)
  271. {
  272. addr_str[0] = '\0';
  273. }
  274. syslog(LOG_ERR, "%s(sock=%d, len=%u, dest=%s): sendto: %m", "try_sendto", elt->sockfd, (unsigned) elt->len, addr_str);
  275. ret--;
  276. }
  277. }
  278. else if ((int) n != (int) elt->len)
  279. {
  280. syslog(LOG_WARNING, "%s: %d bytes sent out of %d", "try_sendto", (int) n, (int) elt->len);
  281. }
  282. /* remove from the list */
  283. LIST_REMOVE(elt, entries);
  284. free(elt);
  285. }
  286. }
  287. return ret;
  288. }
  289. /* maximum execution time for finalize_sendto() in milliseconds */
  290. #define FINALIZE_SENDTO_DELAY (500)
  291. /* empty the list */
  292. void finalize_sendto(void)
  293. {
  294. ssize_t n;
  295. struct scheduled_send * elt;
  296. struct scheduled_send * next;
  297. fd_set writefds;
  298. struct timeval deadline;
  299. struct timeval now;
  300. struct timeval timeout;
  301. int max_fd;
  302. if (gettimeofday(&deadline, NULL) < 0)
  303. {
  304. syslog(LOG_ERR, "gettimeofday: %m");
  305. return;
  306. }
  307. deadline.tv_usec += FINALIZE_SENDTO_DELAY * 1000;
  308. if (deadline.tv_usec > 1000000)
  309. {
  310. deadline.tv_sec++;
  311. deadline.tv_usec -= 1000000;
  312. }
  313. while (send_list.lh_first)
  314. {
  315. FD_ZERO(&writefds);
  316. max_fd = -1;
  317. for (elt = send_list.lh_first; elt != NULL; elt = next)
  318. {
  319. next = elt->entries.le_next;
  320. syslog(LOG_DEBUG, "finalize_sendto(): %d bytes on socket %d", (int) elt->len, elt->sockfd);
  321. n = send_from_to(elt->sockfd, elt->buf, elt->len, elt->flags, elt->src_addr, elt->dest_addr, elt->addrlen);
  322. /*n = sendto(elt->sockfd, elt->buf, elt->len, elt->flags,
  323. elt->dest_addr, elt->addrlen);*/
  324. if (n < 0)
  325. {
  326. if (errno == EAGAIN || errno == EWOULDBLOCK)
  327. {
  328. FD_SET(elt->sockfd, &writefds);
  329. if (elt->sockfd > max_fd)
  330. {
  331. max_fd = elt->sockfd;
  332. }
  333. continue;
  334. }
  335. syslog(LOG_WARNING, "finalize_sendto(): socket=%d sendto: %m", elt->sockfd);
  336. }
  337. /* remove from the list */
  338. LIST_REMOVE(elt, entries);
  339. free(elt);
  340. }
  341. /* check deadline */
  342. if (gettimeofday(&now, NULL) < 0)
  343. {
  344. syslog(LOG_ERR, "gettimeofday: %m");
  345. return;
  346. }
  347. if (now.tv_sec > deadline.tv_sec || (now.tv_sec == deadline.tv_sec && now.tv_usec > deadline.tv_usec))
  348. {
  349. /* deadline ! */
  350. while ((elt = send_list.lh_first) != NULL)
  351. {
  352. LIST_REMOVE(elt, entries);
  353. free(elt);
  354. }
  355. return;
  356. }
  357. /* compute timeout value */
  358. timeout.tv_sec = deadline.tv_sec - now.tv_sec;
  359. timeout.tv_usec = deadline.tv_usec - now.tv_usec;
  360. if (timeout.tv_usec < 0)
  361. {
  362. timeout.tv_sec--;
  363. timeout.tv_usec += 1000000;
  364. }
  365. if (max_fd >= 0)
  366. {
  367. if (select(max_fd + 1, NULL, &writefds, NULL, &timeout) < 0)
  368. {
  369. syslog(LOG_ERR, "select: %m");
  370. return;
  371. }
  372. }
  373. }
  374. }