- 论坛徽章:
- 0
|
原先的测试代码有一个小bug,
- if( ioctl(fd, LWFW_GET_STATS,*(unsigned long*)&data) == -1)
复制代码
中的 *(unsigned long*)&data)不对,应该为(unsigned long*)&data),去掉*号,这样才是一个指针。
改了一下测试的代码:
首先,激活模块。
lwfwtables -a
然后设置条件
lwfwtables -i 192.168.18.5 设置ip
lwfwtables -g 获取信息
同样使用
lwfwtables -p 22 设置端口
lwfwtables -f eth0 设置网卡
- #include<sys/types.h>
- #include<unistd.h>
- #include<fcntl.h>
- #include<linux/rtc.h>
- #include<linux/ioctl.h>
- #include<stdio.h>
- #include<stdlib.h>
- #include<arpa/inet.h>
- #include "lwfw.h"
- void print_help(void);
- int main(int argc,char *argv[])
- {
- int fd;
- unsigned short port = 0;
- char ch;
- struct lwfw_stats data;
- if (argc < 2)
- {
- print_help();
- return 1;
- }
- fd = open (LWFW_NAME, O_RDONLY);
- if (fd == -1)
- {
- perror ("open LWFW_NAME fail\n");
- goto error;
- }
- while ( (ch=getopt(argc,argv,"adi:p:f:g")) != EOF)
- {
- switch (ch)
- {
- case 'a': //Active
- if (ioctl (fd, LWFW_ACTIVATE, 0) == -1)
- {
- perror ("ioctl LWFW_ACTIVATE fail!\n");
- goto error;
- }
- break;
- case 'd': //Deactive
- if (ioctl (fd, LWFW_DEACTIVATE, 0) == -1)
- {
- perror ("ioctl LWFW_ACTIVATE fail!\n");
- goto error;
- }
- break;
- case 'i': //IP
- if (ioctl (fd, LWFW_DENY_IP, inet_addr (optarg)) == -1)
- {
- printf ("ioctl LWFW_DENY_IP fail\n");
- goto error;
- }
- break;
- case 'p': //Port
- port=(unsigned short)atoi(optarg);
- port=htons(port);
- if (ioctl (fd, LWFW_DENY_PORT,port) == -1)
- {
- printf ("ioctl LWFW_DENY_PORT fail!\n");
- goto error;
- }
- break;
- case 'f': //Interface
- if (ioctl(fd, LWFW_DENY_IF, optarg) == -1)
- {
- printf("ioctl LWFW_DENY_IF fail!\n");
- goto error;
- }
- break;
- case 'g': //Get Stats
- if (ioctl (fd, LWFW_GET_STATS, (unsigned long *) &data) == -1)
- {
- printf ("iotcl LWFW_GET_STATS fail!\n");
- goto error;
- }
- printf ("if dropped : %u\n", data.if_dropped);
- printf ("ip dropped : %u\n", data.ip_dropped);
- printf ("tcp dropped : %u\n", data.tcp_dropped);
- printf ("total dropped : %lu\n", data.total_dropped);
- printf ("total seen: %lu\n", data.total_seen);
- break;
- default:
- print_help();
- return 1;
- }
- }
- close (fd);
- return 0;
- error:
- close (fd);
- return 1;
- }
- void print_help()
- {
- printf("lwfwtables -[a|d|g] -[i:ip | f:NIC | o:Port]\n");
- printf("Note: You Should Run lwfwtables -a First to active lwfw.\n");
- }
复制代码
[ 本帖最后由 CUDev 于 2007-8-7 22:15 编辑 ] |
|