Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local ipapi = {
- currentIP = "0.0.0.0",
- spoofMode = false,
- gateway = "",
- isIpRenewRequested = false,
- gatewayMac = "",
- debug=true,
- currentMac = require("component").modem.address
- }
- local component = require("component")
- local modem = component.modem
- local ser = require("serialization")
- local event = require("event")
- function ipapi.newPacket(type, payload, receiver, receiverMac, port)
- local packet = {}
- packet.senderIp = ipapi.currentIP
- packet.senderMac = require("computer").address()
- packet.toIP = receiver or "255.255.255.255"
- packet.payload = payload or ""
- packet.toMac = receiverMac
- packet.port = port or 255
- packet.type = type
- function packet:send()
- packet.send = nil
- if ipapi.gatewayMac == "" then
- if packet.toMac ~= nil then
- modem.send(packet.toMac, packet.port, "ipPacket", ser.serialize(packet))
- else
- print(packet.port, "ipPacket", ser.serialize(packet))
- modem.broadcast(packet.port, "ipPacket", ser.serialize(packet))
- end
- else
- if packet.toIP == "255.255.255.255" then
- modem.broadcast(packet.port, "ipPacket", ser.serialize(packet))
- else
- modem.send(ipapi.gatewayMac, 255, "ipPacket", ser.serialize(packet))
- end
- end
- end
- return packet
- end
- function ipapi.renewIP()
- if ipapi.debug then
- print("Renewing IP")
- end
- ipapi.isIpRenewRequested = true
- local packet = ipapi.newPacket("DHCPDiscover")
- packet:send()
- end
- event.listen("modem_message", function(...)
- local _, _, _, _, _, msg, packet = ...
- if msg == "ipPacket" then
- local packet = ser.unserialize(packet)
- if packet.senderIp == ipapi.currentIP or (packet.toIP ~= ipapi.currentIP and ipapi.spoofMode == false) or packet.toIP == "255.255.255.255" then
- return
- end
- print("Received packet from " .. packet.senderIp .. " to " .. packet.toIP..", payload: "..packet.payload)
- event.push("ip_message", packet.senderIp, packet.senderMac, packet.type, packet.payload, packet)
- if packet.type == "DHCPOffer" and ipapi.isIpRenewRequested then
- if ipapi.debug then
- print("Got DHCPOffer")
- end
- local offeredIp = packet.payload.ip
- local offeredGateway = packet.payload.gateway
- local offeredDNS = packet.payload.dns
- local packet = ipapi.newPacket("DHCPRequest", {
- ip = ipapi.currentIP,
- }, "255.255.255.255", packet.senderMac)
- end
- if packet.type == "DHCPACK" then
- if ipapi.debug then
- print("DHCP acknowledged. new ip set.")
- end
- ipapi.currentIP = packet.payload.ip
- ipapi.gateway = packet.payload.gateway
- ipapi.isIpRenewRequested = false
- event.push("new_ip", ipapi.currentIP, ipapi.gatewayMac)
- end
- if packet.type == "pong" then
- if ipapi.debug then
- print("Got pong")
- end
- end
- if packet.type == "ping" then
- if ipapi.debug then
- print("Got ping")
- end
- local packet = ipapi.newPacket("pong", nil, packet.senderIp)
- packet:send()
- end
- if packet.type == "ARPRequest" then
- if ipapi.debug then
- print("Got ARPRequest")
- end
- local requestedIp = packet.payload.ip
- if requestedIp == ipapi.currentIP then
- local packet = ipapi.newPacket("ARPReply", {
- ip = ipapi.currentIP,
- mac = ipapi.currentMac
- }, packet.senderIp == "0.0.0.0" and "255.255.255.255" or packet.senderIp)
- packet:send()
- end
- end
- end
- end)
- if modem.isOpen(255) == false then
- modem.open(255)
- end
- return ipapi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement