Skip to content

Commit 91cbdfa

Browse files
ablighyoe
authored andcommitted
Add GnuTLS infrastructure
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 <[email protected]> (cherry picked from commit aac8f6a) [wouter: updated to cooperate with server-side GnuTLS support that already exists] Signed-off-by: Wouter Verhelst <[email protected]>
1 parent 6a73e15 commit 91cbdfa

File tree

7 files changed

+953
-0
lines changed

7 files changed

+953
-0
lines changed

Makefile.am

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@ nbd_server_LDADD = @GLIB_LIBS@ libnbdsrv.la libcliserv.la @GnuTLS_LIBS@
1919
nbd_trdump_LDADD = libcliserv.la
2020
make_integrityhuge_SOURCES = make-integrityhuge.c cliserv.h nbd.h nbd-debug.h
2121
EXTRA_DIST = maketr CodingStyle autogen.sh README.md support/genver.sh
22+
TLSSRC = crypto-gnutls.c crypto-gnutls.h buffer.c buffer.h
23+
if GNUTLS
24+
nbd_client_SOURCES += $(TLSSRC)
25+
nbd_server_SOURCES += $(TLSSRC)
26+
endif

buffer.c

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
/*
2+
3+
The MIT License (MIT)
4+
5+
Copyright (c) 2016 Wrymouth Innovation Ltd
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a
8+
copy of this software and associated documentation files (the "Software"),
9+
to deal in the Software without restriction, including without limitation
10+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
11+
and/or sell copies of the Software, and to permit persons to whom the
12+
Software is furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included
15+
in all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20+
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
OTHER DEALINGS IN THE SOFTWARE.
24+
25+
*/
26+
27+
#include <sys/types.h>
28+
29+
#include "buffer.h"
30+
31+
typedef struct buffer
32+
{
33+
char *buf;
34+
ssize_t size;
35+
ssize_t hwm;
36+
ssize_t ridx;
37+
ssize_t widx;
38+
int empty;
39+
} buffer_t;
40+
41+
/* the buffer is organised internally as follows:
42+
*
43+
* * There are b->size bytes in the buffer.
44+
*
45+
* * Bytes are at offsets 0 to b->size-1
46+
*
47+
* * b->ridx points to the first readable byte
48+
*
49+
* * b->widx points to the first empty space
50+
*
51+
* * b->ridx < b->widx indicates a non-wrapped buffer:
52+
*
53+
* 0 ridx widx size
54+
* | | | |
55+
* V V V V
56+
* ........XXXXXXXXX................
57+
*
58+
* * b->ridx > b->widx indicates a wrapped buffer:
59+
*
60+
* 0 widx ridx size
61+
* | | | |
62+
* V V V V
63+
* XXXXXXXX.........XXXXXXXXXXXXXXXX
64+
*
65+
* * b->ridx == b->widx indicates a FULL buffer:
66+
*
67+
* * b->ridx == b->widx indicates a wrapped buffer:
68+
*
69+
* 0 widx == ridx size
70+
* | | |
71+
* V V V
72+
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
73+
*
74+
* An empty buffer is indicated by empty=1
75+
*
76+
*/
77+
78+
buffer_t *
79+
bufNew (ssize_t size, ssize_t hwm)
80+
{
81+
buffer_t *b = calloc (1, sizeof (buffer_t));
82+
b->buf = calloc (1, size);
83+
b->size = size;
84+
b->hwm = hwm;
85+
b->empty = 1;
86+
return b;
87+
}
88+
89+
90+
void
91+
bufFree (buffer_t * b)
92+
{
93+
free (b->buf);
94+
free (b);
95+
}
96+
97+
/* get a maximal span to read. Returns 0 if buffer
98+
* is empty
99+
*/
100+
ssize_t
101+
bufGetReadSpan (buffer_t * b, void **addr)
102+
{
103+
if (b->empty)
104+
{
105+
*addr = NULL;
106+
return 0;
107+
}
108+
*addr = &(b->buf[b->ridx]);
109+
ssize_t len = b->widx - b->ridx;
110+
if (len <= 0)
111+
len = b->size - b->ridx;
112+
return len;
113+
}
114+
115+
/* get a maximal span to write. Returns 0 id buffer is full
116+
*/
117+
ssize_t
118+
bufGetWriteSpan (buffer_t * b, void **addr)
119+
{
120+
if (b->empty)
121+
{
122+
*addr = b->buf;
123+
b->ridx = 0;
124+
b->widx = 0;
125+
return b->size;
126+
}
127+
if (b->ridx == b->widx)
128+
{
129+
*addr = NULL;
130+
return 0;
131+
}
132+
*addr = &(b->buf[b->widx]);
133+
ssize_t len = b->ridx - b->widx;
134+
if (len <= 0)
135+
len = b->size - b->widx;
136+
return len;
137+
}
138+
139+
/* mark size bytes as read */
140+
void
141+
bufDoneRead (buffer_t * b, ssize_t size)
142+
{
143+
while (!b->empty && (size > 0))
144+
{
145+
/* empty can't occur here, so equal pointers means full */
146+
ssize_t len = b->widx - b->ridx;
147+
if (len <= 0)
148+
len = b->size - b->ridx;
149+
150+
/* len is the number of bytes in one read span */
151+
if (len > size)
152+
len = size;
153+
154+
b->ridx += len;
155+
if (b->ridx >= b->size)
156+
b->ridx = 0;
157+
158+
if (b->ridx == b->widx)
159+
{
160+
b->ridx = 0;
161+
b->widx = 0;
162+
b->empty = 1;
163+
}
164+
165+
size -= len;
166+
}
167+
}
168+
169+
/* mark size bytes as written */
170+
void
171+
bufDoneWrite (buffer_t * b, ssize_t size)
172+
{
173+
while ((b->empty || (b->ridx != b->widx)) && (size > 0))
174+
{
175+
/* full can't occur here, so equal pointers means empty */
176+
ssize_t len = b->ridx - b->widx;
177+
if (len <= 0)
178+
len = b->size - b->widx;
179+
180+
/* len is the number of bytes in one write span */
181+
if (len > size)
182+
len = size;
183+
184+
b->widx += len;
185+
if (b->widx >= b->size)
186+
b->widx = 0;
187+
188+
/* it can't be empty as we've written at least one byte */
189+
b->empty = 0;
190+
191+
size -= len;
192+
}
193+
}
194+
195+
int
196+
bufIsEmpty (buffer_t * b)
197+
{
198+
return b->empty;
199+
}
200+
201+
int
202+
bufIsFull (buffer_t * b)
203+
{
204+
return !b->empty && (b->ridx == b->widx);
205+
}
206+
207+
int
208+
bufIsOverHWM (buffer_t * b)
209+
{
210+
return bufGetCount (b) > b->hwm;
211+
}
212+
213+
ssize_t
214+
bufGetFree (buffer_t * b)
215+
{
216+
return b->size - bufGetCount (b);
217+
}
218+
219+
ssize_t
220+
bufGetCount (buffer_t * b)
221+
{
222+
if (b->empty)
223+
return 0;
224+
return b->widx - b->ridx + ((b->ridx < b->widx) ? 0 : b->size);
225+
}

