;;; pure-pr-ipaddr.el --- convert IP address <-> 32bit integer ;; Copyright (C) 2000 Project Pure. ;; Author: SHIMADA Mitsunobu ;; Keywords: PURE, process, TCP, network, IP adress ;; $Id: pure-pr-ipaddr.el,v 1.3 2001/07/09 16:12:51 simm Exp $ ;; This file is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation; either version 2, or (at your option) ;; any later version. ;; This file is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with GNU Emacs; see the file COPYING. If not, write to ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, ;; Boston, MA 02111-1307, USA. ;;; Commentary: ;; PURE means "Primitive Universal Relay-chat Environment" ;; PR means "process" ;;; Code: ;; IP address translation (DCC format -> IP address string) (defun pure-pr-ipaddr-decode (str) "Translate IP address, 32bit integer(DCC format) into \"aaa.bbb.ccc.ddd\" string." (if (stringp str) (let (delimiter num ip0 ip1 ip2 ip3) (if (< (length str) 9) (setq num (string-to-number str) ip0 (% num 256) num (/ num 256) ip1 (% num 256) num (/ num 256) ip2 (% num 256) ip3 (/ num 256)) (setq delimiter (- (length str) 8) num (string-to-number (substring str delimiter)) ip0 (% num 256) num (+ (/ num 256) (* 390625 ; 390625 = 5 ** 8 (string-to-number (substring str 0 delimiter)))) ip1 (% num 256) num (/ num 256) ip2 (% num 256) ip3 (/ num 256))) (format "%d.%d.%d.%d" ip3 ip2 ip1 ip0)))) ;; IP address translation (IP address number -> DCC format) (defun pure-pr-ipaddr-encode (str) "Translate IP address, aaa.bbb.ccc.ddd format into DCC-format 32bit integer." (let (res ip3 ip2 ip1 ip0 num) (save-match-data (when (string-match "^\\([0-9]+\\)\.\\([0-9]+\\)\.\\([0-9]+\\)\.\\([0-9]+\\)$" str) (setq ip3 (string-to-number (match-string 1 str)) ip2 (string-to-number (match-string 2 str)) ip1 (string-to-number (match-string 3 str)) ip0 (string-to-number (match-string 4 str)) num (+ ip1 (* 256 (+ ip2 (* 256 ip3))))) (if (< num 390625) (setq res "") (setq res (number-to-string (/ num 390625)) num (% num 390625))) (format "%s%08d" res (+ ip0 (* 256 num))))))) ;; That's all (provide 'pure-pr-ipaddr) ;;; pure-pr-ipaddr.el ends here