src/sslconn.c

branch
cpw.khc.msnp14
changeset 20472
6a6d2ef151e6
parent 13912
463b4fa9f067
parent 20469
b2836a24d81e
child 20473
91e1b3a49d10
equal deleted inserted replaced
13912:463b4fa9f067 20472:6a6d2ef151e6
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)
49 return ops->init();
50 else
51 return FALSE;
52 }
53
54 gboolean
55 gaim_ssl_is_supported(void)
56 {
57 #ifdef HAVE_SSL
58 ssl_init();
59 return (gaim_ssl_get_ops() != NULL);
60 #else
61 return FALSE;
62 #endif
63 }
64
65 GaimSslConnection *
66 gaim_ssl_connect(GaimAccount *account, const char *host, int port,
67 GaimSslInputFunction func, GaimSslErrorFunction error_func,
68 void *data)
69 {
70 GaimSslConnection *gsc;
71 GaimSslOps *ops;
72 int i;
73
74 g_return_val_if_fail(host != NULL, NULL);
75 g_return_val_if_fail(port != 0 && port != -1, NULL);
76 g_return_val_if_fail(func != NULL, NULL);
77 g_return_val_if_fail(gaim_ssl_is_supported(), NULL);
78
79 ops = gaim_ssl_get_ops();
80
81 g_return_val_if_fail(ops != NULL, NULL);
82 g_return_val_if_fail(ops->connect_cb != NULL, NULL);
83
84 if (!_ssl_initialized)
85 {
86 if (!ssl_init())
87 return NULL;
88 }
89
90 gsc = g_new0(GaimSslConnection, 1);
91
92 gsc->host = g_strdup(host);
93 gsc->port = port;
94 gsc->connect_cb_data = data;
95 gsc->connect_cb = func;
96 gsc->error_cb = error_func;
97
98 i = gaim_proxy_connect(account, host, port, ops->connect_cb, gsc);
99
100 if (i < 0)
101 {
102 g_free(gsc->host);
103 g_free(gsc);
104
105 return NULL;
106 }
107
108 return (GaimSslConnection *)gsc;
109 }
110
111 static void
112 recv_cb(gpointer data, gint source, GaimInputCondition cond)
113 {
114 GaimSslConnection *gsc = data;
115
116 gsc->recv_cb(gsc->recv_cb_data, gsc, cond);
117 }
118
119 void
120 gaim_ssl_input_add(GaimSslConnection *gsc, GaimSslInputFunction func,
121 void *data)
122 {
123 g_return_if_fail(func != NULL);
124 g_return_if_fail(gaim_ssl_is_supported());
125
126 gsc->recv_cb_data = data;
127 gsc->recv_cb = func;
128
129 gsc->inpa = gaim_input_add(gsc->fd, GAIM_INPUT_READ, recv_cb, gsc);
130 }
131
132 GaimSslConnection *
133 gaim_ssl_connect_fd(GaimAccount *account, int fd,
134 GaimSslInputFunction func,
135 GaimSslErrorFunction error_func, void *data)
136 {
137 GaimSslConnection *gsc;
138 GaimSslOps *ops;
139
140 g_return_val_if_fail(fd > 0, NULL);
141 g_return_val_if_fail(func != NULL, NULL);
142 g_return_val_if_fail(gaim_ssl_is_supported(), NULL);
143
144 ops = gaim_ssl_get_ops();
145
146 g_return_val_if_fail(ops != NULL, NULL);
147 g_return_val_if_fail(ops->connect_cb != NULL, NULL);
148
149 if (!_ssl_initialized)
150 {
151 if (!ssl_init())
152 return NULL;
153 }
154
155 gsc = g_new0(GaimSslConnection, 1);
156
157 gsc->connect_cb_data = data;
158 gsc->connect_cb = func;
159 gsc->error_cb = error_func;
160
161 ops->connect_cb(gsc, fd, GAIM_INPUT_READ);
162
163 return (GaimSslConnection *)gsc;
164 }
165
166 void
167 gaim_ssl_close(GaimSslConnection *gsc)
168 {
169 GaimSslOps *ops;
170
171 g_return_if_fail(gsc != NULL);
172
173 ops = gaim_ssl_get_ops();
174
175 if (gsc->inpa)
176 gaim_input_remove(gsc->inpa);
177
178 if (ops != NULL && ops->close != NULL)
179 (ops->close)(gsc);
180
181 if (gsc->fd)
182 close(gsc->fd);
183
184 if (gsc->host != NULL)
185 g_free(gsc->host);
186
187 g_free(gsc);
188 }
189
190 size_t
191 gaim_ssl_read(GaimSslConnection *gsc, void *data, size_t len)
192 {
193 GaimSslOps *ops;
194
195 g_return_val_if_fail(gsc != NULL, 0);
196 g_return_val_if_fail(data != NULL, 0);
197 g_return_val_if_fail(len > 0, 0);
198
199 ops = gaim_ssl_get_ops();
200
201 if (ops != NULL && (ops->read) != NULL)
202 return (ops->read)(gsc, data, len);
203
204 return 0;
205 }
206
207 size_t
208 gaim_ssl_write(GaimSslConnection *gsc, const void *data, size_t len)
209 {
210 GaimSslOps *ops;
211
212 g_return_val_if_fail(gsc != NULL, 0);
213 g_return_val_if_fail(data != NULL, 0);
214 g_return_val_if_fail(len > 0, 0);
215
216 ops = gaim_ssl_get_ops();
217
218 if (ops != NULL && (ops->write) != NULL)
219 return (ops->write)(gsc, data, len);
220
221 return 0;
222 }
223
224 void
225 gaim_ssl_set_ops(GaimSslOps *ops)
226 {
227 _ssl_ops = ops;
228 }
229
230 GaimSslOps *
231 gaim_ssl_get_ops(void)
232 {
233 return _ssl_ops;
234 }
235
236 void
237 gaim_ssl_init(void)
238 {
239 }
240
241 void
242 gaim_ssl_uninit(void)
243 {
244 GaimSslOps *ops;
245
246 if (!_ssl_initialized)
247 return;
248
249 ops = gaim_ssl_get_ops();
250
251 if (ops != NULL && ops->uninit != NULL)
252 ops->uninit();
253
254 _ssl_initialized = FALSE;
255 }

mercurial