buffer.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2016 Wrymouth Innovation Ltd
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a
7+
copy of this software and associated documentation files (the "Software"),
8+
to deal in the Software without restriction, including without limitation
9+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
10+
and/or sell copies of the Software, and to permit persons to whom the
11+
Software is furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included
14+
in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19+
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22+
OTHER DEALINGS IN THE SOFTWARE.
23+
*/
24+
25+
#ifndef __TLSPROXY_BUFFERS_H
26+
#define __TLSPROXY_BUFFERS_H
27+
28+
#include <stdlib.h>
29+
#include <sys/types.h>
30+
31+
typedef struct buffer buffer_t;
32+
33+
buffer_t *bufNew (ssize_t size, ssize_t hwm);
34+
void bufFree (buffer_t * b);
35+
ssize_t bufGetReadSpan (buffer_t * b, void **addr);
36+
ssize_t bufGetWriteSpan (buffer_t * b, void **addr);
37+
void bufDoneRead (buffer_t * b, ssize_t size);
38+
void bufDoneWrite (buffer_t * b, ssize_t size);
39+
int bufIsEmpty (buffer_t * b);
40+
int bufIsFull (buffer_t * b);
41+
int bufIsOverHWM (buffer_t * b);
42+
ssize_t bufGetFree (buffer_t * b);
43+
ssize_t bufGetCount (buffer_t * b);
44+
45+
#endif

configure.ac

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ AC_CHECK_SIZEOF(unsigned long long int)
121121
AC_STRUCT_DIRENT_D_TYPE
122122
AC_CHECK_FUNCS([llseek alarm gethostbyname inet_ntoa memset socket strerror strstr mkstemp fdatasync])
123123
HAVE_FL_PH=no
124+
124125
AC_CHECK_FUNC(fallocate,
125126
[
126127
AC_CHECK_HEADERS([linux/falloc.h])
@@ -211,6 +212,7 @@ dnl fi
211212

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

215217
AC_CHECK_HEADERS([winioctl.h], [], [],
216218
[#include <io.h>

0 commit comments

Comments
 (0)