+++ /dev/null
-/*
- * DEAD CODE ALERT -- for whatever reason all this wonderful stuff is
- * unused. The initialization was the only code
- * exercised as of May 2009 when that was nipped.
- */
-
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#include <stdio.h>
-#include "ntp_machine.h"
-#include "ntp_fp.h"
-#include "ntp_stdlib.h"
-#include "ntp_syslog.h"
-
-#include <isc/list.h>
-#include "transmitbuff.h"
-
-/*
- * transmitbuf memory management
- */
-#define TRANSMIT_INIT 10 /* 10 buffers initially */
-#define TRANSMIT_LOWAT 3 /* when we're down to three buffers get more */
-#define TRANSMIT_INC 5 /* get 5 more at a time */
-#define TRANSMIT_TOOMANY 40 /* this is way too many buffers */
-
-/*
- * Memory allocation
- */
-static volatile u_long full_transmitbufs = 0; /* number of transmitbufs on fulllist */
-static volatile u_long free_transmitbufs = 0; /* number of transmitbufs on freelist */
-
-ISC_LIST(transmitbuf_t) free_transmit_list; /* Currently used transmit buffers */
-ISC_LIST(transmitbuf_t) full_transmit_list; /* Currently used transmit buffers */
-
-static u_long total_transmitbufs = 0; /* total transmitbufs currently in use */
-static u_long lowater_additions = 0; /* number of times we have added memory */
-
-static CRITICAL_SECTION TransmitLock;
-# define LOCK(lock) EnterCriticalSection(lock)
-# define UNLOCK(lock) LeaveCriticalSection(lock)
-
-static inline void
-initialise_buffer(transmitbuf *buff)
-{
- memset(buff, 0, sizeof(*buff));
-}
-
-static void
-add_buffer_to_freelist(transmitbuf *tb)
-{
- ISC_LIST_APPEND(free_transmit_list, tb, link);
- free_transmitbufs++;
-}
-
-static void
-create_buffers(int nbufs)
-{
- transmitbuf_t *buf;
- int i;
-
- buf = emalloc(nbufs * sizeof(*buf));
- for (i = 0; i < nbufs; i++)
- {
- initialise_buffer(buf);
- add_buffer_to_freelist(buf);
- total_transmitbufs++;
- buf++;
- }
-
- lowater_additions++;
-}
-
-extern void
-init_transmitbuff(void)
-{
- /*
- * Init buffer free list and stat counters
- */
- ISC_LIST_INIT(full_transmit_list);
- ISC_LIST_INIT(free_transmit_list);
- free_transmitbufs = total_transmitbufs = 0;
- full_transmitbufs = lowater_additions = 0;
- create_buffers(TRANSMIT_INIT);
-
- InitializeCriticalSection(&TransmitLock);
-}
-
-static void
-delete_buffer_from_full_list(transmitbuf_t *tb) {
-
- transmitbuf_t *next = NULL;
- transmitbuf_t *lbuf = ISC_LIST_HEAD(full_transmit_list);
-
- while (lbuf != NULL) {
- next = ISC_LIST_NEXT(lbuf, link);
- if (lbuf == tb) {
- ISC_LIST_DEQUEUE(full_transmit_list, lbuf, link);
- break;
- }
- else
- lbuf = next;
- }
- full_transmitbufs--;
-}
-
-extern void
-free_transmit_buffer(transmitbuf_t *rb)
-{
- LOCK(&TransmitLock);
- delete_buffer_from_full_list(rb);
- add_buffer_to_freelist(rb);
- UNLOCK(&TransmitLock);
-}
-
-
-extern transmitbuf *
-get_free_transmit_buffer(void)
-{
-
- transmitbuf_t * buffer = NULL;
- LOCK(&TransmitLock);
- if (free_transmitbufs <= 0) {
- create_buffers(TRANSMIT_INC);
- }
- buffer = ISC_LIST_HEAD(free_transmit_list);
- if (buffer != NULL)
- {
- ISC_LIST_DEQUEUE(free_transmit_list, buffer, link);
- free_transmitbufs--;
- ISC_LIST_APPEND(full_transmit_list, buffer, link);
- full_transmitbufs++;
- }
- UNLOCK(&TransmitLock);
- return (buffer);
-}
-
int request_type;
union {
recvbuf_t * recv_buf;
- transmitbuf_t * trans_buf;
+ void * trans_buf;
};
#ifdef DEBUG
struct IoCompletionInfo *link;
#endif
}
-transmitbuf_t *
-get_trans_buf()
-{
- transmitbuf_t *tb = emalloc(sizeof(*tb));
- return (tb);
-}
-
-void
-free_trans_buf(transmitbuf_t *tb)
-{
- free(tb);
-}
-
HANDLE
-get_io_event()
+get_io_event(void)
{
return( WaitableIoEventHandle );
}
+
HANDLE
-get_exit_event()
+get_exit_event(void)
{
return( WaitableExitEventHandle );
}
DWORD BytesTransferred = 0;
ULONG_PTR Key = 0;
IoCompletionInfo * lpo = NULL;
- u_long time_next_ifscan_after_error = 0;
UNUSED_ARG(NotUsed);
&Key,
(LPOVERLAPPED *) &lpo,
INFINITE);
- if (lpo == NULL)
- {
+ if (lpo == NULL) {
DPRINTF(2, ("Overlapped IO Thread Exiting\n"));
break; /* fail */
}
if (bSuccess)
errstatus = 0;
else
- {
errstatus = GetLastError();
- if (BytesTransferred == 0)
- {
- if (WSA_OPERATION_ABORTED == errstatus) {
- DPRINTF(4, ("Transfer Operation aborted\n"));
- } else if (ERROR_UNEXP_NET_ERR == errstatus) {
- /*
- * We get this error when trying to send an the network
- * interface is gone or has lost link. Rescan interfaces
- * to catch on sooner, but no more than once per minute.
- * Once ntp is able to detect changes without polling
- * this should be unneccessary
- */
- if (time_next_ifscan_after_error < current_time) {
- time_next_ifscan_after_error = current_time + 60;
- timer_interfacetimeout(current_time);
- }
- DPRINTF(4, ("sendto unexpected network error, interface may be down\n"));
- }
- }
- else
- {
- msyslog(LOG_ERR, "sendto error after %d bytes: %m", BytesTransferred);
- }
- }
/*
* Invoke the appropriate function based on
OnSocketRecv(Key, lpo, BytesTransferred, errstatus);
break;
case SOCK_SEND:
+ NTP_INSIST(0);
+ break;
case SERIAL_WRITE:
OnWriteComplete(Key, lpo, BytesTransferred, errstatus);
break;
}
#endif
-#if 0 /* transmitbuff.c unused, no need to initialize it */
- init_transmitbuff();
-#endif
-
/* Create the event used to signal an IO event
*/
WaitableIoEventHandle = CreateEvent(NULL, FALSE, FALSE, WAITABLEIOEVENTHANDLE);
return 0;
}
+
static int
OnWriteComplete(ULONG_PTR i, IoCompletionInfo *lpo, DWORD Bytes, int errstatus)
{
- transmitbuf_t *buff;
- struct interface *inter;
+ void *buff;
UNUSED_ARG(Bytes);
+ UNUSED_ARG(errstatus);
buff = lpo->trans_buf;
-
- free_trans_buf(buff);
+ free(buff);
lpo->trans_buf = NULL;
+ FreeHeap(lpo, "OnWriteComplete");
- if (SOCK_SEND == lpo->request_type) {
- switch (errstatus) {
- case WSA_OPERATION_ABORTED:
- case NO_ERROR:
- break;
-
- default:
- inter = (struct interface *)i;
- packets_notsent++;
- inter->notsent++;
- break;
- }
- }
-
- if (errstatus == WSA_OPERATION_ABORTED)
- FreeHeap(lpo, "OnWriteComplete: Socket Closed");
- else
- FreeHeap(lpo, "OnWriteComplete");
return 1;
}
/*
- * Return value is really GetLastError-style error code
- * which is a DWORD but using int, which is large enough,
- * decreases #ifdef forest in ntp_io.c harmlessly.
+ * io_completion_port_sendto() -- sendto() replacement for Windows
+ *
+ * Returns 0 after successful send.
+ * Returns -1 for any error, with the error code available via
+ * msyslog() %m, or GetLastError().
*/
int
io_completion_port_sendto(
sockaddr_u* dest
)
{
+ static u_long time_next_ifscan_after_error;
WSABUF wsabuf;
- transmitbuf_t *buff;
- DWORD Result = ERROR_SUCCESS;
+ DWORD octets_sent;
+ DWORD Result;
int errval;
int AddrLen;
- IoCompletionInfo *lpo;
- DWORD Flags;
-
- lpo = (IoCompletionInfo *) GetHeapAlloc("io_completion_port_sendto");
- if (lpo == NULL)
- return ERROR_OUTOFMEMORY;
+ wsabuf.buf = (void *)pkt;
+ wsabuf.len = len;
+ AddrLen = SOCKLEN(dest);
+ octets_sent = 0;
- if (len <= sizeof(buff->pkt)) {
- buff = get_trans_buf();
-
- if (buff == NULL) {
- msyslog(LOG_ERR, "No more transmit buffers left - data discarded");
- FreeHeap(lpo, "io_completion_port_sendto");
- return ERROR_OUTOFMEMORY;
- }
-
-
- memcpy(&buff->pkt, pkt, len);
- wsabuf.buf = buff->pkt;
- wsabuf.len = len;
-
- AddrLen = SOCKLEN(dest);
- lpo->request_type = SOCK_SEND;
- lpo->trans_buf = buff;
- Flags = 0;
-
- Result = WSASendTo(inter->fd, &wsabuf, 1, NULL, Flags,
- &dest->sa, AddrLen,
- (LPOVERLAPPED)lpo, NULL);
-
- if(Result == SOCKET_ERROR)
- {
- errval = WSAGetLastError();
- switch (errval) {
-
- case NO_ERROR :
- case WSA_IO_PENDING :
- Result = ERROR_SUCCESS;
- break ;
+ Result = WSASendTo(inter->fd, &wsabuf, 1, &octets_sent, 0,
+ &dest->sa, AddrLen, NULL, NULL);
+ if (SOCKET_ERROR == Result) {
+ errval = GetLastError();
+ if (ERROR_UNEXP_NET_ERR == errval) {
/*
- * Something bad happened
+ * We get this error when trying to send if the
+ * network interface is gone or has lost link.
+ * Rescan interfaces to catch on sooner, but no
+ * more often than once per minute. Once ntpd
+ * is able to detect changes without polling
+ * this should be unneccessary
*/
- default :
- msyslog(LOG_ERR,
- "WSASendTo(%s) error %d: %s",
- stoa(dest), errval, strerror(errval));
- free_trans_buf(buff);
- lpo->trans_buf = NULL;
- FreeHeap(lpo, "io_completion_port_sendto");
- break;
+ if (time_next_ifscan_after_error < current_time) {
+ time_next_ifscan_after_error = current_time + 60;
+ timer_interfacetimeout(current_time);
}
- }
-#ifdef DEBUG
- if (debug > 3)
- printf("WSASendTo - %d bytes to %s : %d\n", len, stoa(dest), Result);
-#endif
- return (Result);
+ DPRINTF(4, ("sendto unexpected network error, interface may be down\n"));
+ } else
+ msyslog(LOG_ERR, "WSASendTo(%s) error %m",
+ stoa(dest));
+ SetLastError(errval);
+ return -1;
}
- else {
-#ifdef DEBUG
- if (debug) printf("Packet too large: %d Bytes\n", len);
-#endif
- return ERROR_INSUFFICIENT_BUFFER;
+
+ if (len != (int)octets_sent) {
+ msyslog(LOG_ERR, "WSASendTo(%s) sent %u of %d octets",
+ stoa(dest), octets_sent, len);
+ SetLastError(ERROR_BAD_LENGTH);
+ return -1;
}
+
+ DPRINTF(4, ("sendto %s %d octets\n", stoa(dest), len));
+
+ return 0;
}
const void *data,
unsigned int count)
{
- transmitbuf_t *buff;
+ void *buff;
IoCompletionInfo *lpo;
DWORD BytesWritten;
- if (count > sizeof buff->pkt) {
-#ifdef DEBUG
- if (debug) {
- printf("async_write: %d bytes too large, limit is %d\n",
- count, sizeof buff->pkt);
- exit(-1);
- }
-#endif
- errno = ENOMEM;
- return -1;
- }
-
- buff = get_trans_buf();
- lpo = (IoCompletionInfo *) GetHeapAlloc("async_write");
-
- if (! buff || ! lpo) {
- if (buff) {
- free_trans_buf(buff);
- DPRINTF(1, ("async_write: out of memory\n"));
- } else
- msyslog(LOG_ERR, "No more transmit buffers left - data discarded");
-
+ buff = emalloc(count);
+ lpo = GetHeapAlloc("async_write");
+ if (lpo == NULL) {
+ free(buff);
+ DPRINTF(1, ("async_write: out of memory\n"));
errno = ENOMEM;
return -1;
}
lpo->request_type = SERIAL_WRITE;
lpo->trans_buf = buff;
- memcpy(&buff->pkt, data, count);
+ memcpy(buff, data, count);
- if (!WriteFile((HANDLE)_get_osfhandle(fd), buff->pkt, count,
+ if (!WriteFile((HANDLE)_get_osfhandle(fd), buff, count,
&BytesWritten, (LPOVERLAPPED)lpo)
&& ERROR_IO_PENDING != GetLastError()) {
msyslog(LOG_ERR, "async_write - error %m");
- free_trans_buf(buff);
- lpo->trans_buf = NULL;
+ free(buff);
FreeHeap(lpo, "async_write");
errno = EBADF;
return -1;