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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
/* $Id: xioread.c,v 1.24 2005/09/04 09:40:45 gerhard Exp $ */
/* Copyright Gerhard Rieger 2001-2005 */
/* Published under the GNU General Public License V.2, see file COPYING */
/* this is the source of the extended read function */
#include "xiosysincludes.h"
#include "xioopen.h"
#include "xio-termios.h"
#include "xio-readline.h"
#include "xio-openssl.h"
/* xioread() performs read() or recvfrom()
If result is < 0, errno is valid */
ssize_t xioread(xiofile_t *file, void *buff, size_t bufsiz) {
ssize_t bytes;
#if WITH_IP6 && 0
int nexthead;
#endif
struct single *pipe;
int _errno;
if (file->tag == XIO_TAG_INVALID) {
Error("xioread(): invalid file descriptor");
errno = EINVAL;
return -1;
}
if (file->tag == XIO_TAG_DUAL) {
pipe = &file->dual.stream[0]->stream;
if (pipe->tag == XIO_TAG_INVALID) {
Error("xioread(): invalid file descriptor");
errno = EINVAL;
return -1;
}
} else {
pipe = &file->stream;
}
if (pipe->readbytes) {
if (pipe->actbytes == 0) {
return 0; /* EOF by count */
}
if (pipe->actbytes < bufsiz) {
bufsiz = pipe->actbytes;
}
}
if (pipe->dtype == DATA_STREAM || pipe->dtype == DATA_PIPE ||
pipe->dtype == DATA_2PIPE) {
do {
bytes = Read(pipe->fd, buff, bufsiz);
} while (bytes < 0 && errno == EINTR);
if (bytes < 0) {
_errno = errno;
switch (_errno) {
#if 1
case EPIPE: case ECONNRESET:
Warn4("read(%d, %p, "F_Zu"): %s",
pipe->fd, buff, bufsiz, strerror(_errno));
break;
#endif
default:
Error4("read(%d, %p, "F_Zu"): %s",
pipe->fd, buff, bufsiz, strerror(_errno));
}
errno = _errno;
return -1;
}
} else if (pipe->dtype == DATA_PTY) {
do {
bytes = Read(pipe->fd, buff, bufsiz);
} while (bytes < 0 && errno == EINTR);
if (bytes < 0) {
_errno = errno;
if (_errno == EIO) {
Notice4("read(%d, %p, "F_Zu"): %s (probably PTY closed)",
pipe->fd, buff, bufsiz, strerror(_errno));
return 0;
} else {
Error4("read(%d, %p, "F_Zu"): %s",
pipe->fd, buff, bufsiz, strerror(_errno));
}
errno = _errno;
return -1;
}
#if WITH_READLINE
} else if (pipe->dtype == DATA_READLINE) {
if ((bytes = xioread_readline(pipe, buff, bufsiz)) < 0) {
return -1;
}
#endif /* WITH_READLINE */
#if WITH_OPENSSL
} else if (pipe->dtype == DATA_OPENSSL) {
/* this function prints its error messages */
if ((bytes = xioread_openssl(pipe, buff, bufsiz)) < 0) {
return -1;
}
#endif /* WITH_OPENSSL */
} else {
#if WITH_RAWIP
union sockaddr_union from;
socklen_t fromlen = sizeof(from);
do {
bytes =
Recvfrom(pipe->fd, buff, bufsiz, 0, &from.soa, &fromlen);
} while (bytes < 0 && errno == EINTR);
if (bytes < 0) {
char infobuff[256];
_errno = errno;
Error6("recvfrom(%d, %p, "F_Zu", 0, %s, "F_socklen"): %s",
pipe->fd, buff, bufsiz,
sockaddr_info(&from.soa, infobuff, sizeof(infobuff)),
&fromlen, strerror(errno));
errno = _errno;
return -1;
}
switch(from.soa.sa_family) {
int headlen;
#if WITH_IP4
case AF_INET:
headlen = 4*((struct ip *)buff)->ip_hl;
if (headlen > bytes) {
Warn1("xioread(%d, ...)/IP4: short packet", pipe->fd);
bytes = 0;
} else {
memmove(buff, ((char *)buff)+headlen, bytes-headlen);
bytes -= headlen;
}
break;
#endif
#if WITH_IP6
case AF_INET6: /* does not seem to include header... */
break;
#endif
default:
/* do nothing, for now */
break;
}
#if 0
if (fromlen != pipe->fd[0].salen) {
Debug("recvfrom(): wrong peer address length, ignoring packet");
continue;
}
if (memcmp(&from, &pipe->fd[0].peersa.sa, fromlen)) {
Debug("recvfrom(): other peer address, ignoring packet");
Debug16("peer: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
pipe->fd[0].peersa.space[0],
pipe->fd[0].peersa.space[1],
pipe->fd[0].peersa.space[2],
pipe->fd[0].peersa.space[3],
pipe->fd[0].peersa.space[4],
pipe->fd[0].peersa.space[5],
pipe->fd[0].peersa.space[6],
pipe->fd[0].peersa.space[7],
pipe->fd[0].peersa.space[8],
pipe->fd[0].peersa.space[9],
pipe->fd[0].peersa.space[10],
pipe->fd[0].peersa.space[11],
pipe->fd[0].peersa.space[12],
pipe->fd[0].peersa.space[13],
pipe->fd[0].peersa.space[14],
pipe->fd[0].peersa.space[15]);
Debug16("from: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
from.space[0], from.space[1],
from.space[2], from.space[3],
from.space[4], from.space[5],
from.space[6], from.space[7],
from.space[8], from.space[9],
from.space[10], from.space[11],
from.space[12], from.space[13],
from.space[14], from.space[15]);
continue;
}
#endif
#else /* !WITH_RAWIP */
Fatal("address requires raw sockets, but they are not compiled in");
return -1;
#endif /* !WITH_RAWIP */
}
pipe->actbytes -= bytes;
return bytes;
}
|