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

📄 toolkit.c

📁 这是一款局域网聊天控制软件。用C编写。里面包括功能:查看局域网内有多少台在线主机
💻 C
字号:
/* * Title: toolkit.c * Description: This program is a LAN toolkit. It could find out how many computers *              are in the same LAN. And in addtion, it is capable to detect whether *              the applications(FTP,TELNET,HTTP) are provided on a certain host. * @compile: gcc toolkit.c -lpcap -lnet -o toolkit * @version: 1.0 */#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>#include <stdlib.h>#include <string.h>#include<libnet.h>#include<pcap.h>#define SNAP_LEN 1518#define SIZE_ETHERNET 14struct sniff_ethernet {         u_char  ether_dhost[ETHER_ADDR_LEN];    /* destination host address */        u_char  ether_shost[ETHER_ADDR_LEN];    /* source host address */         u_short ether_type;                        /* IP? ARP? RARP? etc */ }; /* ARP header */struct sniff_arp {         u_short     ar_hrd;          /* format of hardware address */         u_short     ar_pro;          /* format of protocol address */         u_char      ar_hln;         /* length of hardware address  */         u_char      ar_pln;         /* length of protocol address  */         u_short     ar_op;         u_char      ar_sha[6];      /* sender hardware address  */         u_char      ar_sip[4];      /* sender IP address       */         u_char      ar_tha[6];      /* target hardware address  */         u_char      ar_tip[4];      /* target IP address       */ };int count=1;bpf_u_int32 my_ip,first_ip,last_ip,client_ip;u_char enet_src[6],enet_dst[6],tar_dst[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};char mac[256][235], tempmac[256][256];struct  libnet_ether_addr *ether;pcap_t* descr;bpf_u_int32 ips[256], final_ip[256], temp[256];void my_callback (u_char* none,const struct pcap_pkthdr* pkthdr,const u_char* packet ){	struct sniff_ethernet* ether;	struct sniff_arp * arp;	struct in_addr current, sourceaddr;	char ipinchar[20], sipinchar[20];	ether = (struct sniff_ethernet*)(packet);	arp = (struct sniff_arp*)(packet + sizeof(struct sniff_ethernet));        sprintf(ipinchar, "%d.%d.%d.%d", arp->ar_tip[0], arp->ar_tip[1], arp->ar_tip[2], arp->ar_tip[3]);	sprintf(sipinchar, "%d.%d.%d.%d", arp->ar_sip[0], arp->ar_sip[1], arp->ar_sip[2], arp->ar_sip[3]);	inet_aton(ipinchar, &current);	inet_aton(sipinchar, &sourceaddr);	if(current.s_addr == my_ip)          {              int index = ntohl(sourceaddr.s_addr) - first_ip;	     ips[index] = sourceaddr.s_addr;             temp[count]=sourceaddr.s_addr;             sprintf(mac[index],"%02X:%02X:%02X:%02X:%02X:%02X",arp->ar_sha[0],arp->ar_sha[1],arp->ar_sha[2],arp->ar_sha[3],arp->ar_sha[4],arp->ar_sha[5]);                  sprintf(tempmac[count],"%02X:%02X:%02X:%02X:%02X:%02X",arp->ar_sha[0],arp->ar_sha[1],arp->ar_sha[2],arp->ar_sha[3],arp->ar_sha[4],arp->ar_sha[5]);              count++;	  }}void opendevice(){              libnet_t *l;        char *device="eth0";        char *dev;        int ret;        bpf_u_int32 maskp;             /* subnet mask  */        bpf_u_int32 netp;              /* ip */        char errbuf[LIBNET_ERRBUF_SIZE];        char filter[100]="";        struct bpf_program fp;                dev=pcap_lookupdev(errbuf);        if (dev==NULL)          {             printf("\nERROR: Cannot Finding a Device. Check your internet connection!\n[%s]\n",errbuf);             exit(1);          }          ret=pcap_lookupnet(dev,&netp,&maskp,errbuf);        if(ret==-1)          {              printf("\nERROR: Faild Looking Up Network.\n[%s]\n",errbuf);              exit(1);          }          descr = pcap_open_live(dev,BUFSIZ,1,-1,errbuf);        if(descr == NULL)          { 	              printf("pcap_open_live(): %s\n",errbuf);               exit(1);          }           sprintf(filter,"arp");        if(pcap_compile(descr,&fp,filter,0,netp) == -1)          {                           fprintf(stderr,"Error calling pcap_compile\n");                exit(1);          }        /* set the compiled program as the filter */        if(pcap_setfilter(descr,&fp) == -1)          { 	               fprintf(stderr,"Error setting filter\n");                exit(1);           }           first_ip = netp;        last_ip = netp|~maskp;        first_ip = htonl(first_ip)+1;        last_ip = htonl(last_ip);}void buildpacket(){        int i,c,j;        libnet_t *l;        libnet_ptag_t t;        char *device="eth0";	        char errbuf[LIBNET_ERRBUF_SIZE];          struct timeval timeout;        fd_set readfd;        timeout.tv_sec=0;        timeout.tv_usec=1000;        for(j = 0 ; j <= last_ip-first_ip; j++)          {              client_ip = first_ip + j;
              client_ip = htonl(client_ip);
              l = libnet_init(LIBNET_LINK_ADV,device,errbuf); 
              if (l == NULL)
                 {
                    fprintf(stderr, "libnet_init() failed: %s", errbuf);
                    exit(EXIT_FAILURE);
                 }
  
              my_ip = libnet_get_ipaddr4(l);
              ether = libnet_get_hwaddr(l);
              memcpy(&enet_src, ether,6);
    
              t = libnet_build_arp(
                      ARPHRD_ETHER,                       
	              ETHERTYPE_IP,                 /* protocol addr */
	              6,                             /* hardware addr size */
	              4,                             /* protocol addr size */
	              ARPOP_REQUEST,               /* operation type */
	              enet_src,                       /* sender hardware addr */
	              (uint8_t *)&my_ip,              /* sender protocol addr */
	              tar_dst,                        /* target hardware addr */
	              (uint8_t *)&client_ip,            /* target protocol addr */
	              NULL,                        /* payload */
	              0,                            /* payload size */
	              l,                            /* libnet handle */
	              0);                           /* libnet id */
    
                if (t == -1)
                  {
                     fprintf(stderr, "Can't build ARP header: %s\n", libnet_geterror(l));
                     return;
                  }

                libnet_build_ethernet(
	              tar_dst, enet_src,
                      ETHERTYPE_ARP,
	              NULL, 0,
	              l,
	              0);
    
                if (t == -1)
                  {
                      fprintf(stderr, "Can't build ethernet header: %s\n", libnet_geterror(l));
                      return;
                  }	
    
                c = libnet_write(l);
                if(c == -1) 
                  {	
                       fprintf(stderr, "Can't build ethernet header: %s\n", libnet_geterror(l));
                       return;
                  }

        }		//for loop ended
   
        while(1) 
          {
                int fd = pcap_fileno(descr); 
                FD_ZERO(&readfd);
                FD_SET(fd,&readfd);
                int maxfd = fd+1;
                int ret=0;
                ret = select(maxfd,&readfd,NULL,NULL,&timeout);
                if(ret == 0) 
                break;    
                if(FD_ISSET(fd,&readfd)) 
                {
                     /* capture packets */ 
                     pcap_dispatch(descr,1,my_callback,NULL); 
                }
           }	

         struct in_addr printAddr;
         char printMAC[256][256];
         int n=0;
         printf("\nThe host in the LAN are:\n");
         for(i = 0; i <=  last_ip-first_ip; i ++)
	 if(ips[i] != 0)           {
                   n++;
                   final_ip[n]=ips[i];
                   printAddr.s_addr = final_ip[n];
                   strcpy(printMAC[n],mac[i]);
                   printf("[%-2d]  IP: %-20s    MAC address: %-20s\n", n,inet_ntoa(printAddr), printMAC[n]);
            }	
         printf("There are %d hosts in the same LAN in total.",n);	

         printf("\n\nPress Enter to detect IP conflict:");
         getchar();
         getchar();

         int g,h;
         int conflictCounter = 0;
         struct in_addr conflictAddrA;
         for ( g= 1; g <count; g++) 
           {
              for (h = g + 1; h <= count; h++)
                 {
                    if ( temp[g] == temp[h]) 
                      {
                         conflictAddrA.s_addr = temp[g];
                         printf("Found conflict: IP [%s]!",inet_ntoa(conflictAddrA));       
                         printf("\nMAC %2s and MAC %2s confilct.\n", tempmac[g],tempmac[h]);
                         conflictCounter ++;
                       } 
                  }
            }
         if (conflictCounter == 0) 
           {
                 printf("\nNo IP conflict found.");
           }else 
           {
                 printf("\n%d conflict in total.",conflictCounter);
           }	


         printf("\n\nPress '1' to continue...  OR  Press '2' to return to the main menu: ");
         int choice;
         scanf("%d",&choice);        
         if(choice==1 && n>=1)         detect_ftp_service();
         libnet_destroy(l);
         return; 
}


