summaryrefslogtreecommitdiffstats
path: root/usr.sbin/xntpd/xntpd/ntp_monitor.c
diff options
context:
space:
mode:
Diffstat (limited to 'usr.sbin/xntpd/xntpd/ntp_monitor.c')
-rw-r--r--usr.sbin/xntpd/xntpd/ntp_monitor.c130
1 files changed, 71 insertions, 59 deletions
diff --git a/usr.sbin/xntpd/xntpd/ntp_monitor.c b/usr.sbin/xntpd/xntpd/ntp_monitor.c
index e2d2eb5..55bd5d5 100644
--- a/usr.sbin/xntpd/xntpd/ntp_monitor.c
+++ b/usr.sbin/xntpd/xntpd/ntp_monitor.c
@@ -1,6 +1,7 @@
-/* ntp_monitor.c,v 3.1 1993/07/06 01:11:21 jbj Exp
+/*
* ntp_monitor.c - monitor who is using the xntpd server
*/
+#include <stdio.h>
#include <sys/types.h>
#include <signal.h>
#include <errno.h>
@@ -33,6 +34,8 @@
* hit the memory limit. Then we free memory by grabbing entries off
* the tail for the MRU list, unlinking from the hash table, and
* reinitializing.
+ *
+ * trimmed back memory consumption ... jdg 8/94
*/
/*
@@ -40,9 +43,15 @@
* with the illicit knowlege that we can only return somewhat less
* than 8K bytes in a mode 7 response packet, and that each structure
* will require about 20 bytes of space in the response.
+ *
+ * ... I don't believe the above is true anymore ... jdg
*/
-#define MAXMONMEM 400 /* we allocate up to 400 structures */
+#ifndef MAXMONMEM
+#define MAXMONMEM 600 /* we allocate up to 400 structures */
+#endif
+#ifndef MONMEMINC
#define MONMEMINC 40 /* allocate them 40 at a time */
+#endif
/*
* Hashing stuff
@@ -55,17 +64,15 @@
* Pointers to the hash table, the MRU list and the count table. Memory
* for the hash and count tables is only allocated if monitoring is turned on.
*/
-static struct mon_data *mon_hash; /* Pointer to array of hash buckets */
-static int *mon_hash_count; /* Point to hash count stats keeper */
+static struct mon_data *mon_hash[MON_HASH_SIZE]; /* array of list ptrs */
struct mon_data mon_mru_list;
struct mon_data mon_fifo_list;
/*
* List of free structures structures, and counters of free and total
* structures. The free structures are linked with the hash_next field.
*/
-static struct mon_data *mon_free;
+static struct mon_data *mon_free; /* the free list or null if none */
-static int mon_free_mem; /* number of structures on free list */
static int mon_total_mem; /* total number of structures allocated */
static int mon_mem_increments; /* number of times we've called malloc() */
@@ -79,9 +86,10 @@ static int mon_have_memory;
/*
* Imported from the timer module
*/
-extern U_LONG current_time;
+extern u_long current_time;
static void mon_getmoremem P((void));
+static void remove_from_hash P((struct mon_data *));
/*
* init_mon - initialize monitoring global data
@@ -96,12 +104,10 @@ init_mon()
mon_enabled = MON_OFF;
mon_have_memory = 0;
- mon_free_mem = 0;
mon_total_mem = 0;
mon_mem_increments = 0;
- mon_free = 0;
- mon_hash = 0;
- mon_hash_count = 0;
+ mon_free = NULL;
+ memset((char *)&mon_hash[0], 0, sizeof mon_hash);
memset((char *)&mon_mru_list, 0, sizeof mon_mru_list);
memset((char *)&mon_fifo_list, 0, sizeof mon_fifo_list);
}
@@ -114,8 +120,6 @@ void
mon_start(mode)
int mode;
{
- register struct mon_data *md;
- register int i;
if (mon_enabled != MON_OFF) {
mon_enabled |= mode;
@@ -125,26 +129,13 @@ mon_start(mode)
return; /* Ooops.. */
if (!mon_have_memory) {
- mon_hash = (struct mon_data *)
- emalloc(MON_HASH_SIZE * sizeof(struct mon_data));
- memset((char *)mon_hash, 0,
- MON_HASH_SIZE*sizeof(struct mon_data));
- mon_hash_count = (int *)emalloc(MON_HASH_SIZE * sizeof(int));
- mon_free_mem = 0;
mon_total_mem = 0;
mon_mem_increments = 0;
- mon_free = 0;
+ mon_free = NULL;
mon_getmoremem();
mon_have_memory = 1;
}
- md = mon_hash;
- for (i = 0; i < MON_HASH_SIZE; i++, md++) {
- md->hash_next = md;
- md->hash_prev = md;
- *(mon_hash_count + i) = 0;
- }
-
mon_mru_list.mru_next = &mon_mru_list;
mon_mru_list.mru_prev = &mon_mru_list;
@@ -162,7 +153,7 @@ void
mon_stop(mode)
int mode;
{
- register struct mon_data *md;
+ register struct mon_data *md, *md_next;
register int i;
if (mon_enabled == MON_OFF)
@@ -177,15 +168,14 @@ mon_stop(mode)
/*
* Put everything back on the free list
*/
- md = mon_hash;
- for (i = 0; i < MON_HASH_SIZE; i++, md++) {
- if (md->hash_next != md) {
- md->hash_prev->hash_next = mon_free;
- mon_free = md->hash_next;
- mon_free_mem += *(mon_hash_count + i);
- md->hash_next = md;
- md->hash_prev = md;
- *(mon_hash_count + i) = 0;
+ for (i = 0; i < MON_HASH_SIZE; i++) {
+ md = mon_hash[i]; /* get next list */
+ mon_hash[i] = NULL; /* zero the list head */
+ while (md != NULL) {
+ md_next = md->hash_next;
+ md->hash_next = mon_free;
+ mon_free = md;
+ md = md_next;
}
}
@@ -206,10 +196,9 @@ monitor(rbufp)
{
register struct pkt *pkt;
register struct mon_data *md;
- register U_LONG netnum;
+ register u_long netnum;
register int hash;
register int mode;
- register struct mon_data *mdhash;
if (mon_enabled == MON_OFF)
return;
@@ -219,9 +208,11 @@ monitor(rbufp)
hash = MON_HASH(netnum);
mode = PKT_MODE(pkt->li_vn_mode);
- md = (mon_hash + hash)->hash_next;
- while (md != (mon_hash + hash)) {
- if (md->rmtadr == netnum && md->mode == (u_char)mode) {
+ md = mon_hash[hash];
+ while (md != NULL) {
+ if (md->rmtadr == netnum &&
+ /* ?? md->interface == rbufp->dstadr && ?? */
+ md->mode == (u_char)mode) {
md->lasttime = current_time;
md->count++;
md->version = PKT_VERSION(pkt->li_vn_mode);
@@ -248,16 +239,16 @@ monitor(rbufp)
* guy. Get him some memory, either from the free list
* or from the tail of the MRU list.
*/
- if (mon_free_mem == 0 && mon_total_mem >= MAXMONMEM) {
+ if (mon_free == NULL && mon_total_mem >= MAXMONMEM) {
/*
* Get it from MRU list
*/
md = mon_mru_list.mru_prev;
md->mru_prev->mru_next = &mon_mru_list;
mon_mru_list.mru_prev = md->mru_prev;
- md->hash_next->hash_prev = md->hash_prev;
- md->hash_prev->hash_next = md->hash_next;
- *(mon_hash_count + MON_HASH(md->rmtadr)) -= 1;
+
+ remove_from_hash(md);
+
/*
* Get it from FIFO list
*/
@@ -265,11 +256,10 @@ monitor(rbufp)
md->fifo_next->fifo_prev = md->fifo_prev;
} else {
- if (mon_free_mem == 0)
- mon_getmoremem();
+ if (mon_free == NULL) /* if free list empty */
+ mon_getmoremem(); /* then get more */
md = mon_free;
mon_free = md->hash_next;
- mon_free_mem--;
}
/*
@@ -282,18 +272,19 @@ monitor(rbufp)
md->rmtport = NSRCPORT(&rbufp->recv_srcadr);
md->mode = (u_char) mode;
md->version = PKT_VERSION(pkt->li_vn_mode);
+ md->interface = rbufp->dstadr;
+ md->cast_flags = ((rbufp->dstadr->flags & INT_MULTICAST) &&
+ rbufp->fd == md->interface->fd) ? MDF_MCAST: rbufp->fd ==
+ md->interface->bfd ? MDF_BCAST : MDF_UCAST;
/*
- * Shuffle him into the hash table, inserting him at the
- * end. Also put him on top of the MRU list
+ * Drop him into front of the hash table.
+ * Also put him on top of the MRU list
* and at bottom of FIFO list
*/
- mdhash = mon_hash + MON_HASH(netnum);
- md->hash_next = mdhash;
- md->hash_prev = mdhash->hash_prev;
- mdhash->hash_prev->hash_next = md;
- mdhash->hash_prev = md;
- *(mon_hash_count + MON_HASH(netnum)) += 1;
+
+ md->hash_next = mon_hash[hash];
+ mon_hash[hash] = md;
md->mru_next = mon_mru_list.mru_next;
md->mru_prev = &mon_mru_list;
@@ -315,7 +306,7 @@ mon_getmoremem()
{
register struct mon_data *md;
register int i;
- struct mon_data *freedata;
+ struct mon_data *freedata; /* 'old' free list (null) */
md = (struct mon_data *)emalloc(MONMEMINC * sizeof(struct mon_data));
freedata = mon_free;
@@ -331,7 +322,28 @@ mon_getmoremem()
*/
md->hash_next = freedata;
- mon_free_mem += MONMEMINC;
mon_total_mem += MONMEMINC;
mon_mem_increments++;
}
+
+static void
+remove_from_hash(md)
+struct mon_data *md;
+{ register int hash;
+ register struct mon_data *md_prev;
+
+ hash = MON_HASH(md->rmtadr);
+ if (mon_hash[hash] == md) {
+ mon_hash[hash] = md->hash_next;
+ } else {
+ md_prev = mon_hash[hash];
+ while (md_prev->hash_next != md) {
+ md_prev = md_prev->hash_next;
+ if (md_prev == NULL) {
+ /* logic error */
+ return;
+ }
+ }
+ md_prev->hash_next = md->hash_next;
+ }
+}
OpenPOWER on IntegriCloud