comparison libgaim/sslconn.c @ 14192:60b1bc8dbf37

[gaim-migrate @ 16863] Renamed 'core' to 'libgaim' committer: Tailor Script <tailor@pidgin.im>
author Evan Schoenberg <evan.s@dreskin.net>
date Sat, 19 Aug 2006 01:50:10 +0000
parents
children baff095b146c
comparison
equal deleted inserted replaced
14191:009db0b357b5 14192:60b1bc8dbf37
1 /**
2 * @file sslconn.c SSL API
3 * @ingroup core
4 *
5 * gaim
6 *
7 * Gaim is the legal property of its developers, whose names are too numerous
8 * to list here. Please refer to the COPYRIGHT file distributed with this
9 * source distribution.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25 #include "internal.h"
26
27 #include "debug.h"
28 #include "sslconn.h"
29
30 static gboolean _ssl_initialized = FALSE;
31 static GaimSslOps *_ssl_ops = NULL;
32
33 static gboolean
34 ssl_init(void)
35 {
36 GaimPlugin *plugin;
37 GaimSslOps *ops;
38
39 if (_ssl_initialized)
40 return FALSE;
41
42 plugin = gaim_plugins_find_with_id("core-ssl");
43
44 if (plugin != NULL && !gaim_plugin_is_loaded(plugin))
45 gaim_plugin_load(plugin);
46
47 ops = gaim_ssl_get_ops();
48 if ((ops == NULL) || (ops->init == NULL) || (ops->uninit == NULL) ||
49 (ops->connectfunc == NULL) || (ops->close == NULL) ||
50 (ops->read == NULL) || (ops->write == NULL))
51 {
52 return FALSE;
53 }
54
55 return ops->init();
56 }
57
58 gboolean
59 gaim_ssl_is_supported(void)
60 {
61 #ifdef HAVE_SSL
62 ssl_init();
63 return (gaim_ssl_get_ops() != NULL);
64 #else
65 return FALSE;
66 #endif
67 }
68
69 static void
70 gaim_ssl_connect_cb(gpointer data, gint source, const gchar *error_message)
71 {
72 GaimSslConnection *gsc;
73 GaimSslOps *ops;
74
75 gsc = data;
76 gsc->connect_info = NULL;
77
78 if (source < 0)
79 {
80 if (gsc->error_cb != NULL)
81 gsc->error_cb(gsc, GAIM_SSL_CONNECT_FAILED, gsc->connect_cb_data);
82
83 gaim_ssl_close(gsc);
84 return;
85 }
86
87 gsc->fd = source;
88
89 ops = gaim_ssl_get_ops();
90 ops->connectfunc(gsc);
91 }
92
93 GaimSslConnection *
94 gaim_ssl_connect(GaimAccount *account, const char *host, int port,
95 GaimSslInputFunction func, GaimSslErrorFunction error_func,
96 void *data)
97 {
98 GaimSslConnection *gsc;
99
100 g_return_val_if_fail(host != NULL, NULL);
101 g_return_val_if_fail(port != 0 && port != -1, NULL);
102 g_return_val_if_fail(func != NULL, NULL);
103 g_return_val_if_fail(gaim_ssl_is_supported(), NULL);
104
105 if (!_ssl_initialized)
106 {
107 if (!ssl_init())
108 return NULL;
109 }
110
111 gsc = g_new0(GaimSslConnection, 1);
112
113 gsc->fd = -1;
114 gsc->host = g_strdup(host);
115 gsc->port = port;
116 gsc->connect_cb_data = data;
117 gsc->connect_cb = func;
118 gsc->error_cb = error_func;
119
120 gsc->connect_info = gaim_proxy_connect(account, host, port, gaim_ssl_connect_cb, gsc);
121
122 if (gsc->connect_info == NULL)
123 {
124 g_free(gsc->host);
125 g_free(gsc);
126
127 return NULL;
128 }
129
130 return (GaimSslConnection *)gsc;
131 }
132
133 static void
134 recv_cb(gpointer data, gint source, GaimInputCondition cond)
135 {
136 GaimSslConnection *gsc = data;
137
138 gsc->recv_cb(gsc->recv_cb_data, gsc, cond);
139 }
140
141 void
142 gaim_ssl_input_add(GaimSslConnection *gsc, GaimSslInputFunction func,
143 void *data)
144 {
145 g_return_if_fail(func != NULL);
146 g_return_if_fail(gaim_ssl_is_supported());
147
148 gsc->recv_cb_data = data;
149 gsc->recv_cb = func;
150
151 gsc->inpa = gaim_input_add(gsc->fd, GAIM_INPUT_READ, recv_cb, gsc);
152 }
153
154 GaimSslConnection *
155 gaim_ssl_connect_fd(GaimAccount *account, int fd,
156 GaimSslInputFunction func,
157 GaimSslErrorFunction error_func, void *data)
158 {
159 GaimSslConnection *gsc;
160 GaimSslOps *ops;
161
162 g_return_val_if_fail(fd != -1, NULL);
163 g_return_val_if_fail(func != NULL, NULL);
164 g_return_val_if_fail(gaim_ssl_is_supported(), NULL);
165
166 if (!_ssl_initialized)
167 {
168 if (!ssl_init())
169 return NULL;
170 }
171
172 gsc = g_new0(GaimSslConnection, 1);
173
174 gsc->connect_cb_data = data;
175 gsc->connect_cb = func;
176 gsc->error_cb = error_func;
177 gsc->fd = fd;
178
179 ops = gaim_ssl_get_ops();
180 ops->connectfunc(gsc);
181
182 return (GaimSslConnection *)gsc;
183 }
184
185 void
186 gaim_ssl_close(GaimSslConnection *gsc)
187 {
188 GaimSslOps *ops;
189
190 g_return_if_fail(gsc != NULL);
191
192 ops = gaim_ssl_get_ops();
193 (ops->close)(gsc);
194
195 if (gsc->connect_info != NULL)
196 gaim_proxy_connect_cancel(gsc->connect_info);
197
198 if (gsc->inpa > 0)
199 gaim_input_remove(gsc->inpa);
200
201 if (gsc->fd >= 0)
202 close(gsc->fd);
203
204 g_free(gsc->host);
205 g_free(gsc);
206 }
207
208 size_t
209 gaim_ssl_read(GaimSslConnection *gsc, void *data, size_t len)
210 {
211 GaimSslOps *ops;
212
213 g_return_val_if_fail(gsc != NULL, 0);
214 g_return_val_if_fail(data != NULL, 0);
215 g_return_val_if_fail(len > 0, 0);
216
217 ops = gaim_ssl_get_ops();
218 return (ops->read)(gsc, data, len);
219 }
220
221 size_t
222 gaim_ssl_write(GaimSslConnection *gsc, const void *data, size_t len)
223 {
224 GaimSslOps *ops;
225
226 g_return_val_if_fail(gsc != NULL, 0);
227 g_return_val_if_fail(data != NULL, 0);
228 g_return_val_if_fail(len > 0, 0);
229
230 ops = gaim_ssl_get_ops();
231 return (ops->write)(gsc, data, len);
232 }
233
234 void
235 gaim_ssl_set_ops(GaimSslOps *ops)
236 {
237 _ssl_ops = ops;
238 }
239
240 GaimSslOps *
241 gaim_ssl_get_ops(void)
242 {
243 return _ssl_ops;
244 }
245
246 void
247 gaim_ssl_init(void)
248 {
249 }
250
251 void
252 gaim_ssl_uninit(void)
253 {
254 GaimSslOps *ops;
255
256 if (!_ssl_initialized)
257 return;
258
259 ops = gaim_ssl_get_ops();
260 ops->uninit();
261
262 _ssl_initialized = FALSE;
263 }