libipt_ACCOUNT, ipt_ACCOUNT: (tomj) implemented handle usage, free all handles and...
authorThomas Jarosch <thomas.jarosch@intra2net.com>
Mon, 12 Apr 2004 17:33:00 +0000 (17:33 +0000)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Mon, 12 Apr 2004 17:33:00 +0000 (17:33 +0000)
                                    Fixed one memory free issue causing a kernel oops

iptaccount/iptaccount.c
libipt_account.kdevelop.pcs
libipt_account.kdevses
src/ipt_ACCOUNT_cl.c
src/ipt_ACCOUNT_cl.h

index 7af5955..f8cc97f 100644 (file)
@@ -44,7 +44,7 @@ void show_usage(void)
     printf ("Unknown command line option. Try: [-u] [-h] [-a] [-f] [-l name]\n");
     printf("[-u] show kernel handle usage\n");
     printf("[-h] free all kernel handles (experts only!)\n");
-    printf("[-u] list all table names\n\n");
+    printf("[-a] list all table names\n\n");
     printf("[-l name] show table data\n");
     printf("[-f] flush data after show\n");
     printf("[-c] loop every second (abort with CTRL+C)\n");
@@ -56,15 +56,9 @@ int main(int argc, char *argv[])
     struct ipt_ACCOUNT_context ctx;
     struct ipt_account_handle_ip *entry;
     int i;
-    char optchar, doHandleUsage=0, doHandleFree=0, doListAll=0, doFlush=0, doContinue=0;
+    char optchar, doHandleUsage=0, doHandleFree=0, doTableNames=0, doFlush=0, doContinue=0;
     char *table_name = NULL;
     
-    // Show handle use count
-    // Free all handles
-    // List all tables
-    // do flush
-    // list TABLENAME
-    
     printf("\nipt_ACCOUNT userspace accounting tool v%s\n\n", VERSION);
 
     if (argc == 1)
@@ -84,7 +78,7 @@ int main(int argc, char *argv[])
                 doHandleFree=1;
                 break;
             case 'a':
-                doListAll=1;
+                doTableNames=1;
                 break;
             case 'f':
                 doFlush=1;
@@ -127,36 +121,77 @@ int main(int argc, char *argv[])
         exit (-1);
     }
 
-    // Read out data
-    printf("Showing table: %s\n", table_name);
-    i = 0;
-    while (!exit_now)
+    // Get handle usage?
+    if (doHandleUsage)
     {
-        // Get entries from table test
-        if (ipt_ACCOUNT_read_entries(&ctx, table_name, !doFlush))
+        int rtn = ipt_ACCOUNT_get_handle_usage(&ctx);
+        if (rtn < 0)
         {
-            printf("Read failed: %s\n", ctx.error_str);
-            ipt_ACCOUNT_deinit(&ctx);
+            printf("get_handle_usage failed: %s\n", ctx.error_str);
             exit (-1);
         }
         
-        printf("Run #%d - %u %s found\n", i, ctx.handle.itemcount, ctx.handle.itemcount == 1 ? "item" : "items");
-        
-        // Output and free entries
-        while ((entry = ipt_ACCOUNT_get_next_entry(&ctx)) != NULL)
+        printf("Current kernel handle usage: %d\n", ctx.handle.itemcount);
+    }
+    
+    if (doHandleFree)
+    {
+        int rtn = ipt_ACCOUNT_free_all_handles(&ctx);
+        if (rtn < 0)
         {
-            printf("IP: %s SRC packets: %u bytes: %u DST packets: %u bytes: %u\n",
-                   addr_to_dotted(entry->ip), entry->src_packets, entry->src_bytes, entry->dst_packets, entry->dst_bytes);
+            printf("handle_free_all failed: %s\n", ctx.error_str);
+            exit (-1);
         }
         
-        if (doContinue)
+        printf("Freed all handles in kernel space\n");
+    }
+
+    if (doTableNames)
+    {
+        int rtn = ipt_ACCOUNT_get_table_names(&ctx);
+        if (rtn < 0)
         {
-            sleep(1);
-            i++;
-        } else
-            exit_now = 1;
+            printf("get_table_names failed: %s\n", ctx.error_str);
+            exit (-1);
+        }
+        char *name;
+        while ((name = ipt_ACCOUNT_get_next_name(&ctx)) != NULL)
+            printf("Found table: %s\n", name);
     }
+        
+    if (table_name)
+    {
+        // Read out data
+        printf("Showing table: %s\n", table_name);
+        i = 0;
+        while (!exit_now)
+        {
+            // Get entries from table test
+            if (ipt_ACCOUNT_read_entries(&ctx, table_name, !doFlush))
+            {
+                printf("Read failed: %s\n", ctx.error_str);
+                ipt_ACCOUNT_deinit(&ctx);
+                exit (-1);
+            }
             
+            printf("Run #%d - %u %s found\n", i, ctx.handle.itemcount, ctx.handle.itemcount == 1 ? "item" : "items");
+            
+            // Output and free entries
+            while ((entry = ipt_ACCOUNT_get_next_entry(&ctx)) != NULL)
+            {
+                printf("IP: %s SRC packets: %u bytes: %u DST packets: %u bytes: %u\n",
+                    addr_to_dotted(entry->ip), entry->src_packets, entry->src_bytes, entry->dst_packets, entry->dst_bytes);
+            }
+            
+            if (doContinue)
+            {
+                sleep(1);
+                i++;
+            } else
+                exit_now = 1;
+        }
+    }            
+        
     printf("Finished.\n");
     ipt_ACCOUNT_deinit(&ctx);
     exit (0);
