1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
/* copy.c - twoftpd network copy routine
* Copyright (C) 2008 Bruce Guenter <bruce@untroubled.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <sysdeps.h>
#include <errno.h>
#include <unistd.h>
#include <net/socket.h>
#include "backend.h"
static int error_code(int err)
{
return (err == EPIPE
|| err == ECONNRESET)
? 2
: -1;
}
/* Return value:
* 0 if the iobuf was ready
* 1 for timeout
* 2 for an interrupt
*/
static int pollit(int fd, int events, int timeout)
{
iopoll_fd pfd[2];
pfd[0].fd = 0;
pfd[0].events = IOPOLL_READ;
pfd[0].revents = 0;
pfd[1].fd = fd;
pfd[1].events = events;
pfd[1].revents = 0;
switch (iopoll_restart(pfd, 2, timeout)) {
case -1:
return -1;
case 0:
return 1;
default:
if (pfd[0].revents)
return 2;
return 0;
}
}
/* Return values for following routines:
* -1 for a system error
* 0 for a successful transfer
* 1 for timeout
* 2 for interrupted transfer
*/
int netwrite(int out, const char* optr, unsigned long ocount, int timeout)
{
int result;
ssize_t wr;
while (ocount > 0) {
if ((result = pollit(out, IOPOLL_WRITE, timeout)) != 0)
return result;
if ((wr = write(out, optr, ocount)) == -1)
return error_code(errno);
if (wr == 0) {
errno = EIO;
return -1;
}
ocount -= wr;
optr += wr;
}
return 0;
}
static int copy_xlate(int in, int out, int timeout,
unsigned long (*xlate)(char* out,
const char* in,
unsigned long inlen),
unsigned long* bytes_in,
unsigned long* bytes_out)
{
char in_buf[iobuf_bufsize];
char out_buf[sizeof in_buf * 2];
char* optr;
ssize_t icount;
ssize_t ocount;
int result;
*bytes_in = 0;
*bytes_out = 0;
for (;;) {
if ((result = pollit(in, IOPOLL_READ, timeout)) != 0)
return result;
if ((icount = read(in, in_buf, sizeof in_buf)) == -1)
return -1;
if (icount == 0)
return 0;
*bytes_in += icount;
if (xlate) {
optr = out_buf;
ocount = xlate(out_buf, in_buf, icount);
}
else {
optr = in_buf;
ocount = icount;
}
if ((result = netwrite(out, optr, ocount, timeout)) != 0)
return result;
*bytes_out += ocount;
}
return 0;
}
int copy_xlate_close(int in, int out, int timeout,
unsigned long (*xlate)(char*, const char*, unsigned long),
unsigned long* bytes_in,
unsigned long* bytes_out)
{
int status;
status = copy_xlate(in, out, timeout, xlate, bytes_in, bytes_out);
/* Three possible cases here: in is at EOF, an error occurred while
* reading in, or writing is completed. In all cases we can ignore
* errors during closing in. */
close(in);
/* The output might be a socket that needs to be uncorked. If the
* output is a file, this is harmless. */
socket_uncork(out);
if (close(out) != 0)
if (status == 0)
status = error_code(out);
return status;
}
|