Remove connio code from the header files. Good grief, it's now debuggable!

This commit is contained in:
alexhudson
2007-09-03 10:45:36 +00:00
parent cdd1000b66
commit e050a05905
7 changed files with 273 additions and 372 deletions
+1
View File
@@ -5,6 +5,7 @@ bongoinclude_HEADERS = \
include/bongo-config.h \
include/calcmd.h \
include/connio.h \
include/connio-trace.h \
include/bongocal.h \
include/bongocheck.h \
include/bongojson.h \
+46
View File
@@ -0,0 +1,46 @@
#if !defined(_BONGO_CONN_TRACE)
#define CONN_TRACE_GET_FLAGS() 0
#define CONN_TRACE_SET_FLAGS(f)
#define CONN_TRACE_INIT(p, n)
#define CONN_TRACE_SHUTDOWN()
#define CONN_TRACE_CREATE_DESTINATION(t) NULL
#define CONN_TRACE_FREE_DESTINATION(d)
#define CONN_TRACE_BEGIN(c, t, d)
#define CONN_TRACE_END(c)
#define CONN_TRACE_EVENT(c, t)
#define CONN_TRACE_ERROR(c, e, l)
#define CONN_TRACE_DATA(c, t, b, l)
#define CONN_TRACE_DATA_AND_ERROR(c, t, b, l, e)
#else
void ConnTraceSetFlags(unsigned long flags);
unsigned long ConnTraceGetFlags(void);
void ConnTraceInit(char *path, char *name);
void ConnTraceShutdown(void);
TraceDestination *ConnTraceCreatePersistentDestination(unsigned char type);
void ConnTraceFreeDestination(TraceDestination *destination);
void ConnTraceBegin(Connection *c, unsigned long type, TraceDestination *destination);
void ConnTraceEnd(Connection *c);
void ConnTraceEvent(Connection *c, unsigned long event, char *buffer, long len);
#define CONN_TRACE_GET_FLAGS() ConnTraceGetFlags()
#define CONN_TRACE_SET_FLAGS(f) ConnTraceSetFlags(f)
#define CONN_TRACE_INIT(p, n) ConnTraceInit(p, n)
#define CONN_TRACE_SHUTDOWN() ConnTraceShutdown()
#define CONN_TRACE_CREATE_DESTINATION(t) ConnTraceCreatePersistentDestination(t)
#define CONN_TRACE_FREE_DESTINATION(d) ConnTraceFreeDestination(d)
#define CONN_TRACE_BEGIN(c, t, d) ConnTraceBegin((c), (t), (d))
#define CONN_TRACE_END(c) ConnTraceEnd((c))
#define CONN_TRACE_EVENT(c, t) ConnTraceEvent((c), (t), NULL, 0)
#define CONN_TRACE_ERROR(c, e, l) ConnTraceEvent((c), CONN_TRACE_EVENT_ERROR, (e), (l))
#define CONN_TRACE_DATA(c, t, b, l) ConnTraceEvent((c), (t), (b), (l))
#define CONN_TRACE_DATA_AND_ERROR(c, t, b, l, e) if ((l) > 0) { \
ConnTraceEvent((c), (t), (b), (l)); \
} else { \
ConnTraceEvent((c), CONN_TRACE_EVENT_ERROR, (e), (l)); \
}
#endif
+9 -312
View File
@@ -82,30 +82,8 @@ typedef struct {
#define SSL_DISABLE_EMPTY_FRAGMENTS (1 << 4)
#define SSL_DONT_INSERT_EMPTY_FRAGMENTS (1 << 5)
#define CHOP_NEWLINE(s) \
{ unsigned char *p; p = strchr((s), 0x0A); \
if (p) { \
*p = '\0'; \
} \
p = strrchr((s), 0x0D); \
if (p) { \
*p = '\0'; \
} \
}
#define SET_POINTER_TO_VALUE(p,s) \
{ (p) = (s); \
while(isspace(*(p))) { \
(p)++; \
} \
if ((*(p) == '=') || (*(p) == ':')) { \
(p)++; \
} \
while(isspace(*(p))) { \
(p)++; \
} \
}
void CHOP_NEWLINE(unsigned char *s);
void SET_POINTER_TO_VALUE(unsigned char *p, unsigned char *s); // FIXME: Unused?
#if defined(_BONGO_LINUX) || defined(S390RH) || defined(SOLARIS)
@@ -134,287 +112,12 @@ typedef struct {
#define IPgetsockname(s, a, l) getsockname((s), (a), (l))
#define IPgetpeername(s, a, l) getpeername((s), (a), (l))
#define CONN_TCP_READ(c, b, l, r) \
{ \
struct pollfd pfd; \
do { \
pfd.fd = (int)(c)->socket; \
pfd.events = POLLIN; \
(r) = poll(&pfd, 1, (c)->receive.timeOut * 1000); \
if ((r) > 0) { \
if ((pfd.revents & (POLLIN | POLLPRI))) { \
do { \
(r) = IPrecv((c)->socket, b, l, 0); \
if ((r) >= 0) { \
CONN_TRACE_DATA((c), CONN_TRACE_EVENT_READ, (b), (r)); \
break; \
} else if (errno == EINTR) { \
continue; \
} \
CONN_TRACE_ERROR((c), "RECV", (r)); \
(r) = -1; \
break; \
} while (TRUE); \
break; \
} else if ((pfd.revents & (POLLERR | POLLHUP | POLLNVAL))) { \
CONN_TRACE_ERROR((c), "POLL EVENT", (r)); \
(r) = -1; \
break; \
} \
} \
if (errno == EINTR) { \
continue; \
} \
CONN_TRACE_ERROR((c), "POLL", (r)); \
(r) = -1; \
break; \
} while (TRUE); \
}
#define CONN_TCP_WRITE(c, b, l, r) \
{ \
do { \
(r) = IPsend((c)->socket, b, l, MSG_NOSIGNAL); \
if ((r) >= 0) { \
CONN_TRACE_DATA((c), CONN_TRACE_EVENT_WRITE, (b), (r)); \
break; \
} else if (errno == EINTR) { \
continue; \
} \
CONN_TRACE_ERROR((c), "POLL", (r)); \
(r) = -1; \
break; \
} while (TRUE); \
}
#elif defined(LIBC) || defined(NETWARE) || defined(WIN32)
#ifdef WIN32
typedef int socklen_t;
typedef unsigned int in_addr_t;
#define ETIMEDOUT WSAETIMEDOUT
#define ECONNREFUSED WSAECONNREFUSED
#define ENETUNREACH WSAENETUNREACH
#endif
#define ConnSockOpt(c, o, v, p, l) setsockopt((c)->socket, (o), (v), (p), (l))
#define IPSOCKET SOCKET
#define IPInit() {WSADATA data; WSAStartup(MAKEWORD(1,1), &data);}
#define IPCleanup() WSACleanup();
#define IPsocket(domain, type, protocol) socket(domain, type, protocol)
#define IPaccept(s, addr, addrlen) accept(s, addr, addrlen)
#define IPlisten(sock, backlog) listen(sock, backlog)
#define IPbind(sock, addr, addrlen) bind(sock, addr, addrlen)
#define IPconnect(sock, addr, addrlen) connect(sock, addr, addrlen)
#define IPrecv(sock, buf, len, flags) recv(sock, buf, len, flags)
#define IPsend(sock, buf, len, flags) send(sock, buf, len, flags)
#define IPclose(sock) closesocket(sock)
#define IPshutdown(s, how) shutdown(s, how)
#define IPgetsockname(s, addr, addrlen) getsockname(s, addr, addrlen)
#define IPgetpeername(s, addr, addrlen) getpeername(s, addr, addrlen)
#define IPselect(nfds, rfds, wfds, efds, t) select(nfds, rfds, wfds, efds, t)
#define CONN_TCP_READ(c, b, l, r) \
{ \
fd_set rfds; \
struct timeval timeout; \
FD_ZERO(&rfds); \
FD_SET((c)->socket, &rfds); \
timeout.tv_usec = 0; \
timeout.tv_sec = (c)->receive.timeOut; \
(r) = select(FD_SETSIZE, &rfds, NULL, NULL, &timeout); \
if ((r) > 0) { \
(r) = IPrecv((c)->socket, (b), (l), 0); \
CONN_TRACE_DATA_AND_ERROR((c), CONN_TRACE_EVENT_READ, (b), (r), "RECV"); \
} else { \
CONN_TRACE_ERROR((c), "SELECT", (r)); \
(r) = -1; \
} \
}
#define CONN_TCP_WRITE(c, b, l, r) \
(r) = IPsend((c)->socket, (b), (l), 0); \
CONN_TRACE_DATA_AND_ERROR((c), CONN_TRACE_EVENT_WRITE, (b), (r), "SEND");
#else
#error Connection management library not implemented on this platform.
#endif
#define CONN_TCP_SEND(c, b, l, r) \
{ \
if (!(c)->ssl.enable) { \
CONN_TCP_WRITE((c), (b), (l), (r)); \
} else { \
CONN_SSL_WRITE((c), (b), (l), (r)); \
} \
}
#define CONN_TCP_RECEIVE(c, b, l, r) \
{ \
if (!(c)->ssl.enable) { \
CONN_TCP_READ((c), (b), (l), (r)); \
} else { \
CONN_SSL_READ((c), (b), (l), (r)); \
} \
}
#define CONN_TCP_FLUSH(c, b, e, r) \
{ \
if ((b) < (e)) { \
register char *curPTR = (char *)(b); \
if (!(c)->ssl.enable) { \
while (curPTR < (e)) { \
CONN_TCP_WRITE((c), curPTR, (e) - curPTR, (r)); \
if ((r) > 0) { \
curPTR += (r); \
continue; \
} \
break; \
} \
} else { \
while (curPTR < (e)) { \
CONN_SSL_WRITE((c), curPTR, (e) - curPTR, (r)); \
if ((r) > 0) { \
curPTR += (r); \
continue; \
} \
break; \
} \
} \
if (curPTR == (e)) { \
(r) = (e) - (b); \
} else { \
(r) = -1; \
} \
} else { \
(r) = 0; \
} \
}
#define CONN_TCP_CLOSE(c) \
{ \
if ((c)->receive.buffer) { \
(c)->receive.remaining = CONN_TCP_MTU; \
} else { \
(c)->receive.remaining = 0; \
} \
(c)->receive.read = (c)->receive.write = (c)->receive.buffer; \
if ((c)->send.buffer) { \
ConnFlush(c); \
(c)->send.remaining = CONN_TCP_MTU; \
} else { \
(c)->send.remaining = 0; \
} \
(c)->send.read = (c)->send.write = (c)->send.buffer; \
if ((c)->ssl.enable) { \
gnutls_bye((c)->ssl.context, GNUTLS_SHUT_RDWR); \
gnutls_deinit((c)->ssl.context); \
(c)->ssl.context = NULL; \
(c)->ssl.enable = FALSE; \
} \
if ((c)->socket != -1) { \
IPshutdown((c)->socket, 2); \
IPclose((c)->socket); \
(c)->socket = -1; \
} \
}
#define CONN_SSL_NEW(c, s) \
{ \
(c)->ssl.context = __gnutls_new(s); \
if ((c)->ssl.context) \
(c)->ssl.enable = TRUE; \
} else { \
(c)->ssl.context = NULL; \
(c)->ssl.enable = FALSE; \
} \
}
#define CONN_SSL_FREE(c) \
{ \
if ((c)->ssl.enable) { \
gnutls_deinit((c)->ssl.context); \
(c)->ssl.context = NULL; \
(c)->ssl.enable = FALSE; \
} \
}
#define CONN_SSL_ACCEPT(c, s) \
{ \
(c)->ssl.context = __gnutls_new(s); \
if ((c)->ssl.context \
&& (gnutls_handshake((c)->ssl.context) == 0)) { \
(c)->ssl.enable = TRUE; \
} else { \
if ((c)->ssl.context) { \
gnutls_deinit((c)->ssl.context); \
(c)->ssl.context = NULL; \
} \
(c)->ssl.enable = FALSE; \
} \
}
#define CONN_SSL_CONNECT(c, s, r) \
{ \
(c)->ssl.context = __gnutls_new(s); \
if ((c)->ssl.context \
&& (gnutls_handshake((c)->ssl.context) == 0)) { \
(c)->ssl.enable = TRUE; \
} else { \
if ((c)->ssl.context) { \
gnutls_deinit((c)->ssl.context); \
(c)->ssl.context = NULL; \
} \
(c)->ssl.enable = FALSE; \
} \
}
#if !defined(_BONGO_CONN_TRACE)
#define CONN_TRACE_GET_FLAGS() 0
#define CONN_TRACE_SET_FLAGS(f)
#define CONN_TRACE_INIT(p, n)
#define CONN_TRACE_SHUTDOWN()
#define CONN_TRACE_CREATE_DESTINATION(t) NULL
#define CONN_TRACE_FREE_DESTINATION(d)
#define CONN_TRACE_BEGIN(c, t, d)
#define CONN_TRACE_END(c)
#define CONN_TRACE_EVENT(c, t)
#define CONN_TRACE_ERROR(c, e, l)
#define CONN_TRACE_DATA(c, t, b, l)
#define CONN_TRACE_DATA_AND_ERROR(c, t, b, l, e)
#else
#define CONN_TRACE_GET_FLAGS() ConnTraceGetFlags()
#define CONN_TRACE_SET_FLAGS(f) ConnTraceSetFlags(f)
#define CONN_TRACE_INIT(p, n) ConnTraceInit(p, n)
#define CONN_TRACE_SHUTDOWN() ConnTraceShutdown()
#define CONN_TRACE_CREATE_DESTINATION(t) ConnTraceCreatePersistentDestination(t)
#define CONN_TRACE_FREE_DESTINATION(d) ConnTraceFreeDestination(d)
#define CONN_TRACE_BEGIN(c, t, d) ConnTraceBegin((c), (t), (d))
#define CONN_TRACE_END(c) ConnTraceEnd((c))
#define CONN_TRACE_EVENT(c, t) ConnTraceEvent((c), (t), NULL, 0)
#define CONN_TRACE_ERROR(c, e, l) ConnTraceEvent((c), CONN_TRACE_EVENT_ERROR, (e), (l))
#define CONN_TRACE_DATA(c, t, b, l) ConnTraceEvent((c), (t), (b), (l))
#define CONN_TRACE_DATA_AND_ERROR(c, t, b, l, e) if ((l) > 0) { \
ConnTraceEvent((c), (t), (b), (l)); \
} else { \
ConnTraceEvent((c), CONN_TRACE_EVENT_ERROR, (e), (l)); \
}
#endif
#define CONN_SSL_READ(c, b, l, r) (r) = gnutls_record_recv((c)->ssl.context, (void *)(b), (l)); \
CONN_TRACE_DATA_AND_ERROR((c), CONN_TRACE_EVENT_READ, (b), (r), "SSL_READ");
#define CONN_SSL_WRITE(c, b, l, r) (r) = gnutls_record_send((c)->ssl.context, (void *)(b), (l)); \
CONN_TRACE_DATA_AND_ERROR((c), CONN_TRACE_EVENT_WRITE, (b), (r), "SSL_WRITE");
typedef struct _ConnectionBuffer {
char *read;
char *write;
@@ -523,6 +226,13 @@ typedef struct {
unsigned long initialized;
} AddressPool;
#include <connio-trace.h>
void ConnTcpWrite(Connection *c, char *b, unsigned int l, unsigned int *r);
void ConnTcpRead(Connection *c, char *b, unsigned int l, unsigned int *r);
void ConnTcpFlush(Connection *c, char *b, char *e, unsigned int *r);
void ConnTcpClose(Connection *c);
void ConnAddressPoolStartup(AddressPool *pool, unsigned long errorThreshold, unsigned long errorTimeThreshold);
void ConnAddressPoolShutdown(AddressPool *pool);
BOOL ConnAddressPoolAddHost(AddressPool *pool, char *hostName, unsigned short hostPort, unsigned long weight);
@@ -576,23 +286,10 @@ int ConnWriteFile(Connection *Conn, FILE *Source);
int ConnWriteFromFile(Connection *Conn, FILE *Source, int Count);
#define ConnWriteStr(conn, mesg) ConnWrite(conn, mesg, strlen(mesg))
int ConnFlush(Connection *Conn);
BOOL ConnTraceAvailable(void);
#if defined(_BONGO_CONN_TRACE)
void ConnTraceSetFlags(unsigned long flags);
unsigned long ConnTraceGetFlags(void);
void ConnTraceInit(char *path, char *name);
void ConnTraceShutdown(void);
TraceDestination *ConnTraceCreatePersistentDestination(unsigned char type);
void ConnTraceFreeDestination(TraceDestination *destination);
void ConnTraceBegin(Connection *c, unsigned long type, TraceDestination *destination);
void ConnTraceEnd(Connection *c);
void ConnTraceEvent(Connection *c, unsigned long event, char *buffer, long len);
#endif
/* fixme - to be deprecated */
#define XPLNETDB_DEFINE_CONTEXT
+1
View File
@@ -6,6 +6,7 @@ libbongoconnio_la_SOURCES = \
src/libs/connio/trace.c \
src/libs/connio/conniop.h \
src/libs/connio/addrpool.c \
src/libs/connio/sockets.c \
src/libs/connio/unix-ip.c
libbongoconnio_la_LIBADD = $(GNUTLS_LIBS)
+58 -57
View File
@@ -482,7 +482,8 @@ ConnClose(Connection *Conn, unsigned long Flags)
CONN_TRACE_EVENT(Conn, CONN_TRACE_EVENT_CLOSE);
CONN_TRACE_END(c);
CONN_TCP_CLOSE(c);
ConnTcpClose(c);
return(0);
}
@@ -497,7 +498,7 @@ ConnCloseAll(unsigned long Flags)
c = ConnIO.allocated.head;
while (c) {
if (!c->bSelfManage) {
CONN_TCP_CLOSE(c);
ConnTcpClose(c);
}
c = c->allocated.next;
@@ -518,7 +519,7 @@ ConnFree(Connection *Conn)
}
if (c->socket != -1) {
CONN_TCP_CLOSE(c);
ConnTcpClose(c);
}
if (c->receive.buffer) {
@@ -561,7 +562,7 @@ ConnShutdown(void)
ConnIO.allocated.head = NULL;
while (c != NULL) {
CONN_TCP_CLOSE(c);
ConnTcpClose(c);
if (c->receive.buffer) {
MemFree(c->receive.buffer);
@@ -624,9 +625,9 @@ int ConnAccept(Connection *Server, Connection **Client)
int
ConnSend(Connection *Conn, char *Buffer, unsigned int Length)
{
register int count;
int count;
CONN_TCP_SEND(Conn, Buffer, Length, count);
ConnTcpWrite(Conn, Buffer, Length, &count);
return(count);
}
@@ -634,9 +635,9 @@ ConnSend(Connection *Conn, char *Buffer, unsigned int Length)
int
ConnReceive(Connection *Conn, char *Buffer, unsigned int Length)
{
register int count;
int count;
CONN_TCP_RECEIVE(Conn, Buffer, Length, count);
ConnTcpRead(Conn, Buffer, Length, &count);
return(count);
}
@@ -654,8 +655,8 @@ ConnReceive(Connection *Conn, char *Buffer, unsigned int Length)
int
ConnRead(Connection *Conn, char *Dest, int Length)
{
register int read;
register Connection *c = Conn;
int read;
Connection *c = Conn;
read = c->receive.write - c->receive.read;
do {
@@ -677,7 +678,7 @@ ConnRead(Connection *Conn, char *Dest, int Length)
return(Length);
}
CONN_TCP_RECEIVE(c, c->receive.buffer, CONN_TCP_MTU, read);
ConnTcpRead(c, c->receive.buffer, CONN_TCP_MTU, &read);
if (read > 0) {
c->receive.read = c->receive.buffer;
c->receive.write = c->receive.buffer + read;
@@ -736,7 +737,7 @@ ConnReadCount(Connection *Conn, char *Dest, int Count)
}
if (remaining < CONN_TCP_MTU) {
CONN_TCP_RECEIVE(c, c->receive.write, c->receive.remaining, buffered);
ConnTcpRead(c, c->receive.write, c->receive.remaining, &buffered);
if (buffered > 0) {
c->receive.write += buffered;
@@ -747,7 +748,7 @@ ConnReadCount(Connection *Conn, char *Dest, int Count)
}
} else {
do {
CONN_TCP_RECEIVE(c, d, remaining, buffered);
ConnTcpRead(c, d, remaining, &buffered);
if (buffered > 0) {
d += buffered;
remaining -= buffered;
@@ -859,7 +860,7 @@ ConnReadLine(Connection *Conn, char *Line, int Length)
}
}
CONN_TCP_RECEIVE(c, c->receive.buffer, CONN_TCP_MTU, count);
ConnTcpRead(c, c->receive.buffer, CONN_TCP_MTU, &count);
if (count > 0) {
cur = c->receive.read = c->receive.buffer;
limit = c->receive.write = cur + count;
@@ -989,7 +990,7 @@ ConnReadAnswer(Connection *Conn, char *Line, int Length)
}
}
CONN_TCP_RECEIVE(c, c->receive.buffer, CONN_TCP_MTU, count);
ConnTcpRead(c, c->receive.buffer, CONN_TCP_MTU, &count);
if (count > 0) {
cur = c->receive.read = c->receive.buffer;
limit = c->receive.write = cur + count;
@@ -1136,7 +1137,7 @@ ConnReadToAllocatedBuffer(Connection *c, char **buffer, unsigned long *bufferSiz
c->receive.remaining = CONN_TCP_MTU;
c->receive.write[0] = '\0';
CONN_TCP_RECEIVE(c, c->receive.buffer, CONN_TCP_MTU, count);
ConnTcpRead(c, c->receive.buffer, CONN_TCP_MTU, &count);
if (count <= 0) {
break;
}
@@ -1183,7 +1184,7 @@ ConnReadToAllocatedBuffer(Connection *c, char **buffer, unsigned long *bufferSiz
}
}
CONN_TCP_RECEIVE(c, c->receive.buffer, CONN_TCP_MTU, count);
ConnTcpRead(c, c->receive.buffer, CONN_TCP_MTU, &count);
if (count <= 0) {
c->send.remaining = 0;
c->send.read = c->send.write;
@@ -1202,9 +1203,9 @@ ConnReadToAllocatedBuffer(Connection *c, char **buffer, unsigned long *bufferSiz
int
ConnReadToFile(Connection *Conn, FILE *Dest, int Count)
{
register size_t buffered;
register size_t remaining;
register Connection *c = Conn;
size_t buffered;
size_t remaining;
Connection *c = Conn;
remaining = Count;
while (remaining > 0) {
@@ -1240,7 +1241,7 @@ ConnReadToFile(Connection *Conn, FILE *Dest, int Count)
}
}
CONN_TCP_RECEIVE(c, c->receive.write, c->receive.remaining, buffered);
ConnTcpRead(c, c->receive.write, c->receive.remaining, &buffered);
if (buffered > 0) {
c->receive.write += buffered;
c->receive.remaining -= buffered;
@@ -1350,7 +1351,7 @@ ConnReadToFileUntilEOS(Connection *Src, FILE *Dest)
Src->receive.write[0] = '\0';
}
CONN_TCP_RECEIVE(Src, Src->receive.write, Src->receive.remaining, count);
ConnTcpRead(Src, Src->receive.write, Src->receive.remaining, &count);
if (count > 0) {
Src->receive.read = Src->receive.buffer;
Src->receive.write += count;
@@ -1369,14 +1370,14 @@ ConnReadToFileUntilEOS(Connection *Src, FILE *Dest)
int
ConnReadToConn(Connection *Src, Connection *Dest, int Count)
{
size_t sent;
unsigned int sent;
size_t buffered;
size_t remaining;
Connection *s = Src;
Connection *d = Dest;
if ((remaining = Count) > 0) {
CONN_TCP_FLUSH(d, d->send.read, d->send.write, sent);
ConnTcpFlush(d, d->send.read, d->send.write, &sent);
if (sent >= 0) {
d->send.read = d->send.write = d->send.buffer;
d->send.remaining = CONN_TCP_MTU;
@@ -1393,7 +1394,7 @@ ConnReadToConn(Connection *Src, Connection *Dest, int Count)
buffered = s->receive.write - s->receive.read;
if (buffered > 0) {
if (remaining <= buffered) {
CONN_TCP_SEND(d, s->receive.read, remaining, sent);
ConnTcpWrite(d, s->receive.read, remaining, &sent);
if (sent == remaining) {
s->receive.read += sent;
if (s->receive.read == s->receive.write) {
@@ -1410,7 +1411,7 @@ ConnReadToConn(Connection *Src, Connection *Dest, int Count)
}
if ((remaining > s->receive.remaining) && (s->receive.remaining < CONN_TCP_THRESHOLD)) {
CONN_TCP_SEND(d, s->receive.read, buffered, sent);
ConnTcpWrite(d, s->receive.read, buffered, &sent);
if (sent == buffered) {
remaining -= sent;
@@ -1424,7 +1425,7 @@ ConnReadToConn(Connection *Src, Connection *Dest, int Count)
}
}
CONN_TCP_RECEIVE(s, s->receive.write, s->receive.remaining, buffered);
ConnTcpRead(s, s->receive.write, s->receive.remaining, &buffered);
if (buffered > 0) {
s->receive.write += buffered;
s->receive.remaining -= buffered;
@@ -1443,12 +1444,12 @@ int
ConnReadToConnUntilEOS(Connection *Src, Connection *Dest)
{
int written = 0;
size_t count;
unsigned int count;
char *cur;
char *limit;
BOOL finished = FALSE;
CONN_TCP_FLUSH(Dest, Dest->send.read, Dest->send.write, count);
ConnTcpFlush(Dest, Dest->send.read, Dest->send.write, &count);
if (count >= 0) {
Dest->send.read = Dest->send.write = Dest->send.buffer;
Dest->send.remaining = CONN_TCP_MTU;
@@ -1488,7 +1489,7 @@ ConnReadToConnUntilEOS(Connection *Src, Connection *Dest)
}
if (Src->receive.read < cur) {
CONN_TCP_FLUSH(Dest, Src->receive.read, cur, count);
ConnTcpFlush(Dest, Src->receive.read, cur, &count);
if ((size_t)(cur - Src->receive.read) == count) {
written += count;
@@ -1520,7 +1521,7 @@ ConnReadToConnUntilEOS(Connection *Src, Connection *Dest)
}
}
CONN_TCP_RECEIVE(Src, Src->receive.write, Src->receive.remaining, count);
ConnTcpRead(Src, Src->receive.write, Src->receive.remaining, &count);
if (count > 0) {
Src->receive.read = Src->receive.buffer;
Src->receive.write += count;
@@ -1535,14 +1536,14 @@ ConnReadToConnUntilEOS(Connection *Src, Connection *Dest)
return(written);
}
int
ConnWrite(Connection *Conn, const char *Buffer, int Length)
{
register const char *b;
register int i;
register size_t r;
register Connection *c = Conn;
const char *b;
int i;
size_t r;
Connection *c = Conn;
b = Buffer;
r = Length;
@@ -1564,7 +1565,7 @@ ConnWrite(Connection *Conn, const char *Buffer, int Length)
c->send.write += c->send.remaining;
CONN_TCP_FLUSH(c, c->send.read, c->send.write, i);
ConnTcpFlush(c, c->send.read, c->send.write, &i);
if (i > 0) {
c->send.read = c->send.write = c->send.buffer;
c->send.remaining = CONN_TCP_MTU;
@@ -1579,7 +1580,7 @@ ConnWrite(Connection *Conn, const char *Buffer, int Length)
}
do {
CONN_TCP_FLUSH(c, b, b + r, i);
ConnTcpFlush(c, b, b + r, &i);
if (i > 0) {
b += r;
r -= r;
@@ -1599,10 +1600,10 @@ ConnWrite(Connection *Conn, const char *Buffer, int Length)
int
ConnWriteFromFile(Connection *Conn, FILE *Source, int Count)
{
register int i;
register size_t n;
register size_t r;
register Connection *c = Conn;
int i;
size_t n;
size_t r;
Connection *c = Conn;
r = Count;
while (r > 0) {
@@ -1623,7 +1624,7 @@ ConnWriteFromFile(Connection *Conn, FILE *Source, int Count)
return(-1);
}
CONN_TCP_FLUSH(c, c->send.read, c->send.write, i);
ConnTcpFlush(c, c->send.read, c->send.write, &i);
if (i > 0) {
c->send.read = c->send.write = c->send.buffer;
c->send.remaining = CONN_TCP_MTU;
@@ -1641,11 +1642,11 @@ ConnWriteFromFile(Connection *Conn, FILE *Source, int Count)
int
ConnWriteFile(Connection *Conn, FILE *Source)
{
register int i;
register size_t n;
register size_t r;
register Connection *c = Conn;
int i;
size_t n;
size_t r;
Connection *c = Conn;
n = 0;
r = c->send.remaining;
do {
@@ -1659,10 +1660,10 @@ ConnWriteFile(Connection *Conn, FILE *Source)
return(-1);
}
}
n += c->send.write - c->send.read;
CONN_TCP_FLUSH(c, c->send.read, c->send.write, i);
ConnTcpFlush(c, c->send.read, c->send.write, &i);
if (i > 0) {
c->send.read = c->send.write = c->send.buffer;
c->send.remaining = CONN_TCP_MTU;
@@ -1670,10 +1671,10 @@ ConnWriteFile(Connection *Conn, FILE *Source)
c->send.write[0] = '\0';
continue;
}
return(-1);
} while (!feof(Source) && !ferror(Source));
return(n);
}
@@ -1717,7 +1718,7 @@ ConnWriteVF(Connection *c, const char *format, va_list ap)
va_end(ap2);
}
CONN_TCP_FLUSH(c, c->send.read, c->send.write, i);
ConnTcpFlush(c, c->send.read, c->send.write, &i);
if (i > 0) {
c->send.read = c->send.write = c->send.buffer;
c->send.remaining = CONN_TCP_MTU;
@@ -1746,10 +1747,10 @@ ConnWriteF(Connection *conn, const char *Format, ...)
int
ConnFlush(Connection *Conn)
{
register int count;
register Connection *c = Conn;
int count;
Connection *c = Conn;
CONN_TCP_FLUSH(c, c->send.read, c->send.write, count);
ConnTcpFlush(c, c->send.read, c->send.write, &count);
if (count >= 0) {
c->send.read += count;
+155
View File
@@ -0,0 +1,155 @@
#include <unistd.h>
#include <sys/poll.h>
#include <stdio.h>
#include <xpl.h>
#include <connio.h>
#include "config.h"
void
CHOP_NEWLINE(unsigned char *s)
{
unsigned char *p;
p = strchr(s, 0x0A);
if (p) {
*p = '\0';
}
p = strrchr(s, 0x0D);
if (p) {
*p = '\0';
}
}
void
SET_POINTER_TO_VALUE(unsigned char *p, unsigned char *s)
{
// FIXME: Unused?
p = s;
while(isspace(*p)) {
p++;
}
if ((*p == '=') || (*p == ':')) {
p++;
}
while(isspace(*p)) {
p++;
}
}
void
ConnTcpFlush(Connection *c, char *b, char *e, unsigned int *r)
{
if (b < e) {
char *curPTR = (char *)b;
while (curPTR < e) {
ConnTcpWrite(c, curPTR, e - curPTR, r);
if (*r > 0) {
curPTR = curPTR + (int)*r;
continue;
}
break;
}
if (curPTR == e) {
*r = e - b;
} else {
b = -1;
}
} else {
*r = 0;
}
}
void
ConnTcpClose(Connection *c)
{
if (c->receive.buffer) {
c->receive.remaining = CONN_TCP_MTU;
} else {
c->receive.remaining = 0;
}
c->receive.read = c->receive.write = c->receive.buffer;
if (c->send.buffer) {
ConnFlush(c);
c->send.remaining = CONN_TCP_MTU;
} else {
c->send.remaining = 0;
}
c->send.read = c->send.write = c->send.buffer;
if (c->ssl.enable) {
gnutls_bye(c->ssl.context, GNUTLS_SHUT_RDWR);
gnutls_deinit(c->ssl.context);
c->ssl.context = NULL;
c->ssl.enable = FALSE;
}
if (c->socket != -1) {
IPshutdown(c->socket, 2);
IPclose(c->socket);
c->socket = -1;
}
}
#if defined(LINUX) || defined(S390RH) || defined(SOLARIS)
void
ConnTcpRead(Connection *c, char *b, unsigned int l, unsigned int *r)
{
struct pollfd pfd;
do {
pfd.fd = (int)c->socket;
pfd.events = POLLIN;
*r = poll(&pfd, 1, c->receive.timeOut * 1000);
if (*r > 0) {
if ((pfd.revents & (POLLIN | POLLPRI))) {
do {
if (!c->ssl.enable) {
*r = IPrecv((c)->socket, b, l, 0);
} else {
*r = gnutls_record_recv(c->ssl.context, (void *)b, l);
}
if (*r >= 0) {
CONN_TRACE_DATA(c, CONN_TRACE_EVENT_READ, b, r);
break;
} else if (errno == EINTR) {
continue;
}
CONN_TRACE_ERROR(c, "RECV", r);
break;
} while (TRUE);
break;
} else if ((pfd.revents & (POLLERR | POLLHUP | POLLNVAL))) {
CONN_TRACE_ERROR(c, "POLL EVENT", r);
*r = -2;
break;
}
}
if (errno == EINTR) {
continue;
}
CONN_TRACE_ERROR(c, "POLL", r);
*r = -3;
break;
} while (TRUE);
}
void
ConnTcpWrite(Connection *c, char *b, unsigned int l, unsigned int *r)
{
do {
if (!c->ssl.enable) {
*r = IPsend(c->socket, b, l, MSG_NOSIGNAL);
} else {
*r = gnutls_record_send(c->ssl.context, (void *)b, l);
}
if (*r >= 0) {
CONN_TRACE_DATA(c, CONN_TRACE_EVENT_WRITE, b, r);
break;
} else if (errno == EINTR) {
continue;
}
CONN_TRACE_ERROR(c, "POLL", r);
*r = -1;
break;
} while (TRUE);
}
#endif
+3 -3
View File
@@ -127,7 +127,7 @@ FindEndOfLine(Connection *conn)
if (count < CONN_TCP_MTU) {
if (count == 0) {
CONN_TCP_RECEIVE(c, c->receive.buffer, CONN_TCP_MTU, count);
ConnTcpRead(c, c->receive.buffer, CONN_TCP_MTU, &count);
if (count > 0) {
c->receive.read = c->receive.buffer;
c->receive.write = c->receive.buffer + count;
@@ -144,7 +144,7 @@ FindEndOfLine(Connection *conn)
c->receive.read = c->receive.buffer;
c->receive.write = c->receive.buffer + count;
c->receive.remaining = CONN_TCP_MTU - count;
CONN_TCP_RECEIVE(c, c->receive.write, c->receive.remaining, count);
ConnTcpRead(c, c->receive.write, c->receive.remaining, &count);
if (count > 0) {
c->receive.write += count;
c->receive.remaining -= count;
@@ -1606,7 +1606,7 @@ NMAPQuit(Connection *conn)
{
ConnWrite(conn, "QUIT\r\n", 6);
CONN_TCP_CLOSE(conn);
ConnTcpClose(conn);
return;
}