summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bio/bss_conn.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/bio/bss_conn.c')
-rw-r--r--src/lib/libcrypto/bio/bss_conn.c651
1 files changed, 310 insertions, 341 deletions
diff --git a/src/lib/libcrypto/bio/bss_conn.c b/src/lib/libcrypto/bio/bss_conn.c
index 5162e75c87..df34c49a4a 100644
--- a/src/lib/libcrypto/bio/bss_conn.c
+++ b/src/lib/libcrypto/bio/bss_conn.c
@@ -76,8 +76,7 @@
76#endif 76#endif
77 77
78 78
79typedef struct bio_connect_st 79typedef struct bio_connect_st {
80 {
81 int state; 80 int state;
82 81
83 char *param_hostname; 82 char *param_hostname;
@@ -95,8 +94,8 @@ typedef struct bio_connect_st
95 /* called when the connection is initially made 94 /* called when the connection is initially made
96 * callback(BIO,state,ret); The callback should return 95 * callback(BIO,state,ret); The callback should return
97 * 'ret'. state is for compatibility with the ssl info_callback */ 96 * 'ret'. state is for compatibility with the ssl info_callback */
98 int (*info_callback)(const BIO *bio,int state,int ret); 97 int (*info_callback)(const BIO *bio, int state, int ret);
99 } BIO_CONNECT; 98} BIO_CONNECT;
100 99
101static int conn_write(BIO *h, const char *buf, int num); 100static int conn_write(BIO *h, const char *buf, int num);
102static int conn_read(BIO *h, char *buf, int size); 101static int conn_read(BIO *h, char *buf, int size);
@@ -108,11 +107,10 @@ static long conn_callback_ctrl(BIO *h, int cmd, bio_info_cb *);
108 107
109static int conn_state(BIO *b, BIO_CONNECT *c); 108static int conn_state(BIO *b, BIO_CONNECT *c);
110static void conn_close_socket(BIO *data); 109static void conn_close_socket(BIO *data);
111BIO_CONNECT *BIO_CONNECT_new(void ); 110BIO_CONNECT *BIO_CONNECT_new(void);
112void BIO_CONNECT_free(BIO_CONNECT *a); 111void BIO_CONNECT_free(BIO_CONNECT *a);
113 112
114static BIO_METHOD methods_connectp= 113static BIO_METHOD methods_connectp = {
115 {
116 BIO_TYPE_CONNECT, 114 BIO_TYPE_CONNECT,
117 "socket connect", 115 "socket connect",
118 conn_write, 116 conn_write,
@@ -123,530 +121,501 @@ static BIO_METHOD methods_connectp=
123 conn_new, 121 conn_new,
124 conn_free, 122 conn_free,
125 conn_callback_ctrl, 123 conn_callback_ctrl,
126 }; 124};
127 125
128static int conn_state(BIO *b, BIO_CONNECT *c) 126static int
129 { 127conn_state(BIO *b, BIO_CONNECT *c)
130 int ret= -1,i; 128{
129 int ret = -1, i;
131 unsigned long l; 130 unsigned long l;
132 char *p,*q; 131 char *p, *q;
133 int (*cb)(const BIO *,int,int)=NULL; 132 int (*cb)(const BIO *, int, int) = NULL;
134 133
135 if (c->info_callback != NULL) 134 if (c->info_callback != NULL)
136 cb=c->info_callback; 135 cb = c->info_callback;
137 136
138 for (;;) 137 for (;;) {
139 { 138 switch (c->state) {
140 switch (c->state)
141 {
142 case BIO_CONN_S_BEFORE: 139 case BIO_CONN_S_BEFORE:
143 p=c->param_hostname; 140 p = c->param_hostname;
144 if (p == NULL) 141 if (p == NULL) {
145 { 142 BIOerr(BIO_F_CONN_STATE, BIO_R_NO_HOSTNAME_SPECIFIED);
146 BIOerr(BIO_F_CONN_STATE,BIO_R_NO_HOSTNAME_SPECIFIED);
147 goto exit_loop; 143 goto exit_loop;
148 } 144 }
149 for ( ; *p != '\0'; p++) 145 for (; *p != '\0'; p++) {
150 { 146 if ((*p == ':') || (*p == '/'))
151 if ((*p == ':') || (*p == '/')) break; 147 break;
152 } 148 }
153 149
154 i= *p; 150 i= *p;
155 if ((i == ':') || (i == '/')) 151 if ((i == ':') || (i == '/')) {
156 { 152 *(p++) = '\0';
157 153 if (i == ':') {
158 *(p++)='\0'; 154 for (q = p; *q; q++)
159 if (i == ':') 155 if (*q == '/') {
160 { 156 *q = '\0';
161 for (q=p; *q; q++)
162 if (*q == '/')
163 {
164 *q='\0';
165 break; 157 break;
166 } 158 }
167 if (c->param_port != NULL) 159 if (c->param_port != NULL)
168 OPENSSL_free(c->param_port); 160 OPENSSL_free(c->param_port);
169 c->param_port=BUF_strdup(p); 161 c->param_port = BUF_strdup(p);
170 }
171 } 162 }
163 }
172 164
173 if (c->param_port == NULL) 165 if (c->param_port == NULL) {
174 { 166 BIOerr(BIO_F_CONN_STATE, BIO_R_NO_PORT_SPECIFIED);
175 BIOerr(BIO_F_CONN_STATE,BIO_R_NO_PORT_SPECIFIED); 167 ERR_add_error_data(2, "host=", c->param_hostname);
176 ERR_add_error_data(2,"host=",c->param_hostname);
177 goto exit_loop; 168 goto exit_loop;
178 } 169 }
179 c->state=BIO_CONN_S_GET_IP; 170 c->state = BIO_CONN_S_GET_IP;
180 break; 171 break;
181 172
182 case BIO_CONN_S_GET_IP: 173 case BIO_CONN_S_GET_IP:
183 if (BIO_get_host_ip(c->param_hostname,&(c->ip[0])) <= 0) 174 if (BIO_get_host_ip(c->param_hostname, &(c->ip[0])) <= 0)
184 goto exit_loop; 175 goto exit_loop;
185 c->state=BIO_CONN_S_GET_PORT; 176 c->state = BIO_CONN_S_GET_PORT;
186 break; 177 break;
187 178
188 case BIO_CONN_S_GET_PORT: 179 case BIO_CONN_S_GET_PORT:
189 if (c->param_port == NULL) 180 if (c->param_port == NULL) {
190 {
191 /* abort(); */ 181 /* abort(); */
192 goto exit_loop; 182 goto exit_loop;
193 } 183 } else if (BIO_get_port(c->param_port, &c->port) <= 0)
194 else if (BIO_get_port(c->param_port,&c->port) <= 0)
195 goto exit_loop; 184 goto exit_loop;
196 c->state=BIO_CONN_S_CREATE_SOCKET; 185 c->state = BIO_CONN_S_CREATE_SOCKET;
197 break; 186 break;
198 187
199 case BIO_CONN_S_CREATE_SOCKET: 188 case BIO_CONN_S_CREATE_SOCKET:
200 /* now setup address */ 189 /* now setup address */
201 memset((char *)&c->them,0,sizeof(c->them)); 190 memset((char *)&c->them, 0, sizeof(c->them));
202 c->them.sin_family=AF_INET; 191 c->them.sin_family = AF_INET;
203 c->them.sin_port=htons((unsigned short)c->port); 192 c->them.sin_port = htons((unsigned short)c->port);
204 l=(unsigned long) 193 l = (unsigned long)
205 ((unsigned long)c->ip[0]<<24L)| 194 ((unsigned long)c->ip[0] << 24L)|
206 ((unsigned long)c->ip[1]<<16L)| 195 ((unsigned long)c->ip[1] << 16L)|
207 ((unsigned long)c->ip[2]<< 8L)| 196 ((unsigned long)c->ip[2] << 8L)|
208 ((unsigned long)c->ip[3]); 197 ((unsigned long)c->ip[3]);
209 c->them.sin_addr.s_addr=htonl(l); 198 c->them.sin_addr.s_addr = htonl(l);
210 c->state=BIO_CONN_S_CREATE_SOCKET; 199 c->state = BIO_CONN_S_CREATE_SOCKET;
211 200
212 ret=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL); 201 ret = socket(AF_INET, SOCK_STREAM, SOCKET_PROTOCOL);
213 if (ret == -1) 202 if (ret == -1) {
214 { 203 SYSerr(SYS_F_SOCKET, errno);
215 SYSerr(SYS_F_SOCKET,errno); 204 ERR_add_error_data(4, "host=",
216 ERR_add_error_data(4,"host=",c->param_hostname, 205 c->param_hostname, ":", c->param_port);
217 ":",c->param_port); 206 BIOerr(BIO_F_CONN_STATE,
218 BIOerr(BIO_F_CONN_STATE,BIO_R_UNABLE_TO_CREATE_SOCKET); 207 BIO_R_UNABLE_TO_CREATE_SOCKET);
219 goto exit_loop; 208 goto exit_loop;
220 } 209 }
221 b->num=ret; 210 b->num = ret;
222 c->state=BIO_CONN_S_NBIO; 211 c->state = BIO_CONN_S_NBIO;
223 break; 212 break;
224 213
225 case BIO_CONN_S_NBIO: 214 case BIO_CONN_S_NBIO:
226 if (c->nbio) 215 if (c->nbio) {
227 { 216 if (!BIO_socket_nbio(b->num, 1)) {
228 if (!BIO_socket_nbio(b->num,1)) 217 BIOerr(BIO_F_CONN_STATE,
229 { 218 BIO_R_ERROR_SETTING_NBIO);
230 BIOerr(BIO_F_CONN_STATE,BIO_R_ERROR_SETTING_NBIO); 219 ERR_add_error_data(4, "host=",
231 ERR_add_error_data(4,"host=", 220 c->param_hostname, ":",
232 c->param_hostname, 221 c->param_port);
233 ":",c->param_port);
234 goto exit_loop; 222 goto exit_loop;
235 }
236 } 223 }
237 c->state=BIO_CONN_S_CONNECT; 224 }
225 c->state = BIO_CONN_S_CONNECT;
238 226
239#if defined(SO_KEEPALIVE) && !defined(OPENSSL_SYS_MPE) 227#if defined(SO_KEEPALIVE) && !defined(OPENSSL_SYS_MPE)
240 i=1; 228 i = 1;
241 i=setsockopt(b->num,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i)); 229 i = setsockopt(b->num, SOL_SOCKET, SO_KEEPALIVE,(char *)&i, sizeof(i));
242 if (i < 0) 230 if (i < 0) {
243 { 231 SYSerr(SYS_F_SOCKET, errno);
244 SYSerr(SYS_F_SOCKET,errno); 232 ERR_add_error_data(4, "host=",
245 ERR_add_error_data(4,"host=",c->param_hostname, 233 c->param_hostname, ":", c->param_port);
246 ":",c->param_port); 234 BIOerr(BIO_F_CONN_STATE, BIO_R_KEEPALIVE);
247 BIOerr(BIO_F_CONN_STATE,BIO_R_KEEPALIVE);
248 goto exit_loop; 235 goto exit_loop;
249 } 236 }
250#endif 237#endif
251 break; 238 break;
252 239
253 case BIO_CONN_S_CONNECT: 240 case BIO_CONN_S_CONNECT:
254 BIO_clear_retry_flags(b); 241 BIO_clear_retry_flags(b);
255 ret=connect(b->num, 242 ret = connect(b->num,
256 (struct sockaddr *)&c->them, 243 (struct sockaddr *)&c->them,
257 sizeof(c->them)); 244 sizeof(c->them));
258 b->retry_reason=0; 245 b->retry_reason = 0;
259 if (ret < 0) 246 if (ret < 0) {
260 { 247 if (BIO_sock_should_retry(ret)) {
261 if (BIO_sock_should_retry(ret))
262 {
263 BIO_set_retry_special(b); 248 BIO_set_retry_special(b);
264 c->state=BIO_CONN_S_BLOCKED_CONNECT; 249 c->state = BIO_CONN_S_BLOCKED_CONNECT;
265 b->retry_reason=BIO_RR_CONNECT; 250 b->retry_reason = BIO_RR_CONNECT;
266 } 251 } else {
267 else 252 SYSerr(SYS_F_CONNECT, errno);
268 { 253 ERR_add_error_data(4, "host=",
269 SYSerr(SYS_F_CONNECT,errno); 254 c->param_hostname, ":",
270 ERR_add_error_data(4,"host=", 255 c->param_port);
271 c->param_hostname, 256 BIOerr(BIO_F_CONN_STATE,
272 ":",c->param_port); 257 BIO_R_CONNECT_ERROR);
273 BIOerr(BIO_F_CONN_STATE,BIO_R_CONNECT_ERROR);
274 }
275 goto exit_loop;
276 } 258 }
277 else 259 goto exit_loop;
278 c->state=BIO_CONN_S_OK; 260 } else
261 c->state = BIO_CONN_S_OK;
279 break; 262 break;
280 263
281 case BIO_CONN_S_BLOCKED_CONNECT: 264 case BIO_CONN_S_BLOCKED_CONNECT:
282 i=BIO_sock_error(b->num); 265 i = BIO_sock_error(b->num);
283 if (i) 266 if (i) {
284 {
285 BIO_clear_retry_flags(b); 267 BIO_clear_retry_flags(b);
286 SYSerr(SYS_F_CONNECT,i); 268 SYSerr(SYS_F_CONNECT, i);
287 ERR_add_error_data(4,"host=", 269 ERR_add_error_data(4, "host=",
288 c->param_hostname, 270 c->param_hostname, ":", c->param_port);
289 ":",c->param_port); 271 BIOerr(BIO_F_CONN_STATE,
290 BIOerr(BIO_F_CONN_STATE,BIO_R_NBIO_CONNECT_ERROR); 272 BIO_R_NBIO_CONNECT_ERROR);
291 ret=0; 273 ret = 0;
292 goto exit_loop; 274 goto exit_loop;
293 } 275 } else
294 else 276 c->state = BIO_CONN_S_OK;
295 c->state=BIO_CONN_S_OK;
296 break; 277 break;
297 278
298 case BIO_CONN_S_OK: 279 case BIO_CONN_S_OK:
299 ret=1; 280 ret = 1;
300 goto exit_loop; 281 goto exit_loop;
301 default: 282 default:
302 /* abort(); */ 283 /* abort(); */
303 goto exit_loop; 284 goto exit_loop;
304 } 285 }
305 286
306 if (cb != NULL) 287 if (cb != NULL) {
307 { 288 if (!(ret = cb((BIO *)b, c->state, ret)))
308 if (!(ret=cb((BIO *)b,c->state,ret)))
309 goto end; 289 goto end;
310 }
311 } 290 }
291 }
312 292
313 /* Loop does not exit */ 293 /* Loop does not exit */
314exit_loop: 294exit_loop:
315 if (cb != NULL) 295 if (cb != NULL)
316 ret=cb((BIO *)b,c->state,ret); 296 ret = cb((BIO *)b, c->state, ret);
317end: 297end:
318 return(ret); 298 return (ret);
319 } 299}
320 300
321BIO_CONNECT *BIO_CONNECT_new(void) 301BIO_CONNECT
322 { 302*BIO_CONNECT_new(void)
303{
323 BIO_CONNECT *ret; 304 BIO_CONNECT *ret;
324 305
325 if ((ret=(BIO_CONNECT *)OPENSSL_malloc(sizeof(BIO_CONNECT))) == NULL) 306 if ((ret = (BIO_CONNECT *)OPENSSL_malloc(sizeof(BIO_CONNECT))) == NULL)
326 return(NULL); 307 return (NULL);
327 ret->state=BIO_CONN_S_BEFORE; 308 ret->state = BIO_CONN_S_BEFORE;
328 ret->param_hostname=NULL; 309 ret->param_hostname = NULL;
329 ret->param_port=NULL; 310 ret->param_port = NULL;
330 ret->info_callback=NULL; 311 ret->info_callback = NULL;
331 ret->nbio=0; 312 ret->nbio = 0;
332 ret->ip[0]=0; 313 ret->ip[0] = 0;
333 ret->ip[1]=0; 314 ret->ip[1] = 0;
334 ret->ip[2]=0; 315 ret->ip[2] = 0;
335 ret->ip[3]=0; 316 ret->ip[3] = 0;
336 ret->port=0; 317 ret->port = 0;
337 memset((char *)&ret->them,0,sizeof(ret->them)); 318 memset((char *)&ret->them, 0, sizeof(ret->them));
338 return(ret); 319 return (ret);
339 } 320}
340 321
341void BIO_CONNECT_free(BIO_CONNECT *a) 322void
342 { 323BIO_CONNECT_free(BIO_CONNECT *a)
343 if(a == NULL) 324{
344 return; 325 if (a == NULL)
326 return;
345 327
346 if (a->param_hostname != NULL) 328 if (a->param_hostname != NULL)
347 OPENSSL_free(a->param_hostname); 329 OPENSSL_free(a->param_hostname);
348 if (a->param_port != NULL) 330 if (a->param_port != NULL)
349 OPENSSL_free(a->param_port); 331 OPENSSL_free(a->param_port);
350 OPENSSL_free(a); 332 OPENSSL_free(a);
351 } 333}
352 334
353BIO_METHOD *BIO_s_connect(void) 335BIO_METHOD
354 { 336*BIO_s_connect(void)
355 return(&methods_connectp); 337{
356 } 338 return (&methods_connectp);
357 339}
358static int conn_new(BIO *bi) 340
359 { 341static int
360 bi->init=0; 342conn_new(BIO *bi)
361 bi->num=-1; 343{
362 bi->flags=0; 344 bi->init = 0;
363 if ((bi->ptr=(char *)BIO_CONNECT_new()) == NULL) 345 bi->num = -1;
364 return(0); 346 bi->flags = 0;
347 if ((bi->ptr = (char *)BIO_CONNECT_new()) == NULL)
348 return (0);
365 else 349 else
366 return(1); 350 return (1);
367 } 351}
368 352
369static void conn_close_socket(BIO *bio) 353static void
370 { 354conn_close_socket(BIO *bio)
355{
371 BIO_CONNECT *c; 356 BIO_CONNECT *c;
372 357
373 c=(BIO_CONNECT *)bio->ptr; 358 c = (BIO_CONNECT *)bio->ptr;
374 if (bio->num != -1) 359 if (bio->num != -1) {
375 {
376 /* Only do a shutdown if things were established */ 360 /* Only do a shutdown if things were established */
377 if (c->state == BIO_CONN_S_OK) 361 if (c->state == BIO_CONN_S_OK)
378 shutdown(bio->num, SHUT_RDWR); 362 shutdown(bio->num, SHUT_RDWR);
379 close(bio->num); 363 close(bio->num);
380 bio->num=-1; 364 bio->num = -1;
381 }
382 } 365 }
366}
383 367
384static int conn_free(BIO *a) 368static int
385 { 369conn_free(BIO *a)
370{
386 BIO_CONNECT *data; 371 BIO_CONNECT *data;
387 372
388 if (a == NULL) return(0); 373 if (a == NULL)
389 data=(BIO_CONNECT *)a->ptr; 374 return (0);
390 375 data = (BIO_CONNECT *)a->ptr;
391 if (a->shutdown) 376
392 { 377 if (a->shutdown) {
393 conn_close_socket(a); 378 conn_close_socket(a);
394 BIO_CONNECT_free(data); 379 BIO_CONNECT_free(data);
395 a->ptr=NULL; 380 a->ptr = NULL;
396 a->flags=0; 381 a->flags = 0;
397 a->init=0; 382 a->init = 0;
398 }
399 return(1);
400 } 383 }
401 384 return (1);
402static int conn_read(BIO *b, char *out, int outl) 385}
403 { 386
404 int ret=0; 387static int
388conn_read(BIO *b, char *out, int outl)
389{
390 int ret = 0;
405 BIO_CONNECT *data; 391 BIO_CONNECT *data;
406 392
407 data=(BIO_CONNECT *)b->ptr; 393 data = (BIO_CONNECT *)b->ptr;
408 if (data->state != BIO_CONN_S_OK) 394 if (data->state != BIO_CONN_S_OK) {
409 { 395 ret = conn_state(b, data);
410 ret=conn_state(b,data);
411 if (ret <= 0) 396 if (ret <= 0)
412 return(ret); 397 return (ret);
413 } 398 }
414 399
415 if (out != NULL) 400 if (out != NULL) {
416 {
417 errno = 0; 401 errno = 0;
418 ret=read(b->num,out,outl); 402 ret = read(b->num, out, outl);
419 BIO_clear_retry_flags(b); 403 BIO_clear_retry_flags(b);
420 if (ret <= 0) 404 if (ret <= 0) {
421 {
422 if (BIO_sock_should_retry(ret)) 405 if (BIO_sock_should_retry(ret))
423 BIO_set_retry_read(b); 406 BIO_set_retry_read(b);
424 }
425 } 407 }
426 return(ret);
427 } 408 }
409 return (ret);
410}
428 411
429static int conn_write(BIO *b, const char *in, int inl) 412static int
430 { 413conn_write(BIO *b, const char *in, int inl)
414{
431 int ret; 415 int ret;
432 BIO_CONNECT *data; 416 BIO_CONNECT *data;
433 417
434 data=(BIO_CONNECT *)b->ptr; 418 data = (BIO_CONNECT *)b->ptr;
435 if (data->state != BIO_CONN_S_OK) 419 if (data->state != BIO_CONN_S_OK) {
436 { 420 ret = conn_state(b, data);
437 ret=conn_state(b,data); 421 if (ret <= 0)
438 if (ret <= 0) return(ret); 422 return (ret);
439 } 423 }
440 424
441 errno = 0; 425 errno = 0;
442 ret=write(b->num,in,inl); 426 ret = write(b->num, in, inl);
443 BIO_clear_retry_flags(b); 427 BIO_clear_retry_flags(b);
444 if (ret <= 0) 428 if (ret <= 0) {
445 {
446 if (BIO_sock_should_retry(ret)) 429 if (BIO_sock_should_retry(ret))
447 BIO_set_retry_write(b); 430 BIO_set_retry_write(b);
448 }
449 return(ret);
450 } 431 }
432 return (ret);
433}
451 434
452static long conn_ctrl(BIO *b, int cmd, long num, void *ptr) 435static long
453 { 436conn_ctrl(BIO *b, int cmd, long num, void *ptr)
437{
454 BIO *dbio; 438 BIO *dbio;
455 int *ip; 439 int *ip;
456 const char **pptr; 440 const char **pptr;
457 long ret=1; 441 long ret = 1;
458 BIO_CONNECT *data; 442 BIO_CONNECT *data;
459 443
460 data=(BIO_CONNECT *)b->ptr; 444 data = (BIO_CONNECT *)b->ptr;
461 445
462 switch (cmd) 446 switch (cmd) {
463 {
464 case BIO_CTRL_RESET: 447 case BIO_CTRL_RESET:
465 ret=0; 448 ret = 0;
466 data->state=BIO_CONN_S_BEFORE; 449 data->state = BIO_CONN_S_BEFORE;
467 conn_close_socket(b); 450 conn_close_socket(b);
468 b->flags=0; 451 b->flags = 0;
469 break; 452 break;
470 case BIO_C_DO_STATE_MACHINE: 453 case BIO_C_DO_STATE_MACHINE:
471 /* use this one to start the connection */ 454 /* use this one to start the connection */
472 if (data->state != BIO_CONN_S_OK) 455 if (data->state != BIO_CONN_S_OK)
473 ret=(long)conn_state(b,data); 456 ret = (long)conn_state(b, data);
474 else 457 else
475 ret=1; 458 ret = 1;
476 break; 459 break;
477 case BIO_C_GET_CONNECT: 460 case BIO_C_GET_CONNECT:
478 if (ptr != NULL) 461 if (ptr != NULL) {
479 { 462 pptr = (const char **)ptr;
480 pptr=(const char **)ptr; 463 if (num == 0) {
481 if (num == 0) 464 *pptr = data->param_hostname;
482 { 465
483 *pptr=data->param_hostname; 466 } else if (num == 1) {
484 467 *pptr = data->param_port;
485 } 468 } else if (num == 2) {
486 else if (num == 1) 469 *pptr = (char *)&(data->ip[0]);
487 { 470 } else if (num == 3) {
488 *pptr=data->param_port; 471 *((int *)ptr) = data->port;
489 }
490 else if (num == 2)
491 {
492 *pptr= (char *)&(data->ip[0]);
493 }
494 else if (num == 3)
495 {
496 *((int *)ptr)=data->port;
497 }
498 if ((!b->init) || (ptr == NULL))
499 *pptr="not initialized";
500 ret=1;
501 } 472 }
473 if ((!b->init) || (ptr == NULL))
474 *pptr = "not initialized";
475 ret = 1;
476 }
502 break; 477 break;
503 case BIO_C_SET_CONNECT: 478 case BIO_C_SET_CONNECT:
504 if (ptr != NULL) 479 if (ptr != NULL) {
505 { 480 b->init = 1;
506 b->init=1; 481 if (num == 0) {
507 if (num == 0)
508 {
509 if (data->param_hostname != NULL) 482 if (data->param_hostname != NULL)
510 OPENSSL_free(data->param_hostname); 483 OPENSSL_free(data->param_hostname);
511 data->param_hostname=BUF_strdup(ptr); 484 data->param_hostname = BUF_strdup(ptr);
512 } 485 } else if (num == 1) {
513 else if (num == 1)
514 {
515 if (data->param_port != NULL) 486 if (data->param_port != NULL)
516 OPENSSL_free(data->param_port); 487 OPENSSL_free(data->param_port);
517 data->param_port=BUF_strdup(ptr); 488 data->param_port = BUF_strdup(ptr);
518 } 489 } else if (num == 2) {
519 else if (num == 2)
520 {
521 char buf[16]; 490 char buf[16];
522 unsigned char *p = ptr; 491 unsigned char *p = ptr;
523 492
524 (void) snprintf(buf,sizeof buf,"%d.%d.%d.%d", 493 (void) snprintf(buf, sizeof buf, "%d.%d.%d.%d",
525 p[0],p[1],p[2],p[3]); 494 p[0], p[1], p[2], p[3]);
526 if (data->param_hostname != NULL) 495 if (data->param_hostname != NULL)
527 OPENSSL_free(data->param_hostname); 496 OPENSSL_free(data->param_hostname);
528 data->param_hostname=BUF_strdup(buf); 497 data->param_hostname = BUF_strdup(buf);
529 memcpy(&(data->ip[0]),ptr,4); 498 memcpy(&(data->ip[0]), ptr, 4);
530 } 499 } else if (num == 3) {
531 else if (num == 3) 500 char buf[DECIMAL_SIZE(int) + 1];
532 {
533 char buf[DECIMAL_SIZE(int)+1];
534 501
535 (void) snprintf(buf,sizeof buf,"%d",*(int *)ptr); 502 (void) snprintf(buf, sizeof buf, "%d",
503 *(int *)ptr);
536 if (data->param_port != NULL) 504 if (data->param_port != NULL)
537 OPENSSL_free(data->param_port); 505 OPENSSL_free(data->param_port);
538 data->param_port=BUF_strdup(buf); 506 data->param_port = BUF_strdup(buf);
539 data->port= *(int *)ptr; 507 data->port= *(int *)ptr;
540 }
541 } 508 }
509 }
542 break; 510 break;
543 case BIO_C_SET_NBIO: 511 case BIO_C_SET_NBIO:
544 data->nbio=(int)num; 512 data->nbio = (int)num;
545 break; 513 break;
546 case BIO_C_GET_FD: 514 case BIO_C_GET_FD:
547 if (b->init) 515 if (b->init) {
548 { 516 ip = (int *)ptr;
549 ip=(int *)ptr;
550 if (ip != NULL) 517 if (ip != NULL)
551 *ip=b->num; 518 *ip = b->num;
552 ret=b->num; 519 ret = b->num;
553 } 520 } else
554 else 521 ret = -1;
555 ret= -1;
556 break; 522 break;
557 case BIO_CTRL_GET_CLOSE: 523 case BIO_CTRL_GET_CLOSE:
558 ret=b->shutdown; 524 ret = b->shutdown;
559 break; 525 break;
560 case BIO_CTRL_SET_CLOSE: 526 case BIO_CTRL_SET_CLOSE:
561 b->shutdown=(int)num; 527 b->shutdown = (int)num;
562 break; 528 break;
563 case BIO_CTRL_PENDING: 529 case BIO_CTRL_PENDING:
564 case BIO_CTRL_WPENDING: 530 case BIO_CTRL_WPENDING:
565 ret=0; 531 ret = 0;
566 break; 532 break;
567 case BIO_CTRL_FLUSH: 533 case BIO_CTRL_FLUSH:
568 break; 534 break;
569 case BIO_CTRL_DUP: 535 case BIO_CTRL_DUP:
570 { 536 {
571 dbio=(BIO *)ptr; 537 dbio = (BIO *)ptr;
572 if (data->param_port) 538 if (data->param_port)
573 BIO_set_conn_port(dbio,data->param_port); 539 BIO_set_conn_port(dbio, data->param_port);
574 if (data->param_hostname) 540 if (data->param_hostname)
575 BIO_set_conn_hostname(dbio,data->param_hostname); 541 BIO_set_conn_hostname(dbio,
576 BIO_set_nbio(dbio,data->nbio); 542 data->param_hostname);
577 /* FIXME: the cast of the function seems unlikely to be a good idea */ 543 BIO_set_nbio(dbio, data->nbio);
578 (void)BIO_set_info_callback(dbio,(bio_info_cb *)data->info_callback); 544 /* FIXME: the cast of the function seems unlikely to be a good idea */
545 (void)BIO_set_info_callback(dbio,
546 (bio_info_cb *)data->info_callback);
579 } 547 }
580 break; 548 break;
581 case BIO_CTRL_SET_CALLBACK: 549 case BIO_CTRL_SET_CALLBACK:
582 { 550 {
583#if 0 /* FIXME: Should this be used? -- Richard Levitte */ 551#if 0 /* FIXME: Should this be used? -- Richard Levitte */
584 BIOerr(BIO_F_CONN_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 552 BIOerr(BIO_F_CONN_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
585 ret = -1; 553 ret = -1;
586#else 554#else
587 ret=0; 555 ret = 0;
588#endif 556#endif
589 } 557 }
590 break; 558 break;
591 case BIO_CTRL_GET_CALLBACK: 559 case BIO_CTRL_GET_CALLBACK:
592 { 560 {
593 int (**fptr)(const BIO *bio,int state,int xret); 561 int (**fptr)(const BIO *bio, int state, int xret);
594 562
595 fptr=(int (**)(const BIO *bio,int state,int xret))ptr; 563 fptr = (int (**)(const BIO *bio, int state, int xret))ptr;
596 *fptr=data->info_callback; 564 *fptr = data->info_callback;
597 } 565 }
598 break; 566 break;
599 default: 567 default:
600 ret=0; 568 ret = 0;
601 break; 569 break;
602 }
603 return(ret);
604 } 570 }
571 return (ret);
572}
605 573
606static long conn_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) 574static long
607 { 575conn_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
608 long ret=1; 576{
577 long ret = 1;
609 BIO_CONNECT *data; 578 BIO_CONNECT *data;
610 579
611 data=(BIO_CONNECT *)b->ptr; 580 data = (BIO_CONNECT *)b->ptr;
612 581
613 switch (cmd) 582 switch (cmd) {
614 {
615 case BIO_CTRL_SET_CALLBACK: 583 case BIO_CTRL_SET_CALLBACK:
616 { 584 {
617 data->info_callback=(int (*)(const struct bio_st *, int, int))fp; 585 data->info_callback = (int (*)(const struct bio_st *, int, int))fp;
618 } 586 }
619 break; 587 break;
620 default: 588 default:
621 ret=0; 589 ret = 0;
622 break; 590 break;
623 }
624 return(ret);
625 } 591 }
626 592 return (ret);
627static int conn_puts(BIO *bp, const char *str) 593}
628 { 594
629 int n,ret; 595static int
630 596conn_puts(BIO *bp, const char *str)
631 n=strlen(str); 597{
632 ret=conn_write(bp,str,n); 598 int n, ret;
633 return(ret); 599
634 } 600 n = strlen(str);
635 601 ret = conn_write(bp, str, n);
636BIO *BIO_new_connect(char *str) 602 return (ret);
637 { 603}
604
605BIO
606*BIO_new_connect(char *str)
607{
638 BIO *ret; 608 BIO *ret;
639 609
640 ret=BIO_new(BIO_s_connect()); 610 ret = BIO_new(BIO_s_connect());
641 if (ret == NULL) return(NULL); 611 if (ret == NULL)
642 if (BIO_set_conn_hostname(ret,str)) 612 return (NULL);
643 return(ret); 613 if (BIO_set_conn_hostname(ret, str))
644 else 614 return (ret);
645 { 615 else {
646 BIO_free(ret); 616 BIO_free(ret);
647 return(NULL); 617 return (NULL);
648 }
649 } 618 }
619}
650 620
651#endif 621#endif
652