-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial Attempt to resolve Issue #6739. Creating of DNS Server that o…
…pens UDP socket to send DNS queries for hostname resolution. Running into troubles getting UDP socket and DNS server to communicate, leading to timeout issues. Some help with this would be greatly appreciated :)
- Loading branch information
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import asyncio | ||
import socket | ||
|
||
class DNSProtocol: | ||
def __init__(self): | ||
self.response_received = asyncio.Event() | ||
self.response_data = None | ||
|
||
def received(self, data): | ||
self.response_data = data | ||
self.response_received.set() | ||
|
||
class DNSServer: | ||
async def async_getaddrinfo(self, hostname): | ||
try: | ||
udp_server_address = 'local' | ||
udp_server_port = 12234 | ||
|
||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | ||
sock.settimeout(10) # Set timeout threshold for socket operations | ||
|
||
try: | ||
sock.sendto(hostname.encode(), (udp_server_address, udp_server_port)) | ||
|
||
data, _ = sock.recvfrom(1024) | ||
|
||
ip_address = data.decode() | ||
return ip_address | ||
finally: | ||
sock.close() | ||
except socket.timeout as e: | ||
print("Timeout waiting for data from UDP socket:", e) | ||
return None | ||
except Exception as e: | ||
print("Error communicating with UDP server:", e) | ||
return None | ||
|
||
|
||
async def receive_from_socket(self, sock): | ||
try: | ||
data, _ = await asyncio.wait_for(sock.recvfrom(1024), timeout=5) | ||
return data | ||
except asyncio.TimeoutError as e: | ||
print("Timeout waiting for data from UDP socket:", e) | ||
return None | ||
except Exception as e: | ||
print("Error receiving data from UDP socket:", e) | ||
return None | ||
finally: | ||
sock.close() | ||
|
||
addons=[DNSServer()] |