155714Skris/* crypto/bio/bss_conn.c */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 8280297Sjkim * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15280297Sjkim * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 22280297Sjkim * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 37280297Sjkim * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40280297Sjkim * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 52280297Sjkim * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 5855714Skris 5955714Skris#include <stdio.h> 6055714Skris#include <errno.h> 6155714Skris#define USE_SOCKETS 6255714Skris#include "cryptlib.h" 6355714Skris#include <openssl/bio.h> 6455714Skris 65160814Ssimon#ifndef OPENSSL_NO_SOCK 66160814Ssimon 67280297Sjkim# ifdef OPENSSL_SYS_WIN16 68280297Sjkim# define SOCKET_PROTOCOL 0 /* more microsoft stupidity */ 69280297Sjkim# else 70280297Sjkim# define SOCKET_PROTOCOL IPPROTO_TCP 71280297Sjkim# endif 7255714Skris 73280297Sjkim# if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000) 7455714Skris/* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */ 75280297Sjkim# undef FIONBIO 76280297Sjkim# endif 7755714Skris 78280297Sjkimtypedef struct bio_connect_st { 79280297Sjkim int state; 80280297Sjkim char *param_hostname; 81280297Sjkim char *param_port; 82280297Sjkim int nbio; 83280297Sjkim unsigned char ip[4]; 84280297Sjkim unsigned short port; 85280297Sjkim struct sockaddr_in them; 86280297Sjkim /* 87280297Sjkim * int socket; this will be kept in bio->num so that it is compatible 88280297Sjkim * with the bss_sock bio 89280297Sjkim */ 90280297Sjkim /* 91280297Sjkim * called when the connection is initially made callback(BIO,state,ret); 92280297Sjkim * The callback should return 'ret'. state is for compatibility with the 93280297Sjkim * ssl info_callback 94280297Sjkim */ 95280297Sjkim int (*info_callback) (const BIO *bio, int state, int ret); 96280297Sjkim} BIO_CONNECT; 9755714Skris 9868651Skrisstatic int conn_write(BIO *h, const char *buf, int num); 9968651Skrisstatic int conn_read(BIO *h, char *buf, int size); 10068651Skrisstatic int conn_puts(BIO *h, const char *str); 10168651Skrisstatic long conn_ctrl(BIO *h, int cmd, long arg1, void *arg2); 10255714Skrisstatic int conn_new(BIO *h); 10355714Skrisstatic int conn_free(BIO *data); 10468651Skrisstatic long conn_callback_ctrl(BIO *h, int cmd, bio_info_cb *); 10555714Skris 10655714Skrisstatic int conn_state(BIO *b, BIO_CONNECT *c); 10755714Skrisstatic void conn_close_socket(BIO *data); 108280297SjkimBIO_CONNECT *BIO_CONNECT_new(void); 10955714Skrisvoid BIO_CONNECT_free(BIO_CONNECT *a); 11055714Skris 111280297Sjkimstatic BIO_METHOD methods_connectp = { 112280297Sjkim BIO_TYPE_CONNECT, 113280297Sjkim "socket connect", 114280297Sjkim conn_write, 115280297Sjkim conn_read, 116280297Sjkim conn_puts, 117280297Sjkim NULL, /* connect_gets, */ 118280297Sjkim conn_ctrl, 119280297Sjkim conn_new, 120280297Sjkim conn_free, 121280297Sjkim conn_callback_ctrl, 122280297Sjkim}; 12355714Skris 12455714Skrisstatic int conn_state(BIO *b, BIO_CONNECT *c) 125280297Sjkim{ 126280297Sjkim int ret = -1, i; 127280297Sjkim unsigned long l; 128280297Sjkim char *p, *q; 129280297Sjkim int (*cb) (const BIO *, int, int) = NULL; 13055714Skris 131280297Sjkim if (c->info_callback != NULL) 132280297Sjkim cb = c->info_callback; 13355714Skris 134280297Sjkim for (;;) { 135280297Sjkim switch (c->state) { 136280297Sjkim case BIO_CONN_S_BEFORE: 137280297Sjkim p = c->param_hostname; 138280297Sjkim if (p == NULL) { 139280297Sjkim BIOerr(BIO_F_CONN_STATE, BIO_R_NO_HOSTNAME_SPECIFIED); 140280297Sjkim goto exit_loop; 141280297Sjkim } 142280297Sjkim for (; *p != '\0'; p++) { 143280297Sjkim if ((*p == ':') || (*p == '/')) 144280297Sjkim break; 145280297Sjkim } 14655714Skris 147280297Sjkim i = *p; 148280297Sjkim if ((i == ':') || (i == '/')) { 14955714Skris 150280297Sjkim *(p++) = '\0'; 151280297Sjkim if (i == ':') { 152280297Sjkim for (q = p; *q; q++) 153280297Sjkim if (*q == '/') { 154280297Sjkim *q = '\0'; 155280297Sjkim break; 156280297Sjkim } 157280297Sjkim if (c->param_port != NULL) 158280297Sjkim OPENSSL_free(c->param_port); 159280297Sjkim c->param_port = BUF_strdup(p); 160280297Sjkim } 161280297Sjkim } 16255714Skris 163280297Sjkim if (c->param_port == NULL) { 164280297Sjkim BIOerr(BIO_F_CONN_STATE, BIO_R_NO_PORT_SPECIFIED); 165280297Sjkim ERR_add_error_data(2, "host=", c->param_hostname); 166280297Sjkim goto exit_loop; 167280297Sjkim } 168280297Sjkim c->state = BIO_CONN_S_GET_IP; 169280297Sjkim break; 17055714Skris 171280297Sjkim case BIO_CONN_S_GET_IP: 172280297Sjkim if (BIO_get_host_ip(c->param_hostname, &(c->ip[0])) <= 0) 173280297Sjkim goto exit_loop; 174280297Sjkim c->state = BIO_CONN_S_GET_PORT; 175280297Sjkim break; 17655714Skris 177280297Sjkim case BIO_CONN_S_GET_PORT: 178280297Sjkim if (c->param_port == NULL) { 179280297Sjkim /* abort(); */ 180280297Sjkim goto exit_loop; 181280297Sjkim } else if (BIO_get_port(c->param_port, &c->port) <= 0) 182280297Sjkim goto exit_loop; 183280297Sjkim c->state = BIO_CONN_S_CREATE_SOCKET; 184280297Sjkim break; 18555714Skris 186280297Sjkim case BIO_CONN_S_CREATE_SOCKET: 187280297Sjkim /* now setup address */ 188280297Sjkim memset((char *)&c->them, 0, sizeof(c->them)); 189280297Sjkim c->them.sin_family = AF_INET; 190280297Sjkim c->them.sin_port = htons((unsigned short)c->port); 191280297Sjkim l = (unsigned long) 192280297Sjkim ((unsigned long)c->ip[0] << 24L) | 193280297Sjkim ((unsigned long)c->ip[1] << 16L) | 194280297Sjkim ((unsigned long)c->ip[2] << 8L) | ((unsigned long)c->ip[3]); 195280297Sjkim c->them.sin_addr.s_addr = htonl(l); 196280297Sjkim c->state = BIO_CONN_S_CREATE_SOCKET; 19755714Skris 198280297Sjkim ret = socket(AF_INET, SOCK_STREAM, SOCKET_PROTOCOL); 199280297Sjkim if (ret == INVALID_SOCKET) { 200280297Sjkim SYSerr(SYS_F_SOCKET, get_last_socket_error()); 201280297Sjkim ERR_add_error_data(4, "host=", c->param_hostname, 202280297Sjkim ":", c->param_port); 203280297Sjkim BIOerr(BIO_F_CONN_STATE, BIO_R_UNABLE_TO_CREATE_SOCKET); 204280297Sjkim goto exit_loop; 205280297Sjkim } 206280297Sjkim b->num = ret; 207280297Sjkim c->state = BIO_CONN_S_NBIO; 208280297Sjkim break; 20955714Skris 210280297Sjkim case BIO_CONN_S_NBIO: 211280297Sjkim if (c->nbio) { 212280297Sjkim if (!BIO_socket_nbio(b->num, 1)) { 213280297Sjkim BIOerr(BIO_F_CONN_STATE, BIO_R_ERROR_SETTING_NBIO); 214280297Sjkim ERR_add_error_data(4, "host=", 215280297Sjkim c->param_hostname, ":", c->param_port); 216280297Sjkim goto exit_loop; 217280297Sjkim } 218280297Sjkim } 219280297Sjkim c->state = BIO_CONN_S_CONNECT; 22055714Skris 221280297Sjkim# if defined(SO_KEEPALIVE) && !defined(OPENSSL_SYS_MPE) 222280297Sjkim i = 1; 223280297Sjkim i = setsockopt(b->num, SOL_SOCKET, SO_KEEPALIVE, (char *)&i, 224280297Sjkim sizeof(i)); 225280297Sjkim if (i < 0) { 226280297Sjkim SYSerr(SYS_F_SOCKET, get_last_socket_error()); 227280297Sjkim ERR_add_error_data(4, "host=", c->param_hostname, 228280297Sjkim ":", c->param_port); 229280297Sjkim BIOerr(BIO_F_CONN_STATE, BIO_R_KEEPALIVE); 230280297Sjkim goto exit_loop; 231280297Sjkim } 232280297Sjkim# endif 233280297Sjkim break; 23455714Skris 235280297Sjkim case BIO_CONN_S_CONNECT: 236280297Sjkim BIO_clear_retry_flags(b); 237280297Sjkim ret = connect(b->num, 238280297Sjkim (struct sockaddr *)&c->them, sizeof(c->them)); 239280297Sjkim b->retry_reason = 0; 240280297Sjkim if (ret < 0) { 241280297Sjkim if (BIO_sock_should_retry(ret)) { 242280297Sjkim BIO_set_retry_special(b); 243280297Sjkim c->state = BIO_CONN_S_BLOCKED_CONNECT; 244280297Sjkim b->retry_reason = BIO_RR_CONNECT; 245280297Sjkim } else { 246280297Sjkim SYSerr(SYS_F_CONNECT, get_last_socket_error()); 247280297Sjkim ERR_add_error_data(4, "host=", 248280297Sjkim c->param_hostname, ":", c->param_port); 249280297Sjkim BIOerr(BIO_F_CONN_STATE, BIO_R_CONNECT_ERROR); 250280297Sjkim } 251280297Sjkim goto exit_loop; 252280297Sjkim } else 253280297Sjkim c->state = BIO_CONN_S_OK; 254280297Sjkim break; 25555714Skris 256280297Sjkim case BIO_CONN_S_BLOCKED_CONNECT: 257280297Sjkim i = BIO_sock_error(b->num); 258280297Sjkim if (i) { 259280297Sjkim BIO_clear_retry_flags(b); 260280297Sjkim SYSerr(SYS_F_CONNECT, i); 261280297Sjkim ERR_add_error_data(4, "host=", 262280297Sjkim c->param_hostname, ":", c->param_port); 263280297Sjkim BIOerr(BIO_F_CONN_STATE, BIO_R_NBIO_CONNECT_ERROR); 264280297Sjkim ret = 0; 265280297Sjkim goto exit_loop; 266280297Sjkim } else 267280297Sjkim c->state = BIO_CONN_S_OK; 268280297Sjkim break; 26955714Skris 270280297Sjkim case BIO_CONN_S_OK: 271280297Sjkim ret = 1; 272280297Sjkim goto exit_loop; 273280297Sjkim default: 274280297Sjkim /* abort(); */ 275280297Sjkim goto exit_loop; 276280297Sjkim } 27755714Skris 278280297Sjkim if (cb != NULL) { 279280297Sjkim if (!(ret = cb((BIO *)b, c->state, ret))) 280280297Sjkim goto end; 281280297Sjkim } 282280297Sjkim } 28355714Skris 284280297Sjkim /* Loop does not exit */ 285280297Sjkim exit_loop: 286280297Sjkim if (cb != NULL) 287280297Sjkim ret = cb((BIO *)b, c->state, ret); 288280297Sjkim end: 289280297Sjkim return (ret); 290280297Sjkim} 29155714Skris 29255714SkrisBIO_CONNECT *BIO_CONNECT_new(void) 293280297Sjkim{ 294280297Sjkim BIO_CONNECT *ret; 29555714Skris 296280297Sjkim if ((ret = (BIO_CONNECT *)OPENSSL_malloc(sizeof(BIO_CONNECT))) == NULL) 297280297Sjkim return (NULL); 298280297Sjkim ret->state = BIO_CONN_S_BEFORE; 299280297Sjkim ret->param_hostname = NULL; 300280297Sjkim ret->param_port = NULL; 301280297Sjkim ret->info_callback = NULL; 302280297Sjkim ret->nbio = 0; 303280297Sjkim ret->ip[0] = 0; 304280297Sjkim ret->ip[1] = 0; 305280297Sjkim ret->ip[2] = 0; 306280297Sjkim ret->ip[3] = 0; 307280297Sjkim ret->port = 0; 308280297Sjkim memset((char *)&ret->them, 0, sizeof(ret->them)); 309280297Sjkim return (ret); 310280297Sjkim} 31155714Skris 31255714Skrisvoid BIO_CONNECT_free(BIO_CONNECT *a) 313280297Sjkim{ 314280297Sjkim if (a == NULL) 315280297Sjkim return; 31655714Skris 317280297Sjkim if (a->param_hostname != NULL) 318280297Sjkim OPENSSL_free(a->param_hostname); 319280297Sjkim if (a->param_port != NULL) 320280297Sjkim OPENSSL_free(a->param_port); 321280297Sjkim OPENSSL_free(a); 322280297Sjkim} 32355714Skris 32455714SkrisBIO_METHOD *BIO_s_connect(void) 325280297Sjkim{ 326280297Sjkim return (&methods_connectp); 327280297Sjkim} 32855714Skris 32955714Skrisstatic int conn_new(BIO *bi) 330280297Sjkim{ 331280297Sjkim bi->init = 0; 332280297Sjkim bi->num = INVALID_SOCKET; 333280297Sjkim bi->flags = 0; 334280297Sjkim if ((bi->ptr = (char *)BIO_CONNECT_new()) == NULL) 335280297Sjkim return (0); 336280297Sjkim else 337280297Sjkim return (1); 338280297Sjkim} 33955714Skris 34055714Skrisstatic void conn_close_socket(BIO *bio) 341280297Sjkim{ 342280297Sjkim BIO_CONNECT *c; 34355714Skris 344280297Sjkim c = (BIO_CONNECT *)bio->ptr; 345280297Sjkim if (bio->num != INVALID_SOCKET) { 346280297Sjkim /* Only do a shutdown if things were established */ 347280297Sjkim if (c->state == BIO_CONN_S_OK) 348280297Sjkim shutdown(bio->num, 2); 349280297Sjkim closesocket(bio->num); 350280297Sjkim bio->num = INVALID_SOCKET; 351280297Sjkim } 352280297Sjkim} 35355714Skris 35455714Skrisstatic int conn_free(BIO *a) 355280297Sjkim{ 356280297Sjkim BIO_CONNECT *data; 35755714Skris 358280297Sjkim if (a == NULL) 359280297Sjkim return (0); 360280297Sjkim data = (BIO_CONNECT *)a->ptr; 361280297Sjkim 362280297Sjkim if (a->shutdown) { 363280297Sjkim conn_close_socket(a); 364280297Sjkim BIO_CONNECT_free(data); 365280297Sjkim a->ptr = NULL; 366280297Sjkim a->flags = 0; 367280297Sjkim a->init = 0; 368280297Sjkim } 369280297Sjkim return (1); 370280297Sjkim} 371280297Sjkim 37255714Skrisstatic int conn_read(BIO *b, char *out, int outl) 373280297Sjkim{ 374280297Sjkim int ret = 0; 375280297Sjkim BIO_CONNECT *data; 37655714Skris 377280297Sjkim data = (BIO_CONNECT *)b->ptr; 378280297Sjkim if (data->state != BIO_CONN_S_OK) { 379280297Sjkim ret = conn_state(b, data); 380280297Sjkim if (ret <= 0) 381280297Sjkim return (ret); 382280297Sjkim } 38355714Skris 384280297Sjkim if (out != NULL) { 385280297Sjkim clear_socket_error(); 386280297Sjkim ret = readsocket(b->num, out, outl); 387280297Sjkim BIO_clear_retry_flags(b); 388280297Sjkim if (ret <= 0) { 389280297Sjkim if (BIO_sock_should_retry(ret)) 390280297Sjkim BIO_set_retry_read(b); 391280297Sjkim } 392280297Sjkim } 393280297Sjkim return (ret); 394280297Sjkim} 39555714Skris 39668651Skrisstatic int conn_write(BIO *b, const char *in, int inl) 397280297Sjkim{ 398280297Sjkim int ret; 399280297Sjkim BIO_CONNECT *data; 40055714Skris 401280297Sjkim data = (BIO_CONNECT *)b->ptr; 402280297Sjkim if (data->state != BIO_CONN_S_OK) { 403280297Sjkim ret = conn_state(b, data); 404280297Sjkim if (ret <= 0) 405280297Sjkim return (ret); 406280297Sjkim } 40755714Skris 408280297Sjkim clear_socket_error(); 409280297Sjkim ret = writesocket(b->num, in, inl); 410280297Sjkim BIO_clear_retry_flags(b); 411280297Sjkim if (ret <= 0) { 412280297Sjkim if (BIO_sock_should_retry(ret)) 413280297Sjkim BIO_set_retry_write(b); 414280297Sjkim } 415280297Sjkim return (ret); 416280297Sjkim} 41755714Skris 41868651Skrisstatic long conn_ctrl(BIO *b, int cmd, long num, void *ptr) 419280297Sjkim{ 420280297Sjkim BIO *dbio; 421280297Sjkim int *ip; 422295009Sjkim const char **pptr = NULL; 423280297Sjkim long ret = 1; 424280297Sjkim BIO_CONNECT *data; 42555714Skris 426280297Sjkim data = (BIO_CONNECT *)b->ptr; 42755714Skris 428280297Sjkim switch (cmd) { 429280297Sjkim case BIO_CTRL_RESET: 430280297Sjkim ret = 0; 431280297Sjkim data->state = BIO_CONN_S_BEFORE; 432280297Sjkim conn_close_socket(b); 433280297Sjkim b->flags = 0; 434280297Sjkim break; 435280297Sjkim case BIO_C_DO_STATE_MACHINE: 436280297Sjkim /* use this one to start the connection */ 437280297Sjkim if (data->state != BIO_CONN_S_OK) 438280297Sjkim ret = (long)conn_state(b, data); 439280297Sjkim else 440280297Sjkim ret = 1; 441280297Sjkim break; 442280297Sjkim case BIO_C_GET_CONNECT: 443280297Sjkim if (ptr != NULL) { 444280297Sjkim pptr = (const char **)ptr; 445295009Sjkim } 44655714Skris 447295009Sjkim if (b->init) { 448295009Sjkim if (pptr != NULL) { 449295009Sjkim ret = 1; 450295009Sjkim if (num == 0) { 451295009Sjkim *pptr = data->param_hostname; 452295009Sjkim } else if (num == 1) { 453295009Sjkim *pptr = data->param_port; 454295009Sjkim } else if (num == 2) { 455295009Sjkim *pptr = (char *)&(data->ip[0]); 456295009Sjkim } else { 457295009Sjkim ret = 0; 458295009Sjkim } 459280297Sjkim } 460295009Sjkim if (num == 3) { 461295009Sjkim ret = data->port; 462295009Sjkim } 463295009Sjkim } else { 464295009Sjkim if (pptr != NULL) 465280297Sjkim *pptr = "not initialized"; 466295009Sjkim ret = 0; 467280297Sjkim } 468280297Sjkim break; 469280297Sjkim case BIO_C_SET_CONNECT: 470280297Sjkim if (ptr != NULL) { 471280297Sjkim b->init = 1; 472280297Sjkim if (num == 0) { 473280297Sjkim if (data->param_hostname != NULL) 474280297Sjkim OPENSSL_free(data->param_hostname); 475280297Sjkim data->param_hostname = BUF_strdup(ptr); 476280297Sjkim } else if (num == 1) { 477280297Sjkim if (data->param_port != NULL) 478280297Sjkim OPENSSL_free(data->param_port); 479280297Sjkim data->param_port = BUF_strdup(ptr); 480280297Sjkim } else if (num == 2) { 481280297Sjkim char buf[16]; 482280297Sjkim unsigned char *p = ptr; 48355714Skris 484331638Sjkim BIO_snprintf(buf, sizeof(buf), "%d.%d.%d.%d", 485280297Sjkim p[0], p[1], p[2], p[3]); 486280297Sjkim if (data->param_hostname != NULL) 487280297Sjkim OPENSSL_free(data->param_hostname); 488280297Sjkim data->param_hostname = BUF_strdup(buf); 489280297Sjkim memcpy(&(data->ip[0]), ptr, 4); 490280297Sjkim } else if (num == 3) { 491280297Sjkim char buf[DECIMAL_SIZE(int) + 1]; 49255714Skris 493331638Sjkim BIO_snprintf(buf, sizeof(buf), "%d", *(int *)ptr); 494280297Sjkim if (data->param_port != NULL) 495280297Sjkim OPENSSL_free(data->param_port); 496280297Sjkim data->param_port = BUF_strdup(buf); 497280297Sjkim data->port = *(int *)ptr; 498280297Sjkim } 499280297Sjkim } 500280297Sjkim break; 501280297Sjkim case BIO_C_SET_NBIO: 502280297Sjkim data->nbio = (int)num; 503280297Sjkim break; 504280297Sjkim case BIO_C_GET_FD: 505280297Sjkim if (b->init) { 506280297Sjkim ip = (int *)ptr; 507280297Sjkim if (ip != NULL) 508280297Sjkim *ip = b->num; 509280297Sjkim ret = b->num; 510280297Sjkim } else 511280297Sjkim ret = -1; 512280297Sjkim break; 513280297Sjkim case BIO_CTRL_GET_CLOSE: 514280297Sjkim ret = b->shutdown; 515280297Sjkim break; 516280297Sjkim case BIO_CTRL_SET_CLOSE: 517280297Sjkim b->shutdown = (int)num; 518280297Sjkim break; 519280297Sjkim case BIO_CTRL_PENDING: 520280297Sjkim case BIO_CTRL_WPENDING: 521280297Sjkim ret = 0; 522280297Sjkim break; 523280297Sjkim case BIO_CTRL_FLUSH: 524280297Sjkim break; 525280297Sjkim case BIO_CTRL_DUP: 526280297Sjkim { 527280297Sjkim dbio = (BIO *)ptr; 528280297Sjkim if (data->param_port) 529280297Sjkim BIO_set_conn_port(dbio, data->param_port); 530280297Sjkim if (data->param_hostname) 531280297Sjkim BIO_set_conn_hostname(dbio, data->param_hostname); 532280297Sjkim BIO_set_nbio(dbio, data->nbio); 533280297Sjkim /* 534280297Sjkim * FIXME: the cast of the function seems unlikely to be a good 535280297Sjkim * idea 536280297Sjkim */ 537280297Sjkim (void)BIO_set_info_callback(dbio, 538280297Sjkim (bio_info_cb *)data->info_callback); 539280297Sjkim } 540280297Sjkim break; 541280297Sjkim case BIO_CTRL_SET_CALLBACK: 542280297Sjkim { 543280297Sjkim# if 0 /* FIXME: Should this be used? -- Richard 544280297Sjkim * Levitte */ 545280297Sjkim BIOerr(BIO_F_CONN_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 546280297Sjkim ret = -1; 547280297Sjkim# else 548280297Sjkim ret = 0; 549280297Sjkim# endif 550280297Sjkim } 551280297Sjkim break; 552280297Sjkim case BIO_CTRL_GET_CALLBACK: 553280297Sjkim { 554280297Sjkim int (**fptr) (const BIO *bio, int state, int xret); 55555714Skris 556280297Sjkim fptr = (int (**)(const BIO *bio, int state, int xret))ptr; 557280297Sjkim *fptr = data->info_callback; 558280297Sjkim } 559280297Sjkim break; 560280297Sjkim default: 561280297Sjkim ret = 0; 562280297Sjkim break; 563280297Sjkim } 564280297Sjkim return (ret); 565280297Sjkim} 56655714Skris 56768651Skrisstatic long conn_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) 568280297Sjkim{ 569280297Sjkim long ret = 1; 570280297Sjkim BIO_CONNECT *data; 57159191Skris 572280297Sjkim data = (BIO_CONNECT *)b->ptr; 57359191Skris 574280297Sjkim switch (cmd) { 575280297Sjkim case BIO_CTRL_SET_CALLBACK: 576280297Sjkim { 577280297Sjkim data->info_callback = 578280297Sjkim (int (*)(const struct bio_st *, int, int))fp; 579280297Sjkim } 580280297Sjkim break; 581280297Sjkim default: 582280297Sjkim ret = 0; 583280297Sjkim break; 584280297Sjkim } 585280297Sjkim return (ret); 586280297Sjkim} 58759191Skris 58868651Skrisstatic int conn_puts(BIO *bp, const char *str) 589280297Sjkim{ 590280297Sjkim int n, ret; 59155714Skris 592280297Sjkim n = strlen(str); 593280297Sjkim ret = conn_write(bp, str, n); 594280297Sjkim return (ret); 595280297Sjkim} 59655714Skris 597290207SjkimBIO *BIO_new_connect(const char *str) 598280297Sjkim{ 599280297Sjkim BIO *ret; 60055714Skris 601280297Sjkim ret = BIO_new(BIO_s_connect()); 602280297Sjkim if (ret == NULL) 603280297Sjkim return (NULL); 604280297Sjkim if (BIO_set_conn_hostname(ret, str)) 605280297Sjkim return (ret); 606280297Sjkim else { 607280297Sjkim BIO_free(ret); 608280297Sjkim return (NULL); 609280297Sjkim } 610280297Sjkim} 61155714Skris 61255714Skris#endif 613