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.

208 lines
6.3KB

  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. av_log(h, AV_LOG_ERROR, "Cannot create context: %s.\n", strerror(errno));
  49. return AVERROR(errno);
  50. }
  51. if (!smbc_init_context(libsmbc->ctx)) {
  52. av_log(h, AV_LOG_ERROR, "Cannot initialize context: %s.\n", strerror(errno));
  53. return AVERROR(errno);
  54. }
  55. smbc_set_context(libsmbc->ctx);
  56. smbc_setOptionUserData(libsmbc->ctx, h);
  57. smbc_setFunctionAuthDataWithContext(libsmbc->ctx, libsmbc_get_auth_data);
  58. if (libsmbc->timeout != -1)
  59. smbc_setTimeout(libsmbc->ctx, libsmbc->timeout);
  60. if (libsmbc->workgroup)
  61. smbc_setWorkgroup(libsmbc->ctx, libsmbc->workgroup);
  62. if (smbc_init(NULL, 0) < 0) {
  63. av_log(h, AV_LOG_ERROR, "Initialization failed: %s\n", strerror(errno));
  64. return AVERROR(errno);
  65. }
  66. return 0;
  67. }
  68. static av_cold int libsmbc_close(URLContext *h)
  69. {
  70. LIBSMBContext *libsmbc = h->priv_data;
  71. if (libsmbc->fd >= 0) {
  72. smbc_close(libsmbc->fd);
  73. libsmbc->fd = -1;
  74. }
  75. if (libsmbc->ctx) {
  76. smbc_free_context(libsmbc->ctx, 1);
  77. libsmbc->ctx = NULL;
  78. }
  79. return 0;
  80. }
  81. static av_cold int libsmbc_open(URLContext *h, const char *url, int flags)
  82. {
  83. LIBSMBContext *libsmbc = h->priv_data;
  84. int access, ret;
  85. struct stat st;
  86. libsmbc->fd = -1;
  87. libsmbc->filesize = -1;
  88. if ((ret = libsmbc_connect(h)) < 0)
  89. goto fail;
  90. if ((flags & AVIO_FLAG_WRITE) && (flags & AVIO_FLAG_READ)) {
  91. access = O_CREAT | O_RDWR;
  92. if (libsmbc->trunc)
  93. access |= O_TRUNC;
  94. } else if (flags & AVIO_FLAG_WRITE) {
  95. access = O_CREAT | O_WRONLY;
  96. if (libsmbc->trunc)
  97. access |= O_TRUNC;
  98. } else
  99. access = O_RDONLY;
  100. /* 0666 = -rw-rw-rw- = read+write for everyone, minus umask */
  101. if ((libsmbc->fd = smbc_open(url, access, 0666)) < 0) {
  102. ret = AVERROR(errno);
  103. av_log(h, AV_LOG_ERROR, "File open failed: %s\n", strerror(errno));
  104. goto fail;
  105. }
  106. if (smbc_fstat(libsmbc->fd, &st) < 0)
  107. av_log(h, AV_LOG_WARNING, "Cannot stat file: %s\n", strerror(errno));
  108. else
  109. libsmbc->filesize = st.st_size;
  110. return 0;
  111. fail:
  112. libsmbc_close(h);
  113. return ret;
  114. }
  115. static int64_t libsmbc_seek(URLContext *h, int64_t pos, int whence)
  116. {
  117. LIBSMBContext *libsmbc = h->priv_data;
  118. int64_t newpos;
  119. if (whence == AVSEEK_SIZE) {
  120. if (libsmbc->filesize == -1) {
  121. av_log(h, AV_LOG_ERROR, "Error during seeking: filesize is unknown.\n");
  122. return AVERROR(EIO);
  123. } else
  124. return libsmbc->filesize;
  125. }
  126. if ((newpos = smbc_lseek(libsmbc->fd, pos, whence)) < 0) {
  127. int err = errno;
  128. av_log(h, AV_LOG_ERROR, "Error during seeking: %s\n", strerror(err));
  129. return AVERROR(err);
  130. }
  131. return newpos;
  132. }
  133. static int libsmbc_read(URLContext *h, unsigned char *buf, int size)
  134. {
  135. LIBSMBContext *libsmbc = h->priv_data;
  136. int bytes_read;
  137. if ((bytes_read = smbc_read(libsmbc->fd, buf, size)) < 0) {
  138. av_log(h, AV_LOG_ERROR, "Read error: %s\n", strerror(errno));
  139. return AVERROR(errno);
  140. }
  141. return bytes_read;
  142. }
  143. static int libsmbc_write(URLContext *h, const unsigned char *buf, int size)
  144. {
  145. LIBSMBContext *libsmbc = h->priv_data;
  146. int bytes_written;
  147. if ((bytes_written = smbc_write(libsmbc->fd, buf, size)) < 0) {
  148. av_log(h, AV_LOG_ERROR, "Write error: %s\n", strerror(errno));
  149. return AVERROR(errno);
  150. }
  151. return bytes_written;
  152. }
  153. #define OFFSET(x) offsetof(LIBSMBContext, x)
  154. #define D AV_OPT_FLAG_DECODING_PARAM
  155. #define E AV_OPT_FLAG_ENCODING_PARAM
  156. static const AVOption options[] = {
  157. {"timeout", "set timeout in ms of socket I/O operations", OFFSET(timeout), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D|E },
  158. {"truncate", "truncate existing files on write", OFFSET(trunc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, E },
  159. {"workgroup", "set the workgroup used for making connections", OFFSET(workgroup), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D|E },
  160. {NULL}
  161. };
  162. static const AVClass libsmbclient_context_class = {
  163. .class_name = "libsmbc",
  164. .item_name = av_default_item_name,
  165. .option = options,
  166. .version = LIBAVUTIL_VERSION_INT,
  167. };
  168. URLProtocol ff_libsmbclient_protocol = {
  169. .name = "smb",
  170. .url_open = libsmbc_open,
  171. .url_read = libsmbc_read,
  172. .url_write = libsmbc_write,
  173. .url_seek = libsmbc_seek,
  174. .url_close = libsmbc_close,
  175. .priv_data_size = sizeof(LIBSMBContext),
  176. .priv_data_class = &libsmbclient_context_class,
  177. .flags = URL_PROTOCOL_FLAG_NETWORK,
  178. };