|
发表于 2004-4-7 17:58:52
|
显示全部楼层
回复: 请大家解释一下这C程序?
- #include <stdio.h>
- #include <string.h>
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <netinet/ip.h>
- #include <netinet/tcp.h>
- int main(int argc, char *argv[])
- {
- int s, bytes,a=0,sy=0,f=0,r=0,u=0,p=0;
- int ac=0,syc=0,fc=0,rc=0;
- struct tcphdr *tcp;
- struct iphdr *ip;
- struct in_addr addr;
- char buffer[4000];
-
- s = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
- if (s == -1)
- {
- perror("socket() failed");
- return 1;
- }
-
- ip = (struct iphdr*) buffer;
- tcp = (struct tcphdr*) (buffer + sizeof(struct iphdr));
-
- while( (bytes = recv(s, buffer, sizeof(buffer), 0)) > 0)
- {
- addr.s_addr = ip->saddr;
- a=ntohs(tcp->ack);
- sy=ntohs(tcp->syn);
- r=ntohs(tcp->rst);
- f=ntohs(tcp->fin);
- p=ntohs(tcp->psh);
- u=ntohs(tcp->urg);
- if (ip->saddr!=inet_addr("192.168.0.113"))
- {
- printf("Packet from source: %s\nwith flags ->",inet_ntoa(addr));
- if(a==256)
- {
- printf("Ack ");
- }
- if ( sy==256)
- {printf("Syn ");
- ;}
- if (f==256)
- {
- printf("Fin ");
- }
- if (r==256)
- {
- printf("Rst ");
- }
- if (p==256)
- {
- printf("Psh ");
- }
- if (u==256)
- {
- printf("Urg");
- }
- printf("\n");
- printf("With the sequence number of ->%i\n",ntohl(tcp->seq));
- printf("From port->%i\n\n",ntohs(tcp->source));
- }
- }
-
- if (bytes == -1)
- {
- perror("recv() failed");
- return 2;
- }
- return 0;
-
- }
复制代码 这样看舒服点。。这是unix的程序。。 |
|