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.
8280304Sjkim *
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).
15280304Sjkim *
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.
22280304Sjkim *
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 :-).
37280304Sjkim * 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)"
40280304Sjkim *
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.
52280304Sjkim *
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
67280304Sjkim# ifdef OPENSSL_SYS_WIN16
68280304Sjkim#  define SOCKET_PROTOCOL 0     /* more microsoft stupidity */
69280304Sjkim# else
70280304Sjkim#  define SOCKET_PROTOCOL IPPROTO_TCP
71280304Sjkim# endif
7255714Skris
73280304Sjkim# 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 */
75280304Sjkim#  undef FIONBIO
76280304Sjkim# endif
7755714Skris
78280304Sjkimtypedef struct bio_connect_st {
79280304Sjkim    int state;
80280304Sjkim    char *param_hostname;
81280304Sjkim    char *param_port;
82280304Sjkim    int nbio;
83280304Sjkim    unsigned char ip[4];
84280304Sjkim    unsigned short port;
85280304Sjkim    struct sockaddr_in them;
86280304Sjkim    /*
87280304Sjkim     * int socket; this will be kept in bio->num so that it is compatible
88280304Sjkim     * with the bss_sock bio
89280304Sjkim     */
90280304Sjkim    /*
91280304Sjkim     * called when the connection is initially made callback(BIO,state,ret);
92280304Sjkim     * The callback should return 'ret'.  state is for compatibility with the
93280304Sjkim     * ssl info_callback
94280304Sjkim     */
95280304Sjkim    int (*info_callback) (const BIO *bio, int state, int ret);
96280304Sjkim} 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);
108280304SjkimBIO_CONNECT *BIO_CONNECT_new(void);
10955714Skrisvoid BIO_CONNECT_free(BIO_CONNECT *a);
11055714Skris
111280304Sjkimstatic BIO_METHOD methods_connectp = {
112280304Sjkim    BIO_TYPE_CONNECT,
113280304Sjkim    "socket connect",
114280304Sjkim    conn_write,
115280304Sjkim    conn_read,
116280304Sjkim    conn_puts,
117280304Sjkim    NULL,                       /* connect_gets, */
118280304Sjkim    conn_ctrl,
119280304Sjkim    conn_new,
120280304Sjkim    conn_free,
121280304Sjkim    conn_callback_ctrl,
122280304Sjkim};
12355714Skris
12455714Skrisstatic int conn_state(BIO *b, BIO_CONNECT *c)
125280304Sjkim{
126280304Sjkim    int ret = -1, i;
127280304Sjkim    unsigned long l;
128280304Sjkim    char *p, *q;
129280304Sjkim    int (*cb) (const BIO *, int, int) = NULL;
13055714Skris
131280304Sjkim    if (c->info_callback != NULL)
132280304Sjkim        cb = c->info_callback;
13355714Skris
134280304Sjkim    for (;;) {
135280304Sjkim        switch (c->state) {
136280304Sjkim        case BIO_CONN_S_BEFORE:
137280304Sjkim            p = c->param_hostname;
138280304Sjkim            if (p == NULL) {
139280304Sjkim                BIOerr(BIO_F_CONN_STATE, BIO_R_NO_HOSTNAME_SPECIFIED);
140280304Sjkim                goto exit_loop;
141280304Sjkim            }
142280304Sjkim            for (; *p != '\0'; p++) {
143280304Sjkim                if ((*p == ':') || (*p == '/'))
144280304Sjkim                    break;
145280304Sjkim            }
14655714Skris
147280304Sjkim            i = *p;
148280304Sjkim            if ((i == ':') || (i == '/')) {
14955714Skris
150280304Sjkim                *(p++) = '\0';
151280304Sjkim                if (i == ':') {
152280304Sjkim                    for (q = p; *q; q++)
153280304Sjkim                        if (*q == '/') {
154280304Sjkim                            *q = '\0';
155280304Sjkim                            break;
156280304Sjkim                        }
157280304Sjkim                    if (c->param_port != NULL)
158280304Sjkim                        OPENSSL_free(c->param_port);
159280304Sjkim                    c->param_port = BUF_strdup(p);
160280304Sjkim                }
161280304Sjkim            }
16255714Skris
163280304Sjkim            if (c->param_port == NULL) {
164280304Sjkim                BIOerr(BIO_F_CONN_STATE, BIO_R_NO_PORT_SPECIFIED);
165280304Sjkim                ERR_add_error_data(2, "host=", c->param_hostname);
166280304Sjkim                goto exit_loop;
167280304Sjkim            }
168280304Sjkim            c->state = BIO_CONN_S_GET_IP;
169280304Sjkim            break;
17055714Skris
171280304Sjkim        case BIO_CONN_S_GET_IP:
172280304Sjkim            if (BIO_get_host_ip(c->param_hostname, &(c->ip[0])) <= 0)
173280304Sjkim                goto exit_loop;
174280304Sjkim            c->state = BIO_CONN_S_GET_PORT;
175280304Sjkim            break;
17655714Skris
177280304Sjkim        case BIO_CONN_S_GET_PORT:
178280304Sjkim            if (c->param_port == NULL) {
179280304Sjkim                /* abort(); */
180280304Sjkim                goto exit_loop;
181280304Sjkim            } else if (BIO_get_port(c->param_port, &c->port) <= 0)
182280304Sjkim                goto exit_loop;
183280304Sjkim            c->state = BIO_CONN_S_CREATE_SOCKET;
184280304Sjkim            break;
18555714Skris
186280304Sjkim        case BIO_CONN_S_CREATE_SOCKET:
187280304Sjkim            /* now setup address */
188280304Sjkim            memset((char *)&c->them, 0, sizeof(c->them));
189280304Sjkim            c->them.sin_family = AF_INET;
190280304Sjkim            c->them.sin_port = htons((unsigned short)c->port);
191280304Sjkim            l = (unsigned long)
192280304Sjkim                ((unsigned long)c->ip[0] << 24L) |
193280304Sjkim                ((unsigned long)c->ip[1] << 16L) |
194280304Sjkim                ((unsigned long)c->ip[2] << 8L) | ((unsigned long)c->ip[3]);
195280304Sjkim            c->them.sin_addr.s_addr = htonl(l);
196280304Sjkim            c->state = BIO_CONN_S_CREATE_SOCKET;
19755714Skris
198280304Sjkim            ret = socket(AF_INET, SOCK_STREAM, SOCKET_PROTOCOL);
199280304Sjkim            if (ret == INVALID_SOCKET) {
200280304Sjkim                SYSerr(SYS_F_SOCKET, get_last_socket_error());
201280304Sjkim                ERR_add_error_data(4, "host=", c->param_hostname,
202280304Sjkim                                   ":", c->param_port);
203280304Sjkim                BIOerr(BIO_F_CONN_STATE, BIO_R_UNABLE_TO_CREATE_SOCKET);
204280304Sjkim                goto exit_loop;
205280304Sjkim            }
206280304Sjkim            b->num = ret;
207280304Sjkim            c->state = BIO_CONN_S_NBIO;
208280304Sjkim            break;
20955714Skris
210280304Sjkim        case BIO_CONN_S_NBIO:
211280304Sjkim            if (c->nbio) {
212280304Sjkim                if (!BIO_socket_nbio(b->num, 1)) {
213280304Sjkim                    BIOerr(BIO_F_CONN_STATE, BIO_R_ERROR_SETTING_NBIO);
214280304Sjkim                    ERR_add_error_data(4, "host=",
215280304Sjkim                                       c->param_hostname, ":", c->param_port);
216280304Sjkim                    goto exit_loop;
217280304Sjkim                }
218280304Sjkim            }
219280304Sjkim            c->state = BIO_CONN_S_CONNECT;
22055714Skris
221280304Sjkim# if defined(SO_KEEPALIVE) && !defined(OPENSSL_SYS_MPE)
222280304Sjkim            i = 1;
223280304Sjkim            i = setsockopt(b->num, SOL_SOCKET, SO_KEEPALIVE, (char *)&i,
224280304Sjkim                           sizeof(i));
225280304Sjkim            if (i < 0) {
226280304Sjkim                SYSerr(SYS_F_SOCKET, get_last_socket_error());
227280304Sjkim                ERR_add_error_data(4, "host=", c->param_hostname,
228280304Sjkim                                   ":", c->param_port);
229280304Sjkim                BIOerr(BIO_F_CONN_STATE, BIO_R_KEEPALIVE);
230280304Sjkim                goto exit_loop;
231280304Sjkim            }
232280304Sjkim# endif
233280304Sjkim            break;
23455714Skris
235280304Sjkim        case BIO_CONN_S_CONNECT:
236280304Sjkim            BIO_clear_retry_flags(b);
237280304Sjkim            ret = connect(b->num,
238280304Sjkim                          (struct sockaddr *)&c->them, sizeof(c->them));
239280304Sjkim            b->retry_reason = 0;
240280304Sjkim            if (ret < 0) {
241280304Sjkim                if (BIO_sock_should_retry(ret)) {
242280304Sjkim                    BIO_set_retry_special(b);
243280304Sjkim                    c->state = BIO_CONN_S_BLOCKED_CONNECT;
244280304Sjkim                    b->retry_reason = BIO_RR_CONNECT;
245280304Sjkim                } else {
246280304Sjkim                    SYSerr(SYS_F_CONNECT, get_last_socket_error());
247280304Sjkim                    ERR_add_error_data(4, "host=",
248280304Sjkim                                       c->param_hostname, ":", c->param_port);
249280304Sjkim                    BIOerr(BIO_F_CONN_STATE, BIO_R_CONNECT_ERROR);
250280304Sjkim                }
251280304Sjkim                goto exit_loop;
252280304Sjkim            } else
253280304Sjkim                c->state = BIO_CONN_S_OK;
254280304Sjkim            break;
25555714Skris
256280304Sjkim        case BIO_CONN_S_BLOCKED_CONNECT:
257280304Sjkim            i = BIO_sock_error(b->num);
258280304Sjkim            if (i) {
259280304Sjkim                BIO_clear_retry_flags(b);
260280304Sjkim                SYSerr(SYS_F_CONNECT, i);
261280304Sjkim                ERR_add_error_data(4, "host=",
262280304Sjkim                                   c->param_hostname, ":", c->param_port);
263280304Sjkim                BIOerr(BIO_F_CONN_STATE, BIO_R_NBIO_CONNECT_ERROR);
264280304Sjkim                ret = 0;
265280304Sjkim                goto exit_loop;
266280304Sjkim            } else
267280304Sjkim                c->state = BIO_CONN_S_OK;
268280304Sjkim            break;
26955714Skris
270280304Sjkim        case BIO_CONN_S_OK:
271280304Sjkim            ret = 1;
272280304Sjkim            goto exit_loop;
273280304Sjkim        default:
274280304Sjkim            /* abort(); */
275280304Sjkim            goto exit_loop;
276280304Sjkim        }
27755714Skris
278280304Sjkim        if (cb != NULL) {
279280304Sjkim            if (!(ret = cb((BIO *)b, c->state, ret)))
280280304Sjkim                goto end;
281280304Sjkim        }
282280304Sjkim    }
28355714Skris
284280304Sjkim    /* Loop does not exit */
285280304Sjkim exit_loop:
286280304Sjkim    if (cb != NULL)
287280304Sjkim        ret = cb((BIO *)b, c->state, ret);
288280304Sjkim end:
289280304Sjkim    return (ret);
290280304Sjkim}
29155714Skris
29255714SkrisBIO_CONNECT *BIO_CONNECT_new(void)
293280304Sjkim{
294280304Sjkim    BIO_CONNECT *ret;
29555714Skris
296280304Sjkim    if ((ret = (BIO_CONNECT *)OPENSSL_malloc(sizeof(BIO_CONNECT))) == NULL)
297280304Sjkim        return (NULL);
298280304Sjkim    ret->state = BIO_CONN_S_BEFORE;
299280304Sjkim    ret->param_hostname = NULL;
300280304Sjkim    ret->param_port = NULL;
301280304Sjkim    ret->info_callback = NULL;
302280304Sjkim    ret->nbio = 0;
303280304Sjkim    ret->ip[0] = 0;
304280304Sjkim    ret->ip[1] = 0;
305280304Sjkim    ret->ip[2] = 0;
306280304Sjkim    ret->ip[3] = 0;
307280304Sjkim    ret->port = 0;
308280304Sjkim    memset((char *)&ret->them, 0, sizeof(ret->them));
309280304Sjkim    return (ret);
310280304Sjkim}
31155714Skris
31255714Skrisvoid BIO_CONNECT_free(BIO_CONNECT *a)
313280304Sjkim{
314280304Sjkim    if (a == NULL)
315280304Sjkim        return;
31655714Skris
317280304Sjkim    if (a->param_hostname != NULL)
318280304Sjkim        OPENSSL_free(a->param_hostname);
319280304Sjkim    if (a->param_port != NULL)
320280304Sjkim        OPENSSL_free(a->param_port);
321280304Sjkim    OPENSSL_free(a);
322280304Sjkim}
32355714Skris
32455714SkrisBIO_METHOD *BIO_s_connect(void)
325280304Sjkim{
326280304Sjkim    return (&methods_connectp);
327280304Sjkim}
32855714Skris
32955714Skrisstatic int conn_new(BIO *bi)
330280304Sjkim{
331280304Sjkim    bi->init = 0;
332280304Sjkim    bi->num = INVALID_SOCKET;
333280304Sjkim    bi->flags = 0;
334280304Sjkim    if ((bi->ptr = (char *)BIO_CONNECT_new()) == NULL)
335280304Sjkim        return (0);
336280304Sjkim    else
337280304Sjkim        return (1);
338280304Sjkim}
33955714Skris
34055714Skrisstatic void conn_close_socket(BIO *bio)
341280304Sjkim{
342280304Sjkim    BIO_CONNECT *c;
34355714Skris
344280304Sjkim    c = (BIO_CONNECT *)bio->ptr;
345280304Sjkim    if (bio->num != INVALID_SOCKET) {
346280304Sjkim        /* Only do a shutdown if things were established */
347280304Sjkim        if (c->state == BIO_CONN_S_OK)
348280304Sjkim            shutdown(bio->num, 2);
349280304Sjkim        closesocket(bio->num);
350280304Sjkim        bio->num = INVALID_SOCKET;
351280304Sjkim    }
352280304Sjkim}
35355714Skris
35455714Skrisstatic int conn_free(BIO *a)
355280304Sjkim{
356280304Sjkim    BIO_CONNECT *data;
35755714Skris
358280304Sjkim    if (a == NULL)
359280304Sjkim        return (0);
360280304Sjkim    data = (BIO_CONNECT *)a->ptr;
361280304Sjkim
362280304Sjkim    if (a->shutdown) {
363280304Sjkim        conn_close_socket(a);
364280304Sjkim        BIO_CONNECT_free(data);
365280304Sjkim        a->ptr = NULL;
366280304Sjkim        a->flags = 0;
367280304Sjkim        a->init = 0;
368280304Sjkim    }
369280304Sjkim    return (1);
370280304Sjkim}
371280304Sjkim
37255714Skrisstatic int conn_read(BIO *b, char *out, int outl)
373280304Sjkim{
374280304Sjkim    int ret = 0;
375280304Sjkim    BIO_CONNECT *data;
37655714Skris
377280304Sjkim    data = (BIO_CONNECT *)b->ptr;
378280304Sjkim    if (data->state != BIO_CONN_S_OK) {
379280304Sjkim        ret = conn_state(b, data);
380280304Sjkim        if (ret <= 0)
381280304Sjkim            return (ret);
382280304Sjkim    }
38355714Skris
384280304Sjkim    if (out != NULL) {
385280304Sjkim        clear_socket_error();
386280304Sjkim        ret = readsocket(b->num, out, outl);
387280304Sjkim        BIO_clear_retry_flags(b);
388280304Sjkim        if (ret <= 0) {
389280304Sjkim            if (BIO_sock_should_retry(ret))
390280304Sjkim                BIO_set_retry_read(b);
391280304Sjkim        }
392280304Sjkim    }
393280304Sjkim    return (ret);
394280304Sjkim}
39555714Skris
39668651Skrisstatic int conn_write(BIO *b, const char *in, int inl)
397280304Sjkim{
398280304Sjkim    int ret;
399280304Sjkim    BIO_CONNECT *data;
40055714Skris
401280304Sjkim    data = (BIO_CONNECT *)b->ptr;
402280304Sjkim    if (data->state != BIO_CONN_S_OK) {
403280304Sjkim        ret = conn_state(b, data);
404280304Sjkim        if (ret <= 0)
405280304Sjkim            return (ret);
406280304Sjkim    }
40755714Skris
408280304Sjkim    clear_socket_error();
409280304Sjkim    ret = writesocket(b->num, in, inl);
410280304Sjkim    BIO_clear_retry_flags(b);
411280304Sjkim    if (ret <= 0) {
412280304Sjkim        if (BIO_sock_should_retry(ret))
413280304Sjkim            BIO_set_retry_write(b);
414280304Sjkim    }
415280304Sjkim    return (ret);
416280304Sjkim}
41755714Skris
41868651Skrisstatic long conn_ctrl(BIO *b, int cmd, long num, void *ptr)
419280304Sjkim{
420280304Sjkim    BIO *dbio;
421280304Sjkim    int *ip;
422295016Sjkim    const char **pptr = NULL;
423280304Sjkim    long ret = 1;
424280304Sjkim    BIO_CONNECT *data;
42555714Skris
426280304Sjkim    data = (BIO_CONNECT *)b->ptr;
42755714Skris
428280304Sjkim    switch (cmd) {
429280304Sjkim    case BIO_CTRL_RESET:
430280304Sjkim        ret = 0;
431280304Sjkim        data->state = BIO_CONN_S_BEFORE;
432280304Sjkim        conn_close_socket(b);
433280304Sjkim        b->flags = 0;
434280304Sjkim        break;
435280304Sjkim    case BIO_C_DO_STATE_MACHINE:
436280304Sjkim        /* use this one to start the connection */
437280304Sjkim        if (data->state != BIO_CONN_S_OK)
438280304Sjkim            ret = (long)conn_state(b, data);
439280304Sjkim        else
440280304Sjkim            ret = 1;
441280304Sjkim        break;
442280304Sjkim    case BIO_C_GET_CONNECT:
443280304Sjkim        if (ptr != NULL) {
444280304Sjkim            pptr = (const char **)ptr;
445295016Sjkim        }
44655714Skris
447295016Sjkim        if (b->init) {
448295016Sjkim            if (pptr != NULL) {
449295016Sjkim                ret = 1;
450295016Sjkim                if (num == 0) {
451295016Sjkim                    *pptr = data->param_hostname;
452295016Sjkim                } else if (num == 1) {
453295016Sjkim                    *pptr = data->param_port;
454295016Sjkim                } else if (num == 2) {
455295016Sjkim                    *pptr = (char *)&(data->ip[0]);
456295016Sjkim                } else {
457295016Sjkim                    ret = 0;
458295016Sjkim                }
459280304Sjkim            }
460295016Sjkim            if (num == 3) {
461295016Sjkim                ret = data->port;
462295016Sjkim            }
463295016Sjkim        } else {
464295016Sjkim            if (pptr != NULL)
465280304Sjkim                *pptr = "not initialized";
466295016Sjkim            ret = 0;
467280304Sjkim        }
468280304Sjkim        break;
469280304Sjkim    case BIO_C_SET_CONNECT:
470280304Sjkim        if (ptr != NULL) {
471280304Sjkim            b->init = 1;
472280304Sjkim            if (num == 0) {
473280304Sjkim                if (data->param_hostname != NULL)
474280304Sjkim                    OPENSSL_free(data->param_hostname);
475280304Sjkim                data->param_hostname = BUF_strdup(ptr);
476280304Sjkim            } else if (num == 1) {
477280304Sjkim                if (data->param_port != NULL)
478280304Sjkim                    OPENSSL_free(data->param_port);
479280304Sjkim                data->param_port = BUF_strdup(ptr);
480280304Sjkim            } else if (num == 2) {
481280304Sjkim                char buf[16];
482280304Sjkim                unsigned char *p = ptr;
48355714Skris
484280304Sjkim                BIO_snprintf(buf, sizeof buf, "%d.%d.%d.%d",
485280304Sjkim                             p[0], p[1], p[2], p[3]);
486280304Sjkim                if (data->param_hostname != NULL)
487280304Sjkim                    OPENSSL_free(data->param_hostname);
488280304Sjkim                data->param_hostname = BUF_strdup(buf);
489280304Sjkim                memcpy(&(data->ip[0]), ptr, 4);
490280304Sjkim            } else if (num == 3) {
491280304Sjkim                char buf[DECIMAL_SIZE(int) + 1];
49255714Skris
493280304Sjkim                BIO_snprintf(buf, sizeof buf, "%d", *(int *)ptr);
494280304Sjkim                if (data->param_port != NULL)
495280304Sjkim                    OPENSSL_free(data->param_port);
496280304Sjkim                data->param_port = BUF_strdup(buf);
497280304Sjkim                data->port = *(int *)ptr;
498280304Sjkim            }
499280304Sjkim        }
500280304Sjkim        break;
501280304Sjkim    case BIO_C_SET_NBIO:
502280304Sjkim        data->nbio = (int)num;
503280304Sjkim        break;
504280304Sjkim    case BIO_C_GET_FD:
505280304Sjkim        if (b->init) {
506280304Sjkim            ip = (int *)ptr;
507280304Sjkim            if (ip != NULL)
508280304Sjkim                *ip = b->num;
509280304Sjkim            ret = b->num;
510280304Sjkim        } else
511280304Sjkim            ret = -1;
512280304Sjkim        break;
513280304Sjkim    case BIO_CTRL_GET_CLOSE:
514280304Sjkim        ret = b->shutdown;
515280304Sjkim        break;
516280304Sjkim    case BIO_CTRL_SET_CLOSE:
517280304Sjkim        b->shutdown = (int)num;
518280304Sjkim        break;
519280304Sjkim    case BIO_CTRL_PENDING:
520280304Sjkim    case BIO_CTRL_WPENDING:
521280304Sjkim        ret = 0;
522280304Sjkim        break;
523280304Sjkim    case BIO_CTRL_FLUSH:
524280304Sjkim        break;
525280304Sjkim    case BIO_CTRL_DUP:
526280304Sjkim        {
527280304Sjkim            dbio = (BIO *)ptr;
528280304Sjkim            if (data->param_port)
529280304Sjkim                BIO_set_conn_port(dbio, data->param_port);
530280304Sjkim            if (data->param_hostname)
531280304Sjkim                BIO_set_conn_hostname(dbio, data->param_hostname);
532280304Sjkim            BIO_set_nbio(dbio, data->nbio);
533280304Sjkim            /*
534280304Sjkim             * FIXME: the cast of the function seems unlikely to be a good
535280304Sjkim             * idea
536280304Sjkim             */
537280304Sjkim            (void)BIO_set_info_callback(dbio,
538280304Sjkim                                        (bio_info_cb *)data->info_callback);
539280304Sjkim        }
540280304Sjkim        break;
541280304Sjkim    case BIO_CTRL_SET_CALLBACK:
542280304Sjkim        {
543280304Sjkim# if 0                          /* FIXME: Should this be used? -- Richard
544280304Sjkim                                 * Levitte */
545280304Sjkim            BIOerr(BIO_F_CONN_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
546280304Sjkim            ret = -1;
547280304Sjkim# else
548280304Sjkim            ret = 0;
549280304Sjkim# endif
550280304Sjkim        }
551280304Sjkim        break;
552280304Sjkim    case BIO_CTRL_GET_CALLBACK:
553280304Sjkim        {
554280304Sjkim            int (**fptr) (const BIO *bio, int state, int xret);
55555714Skris
556280304Sjkim            fptr = (int (**)(const BIO *bio, int state, int xret))ptr;
557280304Sjkim            *fptr = data->info_callback;
558280304Sjkim        }
559280304Sjkim        break;
560280304Sjkim    default:
561280304Sjkim        ret = 0;
562280304Sjkim        break;
563280304Sjkim    }
564280304Sjkim    return (ret);
565280304Sjkim}
56655714Skris
56768651Skrisstatic long conn_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
568280304Sjkim{
569280304Sjkim    long ret = 1;
570280304Sjkim    BIO_CONNECT *data;
57159191Skris
572280304Sjkim    data = (BIO_CONNECT *)b->ptr;
57359191Skris
574280304Sjkim    switch (cmd) {
575280304Sjkim    case BIO_CTRL_SET_CALLBACK:
576280304Sjkim        {
577280304Sjkim            data->info_callback =
578280304Sjkim                (int (*)(const struct bio_st *, int, int))fp;
579280304Sjkim        }
580280304Sjkim        break;
581280304Sjkim    default:
582280304Sjkim        ret = 0;
583280304Sjkim        break;
584280304Sjkim    }
585280304Sjkim    return (ret);
586280304Sjkim}
58759191Skris
58868651Skrisstatic int conn_puts(BIO *bp, const char *str)
589280304Sjkim{
590280304Sjkim    int n, ret;
59155714Skris
592280304Sjkim    n = strlen(str);
593280304Sjkim    ret = conn_write(bp, str, n);
594280304Sjkim    return (ret);
595280304Sjkim}
59655714Skris
59755714SkrisBIO *BIO_new_connect(char *str)
598280304Sjkim{
599280304Sjkim    BIO *ret;
60055714Skris
601280304Sjkim    ret = BIO_new(BIO_s_connect());
602280304Sjkim    if (ret == NULL)
603280304Sjkim        return (NULL);
604280304Sjkim    if (BIO_set_conn_hostname(ret, str))
605280304Sjkim        return (ret);
606280304Sjkim    else {
607280304Sjkim        BIO_free(ret);
608280304Sjkim        return (NULL);
609280304Sjkim    }
610280304Sjkim}
61155714Skris
61255714Skris#endif
613