tsdumper2 reads incoming mpeg transport stream over UDP/RTP and then records it to disk. The files names are generated based on preconfigured time interval. https://georgi.unixsol.org/programs/tsdumper2/
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.

udp.c 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * UDP functions
  3. * Copyright (C) 2013 Unix Solutions Ltd.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License (COPYING file) for more details.
  13. *
  14. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <unistd.h>
  21. #include <fcntl.h>
  22. #include <sys/socket.h>
  23. #include <netinet/in.h>
  24. #include <arpa/inet.h>
  25. #include <errno.h>
  26. #include "tsdumper2.h"
  27. #ifndef IPV6_ADD_MEMBERSHIP
  28. #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
  29. #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
  30. #endif
  31. int ai_family = AF_UNSPEC;
  32. static int is_multicast(struct sockaddr_storage *addr) {
  33. int ret = 0;
  34. switch (addr->ss_family) {
  35. case AF_INET: {
  36. struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
  37. ret = IN_MULTICAST(ntohl(addr4->sin_addr.s_addr));
  38. break;
  39. }
  40. case AF_INET6: {
  41. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
  42. ret = IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr);
  43. break;
  44. } }
  45. return ret;
  46. }
  47. static int bind_addr(const char *hostname, const char *service, int socktype, struct sockaddr_storage *addr, int *addrlen, int *sock) {
  48. struct addrinfo hints, *res, *ressave;
  49. int n, ret = -1;
  50. memset(&hints, 0, sizeof(struct addrinfo));
  51. hints.ai_family = ai_family;
  52. hints.ai_socktype = socktype;
  53. n = getaddrinfo(hostname, service, &hints, &res);
  54. if (n < 0) {
  55. p_info("ERROR: getaddrinfo(%s): %s\n", hostname, gai_strerror(n));
  56. return ret;
  57. }
  58. ressave = res;
  59. while (res) {
  60. *sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  61. if (*sock > -1) {
  62. int on = 1;
  63. setsockopt(*sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
  64. set_sock_nonblock(*sock);
  65. if (bind(*sock, res->ai_addr, res->ai_addrlen) == 0) {
  66. memcpy(addr, res->ai_addr, res->ai_addrlen);
  67. *addrlen = res->ai_addrlen;
  68. ret = 0;
  69. goto OUT;
  70. } else {
  71. char str_addr[INET6_ADDRSTRLEN];
  72. my_inet_ntop(res->ai_family, res->ai_addr, str_addr, sizeof(str_addr));
  73. p_err("bind: %s:%s (%s)", hostname, service, str_addr);
  74. }
  75. close(*sock);
  76. *sock = -1;
  77. }
  78. res = res->ai_next;
  79. }
  80. OUT:
  81. freeaddrinfo(ressave);
  82. return ret;
  83. }
  84. static int join_multicast_group(int sock, struct sockaddr_storage *addr) {
  85. switch (addr->ss_family) {
  86. case AF_INET: {
  87. struct ip_mreq mreq;
  88. mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
  89. mreq.imr_interface.s_addr = INADDR_ANY;
  90. if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
  91. p_err("setsockopt(IP_ADD_MEMBERSHIP)");
  92. return -1;
  93. }
  94. break;
  95. }
  96. case AF_INET6: {
  97. struct ipv6_mreq mreq6;
  98. memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
  99. mreq6.ipv6mr_interface = 0; // interface index, will be set later
  100. if (setsockopt(sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
  101. p_err("setsockopt(IPV6_ADD_MEMBERSHIP)");
  102. return -1;
  103. }
  104. break;
  105. }
  106. }
  107. return 0;
  108. }
  109. int udp_connect_input(struct io *io) {
  110. struct sockaddr_storage addr;
  111. int addrlen = sizeof(addr);
  112. int sock = -1;
  113. memset(&addr, 0, sizeof(addr));
  114. p_info("Connecting input to %s port %s\n", io->hostname, io->service);
  115. if (bind_addr(io->hostname, io->service, SOCK_DGRAM, &addr, &addrlen, &sock) < 0)
  116. return -1;
  117. /* Set receive buffer size to ~4.0MB */
  118. int bufsize = (4000000 / 1316) * 1316;
  119. setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (void *)&bufsize, sizeof(bufsize));
  120. if (is_multicast(&addr)) {
  121. if (join_multicast_group(sock, &addr) < 0) {
  122. close(sock);
  123. return -1;
  124. }
  125. }
  126. io->fd = sock;
  127. p_info("Input connected to fd:%d\n", io->fd);
  128. return 1;
  129. }