Page 1 of 1

Network Stack

Posted: Sun Jan 13, 2013 3:58 pm
by oreo
Hi
Can someone briefly explain how they have implemented their network stack. I tend to explain my self better by writing code, so here’s my idea in pseudo code.

Code: Select all

struct nic_info
{
	char[6] macAddr = {DE, AD, BE, EF, C0, FE}
	char[4] ipAddr = {192.168.1.100};
	char[4] defaultGateway = {192,168,1,254}; 
	char[4] dnsServer = {8,8,8,8};
	int ioaddr;
	int pciBus;
	int pciDevice;
	int IRQ;
	char [10] niceName = "eth0"; 
};

struct nic_info networkInterfaces[5];

void main()
{
//Kernel init stuff

	
	int nicCardCound = 0;
	
	// Scan PCI bus for supported nic cards

	for(bus = 0; bus > 256; bus++)
	{
		for(device = 0; device > 256; device++)
		{
			U16int vendorID = PCI_ConfigReadWord(bus, device, 0, 0);
			U16int deviceID = PCI_ConfigReadWord(bus, device, 0, 2);
			if(nicCardCound > 5)
				break;
				
			if(deviceID == 0x8139)
			{
				networkInterfaces[nicCardCound] = Install_RTL8139(bus, device);	// Deals with getting ipAddr (DHCP), defaultGatway, dnsServer and loading correct macAddr, and ioaddr. This is really going to slow down boot, unless we implement some form of multitasking. 
				nicCardCound++;
			}
			else if(vendorID == foo && deviceID == bar)
			{
				networkInterfaces[nicCardCound] = Install_FooBar(bus, device);
				nicCardCound++;
			}			
		}
	}
	
	
	char[1024] buffer;
	downloadHTTP("http://www.example.com", &buffer, networkInterfaces[0]);
}

void downloadHTTP(char[1024] url, char buffer, struct nic_info interface)
{
	char[1024] packet;
	char[4] urlIP;
	
	//Lookup url with DNS
	urlIP = DNS_getIPAddr(url);
	
	generateEth(&packet);
	generateIP(&packet);
//Build up and send packet as we have done below with DNS
// Await response and return
}

char DNS_getIpAddr(char[1024] url, struct nic_info interface)
{
	char[1024] packet;
	
	generateEth(&packet);
	generateIP(&packet);
	generateUDP(&packet);
				
	packet += { 0xe7, 0xc1 };	// Transaction ID
	packet += { 0x01, 0x00 };	// Flags (Standard Query)
	//... more DNS feilds
	packet += url;
	packet += { 0x00, 0x01 };	// A Record
	packet += { 0x00, 0x01 };	// Class (IN)
	
	SendPacket(interface, packet);
	
	while(getPacket(interface))
	{
		if(packet[type] == dns && packet[transactionID] == 0xe7c1)
			// Analyse packet
			// Return IP addr
	}
}

Re: Network Stack

Posted: Sun Jan 13, 2013 10:34 pm
by trinopoty
A full network stack is more complex than that.
You see; a network stack may contain multiple protocol drivers (TCP, UDP, etc.).
DNS and HTTP are services on top of the protocol. DNS uses UDP. HTTP uses TCP. DHCP is also a service that uses UDP.
A NIC driver receives data on the card and sends it to some service that iterates through all the available protocol and sends them the received data. The protocol driver then gets to decide whether it will accept the data or not.

Something like this should work:

Code: Select all


 +-----------+      +-----------+
 |   NIC1    |      |   NIC2    |
 +-----------+      +-----------+
           \            /
            \          /
         +-----------------+
         | Network Service |
         +-----------------+
              /        \
             /          \
 +-----------+      +-----------+
 | Protocol1 |      | Protocol2 |
 +-----------+      +-----------+
       |                  |
 +-------------+    +--------------+
 | Application |    | Application2 |
 +-------------+    +--------------+

Regards,
Trinopoty