1 ;;; net-utils.el --- network functions
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Peter Breton <pbreton@cs.umb.edu>
7 ;; Created: Sun Mar 16 1997
8 ;; Keywords: network comm
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
30 ;; There are three main areas of functionality:
32 ;; * Wrap common network utility programs (ping, traceroute, netstat,
33 ;; nslookup, arp, route). Note that these wrappers are of the diagnostic
34 ;; functions of these programs only.
36 ;; * Implement some very basic protocols in Emacs Lisp (finger and whois)
38 ;; * Support connections to HOST/PORT, generally for debugging and the like.
39 ;; In other words, for doing much the same thing as "telnet HOST PORT", and
40 ;; then typing commands.
44 ;; On some systems, some of these programs are not in normal user path,
45 ;; but rather in /sbin, /usr/sbin, and so on.
50 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
51 ;; Customization Variables
52 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
54 (defgroup net-utils nil
55 "Network utility functions."
60 (defcustom net-utils-remove-ctl-m
61 (member system-type (list 'windows-nt 'msdos))
62 "If non-nil, remove control-Ms from output."
66 (defcustom traceroute-program
67 (if (eq system-type 'windows-nt)
70 "Program to trace network hops to a destination."
74 (defcustom traceroute-program-options nil
75 "Options for the traceroute program."
77 :type '(repeat string))
79 (defcustom ping-program "ping"
80 "Program to send network test packets to a host."
84 ;; On GNU/Linux and Irix, the system's ping program seems to send packets
85 ;; indefinitely unless told otherwise
86 (defcustom ping-program-options
87 (and (memq system-type (list 'linux 'gnu/linux 'irix))
89 "Options for the ping program.
90 These options can be used to limit how many ICMP packets are emitted."
92 :type '(repeat string))
94 (defcustom ifconfig-program
95 (if (eq system-type 'windows-nt)
98 "Program to print network configuration information."
102 (define-obsolete-variable-alias 'ipconfig-program 'ifconfig-program "22.2")
104 (defcustom ifconfig-program-options
106 (if (eq system-type 'windows-nt)
108 "Options for the ifconfig program."
110 :type '(repeat string))
112 (defcustom iwconfig-program "iwconfig"
113 "Program to print wireless network configuration information."
118 (defcustom iwconfig-program-options nil
119 "Options for `iwconfig-program'."
121 :type '(repeat string)
124 (define-obsolete-variable-alias 'ipconfig-program-options
125 'ifconfig-program-options "22.2")
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 "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 (defconst 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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
266 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
268 ;; Simplified versions of some at-point functions from ffap.el.
269 ;; It's not worth loading all of ffap just for these.
270 (defun net-utils-machine-at-point ()
272 (buffer-substring-no-properties
274 (skip-chars-backward "-a-zA-Z0-9.")
277 (skip-chars-forward "-a-zA-Z0-9.")
278 (skip-chars-backward "." pt)
281 (defun net-utils-url-at-point ()
283 (buffer-substring-no-properties
285 (skip-chars-backward "--:=&?$+@-Z_a-z~#,%")
286 (skip-chars-forward "^A-Za-z0-9" pt)
289 (skip-chars-forward "--:=&?$+@-Z_a-z~#,%")
290 (skip-chars-backward ":;.,!?" pt)
294 (defun net-utils-remove-ctrl-m-filter (process output-string)
295 "Remove trailing control Ms."
296 (let ((old-buffer (current-buffer))
297 (filtered-string output-string))
300 (set-buffer (process-buffer process))
301 (setq moving (= (point) (process-mark process)))
303 (while (string-match "\r" filtered-string)
304 (setq filtered-string
305 (replace-match "" nil nil filtered-string)))
308 ;; Insert the text, moving the process-marker.
309 (goto-char (process-mark process))
310 (insert filtered-string)
311 (set-marker (process-mark process) (point)))
312 (if moving (goto-char (process-mark process))))
313 (set-buffer old-buffer))))
315 (defmacro net-utils-run-program (name header program &rest args)
316 "Run a network information program."
317 ` (let ((buf (get-buffer-create (concat "*" ,name "*"))))
320 (insert ,header "\n")
322 (apply 'start-process ,name buf ,program ,@args)
323 'net-utils-remove-ctrl-m-filter)
327 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
328 ;; Wrappers for external network programs
329 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
332 (defun traceroute (target)
333 "Run traceroute program for TARGET."
334 (interactive "sTarget: ")
336 (if traceroute-program-options
337 (append traceroute-program-options (list target))
339 (net-utils-run-program
340 (concat "Traceroute" " " target)
341 (concat "** Traceroute ** " traceroute-program " ** " target)
348 If your system's ping continues until interrupted, you can try setting
349 `ping-program-options'."
351 (list (read-from-minibuffer "Ping host: " (net-utils-machine-at-point))))
353 (if ping-program-options
354 (append ping-program-options (list host))
356 (net-utils-run-program
357 (concat "Ping" " " host)
358 (concat "** Ping ** " ping-program " ** " host)
364 "Run ifconfig program."
366 (net-utils-run-program
368 (concat "** Ifconfig ** " ifconfig-program " ** ")
370 ifconfig-program-options))
372 ;; Windows uses this name.
374 (defalias 'ipconfig 'ifconfig)
378 "Run iwconfig program."
380 (net-utils-run-program
382 (concat "** Iwconfig ** " iwconfig-program " ** ")
384 iwconfig-program-options))
388 "Run netstat program."
390 (net-utils-run-program
392 (concat "** Netstat ** " netstat-program " ** ")
394 netstat-program-options))
400 (net-utils-run-program
402 (concat "** Arp ** " arp-program " ** ")
404 arp-program-options))
410 (net-utils-run-program
412 (concat "** Route ** " route-program " ** ")
414 route-program-options))
416 ;; FIXME -- Needs to be a process filter
417 ;; (defun netstat-with-filter (filter)
418 ;; "Run netstat program."
419 ;; (interactive "sFilter: ")
421 ;; (set-buffer (get-buffer "*Netstat*"))
422 ;; (goto-char (point-min))
423 ;; (delete-matching-lines filter))
426 (defun nslookup-host (host)
427 "Lookup the DNS information for HOST."
429 (list (read-from-minibuffer "Lookup host: " (net-utils-machine-at-point))))
431 (if nslookup-program-options
432 (append nslookup-program-options (list host))
434 (net-utils-run-program
438 (list "Nslookup" host nslookup-program)
445 "Run nslookup program."
447 (comint-run nslookup-program)
450 (defvar comint-prompt-regexp)
451 (defvar comint-input-autoexpand)
453 (autoload 'comint-mode "comint" nil t)
455 ;; Using a derived mode gives us keymaps, hooks, etc.
456 (define-derived-mode nslookup-mode comint-mode "Nslookup"
457 "Major mode for interacting with the nslookup program."
459 (make-local-variable 'font-lock-defaults)
460 '((nslookup-font-lock-keywords)))
461 (setq comint-prompt-regexp nslookup-prompt-regexp)
462 (setq comint-input-autoexpand t))
464 (define-key nslookup-mode-map "\t" 'comint-dynamic-complete)
467 (defun dns-lookup-host (host)
468 "Lookup the DNS information for HOST (name or IP address)."
470 (list (read-from-minibuffer "Lookup host: " (net-utils-machine-at-point))))
472 (if dns-lookup-program-options
473 (append dns-lookup-program-options (list host))
475 (net-utils-run-program
476 (concat "DNS Lookup [" host "]")
479 (list "DNS Lookup" host dns-lookup-program)
484 (autoload 'ffap-string-at-point "ffap")
487 (defun run-dig (host)
491 (read-from-minibuffer "Lookup host: "
492 (or (ffap-string-at-point 'machine) ""))))
493 (net-utils-run-program
497 (list "Dig" host dig-program)
502 (autoload 'comint-exec "comint")
504 ;; This is a lot less than ange-ftp, but much simpler.
510 (read-from-minibuffer
511 "Ftp to Host: " (net-utils-machine-at-point))))
512 (let ((buf (get-buffer-create (concat "*ftp [" host "]*"))))
515 (comint-exec buf (concat "ftp-" host) ftp-program nil
516 (if ftp-program-options
517 (append (list host) ftp-program-options)
519 (pop-to-buffer buf)))
521 (define-derived-mode ftp-mode comint-mode "FTP"
522 "Major mode for interacting with the ftp program."
523 (setq comint-prompt-regexp ftp-prompt-regexp)
524 (setq comint-input-autoexpand t)
525 ;; Only add the password-prompting hook if it's not already in the
526 ;; global hook list. This stands a small chance of losing, if it's
527 ;; later removed from the global list (very small, since any
528 ;; password prompts will probably immediately follow the initial
529 ;; connection), but it's better than getting prompted twice for the
531 (unless (memq 'comint-watch-for-password-prompt
532 (default-value 'comint-output-filter-functions))
533 (add-hook 'comint-output-filter-functions 'comint-watch-for-password-prompt
536 ;; Occasionally useful
537 (define-key ftp-mode-map "\t" 'comint-dynamic-complete)
539 (defun smbclient (host service)
540 "Connect to SERVICE on HOST via SMB."
543 (read-from-minibuffer
544 "Connect to Host: " (net-utils-machine-at-point))
545 (read-from-minibuffer "SMB Service: ")))
546 (let* ((name (format "smbclient [%s\\%s]" host service))
547 (buf (get-buffer-create (concat "*" name "*")))
548 (service-name (concat "\\\\" host "\\" service)))
551 (comint-exec buf name smbclient-program nil
552 (if smbclient-program-options
553 (append (list service-name) smbclient-program-options)
554 (list service-name)))
555 (pop-to-buffer buf)))
557 (defun smbclient-list-shares (host)
558 "List services on HOST."
561 (read-from-minibuffer
562 "Connect to Host: " (net-utils-machine-at-point))))
563 (let ((buf (get-buffer-create (format "*SMB Shares on %s*" host))))
566 (comint-exec buf "smbclient-list-shares"
567 smbclient-program nil (list "-L" host))
568 (pop-to-buffer buf)))
570 (define-derived-mode smbclient-mode comint-mode "smbclient"
571 "Major mode for interacting with the smbclient program."
572 (setq comint-prompt-regexp smbclient-prompt-regexp)
573 (setq comint-input-autoexpand t)
574 ;; Only add the password-prompting hook if it's not already in the
575 ;; global hook list. This stands a small chance of losing, if it's
576 ;; later removed from the global list (very small, since any
577 ;; password prompts will probably immediately follow the initial
578 ;; connection), but it's better than getting prompted twice for the
580 (unless (memq 'comint-watch-for-password-prompt
581 (default-value 'comint-output-filter-functions))
582 (add-hook 'comint-output-filter-functions 'comint-watch-for-password-prompt
586 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
587 ;; Network Connections
588 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
590 ;; Full list is available at:
591 ;; http://www.iana.org/assignments/port-numbers
592 (defvar network-connection-service-alist
595 (cons 'active-users 11)
611 (cons 'netbios-name 137)
612 (cons 'netbios-data 139)
616 "Alist of services and associated TCP port numbers.
617 This list is not complete.")
620 (defmacro run-network-program (process-name host port
621 &optional initial-string)
622 `(let ((tcp-connection)
624 (setq buf (get-buffer-create (concat "*" ,process-name "*")))
633 (error "Could not open connection to %s" ,host))
635 (set-marker (process-mark tcp-connection) (point-min))
636 (set-process-filter tcp-connection 'net-utils-remove-ctrl-m-filter)
638 (process-send-string tcp-connection
639 (concat ,initial-string "\r\n")))
640 (display-buffer buf)))
642 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
644 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
646 (defcustom finger-X.500-host-regexps nil
647 "A list of regular expressions matching host names.
648 If a host name passed to `finger' matches one of these regular
649 expressions, it is assumed to be a host that doesn't accept
650 queries of the form USER@HOST, and wants a query containing USER only."
652 :type '(repeat regexp)
657 (defun finger (user host)
658 "Finger USER on HOST."
659 ;; One of those great interactive statements that's actually
660 ;; longer than the function call! The idea is that if the user
661 ;; uses a string like "pbreton@cs.umb.edu", we won't ask for the
662 ;; host name. If we don't see an "@", we'll prompt for the host.
664 (let* ((answer (read-from-minibuffer "Finger User: "
665 (net-utils-url-at-point)))
666 (index (string-match (regexp-quote "@") answer)))
668 (list (substring answer 0 index)
669 (substring answer (1+ index)))
671 (read-from-minibuffer "At Host: "
672 (net-utils-machine-at-point))))))
673 (let* ((user-and-host (concat user "@" host))
674 (process-name (concat "Finger [" user-and-host "]"))
675 (regexps finger-X.500-host-regexps)
678 (while (not (string-match (car regexps) host))
679 (setq regexps (cdr regexps)))
681 (setq user-and-host user)))
685 (cdr (assoc 'finger network-connection-service-alist))
688 (defcustom whois-server-name "rs.internic.net"
689 "Default host name for the whois service."
693 (defcustom whois-server-list
694 '(("whois.arin.net") ; Networks, ASN's, and related POC's (numbers)
695 ("rs.internic.net") ; domain related info
696 ("whois.publicinterestregistry.net")
703 "A list of whois servers that can be queried."
705 :type '(repeat (list string)))
707 ;; FIXME: modern whois clients include a much better tld <-> whois server
708 ;; list, Emacs should probably avoid specifying the server as the client
709 ;; will DTRT anyway... -rfr
710 (defcustom whois-server-tld
711 '(("rs.internic.net" . "com")
712 ("whois.publicinterestregistry.net" . "org")
713 ("whois.ripe.net" . "be")
714 ("whois.ripe.net" . "de")
715 ("whois.ripe.net" . "dk")
716 ("whois.ripe.net" . "it")
717 ("whois.ripe.net" . "fi")
718 ("whois.ripe.net" . "fr")
719 ("whois.ripe.net" . "uk")
720 ("whois.apnic.net" . "au")
721 ("whois.apnic.net" . "ch")
722 ("whois.apnic.net" . "hk")
723 ("whois.apnic.net" . "jp")
724 ("whois.nic.gov" . "gov")
725 ("whois.nic.mil" . "mil"))
726 "Alist to map top level domains to whois servers."
728 :type '(repeat (cons string string)))
730 (defcustom whois-guess-server t
731 "If non-nil then whois will try to deduce the appropriate whois
732 server from the query. If the query doesn't look like a domain or hostname
733 then the server named by `whois-server-name' is used."
737 (defun whois-get-tld (host)
738 "Return the top level domain of `host', or nil if it isn't a domain name."
739 (let ((i (1- (length host)))
740 (max-len (- (length host) 5)))
741 (while (not (or (= i max-len) (char-equal (aref host i) ?.)))
745 (substring host (1+ i)))))
749 (defun whois (arg search-string)
750 "Send SEARCH-STRING to server defined by the `whois-server-name' variable.
751 If `whois-guess-server' is non-nil, then try to deduce the correct server
752 from SEARCH-STRING. With argument, prompt for whois server."
753 (interactive "P\nsWhois: ")
754 (let* ((whois-apropos-host (if whois-guess-server
755 (rassoc (whois-get-tld search-string)
758 (server-name (if whois-apropos-host
759 (car whois-apropos-host)
763 (completing-read "Whois server name: "
764 whois-server-list nil nil "whois.")
769 (cdr (assoc 'whois network-connection-service-alist))
772 (defcustom whois-reverse-lookup-server "whois.arin.net"
773 "Server which provides inverse DNS mapping."
778 (defun whois-reverse-lookup ()
780 (let ((whois-server-name whois-reverse-lookup-server))
781 (call-interactively 'whois)))
783 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
784 ;;; General Network connection
785 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
787 ;; Using a derived mode gives us keymaps, hooks, etc.
789 network-connection-mode comint-mode "Network-Connection"
790 "Major mode for interacting with the network-connection program.")
792 (defun network-connection-mode-setup (host service)
793 (make-local-variable 'network-connection-host)
794 (setq network-connection-host host)
795 (make-local-variable 'network-connection-service)
796 (setq network-connection-service service))
799 (defun network-connection-to-service (host service)
800 "Open a network connection to SERVICE on HOST."
803 (read-from-minibuffer "Host: " (net-utils-machine-at-point))
804 (completing-read "Service: "
808 (list (symbol-name (car elt)))))
809 network-connection-service-alist))))
812 (cdr (assoc (intern service) network-connection-service-alist))))
815 (defun network-connection (host port)
816 "Open a network connection to HOST on PORT."
817 (interactive "sHost: \nnPort: ")
818 (network-service-connection host (number-to-string port)))
820 (defun network-service-connection (host service)
821 "Open a network connection to SERVICE on HOST."
822 (let* ((process-name (concat "Network Connection [" host " " service "]"))
823 (portnum (string-to-number service))
824 (buf (get-buffer-create (concat "*" process-name "*"))))
825 (or (zerop portnum) (setq service portnum))
830 (network-connection-mode)
831 (network-connection-mode-setup host service)
832 (pop-to-buffer buf)))
834 (defvar comint-input-ring)
836 (defun network-connection-reconnect ()
837 "Reconnect a network connection, preserving the old input ring."
839 (let ((proc (get-buffer-process (current-buffer)))
840 (old-comint-input-ring comint-input-ring)
841 (host network-connection-host)
842 (service network-connection-service))
843 (if (not (or (not proc)
844 (eq (process-status proc) 'closed)))
845 (message "Still connected")
846 (goto-char (point-max))
847 (insert (format "Reopening connection to %s\n" host))
848 (network-connection host
849 (if (numberp service)
851 (cdr (assoc service network-connection-service-alist))))
852 (and old-comint-input-ring
853 (setq comint-input-ring old-comint-input-ring)))))
857 ;;; arch-tag: 97119e91-9edb-4376-838b-bf7058fa1314
858 ;;; net-utils.el ends here