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!