1 ;;; net-utils.el --- network functions
3 ;; Copyright (C) 1998-2015 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 ;; day 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 (memq system-type
'(gnu/linux irix
))
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
"iwconfig"
116 "Program to print wireless network configuration information."
121 (defcustom iwconfig-program-options nil
122 "Options for the iwconfig program."
124 :type
'(repeat string
)
127 (defcustom netstat-program
"netstat"
128 "Program to print network statistics."
132 (defcustom netstat-program-options
134 "Options for the netstat program."
136 :type
'(repeat string
))
138 (defcustom arp-program
(or (net-utils--executable-find-sbin "arp") "arp")
139 "Program to print IP to address translation tables."
143 (defcustom arp-program-options
145 "Options for the arp program."
147 :type
'(repeat string
))
149 (defcustom route-program
150 (if (eq system-type
'windows-nt
)
153 "Program to print routing tables."
157 (defcustom route-program-options
158 (if (eq system-type
'windows-nt
)
161 "Options for the route program."
163 :type
'(repeat string
))
165 (defcustom nslookup-program
"nslookup"
166 "Program to interactively query DNS information."
170 (defcustom nslookup-program-options nil
171 "Options for the nslookup program."
173 :type
'(repeat string
))
175 (defcustom nslookup-prompt-regexp
"^> "
176 "Regexp to match the nslookup prompt.
178 This variable is only used if the variable
179 `comint-use-prompt-regexp' is non-nil."
183 (defcustom dig-program
"dig"
184 "Program to query DNS information."
188 (defcustom ftp-program
"ftp"
189 "Program to run to do FTP transfers."
193 (defcustom ftp-program-options nil
194 "Options for the ftp program."
196 :type
'(repeat string
))
198 (defcustom ftp-prompt-regexp
"^ftp>"
199 "Regexp which matches the FTP program's prompt.
201 This variable is only used if the variable
202 `comint-use-prompt-regexp' is non-nil."
206 (defcustom smbclient-program
"smbclient"
211 (defcustom smbclient-program-options nil
212 "Options for the smbclient program."
214 :type
'(repeat string
))
216 (defcustom smbclient-prompt-regexp
"^smb: >"
217 "Regexp which matches the smbclient program's prompt.
219 This variable is only used if the variable
220 `comint-use-prompt-regexp' is non-nil."
224 (defcustom dns-lookup-program
"host"
225 "Program to interactively query DNS information."
229 (defcustom dns-lookup-program-options nil
230 "Options for the dns-lookup program."
232 :type
'(repeat string
))
234 ;; Internal variables
235 (defvar network-connection-service nil
)
236 (defvar network-connection-host nil
)
238 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
240 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
242 (defvar nslookup-font-lock-keywords
244 (list "^[A-Za-z0-9 _]+:" 0 'font-lock-type-face
)
245 (list "\\<\\(SOA\\|NS\\|MX\\|A\\|CNAME\\)\\>"
246 1 'font-lock-keyword-face
)
250 (make-list 4 "[0-9]+")
252 0 'font-lock-variable-name-face
)
255 (let ((host-expression "[-A-Za-z0-9]+"))
258 (make-list 2 host-expression
)
260 "\\(\\." host-expression
"\\)*"))
261 0 'font-lock-variable-name-face
))
262 "Expressions to font-lock for nslookup.")
264 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
265 ;; General network utilities mode
266 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
268 (defvar net-utils-font-lock-keywords
272 (mapconcat 'identity
(make-list 4 "[0-9]+") "\\.")
273 0 'font-lock-variable-name-face
)
274 ;; Simple rfc4291 addresses
276 "\\( \\([[:xdigit:]]+\\(:\\|::\\)\\)+[[:xdigit:]]+\\)"
278 "\\(::[[:xdigit:]]+\\)")
279 0 'font-lock-variable-name-face
)
282 (let ((host-expression "[-A-Za-z0-9]+"))
284 (mapconcat 'identity
(make-list 2 host-expression
) "\\.")
285 "\\(\\." host-expression
"\\)*"))
286 0 'font-lock-variable-name-face
))
287 "Expressions to font-lock for general network utilities.")
289 (define-derived-mode net-utils-mode special-mode
"NetworkUtil"
290 "Major mode for interacting with an external network utility."
291 (set (make-local-variable 'font-lock-defaults
)
292 '((net-utils-font-lock-keywords)))
293 (setq-local revert-buffer-function
#'net-utils--revert-function
))
295 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
297 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
299 ;; Simplified versions of some at-point functions from ffap.el.
300 ;; It's not worth loading all of ffap just for these.
301 (defun net-utils-machine-at-point ()
303 (buffer-substring-no-properties
305 (skip-chars-backward "-a-zA-Z0-9.")
308 (skip-chars-forward "-a-zA-Z0-9.")
309 (skip-chars-backward "." pt
)
312 (defun net-utils-url-at-point ()
314 (buffer-substring-no-properties
316 (skip-chars-backward "--:=&?$+@-Z_a-z~#,%")
317 (skip-chars-forward "^A-Za-z0-9" pt
)
320 (skip-chars-forward "--:=&?$+@-Z_a-z~#,%")
321 (skip-chars-backward ":;.,!?" pt
)
324 (defun net-utils-remove-ctrl-m-filter (process output-string
)
325 "Remove trailing control Ms."
326 (with-current-buffer (process-buffer process
)
328 (let ((inhibit-read-only t
)
329 (filtered-string output-string
))
330 (while (string-match "\r" filtered-string
)
331 (setq filtered-string
332 (replace-match "" nil nil filtered-string
)))
333 ;; Insert the text, moving the process-marker.
334 (goto-char (process-mark process
))
335 (insert filtered-string
)
336 (set-marker (process-mark process
) (point))))))
338 (declare-function w32-get-console-output-codepage
"w32proc.c" ())
340 (defun net-utils-run-program (name header program args
)
341 "Run a network information program."
342 (let ((buf (get-buffer-create (concat "*" name
"*")))
343 (coding-system-for-read
344 ;; MS-Windows versions of network utilities output text
345 ;; encoded in the console (a.k.a. "OEM") codepage, which is
346 ;; different from the default system (a.k.a. "ANSI")
348 (if (eq system-type
'windows-nt
)
349 (intern (format "cp%d" (w32-get-console-output-codepage)))
350 coding-system-for-read
)))
355 (apply 'start-process name buf program args
)
356 'net-utils-remove-ctrl-m-filter
)
360 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
361 ;; General network utilities (diagnostic)
362 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
364 ;; Todo: This data could be saved in a bookmark.
365 (defvar net-utils--revert-cmd nil
)
367 (defun net-utils-run-simple (buffer program-name args
&optional nodisplay
)
368 "Run a network utility for diagnostic output only."
369 (with-current-buffer (if (stringp buffer
) (get-buffer-create buffer
) buffer
)
370 (let ((proc (get-buffer-process (current-buffer))))
372 (set-process-filter proc nil
)
373 (delete-process proc
)))
374 (let ((inhibit-read-only t
)
375 (coding-system-for-read
376 ;; MS-Windows versions of network utilities output text
377 ;; encoded in the console (a.k.a. "OEM") codepage, which is
378 ;; different from the default system (a.k.a. "ANSI")
380 (if (eq system-type
'windows-nt
)
381 (intern (format "cp%d" (w32-get-console-output-codepage)))
382 coding-system-for-read
)))
385 (setq-local net-utils--revert-cmd
386 `(net-utils-run-simple ,(current-buffer)
387 ,program-name
,args nodisplay
))
389 (apply 'start-process program-name
390 (current-buffer) program-name args
)
391 'net-utils-remove-ctrl-m-filter
)
392 (unless nodisplay
(display-buffer (current-buffer)))))
394 (defun net-utils--revert-function (&optional ignore-auto noconfirm
)
395 (message "Reverting `%s'..." (buffer-name))
396 (apply (car net-utils--revert-cmd
) (cdr net-utils--revert-cmd
))
397 (let ((proc (get-buffer-process (current-buffer))))
399 (set-process-sentinel
401 (lambda (process event
)
402 (when (string= event
"finished\n")
403 (message "Reverting `%s' done" (process-buffer process
))))))))
407 "Run ifconfig and display diagnostic output."
409 (net-utils-run-simple
410 (format "*%s*" ifconfig-program
)
412 ifconfig-program-options
))
414 (defalias 'ipconfig
'ifconfig
)
418 "Run iwconfig and display diagnostic output."
420 (net-utils-run-simple
421 (format "*%s*" iwconfig-program
)
423 iwconfig-program-options
))
427 "Run netstat and display diagnostic output."
429 (net-utils-run-simple
430 (format "*%s*" netstat-program
)
432 netstat-program-options
))
436 "Run arp and display diagnostic output."
438 (net-utils-run-simple
439 (format "*%s*" arp-program
)
441 arp-program-options
))
445 "Run route and display diagnostic output."
447 (net-utils-run-simple
448 (format "*%s*" route-program
)
450 route-program-options
))
452 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
453 ;; Wrappers for external network programs
454 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
457 (defun traceroute (target)
458 "Run traceroute program for TARGET."
459 (interactive "sTarget: ")
461 (if traceroute-program-options
462 (append traceroute-program-options
(list target
))
464 (net-utils-run-simple
465 (concat "Traceroute" " " target
)
472 If your system's ping continues until interrupted, you can try setting
473 `ping-program-options'."
475 (list (read-from-minibuffer "Ping host: " (net-utils-machine-at-point))))
477 (if ping-program-options
478 (append ping-program-options
(list host
))
480 (net-utils-run-program
481 (concat "Ping" " " host
)
482 (concat "** Ping ** " ping-program
" ** " host
)
486 ;; FIXME -- Needs to be a process filter
487 ;; (defun netstat-with-filter (filter)
488 ;; "Run netstat program."
489 ;; (interactive "sFilter: ")
491 ;; (set-buffer (get-buffer "*Netstat*"))
492 ;; (goto-char (point-min))
493 ;; (delete-matching-lines filter))
496 (defun nslookup-host (host)
497 "Lookup the DNS information for HOST."
499 (list (read-from-minibuffer "Lookup host: " (net-utils-machine-at-point))))
501 (if nslookup-program-options
502 (append nslookup-program-options
(list host
))
504 (net-utils-run-program
508 (list "Nslookup" host nslookup-program
)
515 "Run nslookup program."
517 (switch-to-buffer (make-comint "nslookup" nslookup-program
))
520 (defvar comint-prompt-regexp
)
521 (defvar comint-input-autoexpand
)
523 (autoload 'comint-mode
"comint" nil t
)
525 (defvar nslookup-mode-map
526 (let ((map (make-sparse-keymap)))
527 (define-key map
"\t" 'completion-at-point
)
530 ;; Using a derived mode gives us keymaps, hooks, etc.
531 (define-derived-mode nslookup-mode comint-mode
"Nslookup"
532 "Major mode for interacting with the nslookup program."
534 (make-local-variable 'font-lock-defaults
)
535 '((nslookup-font-lock-keywords)))
536 (setq comint-prompt-regexp nslookup-prompt-regexp
)
537 (setq comint-input-autoexpand t
))
540 (defun dns-lookup-host (host)
541 "Lookup the DNS information for HOST (name or IP address)."
543 (list (read-from-minibuffer "Lookup host: " (net-utils-machine-at-point))))
545 (if dns-lookup-program-options
546 (append dns-lookup-program-options
(list host
))
548 (net-utils-run-program
549 (concat "DNS Lookup [" host
"]")
552 (list "DNS Lookup" host dns-lookup-program
)
557 (autoload 'ffap-string-at-point
"ffap")
560 (defun run-dig (host)
564 (read-from-minibuffer "Lookup host: "
565 (or (ffap-string-at-point 'machine
) ""))))
566 (net-utils-run-program
570 (list "Dig" host dig-program
)
575 (autoload 'comint-exec
"comint")
577 ;; This is a lot less than ange-ftp, but much simpler.
583 (read-from-minibuffer
584 "Ftp to Host: " (net-utils-machine-at-point))))
585 (let ((buf (get-buffer-create (concat "*ftp [" host
"]*"))))
588 (comint-exec buf
(concat "ftp-" host
) ftp-program nil
589 (if ftp-program-options
590 (append (list host
) ftp-program-options
)
592 (pop-to-buffer buf
)))
595 (let ((map (make-sparse-keymap)))
596 ;; Occasionally useful
597 (define-key map
"\t" 'completion-at-point
)
600 (define-derived-mode ftp-mode comint-mode
"FTP"
601 "Major mode for interacting with the ftp program."
602 (setq comint-prompt-regexp ftp-prompt-regexp
)
603 (setq comint-input-autoexpand t
)
604 ;; Only add the password-prompting hook if it's not already in the
605 ;; global hook list. This stands a small chance of losing, if it's
606 ;; later removed from the global list (very small, since any
607 ;; password prompts will probably immediately follow the initial
608 ;; connection), but it's better than getting prompted twice for the
610 (unless (memq 'comint-watch-for-password-prompt
611 (default-value 'comint-output-filter-functions
))
612 (add-hook 'comint-output-filter-functions
'comint-watch-for-password-prompt
615 (defun smbclient (host service
)
616 "Connect to SERVICE on HOST via SMB."
619 (read-from-minibuffer
620 "Connect to Host: " (net-utils-machine-at-point))
621 (read-from-minibuffer "SMB Service: ")))
622 (let* ((name (format "smbclient [%s\\%s]" host service
))
623 (buf (get-buffer-create (concat "*" name
"*")))
624 (service-name (concat "\\\\" host
"\\" service
)))
627 (comint-exec buf name smbclient-program nil
628 (if smbclient-program-options
629 (append (list service-name
) smbclient-program-options
)
630 (list service-name
)))
631 (pop-to-buffer buf
)))
633 (defun smbclient-list-shares (host)
634 "List services on HOST."
637 (read-from-minibuffer
638 "Connect to Host: " (net-utils-machine-at-point))))
639 (let ((buf (get-buffer-create (format "*SMB Shares on %s*" host
))))
642 (comint-exec buf
"smbclient-list-shares"
643 smbclient-program nil
(list "-L" host
))
644 (pop-to-buffer buf
)))
646 (define-derived-mode smbclient-mode comint-mode
"smbclient"
647 "Major mode for interacting with the smbclient program."
648 (setq comint-prompt-regexp smbclient-prompt-regexp
)
649 (setq comint-input-autoexpand t
)
650 ;; Only add the password-prompting hook if it's not already in the
651 ;; global hook list. This stands a small chance of losing, if it's
652 ;; later removed from the global list (very small, since any
653 ;; password prompts will probably immediately follow the initial
654 ;; connection), but it's better than getting prompted twice for the
656 (unless (memq 'comint-watch-for-password-prompt
657 (default-value 'comint-output-filter-functions
))
658 (add-hook 'comint-output-filter-functions
'comint-watch-for-password-prompt
662 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
663 ;; Network Connections
664 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
666 ;; Full list is available at:
667 ;; http://www.iana.org/assignments/port-numbers
668 (defvar network-connection-service-alist
671 (cons 'active-users
11)
687 (cons 'netbios-name
137)
688 (cons 'netbios-data
139)
692 "Alist of services and associated TCP port numbers.
693 This list is not complete.")
696 (defun run-network-program (process-name host port
&optional initial-string
)
697 (let ((tcp-connection)
699 (setq buf
(get-buffer-create (concat "*" process-name
"*")))
703 (open-network-stream process-name buf host port
))
704 (error "Could not open connection to %s" host
))
706 (set-marker (process-mark tcp-connection
) (point-min))
707 (set-process-filter tcp-connection
'net-utils-remove-ctrl-m-filter
)
709 (process-send-string tcp-connection
710 (concat initial-string
"\r\n")))
711 (display-buffer buf
)))
713 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
715 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
717 (defcustom finger-X
.500-host-regexps nil
718 "A list of regular expressions matching host names.
719 If a host name passed to `finger' matches one of these regular
720 expressions, it is assumed to be a host that doesn't accept
721 queries of the form USER@HOST, and wants a query containing USER only."
723 :type
'(repeat regexp
)
728 (defun finger (user host
)
729 "Finger USER on HOST."
730 ;; One of those great interactive statements that's actually
731 ;; longer than the function call! The idea is that if the user
732 ;; uses a string like "pbreton@cs.umb.edu", we won't ask for the
733 ;; host name. If we don't see an "@", we'll prompt for the host.
735 (let* ((answer (read-from-minibuffer "Finger User: "
736 (net-utils-url-at-point)))
737 (index (string-match (regexp-quote "@") answer
)))
739 (list (substring answer
0 index
)
740 (substring answer
(1+ index
)))
742 (read-from-minibuffer "At Host: "
743 (net-utils-machine-at-point))))))
744 (let* ((user-and-host (concat user
"@" host
))
745 (process-name (concat "Finger [" user-and-host
"]"))
746 (regexps finger-X
.500-host-regexps
)
749 (while (not (string-match (car regexps
) host
))
750 (setq regexps
(cdr regexps
)))
752 (setq user-and-host user
)))
756 (cdr (assoc 'finger network-connection-service-alist
))
759 (defcustom whois-server-name
"rs.internic.net"
760 "Default host name for the whois service."
764 (defcustom whois-server-list
765 '(("whois.arin.net") ; Networks, ASN's, and related POC's (numbers)
766 ("rs.internic.net") ; domain related info
767 ("whois.publicinterestregistry.net")
774 "A list of whois servers that can be queried."
776 :type
'(repeat (list string
)))
778 ;; FIXME: modern whois clients include a much better tld <-> whois server
779 ;; list, Emacs should probably avoid specifying the server as the client
780 ;; will DTRT anyway... -rfr
781 (defcustom whois-server-tld
782 '(("rs.internic.net" .
"com")
783 ("whois.publicinterestregistry.net" .
"org")
784 ("whois.ripe.net" .
"be")
785 ("whois.ripe.net" .
"de")
786 ("whois.ripe.net" .
"dk")
787 ("whois.ripe.net" .
"it")
788 ("whois.ripe.net" .
"fi")
789 ("whois.ripe.net" .
"fr")
790 ("whois.ripe.net" .
"uk")
791 ("whois.apnic.net" .
"au")
792 ("whois.apnic.net" .
"ch")
793 ("whois.apnic.net" .
"hk")
794 ("whois.apnic.net" .
"jp")
795 ("whois.nic.gov" .
"gov")
796 ("whois.nic.mil" .
"mil"))
797 "Alist to map top level domains to whois servers."
799 :type
'(repeat (cons string string
)))
801 (defcustom whois-guess-server t
802 "If non-nil then whois will try to deduce the appropriate whois
803 server from the query. If the query doesn't look like a domain or hostname
804 then the server named by `whois-server-name' is used."
808 (defun whois-get-tld (host)
809 "Return the top level domain of `host', or nil if it isn't a domain name."
810 (let ((i (1- (length host
)))
811 (max-len (- (length host
) 5)))
812 (while (not (or (= i max-len
) (char-equal (aref host i
) ?.
)))
816 (substring host
(1+ i
)))))
820 (defun whois (arg search-string
)
821 "Send SEARCH-STRING to server defined by the `whois-server-name' variable.
822 If `whois-guess-server' is non-nil, then try to deduce the correct server
823 from SEARCH-STRING. With argument, prompt for whois server."
824 (interactive "P\nsWhois: ")
825 (let* ((whois-apropos-host (if whois-guess-server
826 (rassoc (whois-get-tld search-string
)
829 (server-name (if whois-apropos-host
830 (car whois-apropos-host
)
834 (completing-read "Whois server name: "
835 whois-server-list nil nil
"whois.")
840 (cdr (assoc 'whois network-connection-service-alist
))
843 (defcustom whois-reverse-lookup-server
"whois.arin.net"
844 "Server which provides inverse DNS mapping."
849 (defun whois-reverse-lookup ()
851 (let ((whois-server-name whois-reverse-lookup-server
))
852 (call-interactively 'whois
)))
854 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
855 ;;; General Network connection
856 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
858 ;; Using a derived mode gives us keymaps, hooks, etc.
860 network-connection-mode comint-mode
"Network-Connection"
861 "Major mode for interacting with the network-connection program.")
863 (defun network-connection-mode-setup (host service
)
864 (make-local-variable 'network-connection-host
)
865 (setq network-connection-host host
)
866 (make-local-variable 'network-connection-service
)
867 (setq network-connection-service service
))
870 (defun network-connection-to-service (host service
)
871 "Open a network connection to SERVICE on HOST."
874 (read-from-minibuffer "Host: " (net-utils-machine-at-point))
875 (completing-read "Service: "
879 (list (symbol-name (car elt
)))))
880 network-connection-service-alist
))))
883 (cdr (assoc (intern service
) network-connection-service-alist
))))
886 (defun network-connection (host port
)
887 "Open a network connection to HOST on PORT."
888 (interactive "sHost: \nnPort: ")
889 (network-service-connection host
(number-to-string port
)))
891 (defun network-service-connection (host service
)
892 "Open a network connection to SERVICE on HOST."
893 (let* ((process-name (concat "Network Connection [" host
" " service
"]"))
894 (portnum (string-to-number service
))
895 (buf (get-buffer-create (concat "*" process-name
"*"))))
896 (or (zerop portnum
) (setq service portnum
))
901 (network-connection-mode)
902 (network-connection-mode-setup host service
)
903 (pop-to-buffer buf
)))
905 (defvar comint-input-ring
)
907 (defun network-connection-reconnect ()
908 "Reconnect a network connection, preserving the old input ring."
910 (let ((proc (get-buffer-process (current-buffer)))
911 (old-comint-input-ring comint-input-ring
)
912 (host network-connection-host
)
913 (service network-connection-service
))
914 (if (not (or (not proc
)
915 (eq (process-status proc
) 'closed
)))
916 (message "Still connected")
917 (goto-char (point-max))
918 (insert (format "Reopening connection to %s\n" host
))
919 (network-connection host
920 (if (numberp service
)
922 (cdr (assoc service network-connection-service-alist
))))
923 (and old-comint-input-ring
924 (setq comint-input-ring old-comint-input-ring
)))))
928 ;;; net-utils.el ends here