Skip to content

Commit

Permalink
Fix no-writev fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
mkj committed May 2, 2015
1 parent fee3205 commit 19e1afb
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 61 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The majority of code is written by Matt Johnston, under the license below.
Portions of the client-mode work are (c) 2004 Mihnea Stoenescu, under the
same license:

Copyright (c) 2002-2014 Matt Johnston
Copyright (c) 2002-2015 Matt Johnston
Portions copyright (c) 2004 Mihnea Stoenescu
All rights reserved.

Expand Down
31 changes: 0 additions & 31 deletions circbuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,6 @@ unsigned int cbuf_getavail(circbuffer * cbuf) {

}

unsigned int cbuf_readlen(circbuffer *cbuf) {

dropbear_assert(((2*cbuf->size)+cbuf->writepos-cbuf->readpos)%cbuf->size == cbuf->used%cbuf->size);
dropbear_assert(((2*cbuf->size)+cbuf->readpos-cbuf->writepos)%cbuf->size == (cbuf->size-cbuf->used)%cbuf->size);

if (cbuf->used == 0) {
TRACE(("cbuf_readlen: unused buffer"))
return 0;
}

if (cbuf->readpos < cbuf->writepos) {
return cbuf->writepos - cbuf->readpos;
}

return cbuf->size - cbuf->readpos;
}

