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.

213 lines
6.4KB

  1. /*
  2. * Copyright (c) 2014 Lukasz Marek <lukasz.m.luki@gmail.com>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <libsmbclient.h>
  21. #include "libavutil/avstring.h"
  22. #include "libavutil/opt.h"
  23. #include "avformat.h"
  24. #include "internal.h"
  25. #include "url.h"
  26. typedef struct {
  27. const AVClass *class;
  28. SMBCCTX *ctx;
  29. int fd;
  30. int64_t filesize;
  31. int trunc;
  32. int timeout;
  33. char *workgroup;
  34. } LIBSMBContext;
  35. static void libsmbc_get_auth_data(SMBCCTX *c, const char *server, const char *share,
  36. char *workgroup, int workgroup_len,
  37. char *username, int username_len,
  38. char *password, int password_len)
  39. {
  40. /* Do nothing yet. Credentials are passed via url.
  41. * Callback must exists, there might be a segmentation fault otherwise. */
  42. }
  43. static av_cold int libsmbc_connect(URLContext *h)
  44. {
  45. LIBSMBContext *libsmbc = h->priv_data;
  46. libsmbc->ctx = smbc_new_context();
  47. if (!libsmbc->ctx) {
  48. int ret = AVERROR(errno);
  49. av_log(h, AV_LOG_ERROR, "Cannot create context: %s.\n", strerror(errno));
  50. return ret;
  51. }
  52. if (!smbc_init_context(libsmbc->ctx)) {
  53. int ret = AVERROR(errno);
  54. av_log(h, AV_LOG_ERROR, "Cannot initialize context: %s.\n", strerror(errno));
  55. return ret;
  56. }
  57. smbc_set_context(libsmbc->ctx);
  58. smbc_setOptionUserData(libsmbc->ctx, h);
  59. smbc_setFunctionAuthDataWithContext(libsmbc->ctx, libsmbc_get_auth_data);
  60. if (libsmbc->timeout != -1)
  61. smbc_setTimeout(libsmbc->ctx, libsmbc->timeout);
  62. if (libsmbc->workgroup)
  63. smbc_setWorkgroup(libsmbc->ctx, libsmbc->workgroup);
  64. if (smbc_init(NULL, 0) < 0) {
  65. int ret = AVERROR(errno);
  66. av_log(h, AV_LOG_ERROR, "Initialization failed: %s\n", strerror(errno));
  67. return ret;
  68. }
  69. return 0;
  70. }
  71. static av_cold int libsmbc_close(URLContext *h)
  72. {
  73. LIBSMBContext *libsmbc = h->priv_data;
  74. if (libsmbc->fd >= 0) {
  75. smbc_close(libsmbc->fd);
  76. libsmbc->fd = -1;
  77. }
  78. if (libsmbc->ctx) {
  79. smbc_free_context(libsmbc->ctx, 1);
  80. libsmbc->ctx = NULL;
  81. }
  82. return 0;
  83. }
  84. static av_cold int libsmbc_open(URLContext *h, const char *url, int flags)
  85. {
  86. LIBSMBContext *libsmbc = h->priv_data;
  87. int access, ret;
  88. struct stat st;
  89. libsmbc->fd = -1;
  90. libsmbc->filesize = -1;
  91. if ((ret = libsmbc_connect(h)) < 0)
  92. goto fail;
  93. if ((flags & AVIO_FLAG_WRITE) && (flags & AVIO_FLAG_READ)) {
  94. access = O_CREAT | O_RDWR;
  95. if (libsmbc->trunc)
  96. access |= O_TRUNC;
  97. } else if (flags & AVIO_FLAG_WRITE) {
  98. access = O_CREAT | O_WRONLY;
  99. if (libsmbc->trunc)
  100. access |= O_TRUNC;
  101. } else
  102. access = O_RDONLY;
  103. /* 0666 = -rw-rw-rw- = read+write for everyone, minus umask */
  104. if ((libsmbc->fd = smbc_open(url, access, 0666)) < 0) {
  105. ret = AVERROR(errno);
  106. av_log(h, AV_LOG_ERROR, "File open failed: %s\n", strerror(errno));
  107. goto fail;
  108. }
  109. if (smbc_fstat(libsmbc->fd, &st) < 0)
  110. av_log(h, AV_LOG_WARNING, "Cannot stat file: %s\n", strerror(errno));
  111. else
  112. libsmbc->filesize = st.st_size;
  113. return 0;
  114. fail:
  115. libsmbc_close(h);
  116. return ret;
  117. }
  118. static int64_t libsmbc_seek(URLContext *h, int64_t pos, int whence)
  119. {
  120. LIBSMBContext *libsmbc = h->priv_data;
  121. int64_t newpos;
  122. if (whence == AVSEEK_SIZE) {
  123. if (libsmbc->filesize == -1) {
  124. av_log(h, AV_LOG_ERROR, "Error during seeking: filesize is unknown.\n");
  125. return AVERROR(EIO);
  126. } else
  127. return libsmbc->filesize;
  128. }
  129. if ((newpos = smbc_lseek(libsmbc->fd, pos, whence)) < 0) {
  130. int err = errno;
  131. av_log(h, AV_LOG_ERROR, "Error during seeking: %s\n", strerror(err));
  132. return AVERROR(err);
  133. }
  134. return newpos;
  135. }
  136. static int libsmbc_read(URLContext *h, unsigned char *buf, int size)
  137. {
  138. LIBSMBContext *libsmbc = h->priv_data;
  139. int bytes_read;
  140. if ((bytes_read = smbc_read(libsmbc->fd, buf, size)) < 0) {
  141. int ret = AVERROR(errno);
  142. av_log(h, AV_LOG_ERROR, "Read error: %s\n", strerror(errno));
  143. return ret;
  144. }
  145. return bytes_read;
  146. }
  147. static int libsmbc_write(URLContext *h, const unsigned char *buf, int size)
  148. {
  149. LIBSMBContext *libsmbc = h->priv_data;
  150. int bytes_written;
  151. if ((bytes_written = smbc_write(libsmbc->fd, buf, size)) < 0) {
  152. int ret = AVERROR(errno);
  153. av_log(h, AV_LOG_ERROR, "Write error: %s\n", strerror(errno));
  154. return ret;
  155. }
  156. return bytes_written;
  157. }
  158. #define OFFSET(x) offsetof(LIBSMBContext, x)
  159. #define D AV_OPT_FLAG_DECODING_PARAM
  160. #define E AV_OPT_FLAG_ENCODING_PARAM
  161. static const AVOption options[] = {
  162. {"timeout", "set timeout in ms of socket I/O operations", OFFSET(timeout), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D|E },
  163. {"truncate", "truncate existing files on write", OFFSET(trunc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, E },
  164. {"workgroup", "set the workgroup used for making connections", OFFSET(workgroup), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D|E },
  165. {NULL}
  166. };
  167. static const AVClass libsmbclient_context_class = {
  168. .class_name = "libsmbc",
  169. .item_name = av_default_item_name,
  170. .option = options,
  171. .version = LIBAVUTIL_VERSION_INT,
  172. };
  173. URLProtocol ff_libsmbclient_protocol = {
  174. .name = "smb",
  175. .url_open = libsmbc_open,
  176. .url_read = libsmbc_read,
  177. .url_write = libsmbc_write,
  178. .url_seek = libsmbc_seek,
  179. .url_close = libsmbc_close,
  180. .priv_data_size = sizeof(LIBSMBContext),
  181. .priv_data_class = &libsmbclient_context_class,
  182. .flags = URL_PROTOCOL_FLAG_NETWORK,
  183. };