Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/aceinna/framework/communicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,67 @@
from .wrapper import SocketConnWrapper


class netbios_query:
def __init__(self,name):
self.name = name
self.populate()
def populate(self):
self.HOST = '192.168.137.255'
self.PORT = 137
self.nqs = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.nqs.setblocking(False)
self.QueryData = [
b"\xa9\xfb", # Transaction ID
b"\x01\x10", # Flags Query
b"\x00\x01", # Question:1
b"\x00\x00", # Answer RRS
b"\x00\x00", # Authority RRS
b"\x00\x00", # Additional RRS
b"\x20", # length of Name:32
b"NAME", # Name
b"\x00", # NameNull
b"\x00\x20", # Query Type:NB
b"\x00\x01"] # Class
self.QueryData[7] = str.encode(self.netbios_encode(self.name))



def netbios_encode(self,src):
src = src.ljust(15,"\x20")
src = src.ljust(16,"\x00")
names = []
for c in src:
char_ord = ord(c)
high_4_bits = char_ord >> 4
low_4_bits = char_ord & 0x0f
names.append(high_4_bits)
names.append(low_4_bits)
res = ""
for name in names:
res += chr(0x41+name)
return res

def Query(self):
wait_count = 10
send_data = []
ret = False
for bytes_ele in self.QueryData:
for list_ele in bytes_ele:
send_data.append(list_ele)
while wait_count:
try:
self.nqs.sendto(bytes(send_data), (self.HOST, self.PORT))
data_rev, ADDR = self.nqs.recvfrom(1024)
if(len(data_rev) > 0):
#print(data_rev)
ret = True
break
except:
time.sleep(1)
wait_count-= 1
self.nqs.close()
return ret

class CommunicatorFactory:
'''
Communicator Factory
Expand Down Expand Up @@ -596,7 +657,12 @@ def read(self, size=100):
def find_client_by_hostname(self, name):
is_find = False
try:
nbns = netbios_query("OPENRTK")
ret = nbns.Query()
#print(ret)
'''
socket.gethostbyname(name)
'''
is_find = True
except Exception:
is_find = False
Expand Down