]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/bio/bss_conn.c
Remove NOPROTO definitions and error code comments.
[thirdparty/openssl.git] / crypto / bio / bss_conn.c
1 /* crypto/bio/bss_conn.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59 #ifndef NO_SOCK
60
61 #include <stdio.h>
62 #include <errno.h>
63 #define USE_SOCKETS
64 #include "cryptlib.h"
65 #include <openssl/bio.h>
66
67 #ifdef WIN16
68 #define SOCKET_PROTOCOL 0 /* more microsoft stupidity */
69 #else
70 #define SOCKET_PROTOCOL IPPROTO_TCP
71 #endif
72
73 typedef struct bio_connect_st
74 {
75 int state;
76
77 char *param_hostname;
78 char *param_port;
79 int nbio;
80
81 unsigned char ip[4];
82 unsigned short port;
83
84 struct sockaddr_in them;
85
86 /* int socket; this will be kept in bio->num so that it is
87 * compatable with the bss_sock bio */
88
89 /* called when the connection is initially made
90 * callback(BIO,state,ret); The callback should return
91 * 'ret'. state is for compatablity with the ssl info_callback */
92 int (*info_callback)();
93 } BIO_CONNECT;
94
95 static int conn_write(BIO *h,char *buf,int num);
96 static int conn_read(BIO *h,char *buf,int size);
97 static int conn_puts(BIO *h,char *str);
98 static long conn_ctrl(BIO *h,int cmd,long arg1,char *arg2);
99 static int conn_new(BIO *h);
100 static int conn_free(BIO *data);
101
102 static int conn_state(BIO *b, BIO_CONNECT *c);
103 static void conn_close_socket(BIO *data);
104 BIO_CONNECT *BIO_CONNECT_new(void );
105 void BIO_CONNECT_free(BIO_CONNECT *a);
106
107 static BIO_METHOD methods_connectp=
108 {
109 BIO_TYPE_CONNECT,
110 "socket connect",
111 conn_write,
112 conn_read,
113 conn_puts,
114 NULL, /* connect_gets, */
115 conn_ctrl,
116 conn_new,
117 conn_free,
118 };
119
120 static int conn_state(BIO *b, BIO_CONNECT *c)
121 {
122 int ret= -1,i;
123 unsigned long l;
124 char *p,*q;
125 int (*cb)()=NULL;
126
127 if (c->info_callback != NULL)
128 cb=c->info_callback;
129
130 for (;;)
131 {
132 switch (c->state)
133 {
134 case BIO_CONN_S_BEFORE:
135 p=c->param_hostname;
136 if (p == NULL)
137 {
138 BIOerr(BIO_F_CONN_STATE,BIO_R_NO_HOSTNAME_SPECIFIED);
139 goto exit_loop;
140 }
141 for ( ; *p != '\0'; p++)
142 {
143 if ((*p == ':') || (*p == '/')) break;
144 }
145
146 i= *p;
147 if ((i == ':') || (i == '/'))
148 {
149
150 *(p++)='\0';
151 if (i == ':')
152 {
153 for (q=p; *q; q++)
154 if (*q == '/')
155 {
156 *q='\0';
157 break;
158 }
159 if (c->param_port != NULL)
160 Free(c->param_port);
161 c->param_port=BUF_strdup(p);
162 }
163 }
164
165 if (c->param_port == NULL)
166 {
167 BIOerr(BIO_F_CONN_STATE,BIO_R_NO_PORT_SPECIFIED);
168 ERR_add_error_data(2,"host=",c->param_hostname);
169 goto exit_loop;
170 }
171 c->state=BIO_CONN_S_GET_IP;
172 break;
173
174 case BIO_CONN_S_GET_IP:
175 if (BIO_get_host_ip(c->param_hostname,&(c->ip[0])) <= 0)
176 goto exit_loop;
177 c->state=BIO_CONN_S_GET_PORT;
178 break;
179
180 case BIO_CONN_S_GET_PORT:
181 if (c->param_port == NULL)
182 {
183 abort();
184 goto exit_loop;
185 }
186 else if (BIO_get_port(c->param_port,&c->port) <= 0)
187 goto exit_loop;
188 c->state=BIO_CONN_S_CREATE_SOCKET;
189 break;
190
191 case BIO_CONN_S_CREATE_SOCKET:
192 /* now setup address */
193 memset((char *)&c->them,0,sizeof(c->them));
194 c->them.sin_family=AF_INET;
195 c->them.sin_port=htons((unsigned short)c->port);
196 l=(unsigned long)
197 ((unsigned long)c->ip[0]<<24L)|
198 ((unsigned long)c->ip[1]<<16L)|
199 ((unsigned long)c->ip[2]<< 8L)|
200 ((unsigned long)c->ip[3]);
201 c->them.sin_addr.s_addr=htonl(l);
202 c->state=BIO_CONN_S_CREATE_SOCKET;
203
204 ret=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL);
205 if (ret == INVALID_SOCKET)
206 {
207 SYSerr(SYS_F_SOCKET,get_last_socket_error());
208 ERR_add_error_data(4,"host=",c->param_hostname,
209 ":",c->param_port);
210 BIOerr(BIO_F_CONN_STATE,BIO_R_UNABLE_TO_CREATE_SOCKET);
211 goto exit_loop;
212 }
213 b->num=ret;
214 c->state=BIO_CONN_S_NBIO;
215 break;
216
217 case BIO_CONN_S_NBIO:
218 if (c->nbio)
219 {
220 if (!BIO_socket_nbio(b->num,1))
221 {
222 BIOerr(BIO_F_CONN_STATE,BIO_R_ERROR_SETTING_NBIO);
223 ERR_add_error_data(4,"host=",
224 c->param_hostname,
225 ":",c->param_port);
226 goto exit_loop;
227 }
228 }
229 c->state=BIO_CONN_S_CONNECT;
230
231 #ifdef SO_KEEPALIVE
232 i=1;
233 i=setsockopt(b->num,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i));
234 if (i < 0)
235 {
236 SYSerr(SYS_F_SOCKET,get_last_socket_error());
237 ERR_add_error_data(4,"host=",c->param_hostname,
238 ":",c->param_port);
239 BIOerr(BIO_F_CONN_STATE,BIO_R_KEEPALIVE);
240 goto exit_loop;
241 }
242 #endif
243 break;
244
245 case BIO_CONN_S_CONNECT:
246 BIO_clear_retry_flags(b);
247 ret=connect(b->num,
248 (struct sockaddr *)&c->them,
249 sizeof(c->them));
250 b->retry_reason=0;
251 if (ret < 0)
252 {
253 if (BIO_sock_should_retry(ret))
254 {
255 BIO_set_retry_special(b);
256 c->state=BIO_CONN_S_BLOCKED_CONNECT;
257 b->retry_reason=BIO_RR_CONNECT;
258 }
259 else
260 {
261 SYSerr(SYS_F_CONNECT,get_last_socket_error());
262 ERR_add_error_data(4,"host=",
263 c->param_hostname,
264 ":",c->param_port);
265 BIOerr(BIO_F_CONN_STATE,BIO_R_CONNECT_ERROR);
266 }
267 goto exit_loop;
268 }
269 else
270 c->state=BIO_CONN_S_OK;
271 break;
272
273 case BIO_CONN_S_BLOCKED_CONNECT:
274 i=BIO_sock_error(b->num);
275 if (i)
276 {
277 BIO_clear_retry_flags(b);
278 SYSerr(SYS_F_CONNECT,i);
279 ERR_add_error_data(4,"host=",
280 c->param_hostname,
281 ":",c->param_port);
282 BIOerr(BIO_F_CONN_STATE,BIO_R_NBIO_CONNECT_ERROR);
283 ret=0;
284 goto exit_loop;
285 }
286 else
287 c->state=BIO_CONN_S_OK;
288 break;
289
290 case BIO_CONN_S_OK:
291 ret=1;
292 goto exit_loop;
293 default:
294 abort();
295 goto exit_loop;
296 }
297
298 if (cb != NULL)
299 {
300 if (!(ret=cb((BIO *)b,c->state,ret)))
301 goto end;
302 }
303 }
304
305 /* Loop does not exit */
306 exit_loop:
307 if (cb != NULL)
308 ret=cb((BIO *)b,c->state,ret);
309 end:
310 return(ret);
311 }
312
313 BIO_CONNECT *BIO_CONNECT_new(void)
314 {
315 BIO_CONNECT *ret;
316
317 if ((ret=(BIO_CONNECT *)Malloc(sizeof(BIO_CONNECT))) == NULL)
318 return(NULL);
319 ret->state=BIO_CONN_S_BEFORE;
320 ret->param_hostname=NULL;
321 ret->param_port=NULL;
322 ret->info_callback=NULL;
323 ret->nbio=0;
324 ret->ip[0]=0;
325 ret->ip[1]=0;
326 ret->ip[2]=0;
327 ret->ip[3]=0;
328 ret->port=0;
329 memset((char *)&ret->them,0,sizeof(ret->them));
330 return(ret);
331 }
332
333 void BIO_CONNECT_free(BIO_CONNECT *a)
334 {
335 if(a == NULL)
336 return;
337
338 if (a->param_hostname != NULL)
339 Free(a->param_hostname);
340 if (a->param_port != NULL)
341 Free(a->param_port);
342 Free(a);
343 }
344
345 BIO_METHOD *BIO_s_connect(void)
346 {
347 return(&methods_connectp);
348 }
349
350 static int conn_new(BIO *bi)
351 {
352 bi->init=0;
353 bi->num=INVALID_SOCKET;
354 bi->flags=0;
355 if ((bi->ptr=(char *)BIO_CONNECT_new()) == NULL)
356 return(0);
357 else
358 return(1);
359 }
360
361 static void conn_close_socket(BIO *bio)
362 {
363 BIO_CONNECT *c;
364
365 c=(BIO_CONNECT *)bio->ptr;
366 if (bio->num != INVALID_SOCKET)
367 {
368 /* Only do a shutdown if things were established */
369 if (c->state == BIO_CONN_S_OK)
370 shutdown(bio->num,2);
371 closesocket(bio->num);
372 bio->num=INVALID_SOCKET;
373 }
374 }
375
376 static int conn_free(BIO *a)
377 {
378 BIO_CONNECT *data;
379
380 if (a == NULL) return(0);
381 data=(BIO_CONNECT *)a->ptr;
382
383 if (a->shutdown)
384 {
385 conn_close_socket(a);
386 BIO_CONNECT_free(data);
387 a->ptr=NULL;
388 a->flags=0;
389 a->init=0;
390 }
391 return(1);
392 }
393
394 static int conn_read(BIO *b, char *out, int outl)
395 {
396 int ret=0;
397 BIO_CONNECT *data;
398
399 data=(BIO_CONNECT *)b->ptr;
400 if (data->state != BIO_CONN_S_OK)
401 {
402 ret=conn_state(b,data);
403 if (ret <= 0)
404 return(ret);
405 }
406
407 if (out != NULL)
408 {
409 clear_socket_error();
410 ret=readsocket(b->num,out,outl);
411 BIO_clear_retry_flags(b);
412 if (ret <= 0)
413 {
414 if (BIO_sock_should_retry(ret))
415 BIO_set_retry_read(b);
416 }
417 }
418 return(ret);
419 }
420
421 static int conn_write(BIO *b, char *in, int inl)
422 {
423 int ret;
424 BIO_CONNECT *data;
425
426 data=(BIO_CONNECT *)b->ptr;
427 if (data->state != BIO_CONN_S_OK)
428 {
429 ret=conn_state(b,data);
430 if (ret <= 0) return(ret);
431 }
432
433 clear_socket_error();
434 ret=writesocket(b->num,in,inl);
435 BIO_clear_retry_flags(b);
436 if (ret <= 0)
437 {
438 if (BIO_sock_should_retry(ret))
439 BIO_set_retry_write(b);
440 }
441 return(ret);
442 }
443
444 static long conn_ctrl(BIO *b, int cmd, long num, char *ptr)
445 {
446 BIO *dbio;
447 int *ip;
448 const char **pptr;
449 long ret=1;
450 BIO_CONNECT *data;
451
452 data=(BIO_CONNECT *)b->ptr;
453
454 switch (cmd)
455 {
456 case BIO_CTRL_RESET:
457 ret=0;
458 data->state=BIO_CONN_S_BEFORE;
459 conn_close_socket(b);
460 b->flags=0;
461 break;
462 case BIO_C_DO_STATE_MACHINE:
463 /* use this one to start the connection */
464 if (!data->state != BIO_CONN_S_OK)
465 ret=(long)conn_state(b,data);
466 else
467 ret=1;
468 break;
469 case BIO_C_GET_CONNECT:
470 if (ptr != NULL)
471 {
472 pptr=(const char **)ptr;
473 if (num == 0)
474 {
475 *pptr=data->param_hostname;
476
477 }
478 else if (num == 1)
479 {
480 *pptr=data->param_port;
481 }
482 else if (num == 2)
483 {
484 *pptr= (char *)&(data->ip[0]);
485 }
486 else if (num == 3)
487 {
488 *((int *)ptr)=data->port;
489 }
490 if ((!b->init) || (ptr == NULL))
491 *pptr="not initalised";
492 ret=1;
493 }
494 break;
495 case BIO_C_SET_CONNECT:
496 if (ptr != NULL)
497 {
498 b->init=1;
499 if (num == 0)
500 {
501 if (data->param_hostname != NULL)
502 Free(data->param_hostname);
503 data->param_hostname=BUF_strdup(ptr);
504 }
505 else if (num == 1)
506 {
507 if (data->param_port != NULL)
508 Free(data->param_port);
509 data->param_port=BUF_strdup(ptr);
510 }
511 else if (num == 2)
512 {
513 char buf[16];
514
515 sprintf(buf,"%d.%d.%d.%d",
516 ptr[0],ptr[1],ptr[2],ptr[3]);
517 if (data->param_hostname != NULL)
518 Free(data->param_hostname);
519 data->param_hostname=BUF_strdup(buf);
520 memcpy(&(data->ip[0]),ptr,4);
521 }
522 else if (num == 3)
523 {
524 char buf[16];
525
526 sprintf(buf,"%d",*(int *)ptr);
527 if (data->param_port != NULL)
528 Free(data->param_port);
529 data->param_port=BUF_strdup(buf);
530 data->port= *(int *)ptr;
531 }
532 }
533 break;
534 case BIO_C_SET_NBIO:
535 data->nbio=(int)num;
536 break;
537 case BIO_C_GET_FD:
538 if (b->init)
539 {
540 ip=(int *)ptr;
541 if (ip != NULL)
542 *ip=b->num;
543 ret=b->num;
544 }
545 else
546 ret= -1;
547 break;
548 case BIO_CTRL_GET_CLOSE:
549 ret=b->shutdown;
550 break;
551 case BIO_CTRL_SET_CLOSE:
552 b->shutdown=(int)num;
553 break;
554 case BIO_CTRL_PENDING:
555 case BIO_CTRL_WPENDING:
556 ret=0;
557 break;
558 case BIO_CTRL_FLUSH:
559 break;
560 case BIO_CTRL_DUP:
561 dbio=(BIO *)ptr;
562 if (data->param_port)
563 BIO_set_conn_port(dbio,data->param_port);
564 if (data->param_hostname)
565 BIO_set_conn_hostname(dbio,data->param_hostname);
566 BIO_set_nbio(dbio,data->nbio);
567 BIO_set_info_callback(dbio,data->info_callback);
568 break;
569 case BIO_CTRL_SET_CALLBACK:
570 data->info_callback=(int (*)())ptr;
571 break;
572 case BIO_CTRL_GET_CALLBACK:
573 {
574 int (**fptr)();
575
576 fptr=(int (**)())ptr;
577 *fptr=data->info_callback;
578 }
579 break;
580 default:
581 ret=0;
582 break;
583 }
584 return(ret);
585 }
586
587 static int conn_puts(BIO *bp, char *str)
588 {
589 int n,ret;
590
591 n=strlen(str);
592 ret=conn_write(bp,str,n);
593 return(ret);
594 }
595
596 BIO *BIO_new_connect(char *str)
597 {
598 BIO *ret;
599
600 ret=BIO_new(BIO_s_connect());
601 if (ret == NULL) return(NULL);
602 if (BIO_set_conn_hostname(ret,str))
603 return(ret);
604 else
605 {
606 BIO_free(ret);
607 return(NULL);
608 }
609 }
610
611 #endif
612