BarryServer : Git

All the code for all my projects
// BarryServer : Git / Orion / commit / d41a53cbc7d055b1c00cf0a339dbed6925f4f02c / net / net.c

// Related

Orion

Barry Importing existing Orion kernel d41a53c (2 years, 4 months ago)
diff --git a/net/net.c b/net/net.c
new file mode 100644
index 0000000..5c963dc
--- /dev/null
+++ b/net/net.c
@@ -0,0 +1,90 @@
+/*
+ * This file represents the core of the Kernel Networking System.  It contains
+ * the outwards facing interface, as well as the internal interface for the
+ * lower levels to make use of.
+ */
+
+#include <stdint.h>
+#include <string.h>
+#include "net.h"
+#include "../mem/heap.h"
+
+NetIF *netifs;
+
+/* Switch the endian of integers */
+static uint16_t
+switch_endian16(uint16_t nb)
+{
+	return (nb>>8) | (nb<<8);
+}
+static uint32_t
+switch_endian32(uint32_t nb)
+{
+	return ((nb>>24)&0xff)      |
+	       ((nb<<8)&0xff0000)   |
+	       ((nb>>8)&0xff00)     |
+	       ((nb<<24)&0xff000000);
+}
+
+/* Host to Network byte order */
+uint16_t
+htons(uint16_t hostshort)
+{
+	return switch_endian16(hostshort);
+}
+uint32_t
+htonl(uint32_t hostlong)
+{
+	return switch_endian32(hostlong);
+}
+
+/* Network to Host byte order */
+uint16_t
+ntohs(uint16_t netshort)
+{
+	return switch_endian16(netshort);
+}
+uint32_t
+ntohl(uint32_t netlong)
+{
+	return switch_endian32(netlong);
+}
+
+/* Intialise Networking */
+void
+init_net(void)
+{
+	char ip[] = {10,0,2,2};
+	memcpy(netifs->gatewayIp, ip, 4);
+	arp_request(netifs, ip, netifs->gatewayMac);
+
+	/* Send ICMP ECHO */
+	char icmp[12] = {
+		8, 0,
+		0xF7, 0xFE,
+		0x00, 0x01,
+		0, 0
+	};
+	char pip[] = {1,1,1,1};
+	ipv4_send_packet(netifs, pip, 1, 0, icmp, 12);
+}
+
+/* Register a Network Driver */
+NetIF *
+net_create_interface(uint16_t type, void (*transmit)(void *, size_t),
+                     void (*get_mac)(char *buf))
+{
+	NetIF *netif = kmalloc(sizeof(NetIF));
+	netif->type = type;
+	netif->transmit = transmit;
+	netif->get_mac = get_mac;
+	netif->get_mac(netif->mac);
+	netif->ip[0] = 10;
+	netif->ip[1] =  0;
+	netif->ip[2] =  2;
+	netif->ip[3] = 15;
+
+	/* Link into list */
+	netif->next = netifs;
+	netifs = netif;
+}