Advertisement
Guest User

Code

a guest
Aug 12th, 2012
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.23 KB | None | 0 0
  1. /*
  2.  *  T.c
  3.  *
  4.  *  compile: gcc -Wall T2.c -o T2 -lipq
  5.  */
  6.  
  7. #include <netinet/in.h>
  8. #include <libipq.h>
  9. #include <linux/netfilter.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <string.h>
  14. #include <netinet/ip.h>
  15. #include <netinet/udp.h>
  16. #include <netinet/tcp.h>
  17.  
  18. #define BUFSIZE 131072
  19.  
  20. static void die(struct ipq_handle *h) {
  21.    
  22.     ipq_perror("passer");
  23.     ipq_destroy_handle(h);
  24.    
  25. }
  26.  
  27.  
  28. void start_packet_engine() {
  29.    
  30.         int status;
  31.     unsigned char buf[BUFSIZE];
  32.     struct ipq_handle *h;
  33.  
  34.    
  35.     printf("\nLaukiame paketu\n");
  36.    
  37.  
  38.     h = ipq_create_handle(0, PF_INET);
  39.    
  40.     if (!h)
  41.         die(h);
  42.    
  43.    
  44.     status = ipq_set_mode(h, IPQ_COPY_PACKET, BUFSIZE);
  45.    
  46.     if (status < 0)
  47.         die(h);
  48.    
  49.    
  50.     do {
  51.         status = ipq_read(h, buf, BUFSIZE, 0);
  52.         if (status < 0)
  53.             die(h);
  54.        
  55.        
  56.         switch (ipq_message_type(buf)) {
  57.            
  58.             case NLMSG_ERROR: {
  59.                 fprintf(stderr, "Received error message %d\n",
  60.                 ipq_get_msgerr(buf));
  61.                 break;
  62.             }
  63.           case IPQM_PACKET:
  64.  {
  65.   ipq_packet_msg_t *m = ipq_get_packet(buf);
  66.  
  67.  
  68.   struct iphdr *iph = ((struct iphdr *)m->payload);
  69.  
  70.  
  71.   struct tcphdr *tcp = (struct tcphdr *)(m->payload + (iph->ihl << 2));    
  72.  
  73.   struct udphdr *udp = (struct udphdr *) (m->payload + (iph->ihl << 2));
  74.  
  75.   int unsigned payload_offset = ((iph->ihl << 2) + (tcp->doff << 2));
  76.  
  77.     int unsigned payload_length = (unsigned int) ntohs(iph->tot_len) -
  78. payload_offset;
  79.  
  80. //unsigned char data[2]={0x00,0x00};
  81. unsigned char data[4]={0x00,0x00,0x01,0xb3};
  82.  int i = 0;
  83. while(m->data_len > 100) {
  84.  
  85.  
  86.     if (m->payload[i] != 0x00) {
  87.         i++;
  88.                        }
  89.     else {
  90.     if (memcmp(&m->payload[i],data,sizeof(data)) == 0) {
  91.            
  92.             printf("I frame");
  93.  
  94. break;                                       }
  95.  
  96.    
  97.      }
  98.                 }
  99.  
  100. status = ipq_set_verdict(h, m->packet_id,NF_ACCEPT, 0, NULL);
  101.    
  102.   break;
  103.      }
  104.     }
  105.  
  106.              
  107.        
  108.     }
  109.     while (1);
  110.    
  111.     printf("Engine Stopped...\n");
  112.    
  113.     ipq_destroy_handle(h);
  114.    
  115. }
  116.  
  117. int main() {
  118.     start_packet_engine();
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement