Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2006cdd

Browse filesBrowse files
committed
Simplify and fix PR 70
1 parent c3f563f commit 2006cdd
Copy full SHA for 2006cdd

File tree

Expand file treeCollapse file tree

1 file changed

+24
-47
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+24
-47
lines changed

‎zeroconf.py

Copy file name to clipboardExpand all lines: zeroconf.py
+24-47Lines changed: 24 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ def emit(self, record):
8383
_DNS_TTL = 60 * 60 # one hour default TTL
8484

8585
_MAX_MSG_TYPICAL = 1460 # unused
86-
_MAX_MSG_ABSOLUTE = 8972
86+
_MAX_MSG_ABSOLUTE = 8966
8787

8888
_FLAGS_QR_MASK = 0x8000 # query response mask
8989
_FLAGS_QR_QUERY = 0x0000 # query
9090
_FLAGS_QR_RESPONSE = 0x8000 # response
9191

92-
_FLAGS_AA = 0x0400 # Authorative answer
92+
_FLAGS_AA = 0x0400 # Authoritative answer
9393
_FLAGS_TC = 0x0200 # Truncated
9494
_FLAGS_RD = 0x0100 # Recursion desired
9595
_FLAGS_RA = 0x8000 # Recursion available
@@ -750,15 +750,14 @@ class DNSOutgoing(object):
750750

751751
"""Object representation of an outgoing packet"""
752752

753-
def __init__(self, flags, multicast=True, build_on_fly=False):
753+
def __init__(self, flags, multicast=True):
754754
self.finished = False
755755
self.id = 0
756756
self.multicast = multicast
757757
self.flags = flags
758758
self.names = {}
759759
self.data = []
760760
self.size = 12
761-
self.build_on_fly = build_on_fly
762761
self.state = self.State.init
763762

764763
self.questions = []
@@ -768,26 +767,11 @@ def __init__(self, flags, multicast=True, build_on_fly=False):
768767

769768
class State(enum.Enum):
770769
init = 0
771-
adding_questions = 1
772-
adding_answers = 2
773-
adding_authoratives = 3
774-
adding_additionals = 4
775-
finished = 5
776-
777-
def set_state(self, state):
778-
if self.state != state:
779-
if self.state.value > state.value:
780-
raise Error('Out of order DNSOutgoing build %s -> %s' % (
781-
self.state.name, state.name))
782-
self.state = state
783-
return self.state != self.State.finished
770+
finished = 1
784771

785772
def add_question(self, record):
786773
"""Adds a question"""
787774
self.questions.append(record)
788-
if self.build_on_fly:
789-
if self.set_state(self.State.adding_questions):
790-
self.write_question(record)
791775

792776
def add_answer(self, inp, record):
793777
"""Adds an answer"""
@@ -799,23 +783,14 @@ def add_answer_at_time(self, record, now):
799783
if record is not None:
800784
if now == 0 or not record.is_expired(now):
801785
self.answers.append((record, now))
802-
if self.build_on_fly:
803-
if self.set_state(self.State.adding_answers):
804-
self.write_record(record, now)
805786

806787
def add_authorative_answer(self, record):
807788
"""Adds an authoritative answer"""
808789
self.authorities.append(record)
809-
if self.build_on_fly:
810-
if self.set_state(self.State.adding_authoratives):
811-
self.write_record(record, 0)
812790

813791
def add_additional_answer(self, record):
814792
"""Adds an additional answer"""
815793
self.additionals.append(record)
816-
if self.build_on_fly:
817-
if self.set_state(self.State.adding_additionals):
818-
self.write_record(record, 0)
819794

820795
def pack(self, format_, value):
821796
self.data.append(struct.pack(format_, value))
@@ -916,6 +891,9 @@ def write_question(self, question):
916891
def write_record(self, record, now):
917892
"""Writes a record (answer, authoritative answer, additional) to
918893
the packet"""
894+
if self.state == self.State.finished:
895+
return 1
896+
919897
start_data_length, start_size = len(self.data), self.size
920898
self.write_name(record.name)
921899
self.write_short(record.type)
@@ -944,30 +922,31 @@ def write_record(self, record, now):
944922
self.data.pop()
945923
self.size = start_size
946924
self.state = self.State.finished
925+
return 1
926+
return 0
947927

948928
def packet(self):
949929
"""Returns a string containing the packet's bytes
950930
951931
No further parts should be added to the packet once this
952932
is done."""
933+
934+
overrun_answers, overrun_authorities, overrun_additionals = 0, 0, 0
935+
953936
if self.state != self.State.finished:
954-
if not self.build_on_fly:
955-
for question in self.questions:
956-
self.write_question(question)
957-
for answer, time_ in self.answers:
958-
if self.state != self.State.finished:
959-
self.write_record(answer, time_)
960-
for authority in self.authorities:
961-
if self.state != self.State.finished:
962-
self.write_record(authority, 0)
963-
for additional in self.additionals:
964-
if self.state != self.State.finished:
965-
self.write_record(additional, 0)
937+
for question in self.questions:
938+
self.write_question(question)
939+
for answer, time_ in self.answers:
940+
overrun_answers += self.write_record(answer, time_)
941+
for authority in self.authorities:
942+
overrun_authorities += self.write_record(authority, 0)
943+
for additional in self.additionals:
944+
overrun_additionals += self.write_record(additional, 0)
966945
self.state = self.State.finished
967946

968-
self.insert_short(0, len(self.additionals))
969-
self.insert_short(0, len(self.authorities))
970-
self.insert_short(0, len(self.answers))
947+
self.insert_short(0, len(self.additionals) - overrun_additionals)
948+
self.insert_short(0, len(self.authorities) - overrun_authorities)
949+
self.insert_short(0, len(self.answers) - overrun_answers)
971950
self.insert_short(0, len(self.questions))
972951
self.insert_short(0, self.flags)
973952
if self.multicast:
@@ -1283,13 +1262,11 @@ def run(self):
12831262
return
12841263
now = current_time_millis()
12851264
if self.next_time <= now:
1286-
out = DNSOutgoing(_FLAGS_QR_QUERY, build_on_fly=True)
1265+
out = DNSOutgoing(_FLAGS_QR_QUERY)
12871266
out.add_question(DNSQuestion(self.type, _TYPE_PTR, _CLASS_IN))
12881267
for record in self.services.values():
12891268
if not record.is_expired(now):
12901269
out.add_answer_at_time(record, now)
1291-
if out.state == out.State.finished:
1292-
break
12931270

12941271
self.zc.send(out)
12951272
self.next_time = now + self.delay

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.