- 论坛徽章:
- 0
|
我按照Godbach兄的代码,写了一个关于TCP包计数的程序,只是单纯记录TCP包的数量,
在下载小容量文件<=0.5k左右时可以实现很快的内核用户态交互通信,
但略微大一点的文件1.5k左右就不可以了...或者说效率太低,文件下载不了,想请教下原因
还有在linux里面对文件进行些操作用write(),它所需要的头文件是fctnl.h?
如果是,为什么我编译的时候说 write未定义?
谢谢
代码如下 :
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <signal.h>
- #include <netinet/tcp.h>
- #include <netinet/ip.h>
- #include <arpa/inet.h>
- #include "libipq.h"
- #include <fcntl.h>
- #include </home/kide/clamav/libclamav/clamav.h>
- #define ETH_HDRLEN 14
- #define NF_ACCEPT 1
- #define NF_DROP 0
- struct ipq_handle *h = NULL;
- static void sig_int(int signo)
- {
- ipq_destroy_handle(h);
- printf("Exit: %s\n", ipq_errstr());
- exit(0);
- }
- int main(void)
- {
- unsigned char buf[1024];
- /* creat handle*/
- h = ipq_create_handle(0, PF_INET);
- if(h == NULL){
- printf("%s\n", ipq_errstr());
- return 0;
- }
- printf("ipq_creat_handle success!\n");
- /*set mode*/
- unsigned char mode = IPQ_COPY_PACKET;
- int range = sizeof(buf);
- int ret = ipq_set_mode(h, mode, range);
- printf("ipq_set_mode: send bytes =%d, range=%d\n", ret, range);
-
- /*register signal handler*/
- signal(SIGINT, sig_int);
- /*read packet from kernel*/
- int status;
- struct nlmsghdr *nlh;
- ipq_packet_msg_t *ipq_packet;
- int count = 0;
-
- while(1){
- status = ipq_read(h, buf, sizeof(buf));
- if(status > sizeof(struct nlmsghdr))
- {
- nlh = (struct nlmsghdr *)buf;
- ipq_packet = ipq_get_packet(buf);
- printf("recv bytes =%d, nlmsg_len=%d, indev=%s, datalen=%d, packet_id=%x\n", status, nlh->nlmsg_len,
- ipq_packet->indev_name, ipq_packet->data_len, ipq_packet->packet_id);
- unsigned char payload[65532];
- memset(payload, 0x00, sizeof(payload));
- memcpy(payload + ETH_HDRLEN, ipq_packet->payload, ipq_packet->data_len);
- /*display packet data in hex including 14 bytes of eth hdr(set by 0x00)*/
- int i;
- for(i = 0; i < ipq_packet->data_len + ETH_HDRLEN; i++){
- if(i%16 == 0)
- printf("00%.2x: ", i);
- printf("%.2x ", payload[i]);
- if(i % 16 == 15)
- printf("\n");
- }
- printf("\n");
- }
- struct iphdr * iph = (struct iphdr *) ipq_packet->payload;
- if( iph->protocol == 6)
- {
- //int fd = open("/home/kide/temp",O_WRONLY);
- unsigned char *data= ipq_packet->payload + sizeof(struct iphdr) + sizeof(struct tcphdr);
- //long int num=write(fd,data,ipq_packet->data_len-sizeof(struct iphdr) - sizeof(struct tcphdr));
- printf("%dbytes has been written \n",num);
- if(ipq_set_verdict(h, ipq_packet->packet_id, NF_ACCEPT,ipq_packet->data_len,ipq_packet->payload) !=-1)
- count++;
- printf("%dTCP success\n",count);
- }
- else
- {
- if(ipq_set_verdict(h, ipq_packet->packet_id, NF_ACCEPT,ipq_packet->data_len,ipq_packet->payload) !=-1)
- printf("OTHERS success\n");
- }
-
- }
- return 0;
- }
复制代码
[ 本帖最后由 Godbach 于 2009-8-18 16:38 编辑 ] |
|