Mail Archives: cygwin/2003/10/22/22:59:16
I got linux packet monitoring program,
and succeeded to build.
But when I tried to run, select() doens't respond and
program stops.
I use cygwin 1.5.5-1.
What can I do for this?
Please let me know.
The source code is as follows.
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <netinet/in_systm.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <sys/types.h>
#include <ctype.h>
#include <sys/socket.h>
#define SNIF_PORT 2010
void printable_fwrite(char *data,int len,FILE *fp)
{
int i;
for (i=0;i<len;i++)
if (isprint(data[i]) || data[i]==10 || data[i]==13)
putc(data[i],fp);
}
int main()
{
struct ip *ip_header;
struct tcphdr *tcp_header;
struct sockaddr_in target_info;
int target_info_len;
fd_set read_fd;
char recvbuf[5000];
int sock;
int length;
struct in_addr ia;
char sourceIP[16];
ip_header = (struct ip *)recvbuf;
tcp_header = (struct tcphdr *)(recvbuf+sizeof(struct
iphdr));
if ((sock=socket(AF_INET,SOCK_RAW,IPPROTO_TCP))==-1){
printf("Can not create RAW socket.\n");
return -1;
}
FD_ZERO(&read_fd);
FD_SET(sock,&read_fd);
select(FD_SETSIZE,&read_fd,NULL,NULL,NULL); // program
stop here
for (;;){
if (FD_ISSET(sock,&read_fd)){
target_info_len = sizeof(target_info);
length=recvfrom(sock,recvbuf,5000,0,
(struct sockaddr *)&target_info,&target_info_len);
if (length<=sizeof(struct ip)+sizeof(struct tcphdr))
continue;
if (ntohs(tcp_header->th_dport) == SNIF_PORT){
length-=sizeof(struct ip)-sizeof(struct tcphdr);
memcpy(&ia,&(ip_header->ip_src),sizeof(struct in_addr));
strcpy(sourceIP,(char *)inet_ntoa(ia));
printf("\n\n>\n");
printf("From %s\n",sourceIP);
printable_fwrite(recvbuf+sizeof(struct ip)+sizeof(struct
tcphdr),
length,stdout);
}
}
}
return(0);
}
__________________________________________________
Do You Yahoo!?
Yahoo! BB is Broadband by Yahoo! http://bb.yahoo.co.jp/
--
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
Problem reports: http://cygwin.com/problems.html
Documentation: http://cygwin.com/docs.html
FAQ: http://cygwin.com/faq/
- Raw text -