[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[or-cvs] r8422: added openssl bio (in bsockets/trunk/contrib: . openssl)
Author: chiussi
Date: 2006-09-19 13:35:42 -0400 (Tue, 19 Sep 2006)
New Revision: 8422
Added:
bsockets/trunk/contrib/openssl/
bsockets/trunk/contrib/openssl/bio_bsock.c
Log:
added openssl bio
Added: bsockets/trunk/contrib/openssl/bio_bsock.c
===================================================================
--- bsockets/trunk/contrib/openssl/bio_bsock.c 2006-09-19 17:33:07 UTC (rev 8421)
+++ bsockets/trunk/contrib/openssl/bio_bsock.c 2006-09-19 17:35:42 UTC (rev 8422)
@@ -0,0 +1,262 @@
+
+#include <stdio.h>
+#include <errno.h>
+#define USE_SOCKETS
+//#include "cryptlib.h"
+#include <openssl/bio.h>
+#include <openssl/ssl.h>
+
+#include <bsocket.h>
+
+static int bsock_write(BIO *h, const char *buf, int num);
+static int bsock_read(BIO *h, char *buf, int size);
+//static int bsock_puts(BIO *h, const char *str);
+static long bsock_ctrl(BIO *h, int cmd, long arg1, void *arg2);
+static int bsock_new(BIO *h);
+static int bsock_free(BIO *data);
+int BIO_bsock_should_retry(int s);
+
+static BIO_METHOD methods_bsockp=
+ {
+ BIO_TYPE_SOCKET,
+ "bsocket",
+ bsock_write,
+ bsock_read,
+ NULL,
+ NULL, /* sock_gets, */
+ bsock_ctrl,
+ NULL,
+ NULL,
+ NULL,
+ };
+
+BIO_METHOD *BIO_s_bsocket(void)
+ {
+ return(&methods_bsockp);
+ }
+
+int SSL_set_bfd(SSL *s,int fd)
+ {
+ int ret=0;
+ BIO *bio=NULL;
+
+ bio=BIO_new(BIO_s_bsocket());
+
+ if (bio == NULL)
+ {
+ //SSLerr(SSL_F_SSL_SET_FD,ERR_R_BUF_LIB);
+ goto err;
+ }
+ BIO_set_fd(bio,fd,BIO_NOCLOSE);
+ SSL_set_bio(s,bio,bio);
+ ret=1;
+err:
+ return(ret);
+ }
+
+//BIO *BIO_new_socket(int fd, int close_flag)
+// {
+// BIO *ret;
+//
+// ret=BIO_new(BIO_s_bsocket());
+// if (ret == NULL) return(NULL);
+// BIO_set_fd(ret,fd,close_flag);
+// return(ret);
+// }
+
+static int sock_new(BIO *bi)
+ {
+ bi->init=0;
+ bi->num=0;
+ bi->ptr=NULL;
+ bi->flags=0;
+ return(1);
+ }
+
+static int sock_free(BIO *a)
+ {
+ if (a == NULL) return(0);
+ if (a->shutdown)
+{
+ if (a->init)
+ {
+ //SHUTDOWN2(a->num);
+ }
+ a->init=0;
+ a->flags=0;
+ }
+ return(1);
+ }
+
+static int bsock_read(BIO *b, char *out, int outl)
+ {
+ int ret=0;
+
+ if (out != NULL)
+ {
+ errno=0;
+ ret=brecv(b->num,out,outl);
+ BIO_clear_retry_flags(b);
+ if (ret <= 0)
+ {
+ if (BIO_sock_should_retry(ret))
+ BIO_set_retry_read(b);
+ }
+ }
+ return(ret);
+ }
+
+static int bsock_write(BIO *b, const char *in, int inl)
+ {
+ int ret;
+
+ errno=0;
+ ret=bsend(b->num,in,inl);
+ BIO_clear_retry_flags(b);
+ if (ret <= 0)
+ {
+ if (BIO_sock_should_retry(ret))
+ BIO_set_retry_write(b);
+ }
+ return(ret);
+ }
+
+static long bsock_ctrl(BIO *b, int cmd, long num, void *ptr)
+ {
+ long ret=1;
+ int *ip;
+
+ switch (cmd)
+ {
+ case BIO_CTRL_RESET:
+ num=0;
+ case BIO_C_FILE_SEEK:
+ ret=0;
+ break;
+ case BIO_C_FILE_TELL:
+ case BIO_CTRL_INFO:
+ ret=0;
+ break;
+ case BIO_C_SET_FD:
+ sock_free(b);
+ b->num= *((int *)ptr);
+ b->shutdown=(int)num;
+ b->init=1;
+ break;
+ case BIO_C_GET_FD:
+ if (b->init)
+ {
+ ip=(int *)ptr;
+ if (ip != NULL) *ip=b->num;
+ ret=b->num;
+ }
+ else
+ ret= -1;
+ break;
+ case BIO_CTRL_GET_CLOSE:
+ ret=b->shutdown;
+ break;
+ case BIO_CTRL_SET_CLOSE:
+ b->shutdown=(int)num;
+ break;
+ case BIO_CTRL_PENDING:
+ case BIO_CTRL_WPENDING:
+ ret=0;
+ break;
+ case BIO_CTRL_DUP:
+ case BIO_CTRL_FLUSH:
+ ret=1;
+ break;
+ default:
+ ret=0;
+ break;
+ }
+ return(ret);
+ }
+/*
+static int sock_puts(BIO *bp, const char *str)
+ {
+ int n,ret;
+
+ n=strlen(str);
+ ret=sock_write(bp,str,n);
+ return(ret);
+ }
+*/
+int BIO_bsock_should_retry(int i)
+ {
+ int err;
+
+ if ((i == 0) || (i == -1))
+ {
+ err=errno;
+
+#if defined(OPENSSL_SYS_WINDOWS) && 0 /* more microsoft stupidity? perhaps not? Ben 4/1/99 */
+ if ((i == -1) && (err == 0))
+ return(1);
+#endif
+
+ return(BIO_sock_non_fatal_error(err));
+ }
+ return(0);
+ }
+/*
+int BIO_sock_non_fatal_error(int err)
+ {
+ switch (err)
+ {
+#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_NETWARE)
+# if defined(WSAEWOULDBLOCK)
+ case WSAEWOULDBLOCK:
+# endif
+*/
+//# if 0 /* This appears to always be an error */
+//
+//# if defined(WSAENOTCONN)
+// case WSAENOTCONN:
+//# endif
+//# endif
+//#endif
+//
+//#ifdef EWOULDBLOCK
+//# ifdef WSAEWOULDBLOCK
+//# if WSAEWOULDBLOCK != EWOULDBLOCK
+// case EWOULDBLOCK:
+//# endif
+//# else
+// case EWOULDBLOCK:
+//# endif
+//#endif
+
+//#if defined(ENOTCONN)
+// case ENOTCONN:
+//#endif
+//
+//#ifdef EINTR
+// case EINTR:
+//#endif
+
+//#ifdef EAGAIN
+//# if EWOULDBLOCK != EAGAIN
+// case EAGAIN:
+//# endif
+//#endif
+//
+//#ifdef EPROTO
+// case EPROTO:
+//#endif
+//
+//#ifdef EINPROGRESS
+// case EINPROGRESS:
+//#endif
+
+//#ifdef EALREADY
+// case EALREADY:
+//#endif
+// return(1);
+ /* break; */
+// default:
+// break;
+// }
+// return(0);
+// }
Property changes on: bsockets/trunk/contrib/openssl/bio_bsock.c
___________________________________________________________________
Name: svn:executable
+ *