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.

619 lines
20KB

  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 {
  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. AI_NUMERICHOST);
  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. AI_NUMERICHOST);
  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. int *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 = 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. /* put it in UDP context */
  340. /* return non zero if error */
  341. static int udp_open(URLContext *h, const char *uri, int flags)
  342. {
  343. char hostname[1024], localaddr[1024] = "";
  344. int port, udp_fd = -1, tmp, bind_ret = -1;
  345. UDPContext *s = h->priv_data;
  346. int is_output;
  347. const char *p;
  348. char buf[256];
  349. struct sockaddr_storage my_addr;
  350. int len;
  351. int reuse_specified = 0;
  352. int i, include = 0, num_sources = 0;
  353. char *sources[32];
  354. h->is_streamed = 1;
  355. h->max_packet_size = 1472;
  356. is_output = !(flags & AVIO_FLAG_READ);
  357. s->ttl = 16;
  358. s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_MAX_PKT_SIZE;
  359. p = strchr(uri, '?');
  360. if (p) {
  361. if (av_find_info_tag(buf, sizeof(buf), "reuse", p)) {
  362. char *endptr = NULL;
  363. s->reuse_socket = strtol(buf, &endptr, 10);
  364. /* assume if no digits were found it is a request to enable it */
  365. if (buf == endptr)
  366. s->reuse_socket = 1;
  367. reuse_specified = 1;
  368. }
  369. if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
  370. s->ttl = strtol(buf, NULL, 10);
  371. }
  372. if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
  373. s->local_port = strtol(buf, NULL, 10);
  374. }
  375. if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
  376. h->max_packet_size = strtol(buf, NULL, 10);
  377. }
  378. if (av_find_info_tag(buf, sizeof(buf), "buffer_size", p)) {
  379. s->buffer_size = strtol(buf, NULL, 10);
  380. }
  381. if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
  382. s->is_connected = strtol(buf, NULL, 10);
  383. }
  384. if (av_find_info_tag(buf, sizeof(buf), "localaddr", p)) {
  385. av_strlcpy(localaddr, buf, sizeof(localaddr));
  386. }
  387. if (av_find_info_tag(buf, sizeof(buf), "sources", p))
  388. include = 1;
  389. if (include || av_find_info_tag(buf, sizeof(buf), "block", p)) {
  390. char *source_start;
  391. source_start = buf;
  392. while (1) {
  393. char *next = strchr(source_start, ',');
  394. if (next)
  395. *next = '\0';
  396. sources[num_sources] = av_strdup(source_start);
  397. if (!sources[num_sources])
  398. goto fail;
  399. source_start = next + 1;
  400. num_sources++;
  401. if (num_sources >= FF_ARRAY_ELEMS(sources) || !next)
  402. break;
  403. }
  404. }
  405. }
  406. /* fill the dest addr */
  407. av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
  408. /* XXX: fix av_url_split */
  409. if (hostname[0] == '\0' || hostname[0] == '?') {
  410. /* only accepts null hostname if input */
  411. if (!(flags & AVIO_FLAG_READ))
  412. goto fail;
  413. } else {
  414. if (ff_udp_set_remote_url(h, uri) < 0)
  415. goto fail;
  416. }
  417. if ((s->is_multicast || !s->local_port) && (h->flags & AVIO_FLAG_READ))
  418. s->local_port = port;
  419. udp_fd = udp_socket_create(s, &my_addr, &len, localaddr);
  420. if (udp_fd < 0)
  421. goto fail;
  422. /* Follow the requested reuse option, unless it's multicast in which
  423. * case enable reuse unless explicitly disabled.
  424. */
  425. if (s->reuse_socket || (s->is_multicast && !reuse_specified)) {
  426. s->reuse_socket = 1;
  427. if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
  428. goto fail;
  429. }
  430. /* If multicast, try binding the multicast address first, to avoid
  431. * receiving UDP packets from other sources aimed at the same UDP
  432. * port. This fails on windows. This makes sending to the same address
  433. * using sendto() fail, so only do it if we're opened in read-only mode. */
  434. if (s->is_multicast && !(h->flags & AVIO_FLAG_WRITE)) {
  435. bind_ret = bind(udp_fd,(struct sockaddr *)&s->dest_addr, len);
  436. }
  437. /* bind to the local address if not multicast or if the multicast
  438. * bind failed */
  439. /* the bind is needed to give a port to the socket now */
  440. if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0) {
  441. log_net_error(h, AV_LOG_ERROR, "bind failed");
  442. goto fail;
  443. }
  444. len = sizeof(my_addr);
  445. getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
  446. s->local_port = udp_port(&my_addr, len);
  447. if (s->is_multicast) {
  448. if (h->flags & AVIO_FLAG_WRITE) {
  449. /* output */
  450. if (udp_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
  451. goto fail;
  452. }
  453. if (h->flags & AVIO_FLAG_READ) {
  454. /* input */
  455. if (num_sources == 0 || !include) {
  456. if (udp_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
  457. goto fail;
  458. if (num_sources) {
  459. if (udp_set_multicast_sources(udp_fd, (struct sockaddr *)&s->dest_addr, s->dest_addr_len, sources, num_sources, 0) < 0)
  460. goto fail;
  461. }
  462. } else if (include && num_sources) {
  463. if (udp_set_multicast_sources(udp_fd, (struct sockaddr *)&s->dest_addr, s->dest_addr_len, sources, num_sources, 1) < 0)
  464. goto fail;
  465. } else {
  466. av_log(NULL, AV_LOG_ERROR, "invalid udp settings: inclusive multicast but no sources given\n");
  467. goto fail;
  468. }
  469. }
  470. }
  471. if (is_output) {
  472. /* limit the tx buf size to limit latency */
  473. tmp = s->buffer_size;
  474. if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
  475. log_net_error(h, AV_LOG_ERROR, "setsockopt(SO_SNDBUF)");
  476. goto fail;
  477. }
  478. } else {
  479. /* set udp recv buffer size to the largest possible udp packet size to
  480. * avoid losing data on OSes that set this too low by default. */
  481. tmp = s->buffer_size;
  482. if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) {
  483. log_net_error(h, AV_LOG_WARNING, "setsockopt(SO_RECVBUF)");
  484. }
  485. /* make the socket non-blocking */
  486. ff_socket_nonblock(udp_fd, 1);
  487. }
  488. if (s->is_connected) {
  489. if (connect(udp_fd, (struct sockaddr *) &s->dest_addr, s->dest_addr_len)) {
  490. log_net_error(h, AV_LOG_ERROR, "connect");
  491. goto fail;
  492. }
  493. }
  494. for (i = 0; i < num_sources; i++)
  495. av_free(sources[i]);
  496. s->udp_fd = udp_fd;
  497. return 0;
  498. fail:
  499. if (udp_fd >= 0)
  500. closesocket(udp_fd);
  501. for (i = 0; i < num_sources; i++)
  502. av_free(sources[i]);
  503. return AVERROR(EIO);
  504. }
  505. static int udp_read(URLContext *h, uint8_t *buf, int size)
  506. {
  507. UDPContext *s = h->priv_data;
  508. int ret;
  509. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  510. ret = ff_network_wait_fd(s->udp_fd, 0);
  511. if (ret < 0)
  512. return ret;
  513. }
  514. ret = recv(s->udp_fd, buf, size, 0);
  515. return ret < 0 ? ff_neterrno() : ret;
  516. }
  517. static int udp_write(URLContext *h, const uint8_t *buf, int size)
  518. {
  519. UDPContext *s = h->priv_data;
  520. int ret;
  521. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  522. ret = ff_network_wait_fd(s->udp_fd, 1);
  523. if (ret < 0)
  524. return ret;
  525. }
  526. if (!s->is_connected) {
  527. ret = sendto (s->udp_fd, buf, size, 0,
  528. (struct sockaddr *) &s->dest_addr,
  529. s->dest_addr_len);
  530. } else
  531. ret = send(s->udp_fd, buf, size, 0);
  532. return ret < 0 ? ff_neterrno() : ret;
  533. }
  534. static int udp_close(URLContext *h)
  535. {
  536. UDPContext *s = h->priv_data;
  537. if (s->is_multicast && (h->flags & AVIO_FLAG_READ))
  538. udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
  539. closesocket(s->udp_fd);
  540. return 0;
  541. }
  542. URLProtocol ff_udp_protocol = {
  543. .name = "udp",
  544. .url_open = udp_open,
  545. .url_read = udp_read,
  546. .url_write = udp_write,
  547. .url_close = udp_close,
  548. .url_get_file_handle = udp_get_file_handle,
  549. .priv_data_size = sizeof(UDPContext),
  550. .flags = URL_PROTOCOL_FLAG_NETWORK,
  551. };