Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- dns_server.lua
- -- שרת ראשי של ה־DNS, רץ על מחשב עם מודם
- local modemSide = "back"
- rednet.open(modemSide)
- -- קונפיגורציה של A ו‑B
- local A = 5
- local B = 29
- -- אתחול מספור X
- local nextID = 2
- -- טבלת DNS (hostname → IP)
- local dnsTable = {}
- -- אתחול מחולל אקראיות
- math.randomseed(os.time())
- print("DNS server listening…")
- while true do
- -- מקבלים הודעה מסוג טבלה בפרוטוקול "DNS"
- local senderID, msg = rednet.receive("DNS")
- if type(msg) == "table" and msg.cmd then
- if msg.cmd == "requestIP" then
- local host = msg.name
- if not dnsTable[host] then
- -- בוחרים Y רנדומלי בין 100 ל־250
- local zone = math.random(100, 250)
- -- בונים IP בפורמט 5.29.Y.X
- local ip = string.format("%d.%d.%d.%d", A, B, zone, nextID)
- nextID = nextID + 1
- dnsTable[host] = ip
- print("Registered", host, "→", ip)
- end
- -- שולחים בחזרה את ה‑IP (חדש או קיים)
- rednet.send(senderID, { ip = dnsTable[host] }, "DNS")
- elseif msg.cmd == "resolve" then
- -- מחזירים IP של hostname קיים (או nil אם לא נמצא)
- rednet.send(senderID, { ip = dnsTable[msg.name] }, "DNS")
- elseif msg.cmd == "list" then
- -- שולחים את כל הטבלה
- rednet.send(senderID, { table = dnsTable }, "DNS")
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment