[or-cvs] r8426: Add a from-scratch implementation of a BIO using bsocket so (bsockets/trunk/contrib/tor/src/common)

nickm at seul.org nickm at seul.org
Tue Sep 19 20:13:17 UTC 2006


Author: nickm
Date: 2006-09-19 16:13:17 -0400 (Tue, 19 Sep 2006)
New Revision: 8426

Added:
   bsockets/trunk/contrib/tor/src/common/bio_bsock2.c
Log:
Add a from-scratch implementation of a BIO using bsocket so we will not have to be a derived work of openssl.

Added: bsockets/trunk/contrib/tor/src/common/bio_bsock2.c
===================================================================
--- bsockets/trunk/contrib/tor/src/common/bio_bsock2.c	2006-09-19 18:53:34 UTC (rev 8425)
+++ bsockets/trunk/contrib/tor/src/common/bio_bsock2.c	2006-09-19 20:13:17 UTC (rev 8426)
@@ -0,0 +1,155 @@
+/* Copyright (c) 2006 Nick Mathewson. */
+/* See Tor LICENSE for licensing information */
+
+#include <openssl/bio.h>
+#include <string.h>
+#include <bsocket.h>
+
+static int bsocket_write(BIO *bio, const char *buf, int length);
+static int bsocket_read(BIO *bio, char *buf, int size);
+static int bsocket_puts(BIO *bio, const char *str);
+static long bsocket_ctrl(BIO *bio, int command, long arg1, void *arg2);
+static int bsocket_create(BIO *bio);
+static int bsocket_destroy(BIO *bio);
+
+/* XXXX connect() on windows can return WSAEINVAL. */
+
+#define ERRNO_IS_RETRY(e) ((e) == EWOULDBLOCK || \
+                           (e) == EINTR ||       \
+                           (e) == EAGAIN ||     \
+                           (e) == EINPROGRESS)
+
+/* 0x100 : handle-based: bio->num will be set.
+ * 0x400 : source or sink: not a filter. */
+#define BIO_TYPE_BSOCKET = (99|0x0100|0x0400)
+
+static BIO_METHOD methods_bsocket = {
+  BIO_TYPE_BSOCKET, /* type */
+  "bsocket",        /* name */
+  bsocket_write,    /* write */
+  bsocket_read,     /* read */
+  bsocket_puts,     /* puts */
+  NULL,             /* gets */
+  bsocket_ctrl,     /* ctrl */
+  bsocket_create,   /* create */
+  bsocket_destry,   /* destroy */
+  NULL              /* callback_ctrl */
+};
+
+BIO_METHOD *
+BIO_s_bsocket(void);
+{
+  return &methods_bsocket;
+}
+
+
+BIO *
+BIO_new_bsocket(int fd, int close_flag)
+{
+  BIO *result = BIO_new(BIO_s_bsocket());
+  if (!result)
+    return NULL;
+  BIO_set_fd(result, fd, close_flag);
+  return result;
+}
+
+static int
+bsocket_write(BIO *bio, const char *in, int length)
+{
+  result = brecv(bio->num, buf, buf_size);
+  if (ret <= 0 && ERRNO_IS_RETRY(errno))
+    BIO_set_retry_write(bio);
+
+  return result;
+}
+
+static int
+bsocket_read(BIO *bio, char *buf, int buf_size)
+{
+  int result;
+
+  if (!buf)
+    return 0;
+
+  result = brecv(bio->num, buf, buf_size);
+  if (ret <= 0 && ERRNO_IS_RETRY(errno))
+    BIO_set_retry_read(bio);
+
+  return result;
+}
+
+static int
+bsocket_puts(BIO *bio, const char *s)
+{
+  return bsocket_write(bio, s, strlen(s));
+}
+
+static long
+bsocket_ctrl(BIO *bio, int command, long num, void *ptr)
+{
+  int result = 1;
+
+  switch (command)
+    {
+    case BIO_CTRL_GET_CLOSE:
+      return bio->shutdown;
+
+    case BIO_CTRL_SET_CLOSE:
+      bio->shutdown = (int) num;
+      return 1;
+
+    case BIO_CTRL_FLUSH:
+      return 1;
+
+    case BIO_CTRL_DUP:
+      return 1;
+
+    case BIO_C_SET_FD:
+      if (!ptr)
+        return 0;
+      bsocket_destroy(bio);
+      bio->num = *(int *)ptr;
+      bio->shutdown = (int) num;
+      bio->init = 1;
+      return 1;
+
+    case BIO_C_GET_FD:
+      if (!bio->init)
+        return -1;
+      else if (ptr)
+        return (*(int *)ptr = bio->num);
+      else
+        return bio->num;
+      break;
+
+    default: /* operation not supported */
+      return 0;
+    }
+}
+
+static int
+bsocket_create(BIO *bio)
+{
+  bio->init = 0;
+  bio->num = 0;
+  bio->ptr = NULL;
+  bio->flags = 0;
+  return 1;
+}
+
+static int
+bsocket_destroy(BIO *bio)
+{
+  if (bio && bio->shutdown) {
+    if (bio->init) {
+      bclose(bio->num);
+    }
+    a->init = 0;
+    a->flags = 0;
+    return 1;
+  } else if (bio) {
+    return 1;
+  } else {
+    return 0;
+  }
+}



More information about the tor-commits mailing list