1 /* GStreamer
2 * Copyright (C) 2011 Axis Communications <dev-gstreamer@axis.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 /**
21 * SECTION:element-curlsftpsink
22 * @title: curlsftpsink
23 * @short_description: sink that uploads data to a server using libcurl
24 * @see_also:
25 *
26 * This is a network sink that uses libcurl as a client to upload data to
27 * a SFTP (SSH File Transfer Protocol) server.
28 *
29 * ## Example launch line
30 *
31 * Upload a file to /home/john/sftp_tests/
32 *
33 * |[
34 * gst-launch-1.0 filesrc location=/home/jdoe/some.file ! curlsftpsink \
35 * file-name=some.file.backup \
36 * user=john location=sftp://192.168.0.1/~/sftp_tests/ \
37 * ssh-auth-type=1 ssh-key-passphrase=blabla \
38 * ssh-pub-keyfile=/home/jdoe/.ssh/id_rsa.pub \
39 * ssh-priv-keyfile=/home/jdoe/.ssh/id_rsa \
40 * create-dirs=TRUE
41 * ]|
42 */
43
44 #ifdef HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47
48 #include "gstcurlsshsink.h"
49 #include "gstcurlsftpsink.h"
50
51 #include <curl/curl.h>
52 #include <string.h>
53 #include <stdio.h>
54
55 #ifdef G_OS_WIN32
56 #include <winsock2.h>
57 #else
58 #include <sys/socket.h>
59 #include <netinet/in.h>
60 #include <netinet/ip.h>
61 #include <netinet/tcp.h>
62 #endif
63 #include <sys/types.h>
64 #include <unistd.h>
65 #include <sys/stat.h>
66 #include <fcntl.h>
67
68 /* Default values */
69 #define GST_CAT_DEFAULT gst_curl_sftp_sink_debug
70
71
72 /* Plugin specific settings */
73
74 GST_DEBUG_CATEGORY_STATIC (gst_curl_sftp_sink_debug);
75
76 enum
77 {
78 PROP_0,
79 PROP_CREATE_DIRS
80 };
81
82 /* private functions */
83 static void gst_curl_sftp_sink_set_property (GObject * object, guint prop_id,
84 const GValue * value, GParamSpec * pspec);
85 static void gst_curl_sftp_sink_get_property (GObject * object, guint prop_id,
86 GValue * value, GParamSpec * pspec);
87 static void gst_curl_sftp_sink_finalize (GObject * gobject);
88
89 static gboolean set_sftp_options_unlocked (GstCurlBaseSink * curlbasesink);
90 static gboolean set_sftp_dynamic_options_unlocked (GstCurlBaseSink *
91 curlbasesink);
92
93
94 #define gst_curl_sftp_sink_parent_class parent_class
95 G_DEFINE_TYPE (GstCurlSftpSink, gst_curl_sftp_sink, GST_TYPE_CURL_SSH_SINK);
96
97 static void
gst_curl_sftp_sink_class_init(GstCurlSftpSinkClass * klass)98 gst_curl_sftp_sink_class_init (GstCurlSftpSinkClass * klass)
99 {
100 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
101 GstCurlBaseSinkClass *gstcurlbasesink_class = (GstCurlBaseSinkClass *) klass;
102 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
103
104 GST_DEBUG_CATEGORY_INIT (gst_curl_sftp_sink_debug, "curlsftpsink", 0,
105 "curl sftp sink element");
106
107 GST_DEBUG_OBJECT (klass, "class_init");
108
109 gst_element_class_set_static_metadata (element_class,
110 "Curl sftp sink",
111 "Sink/Network",
112 "Upload data over the SFTP protocol using libcurl",
113 "Sorin L. <sorin@axis.com>");
114
115 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_curl_sftp_sink_finalize);
116
117 gobject_class->set_property = gst_curl_sftp_sink_set_property;
118 gobject_class->get_property = gst_curl_sftp_sink_get_property;
119
120 gstcurlbasesink_class->set_protocol_dynamic_options_unlocked =
121 set_sftp_dynamic_options_unlocked;
122 gstcurlbasesink_class->set_options_unlocked = set_sftp_options_unlocked;
123
124 g_object_class_install_property (gobject_class, PROP_CREATE_DIRS,
125 g_param_spec_boolean ("create-dirs", "Create missing directories",
126 "Attempt to create missing directories",
127 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
128 }
129
130 static void
gst_curl_sftp_sink_init(GstCurlSftpSink * sink)131 gst_curl_sftp_sink_init (GstCurlSftpSink * sink)
132 {
133 }
134
135 static void
gst_curl_sftp_sink_finalize(GObject * gobject)136 gst_curl_sftp_sink_finalize (GObject * gobject)
137 {
138 GST_DEBUG ("finalizing curlsftpsink");
139 G_OBJECT_CLASS (parent_class)->finalize (gobject);
140 }
141
142 static gboolean
set_sftp_dynamic_options_unlocked(GstCurlBaseSink * basesink)143 set_sftp_dynamic_options_unlocked (GstCurlBaseSink * basesink)
144 {
145 gchar *tmp = g_strdup_printf ("%s%s", basesink->url, basesink->file_name);
146 CURLcode curl_err = CURLE_OK;
147
148 curl_err = curl_easy_setopt (basesink->curl, CURLOPT_URL, tmp);
149 g_free (tmp);
150 if (curl_err != CURLE_OK) {
151 basesink->error = g_strdup_printf ("failed to set URL: %s",
152 curl_easy_strerror (curl_err));
153 return FALSE;
154 }
155
156 return TRUE;
157 }
158
159 static gboolean
set_sftp_options_unlocked(GstCurlBaseSink * basesink)160 set_sftp_options_unlocked (GstCurlBaseSink * basesink)
161 {
162 GstCurlSftpSink *sink = GST_CURL_SFTP_SINK (basesink);
163 GstCurlSshSinkClass *parent_class;
164 CURLcode curl_err = CURLE_OK;
165
166 if ((curl_err =
167 curl_easy_setopt (basesink->curl, CURLOPT_UPLOAD, 1L)) != CURLE_OK) {
168 basesink->error = g_strdup_printf ("failed to prepare for upload: %s",
169 curl_easy_strerror (curl_err));
170 return FALSE;
171 }
172
173 if (sink->create_dirs) {
174 if ((curl_err = curl_easy_setopt (basesink->curl,
175 CURLOPT_FTP_CREATE_MISSING_DIRS, 1L)) != CURLE_OK) {
176 basesink->error =
177 g_strdup_printf ("failed to set create missing dirs: %s",
178 curl_easy_strerror (curl_err));
179 return FALSE;
180 }
181 }
182
183 parent_class = GST_CURL_SSH_SINK_GET_CLASS (sink);
184
185 /* chain to parent as well */
186 return parent_class->set_options_unlocked (basesink);
187 }
188
189 static void
gst_curl_sftp_sink_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)190 gst_curl_sftp_sink_set_property (GObject * object, guint prop_id,
191 const GValue * value, GParamSpec * pspec)
192 {
193 GstCurlSftpSink *sink;
194 GstState cur_state;
195
196 g_return_if_fail (GST_IS_CURL_SFTP_SINK (object));
197 sink = GST_CURL_SFTP_SINK (object);
198
199 gst_element_get_state (GST_ELEMENT (sink), &cur_state, NULL, 0);
200 if (cur_state != GST_STATE_PLAYING && cur_state != GST_STATE_PAUSED) {
201 GST_OBJECT_LOCK (sink);
202
203 switch (prop_id) {
204 case PROP_CREATE_DIRS:
205 sink->create_dirs = g_value_get_boolean (value);
206 GST_DEBUG_OBJECT (sink, "create-dirs set to %d", sink->create_dirs);
207 break;
208
209 default:
210 GST_DEBUG_OBJECT (sink, "invalid property id %d", prop_id);
211 break;
212 }
213
214 GST_OBJECT_UNLOCK (sink);
215 }
216 }
217
218 static void
gst_curl_sftp_sink_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)219 gst_curl_sftp_sink_get_property (GObject * object, guint prop_id,
220 GValue * value, GParamSpec * pspec)
221 {
222 GstCurlSftpSink *sink;
223
224 g_return_if_fail (GST_IS_CURL_SFTP_SINK (object));
225 sink = GST_CURL_SFTP_SINK (object);
226
227 switch (prop_id) {
228 case PROP_CREATE_DIRS:
229 g_value_set_boolean (value, sink->create_dirs);
230 break;
231
232 default:
233 GST_DEBUG_OBJECT (sink, "invalid property id");
234 break;
235 }
236 }
237