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.

1108 lines
39KB

  1. /*
  2. * UDP prototype streaming system
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * UDP protocol
  24. */
  25. #define _DEFAULT_SOURCE
  26. #define _BSD_SOURCE /* Needed for using struct ip_mreq with recent glibc */
  27. #include "avformat.h"
  28. #include "avio_internal.h"
  29. #include "libavutil/avassert.h"
  30. #include "libavutil/parseutils.h"
  31. #include "libavutil/fifo.h"
  32. #include "libavutil/intreadwrite.h"
  33. #include "libavutil/avstring.h"
  34. #include "libavutil/opt.h"
  35. #include "libavutil/log.h"
  36. #include "libavutil/time.h"
  37. #include "internal.h"
  38. #include "network.h"
  39. #include "os_support.h"
  40. #include "url.h"
  41. #include "ip.h"
  42. #ifdef __APPLE__
  43. #include "TargetConditionals.h"
  44. #endif
  45. #if HAVE_UDPLITE_H
  46. #include "udplite.h"
  47. #else
  48. /* On many Linux systems, udplite.h is missing but the kernel supports UDP-Lite.
  49. * So, we provide a fallback here.
  50. */
  51. #define UDPLITE_SEND_CSCOV 10
  52. #define UDPLITE_RECV_CSCOV 11
  53. #endif
  54. #ifndef IPPROTO_UDPLITE
  55. #define IPPROTO_UDPLITE 136
  56. #endif
  57. #if HAVE_PTHREAD_CANCEL
  58. #include <pthread.h>
  59. #endif
  60. #ifndef IPV6_ADD_MEMBERSHIP
  61. #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
  62. #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
  63. #endif
  64. #define UDP_TX_BUF_SIZE 32768
  65. #define UDP_MAX_PKT_SIZE 65536
  66. #define UDP_HEADER_SIZE 8
  67. typedef struct UDPContext {
  68. const AVClass *class;
  69. int udp_fd;
  70. int ttl;
  71. int udplite_coverage;
  72. int buffer_size;
  73. int pkt_size;
  74. int is_multicast;
  75. int is_broadcast;
  76. int local_port;
  77. int reuse_socket;
  78. int overrun_nonfatal;
  79. struct sockaddr_storage dest_addr;
  80. int dest_addr_len;
  81. int is_connected;
  82. /* Circular Buffer variables for use in UDP receive code */
  83. int circular_buffer_size;
  84. AVFifoBuffer *fifo;
  85. int circular_buffer_error;
  86. int64_t bitrate; /* number of bits to send per second */
  87. int64_t burst_bits;
  88. int close_req;
  89. #if HAVE_PTHREAD_CANCEL
  90. pthread_t circular_buffer_thread;
  91. pthread_mutex_t mutex;
  92. pthread_cond_t cond;
  93. int thread_started;
  94. #endif
  95. uint8_t tmp[UDP_MAX_PKT_SIZE+4];
  96. int remaining_in_dg;
  97. char *localaddr;
  98. int timeout;
  99. struct sockaddr_storage local_addr_storage;
  100. char *sources;
  101. char *block;
  102. IPSourceFilters filters;
  103. } UDPContext;
  104. #define OFFSET(x) offsetof(UDPContext, x)
  105. #define D AV_OPT_FLAG_DECODING_PARAM
  106. #define E AV_OPT_FLAG_ENCODING_PARAM
  107. static const AVOption options[] = {
  108. { "buffer_size", "System data size (in bytes)", OFFSET(buffer_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
  109. { "bitrate", "Bits to send per second", OFFSET(bitrate), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, .flags = E },
  110. { "burst_bits", "Max length of bursts in bits (when using bitrate)", OFFSET(burst_bits), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, .flags = E },
  111. { "localport", "Local port", OFFSET(local_port), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, D|E },
  112. { "local_port", "Local port", OFFSET(local_port), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
  113. { "localaddr", "Local address", OFFSET(localaddr), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
  114. { "udplite_coverage", "choose UDPLite head size which should be validated by checksum", OFFSET(udplite_coverage), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, D|E },
  115. { "pkt_size", "Maximum UDP packet size", OFFSET(pkt_size), AV_OPT_TYPE_INT, { .i64 = 1472 }, -1, INT_MAX, .flags = D|E },
  116. { "reuse", "explicitly allow reusing UDP sockets", OFFSET(reuse_socket), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, D|E },
  117. { "reuse_socket", "explicitly allow reusing UDP sockets", OFFSET(reuse_socket), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, .flags = D|E },
  118. { "broadcast", "explicitly allow or disallow broadcast destination", OFFSET(is_broadcast), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, E },
  119. { "ttl", "Time to live (multicast only)", OFFSET(ttl), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, INT_MAX, E },
  120. { "connect", "set if connect() should be called on socket", OFFSET(is_connected), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = D|E },
  121. { "fifo_size", "set the UDP receiving circular buffer size, expressed as a number of packets with size of 188 bytes", OFFSET(circular_buffer_size), AV_OPT_TYPE_INT, {.i64 = 7*4096}, 0, INT_MAX, D },
  122. { "overrun_nonfatal", "survive in case of UDP receiving circular buffer overrun", OFFSET(overrun_nonfatal), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, D },
  123. { "timeout", "set raise error timeout (only in read mode)", OFFSET(timeout), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
  124. { "sources", "Source list", OFFSET(sources), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
  125. { "block", "Block list", OFFSET(block), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
  126. { NULL }
  127. };
  128. static const AVClass udp_class = {
  129. .class_name = "udp",
  130. .item_name = av_default_item_name,
  131. .option = options,
  132. .version = LIBAVUTIL_VERSION_INT,
  133. };
  134. static const AVClass udplite_context_class = {
  135. .class_name = "udplite",
  136. .item_name = av_default_item_name,
  137. .option = options,
  138. .version = LIBAVUTIL_VERSION_INT,
  139. };
  140. static int udp_set_multicast_ttl(int sockfd, int mcastTTL,
  141. struct sockaddr *addr)
  142. {
  143. #ifdef IP_MULTICAST_TTL
  144. if (addr->sa_family == AF_INET) {
  145. if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
  146. ff_log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_MULTICAST_TTL)");
  147. return -1;
  148. }
  149. }
  150. #endif
  151. #if defined(IPPROTO_IPV6) && defined(IPV6_MULTICAST_HOPS)
  152. if (addr->sa_family == AF_INET6) {
  153. if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
  154. ff_log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IPV6_MULTICAST_HOPS)");
  155. return -1;
  156. }
  157. }
  158. #endif
  159. return 0;
  160. }
  161. static int udp_join_multicast_group(int sockfd, struct sockaddr *addr,struct sockaddr *local_addr)
  162. {
  163. #ifdef IP_ADD_MEMBERSHIP
  164. if (addr->sa_family == AF_INET) {
  165. struct ip_mreq mreq;
  166. mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
  167. if (local_addr)
  168. mreq.imr_interface= ((struct sockaddr_in *)local_addr)->sin_addr;
  169. else
  170. mreq.imr_interface.s_addr= INADDR_ANY;
  171. if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
  172. ff_log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_ADD_MEMBERSHIP)");
  173. return -1;
  174. }
  175. }
  176. #endif
  177. #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
  178. if (addr->sa_family == AF_INET6) {
  179. struct ipv6_mreq mreq6;
  180. memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
  181. //TODO: Interface index should be looked up from local_addr
  182. mreq6.ipv6mr_interface= 0;
  183. if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
  184. ff_log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IPV6_ADD_MEMBERSHIP)");
  185. return -1;
  186. }
  187. }
  188. #endif
  189. return 0;
  190. }
  191. static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr,struct sockaddr *local_addr)
  192. {
  193. #ifdef IP_DROP_MEMBERSHIP
  194. if (addr->sa_family == AF_INET) {
  195. struct ip_mreq mreq;
  196. mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
  197. if (local_addr)
  198. mreq.imr_interface= ((struct sockaddr_in *)local_addr)->sin_addr;
  199. else
  200. mreq.imr_interface.s_addr= INADDR_ANY;
  201. if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
  202. ff_log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_DROP_MEMBERSHIP)");
  203. return -1;
  204. }
  205. }
  206. #endif
  207. #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
  208. if (addr->sa_family == AF_INET6) {
  209. struct ipv6_mreq mreq6;
  210. memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
  211. //TODO: Interface index should be looked up from local_addr
  212. mreq6.ipv6mr_interface= 0;
  213. if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
  214. ff_log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IPV6_DROP_MEMBERSHIP)");
  215. return -1;
  216. }
  217. }
  218. #endif
  219. return 0;
  220. }
  221. static int udp_set_multicast_sources(URLContext *h,
  222. int sockfd, struct sockaddr *addr,
  223. int addr_len, struct sockaddr_storage *local_addr,
  224. struct sockaddr_storage *sources,
  225. int nb_sources, int include)
  226. {
  227. #if HAVE_STRUCT_GROUP_SOURCE_REQ && defined(MCAST_BLOCK_SOURCE) && !defined(_WIN32) && (!defined(TARGET_OS_TV) || !TARGET_OS_TV)
  228. /* These ones are available in the microsoft SDK, but don't seem to work
  229. * as on linux, so just prefer the v4-only approach there for now. */
  230. int i;
  231. for (i = 0; i < nb_sources; i++) {
  232. struct group_source_req mreqs;
  233. int level = addr->sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
  234. //TODO: Interface index should be looked up from local_addr
  235. mreqs.gsr_interface = 0;
  236. memcpy(&mreqs.gsr_group, addr, addr_len);
  237. memcpy(&mreqs.gsr_source, &sources[i], sizeof(*sources));
  238. if (setsockopt(sockfd, level,
  239. include ? MCAST_JOIN_SOURCE_GROUP : MCAST_BLOCK_SOURCE,
  240. (const void *)&mreqs, sizeof(mreqs)) < 0) {
  241. if (include)
  242. ff_log_net_error(NULL, AV_LOG_ERROR, "setsockopt(MCAST_JOIN_SOURCE_GROUP)");
  243. else
  244. ff_log_net_error(NULL, AV_LOG_ERROR, "setsockopt(MCAST_BLOCK_SOURCE)");
  245. return ff_neterrno();
  246. }
  247. }
  248. #elif HAVE_STRUCT_IP_MREQ_SOURCE && defined(IP_BLOCK_SOURCE)
  249. int i;
  250. if (addr->sa_family != AF_INET) {
  251. av_log(NULL, AV_LOG_ERROR,
  252. "Setting multicast sources only supported for IPv4\n");
  253. return AVERROR(EINVAL);
  254. }
  255. for (i = 0; i < nb_sources; i++) {
  256. struct ip_mreq_source mreqs;
  257. if (sources[i].ss_family != AF_INET) {
  258. av_log(NULL, AV_LOG_ERROR, "Source/block address %d is of incorrect protocol family\n", i + 1);
  259. return AVERROR(EINVAL);
  260. }
  261. mreqs.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
  262. if (local_addr)
  263. mreqs.imr_interface= ((struct sockaddr_in *)local_addr)->sin_addr;
  264. else
  265. mreqs.imr_interface.s_addr= INADDR_ANY;
  266. mreqs.imr_sourceaddr.s_addr = ((struct sockaddr_in *)&sources[i])->sin_addr.s_addr;
  267. if (setsockopt(sockfd, IPPROTO_IP,
  268. include ? IP_ADD_SOURCE_MEMBERSHIP : IP_BLOCK_SOURCE,
  269. (const void *)&mreqs, sizeof(mreqs)) < 0) {
  270. if (include)
  271. ff_log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_ADD_SOURCE_MEMBERSHIP)");
  272. else
  273. ff_log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_BLOCK_SOURCE)");
  274. return ff_neterrno();
  275. }
  276. }
  277. #else
  278. return AVERROR(ENOSYS);
  279. #endif
  280. return 0;
  281. }
  282. static int udp_set_url(URLContext *h,
  283. struct sockaddr_storage *addr,
  284. const char *hostname, int port)
  285. {
  286. struct addrinfo *res0;
  287. int addr_len;
  288. res0 = ff_ip_resolve_host(h, hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
  289. if (!res0) return AVERROR(EIO);
  290. memcpy(addr, res0->ai_addr, res0->ai_addrlen);
  291. addr_len = res0->ai_addrlen;
  292. freeaddrinfo(res0);
  293. return addr_len;
  294. }
  295. static int udp_socket_create(URLContext *h, struct sockaddr_storage *addr,
  296. socklen_t *addr_len, const char *localaddr)
  297. {
  298. UDPContext *s = h->priv_data;
  299. int udp_fd = -1;
  300. struct addrinfo *res0, *res;
  301. int family = AF_UNSPEC;
  302. if (((struct sockaddr *) &s->dest_addr)->sa_family)
  303. family = ((struct sockaddr *) &s->dest_addr)->sa_family;
  304. res0 = ff_ip_resolve_host(h, (localaddr && localaddr[0]) ? localaddr : NULL,
  305. s->local_port,
  306. SOCK_DGRAM, family, AI_PASSIVE);
  307. if (!res0)
  308. goto fail;
  309. for (res = res0; res; res=res->ai_next) {
  310. if (s->udplite_coverage)
  311. udp_fd = ff_socket(res->ai_family, SOCK_DGRAM, IPPROTO_UDPLITE);
  312. else
  313. udp_fd = ff_socket(res->ai_family, SOCK_DGRAM, 0);
  314. if (udp_fd != -1) break;
  315. ff_log_net_error(NULL, AV_LOG_ERROR, "socket");
  316. }
  317. if (udp_fd < 0)
  318. goto fail;
  319. memcpy(addr, res->ai_addr, res->ai_addrlen);
  320. *addr_len = res->ai_addrlen;
  321. freeaddrinfo(res0);
  322. return udp_fd;
  323. fail:
  324. if (udp_fd >= 0)
  325. closesocket(udp_fd);
  326. if(res0)
  327. freeaddrinfo(res0);
  328. return -1;
  329. }
  330. static int udp_port(struct sockaddr_storage *addr, int addr_len)
  331. {
  332. char sbuf[sizeof(int)*3+1];
  333. int error;
  334. if ((error = getnameinfo((struct sockaddr *)addr, addr_len, NULL, 0, sbuf, sizeof(sbuf), NI_NUMERICSERV)) != 0) {
  335. av_log(NULL, AV_LOG_ERROR, "getnameinfo: %s\n", gai_strerror(error));
  336. return -1;
  337. }
  338. return strtol(sbuf, NULL, 10);
  339. }
  340. /**
  341. * If no filename is given to av_open_input_file because you want to
  342. * get the local port first, then you must call this function to set
  343. * the remote server address.
  344. *
  345. * url syntax: udp://host:port[?option=val...]
  346. * option: 'ttl=n' : set the ttl value (for multicast only)
  347. * 'localport=n' : set the local port
  348. * 'pkt_size=n' : set max packet size
  349. * 'reuse=1' : enable reusing the socket
  350. * 'overrun_nonfatal=1': survive in case of circular buffer overrun
  351. *
  352. * @param h media file context
  353. * @param uri of the remote server
  354. * @return zero if no error.
  355. */
  356. int ff_udp_set_remote_url(URLContext *h, const char *uri)
  357. {
  358. UDPContext *s = h->priv_data;
  359. char hostname[256], buf[10];
  360. int port;
  361. const char *p;
  362. av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
  363. /* set the destination address */
  364. s->dest_addr_len = udp_set_url(h, &s->dest_addr, hostname, port);
  365. if (s->dest_addr_len < 0) {
  366. return AVERROR(EIO);
  367. }
  368. s->is_multicast = ff_is_multicast_address((struct sockaddr*) &s->dest_addr);
  369. p = strchr(uri, '?');
  370. if (p) {
  371. if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
  372. int was_connected = s->is_connected;
  373. s->is_connected = strtol(buf, NULL, 10);
  374. if (s->is_connected && !was_connected) {
  375. if (connect(s->udp_fd, (struct sockaddr *) &s->dest_addr,
  376. s->dest_addr_len)) {
  377. s->is_connected = 0;
  378. ff_log_net_error(h, AV_LOG_ERROR, "connect");
  379. return AVERROR(EIO);
  380. }
  381. }
  382. }
  383. }
  384. return 0;
  385. }
  386. /**
  387. * Return the local port used by the UDP connection
  388. * @param h media file context
  389. * @return the local port number
  390. */
  391. int ff_udp_get_local_port(URLContext *h)
  392. {
  393. UDPContext *s = h->priv_data;
  394. return s->local_port;
  395. }
  396. /**
  397. * Return the udp file handle for select() usage to wait for several RTP
  398. * streams at the same time.
  399. * @param h media file context
  400. */
  401. static int udp_get_file_handle(URLContext *h)
  402. {
  403. UDPContext *s = h->priv_data;
  404. return s->udp_fd;
  405. }
  406. #if HAVE_PTHREAD_CANCEL
  407. static void *circular_buffer_task_rx( void *_URLContext)
  408. {
  409. URLContext *h = _URLContext;
  410. UDPContext *s = h->priv_data;
  411. int old_cancelstate;
  412. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
  413. pthread_mutex_lock(&s->mutex);
  414. if (ff_socket_nonblock(s->udp_fd, 0) < 0) {
  415. av_log(h, AV_LOG_ERROR, "Failed to set blocking mode");
  416. s->circular_buffer_error = AVERROR(EIO);
  417. goto end;
  418. }
  419. while(1) {
  420. int len;
  421. struct sockaddr_storage addr;
  422. socklen_t addr_len = sizeof(addr);
  423. pthread_mutex_unlock(&s->mutex);
  424. /* Blocking operations are always cancellation points;
  425. see "General Information" / "Thread Cancelation Overview"
  426. in Single Unix. */
  427. pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_cancelstate);
  428. len = recvfrom(s->udp_fd, s->tmp+4, sizeof(s->tmp)-4, 0, (struct sockaddr *)&addr, &addr_len);
  429. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
  430. pthread_mutex_lock(&s->mutex);
  431. if (len < 0) {
  432. if (ff_neterrno() != AVERROR(EAGAIN) && ff_neterrno() != AVERROR(EINTR)) {
  433. s->circular_buffer_error = ff_neterrno();
  434. goto end;
  435. }
  436. continue;
  437. }
  438. if (ff_ip_check_source_lists(&addr, &s->filters))
  439. continue;
  440. AV_WL32(s->tmp, len);
  441. if(av_fifo_space(s->fifo) < len + 4) {
  442. /* No Space left */
  443. if (s->overrun_nonfatal) {
  444. av_log(h, AV_LOG_WARNING, "Circular buffer overrun. "
  445. "Surviving due to overrun_nonfatal option\n");
  446. continue;
  447. } else {
  448. av_log(h, AV_LOG_ERROR, "Circular buffer overrun. "
  449. "To avoid, increase fifo_size URL option. "
  450. "To survive in such case, use overrun_nonfatal option\n");
  451. s->circular_buffer_error = AVERROR(EIO);
  452. goto end;
  453. }
  454. }
  455. av_fifo_generic_write(s->fifo, s->tmp, len+4, NULL);
  456. pthread_cond_signal(&s->cond);
  457. }
  458. end:
  459. pthread_cond_signal(&s->cond);
  460. pthread_mutex_unlock(&s->mutex);
  461. return NULL;
  462. }
  463. static void *circular_buffer_task_tx( void *_URLContext)
  464. {
  465. URLContext *h = _URLContext;
  466. UDPContext *s = h->priv_data;
  467. int old_cancelstate;
  468. int64_t target_timestamp = av_gettime_relative();
  469. int64_t start_timestamp = av_gettime_relative();
  470. int64_t sent_bits = 0;
  471. int64_t burst_interval = s->bitrate ? (s->burst_bits * 1000000 / s->bitrate) : 0;
  472. int64_t max_delay = s->bitrate ? ((int64_t)h->max_packet_size * 8 * 1000000 / s->bitrate + 1) : 0;
  473. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
  474. pthread_mutex_lock(&s->mutex);
  475. if (ff_socket_nonblock(s->udp_fd, 0) < 0) {
  476. av_log(h, AV_LOG_ERROR, "Failed to set blocking mode");
  477. s->circular_buffer_error = AVERROR(EIO);
  478. goto end;
  479. }
  480. for(;;) {
  481. int len;
  482. const uint8_t *p;
  483. uint8_t tmp[4];
  484. int64_t timestamp;
  485. len=av_fifo_size(s->fifo);
  486. while (len<4) {
  487. if (s->close_req)
  488. goto end;
  489. if (pthread_cond_wait(&s->cond, &s->mutex) < 0) {
  490. goto end;
  491. }
  492. len=av_fifo_size(s->fifo);
  493. }
  494. av_fifo_generic_read(s->fifo, tmp, 4, NULL);
  495. len=AV_RL32(tmp);
  496. av_assert0(len >= 0);
  497. av_assert0(len <= sizeof(s->tmp));
  498. av_fifo_generic_read(s->fifo, s->tmp, len, NULL);
  499. pthread_mutex_unlock(&s->mutex);
  500. pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_cancelstate);
  501. if (s->bitrate) {
  502. timestamp = av_gettime_relative();
  503. if (timestamp < target_timestamp) {
  504. int64_t delay = target_timestamp - timestamp;
  505. if (delay > max_delay) {
  506. delay = max_delay;
  507. start_timestamp = timestamp + delay;
  508. sent_bits = 0;
  509. }
  510. av_usleep(delay);
  511. } else {
  512. if (timestamp - burst_interval > target_timestamp) {
  513. start_timestamp = timestamp - burst_interval;
  514. sent_bits = 0;
  515. }
  516. }
  517. sent_bits += len * 8;
  518. target_timestamp = start_timestamp + sent_bits * 1000000 / s->bitrate;
  519. }
  520. p = s->tmp;
  521. while (len) {
  522. int ret;
  523. av_assert0(len > 0);
  524. if (!s->is_connected) {
  525. ret = sendto (s->udp_fd, p, len, 0,
  526. (struct sockaddr *) &s->dest_addr,
  527. s->dest_addr_len);
  528. } else
  529. ret = send(s->udp_fd, p, len, 0);
  530. if (ret >= 0) {
  531. len -= ret;
  532. p += ret;
  533. } else {
  534. ret = ff_neterrno();
  535. if (ret != AVERROR(EAGAIN) && ret != AVERROR(EINTR)) {
  536. pthread_mutex_lock(&s->mutex);
  537. s->circular_buffer_error = ret;
  538. pthread_mutex_unlock(&s->mutex);
  539. return NULL;
  540. }
  541. }
  542. }
  543. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
  544. pthread_mutex_lock(&s->mutex);
  545. }
  546. end:
  547. pthread_mutex_unlock(&s->mutex);
  548. return NULL;
  549. }
  550. #endif
  551. /* put it in UDP context */
  552. /* return non zero if error */
  553. static int udp_open(URLContext *h, const char *uri, int flags)
  554. {
  555. char hostname[1024], localaddr[1024] = "";
  556. int port, udp_fd = -1, tmp, bind_ret = -1, dscp = -1;
  557. UDPContext *s = h->priv_data;
  558. int is_output;
  559. const char *p;
  560. char buf[256];
  561. struct sockaddr_storage my_addr;
  562. socklen_t len;
  563. h->is_streamed = 1;
  564. is_output = !(flags & AVIO_FLAG_READ);
  565. if (s->buffer_size < 0)
  566. s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_MAX_PKT_SIZE;
  567. if (s->sources) {
  568. if (ff_ip_parse_sources(h, s->sources, &s->filters) < 0)
  569. goto fail;
  570. }
  571. if (s->block) {
  572. if (ff_ip_parse_blocks(h, s->block, &s->filters) < 0)
  573. goto fail;
  574. }
  575. if (s->pkt_size > 0)
  576. h->max_packet_size = s->pkt_size;
  577. p = strchr(uri, '?');
  578. if (p) {
  579. if (av_find_info_tag(buf, sizeof(buf), "reuse", p)) {
  580. char *endptr = NULL;
  581. s->reuse_socket = strtol(buf, &endptr, 10);
  582. /* assume if no digits were found it is a request to enable it */
  583. if (buf == endptr)
  584. s->reuse_socket = 1;
  585. }
  586. if (av_find_info_tag(buf, sizeof(buf), "overrun_nonfatal", p)) {
  587. char *endptr = NULL;
  588. s->overrun_nonfatal = strtol(buf, &endptr, 10);
  589. /* assume if no digits were found it is a request to enable it */
  590. if (buf == endptr)
  591. s->overrun_nonfatal = 1;
  592. if (!HAVE_PTHREAD_CANCEL)
  593. av_log(h, AV_LOG_WARNING,
  594. "'overrun_nonfatal' option was set but it is not supported "
  595. "on this build (pthread support is required)\n");
  596. }
  597. if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
  598. s->ttl = strtol(buf, NULL, 10);
  599. }
  600. if (av_find_info_tag(buf, sizeof(buf), "udplite_coverage", p)) {
  601. s->udplite_coverage = strtol(buf, NULL, 10);
  602. }
  603. if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
  604. s->local_port = strtol(buf, NULL, 10);
  605. }
  606. if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
  607. s->pkt_size = strtol(buf, NULL, 10);
  608. }
  609. if (av_find_info_tag(buf, sizeof(buf), "buffer_size", p)) {
  610. s->buffer_size = strtol(buf, NULL, 10);
  611. }
  612. if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
  613. s->is_connected = strtol(buf, NULL, 10);
  614. }
  615. if (av_find_info_tag(buf, sizeof(buf), "dscp", p)) {
  616. dscp = strtol(buf, NULL, 10);
  617. }
  618. if (av_find_info_tag(buf, sizeof(buf), "fifo_size", p)) {
  619. s->circular_buffer_size = strtol(buf, NULL, 10);
  620. if (!HAVE_PTHREAD_CANCEL)
  621. av_log(h, AV_LOG_WARNING,
  622. "'circular_buffer_size' option was set but it is not supported "
  623. "on this build (pthread support is required)\n");
  624. }
  625. if (av_find_info_tag(buf, sizeof(buf), "bitrate", p)) {
  626. s->bitrate = strtoll(buf, NULL, 10);
  627. if (!HAVE_PTHREAD_CANCEL)
  628. av_log(h, AV_LOG_WARNING,
  629. "'bitrate' option was set but it is not supported "
  630. "on this build (pthread support is required)\n");
  631. }
  632. if (av_find_info_tag(buf, sizeof(buf), "burst_bits", p)) {
  633. s->burst_bits = strtoll(buf, NULL, 10);
  634. }
  635. if (av_find_info_tag(buf, sizeof(buf), "localaddr", p)) {
  636. av_strlcpy(localaddr, buf, sizeof(localaddr));
  637. }
  638. if (av_find_info_tag(buf, sizeof(buf), "sources", p)) {
  639. if (ff_ip_parse_sources(h, buf, &s->filters) < 0)
  640. goto fail;
  641. }
  642. if (av_find_info_tag(buf, sizeof(buf), "block", p)) {
  643. if (ff_ip_parse_blocks(h, buf, &s->filters) < 0)
  644. goto fail;
  645. }
  646. if (!is_output && av_find_info_tag(buf, sizeof(buf), "timeout", p))
  647. s->timeout = strtol(buf, NULL, 10);
  648. if (is_output && av_find_info_tag(buf, sizeof(buf), "broadcast", p))
  649. s->is_broadcast = strtol(buf, NULL, 10);
  650. }
  651. /* handling needed to support options picking from both AVOption and URL */
  652. s->circular_buffer_size *= 188;
  653. if (flags & AVIO_FLAG_WRITE) {
  654. h->max_packet_size = s->pkt_size;
  655. } else {
  656. h->max_packet_size = UDP_MAX_PKT_SIZE;
  657. }
  658. h->rw_timeout = s->timeout;
  659. /* fill the dest addr */
  660. av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
  661. /* XXX: fix av_url_split */
  662. if (hostname[0] == '\0' || hostname[0] == '?') {
  663. /* only accepts null hostname if input */
  664. if (!(flags & AVIO_FLAG_READ))
  665. goto fail;
  666. } else {
  667. if (ff_udp_set_remote_url(h, uri) < 0)
  668. goto fail;
  669. }
  670. if ((s->is_multicast || s->local_port <= 0) && (h->flags & AVIO_FLAG_READ))
  671. s->local_port = port;
  672. if (localaddr[0])
  673. udp_fd = udp_socket_create(h, &my_addr, &len, localaddr);
  674. else
  675. udp_fd = udp_socket_create(h, &my_addr, &len, s->localaddr);
  676. if (udp_fd < 0)
  677. goto fail;
  678. s->local_addr_storage=my_addr; //store for future multicast join
  679. /* Follow the requested reuse option, unless it's multicast in which
  680. * case enable reuse unless explicitly disabled.
  681. */
  682. if (s->reuse_socket > 0 || (s->is_multicast && s->reuse_socket < 0)) {
  683. s->reuse_socket = 1;
  684. if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
  685. goto fail;
  686. }
  687. if (s->is_broadcast) {
  688. #ifdef SO_BROADCAST
  689. if (setsockopt (udp_fd, SOL_SOCKET, SO_BROADCAST, &(s->is_broadcast), sizeof(s->is_broadcast)) != 0)
  690. #endif
  691. goto fail;
  692. }
  693. /* Set the checksum coverage for UDP-Lite (RFC 3828) for sending and receiving.
  694. * The receiver coverage has to be less than or equal to the sender coverage.
  695. * Otherwise, the receiver will drop all packets.
  696. */
  697. if (s->udplite_coverage) {
  698. if (setsockopt (udp_fd, IPPROTO_UDPLITE, UDPLITE_SEND_CSCOV, &(s->udplite_coverage), sizeof(s->udplite_coverage)) != 0)
  699. av_log(h, AV_LOG_WARNING, "socket option UDPLITE_SEND_CSCOV not available");
  700. if (setsockopt (udp_fd, IPPROTO_UDPLITE, UDPLITE_RECV_CSCOV, &(s->udplite_coverage), sizeof(s->udplite_coverage)) != 0)
  701. av_log(h, AV_LOG_WARNING, "socket option UDPLITE_RECV_CSCOV not available");
  702. }
  703. if (dscp >= 0) {
  704. dscp <<= 2;
  705. if (setsockopt (udp_fd, IPPROTO_IP, IP_TOS, &dscp, sizeof(dscp)) != 0)
  706. goto fail;
  707. }
  708. /* If multicast, try binding the multicast address first, to avoid
  709. * receiving UDP packets from other sources aimed at the same UDP
  710. * port. This fails on windows. This makes sending to the same address
  711. * using sendto() fail, so only do it if we're opened in read-only mode. */
  712. if (s->is_multicast && !(h->flags & AVIO_FLAG_WRITE)) {
  713. bind_ret = bind(udp_fd,(struct sockaddr *)&s->dest_addr, len);
  714. }
  715. /* bind to the local address if not multicast or if the multicast
  716. * bind failed */
  717. /* the bind is needed to give a port to the socket now */
  718. if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0) {
  719. ff_log_net_error(h, AV_LOG_ERROR, "bind failed");
  720. goto fail;
  721. }
  722. len = sizeof(my_addr);
  723. getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
  724. s->local_port = udp_port(&my_addr, len);
  725. if (s->is_multicast) {
  726. if (h->flags & AVIO_FLAG_WRITE) {
  727. /* output */
  728. if (udp_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
  729. goto fail;
  730. }
  731. if (h->flags & AVIO_FLAG_READ) {
  732. /* input */
  733. if (s->filters.nb_include_addrs) {
  734. if (udp_set_multicast_sources(h, udp_fd,
  735. (struct sockaddr *)&s->dest_addr,
  736. s->dest_addr_len, &s->local_addr_storage,
  737. s->filters.include_addrs,
  738. s->filters.nb_include_addrs, 1) < 0)
  739. goto fail;
  740. } else {
  741. if (udp_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr,(struct sockaddr *)&s->local_addr_storage) < 0)
  742. goto fail;
  743. }
  744. if (s->filters.nb_exclude_addrs) {
  745. if (udp_set_multicast_sources(h, udp_fd,
  746. (struct sockaddr *)&s->dest_addr,
  747. s->dest_addr_len, &s->local_addr_storage,
  748. s->filters.exclude_addrs,
  749. s->filters.nb_exclude_addrs, 0) < 0)
  750. goto fail;
  751. }
  752. }
  753. }
  754. if (is_output) {
  755. /* limit the tx buf size to limit latency */
  756. tmp = s->buffer_size;
  757. if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
  758. ff_log_net_error(h, AV_LOG_ERROR, "setsockopt(SO_SNDBUF)");
  759. goto fail;
  760. }
  761. } else {
  762. /* set udp recv buffer size to the requested value (default 64K) */
  763. tmp = s->buffer_size;
  764. if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) {
  765. ff_log_net_error(h, AV_LOG_WARNING, "setsockopt(SO_RECVBUF)");
  766. }
  767. len = sizeof(tmp);
  768. if (getsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, &len) < 0) {
  769. ff_log_net_error(h, AV_LOG_WARNING, "getsockopt(SO_RCVBUF)");
  770. } else {
  771. av_log(h, AV_LOG_DEBUG, "end receive buffer size reported is %d\n", tmp);
  772. if(tmp < s->buffer_size)
  773. av_log(h, AV_LOG_WARNING, "attempted to set receive buffer to size %d but it only ended up set as %d", s->buffer_size, tmp);
  774. }
  775. /* make the socket non-blocking */
  776. ff_socket_nonblock(udp_fd, 1);
  777. }
  778. if (s->is_connected) {
  779. if (connect(udp_fd, (struct sockaddr *) &s->dest_addr, s->dest_addr_len)) {
  780. ff_log_net_error(h, AV_LOG_ERROR, "connect");
  781. goto fail;
  782. }
  783. }
  784. s->udp_fd = udp_fd;
  785. #if HAVE_PTHREAD_CANCEL
  786. /*
  787. Create thread in case of:
  788. 1. Input and circular_buffer_size is set
  789. 2. Output and bitrate and circular_buffer_size is set
  790. */
  791. if (is_output && s->bitrate && !s->circular_buffer_size) {
  792. /* Warn user in case of 'circular_buffer_size' is not set */
  793. av_log(h, AV_LOG_WARNING,"'bitrate' option was set but 'circular_buffer_size' is not, but required\n");
  794. }
  795. if ((!is_output && s->circular_buffer_size) || (is_output && s->bitrate && s->circular_buffer_size)) {
  796. int ret;
  797. /* start the task going */
  798. s->fifo = av_fifo_alloc(s->circular_buffer_size);
  799. ret = pthread_mutex_init(&s->mutex, NULL);
  800. if (ret != 0) {
  801. av_log(h, AV_LOG_ERROR, "pthread_mutex_init failed : %s\n", strerror(ret));
  802. goto fail;
  803. }
  804. ret = pthread_cond_init(&s->cond, NULL);
  805. if (ret != 0) {
  806. av_log(h, AV_LOG_ERROR, "pthread_cond_init failed : %s\n", strerror(ret));
  807. goto cond_fail;
  808. }
  809. ret = pthread_create(&s->circular_buffer_thread, NULL, is_output?circular_buffer_task_tx:circular_buffer_task_rx, h);
  810. if (ret != 0) {
  811. av_log(h, AV_LOG_ERROR, "pthread_create failed : %s\n", strerror(ret));
  812. goto thread_fail;
  813. }
  814. s->thread_started = 1;
  815. }
  816. #endif
  817. return 0;
  818. #if HAVE_PTHREAD_CANCEL
  819. thread_fail:
  820. pthread_cond_destroy(&s->cond);
  821. cond_fail:
  822. pthread_mutex_destroy(&s->mutex);
  823. #endif
  824. fail:
  825. if (udp_fd >= 0)
  826. closesocket(udp_fd);
  827. av_fifo_freep(&s->fifo);
  828. ff_ip_reset_filters(&s->filters);
  829. return AVERROR(EIO);
  830. }
  831. static int udplite_open(URLContext *h, const char *uri, int flags)
  832. {
  833. UDPContext *s = h->priv_data;
  834. // set default checksum coverage
  835. s->udplite_coverage = UDP_HEADER_SIZE;
  836. return udp_open(h, uri, flags);
  837. }
  838. static int udp_read(URLContext *h, uint8_t *buf, int size)
  839. {
  840. UDPContext *s = h->priv_data;
  841. int ret;
  842. struct sockaddr_storage addr;
  843. socklen_t addr_len = sizeof(addr);
  844. #if HAVE_PTHREAD_CANCEL
  845. int avail, nonblock = h->flags & AVIO_FLAG_NONBLOCK;
  846. if (s->fifo) {
  847. pthread_mutex_lock(&s->mutex);
  848. do {
  849. avail = av_fifo_size(s->fifo);
  850. if (avail) { // >=size) {
  851. uint8_t tmp[4];
  852. av_fifo_generic_read(s->fifo, tmp, 4, NULL);
  853. avail= AV_RL32(tmp);
  854. if(avail > size){
  855. av_log(h, AV_LOG_WARNING, "Part of datagram lost due to insufficient buffer size\n");
  856. avail= size;
  857. }
  858. av_fifo_generic_read(s->fifo, buf, avail, NULL);
  859. av_fifo_drain(s->fifo, AV_RL32(tmp) - avail);
  860. pthread_mutex_unlock(&s->mutex);
  861. return avail;
  862. } else if(s->circular_buffer_error){
  863. int err = s->circular_buffer_error;
  864. pthread_mutex_unlock(&s->mutex);
  865. return err;
  866. } else if(nonblock) {
  867. pthread_mutex_unlock(&s->mutex);
  868. return AVERROR(EAGAIN);
  869. }
  870. else {
  871. /* FIXME: using the monotonic clock would be better,
  872. but it does not exist on all supported platforms. */
  873. int64_t t = av_gettime() + 100000;
  874. struct timespec tv = { .tv_sec = t / 1000000,
  875. .tv_nsec = (t % 1000000) * 1000 };
  876. if (pthread_cond_timedwait(&s->cond, &s->mutex, &tv) < 0) {
  877. pthread_mutex_unlock(&s->mutex);
  878. return AVERROR(errno == ETIMEDOUT ? EAGAIN : errno);
  879. }
  880. nonblock = 1;
  881. }
  882. } while( 1);
  883. }
  884. #endif
  885. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  886. ret = ff_network_wait_fd(s->udp_fd, 0);
  887. if (ret < 0)
  888. return ret;
  889. }
  890. ret = recvfrom(s->udp_fd, buf, size, 0, (struct sockaddr *)&addr, &addr_len);
  891. if (ret < 0)
  892. return ff_neterrno();
  893. if (ff_ip_check_source_lists(&addr, &s->filters))
  894. return AVERROR(EINTR);
  895. return ret;
  896. }
  897. static int udp_write(URLContext *h, const uint8_t *buf, int size)
  898. {
  899. UDPContext *s = h->priv_data;
  900. int ret;
  901. #if HAVE_PTHREAD_CANCEL
  902. if (s->fifo) {
  903. uint8_t tmp[4];
  904. pthread_mutex_lock(&s->mutex);
  905. /*
  906. Return error if last tx failed.
  907. Here we can't know on which packet error was, but it needs to know that error exists.
  908. */
  909. if (s->circular_buffer_error<0) {
  910. int err=s->circular_buffer_error;
  911. pthread_mutex_unlock(&s->mutex);
  912. return err;
  913. }
  914. if(av_fifo_space(s->fifo) < size + 4) {
  915. /* What about a partial packet tx ? */
  916. pthread_mutex_unlock(&s->mutex);
  917. return AVERROR(ENOMEM);
  918. }
  919. AV_WL32(tmp, size);
  920. av_fifo_generic_write(s->fifo, tmp, 4, NULL); /* size of packet */
  921. av_fifo_generic_write(s->fifo, (uint8_t *)buf, size, NULL); /* the data */
  922. pthread_cond_signal(&s->cond);
  923. pthread_mutex_unlock(&s->mutex);
  924. return size;
  925. }
  926. #endif
  927. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  928. ret = ff_network_wait_fd(s->udp_fd, 1);
  929. if (ret < 0)
  930. return ret;
  931. }
  932. if (!s->is_connected) {
  933. ret = sendto (s->udp_fd, buf, size, 0,
  934. (struct sockaddr *) &s->dest_addr,
  935. s->dest_addr_len);
  936. } else
  937. ret = send(s->udp_fd, buf, size, 0);
  938. return ret < 0 ? ff_neterrno() : ret;
  939. }
  940. static int udp_close(URLContext *h)
  941. {
  942. UDPContext *s = h->priv_data;
  943. #if HAVE_PTHREAD_CANCEL
  944. // Request close once writing is finished
  945. if (s->thread_started && !(h->flags & AVIO_FLAG_READ)) {
  946. pthread_mutex_lock(&s->mutex);
  947. s->close_req = 1;
  948. pthread_cond_signal(&s->cond);
  949. pthread_mutex_unlock(&s->mutex);
  950. }
  951. #endif
  952. if (s->is_multicast && (h->flags & AVIO_FLAG_READ))
  953. udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr,(struct sockaddr *)&s->local_addr_storage);
  954. #if HAVE_PTHREAD_CANCEL
  955. if (s->thread_started) {
  956. int ret;
  957. // Cancel only read, as write has been signaled as success to the user
  958. if (h->flags & AVIO_FLAG_READ)
  959. pthread_cancel(s->circular_buffer_thread);
  960. ret = pthread_join(s->circular_buffer_thread, NULL);
  961. if (ret != 0)
  962. av_log(h, AV_LOG_ERROR, "pthread_join(): %s\n", strerror(ret));
  963. pthread_mutex_destroy(&s->mutex);
  964. pthread_cond_destroy(&s->cond);
  965. }
  966. #endif
  967. closesocket(s->udp_fd);
  968. av_fifo_freep(&s->fifo);
  969. ff_ip_reset_filters(&s->filters);
  970. return 0;
  971. }
  972. const URLProtocol ff_udp_protocol = {
  973. .name = "udp",
  974. .url_open = udp_open,
  975. .url_read = udp_read,
  976. .url_write = udp_write,
  977. .url_close = udp_close,
  978. .url_get_file_handle = udp_get_file_handle,
  979. .priv_data_size = sizeof(UDPContext),
  980. .priv_data_class = &udp_class,
  981. .flags = URL_PROTOCOL_FLAG_NETWORK,
  982. };
  983. const URLProtocol ff_udplite_protocol = {
  984. .name = "udplite",
  985. .url_open = udplite_open,
  986. .url_read = udp_read,
  987. .url_write = udp_write,
  988. .url_close = udp_close,
  989. .url_get_file_handle = udp_get_file_handle,
  990. .priv_data_size = sizeof(UDPContext),
  991. .priv_data_class = &udplite_context_class,
  992. .flags = URL_PROTOCOL_FLAG_NETWORK,
  993. };