Subnets?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Subnets?

Post by pcmattman »

Does anyone know how I can figure out, if all I have is a subnet mask and an IP address, if the IP address is a part of a particular subnet?

I need to figure this out because my ethernet routines need to resolve IP addresses to hostnames and if the IP to send to isn't on the same subnet as the computer to send to, I need to send the ethernet packet to the gateway.
User avatar
Kevin McGuire
Member
Member
Posts: 843
Joined: Tue Nov 09, 2004 12:00 am
Location: United States
Contact:

Post by Kevin McGuire »

Code: Select all

u32 NETWORK, SUBNETMASK, HOST, IP;
NETWORK = IP & SUBNETMASK
HOST = IP & (~SUBNETMASK)
BROADCAST = NETWORK | (~SUBNETMASK);
11111111.11111111.11111111.00000000 = 255.255.255.0 (NETMASK)
11011101.10101111.10110111.11011111 = a.b.c.d (IP)
00000000.00000000.00000000.11111111 = 0.0.0.255 (HOST)
Last edited by Kevin McGuire on Sat Apr 21, 2007 10:42 pm, edited 1 time in total.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

Thankyou, that's what I needed.
User avatar
Kevin McGuire
Member
Member
Posts: 843
Joined: Tue Nov 09, 2004 12:00 am
Location: United States
Contact:

Post by Kevin McGuire »

Do not forget about throwing together a DNS hack to get some basic support while you are working on it. You can just about ignore all the complicated stuff when doing it for the first time.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

Well, now I can ping Google's servers (by using the IP) and any other computer because the ethernet layer handles physical addresses.

DNS is next thing on my list to implement, then continue on my TCP code (and probably UDP too).

Edit: wait... I'm going to have to implement UDP first. :evil:
Post Reply