[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] r456 - in GNUnet: . src/transports
From: |
grothoff |
Subject: |
[GNUnet-SVN] r456 - in GNUnet: . src/transports |
Date: |
Sun, 20 Mar 2005 14:06:24 -0800 (PST) |
Author: grothoff
Date: 2005-03-20 14:06:22 -0800 (Sun, 20 Mar 2005)
New Revision: 456
Modified:
GNUnet/src/transports/http.c
GNUnet/src/transports/tcp.c
GNUnet/todo
Log:
http code cleanup and MTU change
Modified: GNUnet/src/transports/http.c
===================================================================
--- GNUnet/src/transports/http.c 2005-03-20 20:28:49 UTC (rev 455)
+++ GNUnet/src/transports/http.c 2005-03-20 22:06:22 UTC (rev 456)
@@ -87,23 +87,10 @@
} HostAddress;
-/**
- * HTTP Message-Packet header. Size is transmitted as part of the
- * HTTP protocol.
- */
-typedef struct {
-
- /**
- * This struct is followed by MESSAGE_PARTs - until size is reached
- * There is no "end of message".
- */
- p2p_HEADER parts[0];
-} HTTPMessagePack;
-
/* How much do we read from a buffer at least? Answer: for now, at
MOST the size of the message, since we MUST not read the next
header(s) by accident! */
-#define MIN_BUF_READ (4 + sizeof(HTTPMessagePack))
+#define MIN_BUF_READ 4
/* how long do we allow an http-header to be at
most? */
#define MAX_HTTP_HEADER 2048
@@ -178,7 +165,7 @@
/**
* Current size of the read buffer.
*/
- unsigned int size;
+ unsigned int rsize;
/**
* The read buffer (used only for the actual data).
@@ -204,29 +191,20 @@
unsigned int httpRSize;
/**
- * Position in the write buffer (how many bytes are still waiting to
- * be transmitted? -- always the first wsize bytes in wbuff are
- * guaranteed to be valid and are pending).
- */
- unsigned int wsize;
-
- /**
* The write buffer.
*/
char * wbuff;
/**
- * Output buffer used for the http header lines.
- * Read fills this buffer until we hit the end of
- * the request header (CRLF). Then we switch
- * to rbuff.
+ * Number of valid bytes in wbuff.
*/
- char * httpWriteBuff;
+ unsigned int wpos;
/**
- * Total size of httpWriteBuff
+ * Size of the write buffer.
*/
- unsigned int httpWSize;
+ unsigned int wsize;
+
} HTTPSession;
/* *********** globals ************* */
@@ -320,9 +298,7 @@
&i,
sizeof(char));
if (ret != sizeof(char))
- LOG(LOG_ERROR,
- " write to http pipe (signalSelect) failed: %s\n",
- STRERROR(errno));
+ LOG_STRERROR(LOG_ERROR, "write");
}
/**
@@ -349,11 +325,12 @@
MUTEX_DESTROY(&httpsession->lock);
FREENONNULL(httpsession->rbuff);
FREENONNULL(httpsession->httpReadBuff);
- FREENONNULL(httpsession->wbuff);
- FREENONNULL(httpsession->httpWriteBuff);
+ GROW(httpsession->wbuff,
+ httpsession->wsize,
+ 0);
FREE(httpsession);
- FREE(tsession);
}
+ FREE(tsession);
return OK;
}
@@ -376,10 +353,7 @@
httpSession = tsessions[i]->internal;
if (httpSession->sock != -1)
if (0 != SHUTDOWN(httpSession->sock, SHUT_RDWR))
- LOG(LOG_EVERYTHING,
- " error shutting down socket %d: %s\n",
- httpSession->sock,
- STRERROR(errno));
+ LOG_STRERROR(LOG_EVERYTHING, "shutdown");
CLOSE(httpSession->sock);
httpSession->sock = -1;
httpDisconnect(tsessions[i]);
@@ -438,24 +412,6 @@
}
/**
- * We're done processing a message. Reset buffers as needed to
- * prepare for receiving the next chunk.
- */
-static void messageProcessed(HTTPSession * httpSession) {
- /* deallocate read buffer, we don't really know
- how big the next chunk will be */
- GROW(httpSession->rbuff,
- httpSession->size,
- 0);
- /* allocate read-buffer for the next header,
- ALWAYS start with minimum size!!! */
- GROW(httpSession->httpReadBuff,
- httpSession->httpRSize,
- MIN_BUF_READ);
- httpSession->httpRPos = 0;
-}
-
-/**
* We have received more header-bytes. Check if the HTTP header is
* complete, and if yes allocate rbuff and move the data-portion that
* was received over to rbuff (and reset the header-reader).
@@ -505,22 +461,20 @@
16);
httpSession->httpReadBuff[k] = '\r';
if (endPtr == &httpSession->httpReadBuff[k]) {
- if (len >= 65536) {
- BREAK();
+ if (len >= MAX_BUFFER_SIZE) {
+ BREAK(); /* FIMXE: inline method and do proper
+ error handling! */
} else {
GROW(httpSession->rbuff,
- httpSession->size,
+ httpSession->rsize,
len);
memcpy(httpSession->rbuff,
&httpSession->httpReadBuff[k+2],
httpSession->httpRPos - (k+2));
httpSession->rpos = httpSession->httpRPos - (k+2);
- GROW(httpSession->httpReadBuff,
- httpSession->httpRSize,
- 0);
- httpSession->httpRPos = 0;
}
- }
+ httpSession->httpRPos = 0;
+ }
}
}
}
@@ -535,92 +489,65 @@
static int readAndProcess(int i) {
TSession * tsession;
HTTPSession * httpSession;
- int len;
- HTTPMessagePack * pack;
+ unsigned int len;
+ int ret;
MessagePack * mp;
tsession = tsessions[i];
if (SYSERR == httpAssociate(tsession))
return SYSERR;
httpSession = tsession->internal;
- if (httpSession->size == 0) {
+ if (httpSession->rsize == 0) {
/* chunk read mode */
- if (httpSession->httpRSize - httpSession->httpRPos < MIN_BUF_READ) {
- if (httpSession->httpRSize >= MAX_HTTP_HEADER) {
- len = -1; /* error! */
- errno = 0; /* make sure it's not set to retry */
- } else {
- GROW(httpSession->httpReadBuff,
- httpSession->httpRSize,
- httpSession->httpRSize + MIN_BUF_READ);
- len = READ(httpSession->sock,
- &httpSession->httpReadBuff[httpSession->httpRPos],
- httpSession->httpRSize - httpSession->httpRPos);
- }
- } else
- len = READ(httpSession->sock,
- &httpSession->httpReadBuff[httpSession->httpRPos],
- httpSession->httpRSize - httpSession->httpRPos);
- if (len >= 0) {
- httpSession->httpRPos += len;
+ if (httpSession->httpRSize == httpSession->httpRPos) {
+ httpDisconnect(tsession);
+ return SYSERR; /* error! */
+ }
+ ret = READ(httpSession->sock,
+ &httpSession->httpReadBuff[httpSession->httpRPos],
+ httpSession->httpRSize - httpSession->httpRPos);
+ if (ret > 0) {
+ httpSession->httpRPos += ret;
+ incrementBytesReceived(ret);
checkHeaderComplete(httpSession);
+ } else {
+#if DEBUG_HTTP
+ LOG_STRERROR(LOG_DEBUG, "read");
+#endif
+ httpDisconnect(tsession);
+ return SYSERR; /* error! */
}
} else {
+ GNUNET_ASSERT(httpSession->rsize > httpSession->rpos);
/* data read mode */
- len = READ(httpSession->sock,
+ ret = READ(httpSession->sock,
&httpSession->rbuff[httpSession->rpos],
- httpSession->size - httpSession->rpos);
- if (len >= 0)
- httpSession->rpos += len;
- }
- cronTime(&httpSession->lastUse);
- if (len == 0) {
- httpDisconnect(tsession);
+ httpSession->rsize - httpSession->rpos);
+ if (ret > 0) {
+ httpSession->rpos += ret;
+ incrementBytesReceived(ret);
+ } else {
#if DEBUG_HTTP
- LOG(LOG_DEBUG,
- "READ on socket %d returned 0 bytes, closing connection.\n",
- httpSession->sock);
-#endif
- return SYSERR; /* other side closed connection */
- }
- if (len < 0) {
- if ( (errno == EINTR) ||
- (errno == EAGAIN) ) {
-#if DEBUG_HTTP
LOG_STRERROR(LOG_DEBUG, "read");
#endif
httpDisconnect(tsession);
- return SYSERR;
+ return SYSERR; /* error! */
}
-#if DEBUG_HTTP
- LOG_STRERROR(LOG_INFO, "read");
-#endif
+ }
+ if (httpSession->rpos != httpSession->rsize) {
+ /* only have partial message yet */
httpDisconnect(tsession);
- return SYSERR;
- }
- incrementBytesReceived(len);
-#if DEBUG_HTTP
- LOG(LOG_DEBUG,
- "Read %d bytes on socket %d, now having %d of %d (%d)\n",
- len,
- httpSession->sock,
- httpSession->rpos,
- httpSession->size,
- httpSession->httpRPos);
-#endif
- if ( (httpSession->rpos < 2) ||
- (httpSession->rpos < httpSession->size) ) {
- httpDisconnect(tsession);
return OK;
}
-
+ cronTime(&httpSession->lastUse);
/* complete message received, let's check what it is */
+
if (YES == httpSession->expectingWelcome) {
HTTPWelcome * welcome;
#if DEBUG_HTTP
EncName enc;
#endif
-
+
welcome = (HTTPWelcome*) &httpSession->rbuff[0];
if ( (ntohs(welcome->version) != 0) ||
(ntohs(welcome->size) != sizeof(HTTPWelcome)) ) {
@@ -630,9 +557,7 @@
return SYSERR;
}
httpSession->expectingWelcome = NO;
- memcpy(&httpSession->sender,
- &welcome->clientIdentity,
- sizeof(PeerIdentity));
+ httpSession->sender = welcome->clientIdentity;
#if DEBUG_HTTP
IFLOG(LOG_DEBUG,
hash2enc(&httpSession->sender.hashPubKey,
@@ -641,52 +566,41 @@
"Http welcome message from peer '%s' received.\n",
&enc);
#endif
+ GROW(httpSession->rbuff,
+ httpSession->rsize,
+ 0);
httpSession->rpos = 0;
- messageProcessed(httpSession);
- GROW(httpSession->httpWriteBuff,
- httpSession->httpWSize,
+ GNUNET_ASSERT(httpSession->wsize == 0);
+ GROW(httpSession->wbuff,
+ httpSession->wsize,
256);
- len = SNPRINTF(httpSession->httpWriteBuff,
- httpSession->httpWSize,
+ len = SNPRINTF(httpSession->wbuff,
+ httpSession->wsize,
"HTTP/1.1 200 OK\r\n"
"Server: Apache/1.3.27\r\n"
"Transfer-Encoding: chunked\r\n"
- "Content-Type: text/html\r\n"
+ "Content-Type: application/octet-stream\r\n"
"\r\n");
- GROW(httpSession->httpWriteBuff,
- httpSession->httpWSize,
- len);
+ httpSession->wpos = len;
httpDisconnect(tsession);
return OK;
}
-
- pack = (HTTPMessagePack*)&httpSession->rbuff[0];
- /* send msg to core! */
- if (httpSession->size <= sizeof(HTTPMessagePack)) {
- LOG(LOG_WARNING,
- _("Received malformed message from http-peer connection. Closing.\n"));
- httpDisconnect(tsession);
- return SYSERR;
- }
+
+ /* Full normal message received; pass on to core! */
mp = MALLOC(sizeof(MessagePack));
- mp->msg = MALLOC(httpSession->size);
- memcpy(mp->msg,
- &pack->parts[0],
- httpSession->size - sizeof(HTTPMessagePack));
- memcpy(&mp->sender,
- &httpSession->sender,
- sizeof(PeerIdentity));
- mp->tsession = tsession;
+ mp->sender = httpSession->sender;
+ mp->tsession = tsession;
+ mp->msg = httpSession->rbuff;
+ mp->size = httpSession->rsize;
+ httpSession->rbuff = NULL;
+ httpSession->rsize = 0;
+ httpSession->rpos = 0;
#if DEBUG_HTTP
LOG(LOG_DEBUG,
- "Http transport received %d bytes, forwarding to core.\n",
+ "http transport received %d bytes, forwarding to core.\n",
mp->size);
#endif
coreAPI->receive(mp);
-
- httpSession->rpos = 0;
- messageProcessed(httpSession);
-
httpDisconnect(tsession);
return OK;
}
@@ -723,21 +637,21 @@
httpSession = MALLOC(sizeof(HTTPSession));
httpSession->rpos = 0;
- httpSession->size = 0;
+ httpSession->rsize = 0;
httpSession->rbuff = NULL;
httpSession->wsize = 0;
+ httpSession->wpos = 0;
httpSession->wbuff = NULL;
httpSession->httpReadBuff = NULL;
httpSession->httpRPos = 0;
httpSession->httpRSize = 0;
- httpSession->httpWriteBuff = NULL;
- httpSession->httpWSize = 0;
+ GROW(httpSession->httpReadBuff,
+ httpSession->httpRSize,
+ MAX_HTTP_HEADER);
httpSession->sock = sock;
/* fill in placeholder identity to mark that we
are waiting for the welcome message */
- memcpy(&httpSession->sender,
- coreAPI->myIdentity,
- sizeof(PeerIdentity));
+ httpSession->sender = *(coreAPI->myIdentity);
httpSession->expectingWelcome = YES;
MUTEX_CREATE_RECURSIVE(&httpSession->lock);
httpSession->users = 1; /* us only, core has not seen this tsession! */
@@ -767,14 +681,14 @@
int ret;
if (http_sock != -1)
- LISTEN(http_sock, 5);
+ if (0 != LISTEN(http_sock, 5))
+ LOG_STRERROR(LOG_ERROR, "listen");
SEMAPHORE_UP(serverSignal); /* we are there! */
MUTEX_LOCK(&httplock);
while (http_shutdown == NO) {
FD_ZERO(&readSet);
FD_ZERO(&errorSet);
FD_ZERO(&writeSet);
-
if (http_sock != -1) {
if (isSocketValid(http_sock)) {
FD_SET(http_sock, &readSet);
@@ -782,7 +696,9 @@
LOG_STRERROR(LOG_ERROR, "isSocketValid");
http_sock = -1; /* prevent us from error'ing all the time */
}
- }
+ } else
+ LOG(LOG_DEBUG,
+ "HTTP server socket not open!\n");
if (http_pipe[0] != -1) {
if (-1 != FSTAT(http_pipe[0], &buf)) {
FD_SET(http_pipe[0], &readSet);
@@ -801,10 +717,8 @@
if (isSocketValid(sock)) {
FD_SET(sock, &readSet);
FD_SET(sock, &errorSet);
- if ( (httpSession->wsize > 0) ||
- (httpSession->httpWSize > 0) ) {
- FD_SET(sock, &writeSet); /* do we have a pending write request? */
- }
+ if (httpSession->wpos > 0)
+ FD_SET(sock, &writeSet); /* do we have a pending write request? */
} else {
LOG_STRERROR(LOG_ERROR, "isSocketValid");
destroySession(i);
@@ -833,7 +747,7 @@
if (FD_ISSET(http_sock, &readSet)) {
int sock;
- lenOfIncomingAddr = sizeof(clientAddr);
+ lenOfIncomingAddr = sizeof(clientAddr);
sock = ACCEPT(http_sock,
(struct sockaddr *)&clientAddr,
&lenOfIncomingAddr);
@@ -847,14 +761,19 @@
&clientAddr.sin_addr,
sizeof(struct in_addr));
-
if (YES == isBlacklisted(ipaddr)) {
LOG(LOG_INFO,
_("Rejected blacklisted connection from %u.%u.%u.%u.\n"),
PRIP(ntohl(*(int*)&clientAddr.sin_addr)));
CLOSE(sock);
- } else
+ } else {
+#if DEBUG_HTTP
+ LOG(LOG_INFO,
+ "Accepted connection from %u.%u.%u.%u.\n",
+ PRIP(ntohl(*(int*)&clientAddr.sin_addr)));
+#endif
createNewSession(sock);
+ }
} else {
LOG_STRERROR(LOG_INFO, "accept");
}
@@ -865,7 +784,6 @@
in one shot... */
#define MAXSIG_BUF 128
char buf[MAXSIG_BUF];
-
/* just a signal to refresh sets, eat and continue */
if (0 >= READ(http_pipe[0],
&buf[0],
@@ -884,83 +802,45 @@
}
}
if (FD_ISSET(sock, &writeSet)) {
- int ret, success;
+ int ret;
+ int success;
- if (httpSession->httpWSize > 0) {
try_again_1:
- success = SEND_NONBLOCKING(sock,
- httpSession->httpWriteBuff,
- httpSession->httpWSize,
- &ret);
- if (success == SYSERR) {
- LOG_STRERROR(LOG_WARNING, "send");
- destroySession(i);
- i--;
- continue;
- } else if (success == NO) {
- /* this should only happen under Win9x because
- of a bug in the socket implementation (KB177346).
- Let's sleep and try again. */
- gnunet_util_sleep(20);
- goto try_again_1;
- }
- if (ret == 0) {
- /* send only returns 0 on error (other side closed connection),
- * so close the session */
- destroySession(i);
- i--;
- continue;
- }
- if ((unsigned int)ret == httpSession->httpWSize) {
- GROW(httpSession->httpWriteBuff,
- httpSession->httpWSize,
- 0);
- } else {
- memmove(httpSession->httpWriteBuff,
- &httpSession->httpWriteBuff[ret],
- httpSession->httpWSize - ret);
- httpSession->httpWSize -= ret;
- }
- } else { /* httpSession->httpWSize == 0 */
- if (httpSession->wsize == 0)
- errexit(" wsize %d for socket %d\n",
- httpSession->wsize,
- sock);
-try_again_2:
- success = SEND_NONBLOCKING(sock,
- httpSession->wbuff,
- httpSession->wsize,
- &ret);
- if (success == SYSERR) {
- LOG_STRERROR(LOG_WARNING, "send");
- destroySession(i);
- i--;
- continue;
- } else if (success == NO) {
- /* this should only happen under Win9x because
- of a bug in the socket implementation (KB177346).
- Let's sleep and try again. */
- gnunet_util_sleep(20);
- goto try_again_2;
- }
- if (ret == 0) {
- /* send only returns 0 on error (other side closed connection),
- * so close the session */
- destroySession(i);
- i--;
- continue;
- }
- if ((unsigned int)ret == httpSession->wsize) {
- GROW(httpSession->wbuff,
- httpSession->wsize,
- 0);
- } else {
- memmove(httpSession->wbuff,
- &httpSession->wbuff[ret],
- httpSession->wsize - ret);
- httpSession->wsize -= ret;
- }
+ success = SEND_NONBLOCKING(sock,
+ httpSession->wbuff,
+ httpSession->wpos,
+ &ret);
+ if (success == SYSERR) {
+ LOG_STRERROR(LOG_WARNING, "send");
+ destroySession(i);
+ i--;
+ continue;
+ } else if (success == NO) {
+ /* this should only happen under Win9x because
+ of a bug in the socket implementation (KB177346).
+ Let's sleep and try again. */
+ gnunet_util_sleep(20);
+ goto try_again_1;
}
+
+ if (ret == 0) {
+ /* send only returns 0 on error (other side closed connection),
+ * so close the session */
+ destroySession(i);
+ i--;
+ continue;
+ }
+ if ((unsigned int)ret == httpSession->wpos) {
+ FREENONNULL(httpSession->wbuff);
+ httpSession->wbuff = NULL;
+ httpSession->wpos = 0;
+ httpSession->wsize = 0;
+ } else {
+ memmove(httpSession->wbuff,
+ &httpSession->wbuff[ret],
+ httpSession->wpos - ret);
+ httpSession->wpos -= ret;
+ }
}
if (FD_ISSET(sock, &errorSet)) {
destroySession(i);
@@ -1001,55 +881,49 @@
*/
static int httpDirectSend(HTTPSession * httpSession,
int doPost,
- void * mp,
+ const void * mp,
unsigned int ssize) {
- int len;
+ unsigned int len;
+ if (http_shutdown == YES)
+ return SYSERR;
if (httpSession->sock == -1) {
#if DEBUG_HTTP
LOG(LOG_INFO,
- " httpDirectSend called, but socket is closed\n");
+ "httpDirectSend called, but socket is closed\n");
#endif
return SYSERR;
}
- if (ssize > httpAPI.mtu + sizeof(HTTPMessagePack)) {
- BREAK();
+ if (ssize == 0) {
+ BREAK(); /* size 0 not allowed */
return SYSERR;
}
-
- if (httpSession->wbuff != NULL) {
-#if DEBUG_HTTP
- LOG(LOG_INFO,
- "httpTransport has already message "
- "pending, will not queue more.\n");
-#endif
+ MUTEX_LOCK(&httplock);
+ if (httpSession->wpos > 0) {
+ MUTEX_UNLOCK(&httplock);
return SYSERR; /* already have msg pending */
}
- GNUNET_ASSERT(httpSession->httpWriteBuff == NULL);
if (doPost == YES) {
IPaddr ip;
if (SYSERR == getPublicIPAddress(&ip))
return SYSERR;
- GROW(httpSession->httpWriteBuff,
- httpSession->httpWSize,
- 256);
- strcpy(httpSession->httpWriteBuff, "POST ");
- /* In case we're talking to a proxy, we need an absolute URI */
- if (theProxy.sin_addr.s_addr != 0)
- {
- len = SNPRINTF(httpSession->httpWriteBuff + 5,
- httpSession->httpWSize - 5,
- "http://%u.%u.%u.%u:%u",
- PRIP(ntohl(httpSession->hostIP)),
- ntohs(httpSession->hostPort)) + 5;
+ GROW(httpSession->wbuff,
+ httpSession->wsize,
+ 256 + ssize);
+ strcpy(httpSession->wbuff, "POST ");
+ /* if we're talking to a proxy, we need an absolute URI */
+ if (theProxy.sin_addr.s_addr != 0) {
+ len = SNPRINTF(httpSession->wbuff + 5,
+ httpSession->wsize - 5,
+ "http://%u.%u.%u.%u:%u",
+ PRIP(ntohl(httpSession->hostIP)),
+ ntohs(httpSession->hostPort)) + 5;
+ } else {
+ len = 5;
}
- else
- {
- len = 5;
- }
- len += SNPRINTF(httpSession->httpWriteBuff + len,
- httpSession->httpWSize - len,
+ len += SNPRINTF(httpSession->wbuff + len,
+ httpSession->wsize - len,
"/ HTTP/1.1\r\n"
"Host: %u.%u.%u.%u\r\n"
"Transfer-Encoding: chunked\r\n"
@@ -1058,30 +932,24 @@
"%x\r\n",
PRIP(ntohl(*(int*)&ip)),
ssize);
- GROW(httpSession->httpWriteBuff,
- httpSession->httpWSize,
- len);
} else {
- GROW(httpSession->httpWriteBuff,
- httpSession->httpWSize,
- 64);
- len = SNPRINTF(httpSession->httpWriteBuff,
- httpSession->httpWSize,
+ GROW(httpSession->wbuff,
+ httpSession->wsize,
+ 64 + ssize);
+ len = SNPRINTF(httpSession->wbuff,
+ httpSession->wsize,
"\r\n%x\r\n",
ssize);
- GROW(httpSession->httpWriteBuff,
- httpSession->httpWSize,
- len);
- }
- GROW(httpSession->wbuff,
- httpSession->wsize,
- ssize);
- memcpy(httpSession->wbuff,
+ }
+ memcpy(&httpSession->wbuff[len],
mp,
ssize);
+ len += ssize;
+ httpSession->wpos = len;
signalSelect(); /* select set changed! */
cronTime(&httpSession->lastUse);
- incrementBytesSent(ssize);
+ MUTEX_UNLOCK(&httplock);
+ incrementBytesSent(len);
return OK;
}
@@ -1172,7 +1040,9 @@
PRIP(ntohl(*(int*)&haddr->ip.addr)),
ntohs(haddr->port));
#endif
- sock = SOCKET(PF_INET, SOCK_STREAM, 6);/* 6: TCP */
+ sock = SOCKET(PF_INET,
+ SOCK_STREAM,
+ 6);/* 6: TCP */
if (sock == -1) {
LOG_STRERROR(LOG_FAILURE, "socket");
return SYSERR;
@@ -1216,14 +1086,16 @@
httpSession->hostIP = haddr->ip.addr;
httpSession->hostPort = haddr->port;
httpSession->wsize = 0;
+ httpSession->wpos = 0;
httpSession->wbuff = NULL;
- httpSession->size = 0;
+ httpSession->rsize = 0;
httpSession->rbuff = NULL;
httpSession->httpReadBuff = NULL;
httpSession->httpRPos = 0;
httpSession->httpRSize = 0;
- httpSession->httpWriteBuff = NULL;
- httpSession->httpWSize = 0;
+ GROW(httpSession->httpReadBuff,
+ httpSession->httpRSize,
+ MAX_HTTP_HEADER);
tsession = MALLOC(sizeof(TSession));
tsession->internal = httpSession;
tsession->ttype = httpAPI.protocolNumber;
@@ -1231,9 +1103,7 @@
httpSession->users = 2; /* caller + us */
httpSession->rpos = 0;
cronTime(&httpSession->lastUse);
- memcpy(&httpSession->sender,
- &helo->senderIdentity,
- sizeof(PeerIdentity));
+ httpSession->sender = helo->senderIdentity;
httpSession->expectingWelcome = NO;
MUTEX_LOCK(&httplock);
i = addTSession(tsession);
@@ -1243,9 +1113,7 @@
welcome.size = htons(sizeof(HTTPWelcome));
welcome.version = htons(0);
- memcpy(&welcome.clientIdentity,
- coreAPI->myIdentity,
- sizeof(PeerIdentity));
+ welcome.clientIdentity = *(coreAPI->myIdentity);
if (SYSERR == httpDirectSend(httpSession,
YES,
&welcome,
@@ -1256,8 +1124,8 @@
return SYSERR;
}
MUTEX_UNLOCK(&httplock);
+ signalSelect();
- gnunet_util_sleep(50 * cronMILLIS);
*tsessionPtr = tsession;
FREE(helo);
return OK;
@@ -1272,37 +1140,25 @@
* @return SYSERR on error, OK on success
*/
static int httpSend(TSession * tsession,
- const void * msg,
- const unsigned int size) {
- HTTPMessagePack * mp;
+ const void * msg,
+ const unsigned int size) {
int ok;
- int ssize;
- if (http_shutdown == YES) {
- BREAK();
+ if (size >= MAX_BUFFER_SIZE)
return SYSERR;
- }
+ if (http_shutdown == YES)
+ return SYSERR;
if (size == 0) {
BREAK();
return SYSERR;
}
- if (size > httpAPI.mtu) {
- BREAK();
- return SYSERR;
- }
+
if (((HTTPSession*)tsession->internal)->sock == -1)
return SYSERR; /* other side closed connection */
- mp = MALLOC(sizeof(HTTPMessagePack) + size);
- memcpy(&mp->parts[0],
- msg,
- size);
- ssize = size + sizeof(HTTPMessagePack);
-
ok = httpDirectSend(tsession->internal,
NO,
- mp,
- ssize);
- FREE(mp);
+ msg,
+ size);
return ok;
}
@@ -1310,7 +1166,7 @@
* Start the server process to receive inbound traffic.
* @return OK on success, SYSERR if the operation failed
*/
-static int startTransportServer(void) {
+static int startTransportServer() {
struct sockaddr_in serverAddr;
const int on = 1;
unsigned short port;
@@ -1331,13 +1187,23 @@
port = getGNUnetHTTPPort();
if (port != 0) { /* if port == 0, this is a read-only
business! */
- http_sock = SOCKET(PF_INET, SOCK_STREAM, 0);
- if (http_sock < 0)
- DIE_STRERROR("socket");
+ http_sock = SOCKET(PF_INET,
+ SOCK_STREAM,
+ 0);
+ if (http_sock < 0) {
+ LOG_STRERROR(LOG_FAILURE, "socket");
+ CLOSE(http_pipe[0]);
+ CLOSE(http_pipe[1]);
+ SEMAPHORE_FREE(serverSignal);
+ serverSignal = NULL;
+ http_shutdown = YES;
+ return SYSERR;
+ }
if (SETSOCKOPT(http_sock,
SOL_SOCKET,
SO_REUSEADDR,
- &on, sizeof(on)) < 0 )
+ &on,
+ sizeof(on)) < 0 )
DIE_STRERROR("setsockopt");
memset((char *) &serverAddr,
0,
@@ -1347,7 +1213,8 @@
serverAddr.sin_port = htons(getGNUnetHTTPPort());
#if DEBUG_HTTP
LOG(LOG_INFO,
- "Starting http peer server on port %d\n",
+ "starting %s peer server on port %d\n",
+ "http",
ntohs(serverAddr.sin_port));
#endif
if (BIND(http_sock,
@@ -1368,10 +1235,11 @@
if (0 == PTHREAD_CREATE(&listenThread,
(PThreadMain) &httpListenMain,
NULL,
- 2048)) {
+ 4092)) {
SEMAPHORE_DOWN(serverSignal); /* wait for server to be up */
} else {
- LOG_STRERROR(LOG_FATAL, "pthread_create");
+ LOG_STRERROR(LOG_ERROR,
+ "pthread_create");
CLOSE(http_sock);
SEMAPHORE_FREE(serverSignal);
serverSignal = NULL;
@@ -1386,11 +1254,18 @@
*/
static int stopTransportServer() {
void * unused;
+ int haveThread;
+ if (http_shutdown == YES)
+ return OK;
http_shutdown = YES;
signalSelect();
- SEMAPHORE_DOWN(serverSignal);
- SEMAPHORE_FREE(serverSignal);
+ if (serverSignal != NULL) {
+ haveThread = YES;
+ SEMAPHORE_DOWN(serverSignal);
+ SEMAPHORE_FREE(serverSignal);
+ } else
+ haveThread = NO;
serverSignal = NULL;
CLOSE(http_pipe[1]);
CLOSE(http_pipe[0]);
@@ -1398,7 +1273,8 @@
CLOSE(http_sock);
http_sock = -1;
}
- PTHREAD_JOIN(&listenThread, &unused);
+ if (haveThread == YES)
+ PTHREAD_JOIN(&listenThread, &unused);
return OK;
}
@@ -1406,7 +1282,7 @@
* Reload the configuration. Should never fail (keep old
* configuration on error, syslog errors!)
*/
-static void reloadConfiguration(void) {
+static void reloadConfiguration() {
char * ch;
MUTEX_LOCK(&httplock);
@@ -1449,59 +1325,46 @@
* via a global and returns the udp transport API.
*/
TransportAPI * inittransport_http(CoreAPIForTransport * core) {
- int mtu;
struct hostent *ip;
- char *proxy, *proxyPort;
+ char * proxy;
+ char * proxyPort;
MUTEX_CREATE_RECURSIVE(&httplock);
reloadConfiguration();
tsessionCount = 0;
- tsessionArrayLength = 32;
- tsessions = MALLOC(sizeof(TSession*) * tsessionArrayLength);
+ tsessionArrayLength = 0;
+ GROW(tsessions,
+ tsessionArrayLength,
+ 32);
coreAPI = core;
- mtu = getConfigurationInt("HTTP",
- "MTU");
- if (mtu == 0)
- mtu = 1400;
- if (mtu < 1200)
- LOG(LOG_ERROR,
- _("MTU for '%s' is probably too low (fragmentation not
implemented!)\n"),
- "HTTP");
- proxy = getConfigurationString("GNUNETD", "HTTP-PROXY");
- if (proxy != NULL)
- {
- ip = GETHOSTBYNAME(proxy);
- if (ip == NULL)
- {
- LOG(LOG_ERROR,
- _("Could not resolve name of HTTP proxy '%s'.\n"),
- proxy);
- theProxy.sin_addr.s_addr = 0;
- }
- else
- {
- theProxy.sin_addr.s_addr = ((struct in_addr *)ip->h_addr)->s_addr;
- proxyPort = getConfigurationString("GNUNETD", "HTTP-PROXY-PORT");
- if (proxyPort == NULL)
- {
- theProxy.sin_port = htons(8080);
+ proxy = getConfigurationString("GNUNETD",
+ "HTTP-PROXY");
+ if (proxy != NULL) {
+ ip = GETHOSTBYNAME(proxy);
+ if (ip == NULL) {
+ LOG(LOG_ERROR,
+ _("Could not resolve name of HTTP proxy '%s'.\n"),
+ proxy);
+ theProxy.sin_addr.s_addr = 0;
+ } else {
+ theProxy.sin_addr.s_addr = ((struct in_addr *)ip->h_addr)->s_addr;
+ proxyPort = getConfigurationString("GNUNETD",
+ "HTTP-PROXY-PORT");
+ if (proxyPort == NULL) {
+ theProxy.sin_port = htons(8080);
+ } else {
+ theProxy.sin_port = htons(atoi(proxyPort));
+ FREE(proxyPort);
+ }
}
- else
- {
- theProxy.sin_port = htons(atoi(proxyPort));
- FREE(proxyPort);
- }
- }
- FREE(proxy);
+ FREE(proxy);
+ } else {
+ theProxy.sin_addr.s_addr = 0;
}
- else
- {
- theProxy.sin_addr.s_addr = 0;
- }
-
+
httpAPI.protocolNumber = HTTP_PROTOCOL_NUMBER;
- httpAPI.mtu = mtu - sizeof(HTTPMessagePack);
+ httpAPI.mtu = 0;
httpAPI.cost = 20000; /* about equal to udp */
httpAPI.verifyHelo = &verifyHelo;
httpAPI.createHELO = &createHELO;
@@ -1519,9 +1382,12 @@
}
void donetransport_http() {
- FREE(tsessions);
- tsessions = NULL;
- tsessionArrayLength = 0;
+ int i;
+ for (i=tsessionCount-1;i>=0;i--)
+ destroySession(i);
+ GROW(tsessions,
+ tsessionArrayLength,
+ 0);
FREENONNULL(filteredNetworks_);
MUTEX_DESTROY(&httplock);
}
Modified: GNUnet/src/transports/tcp.c
===================================================================
--- GNUnet/src/transports/tcp.c 2005-03-20 20:28:49 UTC (rev 455)
+++ GNUnet/src/transports/tcp.c 2005-03-20 22:06:22 UTC (rev 456)
@@ -603,7 +603,7 @@
}
} else
LOG(LOG_DEBUG,
- "TCP6 server socket not open!\n");
+ "TCP server socket not open!\n");
if (tcp_pipe[0] != -1) {
if (-1 != FSTAT(tcp_pipe[0], &buf)) {
FD_SET(tcp_pipe[0], &readSet);
@@ -652,7 +652,7 @@
if (FD_ISSET(tcp_sock, &readSet)) {
int sock;
- lenOfIncomingAddr = sizeof(clientAddr);
+ lenOfIncomingAddr = sizeof(clientAddr);
sock = ACCEPT(tcp_sock,
(struct sockaddr *)&clientAddr,
&lenOfIncomingAddr);
@@ -689,7 +689,6 @@
if (FD_ISSET(tcp_pipe[0], &readSet)) {
/* allow reading multiple signals in one go in case we get many
in one shot... */
-
#define MAXSIG_BUF 128
char buf[MAXSIG_BUF];
/* just a signal to refresh sets, eat and continue */
@@ -710,7 +709,8 @@
}
}
if (FD_ISSET(sock, &writeSet)) {
- int ret, success;
+ int ret;
+ int success;
try_again_1:
success = SEND_NONBLOCKING(sock,
@@ -836,8 +836,8 @@
tcpSession->wpos = ssize - ret;
signalSelect(); /* select set changed! */
}
- MUTEX_UNLOCK(&tcplock);
cronTime(&tcpSession->lastUse);
+ MUTEX_UNLOCK(&tcplock);
incrementBytesSent(ssize);
return OK;
}
@@ -1005,21 +1005,15 @@
TSession * tsession;
TCPSession * tcpSession;
struct sockaddr_in soaddr;
-#if DEBUG_TCP
- EncName enc;
-#endif
if (tcp_shutdown == YES)
return SYSERR;
haddr = (HostAddress*) &((HELO_Message_GENERIC*)helo)->senderAddress[0];
#if DEBUG_TCP
- hash2enc(&coreAPI->myIdentity->hashPubKey,
- &enc);
LOG(LOG_DEBUG,
- "Creating TCP connection to %u.%u.%u.%u:%u from %s.\n",
+ "Creating TCP connection to %u.%u.%u.%u:%u.\n",
PRIP(ntohl(*(int*)&haddr->ip.addr)),
- ntohs(haddr->port),
- &enc);
+ ntohs(haddr->port));
#endif
sock = SOCKET(PF_INET,
SOCK_STREAM,
@@ -1190,7 +1184,8 @@
serverAddr.sin_port = htons(getGNUnetTCPPort());
#if DEBUG_TCP
LOG(LOG_INFO,
- "starting tcp peer server on port %d\n",
+ "starting %s peer server on port %d\n",
+ "tcp",
ntohs(serverAddr.sin_port));
#endif
if (BIND(tcp_sock,
@@ -1334,8 +1329,6 @@
GROW(tsessions,
tsessionArrayLength,
0);
- tsessions = NULL;
- tsessionArrayLength = 0;
FREENONNULL(filteredNetworks_);
MUTEX_DESTROY(&tcplock);
}
Modified: GNUnet/todo
===================================================================
--- GNUnet/todo 2005-03-20 20:28:49 UTC (rev 455)
+++ GNUnet/todo 2005-03-20 22:06:22 UTC (rev 456)
@@ -6,21 +6,23 @@
- 593??? [ gnunet-setup ]
0.7.0pre1 [4'05] (aka "preview"):
-- uni-peer testing:
- * sqlite: test concurrency with iterators
- * gnunet-pseudonym
-- multi-peer testing: (see sessiontest.c and tbenchtest.c for templates)
- * gap
- * dht / gnunet-dht-join and gnunet-dht-query
-- FSUI:
- * testing
+CODE CHANGES:
- transport refactoring:
- * HTTP: MTU change! => test with tbench!
* port knocking support? [ tricky ]
- UI features:
* date feature (Mantis #789)
* insert meta-data under URI as keyword (to allow getting meta-data from URI
only)
+TESTING / TESTCASES:
+- new HTTP transport code (gnunet-transport-check + tbench!)
+- uni-peer:
+ * sqlite: test concurrency with iterators
+- multi-peer: (see sessiontest.c and tbenchtest.c for templates)
+ * gap
+ * dht / gnunet-dht-join and gnunet-dht-query
+- FSUI
+- gnunet-pseudonym
+
0.7.0 [5'05] (aka "compatibility? what's that?"):
- Missing Features:
* resolve "FIXME 0.7"
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [GNUnet-SVN] r456 - in GNUnet: . src/transports,
grothoff <=