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.

156 lines
4.6KB

  1. /*
  2. * Unix socket protocol
  3. * Copyright (c) 2013 Luca Barbato
  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. *
  24. * Unix socket url_protocol
  25. *
  26. */
  27. #include <sys/un.h>
  28. #include "libavutil/avstring.h"
  29. #include "libavutil/opt.h"
  30. #include "os_support.h"
  31. #include "network.h"
  32. #include "url.h"
  33. typedef struct UnixContext {
  34. const AVClass *class;
  35. struct sockaddr_un addr;
  36. int timeout;
  37. int listen;
  38. int type;
  39. int fd;
  40. } UnixContext;
  41. #define OFFSET(x) offsetof(UnixContext, x)
  42. #define ED AV_OPT_FLAG_DECODING_PARAM|AV_OPT_FLAG_ENCODING_PARAM
  43. static const AVOption unix_options[] = {
  44. { "listen", "Open socket for listening", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, ED },
  45. { "timeout", "Timeout in ms", OFFSET(timeout), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, ED },
  46. { "type", "Socket type", OFFSET(type), AV_OPT_TYPE_INT, { .i64 = SOCK_STREAM }, INT_MIN, INT_MAX, ED, "type" },
  47. { "stream", "Stream (reliable stream-oriented)", 0, AV_OPT_TYPE_CONST, { .i64 = SOCK_STREAM }, INT_MIN, INT_MAX, ED, "type" },
  48. { "datagram", "Datagram (unreliable packet-oriented)", 0, AV_OPT_TYPE_CONST, { .i64 = SOCK_DGRAM }, INT_MIN, INT_MAX, ED, "type" },
  49. { "seqpacket", "Seqpacket (reliable packet-oriented", 0, AV_OPT_TYPE_CONST, { .i64 = SOCK_SEQPACKET }, INT_MIN, INT_MAX, ED, "type" },
  50. { NULL }
  51. };
  52. static const AVClass unix_class = {
  53. .class_name = "unix",
  54. .item_name = av_default_item_name,
  55. .option = unix_options,
  56. .version = LIBAVUTIL_VERSION_INT,
  57. };
  58. static int unix_open(URLContext *h, const char *filename, int flags)
  59. {
  60. UnixContext *s = h->priv_data;
  61. int fd, ret;
  62. av_strstart(filename, "unix:", &filename);
  63. s->addr.sun_family = AF_UNIX;
  64. av_strlcpy(s->addr.sun_path, filename, sizeof(s->addr.sun_path));
  65. if ((fd = ff_socket(AF_UNIX, s->type, 0)) < 0)
  66. return ff_neterrno();
  67. if (s->listen) {
  68. ret = ff_listen_bind(fd, (struct sockaddr *)&s->addr,
  69. sizeof(s->addr), s->timeout, h);
  70. if (ret < 0)
  71. goto fail;
  72. fd = ret;
  73. } else {
  74. ret = ff_listen_connect(fd, (struct sockaddr *)&s->addr,
  75. sizeof(s->addr), s->timeout, h, 0);
  76. if (ret < 0)
  77. goto fail;
  78. }
  79. s->fd = fd;
  80. return 0;
  81. fail:
  82. if (s->listen && AVUNERROR(ret) != EADDRINUSE)
  83. unlink(s->addr.sun_path);
  84. if (fd >= 0)
  85. closesocket(fd);
  86. return ret;
  87. }
  88. static int unix_read(URLContext *h, uint8_t *buf, int size)
  89. {
  90. UnixContext *s = h->priv_data;
  91. int ret;
  92. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  93. ret = ff_network_wait_fd(s->fd, 0);
  94. if (ret < 0)
  95. return ret;
  96. }
  97. ret = recv(s->fd, buf, size, 0);
  98. return ret < 0 ? ff_neterrno() : ret;
  99. }
  100. static int unix_write(URLContext *h, const uint8_t *buf, int size)
  101. {
  102. UnixContext *s = h->priv_data;
  103. int ret;
  104. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  105. ret = ff_network_wait_fd(s->fd, 1);
  106. if (ret < 0)
  107. return ret;
  108. }
  109. ret = send(s->fd, buf, size, MSG_NOSIGNAL);
  110. return ret < 0 ? ff_neterrno() : ret;
  111. }
  112. static int unix_close(URLContext *h)
  113. {
  114. UnixContext *s = h->priv_data;
  115. if (s->listen)
  116. unlink(s->addr.sun_path);
  117. closesocket(s->fd);
  118. return 0;
  119. }
  120. static int unix_get_file_handle(URLContext *h)
  121. {
  122. UnixContext *s = h->priv_data;
  123. return s->fd;
  124. }
  125. URLProtocol ff_unix_protocol = {
  126. .name = "unix",
  127. .url_open = unix_open,
  128. .url_read = unix_read,
  129. .url_write = unix_write,
  130. .url_close = unix_close,
  131. .url_get_file_handle = unix_get_file_handle,
  132. .priv_data_size = sizeof(UnixContext),
  133. .priv_data_class = &unix_class,
  134. .flags = URL_PROTOCOL_FLAG_NETWORK,
  135. };