detect_ftp_service()
{																										
                 int sd,dex,service;	
                 struct sockaddr_in serverAddr;  
                 u_int16_t port[3] = {80, 21, 23};
                 char port_info[3][20] = {"HTTP", "FTP", "Telnet"};	

                 printf("Which service would you like to test? [0]HTTP [1]FTP [2]TELNET: ");
                 scanf("%d",&service);
        
                 printf("Which host would you like to test? Please input the index:"); 
                 scanf("%d",&dex);
        
                 serverAddr.sin_family = AF_INET;            
                 serverAddr.sin_addr.s_addr = final_ip[dex];   
                 serverAddr.sin_port = htons(port[service]);
                 bzero(&(serverAddr.sin_zero), 8);     

                 sd = socket(AF_INET, SOCK_STREAM, 0);
	         if(sd <0)
	            {
		       perror("Cannot open socket!");
		       exit(1);
	            }
	
                 printf("Connecting...\n"); 
	         if(connect(sd,(struct sockaddr *) &serverAddr,sizeof(serverAddr))<0)
	            {
		       printf("%s service is not available on [%s]\n",port_info[service],inet_ntoa(serverAddr.sin_addr));
		 perror("");
	
                    }else                    {
                       printf("%s service is available on [%s]\n",port_info[service],inet_ntoa(serverAddr.sin_addr));
               
                       if(service==1)                         {
                            printf("Now check the manner of FTP logging in! Press Enter to continue:");
                            getchar();
                            getchar();

                            char reply_code[100];
                            memset(reply_code,0,100);                            write(sd,"\r\n",strlen("\r\n"));                             read(sd,reply_code,100);                                                        //        write(1,reply_code,3);                            memset(reply_code,0,100);                                                                                                                                                                   read(sd,reply_code,100);                            //         write(1,reply_code,3);                            memset(reply_code,0,100);                                                                                                                                                                          
                            write(sd,"USER anonymous\r\n",strlen("USER anonymous\r\n"));
                                                                
                            read(sd,reply_code,100);
        
                            if(reply_code[0]=='3')                               {                                          printf("This host is anonymous FTP.\n");
                               }else                               {
                                    printf("This host is traditional FTP.\n"); 
                               }
                           }
                    }
                      close(sd);
}


