git-svn-id: https://spexeah.com:8443/svn/Asuro@484 6dbc8c32-bb84-406f-8558-d1cf31a0ab0c

This commit is contained in:
kieron
2018-04-11 14:21:06 +00:00
parent 3bed07c80a
commit c8b3b14893
43 changed files with 340 additions and 108 deletions

View File

@ -3,7 +3,7 @@ unit netutils;
interface
uses
util, nettypes, console, lmemorymanager;
tracer, util, nettypes, console, lmemorymanager;
procedure copyMAC(src : puint8; dst : puint8);
procedure copyIPv4(src : puint8; dst : puint8);
@ -21,13 +21,15 @@ var
i : uint8;
begin
push_trace('netutils.IPEqual');
IPEqual:= true;
for i:=0 to 3 do begin
if ip1[i] <> ip2[i] then begin
IPEqual:= false;
exit;
break;
end;
end;
pop_trace;
end;
function MACEqual(mac1 : puint8; mac2 : puint8) : boolean;
@ -35,13 +37,15 @@ var
i : uint8;
begin
push_trace('netutils.MACEqual');
MACEqual:= true;
for i:=0 to 5 do begin
if mac1[i] <> mac2[i] then begin
MACEqual:= false;
exit;
break;
end;
end;
pop_trace;
end;
procedure writeIPv4Address(ip : puint8);
@ -49,12 +53,14 @@ var
i : integer;
begin
push_trace('netutils.writeIPv4Address');
console.writeint(ip[0]);
for i:=1 to 3 do begin
console.writestring('.');
console.writeint(ip[i]);
end;
console.writestringln(' ');
pop_trace;
end;
procedure writeMACAddress(mac : puint8);
@ -62,23 +68,29 @@ var
i : integer;
begin
push_trace('netutils.writeMACAddress');
console.writehexpair(mac[0]);
for i:=1 to 5 do begin
console.writestring(':');
console.writehexpair(mac[i]);
end;
console.writestringln(' ');
pop_trace;
end;
function newPacketContext : PPacketContext;
begin
push_trace('netutils.newPacketContext');
newPacketContext:= PPacketContext(kalloc(sizeof(TPacketContext)));
memset(uint32(newPacketContext), 0, sizeof(TPacketContext));
pop_trace;
end;
procedure freePacketContext(p_context : PPacketContext);
begin
push_trace('netutils.freePacketContext');
kfree(void(p_context));
pop_trace;
end;
procedure copyMAC(src : puint8; dst : puint8);
@ -86,9 +98,11 @@ var
i : uint8;
begin
push_trace('netutils.copyMAC');
for i:=0 to 5 do begin
dst[i]:= src[i];
end;
pop_trace;
end;
procedure copyIPv4(src : puint8; dst : puint8);
@ -96,9 +110,11 @@ var
i : uint8;
begin
push_trace('netutils.copyIPv4');
for i:=0 to 3 do begin
dst[i]:= src[i];
end;
pop_trace;
end;
end.

View File

@ -3,6 +3,7 @@ unit net;
interface
uses
tracer,
console,
nettypes, netutils;
@ -25,22 +26,28 @@ var
procedure registerNetworkCard(SendCallback : TNetSendCallback; _MAC : puint8);
begin
push_trace('net.registerNetworkCard');
if CBSend = nil then begin
CBSend:= SendCallback;
MAC:= _MAC;
end;
pop_trace;
end;
procedure registerNextLayer(RecvCallback : TRecvCallback);
begin
push_trace('net.registerNextLayer');
if CBNext = nil then begin
CBNext:= RecvCallback;
end;
pop_trace;
end;
procedure send(p_data : void; p_len : uint16);
begin
push_trace('net.send');
if CBSend <> nil then CBSend(p_data, p_len);
pop_trace;
end;
procedure recv(p_data : void; p_len : uint16);
@ -48,22 +55,28 @@ var
context : PPacketContext;
begin
push_trace('net.recv');
//console.outputln('net', 'RECV.');
context:= newPacketContext;
if CBNext <> nil then CBNext(p_data, p_len, context);
freePacketContext(context);
pop_trace;
end;
function getMAC : puint8;
begin
push_trace('net.getMAC');
getMAC:= MAC;
pop_trace;
end;
procedure init;
begin
push_trace('net.init');
eth2.register;
arp.register;
ipv4.register;
pop_trace;
end;
end.

View File

@ -3,6 +3,7 @@ unit eth2;
interface
uses
tracer,
nettypes, netutils,
net,
console;
@ -19,8 +20,10 @@ var
procedure registerType(eType : uint16; RecvCB : TRecvCallback);
begin
push_trace('eth2.registerType');
register;
if EthTypes[eType] = nil then EthTypes[eType]:= RecvCB;
pop_trace;
end;
procedure recv(p_data : void; p_len : uint16; p_context : PPacketContext);
@ -30,6 +33,7 @@ var
buf : puint8;
begin
push_trace('eth2.recv');
//console.outputln('net.eth2', 'RECV.');
buf:= puint8(p_data);
@ -56,6 +60,7 @@ begin
EthTypes[proto_type](void(buf), p_len - 14, p_context);
end;
end;
pop_trace;
end;
procedure register;
@ -63,6 +68,7 @@ var
i : uint16;
begin
push_trace('eth2.register');
if not Registered then begin
for i:=0 to 65535 do begin
EthTypes[i]:= nil;
@ -71,6 +77,7 @@ begin
MAC:= net.getMAC;
Registered:= true;
end;
pop_trace;
end;
end.

