C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 08-06-2009, 04:03 PM   #1
Registered User
 
Join Date: Aug 2009
Posts: 1
Post Ethernet Packet capture

I am trying to write a program that will allow me to capture packets from an Ethernet Line. Here are the logistics The program has to be a C program and it has to run on windows XP. Here is what i've gathered so far from other people's programs:
Code:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h> /* close() */
#include <string.h> /* memset() */

#define LOCAL_SERVER_PORT 100
#define MAX_MSG 500

int main(int argc, char *argv[])
{

   int sd, rc, n, cliLen;
   struct sockaddr_in cliAddr, servAddr;
   char msg[MAX_MSG];

   /* socket creation */
   sd=socket(AF_INET, SOCK_DGRAM, 0);
   if(sd<0) {
      printf("%s: cannot open socket \n",argv[0]);
      exit(1);
   }

   /* bind local server port */
   servAddr.sin_family = AF_INET;
   servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
   servAddr.sin_port = htons(LOCAL_SERVER_PORT);
   rc = bind (sd, (struct sockaddr *) &servAddr,sizeof(servAddr));
   if(rc<0) {
      printf("%s: cannot bind port number %d \n",
       argv[0], LOCAL_SERVER_PORT);
      exit(1);
   }

   printf("%s: waiting for data on port UDP %u\n",
       argv[0],LOCAL_SERVER_PORT);

   /* server infinite loop */
   while(1)
   {

      /* init buffer */
      memset(msg,0x0,MAX_MSG);


      /* receive message */
     cliLen = sizeof(cliAddr);
     n = recvfrom(sd, msg, MAX_MSG, 0,(struct sockaddr *) &cliAddr, &cliLen);
 printf("Fail\n");
      if(n<0)
      {
         printf("%s: cannot receive data \n",argv[0]);
         continue;
      }

      /* print received message */
      printf("%s: from %s:UDP%u : %s \n",
       argv[0],inet_ntoa(cliAddr.sin_addr),
       ntohs(cliAddr.sin_port),msg);

   }/* end of server infinite loop */

return 0;

}
I believe the problem has to do with windows blocking my use of the Ethernet port. I've looked up the port for the ethernet several different ways the most efficent was thru the command window. The program compiles fine but stalls/ gets stuck after printing "waiting for data on port UDP #". Any help would be great thank you.

Sorry i should have added a few things, first is that this is only the a part of the program i need to write. I need to capture and condition UDP packets from some instruments that i have running. I've tryed using many different ports, i've looked up these ports in the Windows command window. After doing some research, i've found that Windows wants exclusive control over the ports it is using. I've used wireshark and i can caputure packets that way but i can't condition the data using it. I need to know/find a way to unblock these ports. Thank you!

Last edited by smash_boxes; 08-07-2009 at 10:36 AM. Reason: More information to give
smash_boxes is offline   Reply With Quote
Old 08-06-2009, 04:31 PM   #2
Jaxom's & Imriel's Dad
 
Kennedy's Avatar
 
Join Date: Aug 2006
Location: Alabama
Posts: 801
The source for wireshark is out there. Same kind of deal. It may be more trouble than it is worth to hack it, though.
Kennedy is offline   Reply With Quote
Old 08-06-2009, 04:40 PM   #3
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 2,845
Quote:
but stalls/ gets stuck after printing "waiting for data on port UDP #". Any help would be great thank you.
Are you sure that any packets are coming in on port 100? If no packets come in on that port, then your application is just going to hang on that recvfrom() call.
__________________
bit∙hub [bit-huhb] n. A source and destination for information.
bithub is offline   Reply With Quote
Old 08-06-2009, 05:10 PM   #4
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by Kennedy View Post
The source for wireshark is out there. Same kind of deal. It may be more trouble than it is worth to hack it, though.
I was going to recommend the pcap library, which I used to write a packet sniffer* (I think this is what the OP is trying to do).

When I googled "pcap windows", I found this:

WinPcap, The Packet Capture and Network Monitoring Library for Windows

Which, low and behold, "is the packet capture and filtering engine of" wireshark, among other things.

*these are a riot w/ wifi
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 08-06-2009, 05:22 PM   #5
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
An AF_INET/SOCK_DGRAM socket will not allow to to capture anything except UDP traffic. Moreover, you cannot capture packets with a destination MAC that differs from your interface MAC, unless you put the interface into promiscuous mode.

Just use libpcap and be done with it.
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 08-06-2009, 11:45 PM   #6
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
Moved to Networking/Device Communication forum.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Reply

Tags
c programming, communication, ethernet, packets, sockets

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
segmentation fault while writing a packet to a pacp file Neminath C Programming 4 05-05-2009 05:35 AM
Global Variables Taka C Programming 34 11-02-2007 03:25 AM
Obtaining source & destination IP,details of ICMP Header & each of field of it ??? cromologic Networking/Device Communication 1 04-29-2006 02:49 PM
Raw Packet (sorry tripple weird Post) Coder87C Networking/Device Communication 6 03-04-2006 11:34 AM


All times are GMT -6. The time now is 11:24 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22