int main()
{
         int choice;
         pcap_t* descr;
         struct bpf_program fp;         /* hold compiled program        */
         int start=1;
         char str[20];
         libnet_t *l;
         char *device="eth0"; 
         char errbuf[LIBNET_ERRBUF_SIZE];  
 
         printf("\n           Welcome to use our LAN toolkit !!:");
         while(start==1)
         {   
             printf("\n--------------------------------------------------------------------");
                 printf("\n1. Display my own IP address and MAC address.");
                 printf("\n2. See how many PCs in the LAN and display their information.");
                 printf("\n3. Quit the program.\n");
                 printf("--------------------------------------------------------------------");
                 printf("\nWhich service would you like to select?\n"); 
                 scanf("%d",&choice);
                 
                 switch(choice)
                 {
                        case 1:    l=libnet_init(LIBNET_LINK_ADV,device,errbuf); 
                                  if (l == NULL)
                                    {
	                              fprintf(stderr, "libnet_init() failed: %s\n", errbuf);
                                      exit(EXIT_FAILURE);
	                            }
	  
                                      my_ip = libnet_get_ipaddr4(l);
	                              ether = libnet_get_hwaddr(l);
	                              memcpy(&enet_src, ether,6);
	                              printf("\nMy IP is:  %s.\n",inet_ntop(AF_INET,&my_ip,str,sizeof(str)));
	     
                                      printf("My MAC address is:  %02X:%02X:%02X:%02X:%02X:%02X.\n",
                                      enet_src[0],enet_src[1],enet_src[2],enet_src[3],enet_src[4],enet_src[5] );
	                              break;
		
                         case 2:       count=1;
                                      opendevice();
                                      buildpacket();                     
                                      break;

                                      printf("\nPress Enter to continue...");
                                      getchar();
                                      getchar();
     
                         case 3:       start=0;
                                      break;
     
                                      default:  printf("\n You have input a wrong number, please input again!\n");
                  }//switch

         }//while
         return 0;
}//main

⌨️ 快捷键说明

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