comparison src/sslconn.c @ 14160:c8ebbc0110f4

[gaim-migrate @ 16808] gaim_ssl_connect's are now cancelable (without crashing, anyway) This was relatively easy, because the PRPLs already keep a reference to the GaimSslConnection. I just needed to update the core ssl code to keep track of the GaimProxyConnectInfo, and to call gaim_proxy_connect_cancel() when gaim_ssl_close() is called committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Thu, 17 Aug 2006 05:47:10 +0000
parents 7a205b430d19
children fcae2cfe26eb
comparison
equal deleted inserted replaced
14159:ff3db5cdeb9f 14160:c8ebbc0110f4
43 43
44 if (plugin != NULL && !gaim_plugin_is_loaded(plugin)) 44 if (plugin != NULL && !gaim_plugin_is_loaded(plugin))
45 gaim_plugin_load(plugin); 45 gaim_plugin_load(plugin);
46 46
47 ops = gaim_ssl_get_ops(); 47 ops = gaim_ssl_get_ops();
48 if (ops != NULL && ops->init != NULL) 48 if ((ops == NULL) || (ops->init == NULL) || (ops->uninit == NULL) ||
49 return ops->init(); 49 (ops->connect == NULL) || (ops->close == NULL) ||
50 else 50 (ops->read == NULL) || (ops->write == NULL))
51 {
51 return FALSE; 52 return FALSE;
53 }
54
55 return ops->init();
52 } 56 }
53 57
54 gboolean 58 gboolean
55 gaim_ssl_is_supported(void) 59 gaim_ssl_is_supported(void)
56 { 60 {
60 #else 64 #else
61 return FALSE; 65 return FALSE;
62 #endif 66 #endif
63 } 67 }
64 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->connect(gsc);
91 }
92
65 GaimSslConnection * 93 GaimSslConnection *
66 gaim_ssl_connect(GaimAccount *account, const char *host, int port, 94 gaim_ssl_connect(GaimAccount *account, const char *host, int port,
67 GaimSslInputFunction func, GaimSslErrorFunction error_func, 95 GaimSslInputFunction func, GaimSslErrorFunction error_func,
68 void *data) 96 void *data)
69 { 97 {
70 GaimSslConnection *gsc; 98 GaimSslConnection *gsc;
71 GaimSslOps *ops;
72 GaimProxyConnectInfo *connect_info;
73 99
74 g_return_val_if_fail(host != NULL, NULL); 100 g_return_val_if_fail(host != NULL, NULL);
75 g_return_val_if_fail(port != 0 && port != -1, NULL); 101 g_return_val_if_fail(port != 0 && port != -1, NULL);
76 g_return_val_if_fail(func != NULL, NULL); 102 g_return_val_if_fail(func != NULL, NULL);
77 g_return_val_if_fail(gaim_ssl_is_supported(), NULL); 103 g_return_val_if_fail(gaim_ssl_is_supported(), NULL);
78 104
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) 105 if (!_ssl_initialized)
85 { 106 {
86 if (!ssl_init()) 107 if (!ssl_init())
87 return NULL; 108 return NULL;
88 } 109 }
89 110
90 gsc = g_new0(GaimSslConnection, 1); 111 gsc = g_new0(GaimSslConnection, 1);
91 112
113 gsc->fd = -1;
92 gsc->host = g_strdup(host); 114 gsc->host = g_strdup(host);
93 gsc->port = port; 115 gsc->port = port;
94 gsc->connect_cb_data = data; 116 gsc->connect_cb_data = data;
95 gsc->connect_cb = func; 117 gsc->connect_cb = func;
96 gsc->error_cb = error_func; 118 gsc->error_cb = error_func;
97 119
98 connect_info = gaim_proxy_connect(account, host, port, ops->connect_cb, gsc); 120 gsc->connect_info = gaim_proxy_connect(account, host, port, gaim_ssl_connect_cb, gsc);
99 121
100 if (connect_info == NULL) 122 if (gsc->connect_info == NULL)
101 { 123 {
102 g_free(gsc->host); 124 g_free(gsc->host);
103 g_free(gsc); 125 g_free(gsc);
104 126
105 return NULL; 127 return NULL;
139 161
140 g_return_val_if_fail(fd != -1, NULL); 162 g_return_val_if_fail(fd != -1, NULL);
141 g_return_val_if_fail(func != NULL, NULL); 163 g_return_val_if_fail(func != NULL, NULL);
142 g_return_val_if_fail(gaim_ssl_is_supported(), NULL); 164 g_return_val_if_fail(gaim_ssl_is_supported(), NULL);
143 165
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) 166 if (!_ssl_initialized)
150 { 167 {
151 if (!ssl_init()) 168 if (!ssl_init())
152 return NULL; 169 return NULL;
153 } 170 }
155 gsc = g_new0(GaimSslConnection, 1); 172 gsc = g_new0(GaimSslConnection, 1);
156 173
157 gsc->connect_cb_data = data; 174 gsc->connect_cb_data = data;
158 gsc->connect_cb = func; 175 gsc->connect_cb = func;
159 gsc->error_cb = error_func; 176 gsc->error_cb = error_func;
160 177 gsc->fd = fd;
161 ops->connect_cb(gsc, fd, GAIM_INPUT_READ); 178
179 ops = gaim_ssl_get_ops();
180 ops->connect(gsc);
162 181
163 return (GaimSslConnection *)gsc; 182 return (GaimSslConnection *)gsc;
164 } 183 }
165 184
166 void 185 void
169 GaimSslOps *ops; 188 GaimSslOps *ops;
170 189
171 g_return_if_fail(gsc != NULL); 190 g_return_if_fail(gsc != NULL);
172 191
173 ops = gaim_ssl_get_ops(); 192 ops = gaim_ssl_get_ops();
174 193 (ops->close)(gsc);
175 if (gsc->inpa) 194
195 if (gsc->connect_info != NULL)
196 gaim_proxy_connect_cancel(gsc->connect_info);
197
198 if (gsc->inpa > 0)
176 gaim_input_remove(gsc->inpa); 199 gaim_input_remove(gsc->inpa);
177 200
178 if (ops != NULL && ops->close != NULL) 201 if (gsc->fd >= 0)
179 (ops->close)(gsc);
180
181 if (gsc->fd != -1)
182 close(gsc->fd); 202 close(gsc->fd);
183 203
184 g_free(gsc->host); 204 g_free(gsc->host);
185 g_free(gsc); 205 g_free(gsc);
186 } 206 }
193 g_return_val_if_fail(gsc != NULL, 0); 213 g_return_val_if_fail(gsc != NULL, 0);
194 g_return_val_if_fail(data != NULL, 0); 214 g_return_val_if_fail(data != NULL, 0);
195 g_return_val_if_fail(len > 0, 0); 215 g_return_val_if_fail(len > 0, 0);
196 216
197 ops = gaim_ssl_get_ops(); 217 ops = gaim_ssl_get_ops();
198 218 return (ops->read)(gsc, data, len);
199 if (ops != NULL && (ops->read) != NULL)
200 return (ops->read)(gsc, data, len);
201
202 return 0;
203 } 219 }
204 220
205 size_t 221 size_t
206 gaim_ssl_write(GaimSslConnection *gsc, const void *data, size_t len) 222 gaim_ssl_write(GaimSslConnection *gsc, const void *data, size_t len)
207 { 223 {
210 g_return_val_if_fail(gsc != NULL, 0); 226 g_return_val_if_fail(gsc != NULL, 0);
211 g_return_val_if_fail(data != NULL, 0); 227 g_return_val_if_fail(data != NULL, 0);
212 g_return_val_if_fail(len > 0, 0); 228 g_return_val_if_fail(len > 0, 0);
213 229
214 ops = gaim_ssl_get_ops(); 230 ops = gaim_ssl_get_ops();
215 231 return (ops->write)(gsc, data, len);
216 if (ops != NULL && (ops->write) != NULL)
217 return (ops->write)(gsc, data, len);
218
219 return 0;
220 } 232 }
221 233
222 void 234 void
223 gaim_ssl_set_ops(GaimSslOps *ops) 235 gaim_ssl_set_ops(GaimSslOps *ops)
224 { 236 {
243 255
244 if (!_ssl_initialized) 256 if (!_ssl_initialized)
245 return; 257 return;
246 258
247 ops = gaim_ssl_get_ops(); 259 ops = gaim_ssl_get_ops();
248 260 ops->uninit();
249 if (ops != NULL && ops->uninit != NULL)
250 ops->uninit();
251 261
252 _ssl_initialized = FALSE; 262 _ssl_initialized = FALSE;
253 } 263 }