Mailinglist archives

[list-arpalert] Re: arpalert.c invald argument on FreeBSD

From: Fumiyuki Shimizu <fumifumi_at_abacustech.jp>
Date: Fri, 23 Oct 2009 10:29:21 +0900 (JST)

Hi,

Workaround patch for FreeBSD ports system is commited,
http://www.freebsd.org/cgi/query-pr.cgi?pr=139814
so EINVAL may not occur on the system of FreeBSD ports users after the
refresh.

I attached a patch file again, to replace the func_time.c patch I
sent, breaking portability. This is for 2.0.11 too.

Cheers,
Fumiyuki Shimizu
fumifumi_at_abacustech.jp

diff -a -u -b -r /tmp/arpalert-2.0.11/alerte.c arpalert-2.0.11/alerte.c
--- /tmp/arpalert-2.0.11/alerte.c 2008-04-01 03:36:42.000000000 +0900
+++ arpalert-2.0.11/alerte.c 2009-10-21 22:09:43.000000000 +0900
@@ -212,7 +212,7 @@
 
                 // if time exceed
                 if(return_code == 0 &&
- time_comp(&current_t, &(check->time)) == BIGEST){
+ time_comp(&current_t, &(check->time)) > 0){
                         logmsg(LOG_ERR, "kill pid %i: running time exceeded",
                                check->pid);
 
diff -a -u -b -r /tmp/arpalert-2.0.11/arpalert.c arpalert-2.0.11/arpalert.c
--- /tmp/arpalert-2.0.11/arpalert.c 2008-04-01 03:36:42.000000000 +0900
+++ arpalert-2.0.11/arpalert.c 2009-10-21 22:09:43.000000000 +0900
@@ -159,7 +159,7 @@
                         // if timeout is returned
                         if(temp_timeout.tv_sec != -1){
                                 if(cur_timeout.tv_sec != -1){
- if(time_comp(&cur_timeout, &temp_timeout) == BIGEST){
+ if(time_comp(&cur_timeout, &temp_timeout) > 0){
                                                 cur_timeout.tv_sec = temp_timeout.tv_sec;
                                                 cur_timeout.tv_usec = temp_timeout.tv_usec;
                                                 check_timeout = check_temp;
@@ -179,13 +179,21 @@
                 if(cur_timeout.tv_sec != -1){
                    time_sous(&cur_timeout, &current_t, &timeout);
 
+ // add 10000(I5(Bs for prevent premature timeout
+ /* this might not be necessary now. */
+ timeout.tv_usec += 10000;
+
+ /* avoid EINVAL of select() */
+ normalize_timeval (&timeout, &timeout);
+
                         // prevent negative timeout
- if(timeout.tv_sec < 0){
- timeout.tv_usec = 0;
+ if(timeout.tv_sec < 0 || (timeout.tv_sec == 0 && timeout.tv_usec < 10000)){
+ /* Use normalized values */
+ timeout.tv_usec = 10000;
                                 timeout.tv_sec = 0;
                         }
- // add 10000(I5(Bs for prevent premature timeout
- timeout.tv_usec += 10000;
+
+ memcpy (&temp_timeout, &timeout, sizeof (timeout));
                         tmout = &timeout;
 
                 // if no timeout
@@ -197,20 +205,25 @@
                 selret = select(max_filed + 1, &read_filed_set,
                                 NULL, NULL, tmout);
 
- // maj current hour
- gettimeofday(&current_t, NULL);
-
                 // errors:
- #if (__NetBSD__)
- if (selret == -1 && errno != EINTR && errno != EINVAL){
- #else
                 if (selret == -1 && errno != EINTR){
- #endif
+ if (NULL == tmout) {
                         logmsg(LOG_ERR, "[%s %i] select[%d]: %s",
                                __FILE__, __LINE__, errno, strerror(errno));
+ } else {
+ logmsg(LOG_ERR, "[%s %i] select(%d, %ld %ld)[%d]: %s",
+ __FILE__, __LINE__,
+ max_filed + 1,
+ temp_timeout.tv_sec,
+ temp_timeout.tv_usec,
+ errno, strerror(errno));
+ }
                         exit(1);
                 }
 
+ // maj current hour. This may overwite errno.
+ gettimeofday(&current_t, NULL);
+
                 // timeouts
                 if(selret == 0){
                         if(check_timeout != NULL){
diff -a -u -b -r /tmp/arpalert-2.0.11/capture.c arpalert-2.0.11/capture.c
--- /tmp/arpalert-2.0.11/capture.c 2008-04-01 03:36:42.000000000 +0900
+++ arpalert-2.0.11/capture.c 2009-10-21 22:09:43.000000000 +0900
@@ -417,13 +417,14 @@
 // gen bitfield for select
 int cap_gen_bitfield(fd_set *bf){
         struct capt *netif;
- int fd, max = 0;
+ int fd, max = -1;
 
         netif = first_capt;
         while(netif != NULL){
- fd = pcap_fileno(netif->pcap);
+ if (0 <= (fd = pcap_fileno(netif->pcap))) { /* paranoid */
                 FD_SET(fd, bf);
                 if(fd > max) max = fd;
+ }
                 netif = netif->next;
         }
         return(max);
diff -a -u -b -r /tmp/arpalert-2.0.11/data.c arpalert-2.0.11/data.c
--- /tmp/arpalert-2.0.11/data.c 2008-04-01 03:36:42.000000000 +0900
+++ arpalert-2.0.11/data.c 2009-10-21 22:09:43.000000000 +0900
@@ -479,7 +479,7 @@
         clean = timeouts->next_chain;
         while(clean != timeouts){
         
- if(time_comp(&old_t, &(clean->timestamp)) == BIGEST){
+ if(time_comp(&old_t, &(clean->timestamp)) > 0){
 
                         // get next
                         clean_next = clean->next_chain;
@@ -591,8 +591,7 @@
 
         // if timeout and dump are sets
         // next_clean time lowest, then clen
- else if(time_comp(&dump_time,
- &next_clean) == BIGEST){
+ else if(time_comp(&dump_time, &next_clean) > 0){
                 tv->tv_sec = next_clean.tv_sec;
                 tv->tv_usec = next_clean.tv_usec;
                 return data_clean;
diff -a -u -b -r /tmp/arpalert-2.0.11/func_time.c arpalert-2.0.11/func_time.c
--- /tmp/arpalert-2.0.11/func_time.c 2008-04-01 03:36:42.000000000 +0900
+++ arpalert-2.0.11/func_time.c 2009-10-21 22:39:04.000000000 +0900
@@ -6,12 +6,27 @@
 
 #include <sys/time.h>
 
+#include "func_time.h"
+
 /* compare t1 to t2
- * si t1 > t2 => 1
+ * si t1 > t2 => >0
  * si t1 = t2 => 0
- * si t1 < t2 =>-1
+ * si t1 < t2 => <0
  */
 int time_comp(struct timeval *t1, struct timeval *t2){
+ struct timeval tt1;
+ struct timeval tt2;
+ normalize_timeval (&tt1, t1);
+ normalize_timeval (&tt2, t2);
+ if (tt1.tv_sec != tt2.tv_sec) {
+ return tt1.tv_sec > tt2.tv_sec ? 1 : -1;
+ }
+ if (tt1.tv_usec != tt2.tv_usec) {
+ return tt1.tv_usec > tt2.tv_usec ? 1 : -1;
+ }
+ return 0;
+
+ /*
         if(t1->tv_sec == t2->tv_sec && t1->tv_usec == t2->tv_usec)
                 return 0;
         else if((t1->tv_sec == t2->tv_sec && t1->tv_usec > t2->tv_usec) ||
@@ -19,13 +34,23 @@
                 return 1;
         else
                 return -1;
+ */
 }
 
 void time_sous(struct timeval *t1, struct timeval *t2, struct timeval *res){
         res->tv_sec = t1->tv_sec - t2->tv_sec;
         res->tv_usec = t1->tv_usec - t2->tv_usec;
- if(res->tv_usec < 0){
- res->tv_sec -= 1;
- res->tv_usec += 1000000;
+ normalize_timeval (res, res);
+}
+
+#define MEGA (1000000)
+
+void normalize_timeval (struct timeval *n, const struct timeval *t) {
+ if (0 <= t->tv_usec) {
+ n->tv_sec = t->tv_sec + (t->tv_usec / MEGA);
+ n->tv_usec = t->tv_usec % MEGA;
+ } else {
+ n->tv_sec = t->tv_sec + (t->tv_usec / MEGA) - 1;
+ n->tv_usec = (t->tv_usec % MEGA) + MEGA;
         }
 }
diff -a -u -b -r /tmp/arpalert-2.0.11/func_time.h arpalert-2.0.11/func_time.h
--- /tmp/arpalert-2.0.11/func_time.h 2008-04-01 03:36:42.000000000 +0900
+++ arpalert-2.0.11/func_time.h 2009-10-21 22:09:43.000000000 +0900
@@ -7,14 +7,18 @@
 #ifndef __FUNC_TIME_H__
 #define __FUNC_TIME_H__
 
+/*
+It is not the biggest/smallest even if this returns BIGEST/SMALLEST,
+since this is just a comparison of two values, not of set of values.
 #define BIGEST 1
 #define EQUAL 0
 #define SMALLEST -1
+*/
 
 /* compare t1 to t2
- * si t1 > t2 => 1
+ * si t1 > t2 => >0
  * si t1 = t2 => 0
- * si t1 < t2 => -1
+ * si t1 < t2 => <0
  */
 int time_comp(struct timeval *t1, struct timeval *t2);
 
@@ -23,4 +27,8 @@
 void time_sous(struct timeval *t1, struct timeval *t2,
                struct timeval *res);
 
+/* normalize timeval to make tv_usec to hold value between 0 to 999999.
+ n can be t itself. */
+void normalize_timeval (struct timeval *n, const struct timeval *t);
+
 #endif
diff -a -u -b -r /tmp/arpalert-2.0.11/maclist.c arpalert-2.0.11/maclist.c
--- /tmp/arpalert-2.0.11/maclist.c 2008-04-01 03:44:59.000000000 +0900
+++ arpalert-2.0.11/maclist.c 2009-10-21 22:09:43.000000000 +0900
@@ -231,7 +231,7 @@
                                 comp.tv_sec = discover.tv_sec +
                                               config[CF_TOOOLD].valeur.integer;
                                 comp.tv_usec = discover.tv_usec;
- if(time_comp(&comp, &current_t) == BIGEST){
+ if(time_comp(&comp, &current_t) > 0){
                                         // add data
                                         data_add_time(&mac, level, ip, dev, &discover);
                                 }
diff -a -u -b -r /tmp/arpalert-2.0.11/sens_timeouts.c arpalert-2.0.11/sens_timeouts.c
--- /tmp/arpalert-2.0.11/sens_timeouts.c 2008-04-01 03:36:42.000000000 +0900
+++ arpalert-2.0.11/sens_timeouts.c 2009-10-21 22:09:43.000000000 +0900
@@ -175,7 +175,7 @@
         while(look != used_start){
 
                 // if entry expires
- if(time_comp(&current_t, &(look->last)) == BIGEST){
+ if(time_comp(&current_t, &(look->last)) > 0){
 
                         // get next
                         look_next = look->next_chain;

-- 
To unsubscribe send a mail to list+unsubscribe_at_arpalert.org
Received on Fri Oct 23 2009 - 03:30:03 CEST