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.

154 lines
4.6KB

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