View File

@ -3,6 +3,7 @@ unit arp;
interface
uses
tracer,
util, lists, console,
nettypes, netutils,
eth2;
@ -30,14 +31,16 @@ var
r : PARPCacheRecord;
begin
push_trace('arp.findCacheRecordByMAC');
findCacheRecordByMAC:= nil;
for i:=0 to LL_Size(Cache)-1 do begin
r:= PARPCacheRecord(LL_Get(Cache, i));
if MACEqual(mac, @r^.MAC[0]) then begin
findCacheRecordByMAC:= r;
exit;
break;
end;
end;
pop_trace;
end;
function findCacheRecordByIP(ip : puint8) : PARPCacheRecord;
@ -46,14 +49,16 @@ var
r : PARPCacheRecord;
begin
push_trace('arp.findCacheRecordByIP');
findCacheRecordByIP:= nil;
for i:=0 to LL_Size(Cache)-1 do begin
r:= PARPCacheRecord(LL_Get(Cache, i));
if IPEqual(ip, @r^.IP[0]) then begin
findCacheRecordByIP:= r;
exit;
break;
end;
end;
pop_trace;
end;
procedure recv(p_data : void; p_len : uint16; p_context : PPacketContext);
@ -63,6 +68,7 @@ var
CacheElement : PARPCacheRecord;
begin
push_trace('arp.recv');
{ Get our converted Header }
Header:= PARPHeader(p_data);
AHeader.Hardware_Type:= (Header^.Hardware_Type_Hi SHL 8) + Header^.Hardware_Type_Lo;
@ -104,15 +110,18 @@ begin
end;
end;
pop_trace;
end;
procedure register;
begin
push_trace('arp.register');
if not Registered then begin
Cache:= LL_New(sizeof(TARPCacheRecord));
eth2.registerType($0806, @recv);
Registered:= true;
end;
pop_trace;
end;
function IPv4ToMAC(ip : puint8) : puint8;
@ -120,12 +129,14 @@ var
r : PARPCacheRecord;
begin
push_trace('arp.IPv4ToMAC');
register;
IPv4ToMAC:= nil;
r:= findCacheRecordByIP(ip);
if r <> nil then begin
IPv4ToMAC:= @r^.MAC[0];
end;
pop_trace;
end;
function MACToIIPv4(mac : puint8) : puint8;
@ -133,12 +144,14 @@ var
r : PARPCacheRecord;
begin
push_trace('arp.MACToIPv4');
register;
MACToIIPv4:= nil;
r:= findCacheRecordByMAC(mac);
if r <> nil then begin
MACToIIPv4:= @r^.IP[0];
end;
pop_trace;
end;
end.

View File

@ -3,6 +3,7 @@ unit ipv4;
interface
uses
tracer,
util, console, terminal,
net, nettypes, netutils,
eth2;
@ -26,6 +27,7 @@ var
len : uint16;
begin
push_trace('ipv4.recv');
//console.outputln('net.ipv4', 'RECV.');
Header:= PIPV4Header(p_data);
AHeader.version:= Header^.version;
@ -61,12 +63,13 @@ begin
if (IPEqual(@Config.Address[0], @AHeader.Dst[0])) OR (AHeader.Dst[3] = 255) then begin
if Protocols[AHeader.Protocol] <> nil then Protocols[AHeader.Protocol](void(buf), len, p_context);
end;
pop_trace;
end;
procedure terminal_command_ifconfig(params : PParamList);
begin
push_trace('ipv4.terminal_command_ifconfig');
if paramCount(params) > 2 then begin
end else begin
writestring(' MAC: ');
writeMACAddress(net.GetMAC);
@ -81,6 +84,7 @@ begin
else
writestringln(' NetUP: false');
end;
pop_trace;
end;
procedure register;
@ -88,6 +92,7 @@ var
i : uint8;
begin
push_trace('ipv4.register');
if not Registered then begin
for i:=0 to 255 do begin
Protocols[i]:= nil;
@ -102,12 +107,15 @@ begin
terminal.registerCommand('IFCONFIG', @terminal_command_ifconfig, 'Configure Network Settings.');
Registered:= true;
end;
pop_trace;
end;
procedure registerProtocol(Protocol_ID : uint8; recv_callback : TRecvCallback);
begin
push_trace('ipv4.registerProtocol');
register;
if Protocols[Protocol_ID] = nil then Protocols[Protocol_ID]:= recv_callback;
pop_trace;
end;
end.