1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Authors: Jeffrey Stedfast <fejj@ximian.com>
5 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of version 2 of the GNU Lesser General Public
9 * License as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
29 #include "camel-debug.h"
30 #include "camel-tcp-stream.h"
39 #define CAMEL_TCP_STREAM_GET_PRIVATE(obj) \
40 (G_TYPE_INSTANCE_GET_PRIVATE \
41 ((obj), CAMEL_TYPE_TCP_STREAM, CamelTcpStreamPrivate))
43 struct _CamelTcpStreamPrivate {
48 G_DEFINE_TYPE (CamelTcpStream, camel_tcp_stream, CAMEL_TYPE_STREAM)
51 camel_tcp_stream_finalize (GObject *object)
53 CamelTcpStream *stream = CAMEL_TCP_STREAM (object);
55 g_free (stream->priv->socks_host);
57 /* Chain up to parent's finalize() method. */
58 G_OBJECT_CLASS (camel_tcp_stream_parent_class)->finalize (object);
62 camel_tcp_stream_class_init (CamelTcpStreamClass *class)
64 GObjectClass *object_class;
66 g_type_class_add_private (class, sizeof (CamelTcpStreamPrivate));
68 object_class = G_OBJECT_CLASS (class);
69 object_class->finalize = camel_tcp_stream_finalize;
73 camel_tcp_stream_init (CamelTcpStream *tcp_stream)
75 tcp_stream->priv = CAMEL_TCP_STREAM_GET_PRIVATE (tcp_stream);
79 * camel_tcp_stream_connect:
80 * @stream: a #CamelTcpStream object
81 * @host: Hostname for connection
82 * @service: Service name or port number in string form
83 * @fallback_port: Port number to retry if @service is not present
84 * in the system's services database, or 0 to avoid retrying
85 * @cancellable: optional #GCancellable object, or %NULL
86 * @error: return location for a #GError, or %NULL
88 * Create a socket and connect based upon the data provided.
90 * Returns: %0 on success or %-1 on fail
93 camel_tcp_stream_connect (CamelTcpStream *stream,
97 GCancellable *cancellable,
100 CamelTcpStreamClass *class;
103 g_return_val_if_fail (CAMEL_IS_TCP_STREAM (stream), -1);
104 g_return_val_if_fail (host != NULL, -1);
105 g_return_val_if_fail (service != NULL, -1);
106 g_return_val_if_fail (error == NULL || *error == NULL, -1);
108 class = CAMEL_TCP_STREAM_GET_CLASS (stream);
109 g_return_val_if_fail (class->connect != NULL, -1);
111 retval = class->connect (
112 stream, host, service, fallback_port, cancellable, error);
113 CAMEL_CHECK_GERROR (stream, connect, retval == 0, error);
119 * camel_tcp_stream_getsockopt:
120 * @stream: a #CamelTcpStream object
121 * @data: socket option data
123 * Get the socket options set on the stream and populate @data.
125 * Returns: %0 on success or %-1 on fail
128 camel_tcp_stream_getsockopt (CamelTcpStream *stream,
129 CamelSockOptData *data)
131 CamelTcpStreamClass *class;
133 g_return_val_if_fail (CAMEL_IS_TCP_STREAM (stream), -1);
135 class = CAMEL_TCP_STREAM_GET_CLASS (stream);
136 g_return_val_if_fail (class->getsockopt != NULL, -1);
138 return class->getsockopt (stream, data);
142 * camel_tcp_stream_setsockopt:
143 * @stream: a #CamelTcpStream object
144 * @data: socket option data
146 * Set the socket options contained in @data on the stream.
148 * Returns: %0 on success or %-1 on fail
151 camel_tcp_stream_setsockopt (CamelTcpStream *stream,
152 const CamelSockOptData *data)
154 CamelTcpStreamClass *class;
156 g_return_val_if_fail (CAMEL_IS_TCP_STREAM (stream), -1);
158 class = CAMEL_TCP_STREAM_GET_CLASS (stream);
159 g_return_val_if_fail (class->setsockopt != NULL, -1);
161 return class->setsockopt (stream, data);
165 * camel_tcp_stream_get_local_address:
166 * @stream: a #CamelTcpStream object
167 * @len: pointer to address length which must be supplied
169 * Get the local address of @stream.
171 * Returns: the stream's local address (which must be freed with
172 * g_free()) if the stream is connected, or %NULL if not
177 camel_tcp_stream_get_local_address (CamelTcpStream *stream,
180 CamelTcpStreamClass *class;
182 g_return_val_if_fail (CAMEL_IS_TCP_STREAM (stream), NULL);
183 g_return_val_if_fail (len != NULL, NULL);
185 class = CAMEL_TCP_STREAM_GET_CLASS (stream);
186 g_return_val_if_fail (class->get_local_address != NULL, NULL);
188 return class->get_local_address (stream, len);
192 * camel_tcp_stream_get_remote_address:
193 * @stream: a #CamelTcpStream object
194 * @len: pointer to address length, which must be supplied
196 * Get the remote address of @stream.
198 * Returns: the stream's remote address (which must be freed with
199 * g_free()) if the stream is connected, or %NULL if not.
204 camel_tcp_stream_get_remote_address (CamelTcpStream *stream,
207 CamelTcpStreamClass *class;
209 g_return_val_if_fail (CAMEL_IS_TCP_STREAM (stream), NULL);
210 g_return_val_if_fail (len != NULL, NULL);
212 class = CAMEL_TCP_STREAM_GET_CLASS (stream);
213 g_return_val_if_fail (class->get_remote_address != NULL, NULL);
215 return class->get_remote_address (stream, len);
219 * camel_tcp_stream_get_file_desc:
220 * @stream: a #CamelTcpStream
225 camel_tcp_stream_get_file_desc (CamelTcpStream *stream)
227 CamelTcpStreamClass *class;
229 g_return_val_if_fail (CAMEL_IS_TCP_STREAM (stream), NULL);
231 class = CAMEL_TCP_STREAM_GET_CLASS (stream);
232 g_return_val_if_fail (class->get_file_desc != NULL, NULL);
234 return class->get_file_desc (stream);
238 * camel_tcp_stream_set_socks_proxy:
239 * @stream: a #CamelTcpStream object
240 * @socks_host: hostname to use for the SOCKS proxy
241 * @socks_port: port number to use for the SOCKS proxy
243 * Configures a SOCKS proxy for the specified @stream. Instead of
244 * direct connections, this @stream will instead go through the proxy.
249 camel_tcp_stream_set_socks_proxy (CamelTcpStream *stream,
250 const gchar *socks_host,
253 g_return_if_fail (CAMEL_IS_TCP_STREAM (stream));
255 g_free (stream->priv->socks_host);
257 if (socks_host != NULL && socks_host[0] != '\0') {
258 stream->priv->socks_host = g_strdup (socks_host);
259 stream->priv->socks_port = socks_port;
261 stream->priv->socks_host = NULL;
262 stream->priv->socks_port = 0;
267 * camel_tcp_stream_peek_socks_proxy:
268 * @stream: a #CamelTcpStream
269 * @socks_host_ret: location to return the name of the SOCKS host
270 * @socks_port_ret: location to return the port number in the SOCKS host
272 * Queries the SOCKS proxy that is configured for a @stream. This will
273 * return %NULL in @socks_host_ret if no proxy is configured.
278 camel_tcp_stream_peek_socks_proxy (CamelTcpStream *stream,
279 const gchar **socks_host_ret,
280 gint *socks_port_ret)
282 g_return_if_fail (CAMEL_IS_TCP_STREAM (stream));
284 if (socks_host_ret != NULL)
285 *socks_host_ret = stream->priv->socks_host;
287 if (socks_port_ret != NULL)
288 *socks_port_ret = stream->priv->socks_port;