Advertisement
klindley

rnip

Mar 12th, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.01 KB | None | 0 0
  1. ----------------------------------------
  2. -- RNIP
  3. -- v0.1
  4. ----------------------------------------
  5.  
  6. -- CONSTANTS
  7. -- List of constants
  8. log_file = fs.open("/log.txt", "w")
  9. -- FILTERING
  10. ETHERNET = 1
  11. ARP = 2
  12. IPV4 = 3
  13.  
  14. DISABLED = 0
  15. ENABLED = 1
  16. PROMISCUOUS = 2
  17.  
  18. -- Hardware Layer (1)
  19. CHANNEL = 9088
  20.  
  21. MAC = os.getComputerID()
  22. MAC_BROADCAST = 9099 -- The broadcast channl
  23.  
  24. -- Ethernet info (Network Layer)
  25. ETHERNET_TEMPLATE = {source=0, target=0, ethertype=2048, payload=""}
  26. ETHERNET_TYPE_IPV4 = 0x0800
  27. ETHERNET_TYPE_ARP = 0x0806
  28.  
  29. -- ARP Layers 2 and 3
  30. ARP_TEMPLATE = {protocol=2048, operation=1, sha=0, spa="", tha=0, tpa=""}
  31. APR_REQUEST = 1
  32. ARP_REPLY = 2
  33.  
  34. -- IPv4 (Data Link Layer)
  35. IPV4_TEMPLATE = {ttl=128, protocol=17, source="", destination="", payload=""}
  36. IPV4_BROADCAST = "255.255.255.255"
  37. IPV4_LOCALHOST = "127.0.0.1"
  38. IPV4_PROTOCOL_ICMP = 1
  39. IPV4_PROTOCOL_TCP = 6
  40. IPV4_PROTOCOL_UDP = 17
  41. IPV4_DEFAULT_TTL = 128
  42.  
  43. --TCP
  44. TCP_TEMPLATE = {port=0, seq=0, syn_flag=false, ack_flag=false, fin_flag=false, rst_flag=false, payload=""}
  45. TCP_STATE_LISTEN = 1
  46. TCP_STATE_SYN_SENT = 2
  47. TCP_STATE_SYN_RECEIVED = 3
  48. TCP_STATE_ESTABLISHED = 4
  49. TCP_STATE_FIN_WAIT_1 = 5
  50. TCP_STATE_FIN_WAIT_2 = 6
  51. TCP_STATE_CLOSE_WAIT = 7
  52. TCP_STATE_LAST_ACK = 8
  53. TCP_STATE_TIME_WAIT = 9
  54. TCP_STATE_CLOSED = 10
  55.  
  56. -- ICMP
  57. ICMP_TEMPLATE = {type=8, code=0, payload=""}
  58. ICMP_TYPE_ECHO_REPLY = 0
  59. ICMP_TYPE_DESTINATION_UNREACHABLE = 3
  60. ICMP_TYPE_ECHO = 8
  61. ICMP_TYPE_TIME_EXCEEDED = 11
  62. ICMP_TEMPLATE_ECHO = {identifier = 0, sequence = 0, payload = ""}
  63.  
  64.  
  65. SOCKET_TEMPLATE = {local_address="", remote_address="", local_port=0, remote_port=0, state=TCP_STATE_CLOSED, seq=0, protocol=IPV4_PROTOCOL_TCP}
  66. -- END CONSTANTS
  67.  
  68. -- Runtime variables
  69. interfaces = {} -- associates interface names with modems
  70. interface_sides = {} -- associates block sides with interface names
  71. default_interface = nil -- first interface initialized
  72. packet_filters = {} -- protocol type
  73.  
  74. ipv4_packet_queue = {}
  75.  
  76. -- Networking variables
  77. arp_cache = {}
  78.  
  79. ipv4_interfaces = {}
  80. ipv4_routes = {}
  81.  
  82. sockets = {} -- {local_address, remote_address, local_port, remote_port, state=<state>, seq=<random_seq_number>, protocol=<protocol>}
  83.  
  84. icmp_echo_identifier = 42
  85. icmp_echo_sequence = 0
  86.  
  87. socket_id = 0
  88. listener_id = 0
  89.  
  90. function initialize()
  91. local SIDES = {"front", "back", "top", "bottom", "left", "right"}
  92. local wired, wireless = 0,0
  93.  
  94. for i, side in ipairs(SIDES) do
  95. local device = peripheral.wrap(side)
  96.  
  97. if device ~= nil then
  98. local interface = nil
  99.  
  100. if peripheral.getType(side) == "modem" then
  101. if device.isWireless() then
  102. interface = "wlan"..wireless
  103. wireless = wireless + 1
  104. else
  105. interface = "eth"..wired
  106. wired = wired + 1
  107. end
  108.  
  109. device.open(CHANNEL)
  110. end
  111.  
  112. if interface ~= nil then
  113. if default_interface == nil then
  114. default_interface = interface
  115. interfaces["default"] = device
  116. end
  117.  
  118. interfaces[interface] = device
  119. interface_sides[side] = interface
  120. end
  121. end
  122. end
  123. end
  124.  
  125. function stop()
  126. for interface, modem in interfaces do
  127. modem.close(CHANNEL)
  128. end
  129.  
  130. interfaces = {}
  131. end
  132.  
  133. function send_raw (interface, frame)
  134. if interface == "default" then
  135. interface = default_interface
  136. end
  137.  
  138. interfaces[interface].transmit(CHANNEL, CHANNEL, frame)
  139. end
  140.  
  141. function receive_raw()
  142. local event, side, message, distance
  143.  
  144. parallel.waitForAny(
  145. function()
  146. event, side, _, _, message, distance = os.pullEventRaw("modem_message")
  147. end
  148. )
  149.  
  150. local packet = textutils.unserialize(message)
  151.  
  152. local interface = get_interface(side)
  153.  
  154. ethernet_event(interface, packet)
  155.  
  156. return interface, packet
  157. end
  158.  
  159. function loop()
  160.  
  161. while true do
  162. local interface, packet = receive_raw()
  163. local send = false
  164.  
  165. for protocol, state in pairs(packet_filters) do
  166. if state ~= DISABLED then
  167. if protocol == ETHERNET then
  168. if packet.target == MAC or state == PROMISCUOUS then send = true end
  169. elseif protocol == ARP then
  170. if packet.ethertype == ETHERNET_TYPE_ARP then
  171. if packet.payload.tha == MAC
  172. or packet.payload.tpa == ipv4_interfaces[interface]
  173. or state == PROMISCUOUS then send = true end
  174. end
  175. elseif protocol == IPV4 then
  176. print("IPV4")
  177. if packet.target == MAC and packet.ethertype == ETHERNET_TYPE_IPV4 then
  178. print("IPV4-2")
  179. if (packet.payload.destination == ipv4_interfaces[interface] or packet.payload.destination == IPV4_BROADCAST)
  180. or state == PROMISCUOUS then
  181. print("IPV4-3")
  182. send = true
  183. end
  184. end
  185. elseif protocol == ICMP then
  186. if packet.ethertype == ETHERNET_TYPE_IPV4 and packet.payload.protocol == IPV4_PROTOCOL_ICMP then
  187. if (packet.payload.destination == ipv4_interfaces[interface] or packet.payload.destination == IPV4_BROADCAST)
  188. or state == PROMISCUOUS then send = true end
  189. end
  190. end
  191. end
  192. end
  193.  
  194. if send then os.queueEvent("rnip", interface, packet) end
  195.  
  196. end
  197. end
  198.  
  199. function get_interface(side)
  200. return interface_sides[side]
  201. end
  202.  
  203. function filter (protocol, state)
  204. packet_filters[protocol] = state
  205. end
  206.  
  207. --[[function run (user_function)
  208. parallel.waitForAny(
  209. function()
  210. user_function()
  211. end,
  212. function()
  213. loop()
  214. end
  215. )
  216. end
  217. ]]--
  218.  
  219. function ethernet_send(interface, target, type, message)
  220. local frame = ETHERNET_TEMPLATE
  221. frame.source = MAC
  222. frame.target = target
  223. frame.ethertype = type
  224. frame.payload = message
  225.  
  226. log_file.write("Sent: "..textutils.serialize(frame))
  227. log_file.flush()
  228. send_raw(interface, textutils.serialize(frame))
  229.  
  230. end
  231.  
  232. function ethernet_event(interface, frame)
  233. log_file.write("Received: "..textutils.serialize(frame))
  234. log_file.flush()
  235. if frame.ethertype == ETHERNET_TYPE_ARP then
  236. arp_event(interface, frame, frame.payload)
  237. elseif frame.ethertype == ETHERNET_TYPE_IPV4 then
  238. print("got IPV4")
  239. ipv4_event(interface, frame, frame.payload)
  240. end
  241. end
  242.  
  243. -- ARP
  244. function arp_send(interface, protocol, operation, sha, spa, tha, tpa)
  245. local packet = ARP_TEMPLATE
  246. packet.protocol = protocol
  247. packet.operation = operation
  248. packet.sha = sha
  249. packet.spa = spa
  250. packet.tha = tha
  251. packet.tpa = tpa
  252.  
  253. ethernet_send(interface, (operation == ARP_REQUEST and MAC_BROADCAST or tha), ETHERNET_TYPE_ARP, packet)
  254. end
  255.  
  256. function arp_request (interface, tpa)
  257. if arp_cache[tpa] == nil then
  258. arp_send(interface, ETHERNET_TYPE_IPV4, ARP_REQUEST, MAC, ipv4_interfaces[interface], 0, tpa)
  259. end
  260. end
  261.  
  262. function arp_reply(interface, tha, tpa)
  263. arp_send(interface, ETHERNET_TYPE_IPV4, ARP_REPLY, MAC, ipv4_interfaces[interface], tha, tpa)
  264. end
  265.  
  266. function arp_event(interface, raw, arp)
  267. if arp.operation == ARP_REQUEST then
  268. if arp.tpa == ipv4_interfaces[interface] then
  269. arp_reply(interface, arp.sha, arp.spa)
  270. end
  271.  
  272. end
  273. arp_cache[arp.spa] = arp.sha
  274. --print(textutils.serialize(arp_cache))
  275. ipv4_process_queue()
  276.  
  277. end
  278.  
  279. -- IPV4
  280. function ipv4_initialize(interface, address, subnet, gateway)
  281. if interface == "default" or interface == default_interface then
  282. ipv4_interfaces["default"] = addres
  283. interface = default_interface
  284. end
  285.  
  286. ipv4_interfaces[interface] = address
  287.  
  288. arp_cache[IPV4_BROADCAST] = MAC_BROADCAST
  289. arp_cache[address] = MAC
  290.  
  291. prefix = 32 - math.ceil(math.log10(bit.bxor(math.pow(2, 32) - 1, ip_to_binary(subnet))) / math.log10(2))
  292. route_add(address.."/"..prefix, IPV4_BROADCAST, interface)
  293. route_add("0.0.0.0/0", gateway, interface)
  294. end
  295.  
  296. function ipv4_send (destination, protocol, ttl, payload)
  297. dest_binary = ip_to_binary(destination)
  298.  
  299. route_choice = { netmask = -1 }
  300. for cidr, route in pairs(ipv4_routes) do
  301. if bit.band(dest_binary, route.netmask) == route.network and route.netmask > route_choice.netmask then
  302. route_choice = route
  303. end
  304.  
  305. end
  306.  
  307. interface = route_choice.interface
  308. hostdestination = route_choice.route
  309. --print("hostdestination: "..tostring(hostdestination))
  310. if hostdestination == IPV4_BROADCAST then hostdestination = destination end
  311.  
  312. if arp_cache[hostdestination] == nil then
  313. local data = {interface = interface, destination = destination, port=port, protocol = protocol, ttl = ttl, payload = payload}
  314.  
  315. arp_request(interface, destination)
  316. if ipv4_packet_queue == nil then ipv4_packet_queue = {} end
  317. table.insert(ipv4_packet_queue, textutils.serialize(data))
  318.  
  319. return
  320. end
  321.  
  322. local packet = IPV4_TEMPLATE
  323. packet.source = ipv4_interfaces[interface]
  324. packet.destination = destination
  325. packet.protocol = protocol
  326. packet.ttl = ttl
  327. packet.payload = payload
  328. packet.port = port
  329.  
  330. ethernet_send(interface, arp_cache[hostdestination], ETHERNET_TYPE_IPV4, packet)
  331. end
  332.  
  333. function ipv4_process_queue ()
  334. --print("Packet queue: "..textutils.serialize(ipv4_packet_queue))
  335. if ipv4_packet_queue ~= nil then
  336. for i, queue_packet in ipairs(ipv4_packet_queue) do
  337. local data = textutils.unserialize(queue_packet)
  338. ipv4_send(data.destination, data.port, data.protocol, data.ttl, data.payload)
  339. if arp_cache[data.destination] ~= nil then
  340. table.remove(ipv4_packet_queue, i)
  341. end
  342. end
  343. end
  344. end
  345.  
  346. function ipv4_event(interface, raw, ipv4)
  347. raw.payload.ttl = raw.payload.ttl - 1
  348. ipv4 = raw.payload
  349.  
  350. if raw.payload.ttl == 0 then
  351. icmp_send(raw.payload.source, IPV4_DEFAULT_TTL, ICMP_TYPE_TIME_EXCEEDED, 0, "")
  352. return
  353. end
  354.  
  355. arp_cache[ipv4.source] = raw.source
  356.  
  357. if ipv4.protocol == 1 then
  358. print("got ICMP")
  359. icmp_event(interface, raw, ipv4.payload)
  360. end
  361. end
  362.  
  363. function ip_to_binary(address)
  364. a, b, c, d = string.match(address, "(%d+).(%d+).(%d+).(%d+)")
  365. return (bit.blshift(tonumber(a), 24))
  366. + (bit.blshift(tonumber(b), 16))
  367. + (bit.blshift(tonumber(c), 8))
  368. + tonumber(d)
  369. end
  370.  
  371. function route_add(cidr, route, interface)
  372. network, prefix = string.match(cidr, "(.+)/(%d+)")
  373.  
  374. --print(tostring(prefix))
  375.  
  376. netmask = bit.bxor(math.pow(2, 32 - tonumber(prefix)) - 1, math.pow(2, 32) - 1)
  377. network_short = bit.band(ip_to_binary(network), netmask)
  378.  
  379. ipv4_routes[cidr] = {
  380. route = route,
  381. interface = interface,
  382. network = network_short,
  383. netmask = netmask
  384. }
  385. end
  386.  
  387. function route_remove(cidr)
  388. ipv4_routes[cidr] = nil
  389. end
  390.  
  391. -- ICMP
  392. function icmp_send(destination, ttl, type, code, payload)
  393. local packet = ICMP_TEMPLATE
  394. packet.type = type
  395. packet.code = code
  396. packet.payload = payload
  397.  
  398. ipv4_send(destination, IPV4_PROTOCOL_ICMP, ttl, packet)
  399. end
  400.  
  401. function icmp_ping_ttl(destination, ttl)
  402. local packet = ICMP_TEMPLATE_ECHO
  403. packet.identifier = icmp_echo_identifier
  404. packet.sequence = icmp_echo_sequence
  405. packet.payload = os.clock()
  406.  
  407. icmp_send(destination, ttl, ICMP_TYPE_ECHO, 0, packet)
  408.  
  409. icmp_echo_sequence = icmp_echo_sequence + 1
  410.  
  411. end
  412.  
  413. function icmp_ping(interface, destination)
  414. icmp_ping_ttl(interface, destination, IPV4_DEfAULT_TTL)
  415. end
  416.  
  417. function icmp_event(interface, raw, icmp)
  418. if icmp.type == ICMP_TYPE_ECHO then
  419. local packet = ICMP_TEMPLATE_ECHO
  420. packet.identifier = icmp.payload.identifier
  421. packet.sequence = icmp.payload.sequence
  422. packet.payload = icmp.payload.payload
  423.  
  424. icmp_send(raw.payload.source, raw.payload.ttl, ICMP_TYPE_ECHO_REPLY, 0, packet)
  425. end
  426. end
  427.  
  428. -- TCP
  429.  
  430. function tcp_find_open_port()
  431. for i = 8000 to 65535 do
  432. if sockets[i] == nil then
  433. return i
  434. end
  435. end
  436. end
  437.  
  438. function tcp_create_packet(port, payload)
  439. local packet = TCP_TEMPLATE
  440. packet.remote_port = sockets[port].remote_port
  441. packet.lo
  442. packet.seq = sockets[port].seq
  443. packet.payload = payload
  444. return packet
  445. end
  446.  
  447. function socket_create(remote_address, remote_port, local_port, protocol, listener)
  448. -- first, is the local port available to create a socket on
  449. if sockets[local_port] == nil then
  450. -- it is, create the socket
  451. local socket = SOCKET_TEMPLATE
  452. socket.remote_address = remote_address
  453. socket.remote_port = remote_port
  454. socket.local_port = local_port
  455. socket.protocol = IPV4_PROTOCOL_TCP
  456. socket.seq = math.random(65535)
  457. if listener then
  458. socket.state = TCP_STATE_LISTEN
  459. end
  460. sockets[local_port] = socket
  461. return local_port
  462. end
  463. end
  464.  
  465. function socket_open(dest, port)
  466. socket_create(dest, port, port, IPV4_PROTOCOL_TCP, false)
  467. -- begin the TCP handshake
  468. local packet = tcp_create_packet(port)
  469. packet.syn_flag = true
  470. ipv4_send(dest, IPV4_PROTOCOL_TCP, IPV4_DEFAULT_TTL, packet)
  471. end
  472.  
  473. function socket_event(interface, raw, tcp)
  474. if sockets[tcp.port].state == TCP_STATE_LISTEN then
  475. -- any flags?
  476. -- the only flag valid in this state is SYN, ignore otherwise
  477. if tcp.syn_flag then
  478. -- client is trying to connect
  479. -- determine what port to use, create a socket
  480. local local_port = socket_create(raw.payload.source, raw.payload.source_port, tcp_find_open_port(), IPV4_PROTOCOL_TCP, false)
  481.  
  482. -- got a socket, create the SYN/ACK packet
  483. local my_packet = tcp_create_packet(local_port)
  484. my_packet.syn_flag = true
  485. my_packet.ack_flag = true
  486. ipv4_send(raw.payload.source, IPV4_PROTOCOL_TCP, IPV4_DEFAULT_TTL, my_packet)
  487.  
  488. --Change the socket state to SYN_RECEIVED
  489. sockets[local_port].state = TCP_STATE_SYN_RECEIVED
  490. end
  491. -- nothing else to do for this one
  492. elseif sockets[tcp.remote_port].state == TCP_STATE_SYN_SEND then
  493. -- This is client side, the only thing that the client can receive at this point is a SYN/ACK
  494. if tcp.syn_flag and tcp.ack_flag then
  495. if tcp.local_port ~= tcp.remote_port then
  496. sockets[tcp.remote_port].remote_port = tcp.local_port
  497. -- This sets the remote_port on the client to be the local_port sent by the server.
  498. -- Now create an ACK packet to complete the handshake
  499. local my_packet = tcp_create_packet(tcp.remote_port)
  500. my_packet.ack_flag = true
  501. ipv4_send(raw.payload.source, IPV4_PROTOCOL, IPV4_DEFAULT_TTL, my_packet)
  502.  
  503. -- Change this socket state to ESTABLISHED
  504. sockets[tcp.remote_port].state = TCP_STATE_ESTABLISHED
  505. end
  506. elseif sockets[tcp.port].state == TCP_STATE_SYN_RECEIVED then
  507. -- This is server side, SYN/ACK has been sent, now we await the ACK
  508. if tcp.ack_flag then
  509. -- all good, set the socket to ESTABLISHED
  510. sockets[tcp.remote_port].state = TCP_STATE_ESTABLISHED
  511. end
  512. elseif sockets[tcp.port].state == TCP_STATE_ESTABLISHED then
  513. -- queue the event to be handled by the OS.
  514. os.queueEvent("socket_received", raw)
  515. elseif sockets[tcp.port].state == TCP_STATE_FIN_WAIT_1 then
  516. -- This state is reached when we have sent FIN. The only possibility is to receive FIN/ACK
  517. if tcp.fin_flag and tcp.ack_flag then
  518. -- send back an ACK
  519. local my_packet = tcp_create_packet(tcp.remote_port)
  520. my_packet.ack_flag = true
  521. ipv4_send(raw.payload.source, IPV4_PROTOCOL, IPV4_DEFAULT_TTL, my_packet)
  522.  
  523. -- set the port to TIME_WAIT
  524. sockets[tcp.remote_port].state = TCP_STATE_TIME_WAIT
  525. parallel.waitForAny(function()
  526. os.sleep(2)
  527. socket_close(tcp.remote_port)
  528. end)
  529. end
  530.  
  531. elseif sockets[tcp.port].state == TCP_STATE_CLOSE_WAIT then
  532. -- We have received a FIN message, send FIN/ACK
  533. local my_packet = tcp_create_packet(tcp.remote_port)
  534. my_packet.ack_flag = true
  535. ipv4_send(raw.payload.source, IPV4_PROTOCOL, IPV4_DEFAULT_TTL, my_packet)
  536.  
  537. -- Change socket state to LAST_ACK
  538. sockets[tcp.remote_port].state = TCP_STATE_LAST_ACK
  539.  
  540. elseif sockets[tcp.port].state == TCP_STATE_LAST_ACK then
  541. -- we are awaiting the final ACK from the remote
  542. if tcp.ack_flag then
  543. -- Close the socket
  544. socket_close(tcp.remote_port)
  545. end
  546. elseif sockets[tcp.port].state == TCP_STATE_TIME_WAIT then
  547.  
  548. elseif sockets[tcp.port].state == TCP_STATE_CLOSED then
  549. -- Socket is CLOSED
  550. -- send the RST packet
  551. local my_packet = tcp
  552. local my_packet.rst_flag = true
  553. ipv4_send(tcp.source, IPV4_PROTOCOL_TCP, IPV4_DEFAULT_TTL, my_packet)
  554. end
  555. end
  556.  
  557. function socket_send(remote_port, payload)
  558. if sockets[remote_port].state == TCP_STATE_ESTABLISHED then
  559. local my_packet = tcp_create_packet(remote_port, payload)
  560. ipv4_send(my_packet[destination], IPV4_PROTOCOL_TCP, IPV4_DEFAULT_TTL, my_packet)
  561. end
  562. end
  563.  
  564. function socket_close(p)
  565. sockets[p] = SOCKET_TEMPLATE
  566. end
  567.  
  568. function create_sockets()
  569. for i = 1 to 65535 do
  570. sockets[i] = SOCKET_TEMPLATE
  571. end
  572. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement