2 #include <sys/socket.h>
3 #include <netinet/in.h>
5 #include <ipt_ACCOUNT_cl.h>
7 int ipt_ACCOUNT_init(struct ipt_ACCOUNT_context *ctx)
9 memset (ctx, 0, sizeof(struct ipt_ACCOUNT_context));
11 ctx->sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
15 ctx->error_str = "Can't open socket to kernel. Permission denied or ipt_ACCOUNT module not loaded";
19 // 4096 bytes default buffer should save us from reallocations
20 // as it fits 200 concurrent active clients
21 if((ctx->data = (void *)malloc(IPT_ACCOUNT_MIN_BUFSIZE)) == NULL)
25 ctx->error_str = "Out of memory for data buffer";
28 ctx->data_size = IPT_ACCOUNT_MIN_BUFSIZE;
33 void ipt_ACCOUNT_free_entries(struct ipt_ACCOUNT_context *ctx)
35 if (ctx->handle.handle_nr >= 0)
37 setsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_SET_ACCOUNT_HANDLE_FREE, &ctx->handle, sizeof (struct ipt_account_handle_sockopt));
38 ctx->handle.handle_nr = -1;
41 ctx->handle.itemcount = 0;
45 void ipt_ACCOUNT_deinit(struct ipt_ACCOUNT_context *ctx)
50 ipt_ACCOUNT_free_entries(ctx);
56 int ipt_ACCOUNT_read_entries(struct ipt_ACCOUNT_context *ctx, char *table, char dont_flush)
58 unsigned int s = sizeof (struct ipt_account_handle_sockopt);
61 strncpy(ctx->handle.name, table, ACCOUNT_TABLE_NAME_LEN-1);
63 // Get table information
65 rtn = getsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH, &ctx->handle, &s);
67 rtn = getsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_GET_ACCOUNT_PREPARE_READ, &ctx->handle, &s);
71 ctx->error_str = "Can't get table information from kernel. Is the table existing?";
75 // Check data buffer size
77 unsigned int new_size = ctx->handle.itemcount * sizeof(struct ipt_account_handle_ip);
78 // We want to prevent reallocations all the time
79 if (new_size < IPT_ACCOUNT_MIN_BUFSIZE)
80 new_size = IPT_ACCOUNT_MIN_BUFSIZE;
82 // Reallocate if it's too small or twice as big
83 if (ctx->data_size < new_size || ctx->data_size > new_size*2)
89 if ((ctx->data = (void*)malloc(new_size)) == NULL)
91 ctx->error_str = "Out of memory for data buffer";
92 ipt_ACCOUNT_free_entries(ctx);
96 ctx->data_size = new_size;
99 // Copy data from kernel
100 memcpy(ctx->data, &ctx->handle, sizeof(struct ipt_account_handle_sockopt));
101 rtn = getsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_GET_ACCOUNT_GET_DATA, ctx->data, &ctx->data_size);
104 ctx->error_str = "Can't get data from kernel. Check /var/log/messages for details.";
105 ipt_ACCOUNT_free_entries(ctx);
109 // Free kernel handle but don't reset pos/itemcount
110 setsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_SET_ACCOUNT_HANDLE_FREE, &ctx->handle, sizeof (struct ipt_account_handle_sockopt));
111 ctx->handle.handle_nr = -1;
116 struct ipt_account_handle_ip *ipt_ACCOUNT_get_next_entry(struct ipt_ACCOUNT_context *ctx)
118 struct ipt_account_handle_ip *rtn;
120 // Empty or no more items left to return?
121 if (!ctx->handle.itemcount || ctx->pos >= ctx->handle.itemcount)
125 rtn = (struct ipt_account_handle_ip *)(ctx->data + ctx->pos*sizeof(struct ipt_account_handle_ip));
131 char *addr_to_dotted(unsigned int addr)
134 const unsigned char *bytep;
136 bytep = (const unsigned char *) &addr;
137 sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]);
143 struct ipt_ACCOUNT_context ctx;
144 struct ipt_account_handle_ip *entry;
147 if(ipt_ACCOUNT_init(&ctx))
149 printf("Init failed: %s\n", ctx.error_str);
153 for (i = 0; i < 3; i++)
155 printf("Run #%d\n", i);
157 // Get entries from table test
158 if (ipt_ACCOUNT_read_entries(&ctx, "test", 0))
160 printf("Read failed: %s\n", ctx.error_str);
161 ipt_ACCOUNT_deinit(&ctx);
165 // Output and free entries
166 while ((entry = ipt_ACCOUNT_get_next_entry(&ctx)) != NULL)
168 printf("IP: %s SRC packets: %u bytes: %u DST packets: %u bytes: %u\n",
169 addr_to_dotted(entry->ip), entry->src_packets, entry->src_bytes, entry->dst_packets, entry->dst_bytes);
174 ipt_ACCOUNT_deinit(&ctx);