From 95f8aa4b0cbe3c1541b30fe0511b26e5caa612c3 Mon Sep 17 00:00:00 2001 From: Andreas Gerler Date: Fri, 27 Jun 2014 11:40:40 +0200 Subject: [PATCH] added ipcalc.el from https://github.com/dotemacs/ipcalc.el Signed-off-by: Andreas Gerler --- init.el | 3 + ipcalc.el | 189 +++++++++++++++++++++++++++++++++++++++++++++++++++++ ipcalc.elc | Bin 0 -> 4556 bytes 3 files changed, 192 insertions(+) create mode 100644 ipcalc.el create mode 100644 ipcalc.elc diff --git a/init.el b/init.el index d9ea03c1..23b3d163 100644 --- a/init.el +++ b/init.el @@ -563,6 +563,9 @@ as the default task." (setq recentf-max-menu-items 42) (global-set-key "\C-cr" 'recentf-open-files) +;;; * ipcalc https://github.com/dotemacs/ipcalc.el +(require 'ipcalc) + ;;; * Some nice functions ;;(blink-matching-paren 1) ;;(paren-activate) diff --git a/ipcalc.el b/ipcalc.el new file mode 100644 index 00000000..59fdec76 --- /dev/null +++ b/ipcalc.el @@ -0,0 +1,189 @@ +;; ipcalc.el --- IP subnet calculator + +;; Filename: ipcalc.el +;; Description: IP calculator +;; Author: "Aleksandar Simic" +;; License: BSD +;; Created: 2012-03-11 17:10 +;; Version: 0.2.1 +;; URL: http://github.org/dotemacs/ipcalc.el +;; Keywords: networking + +;;; Commentary: +;; +;; Usage: evaluate (ipcalc "192.168.0.23/21") + +;;; This file is NOT part of GNU Emacs +;; +;; Copyright (c) 2012, Aleksandar Simic +;; All rights reserved. +;; +;; Redistribution and use in source and binary forms, with or without +;; modification, are permitted provided that the following conditions +;; are met: +;; 1. Redistributions of source code must retain the above copyright +;; notice, this list of conditions and the following disclaimer. +;; 2. Redistributions in binary form must reproduce the above +;; copyright notice, this list of conditions and the following +;; disclaimer in the documentation and/or other materials provided +;; with the distribution. +;; +;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +;; FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +;; COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +;; INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +;; (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +;; SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +;; HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +;; STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +;; ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED +;; OF THE POSSIBILITY OF SUCH DAMAGE. + +;;; Code + +(defconst cidr-default 32 "CIDR value") + +(defun int-to-bin-string (n &optional length) + ;; 08 Jun 1997 Jamie Zawinski comp.emacs + "Convert integer N to bit string (LENGTH, default 8)." + (let* ((i 0) + (len (or length 8)) + (s (make-string len ?0))) + (while (< i len) + (if (not (zerop (logand n (ash 1 i)))) + (aset s (- len (1+ i)) ?1)) + (setq i (1+ i)) + ) + s)) + +(defun octets-as-binary (list-of-octets) + "return LST of octets as a single binary string" + (let ((binary "")) + (while list-of-octets + (setq binary + (concat binary + (int-to-bin-string + (string-to-int (car list-of-octets))))) + (setq list-of-octets (cdr list-of-octets))) + binary)) + +(defun ip-to-octets (ip) + "split IP address and return the octets" + (let ((octets (split-string ip "\\."))) + (if (= 4 (length octets)) + octets + (message "not correct IP format")))) + +(defun ones-and-pad (num) + "return 1's equal to NUM and pad the rest up to 32" + (unless (and (<= num cidr-default) (> num 0)) + (error "wrong value provided")) + (concat (make-string num ?1) + (make-string (- cidr-default num) ?0))) + +(defun invert-binary (num) + "Invert 1s to 0s and vice versa + + (credit: the simpler version below re-writen by pjb from #emacs + on freenode)" + (map 'string + (lambda (ch) (aref "10" (position ch "01"))) + (remove-if-not (lambda (ch) (position ch "01")) num))) + +(defun network (ip cidr) + "Takes IP & CIDR and produces network" + (let ((cidr-as-int (string-to-int cidr))) + (concat + (substring (octets-as-binary + (ip-to-octets ip)) 0 cidr-as-int) + (make-string (- cidr-default cidr-as-int) ?0)))) + +(defun host+1 (binary-ip) + "increment the given BINARY-IP by 1" + (let ((tmp binary-ip)) + (aset tmp (- cidr-default 1) ?1) + tmp)) + +(defun host-max (ip cidr) + (let ((count (string-to-int cidr)) + (max (- cidr-default 1))) + (while (< count max) + (aset ip count ?1) + (setq count(incf count))) + ip)) + +(defun hosts/net (num) + "Calculate Hosts/Net for the NUM given" + (- (expt 2 (- cidr-default num)) 2)) + +(defmacro list-to-string (lst) + "convert LST to string" + `(mapconcat + '(lambda (val) + (identity (if (integerp val) + (int-to-string val) + val))) + ,lst "")) + +(defun insert-values (lst) + "insert given LST" + (insert + (list-to-string lst))) + +(defun bin-to-int (bin) + "Convert binary to integer" + (int-to-string (read (concat "#b" bin)))) + +(defun binary-to-ip (binary) + "convert binary to IP address" + (let* (full-ip + (count 0) + (1st-octet (substring binary 0 8)) + (2nd-octet (substring binary 8 16)) + (3rd-octet (substring binary 16 24)) + (4th-octet (substring binary 24 32)) + (octets (mapcar + 'bin-to-int + `(,1st-octet ,2nd-octet ,3rd-octet ,4th-octet)))) + (while (< count 3) + (setq full-ip (concat full-ip (nth count octets) ".")) + (setq count (incf count))) + (concat full-ip (car (last octets))))) + +(defun ipcalc (ip/cidr) + "IP calculator" + (let* ((ip (car (split-string ip/cidr "/"))) + (ip-in-binary (octets-as-binary (ip-to-octets ip))) + (cidr (car (cdr (split-string ip/cidr "/")))) + (cidr-int (string-to-int cidr)) + (cidr-binary (ones-and-pad cidr-int)) + (wildcard-binary (invert-binary (ones-and-pad cidr-int))) + (wildcard-ip (binary-to-ip wildcard-binary)) + (netmask (binary-to-ip (invert-binary wildcard-binary))) + (net-binary (network ip cidr)) + (net-ip (binary-to-ip net-binary)) + (host-max-binary (host-max (network ip cidr) cidr)) + (host-max-ip (binary-to-ip host-max-binary)) + (host-min-binary (host+1 (network ip cidr))) + (host-min-ip (binary-to-ip host-min-binary)) + (broadcast-binary (host+1 (host-max net-binary cidr))) + (broadcast-ip (binary-to-ip broadcast-binary)) + (buffer "*ipcalc*")) + (if (get-buffer buffer) + (kill-buffer buffer)) + (pop-to-buffer buffer) + (insert + (format "Address:%15s%41s\n" ip ip-in-binary) + (format "Netmask:%16s = %2s %34s\n" netmask cidr cidr-binary) + (format "Wildcard:%11s%44s\n" wildcard-ip wildcard-binary) + (format "=>\nNetwork:%14s%42s\n" net-ip (network ip cidr)) + (format "HostMin:%14s%42s\n" host-min-ip host-min-binary) + (format "HostMax:%16s%40s\n" host-max-ip host-max-binary) + (format "Broadcast:%14s%40s\n" broadcast-ip broadcast-binary) + (format "Hosts/Net: %d\n" (hosts/net cidr-int))))) + +(provide 'ipcalc) + +;;; ipcalc.el ends here diff --git a/ipcalc.elc b/ipcalc.elc new file mode 100644 index 0000000000000000000000000000000000000000..3fa36635495926b177744a7ee78b9fae35250b54 GIT binary patch literal 4556 zcmbtY`*YjI5#~<~NsT$OGtfN1_1;B zB`be@zr8z9gyWh1P|gszyS>}n{q{*7J%9P^FRfOqdvtU}&$2}xC((?i*A$#bMG_Uq z=UFy84;E2bEoX67j>2p~SxR3QF@3vC$)1p99XgXk(xH$;*e%pSXM=xo|j*OT9g$9vl&!c-D4e$?1NEv5YK54q%#^! zvt>HVX-7zBC^3fJ4h{Z_9Yr*!289NeL4zbp&mmlcG8_)+?@z?ZtoV?5o`n~Ohc$PK znseO7pMezJNTRaLo6JcUg6p`XE4D^Ofm`%{ui)IFtDq#szg>CButZ_iVAQL z0K(w^*QeX;mnwp*(JQJln#L7L2x#!~`KxbEUwlb5ll|dHUJxWP^xVNgqsokFoEqxL z)~|z<9<#4xX^@aK(U5fCF}=?^zHN>_X<%Ojm>h82Q{Iq@g3s$`o-?q&5D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