Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revision for warnings and code structure #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ btls/
/*.dylib
/*.a
/*.pc
TAGS
33 changes: 17 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
OBJ = btls.o iol.o utils.o
SRC=src/btls.c src/iol.c src/utils.c
OBJ = $(SRC:%.c=%.o)
LIBNAME = libbtls
PKGCONFNAME = btls.pc

MAJOR_VERSION = $(shell grep BTLS_MAJOR btls.h | awk '{print $$3}')
MINOR_VERSION = $(shell grep BTLS_MINOR btls.h | awk '{print $$3}')
PATCH_VERSION = $(shell grep BTLS_PATCH btls.h | awk '{print $$3}')
SONAME = $(shell grep BTLS_SONAME btls.h | awk '{print $$3}')
MAJOR_VERSION = $(shell grep BTLS_MAJOR include/btls.h | awk '{print $$3}')
MINOR_VERSION = $(shell grep BTLS_MINOR include/btls.h | awk '{print $$3}')
PATCH_VERSION = $(shell grep BTLS_PATCH include/btls.h | awk '{print $$3}')
SONAME = $(shell grep BTLS_SONAME include/btls.h | awk '{print $$3}')

# Installation related variables and target
PREFIX ?= /usr/local
Expand All @@ -25,8 +26,8 @@ CC := $(shell sh -c 'type $(CC) >/dev/null 2>/dev/null && echo $(CC) || echo gcc
OPTIMIZATION ?= -O3
WARNINGS = -Wall -W -Wstrict-prototypes -Wwrite-strings
DEBUG_FLAGS ?= -g -ggdb
REAL_CFLAGS = $(OPTIMIZATION) -fPIC $(CFLAGS) $(WARNINGS) $(DEBUG_FLAGS) $(ARCH) -I$(LIBRESSL_INCLUDE_PATH)
REAL_LDFLAGS = $(LDFLAGS) $(ARCH) -L$(LIBRESSL_LIBRARY_PATH) -lcrypto -lssl -ltls -ldill
REAL_CFLAGS = $(OPTIMIZATION) -I./include -I/usr/local/include -fPIC $(CFLAGS) $(WARNINGS) $(DEBUG_FLAGS) $(ARCH) -I$(LIBRESSL_INCLUDE_PATH)
REAL_LDFLAGS = $(LDFLAGS) $(ARCH) -L$(LIBRESSL_LIBRARY_PATH) -L/usr/local/lib -lcrypto -lssl -ltls -ldill

DYLIBSUFFIX = so
STLIBSUFFIX = a
Expand All @@ -49,9 +50,9 @@ endif
all: $(DYLIBNAME) $(STLIBNAME) $(PKGCONFNAME)

# Deps (use make dep to generate this)
btls.o: btls.c btls.h iol.h utils.h
iol.o: iol.c iol.h utils.h
utils.o: utils.c utils.h
btls.o: src/btls.c include/btls.h include/iol.h include/utils.h
iol.o: src/iol.c include/iol.h include/utils.h
utils.o: src/utils.c include/utils.h

$(DYLIBNAME): $(OBJ)
$(DYLIB_MAKE_CMD) $(OBJ)
Expand All @@ -65,18 +66,18 @@ static: $(STLIBNAME)
btls-%: %.o $(STLIBNAME)
$(CC) $(REAL_CFLAGS) -o $@ $(REAL_LDFLAGS) $< $(STLIBNAME)

.c.o:
$(CC) -std=gnu99 -pedantic -c $(REAL_CFLAGS) $<
%.o: %.c
$(CC) -c $(REAL_CFLAGS) -std=gnu99 -pedantic $< -o $@

clean:
rm -rf $(DYLIBNAME) $(STLIBNAME) $(PKGCONFNAME) *.o
rm -rf $(DYLIBNAME) $(STLIBNAME) $(PKGCONFNAME) $(OBJ)

dep:
$(CC) -MM *.c
$(CC) -MM $(SRC)

INSTALL ?= cp -a

$(PKGCONFNAME): btls.h
$(PKGCONFNAME): include/btls.h
@echo "Generating $@ for pkgconfig..."
@echo prefix=$(PREFIX) > $@
@echo exec_prefix=\$${prefix} >> $@
Expand All @@ -93,7 +94,7 @@ $(PKGCONFNAME): btls.h

install: $(DYLIBNAME) $(STLIBNAME) $(PKGCONFNAME)
mkdir -p $(INSTALL_INCLUDE_PATH) $(INSTALL_LIBRARY_PATH)
$(INSTALL) btls.h $(INSTALL_INCLUDE_PATH)
$(INSTALL) include/btls.h $(INSTALL_INCLUDE_PATH)
$(INSTALL) $(DYLIBNAME) $(INSTALL_LIBRARY_PATH)/$(DYLIB_MINOR_NAME)
cd $(INSTALL_LIBRARY_PATH) && ln -sf $(DYLIB_MINOR_NAME) $(DYLIBNAME)
$(INSTALL) $(STLIBNAME) $(INSTALL_LIBRARY_PATH)
Expand Down
File renamed without changes.
3 changes: 1 addition & 2 deletions iol.h → include/iol.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ struct iol_slice {
struct iolist oldlast;
};

void iol_slice_init(struct iol_slice *self, struct iolist *first,
struct iolist *last, size_t offset, size_t len);
void iol_slice_init(struct iol_slice *self, struct iolist *first, size_t offset, size_t len);
void iol_slice_term(struct iol_slice *self);

void iol_copy(struct iolist *first, uint8_t *dst);
Expand Down
File renamed without changes.
28 changes: 13 additions & 15 deletions btls.c → src/btls.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,12 @@ extern const void *tcp_type;
extern const void *tcp_listener_type;
int tcp_fd(int s);

static int btls_init();
static int btls_init(void);
static struct tls_config *btls_configure(uint64_t flags, uint64_t ciphers,
struct btls_kp *kp, size_t kplen, struct btls_ca *ca, const char *alpn);
static int btls_conn_create(int s, struct tls *tls, struct tls_config *c,
const char *servername);
static int btls_listener_create(int s, struct tls *tls, struct tls_config *c,
const char *servername);
static int btls_listener_create(int s, struct tls *tls, struct tls_config *c);
static int btls_wait_close(struct tls *tls, int fd, int64_t deadline);

struct btls_rxbuf {
Expand All @@ -59,7 +58,7 @@ struct btls_rxbuf {
/* TLS connection socket */
/******************************************************************************/

