summary refs log tree commit diff
diff options
context:
space:
mode:
authorFranck Cuny <franck@fcuny.net>2024-09-30 19:19:02 -0700
committerFranck Cuny <franck@fcuny.net>2024-09-30 19:19:02 -0700
commitf812d7e040ea3cc409cc38d53211b8f43267511e (patch)
tree8ee7f90ef0686dca172e56b9118acbb4eadc1833
parentadd SLO calculator (diff)
downloademacs.d-f812d7e040ea3cc409cc38d53211b8f43267511e.tar.gz
network related functions
-rw-r--r--init.el1
-rw-r--r--lisp/network-utils.el30
2 files changed, 31 insertions, 0 deletions
diff --git a/init.el b/init.el
index e58528e..66bc5ea 100644
--- a/init.el
+++ b/init.el
@@ -50,6 +50,7 @@
 
 (require 'my-denote)
 (require 'my-uptime)
+(require 'm-network-utils)
 
 (report-time-since-load)
 
diff --git a/lisp/network-utils.el b/lisp/network-utils.el
new file mode 100644
index 0000000..3e8a7ae
--- /dev/null
+++ b/lisp/network-utils.el
@@ -0,0 +1,30 @@
+;;; network-utils.el --- utils for network stuff -*- lexical-binding: t -*-
+;; Author: Franck Cuny <franck@fcuny.net>
+
+;;; Commentary:
+
+;; commentary
+
+;;; Code:
+
+(defun int-to-ipv4 (integer)
+  "Convert an INTEGER to an IPv4 address string."
+  (format "%d.%d.%d.%d"
+          (logand (ash integer -24) 255)
+          (logand (ash integer -16) 255)
+          (logand (ash integer -8) 255)
+          (logand integer 255)))
+
+(defun ipv4-to-int (ipv4)
+  "Convert an IPV4 address string to an INTEGER."
+  (let ((octets (mapcar #'string-to-number (split-string ipv4 "\\."))))
+    (if (= (length octets) 4)
+        (+ (ash (nth 0 octets) 24)
+           (ash (nth 1 octets) 16)
+           (ash (nth 2 octets) 8)
+           (nth 3 octets))
+      (error "Invalid IPv4 address format"))))
+
+(provide 'network-utils)
+
+;;; network-utils.el ends here