summary refs log tree commit diff
path: root/lisp/network-utils.el
blob: 3e8a7aeda6e7b3da5d079a606b87fd10a411d060 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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