- FDIOBufferInit(&NW->IO.IOB, &NW->IO.RecvBuf, fd, TotalSendSize);
- }
- return eSendReply;
- }
- else
- {
- SetNWCState(IO, eNWCVSFail);
- return eAbort;
- }
-}
-
-eNextState NWC_SendREAD(AsyncNetworker *NW)
-{
- AsyncIO *IO = &NW->IO;
- eNextState rc;
-
- if (NW->IO.IOB.TotalSentAlready < NW->IO.IOB.TotalSendSize)
- {
- /*
- * If shutting down we can exit here and unlink the temp file.
- * this shouldn't loose us any messages.
- */
- if (server_shutting_down)
- {
- FDIOBufferDelete(&NW->IO.IOB);
- unlink(ChrPtr(NW->tempFileName));
- FDIOBufferDelete(&IO->IOB);
- SetNWCState(IO, eNWCVSFail);
- return eAbort;
- }
- StrBufPrintf(NW->IO.SendBuf.Buf, "READ "LOFF_T_FMT"|%ld\n",
- NW->IO.IOB.TotalSentAlready,
- NW->IO.IOB.TotalSendSize);
- return eSendReply;
- }
- else
- {
- NW->State = eCLOS;
- rc = NWC_DispatchWriteDone(&NW->IO);
-
- return rc;
- }
-}
-
-eNextState NWC_ReadREADState(AsyncNetworker *NW)
-{
- AsyncIO *IO = &NW->IO;
- if (ChrPtr(NW->IO.IOBuf)[0] == '6')
- {
- NW->IO.IOB.ChunkSendRemain =
- NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
- return eReadFile;
- }
- FDIOBufferDelete(&IO->IOB);
- return eAbort;
-}
-eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW);
-eNextState NWC_ReadREADBlob(AsyncNetworker *NW)
-{
- eNextState rc;
- if (NW->IO.IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
- {
- NW->State ++;
-
- FDIOBufferDelete(&NW->IO.IOB);
-
- if (link(ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName)) != 0) {
- syslog(LOG_ERR, "netpoll: could not link %s to %s: %s", ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName), strerror(errno));
- }
- else {
- syslog(LOG_INFO, "netpoll: moved %s to %s", ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName));
- }
-
- unlink(ChrPtr(NW->tempFileName));
- rc = NWC_DispatchWriteDone(&NW->IO);
- NW->State --;
- return rc;
- }
- else {
- NW->State --;
- NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
- return eSendReply; //NWC_DispatchWriteDone(&NW->IO);
- }
-}
-
-eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW)
-{
- eNextState rc;
-/* we don't have any data to debug print here. */
- if (NW->IO.IOB.TotalSentAlready >= NW->IO.IOB.TotalSendSize)
- {
- NW->State ++;
-
- FDIOBufferDelete(&NW->IO.IOB);
- if (link(ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName)) != 0) {
- syslog(LOG_ERR, "netpoll: could not link %s to %s: %s", ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName), strerror(errno));
- }
- else {
- syslog(LOG_INFO, "netpoll: moved %s to %s", ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName));
- }
-
- unlink(ChrPtr(NW->tempFileName));
- rc = NWC_DispatchWriteDone(&NW->IO);
- NW->State --;
- return rc;
- }
- else {
- NW->State --;
- NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
- return NWC_DispatchWriteDone(&NW->IO);
- }
-}
-eNextState NWC_SendCLOS(AsyncNetworker *NW)
-{
- AsyncIO *IO = &NW->IO;
- SetNWCState(IO, eNWCVSNDOPDone);
- StrBufPlain(NW->IO.SendBuf.Buf, HKEY("CLOS\n"));
- return eSendReply;
-}
-
-eNextState NWC_ReadCLOSReply(AsyncNetworker *NW)
-{
- AsyncIO *IO = &NW->IO;
- FDIOBufferDelete(&IO->IOB);
- if (ChrPtr(NW->IO.IOBuf)[0] != '2')
- return eTerminateConnection;
- return eSendReply;
-}
-
-
-eNextState NWC_SendNUOP(AsyncNetworker *NW)
-{
- AsyncIO *IO = &NW->IO;
- eNextState rc;
- long TotalSendSize;
- struct stat statbuf;
- int fd;
-
- SetNWCState(IO, eNWCVSNUOP);
- StrBufPrintf(NW->SpoolFileName,
- "%s/%s",
- ctdl_netout_dir,
- ChrPtr(NW->node));
- StrBufStripSlashes(NW->SpoolFileName, 1);
-
- fd = open(ChrPtr(NW->SpoolFileName), O_EXCL|O_NONBLOCK|O_RDONLY);
- if (fd < 0) {
- if (errno != ENOENT) {
- syslog(LOG_ERR, "%s: %s", ChrPtr(NW->SpoolFileName), strerror(errno));
- }
- NW->State = eQUIT;
- rc = NWC_SendQUIT(NW);
- return rc;
- }
-
- if (fstat(fd, &statbuf) == -1) {
- syslog(LOG_ERR, "%s: %s", ChrPtr(NW->SpoolFileName), strerror(errno));
- if (fd > 0) close(fd);
- return eAbort;
- }
- TotalSendSize = statbuf.st_size;
- if (TotalSendSize == 0) {
- syslog(LOG_DEBUG, "netpoll: nothing to send.");
- NW->State = eQUIT;
- rc = NWC_SendQUIT(NW);
- if (fd > 0) close(fd);
- return rc;
- }
- else
- {
- syslog(LOG_INFO, "netpoll: sending %s to %s", ChrPtr(NW->SpoolFileName), ChrPtr(NW->node));
- }
-
- FDIOBufferInit(&NW->IO.IOB, &NW->IO.SendBuf, fd, TotalSendSize);
-
- StrBufPlain(NW->IO.SendBuf.Buf, HKEY("NUOP\n"));
- return eSendReply;
-
-}
-eNextState NWC_ReadNUOPReply(AsyncNetworker *NW)
-{
- AsyncIO *IO = &NW->IO;
- if (ChrPtr(NW->IO.IOBuf)[0] != '2') {
- FDIOBufferDelete(&IO->IOB);
- return eAbort;
- }
- return eSendReply;
-}
-
-eNextState NWC_SendWRIT(AsyncNetworker *NW)
-{
- StrBufPrintf(NW->IO.SendBuf.Buf, "WRIT "LOFF_T_FMT"\n",
- NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready);
- return eSendReply;
-}
-eNextState NWC_ReadWRITReply(AsyncNetworker *NW)
-{
- AsyncIO *IO = &NW->IO;
- if (ChrPtr(NW->IO.IOBuf)[0] != '7')
- {
- FDIOBufferDelete(&IO->IOB);
- return eAbort;
- }
-
- NW->IO.IOB.ChunkSendRemain =
- NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
- return eSendFile;
-}
-
-eNextState NWC_SendBlobDone(AsyncNetworker *NW)
-{
- AsyncIO *IO = &NW->IO;
- eNextState rc;
- if (NW->IO.IOB.TotalSentAlready >= IO->IOB.TotalSendSize)
- {
- NW->State ++;
-
- FDIOBufferDelete(&IO->IOB);
- rc = NWC_DispatchWriteDone(IO);
- NW->State --;
- return rc;
- }
- else {
- NW->State --;
- IO->IOB.ChunkSendRemain = IO->IOB.ChunkSize;
- rc = NWC_DispatchWriteDone(IO);
- NW->State --;
- return rc;
- }
-}
-
-eNextState NWC_SendUCLS(AsyncNetworker *NW)
-{
- StrBufPlain(NW->IO.SendBuf.Buf, HKEY("UCLS 1\n"));
- return eSendReply;
-
-}
-eNextState NWC_ReadUCLS(AsyncNetworker *NW)
-{
- AsyncIO *IO = &NW->IO;
-
- syslog(LOG_INFO, "netpoll: sent %s (%ld octets) to <%s>", ChrPtr(NW->SpoolFileName), NW->IO.IOB.ChunkSize, ChrPtr(NW->node));
-
- if (ChrPtr(NW->IO.IOBuf)[0] == '2') {
- syslog(LOG_DEBUG, "Removing <%s>\n", ChrPtr(NW->SpoolFileName));
- unlink(ChrPtr(NW->SpoolFileName));
- }
- FDIOBufferDelete(&IO->IOB);
- SetNWCState(IO, eNWCVSNUOPDone);
- return eSendReply;
-}
-
-eNextState NWC_SendQUIT(AsyncNetworker *NW)
-{
- StrBufPlain(NW->IO.SendBuf.Buf, HKEY("QUIT\n"));
-
- return eSendReply;
-}
-
-eNextState NWC_ReadQUIT(AsyncNetworker *NW)
-{
- return eAbort;
-}
-
-
-NWClientHandler NWC_ReadHandlers[] = {
- NWC_ReadGreeting,
- NWC_ReadAuthReply,
- NWC_ReadNDOPReply,
- NWC_ReadREADState,
- NWC_ReadREADBlob,
- NWC_ReadCLOSReply,
- NWC_ReadNUOPReply,
- NWC_ReadWRITReply,
- NWC_SendBlobDone,
- NWC_ReadUCLS,
- NWC_ReadQUIT};
-
-long NWC_ConnTimeout = 100;
-
-const long NWC_SendTimeouts[] = {
- 100,
- 100,
- 100,
- 100,
- 100,
- 100,
- 100,
- 100
-};
-const ConstStr NWC[] = {
- {HKEY("Connection broken during ")},
- {HKEY("Connection broken during ")},
- {HKEY("Connection broken during ")},
- {HKEY("Connection broken during ")},
- {HKEY("Connection broken during ")},
- {HKEY("Connection broken during ")},
- {HKEY("Connection broken during ")},
- {HKEY("Connection broken during ")}
-};
-
-NWClientHandler NWC_SendHandlers[] = {
- NULL,
- NWC_SendAuth,
- NWC_SendNDOP,
- NWC_SendREAD,
- NWC_ReadREADBlobDone,
- NWC_SendCLOS,
- NWC_SendNUOP,
- NWC_SendWRIT,
- NWC_SendBlobDone,
- NWC_SendUCLS,
- NWC_SendQUIT
-};
-
-const long NWC_ReadTimeouts[] = {
- 100,
- 100,
- 100,
- 100,
- 100,
- 100,
- 100,
- 100,
- 100,
- 100
-};
-
-
-eNextState nwc_get_one_host_ip_done(AsyncIO *IO)
-{
- AsyncNetworker *NW = IO->Data;
- struct hostent *hostent;
-
- QueryCbDone(IO);
-
- hostent = NW->HostLookup.VParsedDNSReply;
- if ((NW->HostLookup.DNSStatus == ARES_SUCCESS) &&
- (hostent != NULL) ) {
- memset(&NW->IO.ConnectMe->Addr, 0, sizeof(struct in6_addr));
- if (NW->IO.ConnectMe->IPv6) {
- memcpy(&NW->IO.ConnectMe->Addr.sin6_addr.s6_addr,
- &hostent->h_addr_list[0],
- sizeof(struct in6_addr));
-
- NW->IO.ConnectMe->Addr.sin6_family = hostent->h_addrtype;
- NW->IO.ConnectMe->Addr.sin6_port = htons(atol(ChrPtr(NW->port)));//// TODO use the one from the URL.
- }
- else {
- struct sockaddr_in *addr = (struct sockaddr_in*) &NW->IO.ConnectMe->Addr;
- /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
-// addr->sin_addr.s_addr = htonl((uint32_t)&hostent->h_addr_list[0]);
- memcpy(&addr->sin_addr.s_addr,
- hostent->h_addr_list[0],
- sizeof(uint32_t));
-
- addr->sin_family = hostent->h_addrtype;
- addr->sin_port = htons(504);/// default citadel port
-