062b8ef9cf1488913a76b8c6245268e1bfb2dc58
[platform/upstream/glib-networking.git] / tls / gnutls / gtlsoutputstream-gnutls.c
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3  * GIO - GLib Input, Output and Streaming Library
4  *
5  * Copyright 2010 Red Hat, Inc.
6  *
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.
11  *
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.
16  *
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/>.
20  *
21  * In addition, when the library is used with OpenSSL, a special
22  * exception applies. Refer to the LICENSE_EXCEPTION file for details.
23  */
24
25 #include "config.h"
26 #include "gtlsoutputstream-gnutls.h"
27
28 #include <glib/gi18n.h>
29
30 struct _GTlsOutputStreamGnutls
31 {
32   GOutputStream parent_instance;
33
34   GWeakRef weak_conn;
35 };
36
37 static void g_tls_output_stream_gnutls_pollable_iface_init (GPollableOutputStreamInterface *iface);
38
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)
41                          )
42
43 static void
44 g_tls_output_stream_gnutls_dispose (GObject *object)
45 {
46   GTlsOutputStreamGnutls *stream = G_TLS_OUTPUT_STREAM_GNUTLS (object);
47
48   g_weak_ref_set (&stream->weak_conn, NULL);
49
50   G_OBJECT_CLASS (g_tls_output_stream_gnutls_parent_class)->dispose (object);
51 }
52
53 static void
54 g_tls_output_stream_gnutls_finalize (GObject *object)
55 {
56   GTlsOutputStreamGnutls *stream = G_TLS_OUTPUT_STREAM_GNUTLS (object);
57
58   g_weak_ref_clear (&stream->weak_conn);
59
60   G_OBJECT_CLASS (g_tls_output_stream_gnutls_parent_class)->finalize (object);
61 }
62
63 static gssize
64 g_tls_output_stream_gnutls_write (GOutputStream  *stream,
65                                   const void     *buffer,
66                                   gsize           count,
67                                   GCancellable   *cancellable,
68                                   GError        **error)
69 {
70   GTlsOutputStreamGnutls *tls_stream = G_TLS_OUTPUT_STREAM_GNUTLS (stream);
71   GTlsConnectionGnutls *conn;
72   gssize ret;
73
74   conn = g_weak_ref_get (&tls_stream->weak_conn);
75   if (conn == NULL)
76     {
77       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
78                            _("Connection is closed"));
79       return -1;
80     }
81
82   ret = g_tls_connection_gnutls_write (conn, buffer, count, -1  /* blocking */,
83                                        cancellable, error);
84   g_object_unref (conn);
85   return ret;
86 }
87
88 static gboolean
89 g_tls_output_stream_gnutls_pollable_is_writable (GPollableOutputStream *pollable)
90 {
91   GTlsOutputStreamGnutls *tls_stream = G_TLS_OUTPUT_STREAM_GNUTLS (pollable);
92   GTlsConnectionGnutls *conn;
93   gboolean ret;
94
95   conn = g_weak_ref_get (&tls_stream->weak_conn);
96   if (conn == NULL)
97     return FALSE;
98
99   ret = g_tls_connection_gnutls_check (conn, G_IO_OUT);
100
101   g_object_unref (conn);
102
103   return ret;
104 }
105
106 static GSource *
107 g_tls_output_stream_gnutls_pollable_create_source (GPollableOutputStream *pollable,
108                                                    GCancellable         *cancellable)
109 {
110   GTlsOutputStreamGnutls *tls_stream = G_TLS_OUTPUT_STREAM_GNUTLS (pollable);
111   GTlsConnectionGnutls *conn;
112   GSource *ret;
113
114   conn = g_weak_ref_get (&tls_stream->weak_conn);
115   if (conn == NULL)
116     {
117       ret = g_idle_source_new ();
118       g_source_set_name (ret, "[glib-networking] g_tls_output_stream_gnutls_pollable_create_source dummy source");
119       return ret;
120     }
121
122   ret = g_tls_connection_gnutls_create_source (conn,
123                                                G_IO_OUT,
124                                                cancellable);
125   g_object_unref (conn);
126   return ret;
127 }
128
129 static gssize
130 g_tls_output_stream_gnutls_pollable_write_nonblocking (GPollableOutputStream  *pollable,
131                                                        const void             *buffer,
132                                                        gsize                   size,
133                                                        GError                **error)
134 {
135   GTlsOutputStreamGnutls *tls_stream = G_TLS_OUTPUT_STREAM_GNUTLS (pollable);
136   GTlsConnectionGnutls *conn;
137   gssize ret;
138
139   conn = g_weak_ref_get (&tls_stream->weak_conn);
140   if (conn == NULL)
141     {
142       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
143                            _("Connection is closed"));
144       return -1;
145     }
146
147   ret = g_tls_connection_gnutls_write (conn, buffer, size,
148                                        0  /* non-blocking */, NULL, error);
149
150   g_object_unref (conn);
151   return ret;
152 }
153
154 static gboolean
155 g_tls_output_stream_gnutls_close (GOutputStream            *stream,
156                                   GCancellable             *cancellable,
157                                   GError                  **error)
158 {
159   GTlsOutputStreamGnutls *tls_stream = G_TLS_OUTPUT_STREAM_GNUTLS (stream);
160   GIOStream *conn;
161   gboolean ret;
162
163   conn = g_weak_ref_get (&tls_stream->weak_conn);
164   if (conn == NULL)
165     return TRUE;
166
167   ret = g_tls_connection_gnutls_close_internal (conn, G_TLS_DIRECTION_WRITE,
168                                                 -1,  /* blocking */
169                                                 cancellable, error);
170
171   g_object_unref (conn);
172   return ret;
173 }
174
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).
178  */
179 static void
180 close_thread (GTask        *task,
181               gpointer      object,
182               gpointer      task_data,
183               GCancellable *cancellable)
184 {
185   GTlsOutputStreamGnutls *tls_stream = object;
186   GError *error = NULL;
187   GIOStream *conn;
188
189   conn = g_weak_ref_get (&tls_stream->weak_conn);
190
191   if (conn && !g_tls_connection_gnutls_close_internal (conn,
192                                                        G_TLS_DIRECTION_WRITE,
193                                                        -1,  /* blocking */
194                                                        cancellable, &error))
195     g_task_return_error (task, error);
196   else
197     g_task_return_boolean (task, TRUE);
198
199   if (conn)
200     g_object_unref (conn);
201 }
202
203
204 static void
205 g_tls_output_stream_gnutls_close_async (GOutputStream            *stream,
206                                         int                       io_priority,
207                                         GCancellable             *cancellable,
208                                         GAsyncReadyCallback       callback,
209                                         gpointer                  user_data)
210 {
211   GTask *task;
212
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);
218 }
219
220 static gboolean
221 g_tls_output_stream_gnutls_close_finish (GOutputStream            *stream,
222                                          GAsyncResult             *result,
223                                          GError                  **error)
224 {
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);
228
229   return g_task_propagate_boolean (G_TASK (result), error);
230 }
231
232 static void
233 g_tls_output_stream_gnutls_class_init (GTlsOutputStreamGnutlsClass *klass)
234 {
235   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
236   GOutputStreamClass *output_stream_class = G_OUTPUT_STREAM_CLASS (klass);
237
238   gobject_class->dispose = g_tls_output_stream_gnutls_dispose;
239   gobject_class->finalize = g_tls_output_stream_gnutls_finalize;
240
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;
245 }
246
247 static void
248 g_tls_output_stream_gnutls_pollable_iface_init (GPollableOutputStreamInterface *iface)
249 {
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;
253 }
254
255 static void
256 g_tls_output_stream_gnutls_init (GTlsOutputStreamGnutls *stream)
257 {
258 }
259
260 GOutputStream *
261 g_tls_output_stream_gnutls_new (GTlsConnectionGnutls *conn)
262 {
263   GTlsOutputStreamGnutls *tls_stream;
264
265   tls_stream = g_object_new (G_TYPE_TLS_OUTPUT_STREAM_GNUTLS, NULL);
266   g_weak_ref_init (&tls_stream->weak_conn, conn);
267
268   return G_OUTPUT_STREAM (tls_stream);
269 }