1 ;;; net-utils.el --- network functions
3 ;; Copyright (C) 1998-2016 Free Software Foundation, Inc.
5 ;; Author: Peter Breton <pbreton@cs.umb.edu>
6 ;; Created: Sun Mar 16 1997
7 ;; Keywords: network comm
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; There are three main areas of functionality:
29 ;; * Wrap common network utility programs (ping, traceroute, netstat,
30 ;; nslookup, arp, route). Note that these wrappers are of the diagnostic
31 ;; functions of these programs only.
33 ;; * Implement some very basic protocols in Emacs Lisp (finger and whois)
35 ;; * Support connections to HOST/PORT, generally for debugging and the like.
36 ;; In other words, for doing much the same thing as "telnet HOST PORT", and
37 ;; then typing commands.
41 ;; On some systems, programs like ifconfig are not in normal user
42 ;; path, but rather in /sbin, /usr/sbin, etc (but non-root users can
43 ;; still use them for queries). Actually the trend these
44 ;; days is for /sbin to be a symlink to /usr/sbin, but we still need to
45 ;; search both for older systems.
46 (defun net-utils--executable-find-sbin (command)
47 "Return absolute name of COMMAND if found in an sbin directory."
48 (let ((exec-path '("/sbin" "/usr/sbin" "/usr/local/sbin")))
49 (executable-find command)))
51 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
52 ;; Customization Variables
53 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
55 (defgroup net-utils nil
56 "Network utility functions."
61 (defcustom traceroute-program
62 (if (eq system-type 'windows-nt)
65 "Program to trace network hops to a destination."
69 (defcustom traceroute-program-options nil
70 "Options for the traceroute program."
72 :type '(repeat string))
74 (defcustom ping-program "ping"
75 "Program to send network test packets to a host."
79 ;; On GNU/Linux and Irix, the system's ping program seems to send packets
80 ;; indefinitely unless told otherwise
81 (defcustom ping-program-options
82 (and (eq system-type 'gnu/linux)
84 "Options for the ping program.
85 These options can be used to limit how many ICMP packets are emitted."
87 :type '(repeat string))
89 (define-obsolete-variable-alias 'ipconfig-program 'ifconfig-program "22.2")
91 (defcustom ifconfig-program
92 (cond ((eq system-type 'windows-nt) "ipconfig")
93 ((executable-find "ifconfig") "ifconfig")
94 ((net-utils--executable-find-sbin "ifconfig"))
95 ((net-utils--executable-find-sbin "ip"))
97 "Program to print network configuration information."
98 :version "25.1" ; add ip
102 (define-obsolete-variable-alias 'ipconfig-program-options
103 'ifconfig-program-options "22.2")
105 (defcustom ifconfig-program-options
106 (cond ((string-match "ipconfig\\'" ifconfig-program) '("/all"))
107 ((string-match "ifconfig\\'" ifconfig-program) '("-a"))
108 ((string-match "ip\\'" ifconfig-program) '("addr")))
109 "Options for the ifconfig program."
111 :set-after '(ifconfig-program)
113 :type '(repeat string))
115 (defcustom iwconfig-program
116 (cond ((executable-find "iwconfig") "iwconfig")
117 ((net-utils--executable-find-sbin "iw") "iw")
119 "Program to print wireless network configuration information."
124 (defcustom iwconfig-program-options
125 (cond ((string-match-p "iw\\'" iwconfig-program) (list "dev"))
127 "Options for the iwconfig program."
129 :type '(repeat string)
132 (defcustom netstat-program
133 (cond ((executable-find "netstat") "netstat")
134 ((net-utils--executable-find-sbin "ss"))
136 "Program to print network statistics."
141 (defcustom netstat-program-options
143 "Options for the netstat program."
145 :type '(repeat string))
147 (defcustom arp-program (or (net-utils--executable-find-sbin "arp") "arp")
148 "Program to print IP to address translation tables."
152 (defcustom arp-program-options
154 "Options for the arp program."
156 :type '(repeat string))
158 (defcustom route-program
159 (cond ((eq system-type 'windows-nt) "route")
160 ((executable-find "netstat") "netstat")
161 ((net-utils--executable-find-sbin "netstat"))
162 ((executable-find "ip") "ip")
163 ((net-utils--executable-find-sbin "ip"))
165 "Program to print routing tables."
170 (defcustom route-program-options
171 (cond ((eq system-type 'windows-nt) (list "print"))
172 ((string-match-p "netstat\\'" route-program) (list "-r"))
174 "Options for the route program."
176 :type '(repeat string)
179 (defcustom nslookup-program "nslookup"
180 "Program to interactively query DNS information."
184 (defcustom nslookup-program-options nil
185 "Options for the nslookup program."
187 :type '(repeat string))
189 (defcustom nslookup-prompt-regexp "^> "
190 "Regexp to match the nslookup prompt.
192 This variable is only used if the variable
193 `comint-use-prompt-regexp' is non-nil."
197 (defcustom dig-program "dig"
198 "Program to query DNS information."
202 (defcustom ftp-program "ftp"
203 "Program to run to do FTP transfers."
207 (defcustom ftp-program-options nil
208 "Options for the ftp program."
210 :type '(repeat string))
212 (defcustom ftp-prompt-regexp "^ftp>"
213 "Regexp which matches the FTP program's prompt.
215 This variable is only used if the variable
216 `comint-use-prompt-regexp' is non-nil."
220 (defcustom smbclient-program "smbclient"
225 (defcustom smbclient-program-options nil
226 "Options for the smbclient program."
228 :type '(repeat string))
230 (defcustom smbclient-prompt-regexp "^smb: >"
231 "Regexp which matches the smbclient program's prompt.
233 This variable is only used if the variable
234 `comint-use-prompt-regexp' is non-nil."
238 (defcustom dns-lookup-program "host"
239 "Program to interactively query DNS information."
243 (defcustom dns-lookup-program-options nil
244 "Options for the dns-lookup program."
246 :type '(repeat string))
248 ;; Internal variables
249 (defvar network-connection-service nil)
250 (defvar network-connection-host nil)
252 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
254 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
256 (defvar nslookup-font-lock-keywords
258 (list "^[A-Za-z0-9 _]+:" 0 'font-lock-type-face)
259 (list "\\<\\(SOA\\|NS\\|MX\\|A\\|CNAME\\)\\>"
260 1 'font-lock-keyword-face)
264 (make-list 4 "[0-9]+")
266 0 'font-lock-variable-name-face)
269 (let ((host-expression "[-A-Za-z0-9]+"))
272 (make-list 2 host-expression)
274 "\\(\\." host-expression "\\)*"))
275 0 'font-lock-variable-name-face))
276 "Expressions to font-lock for nslookup.")
278 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
279 ;; General network utilities mode
280 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
282 (defvar net-utils-font-lock-keywords
286 (mapconcat 'identity (make-list 4 "[0-9]+") "\\.")
287 0 'font-lock-variable-name-face)
288 ;; Simple rfc4291 addresses
290 "\\( \\([[:xdigit:]]+\\(:\\|::\\)\\)+[[:xdigit:]]+\\)"
292 "\\(::[[:xdigit:]]+\\)")
293 0 'font-lock-variable-name-face)
296 (let ((host-expression "[-A-Za-z0-9]+"))
298 (mapconcat 'identity (make-list 2 host-expression) "\\.")
299 "\\(\\." host-expression "\\)*"))
300 0 'font-lock-variable-name-face))
301 "Expressions to font-lock for general network utilities.")
303 (define-derived-mode net-utils-mode special-mode "NetworkUtil"
304 "Major mode for interacting with an external network utility."
305 (set (make-local-variable 'font-lock-defaults)
306 '((net-utils-font-lock-keywords)))
307 (setq-local revert-buffer-function #'net-utils--revert-function))
309 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
311 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
313 ;; Simplified versions of some at-point functions from ffap.el.
314 ;; It's not worth loading all of ffap just for these.
315 (defun net-utils-machine-at-point ()
317 (buffer-substring-no-properties
319 (skip-chars-backward "-a-zA-Z0-9.")
322 (skip-chars-forward "-a-zA-Z0-9.")
323 (skip-chars-backward "." pt)
326 (defun net-utils-url-at-point ()
328 (buffer-substring-no-properties
330 (skip-chars-backward "--:=&?$+@-Z_a-z~#,%")
331 (skip-chars-forward "^A-Za-z0-9" pt)
334 (skip-chars-forward "--:=&?$+@-Z_a-z~#,%")
335 (skip-chars-backward ":;.,!?" pt)
338 (defun net-utils-remove-ctrl-m-filter (process output-string)
339 "Remove trailing control Ms."
340 (with-current-buffer (process-buffer process)
342 (let ((inhibit-read-only t)
343 (filtered-string output-string))
344 (while (string-match "\r" filtered-string)
345 (setq filtered-string
346 (replace-match "" nil nil filtered-string)))
347 ;; Insert the text, moving the process-marker.
348 (goto-char (process-mark process))
349 (insert filtered-string)
350 (set-marker (process-mark process) (point))))))
352 (declare-function w32-get-console-output-codepage "w32proc.c" ())
354 (defun net-utils-run-program (name header program args)
355 "Run a network information program."
356 (let ((buf (get-buffer-create (concat "*" name "*")))
357 (coding-system-for-read
358 ;; MS-Windows versions of network utilities output text
359 ;; encoded in the console (a.k.a. "OEM") codepage, which is
360 ;; different from the default system (a.k.a. "ANSI")
362 (if (eq system-type 'windows-nt)
363 (intern (format "cp%d" (w32-get-console-output-codepage)))
364 coding-system-for-read)))
369 (apply 'start-process name buf program args)
370 'net-utils-remove-ctrl-m-filter)
374 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
375 ;; General network utilities (diagnostic)
376 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
378 ;; Todo: This data could be saved in a bookmark.
379 (defvar net-utils--revert-cmd nil)
381 (defun net-utils-run-simple (buffer program-name args &optional nodisplay)
382 "Run a network utility for diagnostic output only."
383 (with-current-buffer (if (stringp buffer) (get-buffer-create buffer) buffer)
384 (let ((proc (get-buffer-process (current-buffer))))
386 (set-process-filter proc nil)
387 (delete-process proc)))
388 (let ((inhibit-read-only t)
389 (coding-system-for-read
390 ;; MS-Windows versions of network utilities output text
391 ;; encoded in the console (a.k.a. "OEM") codepage, which is
392 ;; different from the default system (a.k.a. "ANSI")
394 (if (eq system-type 'windows-nt)
395 (intern (format "cp%d" (w32-get-console-output-codepage)))
396 coding-system-for-read)))
399 (setq-local net-utils--revert-cmd
400 `(net-utils-run-simple ,(current-buffer)
401 ,program-name ,args nodisplay))
403 (apply 'start-process program-name
404 (current-buffer) program-name args)
405 'net-utils-remove-ctrl-m-filter)
406 (unless nodisplay (display-buffer (current-buffer)))))
408 (defun net-utils--revert-function (&optional ignore-auto noconfirm)
409 (message "Reverting `%s'..." (buffer-name))
410 (apply (car net-utils--revert-cmd) (cdr net-utils--revert-cmd))
411 (let ((proc (get-buffer-process (current-buffer))))
413 (set-process-sentinel
415 (lambda (process event)
416 (when (string= event "finished\n")
417 (message "Reverting `%s' done" (process-buffer process))))))))
421 "Run ifconfig and display diagnostic output."
423 (net-utils-run-simple
424 (format "*%s*" ifconfig-program)
426 ifconfig-program-options))
428 (defalias 'ipconfig 'ifconfig)
432 "Run iwconfig and display diagnostic output."
434 (net-utils-run-simple
435 (format "*%s*" iwconfig-program)
437 iwconfig-program-options))
441 "Run netstat and display diagnostic output."
443 (net-utils-run-simple
444 (format "*%s*" netstat-program)
446 netstat-program-options))
450 "Run arp and display diagnostic output."
452 (net-utils-run-simple
453 (format "*%s*" arp-program)
455 arp-program-options))
459 "Run route and display diagnostic output."
461 (net-utils-run-simple
462 (format "*%s*" route-program)
464 route-program-options))
466 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
467 ;; Wrappers for external network programs
468 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
471 (defun traceroute (target)
472 "Run traceroute program for TARGET."
473 (interactive "sTarget: ")
475 (if traceroute-program-options
476 (append traceroute-program-options (list target))
478 (net-utils-run-simple
479 (concat "Traceroute" " " target)
486 If your system's ping continues until interrupted, you can try setting
487 `ping-program-options'."
489 (list (read-from-minibuffer "Ping host: " (net-utils-machine-at-point))))
491 (if ping-program-options
492 (append ping-program-options (list host))
494 (net-utils-run-program
495 (concat "Ping" " " host)
496 (concat "** Ping ** " ping-program " ** " host)
500 ;; FIXME -- Needs to be a process filter
501 ;; (defun netstat-with-filter (filter)
502 ;; "Run netstat program."
503 ;; (interactive "sFilter: ")
505 ;; (set-buffer (get-buffer "*Netstat*"))
506 ;; (goto-char (point-min))
507 ;; (delete-matching-lines filter))
510 (defun nslookup-host (host)
511 "Lookup the DNS information for HOST."
513 (list (read-from-minibuffer "Lookup host: " (net-utils-machine-at-point))))
515 (if nslookup-program-options
516 (append nslookup-program-options (list host))
518 (net-utils-run-program
522 (list "Nslookup" host nslookup-program)
529 "Run nslookup program."
531 (switch-to-buffer (make-comint "nslookup" nslookup-program))
534 (defvar comint-prompt-regexp)
535 (defvar comint-input-autoexpand)
537 (autoload 'comint-mode "comint" nil t)
539 (defvar nslookup-mode-map
540 (let ((map (make-sparse-keymap)))
541 (define-key map "\t" 'completion-at-point)
544 ;; Using a derived mode gives us keymaps, hooks, etc.
545 (define-derived-mode nslookup-mode comint-mode "Nslookup"
546 "Major mode for interacting with the nslookup program."
548 (make-local-variable 'font-lock-defaults)
549 '((nslookup-font-lock-keywords)))
550 (setq comint-prompt-regexp nslookup-prompt-regexp)
551 (setq comint-input-autoexpand t))
554 (defun dns-lookup-host (host)
555 "Lookup the DNS information for HOST (name or IP address)."
557 (list (read-from-minibuffer "Lookup host: " (net-utils-machine-at-point))))
559 (if dns-lookup-program-options
560 (append dns-lookup-program-options (list host))
562 (net-utils-run-program
563 (concat "DNS Lookup [" host "]")
566 (list "DNS Lookup" host dns-lookup-program)
571 (autoload 'ffap-string-at-point "ffap")
574 (defun run-dig (host)
578 (read-from-minibuffer "Lookup host: "
579 (or (ffap-string-at-point 'machine) ""))))
580 (net-utils-run-program
584 (list "Dig" host dig-program)
589 (autoload 'comint-exec "comint")
591 ;; This is a lot less than ange-ftp, but much simpler.
597 (read-from-minibuffer
598 "Ftp to Host: " (net-utils-machine-at-point))))
599 (let ((buf (get-buffer-create (concat "*ftp [" host "]*"))))
602 (comint-exec buf (concat "ftp-" host) ftp-program nil
603 (if ftp-program-options
604 (append (list host) ftp-program-options)
606 (pop-to-buffer buf)))
609 (let ((map (make-sparse-keymap)))
610 ;; Occasionally useful
611 (define-key map "\t" 'completion-at-point)
614 (define-derived-mode ftp-mode comint-mode "FTP"
615 "Major mode for interacting with the ftp program."
616 (setq comint-prompt-regexp ftp-prompt-regexp)
617 (setq comint-input-autoexpand t)
618 ;; Only add the password-prompting hook if it's not already in the
619 ;; global hook list. This stands a small chance of losing, if it's
620 ;; later removed from the global list (very small, since any
621 ;; password prompts will probably immediately follow the initial
622 ;; connection), but it's better than getting prompted twice for the
624 (unless (memq 'comint-watch-for-password-prompt
625 (default-value 'comint-output-filter-functions))
626 (add-hook 'comint-output-filter-functions 'comint-watch-for-password-prompt
629 (defun smbclient (host service)
630 "Connect to SERVICE on HOST via SMB."
633 (read-from-minibuffer
634 "Connect to Host: " (net-utils-machine-at-point))
635 (read-from-minibuffer "SMB Service: ")))
636 (let* ((name (format "smbclient [%s\\%s]" host service))
637 (buf (get-buffer-create (concat "*" name "*")))
638 (service-name (concat "\\\\" host "\\" service)))
641 (comint-exec buf name smbclient-program nil
642 (if smbclient-program-options
643 (append (list service-name) smbclient-program-options)
644 (list service-name)))
645 (pop-to-buffer buf)))
647 (defun smbclient-list-shares (host)
648 "List services on HOST."
651 (read-from-minibuffer
652 "Connect to Host: " (net-utils-machine-at-point))))
653 (let ((buf (get-buffer-create (format "*SMB Shares on %s*" host))))
656 (comint-exec buf "smbclient-list-shares"
657 smbclient-program nil (list "-L" host))
658 (pop-to-buffer buf)))
660 (define-derived-mode smbclient-mode comint-mode "smbclient"
661 "Major mode for interacting with the smbclient program."
662 (setq comint-prompt-regexp smbclient-prompt-regexp)
663 (setq comint-input-autoexpand t)
664 ;; Only add the password-prompting hook if it's not already in the
665 ;; global hook list. This stands a small chance of losing, if it's
666 ;; later removed from the global list (very small, since any
667 ;; password prompts will probably immediately follow the initial
668 ;; connection), but it's better than getting prompted twice for the
670 (unless (memq 'comint-watch-for-password-prompt
671 (default-value 'comint-output-filter-functions))
672 (add-hook 'comint-output-filter-functions 'comint-watch-for-password-prompt
676 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
677 ;; Network Connections
678 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
680 ;; Full list is available at:
681 ;; http://www.iana.org/assignments/port-numbers
682 (defvar network-connection-service-alist
685 (cons 'active-users 11)
701 (cons 'netbios-name 137)
702 (cons 'netbios-data 139)
706 "Alist of services and associated TCP port numbers.
707 This list is not complete.")
710 (defun run-network-program (process-name host port &optional initial-string)
711 (let ((tcp-connection)
713 (setq buf (get-buffer-create (concat "*" process-name "*")))
717 (open-network-stream process-name buf host port))
718 (error "Could not open connection to %s" host))
720 (set-marker (process-mark tcp-connection) (point-min))
721 (set-process-filter tcp-connection 'net-utils-remove-ctrl-m-filter)
723 (process-send-string tcp-connection
724 (concat initial-string "\r\n")))
725 (display-buffer buf)))
727 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
729 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
731 (defcustom finger-X.500-host-regexps nil
732 "A list of regular expressions matching host names.
733 If a host name passed to `finger' matches one of these regular
734 expressions, it is assumed to be a host that doesn't accept
735 queries of the form USER@HOST, and wants a query containing USER only."
737 :type '(repeat regexp)
742 (defun finger (user host)
743 "Finger USER on HOST."
744 ;; One of those great interactive statements that's actually
745 ;; longer than the function call! The idea is that if the user
746 ;; uses a string like "pbreton@cs.umb.edu", we won't ask for the
747 ;; host name. If we don't see an "@", we'll prompt for the host.
749 (let* ((answer (read-from-minibuffer "Finger User: "
750 (net-utils-url-at-point)))
751 (index (string-match (regexp-quote "@") answer)))
753 (list (substring answer 0 index)
754 (substring answer (1+ index)))
756 (read-from-minibuffer "At Host: "
757 (net-utils-machine-at-point))))))
758 (let* ((user-and-host (concat user "@" host))
759 (process-name (concat "Finger [" user-and-host "]"))
760 (regexps finger-X.500-host-regexps)
763 (while (not (string-match (car regexps) host))
764 (setq regexps (cdr regexps)))
766 (setq user-and-host user)))
770 (cdr (assoc 'finger network-connection-service-alist))
773 (defcustom whois-server-name "rs.internic.net"
774 "Default host name for the whois service."
778 (defcustom whois-server-list
779 '(("whois.arin.net") ; Networks, ASN's, and related POC's (numbers)
780 ("rs.internic.net") ; domain related info
781 ("whois.publicinterestregistry.net")
788 "A list of whois servers that can be queried."
790 :type '(repeat (list string)))
792 ;; FIXME: modern whois clients include a much better tld <-> whois server
793 ;; list, Emacs should probably avoid specifying the server as the client
794 ;; will DTRT anyway... -rfr
795 (defcustom whois-server-tld
796 '(("rs.internic.net" . "com")
797 ("whois.publicinterestregistry.net" . "org")
798 ("whois.ripe.net" . "be")
799 ("whois.ripe.net" . "de")
800 ("whois.ripe.net" . "dk")
801 ("whois.ripe.net" . "it")
802 ("whois.ripe.net" . "fi")
803 ("whois.ripe.net" . "fr")
804 ("whois.ripe.net" . "uk")
805 ("whois.apnic.net" . "au")
806 ("whois.apnic.net" . "ch")
807 ("whois.apnic.net" . "hk")
808 ("whois.apnic.net" . "jp")
809 ("whois.nic.gov" . "gov")
810 ("whois.nic.mil" . "mil"))
811 "Alist to map top level domains to whois servers."
813 :type '(repeat (cons string string)))
815 (defcustom whois-guess-server t
816 "If non-nil then whois will try to deduce the appropriate whois
817 server from the query. If the query doesn't look like a domain or hostname
818 then the server named by `whois-server-name' is used."
822 (defun whois-get-tld (host)
823 "Return the top level domain of `host', or nil if it isn't a domain name."
824 (let ((i (1- (length host)))
825 (max-len (- (length host) 5)))
826 (while (not (or (= i max-len) (char-equal (aref host i) ?.)))
830 (substring host (1+ i)))))
834 (defun whois (arg search-string)
835 "Send SEARCH-STRING to server defined by the `whois-server-name' variable.
836 If `whois-guess-server' is non-nil, then try to deduce the correct server
837 from SEARCH-STRING. With argument, prompt for whois server."
838 (interactive "P\nsWhois: ")
839 (let* ((whois-apropos-host (if whois-guess-server
840 (rassoc (whois-get-tld search-string)
843 (server-name (if whois-apropos-host
844 (car whois-apropos-host)
848 (completing-read "Whois server name: "
849 whois-server-list nil nil "whois.")
854 (cdr (assoc 'whois network-connection-service-alist))
857 (defcustom whois-reverse-lookup-server "whois.arin.net"
858 "Server which provides inverse DNS mapping."
863 (defun whois-reverse-lookup ()
865 (let ((whois-server-name whois-reverse-lookup-server))
866 (call-interactively 'whois)))
868 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
869 ;;; General Network connection
870 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
872 ;; Using a derived mode gives us keymaps, hooks, etc.
874 network-connection-mode comint-mode "Network-Connection"
875 "Major mode for interacting with the network-connection program.")
877 (defun network-connection-mode-setup (host service)
878 (make-local-variable 'network-connection-host)
879 (setq network-connection-host host)
880 (make-local-variable 'network-connection-service)
881 (setq network-connection-service service))
884 (defun network-connection-to-service (host service)
885 "Open a network connection to SERVICE on HOST."
888 (read-from-minibuffer "Host: " (net-utils-machine-at-point))
889 (completing-read "Service: "
893 (list (symbol-name (car elt)))))
894 network-connection-service-alist))))
897 (cdr (assoc (intern service) network-connection-service-alist))))
900 (defun network-connection (host port)
901 "Open a network connection to HOST on PORT."
902 (interactive "sHost: \nnPort: ")
903 (network-service-connection host (number-to-string port)))
905 (defun network-service-connection (host service)
906 "Open a network connection to SERVICE on HOST."
907 (let* ((process-name (concat "Network Connection [" host " " service "]"))
908 (portnum (string-to-number service))
909 (buf (get-buffer-create (concat "*" process-name "*"))))
910 (or (zerop portnum) (setq service portnum))
915 (network-connection-mode)
916 (network-connection-mode-setup host service)
917 (pop-to-buffer buf)))
919 (defvar comint-input-ring)
921 (defun network-connection-reconnect ()
922 "Reconnect a network connection, preserving the old input ring."
924 (let ((proc (get-buffer-process (current-buffer)))
925 (old-comint-input-ring comint-input-ring)
926 (host network-connection-host)
927 (service network-connection-service))
928 (if (not (or (not proc)
929 (eq (process-status proc) 'closed)))
930 (message "Still connected")
931 (goto-char (point-max))
932 (insert (format "Reopening connection to %s\n" host))
933 (network-connection host
934 (if (numberp service)
936 (cdr (assoc service network-connection-service-alist))))
937 (and old-comint-input-ring
938 (setq comint-input-ring old-comint-input-ring)))))
942 ;;; net-utils.el ends here