minor fixes
authorRalph Ronnquist <ralph.ronnquist@gmail.com>
Mon, 8 May 2023 10:16:27 +0000 (20:16 +1000)
committerRalph Ronnquist <ralph.ronnquist@gmail.com>
Mon, 8 May 2023 10:16:27 +0000 (20:16 +1000)
rwhod-sniffer.lsp

index f39e8418e13356f2a7f77c3f589dde08c84aec56..3572dca809fafb66d281a9ab2e49bcdd591d959e 100755 (executable)
@@ -1,27 +1,29 @@
 #!/usr/bin/newlisp
 #
-# Use  pcap to sniff out rwhod broadcasts on an        interface, and
-# store        select ones at  /var/spool/rwho/rwho.$HOST
-
-(define (die)
-  (write-line 2 (join (map string (args)) " "))
-  (exit 1))
-
-;(signal 2 'die) ;; THIS DOESN'T WORK??
+# Use pcap to sniff out rwhod broadcasts on an interface, and store
+# select ones at /var/spool/rwho/rwho.$HOST
 
 (constant
- 'IFACE "hemmanet"
- 'HOSTS '( "mini" "borta" )
+ 'HOSTS '("ns1" "ns2" ("borta" "ns3") "ns4" "ns5" "ns6")
+ 'IFACE "dev1nsd_br"
  )
 
-(setf ERRBUF (dup "\0" 356))
-
+############################################################
 (constant
+ 'ALIAS (filter list? HOSTS)
+ 'HOSTS (union (clean list? HOSTS) (map first ALIAS))
  'PCAPLIB "/usr/lib/x86_64-linux-gnu/libpcap.so"
  'PCAP_CHAR_ENC_LOCAL 0
  'PCAP_CHAR_ENC_UTF_8 1
  )
-(import PCAPLIB "pcap_init" "int" "int" "char*")
+
+(define (die)
+  (write-line 2 (join (map string (args)) " "))
+  (exit 1))
+
+(setf ERRBUF (dup "\0" 356))
+
+;(import PCAPLIB "pcap_init" "int" "int" "char*")
 (import PCAPLIB "pcap_open_live" "void*" "char*" "int" "int" "int" "char*")
 (import PCAPLIB "pcap_loop" "int" "void*" "int" "void*" "void*")
 
@@ -52,9 +54,6 @@
                               "void*" ;; unsigned char*
                               )))
 
-    (when (!= (setf RET (pcap_init PCAP_CHAR_ENC_LOCAL ERRBUF)))
-      (die "pcap_init" RET ERRBUF))
-
     (when (= (setf HANDLE (pcap_open_live IFACE SNAP 0 0 ERRBUF)))
       (die "pcap_open_live" ERRBUF))
 
         )
 (constant 'udp_head ">uuuu"); loading data into host order
 
-(estruct 'wd_hostname (dup 'char 32))
-(struct 'wd_loadav "int" "int" "int")
-(struct 'rwhod
-        "char"        ;;    wd_vers;
-        "char"        ;;    wd_type;
-        "char" "char" ;;    wd_fill[2];
-        "int"         ;; wd_sendtime;
-        "int"         ;; wd_recvtime;
-        "wd_hostname" ;; char[32];
-        "wd_loadav"   ;; "int" "int" "int" [3];
-        "int"         ;;     wd_boottime;
-        ;; struct  whoent {
-        ;;        struct  outmp we_utmp;
-        ;;        int     we_idle;
-        ;; } wd_we[1024 / sizeof (struct whoent)];
-        )
-
 (unless (directory? "/var/spool/rwho")
   (make-dir "/var/spool/rwho"))
 
+(define (byte-order N)
+  (cpymem (pack "lu" (unpack ">lu" (+ BYTES P)) (+ BYTES P) 4)))
+
+  
 ;; Save rwhod report
 (define (receive_rwhod BYTES LEN)
   (let ((HOST ((unpack "s32" (+ BYTES 12)) 0))
-        (DATA (get-string BYTES LEN)))
+        (S0 "s4 lu lu s32 lu lu lu lu")
+        (S1 "s8 s8 Lu lu") (I BYTES) (H nil) )
     (when (member HOST HOSTS)
-      (write-file (format "/var/spool/rwho/whod.%s" HOST) DATA))))
+      (when (setf H (lookup HOST ALIAS))
+        (setf HOST H)
+        (cpymem HOST (+ BYTES 12) (+ 1 (length HOST))))
+      (cpymem (pack S0 (unpack (string ">" S0) BYTES)) BYTES 60)
+      (inc I 60)
+      (while (< I LEN)
+        (cpymem (pack S1 (unpack (string ">" S1) I)) I 28)
+        (inc I 28))
+      (write-file (format "/var/spool/rwho/whod.%s" HOST)
+                  (DATA (get-string BYTES LEN)) )
+      )))
 
 (define (receive_udp BYTES LEN)
   (let ((HDR (unpack udp_head BYTES)))