dsock_unique_id(btls_conn_type);
dsock_unique_id(btls_conn_type)

static void *btls_conn_hquery(struct hvfs *hvfs, const void *type);
static void btls_conn_hclose(struct hvfs *hvfs);
Expand Down Expand Up @@ -131,7 +130,7 @@ static ssize_t btls_conn_get(struct btls_conn *obj, void *buf, size_t len,
size_t pos = 0;
if(btls_conn_handshake(obj, deadline)) return -1;
while(1) {
ssize_t rc = tls_read(obj->tls, buf + pos, len);
ssize_t rc = tls_read(obj->tls, (char *)buf + pos, len);
if(rc == TLS_WANT_POLLIN)
rc = fdin(obj->fd, deadline);
else if(rc == TLS_WANT_POLLOUT)
Expand All @@ -141,7 +140,7 @@ static ssize_t btls_conn_get(struct btls_conn *obj, void *buf, size_t len,
else if(dsock_slow(rc == 0)) {
errno = ECONNRESET;
return -1;
} else if(dsock_fast(rc == len))
} else if(dsock_fast((size_t)rc == len))
return pos + rc;
else if(dsock_fast(rc > 0)) {
if(!block) return rc;
Expand Down Expand Up @@ -191,7 +190,7 @@ static ssize_t btls_conn_brecv(struct btls_conn *obj, void *buf, size_t len,
/* Use data from rxbuf. */
size_t remaining = rxbuf->len - rxbuf->pos;
size_t tocopy = remaining < len ? remaining : len;
memcpy(buf + pos, (char*)(rxbuf->data) + rxbuf->pos, tocopy);
memcpy((char *)buf + pos, (char*)(rxbuf->data) + rxbuf->pos, tocopy);
rxbuf->pos += tocopy;
pos += tocopy;
len -= tocopy;
Expand All @@ -200,7 +199,7 @@ static ssize_t btls_conn_brecv(struct btls_conn *obj, void *buf, size_t len,
/* If requested amount of data is large avoid the copy
and read it directly into user's buffer. */
if(len >= sizeof(rxbuf->data)) {
ssize_t sz = btls_conn_get(obj, buf + pos, len, 0, deadline);
ssize_t sz = btls_conn_get(obj, (char *)buf + pos, len, 0, deadline);
if(dsock_slow(sz < 0)) return -1;
read += sz;
return read;
Expand Down Expand Up @@ -305,7 +304,7 @@ int btls_attach_client_kp(int s, uint64_t flags, uint64_t ciphers,
/* TLS listener socket */
/******************************************************************************/

dsock_unique_id(btls_listener_type);
dsock_unique_id(btls_listener_type)

static void *btls_listener_hquery(struct hvfs *hvfs, const void *type);
static void btls_listener_hclose(struct hvfs *hvfs);
Expand Down Expand Up @@ -334,8 +333,7 @@ static void btls_listener_hclose(struct hvfs *hvfs) {
free(obj);
}

static int btls_listener_create(int s, struct tls *tls, struct tls_config *c,
const char *servername) {
static int btls_listener_create(int s, struct tls *tls, struct tls_config *c) {
/* Check whether underlying socket is a TCP listener socket. */
if(dsock_slow(!hquery(s, tcp_listener_type))) return -1;
/* Create the object. */
Expand Down Expand Up @@ -378,7 +376,7 @@ int btls_attach_server(int s, uint64_t flags, uint64_t ciphers,
if(flags & BTLS_CLEAR_KEYS)
tls_config_clear_keys(c);
/* Create the btls object. */
int h = btls_listener_create(s, t, c, NULL);
int h = btls_listener_create(s, t, c);
if(h == -1) goto error;
return h;
error:
Expand Down Expand Up @@ -414,7 +412,7 @@ int btls_attach_accept(int s, int l) {
/* TLS common functions */
/******************************************************************************/

static int btls_init() {
static int btls_init(void) {
static int init = 0;
if(dsock_fast(init))
return 0;
Expand Down Expand Up @@ -490,7 +488,7 @@ int btls_detach(int s, int64_t deadline) {
void btls_reset(int s) {
struct btls_conn *c = hquery(s, btls_conn_type);
if(dsock_slow(!c)) {errno = ENOTSUP; return;}
return tls_reset(c->tls);
tls_reset(c->tls);
}

int btls_handshake(int s, int64_t deadline) {
Expand Down Expand Up @@ -707,7 +705,7 @@ static struct tls_config *btls_configure(uint64_t flags, uint64_t ciphers,
if(ciphers & BTLS_CIPHERS_AES256_SHA)
p = strappend(p, cl, "AES256-SHA:");
if(p != cl) {
dsock_assert(p - cl <= sizeof(cl));
dsock_assert((unsigned long)(p - cl) <= sizeof(cl));
*(p - 1) = '\0'; /* remove last ':' */
}
tls_config_set_ciphers(c, cl);
Expand Down
7 changes: 3 additions & 4 deletions iol.c → src/iol.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ int iol_check(struct iolist *first, struct iolist *last,
size_t *nbufs, size_t *nbytes) {
if(dsock_slow(!first || !last || last->iol_next)) {
errno = EINVAL; return -1;}
size_t nbf = 0, nbt = 0, res = 0;
size_t nbf = 0, nbt = 0;
struct iolist *it;
for(it = first; it; it = it->iol_next) {
if(dsock_slow(it->iol_rsvd || (!it->iol_next && it != last)))
Expand Down Expand Up @@ -68,16 +68,15 @@ void iol_copy(struct iolist *first, uint8_t *dst) {
}
}

void iol_slice_init(struct iol_slice *self, struct iolist *first,
struct iolist *last, size_t offset, size_t len) {
void iol_slice_init(struct iol_slice *self, struct iolist *first, size_t offset, size_t len) {
struct iolist *it = first;
while(offset >= it->iol_len) {
offset -= it->iol_len;
it = it->iol_next;
dsock_assert(it);
}
self->first = *it;
self->first.iol_base += offset;
self->first.iol_base = (char *)self->first.iol_base + offset;
self->first.iol_len -= offset;
self->first.iol_rsvd = 0;
it = &self->first;
Expand Down
File renamed without changes.