From 41769c02317835472d7678d3531ecfc23df8e17a Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sat, 20 Nov 2010 11:50:39 +0800 Subject: [PATCH] General fixes - Fixed recursion in ARP_Resolve when there is not a direct route to the destinaton > ARP now does not use routes if the destination address is on the current subnet. - Added debug to routing code, debug to IRC client too --- Kernel/arch/x86/lib.c | 2 + Kernel/debug.c | 2 +- Modules/IPStack/arp.c | 3 +- Modules/IPStack/ipstack.h | 1 + Modules/IPStack/main.c | 28 ++++++++++++++ Modules/IPStack/routing.c | 46 ++++++++++++++++++----- Usermode/Applications/ifconfig_src/main.c | 9 +++-- Usermode/Applications/irc_src/main.c | 10 ++++- 8 files changed, 85 insertions(+), 16 deletions(-) diff --git a/Kernel/arch/x86/lib.c b/Kernel/arch/x86/lib.c index 2fa1e516..9287d9c3 100644 --- a/Kernel/arch/x86/lib.c +++ b/Kernel/arch/x86/lib.c @@ -189,6 +189,8 @@ void *memsetd(void *Dest, Uint32 Val, size_t Num) */ int memcmp(const void *m1, const void *m2, size_t Num) { + if( Num == 0 ) return 1; // No bytes are always identical + while(Num--) { if(*(Uint8*)m1 != *(Uint8*)m2) break; diff --git a/Kernel/debug.c b/Kernel/debug.c index ce87d9de..6bea250e 100644 --- a/Kernel/debug.c +++ b/Kernel/debug.c @@ -399,7 +399,7 @@ void Debug_Leave(char *FuncName, char RetType, ...) while(i--) Debug_Putchar(' '); Debug_Puts(1, FuncName); - Debug_FmtS("(%i)", tid); + Debug_FmtS("[%i]", tid); Debug_Puts(1, ": RETURN"); // No Return diff --git a/Modules/IPStack/arp.c b/Modules/IPStack/arp.c index 9e92414b..476557e6 100644 --- a/Modules/IPStack/arp.c +++ b/Modules/IPStack/arp.c @@ -76,7 +76,8 @@ tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address) ENTER("pInterface xAddress", Interface, Address); - // Check routing tables + // Check routing tables if not on this subnet + if( IPStack_CompareAddress(4, &Address, Interface->Address, Interface->SubnetBits) == 0 ) { tRoute *route = IPStack_FindRoute(4, Interface, &Address); if( route ) { diff --git a/Modules/IPStack/ipstack.h b/Modules/IPStack/ipstack.h index 071c62b1..e6ded4b1 100644 --- a/Modules/IPStack/ipstack.h +++ b/Modules/IPStack/ipstack.h @@ -115,6 +115,7 @@ static const tMacAddr cMAC_BROADCAST = {{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}}; extern int IPStack_AddFile(tSocketFile *File); extern int IPStack_GetAddressSize(int AddressType); extern int IPStack_CompareAddress(int AddressType, void *Address1, void *Address2, int CheckBits); +extern const char *IPStack_PrintAddress(int AddressType, void *Address); extern tRoute *IPStack_FindRoute(int AddressType, tInterface *Interface, void *Address); diff --git a/Modules/IPStack/main.c b/Modules/IPStack/main.c index bec82b33..ef1a2cd8 100644 --- a/Modules/IPStack/main.c +++ b/Modules/IPStack/main.c @@ -122,6 +122,8 @@ int IPStack_CompareAddress(int AddressType, void *Address1, void *Address2, int if( CheckBits < 0 ) CheckBits = 0; if( CheckBits > size*8 ) CheckBits = size*8; + if( CheckBits == 0 ) return 1; // /0 matches anythin + // Check first bits/8 bytes if( memcmp(Address1, Address2, CheckBits/8) != 0 ) return 0; @@ -135,3 +137,29 @@ int IPStack_CompareAddress(int AddressType, void *Address1, void *Address2, int return 0; } + +const char *IPStack_PrintAddress(int AddressType, void *Address) +{ + switch( AddressType ) + { + case 4: { + static char ret[4*3+3+1]; + Uint8 *addr = Address; + sprintf(ret, "%i.%i.%i.%i", addr[0], addr[1], addr[2], addr[3]); + return ret; + } + + case 6: { + static char ret[8*4+7+1]; + Uint16 *addr = Address; + sprintf(ret, "%x:%x:%x:%x:%x:%x:%x:%x", + addr[0], addr[1], addr[2], addr[3], + addr[4], addr[5], addr[6], addr[7] + ); + return ret; + } + + default: + return ""; + } +} diff --git a/Modules/IPStack/routing.c b/Modules/IPStack/routing.c index cae3b986..c1060342 100644 --- a/Modules/IPStack/routing.c +++ b/Modules/IPStack/routing.c @@ -94,6 +94,7 @@ static const char *casIOCtls_RouteDir[] = { int IPStack_RouteDir_IOCtl(tVFS_Node *Node, int ID, void *Data) { int tmp; + ENTER("pNode iID pData", Node, ID, Data); switch(ID) { // --- Standard IOCtls (0-3) --- @@ -116,8 +117,10 @@ int IPStack_RouteDir_IOCtl(tVFS_Node *Node, int ID, void *Data) return tmp; case 4: // Add Route - if( !CheckString(Data) ) return -1; - return IPStack_Route_Create(Data); + if( !CheckString(Data) ) LEAVE_RET('i', -1); + tmp = IPStack_Route_Create(Data); + LEAVE('i', tmp); + return tmp; case 5: // Locate Route { @@ -128,19 +131,23 @@ int IPStack_RouteDir_IOCtl(tVFS_Node *Node, int ID, void *Data) tRoute *rt; if( !CheckMem(Data, sizeof(int)) ) - return -1; + LEAVE_RET('i', -1); if( !CheckMem(Data, sizeof(int) + IPStack_GetAddressSize(data->Type)) ) - return -1; + LEAVE_RET('i', -1); + Log_Debug("IPStack", "Route_RouteDir_IOCtl - FindRoute %i, %s\n", + data->Type, IPStack_PrintAddress(data->Type, data->Addr) ); rt = IPStack_FindRoute(data->Type, NULL, data->Addr); if( !rt ) - return 0; + LEAVE_RET('i', 0); + LEAVE('i', rt->Node.Inode); return rt->Node.Inode; } break; } + LEAVE('i', 0); return 0; } @@ -158,7 +165,10 @@ int IPStack_Route_Create(const char *InterfaceName) // Note: Oh man! This is such a hack { tVFS_Node *node = IPStack_Root_FindDir(NULL, InterfaceName); - if( !node ) return 0; + if( !node ) { + Log_Debug("IPStack", "IPStack_Route_Create - Unknown interface '%s'\n", InterfaceName); + return 0; + } iface = node->ImplPtr; } @@ -195,6 +205,8 @@ int IPStack_Route_Create(const char *InterfaceName) gIP_Routes = gIP_RoutesEnd = rt; } + Log_Log("IPStack", "Route entry for '%s' created\n", InterfaceName); + return rt->Node.Inode; } @@ -205,7 +217,14 @@ tRoute *IPStack_FindRoute(int AddressType, tInterface *Interface, void *Address) tRoute *rt; tRoute *best = NULL; - if( Interface && AddressType != Interface->Type ) return NULL; + ENTER("iAddressType pInterface sAddress", + AddressType, Interface, IPStack_PrintAddress(AddressType, Address)); + + if( Interface && AddressType != Interface->Type ) { + LOG("Interface->Type (%i) != AddressType", Interface->Type); + LEAVE('n'); + return NULL; + } for( rt = gIP_Routes; rt; rt = rt->Next ) { @@ -214,20 +233,29 @@ tRoute *IPStack_FindRoute(int AddressType, tInterface *Interface, void *Address) // Check address type if( rt->AddressType != AddressType ) continue; + LOG("Checking network %s/%i", IPStack_PrintAddress(AddressType, rt->Network), rt->SubnetBits); + // Check if the address matches if( !IPStack_CompareAddress(AddressType, rt->Network, Address, rt->SubnetBits) ) continue; if( best ) { // More direct routes are preferred - if( best->SubnetBits > rt->SubnetBits ) continue; + if( best->SubnetBits > rt->SubnetBits ) { + LOG("Skipped - less direct (%i < %i)", rt->SubnetBits, best->SubnetBits); + continue; + } // If equally direct, choose the best metric - if( best->SubnetBits == rt->SubnetBits && best->Metric < rt->Metric ) continue; + if( best->SubnetBits == rt->SubnetBits && best->Metric < rt->Metric ) { + LOG("Skipped - higher metric (%i > %i)", rt->Metric, best->Metric); + continue; + } } best = rt; } + LEAVE('p', best); return best; } diff --git a/Usermode/Applications/ifconfig_src/main.c b/Usermode/Applications/ifconfig_src/main.c index 7a7fd07a..c3ae3166 100644 --- a/Usermode/Applications/ifconfig_src/main.c +++ b/Usermode/Applications/ifconfig_src/main.c @@ -223,7 +223,7 @@ void DumpRoute(const char *Name) { int fd; int type; - char path[sizeof(IPSTACK_ROOT)+7+FILENAME_MAX+1] = IPSTACK_ROOT"/route/"; + char path[sizeof(IPSTACK_ROOT)+8+FILENAME_MAX+1] = IPSTACK_ROOT"/routes/"; strcat(path, Name); @@ -346,7 +346,7 @@ void AddRoute(const char *Interface, void *Dest, int MaskBits, void *NextHop) int DoAutoConfig(const char *Device) { int tmp, fd; - char path[sizeof(IPSTACK_ROOT)+5+1]; // ip000 + char path[sizeof(IPSTACK_ROOT)+1+4+1]; // /0000 uint8_t addr[4] = {10,0,2,55}; uint8_t gw[4] = {10,0,2,1}; int subnet = 8; @@ -373,10 +373,11 @@ int DoAutoConfig(const char *Device) // Set Subnet ioctl(fd, ioctl(fd, 3, "getset_subnet"), &subnet); - // Set Gateway + // Set routes { uint8_t net[4] = {0,0,0,0}; - AddRoute(path + sizeof(IPSTACK_ROOT) + 1, net, 0, gw); + AddRoute(path + sizeof(IPSTACK_ROOT), addr, 8, net); // This interface + AddRoute(path + sizeof(IPSTACK_ROOT), net, 0, gw); // Gateway } close(fd); diff --git a/Usermode/Applications/irc_src/main.c b/Usermode/Applications/irc_src/main.c index 119e025d..30c579c0 100644 --- a/Usermode/Applications/irc_src/main.c +++ b/Usermode/Applications/irc_src/main.c @@ -48,6 +48,8 @@ int main(int argc, const char *argv[], const char *envp[]) return -1; } + printf("Connection opened\n"); + writef(srv.FD, "USER %s %s %s : %s\n", gsUsername, gsHostname, gsRemoteAddress, gsRealName); writef(srv.FD, "NICK %s", gsNickname); @@ -211,6 +213,8 @@ int OpenTCP(const char *AddressString, short PortNumber) return -1; } + printf("iface = '%s'\n", iface); + // Open client socket // TODO: Move this out to libnet? { @@ -223,16 +227,20 @@ int OpenTCP(const char *AddressString, short PortNumber) free(iface); if( fd == -1 ) { + fprintf(stderr, "Unable to open TCP Client for reading\n"); return -1; } - + // Set remote port and address + printf("Setting port and remote address"); ioctl(fd, 5, &PortNumber); ioctl(fd, 6, addrBuffer); // Connect + printf("Initiating connection"); if( ioctl(fd, 7, NULL) == 0 ) { // Shouldn't happen :( + fprintf(stderr, "Unable to start connection\n"); return -1; } -- 2.20.1