Fiddling with IPStack
authorJohn Hodge <[email protected]>
Sun, 21 Nov 2010 07:50:50 +0000 (15:50 +0800)
committerJohn Hodge <[email protected]>
Sun, 21 Nov 2010 07:50:50 +0000 (15:50 +0800)
- Added support for setting interfaces from the kernel boot line

Kernel/include/acess.h
Kernel/lib.c
Modules/Filesystems/Ext2/ext2.c
Modules/IPStack/interface.c
Modules/IPStack/link.c
Modules/IPStack/main.c
Usermode/Applications/ifconfig_src/main.c

index 9337567..779cf88 100644 (file)
@@ -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);
 /**
  * \}
  */
index 66c9660..c2c1b58 100644 (file)
@@ -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;
+}
index 4a2aece..52793d0 100644 (file)
@@ -73,7 +73,8 @@ tVFS_Node *Ext2_InitDevice(char *Device, char **Options)
        \r
        // Sanity Check Magic value\r
        if(sb.s_magic != 0xEF53) {\r
-               Log_Warning("EXT2", "Volume '%s' is not an EXT2 volume", Device);\r
+               Log_Warning("EXT2", "Volume '%s' is not an EXT2 volume (0x%x != 0xEF53)",\r
+                       Device, sb.s_magic);\r
                VFS_Close(fd);\r
                LEAVE('n');\r
                return NULL;\r
index c6f55ba..50d4c22 100644 (file)
@@ -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;
 }
 
 /**
index afd54eb..07234df 100644 (file)
@@ -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;
                
index 8e3810d..6dac938 100644 (file)
@@ -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 <Device>,<Type>,<HexStreamAddress>,<Bits>
+                       // Define interfaces by <Device>:<Type>:<HexStreamAddress>:<Bits>
                        // Where:
                        // - <Device> is the device path (E.g. /Devices/ne2k/0)
                        // - <Type> is a number (e.g. 4) or symbol (e.g. AF_INET4)
                        // - <HexStreamAddress> is a condensed hexadecimal stream (in big endian)
                        //      (E.g. 0A000201 for 10.0.2.1 IPv4)
                        // - <Bits> 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", "<Device>:<Type>:<HexStreamAddress>:<Bits>");
+                                       continue;
+                               }
+                               *type = '\0';   type ++;
+                               
+                               addr = strchr(type, ':');
+                               if( !addr ) {
+                                       Log_Warning("IPStack", "<Device>:<Type>:<HexStreamAddress>:<Bits>");
+                                       continue;
+                               }
+                               *addr = '\0';   addr ++;
+                               
+                               bits = strchr(addr, ':');
+                               if( !bits ) {
+                                       Log_Warning("IPStack", "<Device>:<Type>:<HexStreamAddress>:<Bits>");
+                                       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 <Interface>,<HexStreamNetwork>,<Bits>,<HexStreamGateway>
-                       // Example: 1,00000000,0,0A000201
+                       // Example: 1:00000000:0:0A000201
                }
        }
        
index 8d97d1b..3a45530 100644 (file)
@@ -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);
 }

UCC git Repository :: git.ucc.asn.au