]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/bio/bss_conn.c
Import of old SSLeay release: SSLeay 0.8.1b
[thirdparty/openssl.git] / crypto / bio / bss_conn.c
1 /* crypto/bio/bss_conn.c */
2 /* Copyright (C) 1995-1997 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 "bio.h"
66
67 /* BIOerr(BIO_F_WSASTARTUP,BIO_R_WSASTARTUP ); */
68
69 #ifdef WIN16
70 #define SOCKET_PROTOCOL 0 /* more microsoft stupidity */
71 #else
72 #define SOCKET_PROTOCOL IPPROTO_TCP
73 #endif
74
75 typedef struct bio_connect_st
76 {
77 int state;
78
79 char *param_hostname;
80 char *param_port;
81 int nbio;
82
83 unsigned char ip[4];
84 short port;
85
86 struct sockaddr_in them;
87
88 /* int socket; this will be kept in bio->num so that it is
89 * compatable with the bss_sock bio */
90 int error;
91 } BIO_CONNECT;
92
93 #ifndef NOPROTO
94 static int conn_write(BIO *h,char *buf,int num);
95 static int conn_read(BIO *h,char *buf,int size);
96 static int conn_puts(BIO *h,char *str);
97 static long conn_ctrl(BIO *h,int cmd,long arg1,char *arg2);
98 static int conn_new(BIO *h);
99 static int conn_free(BIO *data);
100 #else
101 static int conn_write();
102 static int conn_read();
103 static int conn_puts();
104 static long conn_ctrl();
105 static int conn_new();
106 static int conn_free();
107 #endif
108
109 #ifndef NOPROTO
110
111 static int conn_state(BIO *b, BIO_CONNECT *c);
112 static void conn_close_socket(BIO *data);
113 BIO_CONNECT *BIO_CONNECT_new(void );
114 void BIO_CONNECT_free(BIO_CONNECT *a);
115
116 #else
117
118 static int conn_state();
119 static void conn_close_socket();
120 BIO_CONNECT *BIO_CONNECT_new();
121 void BIO_CONNECT_free();
122
123 #endif
124
125 #define CONN_S_BEFORE 1
126 #define CONN_S_GET_IP 2
127 #define CONN_S_GET_PORT 3
128 #define CONN_S_CREATE_SOCKET 4
129 #define CONN_S_CONNECT 5
130 #define CONN_S_OK 6
131 #define CONN_S_BLOCKED_CONNECT 7
132 #define CONN_S_NBIO 8
133
134 static BIO_METHOD methods_connectp=
135 {
136 BIO_TYPE_CONNECT,"socket connect",
137 conn_write,
138 conn_read,
139 conn_puts,
140 NULL, /* connect_gets, */
141 conn_ctrl,
142 conn_new,
143 conn_free,
144 };
145
146 static int conn_state(b,c)
147 BIO *b;
148 BIO_CONNECT *c;
149 {
150 int ret= -1,i;
151 unsigned long l;
152 char *p,*q;
153
154 switch (c->state)
155 {
156 case CONN_S_BEFORE:
157 p=c->param_hostname;
158 if (p == NULL)
159 {
160 BIOerr(BIO_F_CONN_STATE,BIO_R_NO_HOSTHNAME_SPECIFIED);
161 break;
162 }
163 for ( ; *p != '\0'; p++)
164 {
165 if ((*p == ':') || (*p == '/')) break;
166 }
167
168 i= *p;
169 if ((i == ':') || (i == '/'))
170 {
171
172 *(p++)='\0';
173 if (i == ':')
174 {
175 for (q=p; *q; q++)
176 if (*q == '/')
177 {
178 *q='\0';
179 break;
180 }
181 if (c->param_port != NULL)
182 Free(c->param_port);
183 c->param_port=BUF_strdup(p);
184 }
185 }
186
187 if (p == NULL)
188 {
189 BIOerr(BIO_F_CONN_STATE,BIO_R_NO_PORT_SPECIFIED);
190 break;
191 }
192 c->state=CONN_S_GET_IP;
193
194 case CONN_S_GET_IP:
195 if (BIO_get_host_ip(c->param_hostname,&(c->ip[0])) <= 0)
196 break;
197 c->state=CONN_S_GET_PORT;
198
199 case CONN_S_GET_PORT:
200 if (BIO_get_port(c->param_port,&c->port) <= 0)
201 break;
202 c->state=CONN_S_CREATE_SOCKET;
203
204 case CONN_S_CREATE_SOCKET:
205 /* now setup address */
206 memset((char *)&c->them,0,sizeof(c->them));
207 c->them.sin_family=AF_INET;
208 c->them.sin_port=htons((unsigned short)c->port);
209 l=(unsigned long)
210 ((unsigned long)c->ip[0]<<24L)|
211 ((unsigned long)c->ip[1]<<16L)|
212 ((unsigned long)c->ip[2]<< 8L)|
213 ((unsigned long)c->ip[3]);
214 c->them.sin_addr.s_addr=htonl(l);
215 c->state=CONN_S_CREATE_SOCKET;
216
217 ret=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL);
218 if (ret == INVALID_SOCKET)
219 {
220 SYSerr(SYS_F_SOCKET,errno);
221 BIOerr(BIO_F_CONN_STATE,BIO_R_UNABLE_TO_CREATE_SOCKET);
222 break;
223 }
224 b->num=ret;
225 c->state=CONN_S_NBIO;
226
227 case CONN_S_NBIO:
228 #ifdef FIONBIO
229 if (c->nbio)
230 {
231 l=1;
232 ret=BIO_socket_ioctl(b->num,FIONBIO,&l);
233 if (ret < 0)
234 {
235 BIOerr(BIO_F_CONN_STATE,BIO_R_ERROR_SETTING_NBIO);
236 break;
237 }
238 }
239 #endif
240 c->state=CONN_S_CONNECT;
241
242 case CONN_S_CONNECT:
243 BIO_clear_retry_flags(b);
244 ret=connect(b->num,
245 (struct sockaddr *)&c->them,
246 sizeof(c->them));
247 b->retry_reason=0;
248 if (ret < 0)
249 {
250 if (BIO_sock_should_retry(ret))
251 {
252 BIO_set_retry_special(b);
253 c->state=CONN_S_BLOCKED_CONNECT;
254 b->retry_reason=BIO_RR_CONNECT;
255 }
256 else
257 {
258 SYSerr(SYS_F_CONNECT,errno);
259 BIOerr(BIO_F_CONN_STATE,BIO_R_CONNECT_ERROR);
260 }
261 }
262 else
263 {
264 ret=1;
265 c->state=CONN_S_OK;
266 }
267 break;
268
269 case CONN_S_BLOCKED_CONNECT:
270 BIO_clear_retry_flags(b);
271 i=BIO_sock_error(b->num);
272 if (i)
273 {
274 SYSerr(SYS_F_CONNECT,i);
275 BIOerr(BIO_F_CONN_STATE,BIO_R_NBIO_CONNECT_ERROR);
276 }
277 else
278 {
279 c->state=CONN_S_OK;
280 ret=1;
281 }
282 break;
283
284 case CONN_S_OK:
285 ret=1;
286 break;
287 default:
288 abort();
289 }
290 return(ret);
291 }
292
293 BIO_CONNECT *BIO_CONNECT_new()
294 {
295 BIO_CONNECT *ret;
296
297 if ((ret=(BIO_CONNECT *)Malloc(sizeof(BIO_CONNECT))) == NULL)
298 return(NULL);
299 ret->state=CONN_S_BEFORE;
300 ret->param_hostname=NULL;
301 ret->param_port=NULL;
302 ret->nbio=0;
303 ret->ip[0]=0;
304 ret->ip[1]=0;
305 ret->ip[2]=0;
306 ret->ip[3]=0;
307 ret->port=0;
308 memset((char *)&ret->them,0,sizeof(ret->them));
309 ret->error=0;
310 return(ret);
311 }
312
313 void BIO_CONNECT_free(a)
314 BIO_CONNECT *a;
315 {
316 if (a->param_hostname != NULL)
317 Free(a->param_hostname);
318 if (a->param_port != NULL)
319 Free(a->param_port);
320 Free(a);
321 }
322
323 BIO_METHOD *BIO_s_connect()
324 {
325 return(&methods_connectp);
326 }
327
328 static int conn_new(bi)
329 BIO *bi;
330 {
331 bi->init=0;
332 bi->num=INVALID_SOCKET;
333 bi->flags=0;
334 if ((bi->ptr=(char *)BIO_CONNECT_new()) == NULL)
335 return(0);
336 else
337 return(1);
338 }
339
340 static void conn_close_socket(bio)
341 BIO *bio;
342 {
343 BIO_CONNECT *c;
344
345 c=(BIO_CONNECT *)bio->ptr;
346 if (bio->num != INVALID_SOCKET)
347 {
348 /* Only do a shutdown if things were established */
349 if (c->state == CONN_S_OK)
350 shutdown(bio->num,2);
351 # ifdef WINDOWS
352 closesocket(bio->num);
353 # else
354 close(bio->num);
355 # endif
356 bio->num=INVALID_SOCKET;
357 }
358 }
359
360 static int conn_free(a)
361 BIO *a;
362 {
363 BIO_CONNECT *data;
364
365 if (a == NULL) return(0);
366 data=(BIO_CONNECT *)a->ptr;
367
368 if (a->shutdown)
369 {
370 conn_close_socket(a);
371 BIO_CONNECT_free(data);
372 a->ptr=NULL;
373 a->flags=0;
374 a->init=0;
375 }
376 return(1);
377 }
378
379 static int conn_read(b,out,outl)
380 BIO *b;
381 char *out;
382 int outl;
383 {
384 int ret=0;
385 BIO_CONNECT *data;
386
387 data=(BIO_CONNECT *)b->ptr;
388 if (data->state != CONN_S_OK)
389 {
390 ret=conn_state(b,data);
391 if (ret <= 0) return(ret);
392 }
393
394 if (out != NULL)
395 {
396 errno=0;
397 #if defined(WINDOWS)
398 ret=recv(b->num,out,outl,0);
399 #else
400 ret=read(b->num,out,outl);
401 #endif
402 BIO_clear_retry_flags(b);
403 if (ret <= 0)
404 {
405 if (BIO_sock_should_retry(ret))
406 BIO_set_retry_read(b);
407 }
408 }
409 return(ret);
410 }
411
412 static int conn_write(b,in,inl)
413 BIO *b;
414 char *in;
415 int inl;
416 {
417 int ret;
418 BIO_CONNECT *data;
419
420 data=(BIO_CONNECT *)b->ptr;
421 if (data->state != CONN_S_OK)
422 {
423 ret=conn_state(b,data);
424 if (ret <= 0) return(ret);
425 }
426
427 errno=0;
428 #if defined(WINDOWS)
429 ret=send(b->num,in,inl,0);
430 #else
431 ret=write(b->num,in,inl);
432 #endif
433 BIO_clear_retry_flags(b);
434 if (ret <= 0)
435 {
436 if (BIO_sock_should_retry(ret))
437 BIO_set_retry_write(b);
438 }
439 return(ret);
440 }
441
442 static long conn_ctrl(b,cmd,num,ptr)
443 BIO *b;
444 int cmd;
445 long num;
446 char *ptr;
447 {
448 BIO *dbio;
449 int *ip;
450 long ret=1;
451 BIO_CONNECT *data;
452
453 data=(BIO_CONNECT *)b->ptr;
454
455 switch (cmd)
456 {
457 case BIO_CTRL_RESET:
458 ret=0;
459 data->state=CONN_S_BEFORE;
460 conn_close_socket(b);
461 b->flags=0;
462 break;
463 case BIO_C_DO_STATE_MACHINE:
464 /* use this one to start the connection */
465 if (!data->state != CONN_S_OK)
466 ret=(long)conn_state(b,data);
467 else
468 ret=1;
469 break;
470 case BIO_C_SET_CONNECT:
471 if (ptr != NULL)
472 {
473 b->init=1;
474 if (num == 0)
475 {
476 if (data->param_hostname != NULL)
477 Free(data->param_hostname);
478 data->param_hostname=BUF_strdup(ptr);
479 }
480 else if (num == 1)
481 {
482 if (data->param_port != NULL)
483 Free(data->param_port);
484 data->param_port=BUF_strdup(ptr);
485 }
486 }
487 break;
488 case BIO_C_SET_NBIO:
489 data->nbio=(int)num;
490 break;
491 case BIO_C_GET_FD:
492 if (b->init)
493 {
494 ip=(int *)ptr;
495 if (ip != NULL)
496 *ip=b->num;
497 ret=b->num;
498 }
499 else
500 ret= -1;
501 break;
502 case BIO_CTRL_GET_CLOSE:
503 ret=b->shutdown;
504 break;
505 case BIO_CTRL_SET_CLOSE:
506 b->shutdown=(int)num;
507 break;
508 case BIO_CTRL_PENDING:
509 case BIO_CTRL_WPENDING:
510 ret=0;
511 break;
512 case BIO_CTRL_FLUSH:
513 break;
514 case BIO_CTRL_DUP:
515 dbio=(BIO *)ptr;
516 if (data->param_port)
517 BIO_set_port(dbio,data->param_port);
518 if (data->param_hostname)
519 BIO_set_hostname(dbio,data->param_hostname);
520 BIO_set_nbio(dbio,data->nbio);
521 break;
522
523 default:
524 ret=0;
525 break;
526 }
527 return(ret);
528 }
529
530 static int conn_puts(bp,str)
531 BIO *bp;
532 char *str;
533 {
534 int n,ret;
535
536 n=strlen(str);
537 ret=conn_write(bp,str,n);
538 return(ret);
539 }
540
541 BIO *BIO_new_connect(str)
542 char *str;
543 {
544 BIO *ret;
545
546 ret=BIO_new(BIO_s_connect());
547 if (ret == NULL) return(NULL);
548 if (BIO_set_hostname(ret,str))
549 return(ret);
550 else
551 {
552 BIO_free(ret);
553 return(NULL);
554 }
555 }
556
557 #endif
558