unsigned int cbuf_writelen(circbuffer *cbuf) {

dropbear_assert(cbuf->used <= cbuf->size);
Expand All @@ -102,14 +85,6 @@ unsigned int cbuf_writelen(circbuffer *cbuf) {
return cbuf->size - cbuf->writepos;
}

unsigned char* cbuf_readptr(circbuffer *cbuf, unsigned int len) {
if (len > cbuf_readlen(cbuf)) {
dropbear_exit("Bad cbuf read");
}

return &cbuf->data[cbuf->readpos];
}

void cbuf_readptrs(circbuffer *cbuf,
unsigned char **p1, unsigned int *len1,
unsigned char **p2, unsigned int *len2) {
Expand Down Expand Up @@ -146,12 +121,6 @@ void cbuf_incrwrite(circbuffer *cbuf, unsigned int len) {


void cbuf_incrread(circbuffer *cbuf, unsigned int len) {
#if 0
if (len > cbuf_readlen(cbuf)) {
dropbear_exit("Bad cbuf read");
}
#endif

dropbear_assert(cbuf->used >= len);
cbuf->used -= len;
cbuf->readpos = (cbuf->readpos + len) % cbuf->size;
Expand Down
3 changes: 1 addition & 2 deletions circbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ void cbuf_free(circbuffer * cbuf);

unsigned int cbuf_getused(circbuffer * cbuf); /* how much data stored */
unsigned int cbuf_getavail(circbuffer * cbuf); /* how much we can write */
unsigned int cbuf_readlen(circbuffer *cbuf); /* max linear read len */
unsigned int cbuf_writelen(circbuffer *cbuf); /* max linear write len */

unsigned char* cbuf_readptr(circbuffer *cbuf, unsigned int len);
/* returns pointers to the two portions of the circular buffer that can be read */
void cbuf_readptrs(circbuffer *cbuf,
unsigned char **p1, unsigned int *len1,
unsigned char **p2, unsigned int *len2);
Expand Down
68 changes: 44 additions & 24 deletions common-channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -434,20 +434,44 @@ static void send_msg_channel_eof(struct Channel *channel) {
TRACE(("leave send_msg_channel_eof"))
}

/* Called to write data out to the local side of the channel.
Writes the circular buffer contents and also the "moredata" buffer
if not null. Will ignore EAGAIN */
static void writechannel(struct Channel* channel, int fd, circbuffer *cbuf,
#ifndef HAVE_WRITEV
static void writechannel_fallback(struct Channel* channel, int fd, circbuffer *cbuf,
const unsigned char *UNUSED(moredata), unsigned int *morelen) {

unsigned char *circ_p1, *circ_p2;
unsigned int circ_len1, circ_len2;
ssize_t written;

if (morelen) {
/* fallback doesn't consume moredata */
*morelen = 0;
}

/* Write the first portion of the circular buffer */
cbuf_readptrs(cbuf, &circ_p1, &circ_len1, &circ_p2, &circ_len2);
written = write(fd, circ_p1, circ_len1);
if (written < 0) {
if (errno != EINTR && errno != EAGAIN) {
TRACE(("channel IO write error fd %d %s", fd, strerror(errno)))
close_chan_fd(channel, fd, SHUT_WR);
}
} else {
cbuf_incrread(cbuf, written);
channel->recvdonelen += written;
}
}
#endif /* !HAVE_WRITEV */

#ifdef HAVE_WRITEV
static void writechannel_writev(struct Channel* channel, int fd, circbuffer *cbuf,
const unsigned char *moredata, unsigned int *morelen) {

struct iovec iov[3];
unsigned char *circ_p1, *circ_p2;
unsigned int circ_len1, circ_len2;
int io_count = 0;

int written;

TRACE(("enter writechannel fd %d", fd))
ssize_t written;

cbuf_readptrs(cbuf, &circ_p1, &circ_len1, &circ_p2, &circ_len2);

Expand Down Expand Up @@ -502,24 +526,19 @@ static void writechannel(struct Channel* channel, int fd, circbuffer *cbuf,
channel->recvdonelen += written;
}

#if 0

maxlen = cbuf_readlen(cbuf);

/* Write the data out */
len = write(fd, cbuf_readptr(cbuf, maxlen), maxlen);
if (len <= 0) {
TRACE(("errno %d len %d", errno, len))
if (len < 0 && errno != EINTR) {
close_chan_fd(channel, fd, SHUT_WR);
}
TRACE(("leave writechannel: len <= 0"))
return;
}
TRACE(("writechannel wrote %d", len))
}
#endif /* HAVE_WRITEV */

cbuf_incrread(cbuf, len);
channel->recvdonelen += len;
/* Called to write data out to the local side of the channel.
Writes the circular buffer contents and also the "moredata" buffer
if not null. Will ignore EAGAIN */
static void writechannel(struct Channel* channel, int fd, circbuffer *cbuf,
const unsigned char *moredata, unsigned int *morelen) {
TRACE(("enter writechannel fd %d", fd))
#ifdef HAVE_WRITEV
writechannel_writev(channel, fd, cbuf, moredata, morelen);
#else
writechannel_fallback(channel, fd, cbuf, moredata, morelen);
#endif

/* Window adjust handling */
Expand All @@ -537,6 +556,7 @@ static void writechannel(struct Channel* channel, int fd, circbuffer *cbuf,
TRACE(("leave writechannel"))
}


/* Set the file descriptors for the main select in session.c
* This avoid channels which don't have any window available, are closed, etc*/
void setchannelfds(fd_set *readfds, fd_set *writefds, int allow_reads) {
Expand Down
6 changes: 3 additions & 3 deletions common-session.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Dropbear - a SSH2 server
*
* Copyright (c) 2002,2003 Matt Johnston
* Copyright (c) Matt Johnston
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down Expand Up @@ -280,7 +280,7 @@ void session_cleanup() {
return;
}

/* Beware of changing order of functions here. */
/* BEWARE of changing order of functions here. */

/* Must be before extra_session_cleanup() */
chancleanup();
Expand All @@ -289,7 +289,7 @@ void session_cleanup() {
ses.extra_session_cleanup();
}

/* After these are freed most functions will exit */
/* After these are freed most functions will fail */
#ifdef DROPBEAR_CLEANUP
/* listeners call cleanup functions, this should occur before
other session state is freed. */
Expand Down
6 changes: 6 additions & 0 deletions packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ void write_packet() {
/* 50 is somewhat arbitrary */
unsigned int iov_count = 50;
struct iovec iov[50];
#else
int len;
buffer* writebuf;
int packet_type;
#endif

TRACE2(("enter write_packet"))
Expand Down Expand Up @@ -97,6 +101,8 @@ void write_packet() {
* a cleartext packet_type indicator */
packet_type = writebuf->data[writebuf->len-1];
len = writebuf->len - 1 - writebuf->pos;
TRACE2(("write_packet type %d len %d/%d", packet_type,
len, writebuf->len-1))
dropbear_assert(len > 0);
/* Try to write as much as possible */
written = write(ses.sock_out, buf_getptr(writebuf, len), len);
Expand Down

0 comments on commit 19e1afb

Please sign in to comment.