Cleaning up some debug, fixing cosmetic bugs in usermode apps
authorJohn Hodge <[email protected]>
Tue, 13 Jul 2010 14:38:53 +0000 (22:38 +0800)
committerJohn Hodge <[email protected]>
Tue, 13 Jul 2010 14:38:53 +0000 (22:38 +0800)
Modules/IPStack/arp.c
Modules/IPStack/ipv4.c
Modules/IPStack/link.c
Usermode/Applications/Makefile.tpl
Usermode/Applications/ifconfig_src/main.c
Usermode/Applications/mount_src/main.c

index cedc0b3..d9f837c 100644 (file)
@@ -221,6 +221,7 @@ void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffe
                Log_Log("ARP", "Recieved a packet with HWSize != 6 (%i)", req4->HWSize);
                return;
        }
+       #if ARP_DETECT_SPOOFS
        if( !MAC_EQU(req4->SourceMac, From) ) {
                Log_Log("ARP", "ARP spoofing detected "
                        "(%02x%02x:%02x%02x:%02x%02x != %02x%02x:%02x%02x:%02x%02x)",
@@ -231,35 +232,36 @@ void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffe
                        );
                return;
        }
+       #endif
        
-       Log("[ARP  ] Request ID %i", ntohs(req4->Request));
+       Log_Debug("ARP", "Request ID %i", ntohs(req4->Request));
        
        switch( ntohs(req4->Request) )
        {
        case 1: // You want my IP?
-               Log("[ARP  ] ARP Request Address class %i", req4->SWSize);
+               Log_Debug("ARP", "ARP Request Address class %i", req4->SWSize);
                // Check what type of IP it is
                switch( req4->SWSize )
                {
                case 4:
-                       Log("[ARP  ] From MAC %02x:%02x:%02x:%02x:%02x:%02x",
+                       Log_Debug("ARP", "From MAC %02x:%02x:%02x:%02x:%02x:%02x",
                                req4->SourceMac.B[0], req4->SourceMac.B[1],
                                req4->SourceMac.B[2], req4->SourceMac.B[3],
                                req4->SourceMac.B[4], req4->SourceMac.B[5]);
-                       Log("[ARP  ] to MAC %02x:%02x:%02x:%02x:%02x:%02x",
+                       Log_Debug("ARP", "to MAC %02x:%02x:%02x:%02x:%02x:%02x",
                                req4->DestMac.B[0], req4->DestMac.B[1],
                                req4->DestMac.B[2], req4->DestMac.B[3],
                                req4->DestMac.B[4], req4->DestMac.B[5]);
-                       Log("[ARP  ] ARP Request IPv4 Address %i.%i.%i.%i",
+                       Log_Debug("ARP", "ARP Request IPv4 Address %i.%i.%i.%i",
                                req4->DestIP.B[0], req4->DestIP.B[1], req4->DestIP.B[2],
                                req4->DestIP.B[3]);
-                       Log("[ARP  ] from %i.%i.%i.%i",
+                       Log_Debug("ARP", "from %i.%i.%i.%i",
                                req4->SourceIP.B[0], req4->SourceIP.B[1],
                                req4->SourceIP.B[2], req4->SourceIP.B[3]);
                        iface = IPv4_GetInterface(Adapter, req4->DestIP, 0);
                        if( iface )
                        {
-                               Log("[ARP  ] Caching sender's IP Address");
+                               Log_Debug("ARP", "Caching sender's IP Address");
                                ARP_UpdateCache4(req4->SourceIP, req4->SourceMac);
                                
                                req4->DestIP = req4->SourceIP;
@@ -267,8 +269,8 @@ void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffe
                                req4->SourceIP = iface->IP4.Address;
                                req4->SourceMac = Adapter->MacAddr;
                                req4->Request = htons(2);
-                               Log("[ARP  ] Hey, That's us!");
-                               Log("[ARP  ] Sending back %02x:%02x:%02x:%02x:%02x:%02x",
+                               Log_Debug("ARP", "Hey, That's us!");
+                               Log_Debug("ARP", "Sending back %02x:%02x:%02x:%02x:%02x:%02x",
                                        req4->SourceMac.B[0], req4->SourceMac.B[1],
                                        req4->SourceMac.B[2], req4->SourceMac.B[3],
                                        req4->SourceMac.B[4], req4->SourceMac.B[5]);
@@ -277,7 +279,7 @@ void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffe
                        break;
                case 6:
                        if( Length < sizeof(tArpRequest6) ) {
-                               Log("[ARP  ] Recieved undersized packet (IPv6)");
+                               Log_Debug("ARP", "Recieved undersized packet (IPv6)");
                                return ;
                        }
                        iface = IPv6_GetInterface(Adapter, req6->DestIP, 0);
@@ -292,7 +294,7 @@ void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffe
                        }
                        break;
                default:
-                       Log("[ARP  ] Unknown Protocol Address size (%i)", req4->SWSize);
+                       Log_Debug("ARP", "Unknown Protocol Address size (%i)", req4->SWSize);
                        return ;
                }
                
@@ -307,13 +309,13 @@ void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffe
                        break;
                case 6:
                        if( Length < sizeof(tArpRequest6) ) {
-                               Log("[ARP  ] Recieved undersized packet (IPv6)");
+                               Log_Debug("ARP", "Recieved undersized packet (IPv6)");
                                return ;
                        }
                        ARP_UpdateCache6( req6->SourceIP, From );
                        break;
                default:
-                       Log("[ARP  ] Unknown Protocol Address size (%i)", req4->SWSize);
+                       Log_Debug("ARP", "Unknown Protocol Address size (%i)", req4->SWSize);
                        return ;
                }
                
index a1b9178..aff593a 100644 (file)
@@ -217,8 +217,6 @@ Uint16 IPv4_Checksum(void *Buf, int Size)
        Uint16  *arr = Buf;
         int    i;
        
-       Log("IPv4_Checksum: (%p, %i)", Buf, Size);
-       
        Size = (Size + 1) >> 1;
        for(i = 0; i < Size; i++ )
        {
index d914570..ef17c93 100644 (file)
@@ -162,6 +162,8 @@ void Link_WatchDevice(tAdapter *Adapter)
        }
        
        Log_Log("NET", "Watcher terminated (file closed)");
+       
+       Threads_Exit(0, 0);
 }
 
 // From http://www.cl.cam.ac.uk/research/srg/bluebook/21/crc/node6.html
index 530054b..aadfbdb 100644 (file)
@@ -2,7 +2,7 @@
 #
 #
 
-CFLAGS  += -Wall -fno-builtin -fno-stack-protector -g
+CFLAGS  += -Wall -Werror -fno-builtin -fno-stack-protector -g
 LDFLAGS += 
 
 DEPFILES := $(OBJ:%.o=%.d)
index 1047ca3..ca9b73b 100644 (file)
 #define IPSTACK_ROOT   "/Devices/ip"
 
 // === PROTOTYPES ===
-void   PrintUsage(char *ProgName);
-void   DumpInterfaces( int DumpAll );
- int   AddInterface( char *Address );
- int   DoAutoConfig( char *Device );
+void   PrintUsage(const char *ProgName);
+void   DumpInterfaces(void);
+void   DumpInterface(const char *Name);
+ int   AddInterface(const char *Device);
+ int   DoAutoConfig(const char *Device);
 
 // === CODE ===
 /**
- * \fn int main(int argc, char *argv[])
- * \brief Entrypoint
+ * \brief Program entrypoint
  */
 int main(int argc, char *argv[])
 {
+       // No args, dump interfaces
        if(argc == 1) {
-               DumpInterfaces(0);
+               DumpInterfaces();
                return 0;
        }
        
+       // Add a new interface
        if( strcmp(argv[1], "add") == 0 ) {
-               if( argc < 3 ) {
-                       fprintf(stderr, "ERROR: `add` requires an argument\n");
+               if( argc < 4 ) {
+                       fprintf(stderr, "ERROR: %s add require two arguments, %i passed\n", argv[0], argc-2);
                        PrintUsage(argv[0]);
                        return 0;
                }
+               // TODO: Also set the IP address as the usage says it does
                return AddInterface( argv[2] );
        }
        
+       // Autoconfigure an interface
+       // NOTE: Debugging hack (see the function for more details)
        if( strcmp(argv[1], "autoconf") == 0 ) {
                DoAutoConfig(argv[2]);
                return 0;
        }
        
+       // Print usage instructions
        PrintUsage(argv[0]);
        
        return 0;
 }
 
-void PrintUsage(char *ProgName)
+/**
+ * \brief Print usage instructions
+ */
+void PrintUsage(const char *ProgName)
 {
-       fprintf(stderr, "Usage: %s [add <ipaddr>]\n", ProgName);
+       fprintf(stderr, "Usage:\n");
+       fprintf(stderr, "    %s add <device> <ip>/<prefix>\n", ProgName);
+       fprintf(stderr, "        Add a new interface listening on <device> with the specified\n");
+       fprintf(stderr, "        address.\n");
+       fprintf(stderr, "    %s del <interface>\n", ProgName);
+       fprintf(stderr, "    %s set <interface> <option> <value>\n", ProgName);
+       fprintf(stderr, "        Set an option on an interface, a list of valid options follows\n");
+       fprintf(stderr, "        gw      IPv4 default gateway\n");
+       fprintf(stderr, "    %s [<interface>]\n", ProgName);
+       fprintf(stderr, "        Print the current interfaces (or only <interface> if passed)\n");
+       fprintf(stderr, "\n");
+       fprintf(stderr, "A note on Acess's IP Stack:\n");
+       fprintf(stderr, "    Each interface corresponds to only one IP address (either IPv4\n");
+       fprintf(stderr, "    or IPv6). A network device can have multiple interfaces bound\n");
+       fprintf(stderr, "    to it, allowing multiple addresses for one network connection\n");
+       fprintf(stderr, "\n");
 }
 
-void DumpInterfaces(int DumpAll)
+/**
+ * \brief Dump all interfaces
+ */
+void DumpInterfaces(void)
 {
-        int    dp, fd;
-        int    type;
-       char    path[sizeof(IPSTACK_ROOT)+1+FILENAME_MAX+1] = IPSTACK_ROOT"/";
-       char    *filename = &path[sizeof(IPSTACK_ROOT)];
+        int    dp;
+       char    filename[FILENAME_MAX+1];
        
        dp = open(IPSTACK_ROOT, OPENFLAG_READ);
        
        while( readdir(dp, filename) )
        {
                if(filename[0] == '.')  continue;
-               
-               fd = open(path, OPENFLAG_READ);
-               if(fd == -1) {
-                       printf("%s:\tUnable to open ('%s')\n", filename, path);
-                       continue ;
-               }
-               
-               type = ioctl(fd, 4, NULL);
-               
-               printf("%s:\t", filename);
+               DumpInterface(filename);
+       }
+       
+       close(dp);
+}
+
+/**
+ * \brief Dump an interface
+ */
+void DumpInterface(const char *Name)
+{
+        int    fd;
+        int    type;
+       char    path[sizeof(IPSTACK_ROOT)+1+FILENAME_MAX+1] = IPSTACK_ROOT"/";
+       
+       strcat(path, Name);
+       
+       fd = open(path, OPENFLAG_READ);
+       if(fd == -1) {
+               printf("%s:\tUnable to open ('%s')\n", Name, path);
+               return ;
+       }
+       
+       type = ioctl(fd, 4, NULL);
+       
+       printf("%s:\t", Name);
+       {
+                int    call_num = ioctl(fd, 3, "get_device");
+                int    len = ioctl(fd, call_num, NULL);
+               char    *buf = malloc(len+1);
+               ioctl(fd, call_num, buf);
+               printf("'%s'\t", buf);
+               free(buf);
+       }
+       // Get the address type
+       switch(type)
+       {
+       case 0: // Disabled/Unset
+               printf("DISABLED\n");
+               break;
+       case 4: // IPv4
                {
-                       int     call = ioctl(fd, 3, "get_device");
-                       int len = ioctl(fd, call, NULL);
-                       char *buf = malloc(len+1);
-                       ioctl(fd, call, buf);
-                       printf("'%s'\t", buf);
-                       free(buf);
+               uint8_t ip[4];
+                int    subnet;
+               printf("IPv4\n");
+               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);
+               ioctl(fd, 8, ip);       // Get Gateway
+               printf("\tGateway: %i.%i.%i.%i\n", ip[0], ip[1], ip[2], ip[3]);
                }
-               switch(type)
+               break;
+       case 6: // IPv6
                {
-               case 0:
-                       printf("DISABLED\n");
-                       break;
-               case 4:
-                       {
-                       uint8_t ip[4];
-                        int    subnet;
-                       printf("IPv4\n");
-                       ioctl(fd, 5, ip);       // Get IP Address
-                       subnet = ioctl(fd, 7, NULL);    // Get Subnet Bits
-                       printf("\t%i.%i.%i.%i/%i\n", ip[0], ip[1], ip[2], ip[3], subnet);
-                       ioctl(fd, 8, ip);       // Get Gateway
-                       printf("\tGateway: %i.%i.%i.%i\n", ip[0], ip[1], ip[2], ip[3]);
-                       }
-                       break;
-               case 6:
-                       {
-                       uint16_t        ip[8];
-                        int    subnet;
-                       printf("IPv6\n");
-                       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",
-                               ip[0], ip[1], ip[2], ip[3],
-                               ip[4], ip[5], ip[6], ip[7],
-                               subnet);
-                       }
-                       break;
-               default:
-                       printf("UNKNOWN\n");
-                       break;
+               uint16_t        ip[8];
+                int    subnet;
+               printf("IPv6\n");
+               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",
+                       ip[0], ip[1], ip[2], ip[3],
+                       ip[4], ip[5], ip[6], ip[7],
+                       subnet);
                }
-               printf("\n");
-                       
-               close(fd);
+               break;
+       default:        // Unknow
+               printf("UNKNOWN (%i)\n", type);
+               break;
        }
-       
-       close(dp);
+       printf("\n");
+                       
+       close(fd);
 }
 
-int AddInterface( char *Device )
+/**
+ * \brief Create a new interface using the passed device
+ * \param Device       Network device to bind to
+ */
+int AddInterface(const char *Device)
 {
         int    dp, ret;
        
        dp = open(IPSTACK_ROOT, OPENFLAG_READ);
-       ret = ioctl(dp, 4, Device);
+       ret = ioctl(dp, 4, (void*)Device);
        printf("AddInterface: ret = 0x%x = %i\n", ret, ret);
        close(dp);
        
@@ -143,7 +186,12 @@ int AddInterface( char *Device )
        return ret;
 }
 
-int DoAutoConfig( char *Device )
+/**
+ * \note Debugging HACK!
+ * \brief Autoconfigure the specified device to 10.0.2.55/8 using
+ *        10.0.2.1 as the gateway.
+ */
+int DoAutoConfig(const char *Device)
 {
         int    tmp, fd;
        char    path[sizeof(IPSTACK_ROOT)+5+1]; // ip000
index 1608108..5bee7c6 100644 (file)
@@ -9,7 +9,7 @@
 #define        MOUNTED_FILE    "/Devices/System/VFS/Mounts"
 
 // === PROTOTYPES ===
-void   ShowUsage();
+void   ShowUsage(char *ProgName);
  int   GetMountDefs(char *Ident, char **spDevice, char **spDir, char **spType, char **spOptions);
 
 // === CODE ===
@@ -27,8 +27,21 @@ int main(int argc, char *argv[])
        char    *sDir = NULL;
        char    *sOptions = NULL;
 
+       // List mounted filesystems
+       // - This is cheating, isn't it?
+       if(argc == 1) {
+               // Dump the contents of /Devices/system/VFS/Mounts
+               FILE    *fp = fopen("/Devices/system/VFS/Mounts", "r");
+               char    buf[1024];
+                int    len;
+               while( (len = fread(buf, 1024, 1, fp)) )
+                       fwrite(buf, len, 1, stdout);
+               printf("\n");
+               return 0;
+       }
+
        if(argc < 3) {
-               ShowUsage();
+               ShowUsage(argv[0]);
                return EXIT_FAILURE;
        }
        
@@ -133,10 +146,11 @@ int main(int argc, char *argv[])
 
 void ShowUsage(char *ProgName)
 {
-       fprintf(stderr, "Usage:\n", ProgName);
-       fprintf(stderr, "    %s [-t <type>] <device> <directory>\n");
-       fprintf(stderr, "or  %s <device>\n");
-       fprintf(stderr, "or  %s <directory>\n");
+       fprintf(stderr, "Usage:\n");
+       fprintf(stderr, "    %s [-t <type>] <device> <directory>\n", ProgName);
+       fprintf(stderr, "or  %s <device>\n", ProgName);
+       fprintf(stderr, "or  %s <directory>\n", ProgName);
+       fprintf(stderr, "or  %s\n", ProgName);
 }
 
 /**

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