From a6ae0de09b0704ff2a9ffa3ad8f10cd1d4cc58d7 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Wed, 3 Mar 2010 15:28:43 +0800 Subject: [PATCH] Cleanup commit, fixes to UDP, TCP and ICMP - Basicly cleaning up commented out code and potential bugs - More work on TCP support --- Kernel/Makefile.BuildNum | 2 +- Modules/IPStack/icmp.c | 3 +- Modules/IPStack/icmp.h | 1 + Modules/IPStack/tcp.c | 52 +++++++++++++++++-- Modules/IPStack/udp.c | 13 ++++- .../Applications/MultibootCheck_src/Makefile | 2 +- 6 files changed, 64 insertions(+), 9 deletions(-) diff --git a/Kernel/Makefile.BuildNum b/Kernel/Makefile.BuildNum index bc2c5c66..ef0be0a0 100644 --- a/Kernel/Makefile.BuildNum +++ b/Kernel/Makefile.BuildNum @@ -1 +1 @@ -BUILD_NUM = 1461 +BUILD_NUM = 1464 diff --git a/Modules/IPStack/icmp.c b/Modules/IPStack/icmp.c index cdc7126a..77bc6911 100644 --- a/Modules/IPStack/icmp.c +++ b/Modules/IPStack/icmp.c @@ -54,7 +54,7 @@ void ICMP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buff } if(hdr->ID != (Uint16)~hdr->Sequence) { Warning("[ICMP ] ID and Sequence values do not match"); - return ; + //return ; } gICMP_PingSlots[hdr->ID].bArrived = 1; break; @@ -70,6 +70,7 @@ void ICMP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buff Log("[ICMP ] Destination Unreachable (Code %i)", hdr->Code); break; } +// IPv4_Unreachable( Interface, hdr->Code, htons(hdr->Length)-sizeof(tICMPHeader), hdr->Data ); break; // -- 8: Echo Request diff --git a/Modules/IPStack/icmp.h b/Modules/IPStack/icmp.h index a9594f8e..8e787224 100644 --- a/Modules/IPStack/icmp.h +++ b/Modules/IPStack/icmp.h @@ -16,6 +16,7 @@ struct sICMPHeader Uint16 Checksum; Uint16 ID; Uint16 Sequence; + Uint8 Data[]; }; // === CONSTANTS === diff --git a/Modules/IPStack/tcp.c b/Modules/IPStack/tcp.c index 9225ffaf..9b3f8f45 100644 --- a/Modules/IPStack/tcp.c +++ b/Modules/IPStack/tcp.c @@ -115,10 +115,10 @@ void TCP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffe tTCPConnection *conn; Log("[TCP ] sizeof(tTCPHeader) = %i", sizeof(tTCPHeader)); + Log("[TCP ] SourcePort = %i", ntohs(hdr->SourcePort)); Log("[TCP ] DestPort = %i", ntohs(hdr->DestPort)); - Log("[TCP ] DestPort = %i", ntohs(hdr->DestPort)); - Log("[TCP ] SequenceNumber = %i", ntohl(hdr->SequenceNumber)); - Log("[TCP ] AcknowlegementNumber = %i", ntohl(hdr->AcknowlegementNumber)); + Log("[TCP ] SequenceNumber = 0x%x", ntohl(hdr->SequenceNumber)); + Log("[TCP ] AcknowlegementNumber = 0x%x", ntohl(hdr->AcknowlegementNumber)); Log("[TCP ] DataOffset = %i", hdr->DataOffset >> 4); Log("[TCP ] Flags = {"); Log("[TCP ] CWR = %B", !!(hdr->Flags & TCP_FLAG_CWR)); @@ -167,7 +167,51 @@ void TCP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffe } // Open a new connection (well, check that it's a SYN) - //TODO + if(hdr->Flags != TCP_FLAG_SYN) { + Log("[TCP ] Packet is not a SYN"); + continue; + } + + conn = calloc(1, sizeof(tTCPConnection)); + conn->State = TCP_ST_HALFOPEN; + conn->LocalPort = srv->Port; + conn->RemotePort = hdr->SourcePort; + conn->Interface = Interface; + + switch(Interface->Type) + { + case 4: conn->RemoteIP.v4 = *(tIPv4*)Address; break; + case 6: conn->RemoteIP.v6 = *(tIPv6*)Address; break; + } + + conn->NextSequenceRcv = ntohl( hdr->SequenceNumber ); + conn->NextSequenceSend = rand(); + + // Create node + conn->Node.NumACLs = 1; + conn->Node.ACLs = &gVFS_ACL_EveryoneRW; + conn->Node.ImplInt = srv->NextID ++; + conn->Node.Read = TCP_Client_Read; + conn->Node.Write = TCP_Client_Write; + //conn->Node.Close = TCP_SrvConn_Close; + + // Hmm... Theoretically, this lock will never have to wait, + // as the interface is locked to the watching thread, and this + // runs in the watching thread. But, it's a good idea to have + // it, just in case + LOCK(&srv->lConnections); + conn->Next = srv->Connections; + srv->Connections = conn; + RELEASE(&srv->lConnections); + + // Send the SYN ACK + Log("[TCP ] TODO: Sending SYN ACK"); + hdr->Flags |= TCP_FLAG_ACK; + hdr->AcknowlegementNumber = hdr->SequenceNumber; + hdr->SequenceNumber = conn->NextSequenceSend; + hdr->DestPort = hdr->SourcePort; + hdr->SourcePort = srv->Port; + TCP_SendPacket( conn, sizeof(tTCPHeader), hdr ); break; } diff --git a/Modules/IPStack/udp.c b/Modules/IPStack/udp.c index 760c9974..4ce447d8 100644 --- a/Modules/IPStack/udp.c +++ b/Modules/IPStack/udp.c @@ -11,6 +11,8 @@ // === PROTOTYPES === void UDP_Initialise(); void UDP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffer); +void UDP_Unreachable(tInterface *Interface, int Code, void *Address, int Length, void *Buffer); +void UDP_SendPacket(tUDPChannel *Channel, void *Data, size_t Length); // --- Listening Server tVFS_Node *UDP_Server_Init(tInterface *Interface); char *UDP_Server_ReadDir(tVFS_Node *Node, int ID); @@ -50,6 +52,7 @@ void UDP_Initialise() { IPStack_AddFile(&gUDP_ServerFile); IPStack_AddFile(&gUDP_ClientFile); + //IPv4_RegisterCallback(IP4PROT_UDP, UDP_GetPacket, UDP_Unreachable); IPv4_RegisterCallback(IP4PROT_UDP, UDP_GetPacket); } @@ -69,9 +72,7 @@ int UDP_int_ScanList(tUDPChannel *List, tInterface *Interface, void *Address, in chan = chan->Next) { if(chan->Interface != Interface) continue; - //Log("[UDP ] Local (0x%04x) == Dest (0x%04x)", chan->LocalPort, ntohs(hdr->DestPort)); if(chan->LocalPort != ntohs(hdr->DestPort)) continue; - //Log("[UDP ] Remote (0x%04x) == Source (0x%04x)", chan->RemotePort, ntohs(hdr->SourcePort)); if(chan->RemotePort != ntohs(hdr->SourcePort)) continue; if(Interface->Type == 4) { @@ -148,6 +149,14 @@ void UDP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffe } +/** + * \brief Handle an ICMP Unrechable Error + */ +void UDP_Unreachable(tInterface *Interface, int Code, void *Address, int Length, void *Buffer) +{ + +} + /** * \brief Send a packet * \param Channel Channel to send the packet from diff --git a/Usermode/Applications/MultibootCheck_src/Makefile b/Usermode/Applications/MultibootCheck_src/Makefile index 291376e6..ac0dc415 100644 --- a/Usermode/Applications/MultibootCheck_src/Makefile +++ b/Usermode/Applications/MultibootCheck_src/Makefile @@ -1,4 +1,4 @@ -# Project: cat +# Project: Multiboot Check -include ../Makefile.cfg -- 2.20.1