Check for overflow when allocating RcBox
[glib.git] / gio / gtcpwrapperconnection.c
blob9c00869daa675d7072c179f4161fe8a5bb1e4dde
1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2010 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Authors: Nicolas Dufresne <nicolas.dufresne@colllabora.co.uk>
21 /**
22 * SECTION:gtcpwrapperconnection
23 * @title: GTcpWrapperConnection
24 * @short_description: Wrapper for non-GSocketConnection-based,
25 * GSocket-based GIOStreams
26 * @include: gio/gio.h
27 * @see_also: #GSocketConnection.
29 * A #GTcpWrapperConnection can be used to wrap a #GIOStream that is
30 * based on a #GSocket, but which is not actually a
31 * #GSocketConnection. This is used by #GSocketClient so that it can
32 * always return a #GSocketConnection, even when the connection it has
33 * actually created is not directly a #GSocketConnection.
35 * Since: 2.28
38 /**
39 * GTcpWrapperConnection:
41 * #GTcpWrapperConnection is an opaque data structure and can only be accessed
42 * using the following functions.
43 **/
45 #include "config.h"
47 #include "gtcpwrapperconnection.h"
49 #include "gtcpconnection.h"
50 #include "glibintl.h"
52 struct _GTcpWrapperConnectionPrivate
54 GIOStream *base_io_stream;
57 G_DEFINE_TYPE_WITH_PRIVATE (GTcpWrapperConnection, g_tcp_wrapper_connection, G_TYPE_TCP_CONNECTION)
59 enum
61 PROP_NONE,
62 PROP_BASE_IO_STREAM
65 static GInputStream *
66 g_tcp_wrapper_connection_get_input_stream (GIOStream *io_stream)
68 GTcpWrapperConnection *connection = G_TCP_WRAPPER_CONNECTION (io_stream);
70 return g_io_stream_get_input_stream (connection->priv->base_io_stream);
73 static GOutputStream *
74 g_tcp_wrapper_connection_get_output_stream (GIOStream *io_stream)
76 GTcpWrapperConnection *connection = G_TCP_WRAPPER_CONNECTION (io_stream);
78 return g_io_stream_get_output_stream (connection->priv->base_io_stream);
81 static void
82 g_tcp_wrapper_connection_get_property (GObject *object,
83 guint prop_id,
84 GValue *value,
85 GParamSpec *pspec)
87 GTcpWrapperConnection *connection = G_TCP_WRAPPER_CONNECTION (object);
89 switch (prop_id)
91 case PROP_BASE_IO_STREAM:
92 g_value_set_object (value, connection->priv->base_io_stream);
93 break;
95 default:
96 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
100 static void
101 g_tcp_wrapper_connection_set_property (GObject *object,
102 guint prop_id,
103 const GValue *value,
104 GParamSpec *pspec)
106 GTcpWrapperConnection *connection = G_TCP_WRAPPER_CONNECTION (object);
108 switch (prop_id)
110 case PROP_BASE_IO_STREAM:
111 connection->priv->base_io_stream = g_value_dup_object (value);
112 break;
114 default:
115 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
119 static void
120 g_tcp_wrapper_connection_finalize (GObject *object)
122 GTcpWrapperConnection *connection = G_TCP_WRAPPER_CONNECTION (object);
124 if (connection->priv->base_io_stream)
125 g_object_unref (connection->priv->base_io_stream);
127 G_OBJECT_CLASS (g_tcp_wrapper_connection_parent_class)->finalize (object);
130 static void
131 g_tcp_wrapper_connection_class_init (GTcpWrapperConnectionClass *klass)
133 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
134 GIOStreamClass *stream_class = G_IO_STREAM_CLASS (klass);
136 gobject_class->set_property = g_tcp_wrapper_connection_set_property;
137 gobject_class->get_property = g_tcp_wrapper_connection_get_property;
138 gobject_class->finalize = g_tcp_wrapper_connection_finalize;
140 stream_class->get_input_stream = g_tcp_wrapper_connection_get_input_stream;
141 stream_class->get_output_stream = g_tcp_wrapper_connection_get_output_stream;
143 g_object_class_install_property (gobject_class,
144 PROP_BASE_IO_STREAM,
145 g_param_spec_object ("base-io-stream",
146 P_("Base IO Stream"),
147 P_("The wrapped GIOStream"),
148 G_TYPE_IO_STREAM,
149 G_PARAM_CONSTRUCT_ONLY |
150 G_PARAM_READWRITE |
151 G_PARAM_STATIC_STRINGS));
154 static void
155 g_tcp_wrapper_connection_init (GTcpWrapperConnection *connection)
157 connection->priv = g_tcp_wrapper_connection_get_instance_private (connection);
161 * g_tcp_wrapper_connection_new:
162 * @base_io_stream: the #GIOStream to wrap
163 * @socket: the #GSocket associated with @base_io_stream
165 * Wraps @base_io_stream and @socket together as a #GSocketConnection.
167 * Returns: the new #GSocketConnection.
169 * Since: 2.28
171 GSocketConnection *
172 g_tcp_wrapper_connection_new (GIOStream *base_io_stream,
173 GSocket *socket)
175 g_return_val_if_fail (G_IS_IO_STREAM (base_io_stream), NULL);
176 g_return_val_if_fail (G_IS_SOCKET (socket), NULL);
177 g_return_val_if_fail (g_socket_get_family (socket) == G_SOCKET_FAMILY_IPV4 ||
178 g_socket_get_family (socket) == G_SOCKET_FAMILY_IPV6, NULL);
179 g_return_val_if_fail (g_socket_get_socket_type (socket) == G_SOCKET_TYPE_STREAM, NULL);
181 return g_object_new (G_TYPE_TCP_WRAPPER_CONNECTION,
182 "base-io-stream", base_io_stream,
183 "socket", socket,
184 NULL);
188 * g_tcp_wrapper_connection_get_base_io_stream:
189 * @conn: a #GTcpWrapperConnection
191 * Get's @conn's base #GIOStream
193 * Returns: (transfer none): @conn's base #GIOStream
195 GIOStream *
196 g_tcp_wrapper_connection_get_base_io_stream (GTcpWrapperConnection *conn)
198 g_return_val_if_fail (G_IS_TCP_WRAPPER_CONNECTION (conn), NULL);
200 return conn->priv->base_io_stream;