tcpdump mailing list archives

libpcap's code is not working correctly on freebsd 4.7


From: "Patel" <vijayppatel () hotpop com>
Date: Thu, 13 Mar 2003 18:59:01 +0530

Hello Friends,
    I am working on packet capture program on FreeBSD 4.7. My program is not responding in same way as it works on 
linux. 
    
    And by commenting the line from 187 to 194.... 
187 //#####################COMENT THIS PORTION START############
194 //#####################COMENT THIS PORTION ENDS#############
    freebsd program works. But if these comments are removed, i mean these 7 line included in code then it is not 
printing even debugging messages.
 
    Thanking in advance.

Vijay.  

MAKEFILE
----------------
CC = gcc
CFLAGS = 
OBJECTS2 = outgoing.o

target : outgoing

outgoing : $(OBJECTS2)
 $(CC) $(CFLAGS) $(OBJECTS2) -o outgoing -lpcap
 rm $(OBJECTS2)

outgoing.o : outgoing.c
 $(CC) $(CFLAGS) -c outgoing.c

clean:
 rm -f *.o outgoing



OUTGOING.c
--------------------
#include <stdio.h>
#include <pcap.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
//#include <asm/types.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <net/ethernet.h>
#include <arpa/inet.h>


void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet);

unsigned short in_cksum(unsigned short *addr, int len);

unsigned short
in_cksum(unsigned short *addr, int len)
{
 int    nleft = len;
 int    sum = 0;
 unsigned short *w = addr;
 unsigned short answer = 0;

 /*
  * Our algorithm is simple, using a 32 bit accumulator (sum), we add
  * sequential 16 bit words to it, and at the end, fold back all the
  * carry bits from the top 16 bits into the lower 16 bits.
  */
 while (nleft > 1)  {
  sum += *w++;
  nleft -= 2;
 }

  /* 4mop up an odd byte, if necessary */
 if (nleft == 1) {
  *(unsigned char *)(&answer) = *(unsigned char *)w ;
  sum += answer;
 }

  /* 4add back carry outs from top 16 bits to low 16 bits */
 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
 sum += (sum >> 16);   /* add carry */
 answer = ~sum;    /* truncate to 16 bits */
 return(answer);
}


void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet1) {
   struct ether_header *eth_hdr;
   struct ip *ip_hdr;
   struct tcphdr *tcp;
   char *payload;                    /* Packet payload */

   int size_ethernet = sizeof(struct ether_header);
   int size_ip = sizeof(struct ip);
   int size_tcp = sizeof(struct tcphdr);
   int len=0;
   
   char *ptr_src1,*ptr_dest1,*ptr_buffer,*ptr1;
   u_int16_t src_port1,dest_port1;

   struct in_addr new_saddr;
   char test_src1[] = "172.16.2.2";
   char test_dest1[] = "192.168.0.1";

   u_int16_t size_packet;
   char *new_packet;

   char packet[header->len];

   static int count = 1;                   /* Just a counter of how many packets we've had */

   struct tcp_pseudo        /*the tcp pseudo header*/
   {
      struct in_addr src_addr;
      struct in_addr dst_addr;
      unsigned char  dummy;
      unsigned char  proto;
      unsigned short length;
   } pseudohead;

   struct help_checksum   /*struct for checksum calculation*/
   {
      struct tcp_pseudo pshd;
      struct tcphdr tcphd;
      char tcpdata[1024];
   } tcp_chk_construct;

   struct in_addr addr1;
   char *checksum_packet;

   memset (packet, 0, header->len);
   memcpy(packet, packet1, header->len);

   /* -- Define our packet's attributes -- */
   eth_hdr = (struct ether_header*)(packet);
   ip_hdr = (struct ip*)(packet + size_ethernet);
   tcp = (struct tcphdr*)(packet + size_ethernet + size_ip);
   payload = (u_char *)(packet + size_ethernet + size_ip + size_tcp);
   len = ntohs(ip_hdr->ip_len) - size_ip - size_tcp;

   //if(ip_hdr->ip_p == IPPROTO_TCP) {
      addr1 = ip_hdr->ip_src;
      ptr_buffer = inet_ntoa(addr1);
      ptr_src1 = (char *) malloc(strlen(ptr_buffer)+1);
      strcpy(ptr_src1,ptr_buffer);
      src_port1 = ntohs(tcp->th_sport);
      
      addr1 = ip_hdr->ip_dst;
      ptr_buffer = inet_ntoa(addr1);
      ptr_dest1 = (char *) malloc(strlen(ptr_buffer)+1);
      strcpy(ptr_dest1,ptr_buffer);
      dest_port1 = ntohs(tcp->th_dport);

      //Discards packets of ssh.
      /*if(src_port1 ==22 || dest_port1 ==22) {
         free(ptr_src1);
         free(ptr_dest1);
         count++;
         return;
      }*/
      
      printf("%d\t%s:%d\t%s:%d\t%ld\t%ld\n", count, ptr_src1, src_port1, ptr_dest1, dest_port1, 
ntohl(tcp->th_seq),ntohl(tcp->th_ack));

      free(ptr_src1);
      free(ptr_dest1);
   //}
   count++;
}


int main()
{
 //char *dev;
   char dev[] = "rl1";
   bpf_u_int32 mask;
   bpf_u_int32 net;
   
   char errbuf[PCAP_ERRBUF_SIZE];
   
   pcap_t *handle;
   struct pcap_pkthdr header;
   const u_char *packet;
   
   struct bpf_program filter;
   char filter_app[] = "";
   
   int datalink;        /* Type of datalink SLIP/PPP/Ethernet */
   
   printf("pcap_lookupnet");
   pcap_lookupnet(dev,&net,&mask,errbuf);
   
   if((handle = pcap_open_live(dev, BUFSIZ, 1, 0, errbuf))== NULL) {
      printf("Error opening device: %s", errbuf);
   } else {
      printf("pcap_open_live");
   }
   
   if((datalink = pcap_datalink(handle)) < 0 ) {
      printf("Datalink error : %s", pcap_geterr(handle));
   } else {
      printf("pcap_datalink");
   }
   //printf("Datalink = %d\n", datalink);
   
   if( pcap_compile(handle, &filter, filter_app, 0, net) == -1) {
      printf("pcap_compile borqed\n");
      exit(1);
   } else {
      printf("pcap_compile");
   }

   if (pcap_setfilter(handle, &filter) == -1) {
      printf("pcap_setfilter said 'eat shit'\n");
      exit(1);
   } else {
      printf("pcap_setfilter");
   }
   
   //pcap_loop(handle, -1, got_packet, NULL);
   printf("Start.....\n");

   //#####################COMENT THIS PORTION START############
   /*while(1) {
      while((packet = (char *) pcap_next(handle, &header)) == NULL);
      printf("Calling");
      got_packet("", &header,packet);
      //free(packet);
   }*/
   //#####################COMENT THIS PORTION ENDS#############
   
   pcap_close(handle);
 return(0);
}




Current thread: