1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
3 * GIO - GLib Input, Output and Streaming Library
5 * Copyright 2010 Red Hat, Inc.
7 * This library 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.
12 * This library 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.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see
19 * <http://www.gnu.org/licenses/>.
21 * In addition, when the library is used with OpenSSL, a special
22 * exception applies. Refer to the LICENSE_EXCEPTION file for details.
26 #include "gtlsoutputstream-gnutls.h"
28 #include <glib/gi18n.h>
30 struct _GTlsOutputStreamGnutls
32 GOutputStream parent_instance;
37 static void g_tls_output_stream_gnutls_pollable_iface_init (GPollableOutputStreamInterface *iface);
39 G_DEFINE_TYPE_WITH_CODE (GTlsOutputStreamGnutls, g_tls_output_stream_gnutls, G_TYPE_OUTPUT_STREAM,
40 G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_OUTPUT_STREAM, g_tls_output_stream_gnutls_pollable_iface_init)
44 g_tls_output_stream_gnutls_dispose (GObject *object)
46 GTlsOutputStreamGnutls *stream = G_TLS_OUTPUT_STREAM_GNUTLS (object);
48 g_weak_ref_set (&stream->weak_conn, NULL);
50 G_OBJECT_CLASS (g_tls_output_stream_gnutls_parent_class)->dispose (object);
54 g_tls_output_stream_gnutls_finalize (GObject *object)
56 GTlsOutputStreamGnutls *stream = G_TLS_OUTPUT_STREAM_GNUTLS (object);
58 g_weak_ref_clear (&stream->weak_conn);
60 G_OBJECT_CLASS (g_tls_output_stream_gnutls_parent_class)->finalize (object);
64 g_tls_output_stream_gnutls_write (GOutputStream *stream,
67 GCancellable *cancellable,
70 GTlsOutputStreamGnutls *tls_stream = G_TLS_OUTPUT_STREAM_GNUTLS (stream);
71 GTlsConnectionGnutls *conn;
74 conn = g_weak_ref_get (&tls_stream->weak_conn);
77 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
78 _("Connection is closed"));
82 ret = g_tls_connection_gnutls_write (conn, buffer, count, -1 /* blocking */,
84 g_object_unref (conn);
89 g_tls_output_stream_gnutls_pollable_is_writable (GPollableOutputStream *pollable)
91 GTlsOutputStreamGnutls *tls_stream = G_TLS_OUTPUT_STREAM_GNUTLS (pollable);
92 GTlsConnectionGnutls *conn;
95 conn = g_weak_ref_get (&tls_stream->weak_conn);
99 ret = g_tls_connection_gnutls_check (conn, G_IO_OUT);
101 g_object_unref (conn);
107 g_tls_output_stream_gnutls_pollable_create_source (GPollableOutputStream *pollable,
108 GCancellable *cancellable)
110 GTlsOutputStreamGnutls *tls_stream = G_TLS_OUTPUT_STREAM_GNUTLS (pollable);
111 GTlsConnectionGnutls *conn;
114 conn = g_weak_ref_get (&tls_stream->weak_conn);
117 ret = g_idle_source_new ();
118 g_source_set_name (ret, "[glib-networking] g_tls_output_stream_gnutls_pollable_create_source dummy source");
122 ret = g_tls_connection_gnutls_create_source (conn,
125 g_object_unref (conn);
130 g_tls_output_stream_gnutls_pollable_write_nonblocking (GPollableOutputStream *pollable,
135 GTlsOutputStreamGnutls *tls_stream = G_TLS_OUTPUT_STREAM_GNUTLS (pollable);
136 GTlsConnectionGnutls *conn;
139 conn = g_weak_ref_get (&tls_stream->weak_conn);
142 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
143 _("Connection is closed"));
147 ret = g_tls_connection_gnutls_write (conn, buffer, size,
148 0 /* non-blocking */, NULL, error);
150 g_object_unref (conn);
155 g_tls_output_stream_gnutls_close (GOutputStream *stream,
156 GCancellable *cancellable,
159 GTlsOutputStreamGnutls *tls_stream = G_TLS_OUTPUT_STREAM_GNUTLS (stream);
163 conn = g_weak_ref_get (&tls_stream->weak_conn);
167 ret = g_tls_connection_gnutls_close_internal (conn, G_TLS_DIRECTION_WRITE,
171 g_object_unref (conn);
175 /* We do async close as synchronous-in-a-thread so we don't need to
176 * implement G_IO_IN/G_IO_OUT flip-flopping just for this one case
177 * (since handshakes are also done synchronously now).
180 close_thread (GTask *task,
183 GCancellable *cancellable)
185 GTlsOutputStreamGnutls *tls_stream = object;
186 GError *error = NULL;
189 conn = g_weak_ref_get (&tls_stream->weak_conn);
191 if (conn && !g_tls_connection_gnutls_close_internal (conn,
192 G_TLS_DIRECTION_WRITE,
194 cancellable, &error))
195 g_task_return_error (task, error);
197 g_task_return_boolean (task, TRUE);
200 g_object_unref (conn);
205 g_tls_output_stream_gnutls_close_async (GOutputStream *stream,
207 GCancellable *cancellable,
208 GAsyncReadyCallback callback,
213 task = g_task_new (stream, cancellable, callback, user_data);
214 g_task_set_source_tag (task, g_tls_output_stream_gnutls_close_async);
215 g_task_set_priority (task, io_priority);
216 g_task_run_in_thread (task, close_thread);
217 g_object_unref (task);
221 g_tls_output_stream_gnutls_close_finish (GOutputStream *stream,
222 GAsyncResult *result,
225 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
226 g_return_val_if_fail (g_task_get_source_tag (G_TASK (result)) ==
227 g_tls_output_stream_gnutls_close_async, FALSE);
229 return g_task_propagate_boolean (G_TASK (result), error);
233 g_tls_output_stream_gnutls_class_init (GTlsOutputStreamGnutlsClass *klass)
235 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
236 GOutputStreamClass *output_stream_class = G_OUTPUT_STREAM_CLASS (klass);
238 gobject_class->dispose = g_tls_output_stream_gnutls_dispose;
239 gobject_class->finalize = g_tls_output_stream_gnutls_finalize;
241 output_stream_class->write_fn = g_tls_output_stream_gnutls_write;
242 output_stream_class->close_fn = g_tls_output_stream_gnutls_close;
243 output_stream_class->close_async = g_tls_output_stream_gnutls_close_async;
244 output_stream_class->close_finish = g_tls_output_stream_gnutls_close_finish;
248 g_tls_output_stream_gnutls_pollable_iface_init (GPollableOutputStreamInterface *iface)
250 iface->is_writable = g_tls_output_stream_gnutls_pollable_is_writable;
251 iface->create_source = g_tls_output_stream_gnutls_pollable_create_source;
252 iface->write_nonblocking = g_tls_output_stream_gnutls_pollable_write_nonblocking;
256 g_tls_output_stream_gnutls_init (GTlsOutputStreamGnutls *stream)
261 g_tls_output_stream_gnutls_new (GTlsConnectionGnutls *conn)
263 GTlsOutputStreamGnutls *tls_stream;
265 tls_stream = g_object_new (G_TYPE_TLS_OUTPUT_STREAM_GNUTLS, NULL);
266 g_weak_ref_init (&tls_stream->weak_conn, conn);
268 return G_OUTPUT_STREAM (tls_stream);