119 posts
Location
Adelaide, Australia
Posted 23 August 2014 - 08:09 AM
DNS! v1.0I present to you, DNS for computercraft.
Probably nothing new - there's probably been squillions of these made already, but hey, whatever. I was bored and made one anyway.
However, this DNS system
*does* have something special about it; it hooks into the Rednet API, giving you seamless DNS integration, and no extra APIs to learn (unless you want to learn the DNS API, but it isn't required). It also is perfectly compatible with existing programs that don't use this DNS system.
Also, if you don't want DNS to be integrated into rednet.send, that's fine, just don't download the DNS hook.
Functions:Spoiler
rednet.send(hostname, msg, protocol)
[indent=1]This DNS system hooks right into the Rednet API. You can pass a hostname string right into the rednet function.[/indent]
[indent=1]If you don't want to use a hostname, but send a message to an ID, that's fine too, just pass an integer in instead.[/indent]
[indent=1]Returns true if the message was successfully sent, false if not.[/indent]
dns.scan()
[indent=1]This will search the network for DNS servers, if one is found it'll return it's ID, if not, it'll return nil.[/indent]
[indent=1]Typically this function won't be needed. It's mainly used internally to get the ID of the DNS server to resolve the hostname.[/indent]
dns.lookup(hostname)
[indent=1]This will connect to a DNS server, and resolve the hostname to an ID.[/indent]
[indent=1]If you pass a hostname into rednet.send, this will be done internally.[/indent]
rawSend(id, msg, protocol)
[indent=1]The original rednet function. This bypasses the DNS integration.[/indent]
[indent=1]Really there isn't any use for this, it's mainly just used internally to preform the actual DNS lookups.[/indent] Programs and downloads:Spoiler
This DNS system consists of a few programs - not all are required.
DNS API. *required
[indent=1]This is the API that powers all the programs and adds the dns.* functions.[/indent]
[indent=1]This is *not* what adds the rednet.send integration.[/indent]
[indent=1]This must be downloaded as "dns"[/indent]
[indent=1]
pastebin get ASC4wGhU dns
[/indent]
Root Server. *required
[indent=1]This is the program that must be installed and ran on one (no more, no less) computer on the network.[/indent]
[indent=1]It is what stores the DNS records and responds to DNS lookups.[/indent]
[indent=1]
pastebin get mkcQ88AP rootdns
[/indent]
[indent=1]It's a good idea to either rename this file to startup or create a script to run it on startup as the program won't start serving requests until it is started.[/indent]
DNS Hook. *recommended
[indent=1]This is what adds the rednet.send integration. Without it this DNS system is pretty much useless unless you have programs that support DNS through the DNS API.[/indent]
[indent=1]
pastebin get DWyLsEFc dnshook
[/indent]
[indent=1]It's a good idea to either rename this file to startup or create a script to run it on startup as it won't override rednet.send until it has been run.[/indent]
DNS Lookup Tool. *optional
[indent=1]This is an optional command-line utility. It lets you enter a hostname and gives you the resulting ID.[/indent]
[indent=1]It's much like the nslookup tool on Windows.[/indent]
[indent=1]
pastebin get eAynbSQD nslookup
[/indent] How to setup the DNS root server:Spoiler
- Install the root DNS server program on the computer you intend to be the server. The pastebin command will be found in the programs and downloads section.
- (Recommended) Either rename the server to startup, or create a startup script that will start this program on startup.
- Connect a modem (be it wireless or wired) to the server.
- Create a directory called data. Do this by running mkdir data
- Now create a file in it called "zones". Do this by running edit data/zones
- Type a hostname (eg. test.com), then a type a colon, then a computer ID that is on your network.
- You may repeat the above step, with each entry on it's own line.
- Start the program.
Currently the only way to add/remove domains is to stop the program and modify the zones file. This will be changed in the near future.
The program will now serve DNS requests. Edit: This thing keeps killing my formatting.. >.>
Thanks, Mitchfizz05.
Edited on 23 August 2014 - 07:13 AM
8543 posts
Posted 23 August 2014 - 08:15 AM
Can this interact with the built-in rednet hostname support in CC 1.6+?
119 posts
Location
Adelaide, Australia
Posted 23 August 2014 - 08:44 AM
Can this interact with the built-in rednet hostname support in CC 1.6+?
It should be able to, yes. I'll double check now to make sure though.
Edit: Darn. Apparently not. I'm not sure why though. I'll try to fix it now. Just when I thought my program was flawless. D:
Edit 2: Now fixed. It was a spelling mistake. :P/>
Edited on 23 August 2014 - 06:57 AM
163 posts
Location
Pennsylvania
Posted 23 August 2014 - 09:15 AM
Does this cache the IDs retrieved from the DNS, or does it do a lookup on every request?
119 posts
Location
Adelaide, Australia
Posted 23 August 2014 - 09:28 AM
Does this cache the IDs retrieved from the DNS, or does it do a lookup on every request?
It does a lookup on every request.
I was originally planning on having a cache, but then decided against it because Rednet is instant.
I
might still add a cache in the future.
2151 posts
Location
Auckland, New Zealand
Posted 23 August 2014 - 01:00 PM
This is quite a neat idea, and by the looks of it quite a nice implementation with existing APIs too.
7083 posts
Location
Tasmania (AU)
Posted 23 August 2014 - 02:28 PM
A problem with caching is that you'd still need to check every now and then in order to make sure nothing's changed and the cache is still accurate. Given that I wouldn't expect a given script to "need" to perform lookups very often anyway, I think you'd be better off doing a proper check every time one does.
119 posts
Location
Adelaide, Australia
Posted 23 August 2014 - 04:59 PM
A problem with caching is that you'd still need to check every now and then in order to make sure nothing's changed and the cache is still accurate. Given that I wouldn't expect a given script to "need" to perform lookups very often anyway, I think you'd be better off doing a proper check every time one does.
If I did have a cache, I could just have all cached IDs expire after a set amount of time (probably pretty short), like real DNS. Then once the cached value expires, I fetch the new value.
But yeah, I agree - I'd probably be better off doing a proper check every time.
Although, a very short term cache (like 30 seconds) might be useful for some programs such as chat programs. If you were having a conversation it might become very inefficient to do another DNS lookup for every message sent.
7083 posts
Location
Tasmania (AU)
Posted 24 August 2014 - 02:58 PM
The idea is that you simply allow the scripter calling your API to handle such caching within their own script. The IDs are unlikely to change mid-conversation, for example, but it should be up to the users of your API to know that - as opposed to the API itself.
Edited on 25 August 2014 - 12:20 AM
163 posts
Location
Pennsylvania
Posted 25 August 2014 - 12:07 AM
The idea is that you simply allow the scripter calling your API to handle such caching within their own script. The IDs are likely to change mid-conversation, for example, but it should be up to the users of your API to know that - as opposed to the API itself.
But to do that, you'd need to have a way to A) flush the cache manually, and B ) disable DNS lookups as needed, which, in every scenario I can imagine, requires adding arguments to rednet functions.
Also, is rawSend supposed to be dns.rawSend, or is it a global?
Edited on 24 August 2014 - 10:08 PM
7083 posts
Location
Tasmania (AU)
Posted 25 August 2014 - 02:24 AM
But to do that, you'd need to have a way to A) flush the cache manually, and B ) disable DNS lookups as needed, which, in every scenario I can imagine, requires adding arguments to rednet functions.
You're overthinking things, maybe.
As a scripter using the API, to achieve b), you simply perform a DNS lookup when your script starts and store the result in a variable. From then on you refer to that variable and ignore the new version of rednet.send which accepts hostnames - when
you decide it's time to double check the ID is still valid (which I imagine you never would, but let's just say you did), you simply ask the API to do another lookup for you and overwrite your variable with the result.
Edited on 25 August 2014 - 12:25 AM
163 posts
Location
Pennsylvania
Posted 27 August 2014 - 12:08 PM
-snip-
You're overthinking things, maybe.
As a scripter using the API, to achieve B)/>, you simply perform a DNS lookup when your script starts and store the result in a variable. From then on you refer to that variable and ignore the new version of rednet.send which accepts hostnames - when
you decide it's time to double check the ID is still valid (which I imagine you never would, but let's just say you did), you simply ask the API to do another lookup for you and overwrite your variable with the result.
But at that point, there's no need for the rednet hook, since you'd be using the DNS API directly. I'm trying to figure out a way to include it in rednet transparently.