Skip to content

Commit

Permalink
Add GnuTLS infrastructure
Browse files Browse the repository at this point in the history
Add configure.ac section to detect GnuTLS

Add buffer.[ch] and crypto-gnutls.[ch] from
  https://github.com/abligh/tlsproxy

Add Makefile.am changes to link these new files in

Signed-off-by: Alex Bligh <alex@alex.org.uk>
(cherry picked from commit aac8f6a)
[wouter: updated to cooperate with server-side GnuTLS support that already exists]
Signed-off-by: Wouter Verhelst <w@uter.be>
  • Loading branch information
abligh authored and yoe committed Nov 3, 2016
1 parent 6a73e15 commit 91cbdfa
Show file tree
Hide file tree
Showing 7 changed files with 953 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Makefile.am
Expand Up @@ -19,3 +19,8 @@ nbd_server_LDADD = @GLIB_LIBS@ libnbdsrv.la libcliserv.la @GnuTLS_LIBS@
nbd_trdump_LDADD = libcliserv.la
make_integrityhuge_SOURCES = make-integrityhuge.c cliserv.h nbd.h nbd-debug.h
EXTRA_DIST = maketr CodingStyle autogen.sh README.md support/genver.sh
TLSSRC = crypto-gnutls.c crypto-gnutls.h buffer.c buffer.h
if GNUTLS
nbd_client_SOURCES += $(TLSSRC)
nbd_server_SOURCES += $(TLSSRC)
endif
225 changes: 225 additions & 0 deletions buffer.c
@@ -0,0 +1,225 @@
/*
The MIT License (MIT)
Copyright (c) 2016 Wrymouth Innovation Ltd
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/

#include <sys/types.h>

#include "buffer.h"

typedef struct buffer
{
char *buf;
ssize_t size;
ssize_t hwm;
ssize_t ridx;
ssize_t widx;
int empty;
} buffer_t;

/* the buffer is organised internally as follows:
*
* * There are b->size bytes in the buffer.
*
* * Bytes are at offsets 0 to b->size-1
*
* * b->ridx points to the first readable byte
*
* * b->widx points to the first empty space
*
* * b->ridx < b->widx indicates a non-wrapped buffer:
*
* 0 ridx widx size
* | | | |
* V V V V
* ........XXXXXXXXX................
*
* * b->ridx > b->widx indicates a wrapped buffer:
*
* 0 widx ridx size
* | | | |
* V V V V
* XXXXXXXX.........XXXXXXXXXXXXXXXX
*
* * b->ridx == b->widx indicates a FULL buffer:
*
* * b->ridx == b->widx indicates a wrapped buffer:
*
* 0 widx == ridx size
* | | |
* V V V
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*
* An empty buffer is indicated by empty=1
*
*/

buffer_t *
bufNew (ssize_t size, ssize_t hwm)
{
buffer_t *b = calloc (1, sizeof (buffer_t));
b->buf = calloc (1, size);
b->size = size;
b->hwm = hwm;
b->empty = 1;
return b;
}


void
bufFree (buffer_t * b)
{
free (b->buf);
free (b);
}

/* get a maximal span to read. Returns 0 if buffer
* is empty
*/
ssize_t
bufGetReadSpan (buffer_t * b, void **addr)
{
if (b->empty)
{
*addr = NULL;
return 0;
}
*addr = &(b->buf[b->ridx]);
ssize_t len = b->widx - b->ridx;
if (len <= 0)
len = b->size - b->ridx;
return len;
}

/* get a maximal span to write. Returns 0 id buffer is full
*/
ssize_t
bufGetWriteSpan (buffer_t * b, void **addr)
{
if (b->empty)
{
*addr = b->buf;
b->ridx = 0;
b->widx = 0;
return b->size;
}
if (b->ridx == b->widx)
{
*addr = NULL;
return 0;
}
*addr = &(b->buf[b->widx]);
ssize_t len = b->ridx - b->widx;
if (len <= 0)
len = b->size - b->widx;
return len;
}

/* mark size bytes as read */
void
bufDoneRead (buffer_t * b, ssize_t size)
{
while (!b->empty && (size > 0))
{
/* empty can't occur here, so equal pointers means full */
ssize_t len = b->widx - b->ridx;
if (len <= 0)
len = b->size - b->ridx;

/* len is the number of bytes in one read span */
if (len > size)
len = size;

b->ridx += len;
if (b->ridx >= b->size)
b->ridx = 0;

if (b->ridx == b->widx)
{
b->ridx = 0;
b->widx = 0;
b->empty = 1;
}

size -= len;
}
}

/* mark size bytes as written */
void
bufDoneWrite (buffer_t * b, ssize_t size)
{
while ((b->empty || (b->ridx != b->widx)) && (size > 0))
{
/* full can't occur here, so equal pointers means empty */
ssize_t len = b->ridx - b->widx;
if (len <= 0)
len = b->size - b->widx;

/* len is the number of bytes in one write span */
if (len > size)
len = size;

b->widx += len;
if (b->widx >= b->size)
b->widx = 0;

/* it can't be empty as we've written at least one byte */
b->empty = 0;

size -= len;
}
}

int
bufIsEmpty (buffer_t * b)
{
return b->empty;
}

int
bufIsFull (buffer_t * b)
{
return !b->empty && (b->ridx == b->widx);
}

int
bufIsOverHWM (buffer_t * b)
{
return bufGetCount (b) > b->hwm;
}

ssize_t
bufGetFree (buffer_t * b)
{
return b->size - bufGetCount (b);
}

ssize_t
bufGetCount (buffer_t * b)
{
if (b->empty)
return 0;
return b->widx - b->ridx + ((b->ridx < b->widx) ? 0 : b->size);
}
45 changes: 45 additions & 0 deletions buffer.h
@@ -0,0 +1,45 @@
/*
The MIT License (MIT)
Copyright (c) 2016 Wrymouth Innovation Ltd
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef __TLSPROXY_BUFFERS_H
#define __TLSPROXY_BUFFERS_H

#include <stdlib.h>
#include <sys/types.h>

typedef struct buffer buffer_t;

buffer_t *bufNew (ssize_t size, ssize_t hwm);
void bufFree (buffer_t * b);
ssize_t bufGetReadSpan (buffer_t * b, void **addr);
ssize_t bufGetWriteSpan (buffer_t * b, void **addr);
void bufDoneRead (buffer_t * b, ssize_t size);
void bufDoneWrite (buffer_t * b, ssize_t size);
int bufIsEmpty (buffer_t * b);
int bufIsFull (buffer_t * b);
int bufIsOverHWM (buffer_t * b);
ssize_t bufGetFree (buffer_t * b);
ssize_t bufGetCount (buffer_t * b);

#endif
2 changes: 2 additions & 0 deletions configure.ac
Expand Up @@ -121,6 +121,7 @@ AC_CHECK_SIZEOF(unsigned long long int)
AC_STRUCT_DIRENT_D_TYPE
AC_CHECK_FUNCS([llseek alarm gethostbyname inet_ntoa memset socket strerror strstr mkstemp fdatasync])
HAVE_FL_PH=no

AC_CHECK_FUNC(fallocate,
[
AC_CHECK_HEADERS([linux/falloc.h])
Expand Down Expand Up @@ -211,6 +212,7 @@ dnl fi

PKG_CHECK_MODULES(GnuTLS, [gnutls >= 3.3.0],[HAVE_GNUTLS=1],[HAVE_GNUTLS=0])
AC_DEFINE(HAVE_GNUTLS, $HAVE_GNUTLS, [Define to 1 if you have a GnuTLS version of 3.3 or above])
AM_CONDITIONAL([GNUTLS], [test "x$HAVE_GNUTLS" = "x1"])

AC_CHECK_HEADERS([winioctl.h], [], [],
[#include <io.h>
Expand Down

0 comments on commit 91cbdfa

Please sign in to comment.