aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobert Pluim2020-04-03 17:37:01 +0200
committerRobert Pluim2020-04-07 14:32:44 +0200
commit23b04ef0e7d03cd7c178b544d5fff2bda4c7c504 (patch)
tree11049b26b6f315141436bbb7d8329d7581142894
parent8d95e75eb68745322a23424f1af5ab86f0cb0c3b (diff)
downloademacs-23b04ef0e7d03cd7c178b544d5fff2bda4c7c504.tar.gz
emacs-23b04ef0e7d03cd7c178b544d5fff2bda4c7c504.zip
Use length field when dns-query is using TCP
* net/dns.el (dns-write): Correct spelling in docstring. (dns-read): Add optional tcp-p parameter, skip 2-byte length field if non-nil. (dns-query): Tell dns-read and dns-write if we're using TCP.
-rw-r--r--lisp/net/dns.el30
1 files changed, 17 insertions, 13 deletions
diff --git a/lisp/net/dns.el b/lisp/net/dns.el
index 177df4e3329..53ea0b19b52 100644
--- a/lisp/net/dns.el
+++ b/lisp/net/dns.el
@@ -138,7 +138,7 @@ updated. Set this variable to t to disable the check.")
138 138
139(defun dns-write (spec &optional tcp-p) 139(defun dns-write (spec &optional tcp-p)
140 "Write a DNS packet according to SPEC. 140 "Write a DNS packet according to SPEC.
141If TCP-P, the first two bytes of the package with be the length field." 141If TCP-P, the first two bytes of the packet will be the length field."
142 (with-temp-buffer 142 (with-temp-buffer
143 (set-buffer-multibyte nil) 143 (set-buffer-multibyte nil)
144 (dns-write-bytes (dns-get 'id spec) 2) 144 (dns-write-bytes (dns-get 'id spec) 2)
@@ -189,13 +189,15 @@ If TCP-P, the first two bytes of the package with be the length field."
189 (dns-write-bytes (buffer-size) 2)) 189 (dns-write-bytes (buffer-size) 2))
190 (buffer-string))) 190 (buffer-string)))
191 191
192(defun dns-read (packet) 192(defun dns-read (packet &optional tcp-p)
193 (with-temp-buffer 193 (with-temp-buffer
194 (set-buffer-multibyte nil) 194 (set-buffer-multibyte nil)
195 (let ((spec nil) 195 (let ((spec nil)
196 queries answers authorities additionals) 196 queries answers authorities additionals)
197 (insert packet) 197 (insert packet)
198 (goto-char (point-min)) 198 ;; When using TCP we have a 2 byte length field to ignore.
199 (goto-char (+ (point-min)
200 (if tcp-p 2 0)))
199 (push (list 'id (dns-read-bytes 2)) spec) 201 (push (list 'id (dns-read-bytes 2)) spec)
200 (let ((byte (dns-read-bytes 1))) 202 (let ((byte (dns-read-bytes 1)))
201 (push (list 'response-p (if (zerop (logand byte (ash 1 7))) nil t)) 203 (push (list 'response-p (if (zerop (logand byte (ash 1 7))) nil t))
@@ -407,23 +409,25 @@ If REVERSEP, look up an IP address."
407 nil) 409 nil)
408 (with-temp-buffer 410 (with-temp-buffer
409 (set-buffer-multibyte nil) 411 (set-buffer-multibyte nil)
410 (let ((process (condition-case () 412 (let* ((process (condition-case ()
411 (dns-make-network-process (car dns-servers)) 413 (dns-make-network-process (car dns-servers))
412 (error 414 (error
413 (message 415 (message
414 "dns: Got an error while trying to talk to %s" 416 "dns: Got an error while trying to talk to %s"
415 (car dns-servers)) 417 (car dns-servers))
416 nil))) 418 nil)))
417 (step 100) 419 (step 100)
418 (times (* dns-timeout 1000)) 420 (times (* dns-timeout 1000))
419 (id (random 65000))) 421 (id (random 65000))
422 (tcp-p (and process (not (process-contact process :type)))))
420 (when process 423 (when process
421 (process-send-string 424 (process-send-string
422 process 425 process
423 (dns-write `((id ,id) 426 (dns-write `((id ,id)
424 (opcode query) 427 (opcode query)
425 (queries ((,name (type ,type)))) 428 (queries ((,name (type ,type))))
426 (recursion-desired-p t)))) 429 (recursion-desired-p t))
430 tcp-p))
427 (while (and (zerop (buffer-size)) 431 (while (and (zerop (buffer-size))
428 (> times 0)) 432 (> times 0))
429 (let ((step-sec (/ step 1000.0))) 433 (let ((step-sec (/ step 1000.0)))
@@ -436,7 +440,7 @@ If REVERSEP, look up an IP address."
436 (when (and (>= (buffer-size) 2) 440 (when (and (>= (buffer-size) 2)
437 ;; We had a time-out. 441 ;; We had a time-out.
438 (> times 0)) 442 (> times 0))
439 (let ((result (dns-read (buffer-string)))) 443 (let ((result (dns-read (buffer-string) tcp-p)))
440 (if fullp 444 (if fullp
441 result 445 result
442 (let ((answer (car (dns-get 'answers result)))) 446 (let ((answer (car (dns-get 'answers result))))