00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include <sys/socket.h>
00014 #include <netinet/in.h>
00015 #include <arpa/inet.h>
00016 #include <assert.h>
00017 #include <string.h>
00018 #include <stddef.h>
00019 #include "monitor.h"
00020 #include "debug.h"
00021 #include "mn.h"
00022
00023 #define DEBUG_FLAG 'm'
00024
00025 extern struct monitor_global_data mon_conf;
00026 extern struct monitor_interface mon_devs[MAX_INTERFACES];
00027
00028
00029 int next(int p)
00030 {
00031 assert(p >= 0);
00032 if (p == 9)
00033 return 0;
00034 return p + 1;
00035
00036 }
00037
00038
00039 int prev(int p)
00040 {
00041 assert(p >= 0);
00042 if (p == 0)
00043 return 9;
00044 return p - 1;
00045 }
00046
00047
00048
00049
00050
00051
00052 int drop_worst(struct timeval *tstamp, struct monitor_interface *dev)
00053 {
00054 int worst_avg = 1000;
00055 int worst_index = -2, i;
00056
00057
00058
00059 for (i = 0; i < MAX_MONITOR; i++) {
00060
00061
00062
00063 if (dev->mon[i].added.tv_sec +
00064 mon_conf.worst_max_time < tstamp->tv_sec &&
00065 dev->mon[i].avg == 0)
00066 return i;
00067 }
00068
00069
00070 for (i = 0; i < MAX_MONITOR; i++) {
00071 if ((dev->mon[i].added.tv_sec +
00072 mon_conf.worst_min_time) > tstamp->tv_sec)
00073 return -1;
00074
00075 if (dev->mon[i].avg < worst_avg) {
00076 worst_avg = dev->mon[i].avg;
00077 worst_index = i;
00078 }
00079 }
00080
00081 if (worst_index >= 0 && worst_index != mon_conf.current_fa)
00082 return worst_index;
00083
00084
00085 return -1;
00086 }
00087
00088
00089 int get_free_slot(struct timeval *tstamp, struct monitor_interface *dev)
00090 {
00091 int i, ph;
00092
00093 for (i = 0; i < MAX_MONITOR; i++) {
00094 if (dev->mon[i].active != 1)
00095 return i;
00096 }
00097
00098
00099 ph = drop_worst(tstamp, dev);
00100 if (ph < 0) {
00101 DEBUG(DEBUG_FLAG, "get_free_slot: Working set is full, "
00102 "worst entry just changed. Not dropped.\n");
00103 return -1;
00104 }
00105
00106 DEBUG(DEBUG_FLAG, "get_free_slot: Working set is full, "
00107 "worst entry dropped (%s).\n",
00108 inet_ntoa(dev->mon[ph].adv->addr));
00109 return ph;
00110 }
00111
00112
00113 struct monitor_interface *monitor_get_dev(char *ifname)
00114 {
00115 int i;
00116
00117 DEBUG(DEBUG_FLAG, "monitor_get_dev: %s (len %d), interfaces %d\n",
00118 ifname, strlen(ifname), mon_conf.interfaces);
00119 if (ifname == NULL) {
00120 DEBUG(DEBUG_FLAG, "monitor_get_dev: ifname == NULL!\n");
00121 return NULL;
00122 }
00123
00124 for(i = 0; i< MAX_INTERFACES; i++) {
00125 if (mon_devs[i].in_use &&
00126 !strncmp(ifname, mon_devs[i].ifname, IFNAMSIZ)) {
00127 return &mon_devs[i];
00128 }
00129 }
00130
00131 DEBUG(DEBUG_FLAG, "monitor_get_dev: interface %s not found\n",
00132 ifname);
00133 return NULL;
00134 }
00135
00136
00137 void print_entries(void)
00138 {
00139 int i, j;
00140
00141 for (j = 0; j < MAX_INTERFACES; j++) {
00142 if (!mon_devs[j].in_use)
00143 continue;
00144 for (i = 0; i < MAX_MONITOR; i++) {
00145 if (mon_devs[j].mon[i].active == 1) {
00146 DEBUG(DEBUG_FLAG, "%-15s: prio %d\n",
00147 inet_ntoa(mon_devs[j].mon[i].adv->addr),
00148 mon_devs[j].mon[i].adv->priority);
00149 }
00150 }
00151 }
00152 }