• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  *
25  * This is a network sink that uses libcurl as a client to upload data to
26  * a SFTP (SSH File Transfer Protocol) server.
27  *
28  * ## Example launch line
29  *
30  * Upload a file to /home/john/sftp_tests/
31  *
32  * |[
33  * gst-launch-1.0 filesrc location=/home/jdoe/some.file ! curlsftpsink  \
34  *     file-name=some.file.backup  \
35  *     user=john location=sftp://192.168.0.1/~/sftp_tests/  \
36  *     ssh-auth-type=1 ssh-key-passphrase=blabla  \
37  *     ssh-pub-keyfile=/home/jdoe/.ssh/id_rsa.pub  \
38  *     ssh-priv-keyfile=/home/jdoe/.ssh/id_rsa  \
39  *     create-dirs=TRUE
40  * ]|
41  */
42 
43 #ifdef HAVE_CONFIG_H
44 #include "config.h"
45 #endif
46 
47 #include "gstcurlelements.h"
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 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (curlsftpsink, "curlsftpsink",
97     GST_RANK_NONE, GST_TYPE_CURL_SFTP_SINK, curl_element_init (plugin));
98 
99 static void
gst_curl_sftp_sink_class_init(GstCurlSftpSinkClass * klass)100 gst_curl_sftp_sink_class_init (GstCurlSftpSinkClass * klass)
101 {
102   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
103   GstCurlBaseSinkClass *gstcurlbasesink_class = (GstCurlBaseSinkClass *) klass;
104   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
105 
106   GST_DEBUG_CATEGORY_INIT (gst_curl_sftp_sink_debug, "curlsftpsink", 0,
107       "curl sftp sink element");
108 
109   GST_DEBUG ("class_init");
110 
111   gst_element_class_set_static_metadata (element_class,
112       "Curl sftp sink",
113       "Sink/Network",
114       "Upload data over the SFTP protocol using libcurl",
115       "Sorin L. <sorin@axis.com>");
116 
117   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_curl_sftp_sink_finalize);
118 
119   gobject_class->set_property = gst_curl_sftp_sink_set_property;
120   gobject_class->get_property = gst_curl_sftp_sink_get_property;
121 
122   gstcurlbasesink_class->set_protocol_dynamic_options_unlocked =
123       set_sftp_dynamic_options_unlocked;
124   gstcurlbasesink_class->set_options_unlocked = set_sftp_options_unlocked;
125 
126   g_object_class_install_property (gobject_class, PROP_CREATE_DIRS,
127       g_param_spec_boolean ("create-dirs", "Create missing directories",
128           "Attempt to create missing directories",
129           FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
130 }
131 
132 static void
gst_curl_sftp_sink_init(GstCurlSftpSink * sink)133 gst_curl_sftp_sink_init (GstCurlSftpSink * sink)
134 {
135 }
136 
137 static void
gst_curl_sftp_sink_finalize(GObject * gobject)138 gst_curl_sftp_sink_finalize (GObject * gobject)
139 {
140   GST_DEBUG ("finalizing curlsftpsink");
141   G_OBJECT_CLASS (parent_class)->finalize (gobject);
142 }
143 
144 static gboolean
set_sftp_dynamic_options_unlocked(GstCurlBaseSink * basesink)145 set_sftp_dynamic_options_unlocked (GstCurlBaseSink * basesink)
146 {
147   gchar *tmp = g_strdup_printf ("%s%s", basesink->url, basesink->file_name);
148   CURLcode curl_err = CURLE_OK;
149 
150   curl_err = curl_easy_setopt (basesink->curl, CURLOPT_URL, tmp);
151   g_free (tmp);
152   if (curl_err != CURLE_OK) {
153     basesink->error = g_strdup_printf ("failed to set URL: %s",
154         curl_easy_strerror (curl_err));
155     return FALSE;
156   }
157 
158   return TRUE;
159 }
160 
161 static gboolean
set_sftp_options_unlocked(GstCurlBaseSink * basesink)162 set_sftp_options_unlocked (GstCurlBaseSink * basesink)
163 {
164   GstCurlSftpSink *sink = GST_CURL_SFTP_SINK (basesink);
165   GstCurlSshSinkClass *parent_class;
166   CURLcode curl_err = CURLE_OK;
167 
168   if ((curl_err =
169           curl_easy_setopt (basesink->curl, CURLOPT_UPLOAD, 1L)) != CURLE_OK) {
170     basesink->error = g_strdup_printf ("failed to prepare for upload: %s",
171         curl_easy_strerror (curl_err));
172     return FALSE;
173   }
174 
175   if (sink->create_dirs) {
176     if ((curl_err = curl_easy_setopt (basesink->curl,
177                 CURLOPT_FTP_CREATE_MISSING_DIRS, 1L)) != CURLE_OK) {
178       basesink->error =
179           g_strdup_printf ("failed to set create missing dirs: %s",
180           curl_easy_strerror (curl_err));
181       return FALSE;
182     }
183   }
184 
185   parent_class = GST_CURL_SSH_SINK_GET_CLASS (sink);
186 
187   /* chain to parent as well */
188   return parent_class->set_options_unlocked (basesink);
189 }
190 
191 static void
gst_curl_sftp_sink_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)192 gst_curl_sftp_sink_set_property (GObject * object, guint prop_id,
193     const GValue * value, GParamSpec * pspec)
194 {
195   GstCurlSftpSink *sink;
196   GstState cur_state;
197 
198   g_return_if_fail (GST_IS_CURL_SFTP_SINK (object));
199   sink = GST_CURL_SFTP_SINK (object);
200 
201   gst_element_get_state (GST_ELEMENT (sink), &cur_state, NULL, 0);
202   if (cur_state != GST_STATE_PLAYING && cur_state != GST_STATE_PAUSED) {
203     GST_OBJECT_LOCK (sink);
204 
205     switch (prop_id) {
206       case PROP_CREATE_DIRS:
207         sink->create_dirs = g_value_get_boolean (value);
208         GST_DEBUG_OBJECT (sink, "create-dirs set to %d", sink->create_dirs);
209         break;
210 
211       default:
212         GST_DEBUG_OBJECT (sink, "invalid property id %d", prop_id);
213         break;
214     }
215 
216     GST_OBJECT_UNLOCK (sink);
217   }
218 }
219 
220 static void
gst_curl_sftp_sink_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)221 gst_curl_sftp_sink_get_property (GObject * object, guint prop_id,
222     GValue * value, GParamSpec * pspec)
223 {
224   GstCurlSftpSink *sink;
225 
226   g_return_if_fail (GST_IS_CURL_SFTP_SINK (object));
227   sink = GST_CURL_SFTP_SINK (object);
228 
229   switch (prop_id) {
230     case PROP_CREATE_DIRS:
231       g_value_set_boolean (value, sink->create_dirs);
232       break;
233 
234     default:
235       GST_DEBUG_OBJECT (sink, "invalid property id");
236       break;
237   }
238 }
239