From: ueno Date: Fri, 9 Mar 2001 08:38:06 +0000 (+0000) Subject: * hostlong.ml: New file. X-Git-Tag: liece-1_4_7~32 X-Git-Url: http://git.chise.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=df6536dc46e9c7513483eade8940d4923c013347;p=elisp%2Fliece.git * hostlong.ml: New file. * dcc.ml: Use Hostlong.t instead of Naddr.{encode,decode}. * naddr.ml: Remove. --- diff --git a/dcc/dcc.ml b/dcc/dcc.ml index 0b852dc..445b24d 100644 --- a/dcc/dcc.ml +++ b/dcc/dcc.ml @@ -1,27 +1,3 @@ -(* DCC module. - -This file is part of Liece. - -Author: Daiki Ueno -Created: 1998-09-28 -Revised: 1999-01-28 -Keywords: IRC, liece, DCC - -This program 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 program 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. *) - open Unix let usage prefix progname = @@ -94,9 +70,10 @@ let send_file port filename = _ -> Printf.eprintf "Open failed.\n"; flush Pervasives.stderr; exit 1 in let size = (fstat fd).st_size in + let hl = (Hostlong.of_address_string haddr) in close fd; Printf.printf "DCC send %s %d %s %d\n" - (Filename.basename filename) port (Naddr.encode haddr) size; + (Filename.basename filename) port (Hostlong.to_string hl) size; flush Pervasives.stdout; accept_connection (fun t -> write_file filename size t) s; with @@ -121,7 +98,11 @@ let read_file filename size t = let receive_file host port size filename = let s = socket PF_INET SOCK_STREAM 0 in - connect s (ADDR_INET (inet_addr_of_string (Naddr.decode host), port)); + let hl = Hostlong.of_string host in + let inet_addr = + inet_addr_of_string (Hostlong.to_address_string hl) + in + connect s (ADDR_INET (inet_addr, port)); read_file filename size s; () let chat_loop s = @@ -174,8 +155,9 @@ let chat_listen port = ADDR_INET (addr, port) -> port | _ -> port in + let hl = Hostlong.of_address_string haddr in listen s 1; - Printf.printf "DCC chat %s %d\n" (Naddr.encode haddr) port; + Printf.printf "DCC chat %s %d\n" (Hostlong.to_string hl) port; flush Pervasives.stdout; accept_connection (fun t -> @@ -185,7 +167,11 @@ let chat_listen port = let chat_connect host port = let s = socket PF_INET SOCK_STREAM 0 in - connect s (ADDR_INET (inet_addr_of_string (Naddr.decode host), port)); + let hl = Hostlong.of_string host in + let inet_addr = + inet_addr_of_string (Hostlong.to_address_string hl) + in + connect s (ADDR_INET (inet_addr, port)); Printf.printf "DCC chat established\n"; flush Pervasives.stdout; chat_loop s; () diff --git a/dcc/hostlong.ml b/dcc/hostlong.ml new file mode 100644 index 0000000..33a89c3 --- /dev/null +++ b/dcc/hostlong.ml @@ -0,0 +1,39 @@ +type t = int32 + +let address_regexp = + let octet_regexp_string = "\([0-9][0-9]?[0-9]?\)" in + let address_regexp_string = + octet_regexp_string ^ "\." ^ octet_regexp_string ^ "\." ^ + octet_regexp_string ^ "\." ^ octet_regexp_string + in + Str.regexp ("^" ^ address_regexp_string ^ "$") + +let of_int32 = function n -> n + +let to_int32 = function n -> n + +let of_string = Int32.of_string + +let to_string = Int32.to_string + +let of_address_string string = + let n = ref Int32.zero in + if (Str.string_match address_regexp string 0) then + for i = 1 to 4 do + let octet = Int32.of_string (Str.matched_group i string) in + n := Int32.logor !n (Int32.shift_left octet (8 * (3 - i))) + done + else + failwith "hostlong_of_string"; + !n + +let to_address_string n = + let octets = ref [] in + let octet_mask = Int32.of_int 0xff in + for i = 0 to 3 do + let octet = + Int32.logand octet_mask (Int32.shift_right_logical n (i * 8)) + in + octets := (Int32.to_string octet) :: !octets + done; + String.concat "." !octets diff --git a/dcc/hostlong.mli b/dcc/hostlong.mli new file mode 100644 index 0000000..01ca030 --- /dev/null +++ b/dcc/hostlong.mli @@ -0,0 +1,22 @@ +type t + +val of_int32 : int32 -> t + (* Convert the given 32-bit integer (type [int32]) to a hostlong + (type [Hostlong.t]). *) +val to_int32 : t -> int32 + (* Convert the given hostlong (type [Hostlong.t]) to a 32-bit integer + (type [int32]). *) + +val of_string : string -> t + (* Convert the given string to a hostlong (type [Hostlong.t]). *) + +val to_string : t -> string + (* Return the string representation of its argument, + in signed decimal. *) + +val of_address_string : string -> t + (* Convert the given string to a hostlong (type [Hostlong.t]). *) + +val to_address_string : t -> string + (* Return the string representation of its argument, + in signed decimal. *) diff --git a/dcc/naddr.ml b/dcc/naddr.ml deleted file mode 100644 index 24179a9..0000000 --- a/dcc/naddr.ml +++ /dev/null @@ -1,77 +0,0 @@ -(* Network address handling module. - -This file is part of Liece. - -Author: Daiki Ueno -Created: 1998-09-28 -Revised: 1999-01-28 -Keywords: IRC, liece, DCC - -This program 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 program 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. *) - -open Num -open Big_int -open Str - -let encode str = - let _ = string_match - (regexp "\([0-9]+\)\.\([0-9]+\)\.\([0-9]+\)\.\([0-9]+\)") str 0 in - let (a1, a2, a3, a4) = - try - int_of_string (matched_group 1 str), - int_of_string (matched_group 2 str), - int_of_string (matched_group 3 str), - int_of_string (matched_group 4 str) - with - _ -> - Printf.eprintf "Invalid address\n"; flush Pervasives.stdout; exit 1; - in - let (s1, s2, s3, s4) = - Int (1 lsl 24), - Int (1 lsl 16), - Int (1 lsl 8), - Int 1 - in - let ul = - ((Int a1) */ s1) +/ ((Int a2) */ s2) +/ ((Int a3) */ s3) +/ (Int a4) - in - string_of_num ul - -let decode str = - let ul = - try - num_of_string str - with - _ -> - Printf.eprintf "Invalid address\n"; flush Pervasives.stdout; exit 1; - in - let (s1, s2, s3, s4) = - Int (1 lsl 24), - Int (1 lsl 16), - Int (1 lsl 8), - Int 1 - in - let (a1, a2, a3, a4) = - floor_num (ul // s1), - floor_num ((mod_num ul s1) // s2), - floor_num ((mod_num (mod_num ul s1) s2) // s3), - (mod_num (mod_num (mod_num ul s1) s2) s3) - in - Printf.sprintf "%s.%s.%s.%s" - (string_of_num a1) - (string_of_num a2) - (string_of_num a3) - (string_of_num a4) diff --git a/dcc/naddr.mli b/dcc/naddr.mli deleted file mode 100644 index 0c9917a..0000000 --- a/dcc/naddr.mli +++ /dev/null @@ -1,31 +0,0 @@ -(* Network address handling module. - -This file is part of Liece. - -Author: Daiki Ueno -Created: 1998-09-28 -Revised: 1999-01-28 -Keywords: IRC, liece, DCC - -This program 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 program 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. *) - -val encode : string -> string;; - (* [Naddr.encode s] encodes the XXX.XXX.XXX.XXX address [s] into - another string packed by network byte order. *) - -val decode : string -> string;; - (* [Naddr.decode s] does the inverse job than [Naddr.encode], - restoring initial expression of address. *)