diff --git a/src/driver/net/include/nettypes.pas b/src/driver/net/include/nettypes.pas index 63fe89f9..48dbbb66 100644 --- a/src/driver/net/include/nettypes.pas +++ b/src/driver/net/include/nettypes.pas @@ -189,6 +189,132 @@ type end; PUDPPseudoHeader = ^TUDPPseudoHeader; + { DHCP } + TDHCPHeader = packed record + Message_Type : uint8; + Hardware_Type : uint8; + Hardware_Address_Length : uint8; + Hops : uint8; + Transaction_ID : uint32; + Seconds_Elapsed : uint16; + Bootp_Flags : uint16; + Client_IP : TIPv4Address; + Your_IP : TIPv4Address; + Server_IP : TIPV4Header; + Relay_Agent_IP : TIPV4Header; + Client_MAC : TMACAddress; + Padding : Array[0..9] of uint8; + Server_Hostname : Array[0..63] of uint8; + Boot_File : Array[0..127] of uint8; + Magic_Cookie : Array[0..3] of uint8; + Option_start : char; + end; + PDHCPHeader = ^TDHCPHeader; + TDHCPOpCode = ( + //BootTP Vendor Information Extensions + PAD:= 0, + SUBNET_MASK:= 1, + TIME_OFFSET:= 2, + ROUTER:= 3, + TIME_SERVER:= 4, + NAME_SERVER:= 5, + DNS_SERVER:= 6, + LOG_SERVER:= 7, + COOKIE_SERVER:= 8, + LPR_SERVER:= 9, + IMPRESS_SERVER:= 10, + RESOURCE_LOCATION_SERVER:= 11, + HOST_NAME:= 12, + BOOT_FILE_SIZE:= 13, + MERIT_DUMP_FILE:= 14, + DOMAIN_NAME:= 15, + SWAP_SERVER:= 16, + ROOT_PATH:= 17, + EXTENSIONS_PATH:= 18, + END_VENDOR_OPTIONS:= 255, + //IP Layer Parameters Per Host + IP_FORWARDING:= 19, + NONLOCAL_SOURCE_ROUTING:= 20, + POLICY_FILTER:= 21, + MAXIMUM_DATAGRAM_REASSEMBLY_SIZE:= 22, + DEFAULT_IP_TTL:= 23, + PATH_MTU_AGING_TIMEOUT:= 24, + PATH_MTU_PLATEAU_TABLE:= 25, + //IP Layer Parameters Per Interface + INTERFACE_MTU:= 26, + ALL_SUBNETS_ARE_LOCAL:= 27, + BROADCAST_ADDRESS:= 28, + PERFORM_MASK_DISCOVERY:= 29, + MASK_SUPPLIER:= 30, + PERFORM_ROUTER_DISCOVERY:= 31, + ROUTER_SOLICITATION_ADDRESS:= 32, + STATIC_ROUTE:= 33, + //Link Layer Parameters Per Interface + TRAILER_ENCAPSULATION_OPTION:= 34, + ARP_CACHE_TIMEOUT:= 35, + ETHERNET_ENCAPSULATION:= 36, + //TCP Parameters + TCP_DEFAULT_TTL:= 37, + TCP_KEEPALIVE_INTERVAL:= 38, + TCP_KEEPALIVE_GARBAGE:= 39, + //Application and Service Parameters + NETWORK_INFORMATION_SERVICE_DOMAIN:= 40, + NETWORK_INFORMATION_SERVERS:= 41, + NTP_SERVERS:= 42, + VENDOR_SPECIFIC_INFORMATION:= 43, + NETBIOS_OVER_TCP_NAME_SERVER:= 44, + NETBIOS_OVER_TCP_DATAGRAM_DISTRIBUTION_SERVER:= 45, + NETBIOS_OVER_TCP_NODE_TYPE:= 46, + NETBIOS_OVER_TCP_SCOPE:= 47, + X_WINDOW_SYSTEM_FONT_SERVER:= 48, + X_WINDOW_SYSTEM_DISPLAY_MANAGER:= 49, + NETWORK_INFORMATION_SERVICE_PLUS_DOMAIN:= 64, + NETWORK_INFORMATION_SERVICE_PLUS_SERVERS:= 65, + MOBILE_IP_HOME_AGENT:= 68, + SMTP_SERVER:= 69, + POP3_SERVER:= 70, + NNTP_SERVER:= 71, + DEFAULT_WWW_SERVER:= 72, + DEFAULT_FINGER_SERVER:= 73, + DEFAULT_IRC_SERVER:= 74, + STREETTALK_SERVER:= 75, + STDA_SERVER:= 76, + //DHCP Extensions + REQUESTED_IP_ADDRESS:= 50, + IP_ADDRESS_LEASE_TIME:= 51, + OPTION_OVERLOAD:= 52, + DHCP_MESSAGE_TYPE:= 53, + SERVER_IDENTIFIER:= 54, + PARAMETER_REQUEST_LIST:= 55, + MESSAGE:= 56, + MAXIMUM_DHCP_MESSAGE_SIZE:= 57, + RENEWAL_T1_TIME_VALUE:= 58, + REBINDING_T2_TIME_VALUE:= 59, + VENDOR_CLASS_IDENTIFIER:= 60, + CLIENT_IDENTIFIER:= 61, + TFTP_SERVER_NAME:= 66, + BOOTFILE_NAME:= 67, + //Misc + RELAY_AGENT_INFORMATION:= 82, + NDS_SERVERS:= 85, + NDS_TREE_NAME:= 86, + NDS_CONTEXT:= 87, + POSIX_TIMEZONE:= 100, + TZ_TIMEZONE:= 101, + DOMAIN_SEARCH:= 119, + CLASSLESS_STATIC_ROUTE:= 121 + ); + TDHCPMessageType = ( + DISCOVER := 1, + OFFER := 2, + REQUEST := 3, + DECLINE := 4, + PACK := 5, + NAK := 6, + RELEASE := 7, + INFORM := 8 + ); + { Callback Types } TNetSendCallback = function(p_data : void; p_len : uint16) : sint32; @@ -197,6 +323,9 @@ type { Constants } const + { DHCP Magic } + DHCP_MAGIC : Array[0..3] of uint8 = ($63, $82, $53, $63); + { MACs } BROADCAST_MAC : Array[0..5] of uint8 = ($FF, $FF, $FF, $FF, $FF, $FF); NULL_MAC : Array[0..5] of uint8 = ($00, $00, $00, $00, $00, $00); diff --git a/src/driver/net/l1/net.pas b/src/driver/net/l1/net.pas index 6a29330d..e6fc37db 100644 --- a/src/driver/net/l1/net.pas +++ b/src/driver/net/l1/net.pas @@ -26,7 +26,12 @@ procedure writeToLogLn(str : pchar); implementation uses - ipv4, arp, eth2, icmp, e1000, terminal, udp; + terminal, + e1000, //dev + eth2, //L2 + arp, ipv4, //L3 + icmp, tcp, udp, //L4 + dhcp; //L5 var CBSend : TNetSendCallback = nil; @@ -116,11 +121,16 @@ end; procedure init; begin push_trace('net.init'); + //l2 eth2.register; + //l3 arp.register; ipv4.register; + //l4 icmp.register; udp.register; + //l5 + dhcp.register; pop_trace; end; diff --git a/src/driver/net/l4/udp.pas b/src/driver/net/l4/udp.pas index 7a5db721..01c7e9da 100644 --- a/src/driver/net/l4/udp.pas +++ b/src/driver/net/l4/udp.pas @@ -13,9 +13,6 @@ uses ipv4, netlog, net, util; -var - Ports : Array[0..65535] of PUDPBindContext; - procedure register(); function bind(bindContext : PUDPBindContext) : TUDPError; function unbind(bindContext : PUDPBindContext) : TUDPError; @@ -26,6 +23,9 @@ implementation uses console, terminal; +var + Ports : Array[0..65535] of PUDPBindContext; + function CalculateChecksum(p_data : void; p_len : uint16; udpContext : PUDPSendContext) : uint16; var pseudoBuffer : void; @@ -59,7 +59,7 @@ begin pseudoBuffer:= kalloc(sizeof(TUDPPseudoHeader) + pseudoSize); pseudoBuffer8:= puint8(pseudoBuffer); pseudoBuffer16:= puint16(pseudoBuffer); - pseudoBuffer32:= puint32(pseudoBuffer32); + pseudoBuffer32:= puint32(pseudoBuffer); pseudoHeader:= PUDPPseudoHeader(pseudoBuffer); copyIPv4(puint8(@udpContext^.context^.IP.Source[0]), puint8(@pseudoBuffer8[0])); copyIPv4(puint8(@udpContext^.context^.IP.Destination[0]), puint8(@pseudoBuffer8[4])); @@ -74,30 +74,24 @@ begin pseudoHeader^.Source_IP:= switchendian32(pseudoHeader^.Source_IP); pseudoHeader^.Destination_IP:= switchendian32(pseudoHeader^.Destination_IP); - //writehexln(pseudoHeader^.Source_IP); - //writehexln(pseudoHeader^.Destination_IP); - //writehexln(pseudoHeader^.Protocol); - //writehexln(pseudoHeader^.Length); - //writehexln(pseudoHeader^.UDP_Source); - //writehexln(pseudoHeader^.UDP_Destination); - //writehexln(pseudoHeader^.UDP_Length); - Checksum_u:= 0; + for i:=0 to (SizeOf(TUDPPseudoHeader) div 2)-1 do begin - //writestring('+ '); - //writehexln(pseudoBuffer16[i]); Checksum_u:= Checksum_u + pseudoBuffer16[i]; end; + for i:=(SizeOf(TUDPPseudoHeader) div 2) to ((pseudoSize + SizeOf(TUDPPseudoHeader)) div 2) - 1 do begin - //writestring('- '); - //ritehexln( switchendian16(pseudoBuffer16[i])); Checksum_u:= Checksum_u + switchendian16(pseudoBuffer16[i]); end; + Checksum_f:= Checksum_u AND $FFFF; Checksum_c:= (Checksum_u AND $FFFF0000) SHR 16; Checksum_f:= Checksum_f + Checksum_c; Checksum_f:= not Checksum_f; - CalculateChecksum:= Checksum_f; + CalculateChecksum:= Checksum_f; + + kfree(pseudoBuffer); + end; procedure send(p_data : void; p_len : uint16; udpContext : PUDPSendContext); @@ -255,31 +249,18 @@ begin for i:=0 to 65535 do begin Ports[i]:= nil; end; - //context:= PUDPBindContext(kalloc(sizeof(TUDPBindContext))); - //context^.Port:= 22294; - //context^.Callback:= @TestRecv; - //context^.UID:= 4398724; - //r:= bind(context); - writestring('[TestBind] '); - //case r of - // tueOK:writestringln('22294 bind OK'); - // tuePortInUse:writestringln('22294 port in use'); - // tueGenericError:writestringln('22294 generic error'); - // tuePortRestricted:writestringln('22294 restricted'); - //end; - - SendContext:= PUDPSendContext(kalloc(SizeOf(TUDPSendContext))); - BindContext:= PUDPBindContext(kalloc(SizeOf(TUDPBindContext))); - PacketContext:= PPacketContext(kalloc(SizeOf(TPacketContext))); - SendContext^.DstPort:= 10; - SendContext^.Socket:= BindContext; - SendContext^.Context:= PacketContext; - BindContext^.Port:= 20; - copyIPv4(puint8(@UDPT_S_IP[0]), puint8(@PacketContext^.IP.Source[0])); - copyIPv4(puint8(@UDPT_D_IP[0]), puint8(@PacketContext^.IP.Destination[0])); - - Checksum:= CalculateChecksum(void(@UDPT_DATA[0]), 2, SendContext); - writehexln(Checksum); + //writestring('[TestBind] '); + //SendContext:= PUDPSendContext(kalloc(SizeOf(TUDPSendContext))); + //BindContext:= PUDPBindContext(kalloc(SizeOf(TUDPBindContext))); + //PacketContext:= PPacketContext(kalloc(SizeOf(TPacketContext))); + //SendContext^.DstPort:= 10; + //SendContext^.Socket:= BindContext; + //SendContext^.Context:= PacketContext; + //BindContext^.Port:= 20; + //copyIPv4(puint8(@UDPT_S_IP[0]), puint8(@PacketContext^.IP.Source[0])); + //copyIPv4(puint8(@UDPT_D_IP[0]), puint8(@PacketContext^.IP.Destination[0])); + //Checksum:= CalculateChecksum(void(@UDPT_DATA[0]), 2, SendContext); + //writehexln(Checksum); //while true do begin end; ipv4.registerProtocol($11, @ProcessPacket); diff --git a/src/driver/net/l5/dhcp.pas b/src/driver/net/l5/dhcp.pas new file mode 100644 index 00000000..8e9f3b4c --- /dev/null +++ b/src/driver/net/l5/dhcp.pas @@ -0,0 +1,55 @@ +unit dhcp; + +interface + +uses + lmemorymanager, console, + nettypes, netutils, udp, netlog, net, + util, rand, lists; + +type + TDHCPOptions = PLinkedListBase; + PDHCPOptions = ^PLinkedListBase; + TDHCPOption = record + Opcode : TDHCPOpCode; + Size : uint32; + Value : void; + Header_Location : void; + Reverse_Endian : boolean; + end; + PDHCPOption = ^TDHCPOption; + +procedure register(); +procedure DHCPDiscover(); + +implementation + +var + XID : uint32; + Socket : PUDPBindContext; + +procedure processPacket(p_data : void; p_len : uint16; context : PUDPPacketContext); +begin + +end; + +procedure DHCPDiscover(); +begin + +end; + +procedure register(); +begin + console.outputln('DHCP', 'Register begin.'); + Socket:= PUDPBindContext(Kalloc(sizeof(TUDPBindContext))); + Socket^.Port:= 68; + Socket^.Callback:= @processPacket; + Socket^.UID:= rand32; + case UDP.bind(Socket) of + tueOK:console.outputln('DHCP', 'Successfully bound port 68.'); + else console.outputln('DHCP', 'Failed to bind port 68.'); + end; + console.outputln('DHCP', 'Register end.'); +end; + +end. \ No newline at end of file diff --git a/src/include/rand.pas b/src/include/rand.pas index 0eae6816..95f55ec3 100644 --- a/src/include/rand.pas +++ b/src/include/rand.pas @@ -12,10 +12,15 @@ implementation var next : uint32 = 1; -function rand32 : uint32; +function rand : uint32; begin next:= next * 1103515245 + 12345; - rand32:= (next div 65536) mod 32768; + rand:= (next div 65536) mod 32768; +end; + +function rand32 : uint32; +begin + rand32:= (rand SHL 16) AND rand; end; function rand16 : uint16; diff --git a/src/kernel.pas b/src/kernel.pas index 97ad6ed5..e6303c83 100644 --- a/src/kernel.pas +++ b/src/kernel.pas @@ -241,6 +241,10 @@ begin tracer.push_trace('kmain.END'); + writehexln(rand.rand32); + writehexln(rand.rand32); + writehexln(rand.rand32); + tracer.push_trace('kmain.TICK'); while true do begin tracer.push_trace('kmain.RedrawWindows');