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.

635 lines
21KB

  1. /*
  2. * UDP prototype streaming system
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 _BSD_SOURCE /* Needed for using struct ip_mreq with recent glibc */
  26. #include "avformat.h"
  27. #include "avio_internal.h"
  28. #include "libavutil/parseutils.h"
  29. #include "libavutil/avstring.h"
  30. #include "internal.h"
  31. #include "network.h"
  32. #include "os_support.h"
  33. #include "url.h"
  34. #ifndef IPV6_ADD_MEMBERSHIP
  35. #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
  36. #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
  37. #endif
  38. typedef struct UDPContext {
  39. int udp_fd;
  40. int ttl;
  41. int buffer_size;
  42. int is_multicast;
  43. int local_port;
  44. int reuse_socket;
  45. struct sockaddr_storage dest_addr;
  46. int dest_addr_len;
  47. int is_connected;
  48. } UDPContext;
  49. #define UDP_TX_BUF_SIZE 32768
  50. #define UDP_MAX_PKT_SIZE 65536
  51. static void log_net_error(void *ctx, int level, const char* prefix)
  52. {
  53. char errbuf[100];
  54. av_strerror(ff_neterrno(), errbuf, sizeof(errbuf));
  55. av_log(ctx, level, "%s: %s\n", prefix, errbuf);
  56. }
  57. static int udp_set_multicast_ttl(int sockfd, int mcastTTL,
  58. struct sockaddr *addr)
  59. {
  60. #ifdef IP_MULTICAST_TTL
  61. if (addr->sa_family == AF_INET) {
  62. if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
  63. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_MULTICAST_TTL)");
  64. return -1;
  65. }
  66. }
  67. #endif
  68. #if defined(IPPROTO_IPV6) && defined(IPV6_MULTICAST_HOPS)
  69. if (addr->sa_family == AF_INET6) {
  70. if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
  71. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IPV6_MULTICAST_HOPS)");
  72. return -1;
  73. }
  74. }
  75. #endif
  76. return 0;
  77. }
  78. static int udp_join_multicast_group(int sockfd, struct sockaddr *addr)
  79. {
  80. #ifdef IP_ADD_MEMBERSHIP
  81. if (addr->sa_family == AF_INET) {
  82. struct ip_mreq mreq;
  83. mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
  84. mreq.imr_interface.s_addr= INADDR_ANY;
  85. if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
  86. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_ADD_MEMBERSHIP)");
  87. return -1;
  88. }
  89. }
  90. #endif
  91. #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
  92. if (addr->sa_family == AF_INET6) {
  93. struct ipv6_mreq mreq6;
  94. memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
  95. mreq6.ipv6mr_interface= 0;
  96. if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
  97. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IPV6_ADD_MEMBERSHIP)");
  98. return -1;
  99. }
  100. }
  101. #endif
  102. return 0;
  103. }
  104. static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr)
  105. {
  106. #ifdef IP_DROP_MEMBERSHIP
  107. if (addr->sa_family == AF_INET) {
  108. struct ip_mreq mreq;
  109. mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
  110. mreq.imr_interface.s_addr= INADDR_ANY;
  111. if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
  112. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_DROP_MEMBERSHIP)");
  113. return -1;
  114. }
  115. }
  116. #endif
  117. #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
  118. if (addr->sa_family == AF_INET6) {
  119. struct ipv6_mreq mreq6;
  120. memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
  121. mreq6.ipv6mr_interface= 0;
  122. if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
  123. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IPV6_DROP_MEMBERSHIP)");
  124. return -1;
  125. }
  126. }
  127. #endif
  128. return 0;
  129. }
  130. static struct addrinfo* udp_resolve_host(const char *hostname, int port,
  131. int type, int family, int flags)
  132. {
  133. struct addrinfo hints = { 0 }, *res = 0;
  134. int error;
  135. char sport[16];
  136. const char *node = 0, *service = "0";
  137. if (port > 0) {
  138. snprintf(sport, sizeof(sport), "%d", port);
  139. service = sport;
  140. }
  141. if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
  142. node = hostname;
  143. }
  144. hints.ai_socktype = type;
  145. hints.ai_family = family;
  146. hints.ai_flags = flags;
  147. if ((error = getaddrinfo(node, service, &hints, &res))) {
  148. res = NULL;
  149. av_log(NULL, AV_LOG_ERROR, "udp_resolve_host: %s\n", gai_strerror(error));
  150. }
  151. return res;
  152. }
  153. static int udp_set_multicast_sources(int sockfd, struct sockaddr *addr,
  154. int addr_len, char **sources,
  155. int nb_sources, int include)
  156. {
  157. #if HAVE_STRUCT_GROUP_SOURCE_REQ && defined(MCAST_BLOCK_SOURCE) && !defined(_WIN32)
  158. /* These ones are available in the microsoft SDK, but don't seem to work
  159. * as on linux, so just prefer the v4-only approach there for now. */
  160. int i;
  161. for (i = 0; i < nb_sources; i++) {
  162. struct group_source_req mreqs;
  163. int level = addr->sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
  164. struct addrinfo *sourceaddr = udp_resolve_host(sources[i], 0,
  165. SOCK_DGRAM, AF_UNSPEC,
  166. 0);
  167. if (!sourceaddr)
  168. return AVERROR(ENOENT);
  169. mreqs.gsr_interface = 0;
  170. memcpy(&mreqs.gsr_group, addr, addr_len);
  171. memcpy(&mreqs.gsr_source, sourceaddr->ai_addr, sourceaddr->ai_addrlen);
  172. freeaddrinfo(sourceaddr);
  173. if (setsockopt(sockfd, level,
  174. include ? MCAST_JOIN_SOURCE_GROUP : MCAST_BLOCK_SOURCE,
  175. (const void *)&mreqs, sizeof(mreqs)) < 0) {
  176. if (include)
  177. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(MCAST_JOIN_SOURCE_GROUP)");
  178. else
  179. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(MCAST_BLOCK_SOURCE)");
  180. return ff_neterrno();
  181. }
  182. }
  183. #elif HAVE_STRUCT_IP_MREQ_SOURCE && defined(IP_BLOCK_SOURCE)
  184. int i;
  185. if (addr->sa_family != AF_INET) {
  186. av_log(NULL, AV_LOG_ERROR,
  187. "Setting multicast sources only supported for IPv4\n");
  188. return AVERROR(EINVAL);
  189. }
  190. for (i = 0; i < nb_sources; i++) {
  191. struct ip_mreq_source mreqs;
  192. struct addrinfo *sourceaddr = udp_resolve_host(sources[i], 0,
  193. SOCK_DGRAM, AF_UNSPEC,
  194. 0);
  195. if (!sourceaddr)
  196. return AVERROR(ENOENT);
  197. if (sourceaddr->ai_addr->sa_family != AF_INET) {
  198. freeaddrinfo(sourceaddr);
  199. av_log(NULL, AV_LOG_ERROR, "%s is of incorrect protocol family\n",
  200. sources[i]);
  201. return AVERROR(EINVAL);
  202. }
  203. mreqs.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
  204. mreqs.imr_interface.s_addr = INADDR_ANY;
  205. mreqs.imr_sourceaddr.s_addr = ((struct sockaddr_in *)sourceaddr->ai_addr)->sin_addr.s_addr;
  206. freeaddrinfo(sourceaddr);
  207. if (setsockopt(sockfd, IPPROTO_IP,
  208. include ? IP_ADD_SOURCE_MEMBERSHIP : IP_BLOCK_SOURCE,
  209. (const void *)&mreqs, sizeof(mreqs)) < 0) {
  210. if (include)
  211. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_ADD_SOURCE_MEMBERSHIP)");
  212. else
  213. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_BLOCK_SOURCE)");
  214. return ff_neterrno();
  215. }
  216. }
  217. #else
  218. return AVERROR(ENOSYS);
  219. #endif
  220. return 0;
  221. }
  222. static int udp_set_url(struct sockaddr_storage *addr,
  223. const char *hostname, int port)
  224. {
  225. struct addrinfo *res0;
  226. int addr_len;
  227. res0 = udp_resolve_host(hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
  228. if (res0 == 0) return AVERROR(EIO);
  229. memcpy(addr, res0->ai_addr, res0->ai_addrlen);
  230. addr_len = res0->ai_addrlen;
  231. freeaddrinfo(res0);
  232. return addr_len;
  233. }
  234. static int udp_socket_create(UDPContext *s, struct sockaddr_storage *addr,
  235. socklen_t *addr_len, const char *localaddr)
  236. {
  237. int udp_fd = -1;
  238. struct addrinfo *res0 = NULL, *res = NULL;
  239. int family = AF_UNSPEC;
  240. if (((struct sockaddr *) &s->dest_addr)->sa_family)
  241. family = ((struct sockaddr *) &s->dest_addr)->sa_family;
  242. res0 = udp_resolve_host(localaddr[0] ? localaddr : NULL, s->local_port,
  243. SOCK_DGRAM, family, AI_PASSIVE);
  244. if (res0 == 0)
  245. goto fail;
  246. for (res = res0; res; res=res->ai_next) {
  247. udp_fd = ff_socket(res->ai_family, SOCK_DGRAM, 0);
  248. if (udp_fd != -1) break;
  249. log_net_error(NULL, AV_LOG_ERROR, "socket");
  250. }
  251. if (udp_fd < 0)
  252. goto fail;
  253. memcpy(addr, res->ai_addr, res->ai_addrlen);
  254. *addr_len = res->ai_addrlen;
  255. freeaddrinfo(res0);
  256. return udp_fd;
  257. fail:
  258. if (udp_fd >= 0)
  259. closesocket(udp_fd);
  260. if(res0)
  261. freeaddrinfo(res0);
  262. return -1;
  263. }
  264. static int udp_port(struct sockaddr_storage *addr, int addr_len)
  265. {
  266. char sbuf[sizeof(int)*3+1];
  267. int error;
  268. if ((error = getnameinfo((struct sockaddr *)addr, addr_len, NULL, 0, sbuf, sizeof(sbuf), NI_NUMERICSERV)) != 0) {
  269. av_log(NULL, AV_LOG_ERROR, "getnameinfo: %s\n", gai_strerror(error));
  270. return -1;
  271. }
  272. return strtol(sbuf, NULL, 10);
  273. }
  274. /**
  275. * If no filename is given to av_open_input_file because you want to
  276. * get the local port first, then you must call this function to set
  277. * the remote server address.
  278. *
  279. * url syntax: udp://host:port[?option=val...]
  280. * option: 'ttl=n' : set the ttl value (for multicast only)
  281. * 'localport=n' : set the local port
  282. * 'pkt_size=n' : set max packet size
  283. * 'reuse=1' : enable reusing the socket
  284. *
  285. * @param h media file context
  286. * @param uri of the remote server
  287. * @return zero if no error.
  288. */
  289. int ff_udp_set_remote_url(URLContext *h, const char *uri)
  290. {
  291. UDPContext *s = h->priv_data;
  292. char hostname[256], buf[10];
  293. int port;
  294. const char *p;
  295. av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
  296. /* set the destination address */
  297. s->dest_addr_len = udp_set_url(&s->dest_addr, hostname, port);
  298. if (s->dest_addr_len < 0) {
  299. return AVERROR(EIO);
  300. }
  301. s->is_multicast = ff_is_multicast_address((struct sockaddr*) &s->dest_addr);
  302. p = strchr(uri, '?');
  303. if (p) {
  304. if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
  305. int was_connected = s->is_connected;
  306. s->is_connected = strtol(buf, NULL, 10);
  307. if (s->is_connected && !was_connected) {
  308. if (connect(s->udp_fd, (struct sockaddr *) &s->dest_addr,
  309. s->dest_addr_len)) {
  310. s->is_connected = 0;
  311. log_net_error(h, AV_LOG_ERROR, "connect");
  312. return AVERROR(EIO);
  313. }
  314. }
  315. }
  316. }
  317. return 0;
  318. }
  319. /**
  320. * Return the local port used by the UDP connection
  321. * @param h media file context
  322. * @return the local port number
  323. */
  324. int ff_udp_get_local_port(URLContext *h)
  325. {
  326. UDPContext *s = h->priv_data;
  327. return s->local_port;
  328. }
  329. /**
  330. * Return the udp file handle for select() usage to wait for several RTP
  331. * streams at the same time.
  332. * @param h media file context
  333. */
  334. static int udp_get_file_handle(URLContext *h)
  335. {
  336. UDPContext *s = h->priv_data;
  337. return s->udp_fd;
  338. }
  339. static int parse_source_list(char *buf, char **sources, int *num_sources,
  340. int max_sources)
  341. {
  342. char *source_start;
  343. source_start = buf;
  344. while (1) {
  345. char *next = strchr(source_start, ',');
  346. if (next)
  347. *next = '\0';
  348. sources[*num_sources] = av_strdup(source_start);
  349. if (!sources[*num_sources])
  350. return AVERROR(ENOMEM);
  351. source_start = next + 1;
  352. (*num_sources)++;
  353. if (*num_sources >= max_sources || !next)
  354. break;
  355. }
  356. return 0;
  357. }
  358. /* put it in UDP context */
  359. /* return non zero if error */
  360. static int udp_open(URLContext *h, const char *uri, int flags)
  361. {
  362. char hostname[1024], localaddr[1024] = "";
  363. int port, udp_fd = -1, tmp, bind_ret = -1;
  364. UDPContext *s = h->priv_data;
  365. int is_output;
  366. const char *p;
  367. char buf[256];
  368. struct sockaddr_storage my_addr;
  369. socklen_t len;
  370. int reuse_specified = 0;
  371. int i, num_include_sources = 0, num_exclude_sources = 0;
  372. char *include_sources[32], *exclude_sources[32];
  373. h->is_streamed = 1;
  374. h->max_packet_size = 1472;
  375. is_output = !(flags & AVIO_FLAG_READ);
  376. s->ttl = 16;
  377. s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_MAX_PKT_SIZE;
  378. p = strchr(uri, '?');
  379. if (p) {
  380. if (av_find_info_tag(buf, sizeof(buf), "reuse", p)) {
  381. char *endptr = NULL;
  382. s->reuse_socket = strtol(buf, &endptr, 10);
  383. /* assume if no digits were found it is a request to enable it */
  384. if (buf == endptr)
  385. s->reuse_socket = 1;
  386. reuse_specified = 1;
  387. }
  388. if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
  389. s->ttl = strtol(buf, NULL, 10);
  390. }
  391. if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
  392. s->local_port = strtol(buf, NULL, 10);
  393. }
  394. if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
  395. h->max_packet_size = strtol(buf, NULL, 10);
  396. }
  397. if (av_find_info_tag(buf, sizeof(buf), "buffer_size", p)) {
  398. s->buffer_size = strtol(buf, NULL, 10);
  399. }
  400. if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
  401. s->is_connected = strtol(buf, NULL, 10);
  402. }
  403. if (av_find_info_tag(buf, sizeof(buf), "localaddr", p)) {
  404. av_strlcpy(localaddr, buf, sizeof(localaddr));
  405. }
  406. if (av_find_info_tag(buf, sizeof(buf), "sources", p)) {
  407. if (parse_source_list(buf, include_sources, &num_include_sources,
  408. FF_ARRAY_ELEMS(include_sources)))
  409. goto fail;
  410. }
  411. if (av_find_info_tag(buf, sizeof(buf), "block", p)) {
  412. if (parse_source_list(buf, exclude_sources, &num_exclude_sources,
  413. FF_ARRAY_ELEMS(exclude_sources)))
  414. goto fail;
  415. }
  416. }
  417. /* fill the dest addr */
  418. av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
  419. /* XXX: fix av_url_split */
  420. if (hostname[0] == '\0' || hostname[0] == '?') {
  421. /* only accepts null hostname if input */
  422. if (!(flags & AVIO_FLAG_READ))
  423. goto fail;
  424. } else {
  425. if (ff_udp_set_remote_url(h, uri) < 0)
  426. goto fail;
  427. }
  428. if ((s->is_multicast || !s->local_port) && (h->flags & AVIO_FLAG_READ))
  429. s->local_port = port;
  430. udp_fd = udp_socket_create(s, &my_addr, &len, localaddr);
  431. if (udp_fd < 0)
  432. goto fail;
  433. /* Follow the requested reuse option, unless it's multicast in which
  434. * case enable reuse unless explicitly disabled.
  435. */
  436. if (s->reuse_socket || (s->is_multicast && !reuse_specified)) {
  437. s->reuse_socket = 1;
  438. if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
  439. goto fail;
  440. }
  441. /* If multicast, try binding the multicast address first, to avoid
  442. * receiving UDP packets from other sources aimed at the same UDP
  443. * port. This fails on windows. This makes sending to the same address
  444. * using sendto() fail, so only do it if we're opened in read-only mode. */
  445. if (s->is_multicast && !(h->flags & AVIO_FLAG_WRITE)) {
  446. bind_ret = bind(udp_fd,(struct sockaddr *)&s->dest_addr, len);
  447. }
  448. /* bind to the local address if not multicast or if the multicast
  449. * bind failed */
  450. /* the bind is needed to give a port to the socket now */
  451. if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0) {
  452. log_net_error(h, AV_LOG_ERROR, "bind failed");
  453. goto fail;
  454. }
  455. len = sizeof(my_addr);
  456. getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
  457. s->local_port = udp_port(&my_addr, len);
  458. if (s->is_multicast) {
  459. if (h->flags & AVIO_FLAG_WRITE) {
  460. /* output */
  461. if (udp_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
  462. goto fail;
  463. }
  464. if (h->flags & AVIO_FLAG_READ) {
  465. /* input */
  466. if (num_include_sources && num_exclude_sources) {
  467. av_log(h, AV_LOG_ERROR, "Simultaneously including and excluding multicast sources is not supported\n");
  468. goto fail;
  469. }
  470. if (num_include_sources) {
  471. if (udp_set_multicast_sources(udp_fd, (struct sockaddr *)&s->dest_addr, s->dest_addr_len, include_sources, num_include_sources, 1) < 0)
  472. goto fail;
  473. } else {
  474. if (udp_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
  475. goto fail;
  476. }
  477. if (num_exclude_sources) {
  478. if (udp_set_multicast_sources(udp_fd, (struct sockaddr *)&s->dest_addr, s->dest_addr_len, exclude_sources, num_exclude_sources, 0) < 0)
  479. goto fail;
  480. }
  481. }
  482. }
  483. if (is_output) {
  484. /* limit the tx buf size to limit latency */
  485. tmp = s->buffer_size;
  486. if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
  487. log_net_error(h, AV_LOG_ERROR, "setsockopt(SO_SNDBUF)");
  488. goto fail;
  489. }
  490. } else {
  491. /* set udp recv buffer size to the largest possible udp packet size to
  492. * avoid losing data on OSes that set this too low by default. */
  493. tmp = s->buffer_size;
  494. if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) {
  495. log_net_error(h, AV_LOG_WARNING, "setsockopt(SO_RECVBUF)");
  496. }
  497. /* make the socket non-blocking */
  498. ff_socket_nonblock(udp_fd, 1);
  499. }
  500. if (s->is_connected) {
  501. if (connect(udp_fd, (struct sockaddr *) &s->dest_addr, s->dest_addr_len)) {
  502. log_net_error(h, AV_LOG_ERROR, "connect");
  503. goto fail;
  504. }
  505. }
  506. for (i = 0; i < num_include_sources; i++)
  507. av_freep(&include_sources[i]);
  508. for (i = 0; i < num_exclude_sources; i++)
  509. av_freep(&exclude_sources[i]);
  510. s->udp_fd = udp_fd;
  511. return 0;
  512. fail:
  513. if (udp_fd >= 0)
  514. closesocket(udp_fd);
  515. for (i = 0; i < num_include_sources; i++)
  516. av_freep(&include_sources[i]);
  517. for (i = 0; i < num_exclude_sources; i++)
  518. av_freep(&exclude_sources[i]);
  519. return AVERROR(EIO);
  520. }
  521. static int udp_read(URLContext *h, uint8_t *buf, int size)
  522. {
  523. UDPContext *s = h->priv_data;
  524. int ret;
  525. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  526. ret = ff_network_wait_fd(s->udp_fd, 0);
  527. if (ret < 0)
  528. return ret;
  529. }
  530. ret = recv(s->udp_fd, buf, size, 0);
  531. return ret < 0 ? ff_neterrno() : ret;
  532. }
  533. static int udp_write(URLContext *h, const uint8_t *buf, int size)
  534. {
  535. UDPContext *s = h->priv_data;
  536. int ret;
  537. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  538. ret = ff_network_wait_fd(s->udp_fd, 1);
  539. if (ret < 0)
  540. return ret;
  541. }
  542. if (!s->is_connected) {
  543. ret = sendto (s->udp_fd, buf, size, 0,
  544. (struct sockaddr *) &s->dest_addr,
  545. s->dest_addr_len);
  546. } else
  547. ret = send(s->udp_fd, buf, size, 0);
  548. return ret < 0 ? ff_neterrno() : ret;
  549. }
  550. static int udp_close(URLContext *h)
  551. {
  552. UDPContext *s = h->priv_data;
  553. if (s->is_multicast && (h->flags & AVIO_FLAG_READ))
  554. udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
  555. closesocket(s->udp_fd);
  556. return 0;
  557. }
  558. URLProtocol ff_udp_protocol = {
  559. .name = "udp",
  560. .url_open = udp_open,
  561. .url_read = udp_read,
  562. .url_write = udp_write,
  563. .url_close = udp_close,
  564. .url_get_file_handle = udp_get_file_handle,
  565. .priv_data_size = sizeof(UDPContext),
  566. .flags = URL_PROTOCOL_FLAG_NETWORK,
  567. };