index 56662aa..5a58478 100644 (file)
Binary files a/libipt_account.kdevelop.pcs and b/libipt_account.kdevelop.pcs differ
index 7b53e0e..21d235b 100644 (file)
@@ -1,27 +1,17 @@
 <?xml version = '1.0' encoding = 'UTF-8'?>
 <!DOCTYPE KDevPrjSession>
 <KDevPrjSession>
- <DocsAndViews NumberOfDocuments="4" >
+ <DocsAndViews NumberOfDocuments="2" >
   <Doc0 NumberOfViews="1" URL="file:/nfsroot/home/tomj/intranator/libipt_ACCOUNT/iptaccount/iptaccount.c" >
-   <View0 line="0" Type="???" >
-    <AdditionalSettings Top="1" Width="1203" Attach="1" Height="767" Left="1" MinMaxMode="0" />
+   <View0 line="37" Type="???" >
+    <AdditionalSettings Top="1" Width="1203" Attach="1" Height="770" Left="1" MinMaxMode="0" />
    </View0>
   </Doc0>
-  <Doc1 NumberOfViews="1" URL="file:/nfsroot/home/tomj/intranator/libipt_ACCOUNT/src/ipt_ACCOUNT_cl.c" >
-   <View0 line="6" Type="???" >
-    <AdditionalSettings Top="1" Width="1203" Attach="1" Height="772" Left="1" MinMaxMode="0" />
-   </View0>
-  </Doc1>
-  <Doc2 NumberOfViews="1" URL="file:/nfsroot/home/tomj/intranator/libipt_ACCOUNT/src/ipt_ACCOUNT_cl.h" >
-   <View0 line="4" Type="???" >
-    <AdditionalSettings Top="1" Width="1205" Attach="1" Height="770" Left="1" MinMaxMode="0" />
-   </View0>
-  </Doc2>
-  <Doc3 NumberOfViews="1" URL="file:/nfsroot/home/tomj/intranator/libipt_ACCOUNT/libipt_ACCOUNT.pc.in" >
-   <View0 line="10" Type="???" >
+  <Doc1 NumberOfViews="1" URL="file:/nfsroot/home/tomj/intranator/example.c" >
+   <View0 line="0" Type="???" >
     <AdditionalSettings Top="1" Width="1203" Attach="1" Height="770" Left="1" MinMaxMode="0" />
    </View0>
-  </Doc3>
+  </Doc1>
  </DocsAndViews>
  <pluginList>
   <kdevbookmarks>
index 1daf35e..d773b3d 100644 (file)
@@ -139,3 +139,49 @@ struct ipt_account_handle_ip *ipt_ACCOUNT_get_next_entry(struct ipt_ACCOUNT_cont
             
     return rtn;
 }
+
+int ipt_ACCOUNT_get_handle_usage(struct ipt_ACCOUNT_context *ctx)
+{
+    unsigned int s = sizeof (struct ipt_account_handle_sockopt);
+    if (getsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_GET_ACCOUNT_GET_HANDLE_USAGE, &ctx->handle, &s) < 0)
+    {
+        ctx->error_str = "Can't get handle usage information from kernel";
+        return -1;
+    }
+    
+    return ctx->handle.itemcount;
+ }
+    
+int ipt_ACCOUNT_free_all_handles(struct ipt_ACCOUNT_context *ctx)
+{
+    if (setsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_SET_ACCOUNT_HANDLE_FREE_ALL, NULL, 0) < 0)
+    {
+        ctx->error_str = "Can't free all kernel handles";
+        return -1;
+    }
+    
+    return 0;
+}
+    
+int ipt_ACCOUNT_get_table_names(struct ipt_ACCOUNT_context *ctx)
+{
+    int rtn = getsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_GET_ACCOUNT_GET_TABLE_NAMES, ctx->data, &ctx->data_size);
+    if (rtn < 0)
+    {
+        ctx->error_str = "Can't get table names from kernel. Out of memory, MINBUFISZE too small?";
+        return -1;
+    }
+    ctx->pos = 0;
+    return 0;
+}
+
+char *ipt_ACCOUNT_get_next_name(struct ipt_ACCOUNT_context *ctx)
+{
+    if (((char *)ctx->data)[ctx->pos] == NULL)
+        return NULL;
+
+    char *rtn = ctx->data + ctx->pos;
+    ctx->pos += strlen(ctx->data+ctx->pos) + 1;
+        
+    return rtn;
+}
index e753da7..b167f1d 100644 (file)
@@ -40,6 +40,11 @@ extern "C" {
     /* ipt_ACCOUNT_free_entries is for internal use only function as this library
     is constructed to be used in a loop -> Don't allocate memory all the time.
     The data buffer is freed on deinit() */
+    
+    int ipt_ACCOUNT_get_handle_usage(struct ipt_ACCOUNT_context *ctx);
+    int ipt_ACCOUNT_free_all_handles(struct ipt_ACCOUNT_context *ctx);
+    int ipt_ACCOUNT_get_table_names(struct ipt_ACCOUNT_context *ctx);
+    char *ipt_ACCOUNT_get_next_name(struct ipt_ACCOUNT_context *ctx);
 
 #ifdef __cplusplus
 }