From f429a25f24ffd4762d59bee5f7122ac06881f3ca Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sun, 21 Nov 2010 15:50:50 +0800 Subject: [PATCH] Fiddling with IPStack - Added support for setting interfaces from the kernel boot line --- Kernel/include/acess.h | 3 ++ Kernel/lib.c | 54 +++++++++++++++++++++++ Modules/Filesystems/Ext2/ext2.c | 3 +- Modules/IPStack/interface.c | 25 ++++++----- Modules/IPStack/link.c | 1 + Modules/IPStack/main.c | 50 +++++++++++++++++++-- Usermode/Applications/ifconfig_src/main.c | 12 ++--- 7 files changed, 126 insertions(+), 22 deletions(-) diff --git a/Kernel/include/acess.h b/Kernel/include/acess.h index 9337567c..779cf884 100644 --- a/Kernel/include/acess.h +++ b/Kernel/include/acess.h @@ -333,6 +333,7 @@ extern int strucmp(const char *Str1, const char *Str2); #define strdup(Str) _strdup(_MODULE_NAME_"/"__FILE__, __LINE__, (Str)) extern char *_strdup(const char *File, int Line, const char *Str); extern char **str_split(const char *__str, char __ch); +extern char *strchr(const char *__s, int __c); extern int strpos(const char *Str, char Ch); extern int strpos8(const char *str, Uint32 search); extern void itoa(char *buf, Uint num, int base, int minLength, char pad); @@ -341,7 +342,9 @@ extern int ReadUTF8(Uint8 *str, Uint32 *Val); extern int WriteUTF8(Uint8 *str, Uint32 Val); extern int ModUtil_SetIdent(char *Dest, char *Value); extern int ModUtil_LookupString(char **Array, char *Needle); + extern Uint8 ByteSum(void *Ptr, int Size); +extern int UnHex(Uint8 *Dest, size_t DestSize, const char *SourceString); /** * \} */ diff --git a/Kernel/lib.c b/Kernel/lib.c index 66c96603..c2c1b588 100644 --- a/Kernel/lib.c +++ b/Kernel/lib.c @@ -20,6 +20,7 @@ void itoa(char *buf, Uint num, int base, int minLength, char pad); int sprintf(char *__s, const char *__format, ...); int tolower(int c); int strucmp(const char *Str1, const char *Str2); +char *strchr(const char *__s, int __c); int strpos(const char *Str, char Ch); Uint8 ByteSum(void *Ptr, int Size); size_t strlen(const char *__s); @@ -32,13 +33,18 @@ char **str_split(const char *__str, char __ch); int strpos8(const char *str, Uint32 Search); int ReadUTF8(Uint8 *str, Uint32 *Val); int WriteUTF8(Uint8 *str, Uint32 Val); + int DivUp(int num, int dem); Sint64 timestamp(int sec, int mins, int hrs, int day, int month, int year); Uint rand(void); + int CheckString(char *String); int CheckMem(void *Mem, int NumBytes); + int ModUtil_LookupString(char **Array, char *Needle); int ModUtil_SetIdent(char *Dest, char *Value); + + int UnHex(Uint8 *Dest, size_t DestSize, const char *SourceString); // === EXPORTS === EXPORT(atoi); @@ -47,6 +53,7 @@ EXPORT(vsnprintf); EXPORT(sprintf); EXPORT(tolower); EXPORT(strucmp); +EXPORT(strchr); EXPORT(strpos); EXPORT(ByteSum); EXPORT(strlen); @@ -66,6 +73,7 @@ EXPORT(CheckString); EXPORT(CheckMem); EXPORT(ModUtil_LookupString); EXPORT(ModUtil_SetIdent); +EXPORT(UnHex); // === CODE === /** @@ -379,6 +387,18 @@ int strucmp(const char *Str1, const char *Str2) return tolower(*Str1) - tolower(*Str2); } +/** + * \brief Locate a byte in a string + */ +char *strchr(const char *__s, int __c) +{ + for( ; *__s; __s ++ ) + { + if( *__s == __c ) return (char*)__s; + } + return NULL; +} + /** * \fn int strpos(const char *Str, char Ch) * \brief Search a string for an ascii character @@ -823,3 +843,37 @@ int ModUtil_SetIdent(char *Dest, char *Value) strncpy(Dest, Value, 32); return 1; } + +/** + * \brief Convert a string of hexadecimal digits into a byte stream + */ +int UnHex(Uint8 *Dest, size_t DestSize, const char *SourceString) +{ + int i; + + for( i = 0; i < DestSize*2; i += 2 ) + { + Uint8 val = 0; + + if(SourceString[i] == '\0') break; + + if('0' <= SourceString[i] && SourceString[i] <= '9') + val |= (SourceString[i]-'0') << 4; + else if('A' <= SourceString[i] && SourceString[i] <= 'F') + val |= (SourceString[i]-'A'+10) << 4; + else if('a' <= SourceString[i] && SourceString[i] <= 'f') + val |= (SourceString[i]-'a'+10) << 4; + + if(SourceString[i+1] == '\0') break; + + if('0' <= SourceString[i+1] && SourceString[i+1] <= '9') + val |= (SourceString[i+1] - '0'); + else if('A' <= SourceString[i+1] && SourceString[i+1] <= 'F') + val |= (SourceString[i+1] - 'A' + 10); + else if('a' <= SourceString[i+1] && SourceString[i+1] <= 'f') + val |= (SourceString[i+1] - 'a' + 10); + + Dest[i/2] = val; + } + return i/2; +} diff --git a/Modules/Filesystems/Ext2/ext2.c b/Modules/Filesystems/Ext2/ext2.c index 4a2aece0..52793d01 100644 --- a/Modules/Filesystems/Ext2/ext2.c +++ b/Modules/Filesystems/Ext2/ext2.c @@ -73,7 +73,8 @@ tVFS_Node *Ext2_InitDevice(char *Device, char **Options) // Sanity Check Magic value if(sb.s_magic != 0xEF53) { - Log_Warning("EXT2", "Volume '%s' is not an EXT2 volume", Device); + Log_Warning("EXT2", "Volume '%s' is not an EXT2 volume (0x%x != 0xEF53)", + Device, sb.s_magic); VFS_Close(fd); LEAVE('n'); return NULL; diff --git a/Modules/IPStack/interface.c b/Modules/IPStack/interface.c index c6f55ba9..50d4c229 100644 --- a/Modules/IPStack/interface.c +++ b/Modules/IPStack/interface.c @@ -23,8 +23,8 @@ char *IPStack_Root_ReadDir(tVFS_Node *Node, int Pos); tVFS_Node *IPStack_Root_FindDir(tVFS_Node *Node, const char *Name); int IPStack_Root_IOCtl(tVFS_Node *Node, int ID, void *Data); - int IPStack_AddInterface(const char *Device, const char *Name); int IPStack_AddFile(tSocketFile *File); +tInterface *IPStack_AddInterface(const char *Device, const char *Name); tAdapter *IPStack_GetAdapter(const char *Path); char *IPStack_Iface_ReadDir(tVFS_Node *Node, int Pos); @@ -219,7 +219,8 @@ int IPStack_Root_IOCtl(tVFS_Node *Node, int ID, void *Data) if( !CheckString( Data ) ) LEAVE_RET('i', -1); { char name[4] = ""; - tmp = IPStack_AddInterface(Data, name); + tInterface *iface = IPStack_AddInterface(Data, name); + tmp = iface->Node.ImplInt; } LEAVE_RET('i', tmp); } @@ -228,10 +229,10 @@ int IPStack_Root_IOCtl(tVFS_Node *Node, int ID, void *Data) } /** - * \fn int IPStack_AddInterface(char *Device) + * \fn tInterface *IPStack_AddInterface(char *Device) * \brief Adds an interface to the list */ -int IPStack_AddInterface(const char *Device, const char *Name) +tInterface *IPStack_AddInterface(const char *Device, const char *Name) { tInterface *iface; tAdapter *card; @@ -241,8 +242,8 @@ int IPStack_AddInterface(const char *Device, const char *Name) card = IPStack_GetAdapter(Device); if( !card ) { - LEAVE('i', -1); - return -1; // ERR_YOURBAD + LEAVE('n'); + return NULL; // ERR_YOURBAD } nameLen = sprintf(NULL, "%i", giIP_NextIfaceId); @@ -253,8 +254,8 @@ int IPStack_AddInterface(const char *Device, const char *Name) + IPStack_GetAddressSize(-1) ); if(!iface) { - LEAVE('i', -2); - return -2; // Return ERR_MYBAD + LEAVE('n'); + return NULL; // Return ERR_MYBAD } iface->Next = NULL; @@ -282,8 +283,8 @@ int IPStack_AddInterface(const char *Device, const char *Name) iface->Adapter = IPStack_GetAdapter(Device); if( !iface->Adapter ) { free( iface ); - LEAVE('i', -1); - return -1; // Return ERR_YOUFAIL + LEAVE('n'); + return NULL; // Return ERR_YOUFAIL } // Delay setting ImplInt until after the adapter is opened @@ -306,8 +307,8 @@ int IPStack_AddInterface(const char *Device, const char *Name) // gIP_DriverInfo.RootNode.Size ++; // Success! - LEAVE('i', iface->Node.ImplInt); - return iface->Node.ImplInt; + LEAVE('p', iface); + return iface; } /** diff --git a/Modules/IPStack/link.c b/Modules/IPStack/link.c index afd54eb1..07234df0 100644 --- a/Modules/IPStack/link.c +++ b/Modules/IPStack/link.c @@ -124,6 +124,7 @@ void Link_WatchDevice(tAdapter *Adapter) Uint32 checksum; // Wait for a packet (Read on a network device is blocking) + Log_Debug("NET", "Waiting on adapter FD#0x%x", Adapter->DeviceFD); ret = VFS_Read(Adapter->DeviceFD, MAX_PACKET_SIZE, buf); if(ret == -1) break; diff --git a/Modules/IPStack/main.c b/Modules/IPStack/main.c index 8e3810d9..6dac9382 100644 --- a/Modules/IPStack/main.c +++ b/Modules/IPStack/main.c @@ -21,6 +21,7 @@ extern char *IPStack_Root_ReadDir(tVFS_Node *Node, int Pos); extern tVFS_Node *IPStack_Root_FindDir(tVFS_Node *Node, const char *Name); extern int IPStack_Root_IOCtl(tVFS_Node *Node, int ID, void *Data); extern tInterface gIP_LoopInterface; +extern tInterface *IPStack_AddInterface(const char *Device, const char *Name); // === PROTOTYPES === int IPStack_Install(char **Arguments); @@ -64,17 +65,60 @@ int IPStack_Install(char **Arguments) for( i = 0; Arguments[i]; i++ ) { // TODO: - // Define interfaces by ,,, + // Define interfaces by ::: // Where: // - is the device path (E.g. /Devices/ne2k/0) // - is a number (e.g. 4) or symbol (e.g. AF_INET4) // - is a condensed hexadecimal stream (in big endian) // (E.g. 0A000201 for 10.0.2.1 IPv4) // - is the number of subnet bits (E.g. 24 for an IPv4 Class C) - // Example: /Devices/ne2k/0,4,0A00020A,24 + // Example: /Devices/ne2k/0:4:0A00020A:24 + // would define an interface with the address 10.0.2.10/24 + if( Arguments[i][0] == '/' ) { + // Define Interface + char *dev, *type, *addr, *bits; + + // Read definition + dev = Arguments[i]; + type = strchr(dev, ':'); + if( !type ) { + Log_Warning("IPStack", ":::"); + continue; + } + *type = '\0'; type ++; + + addr = strchr(type, ':'); + if( !addr ) { + Log_Warning("IPStack", ":::"); + continue; + } + *addr = '\0'; addr ++; + + bits = strchr(addr, ':'); + if( !bits ) { + Log_Warning("IPStack", ":::"); + continue; + } + *bits = '\0'; bits ++; + + // Define interface + { + int iType = atoi(type); + int size = IPStack_GetAddressSize(iType); + Uint8 addrData[size]; + int iBits = atoi(bits); + + UnHex(addrData, size, addr); + + tInterface *iface = IPStack_AddInterface(dev, ""); + iface->Type = iType; + memcpy(iface->Address, addrData, size); + iface->SubnetBits = iBits; + } + } // I could also define routes using ,,, - // Example: 1,00000000,0,0A000201 + // Example: 1:00000000:0:0A000201 } } diff --git a/Usermode/Applications/ifconfig_src/main.c b/Usermode/Applications/ifconfig_src/main.c index 8d97d1b9..3a455304 100644 --- a/Usermode/Applications/ifconfig_src/main.c +++ b/Usermode/Applications/ifconfig_src/main.c @@ -174,9 +174,10 @@ void DumpInterface(const char *Name) int len = ioctl(fd, call_num, NULL); char *buf = malloc(len+1); ioctl(fd, call_num, buf); - printf("'%s'\t", buf); + printf("'%s'\n", buf); free(buf); } + printf("\t"); // Get the address type switch(type) { @@ -187,20 +188,20 @@ void DumpInterface(const char *Name) { uint8_t ip[4]; int subnet; - printf("IPv4\n"); + printf("IPv4\t"); ioctl(fd, 5, ip); // Get IP Address subnet = ioctl(fd, 7, NULL); // Get Subnet Bits - printf("\tAddress: %i.%i.%i.%i/%i\n", ip[0], ip[1], ip[2], ip[3], subnet); + printf("%i.%i.%i.%i/%i\n", ip[0], ip[1], ip[2], ip[3], subnet); } break; case 6: // IPv6 { uint16_t ip[8]; int subnet; - printf("IPv6\n"); + printf("IPv6\t"); ioctl(fd, 5, ip); // Get IP Address subnet = ioctl(fd, 7, NULL); // Get Subnet Bits - printf("\t%x:%x:%x:%x:%x:%x:%x:%x/%i\n", + printf("%x:%x:%x:%x:%x:%x:%x:%x/%i\n", ntohs(ip[0]), ntohs(ip[1]), ntohs(ip[2]), ntohs(ip[3]), ntohs(ip[4]), ntohs(ip[5]), ntohs(ip[6]), ntohs(ip[7]), subnet); @@ -210,7 +211,6 @@ void DumpInterface(const char *Name) printf("UNKNOWN (%i)\n", type); break; } - printf("\n"); close(fd); } -- 2.20.1