⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcp_sniff_2.c

📁 LINUX网络编程文档
💻 C
字号:
/*****************Tcp_sniff_2.c**************************/
#include<stdio.h>
#include<sys/socket.h>
#include<socketbits.h>
#include<sys/ioctl.h>
#include<net/if.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include "headers.h"

/*Prototype area*/

int Open_Raw_Socket(void);
int Set_Promisc(char *interface, int sock);
int main(){
	int sock,bytes_recieved,fromlen;
	char buffer[65535];
	struct sockddr_in from;
	struct ip *ip;
	struct tcp *tcp;
	
	sock = Open_Raw_Socket();
	
	Set_Promisc(INTERFACE,sock);
	
	while(1)
	{
	  fromlen = sizeof from;
	  bytes_recieved = recvfrom(sock,buffer,sizeof buffer,0,(struct sockaddr *)&from,&fromlen);
	  printf("\nButes received:::%5d\n",bytes_recieved);
	  printf("Source address:::%s\n,inet_ntoa(from.sin_addr));
	  ip=(struct ip *)buffer;
	  /* See if this is a TCP packet*/
	  if(ip->ip_protocol ==6){
	  printf("IP header length:::%d\n"",ip->ip_length);
	  printf("Protocol:::%d\n",ip->ip_protocol);
	  tcp = (struct tcp*)(buffer+(4*ip->ip_length));
	  printf("Source port:::%d\n",ntohs(tcp->tcp_source_port));
	  printf("Dest port:::%d\n",ntohs(tcp->tcp_dest_port));
	  }
	 }
}

int Open_Raw_Socket(){
  int sock;
  if((sock = socket(AF_INET,SOCK_RAW,IPPROTO_TCP)) <0){
  perror("The raw Socket was not created");
  exit(0);
 };
 return(sock);
 
}

int Set_Promisc(char *interface,int sock){
	struct ifreq ifr;
	strncpy(ifr,ifr_name,interface,strnlen(interface)+1);
	if((ioctl(sock,SIOCGIFFLAGS,&ifr)==-1){
	perror("Could not retrive flags for the interface");
	exit(0);
	}
	printf("The interface is ::: %s\n",interface):
	perror("Retrieved flags from interface successfully");
	
	/*now that the flags have been retrieved*/
	/*set the flags to PROMISC*/
	ifr.ifr_flags |= IFF_PROMISC;
	if(ioct|(sock,SIOCSIFFLAGS,&ifr)==-1){
	/*Could not set the flags on the interface */
	perror("Could not set the PROMISC flags:");
	exit(0);
 }
 printf("Setting interface:::%s:::to promisc",interface);
 return(0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -