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

📄 os_generic.cpp

📁 source_code 实现无线局域网中的802.1x功能
💻 CPP
字号:
/**************************************************************************/
/* WIRE1x Version 1.0: A client-side 802.1x implementation                */
/* based on xsupplicant of Open1x for Windows XP, 2000, 98, and Me        */
/*                                                                        */
/* This code is released under both the GPL version 2 and BSD licenses.   */
/* Either license may be used.  The respective licenses are found below.  */
/*                                                                        */
/* Copyright (C) 2004, WIRE Lab, National Tsing Hua Univ., Hsinchu, Taiwan*/
/* All Rights Reserved                                                    */
/**************************************************************************/

/*
 * --- GPL Version 2 License ---
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * --- BSD License ---
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *  - All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *       This product includes software developed by the University of
 *       Maryland at College Park and its contributors.
 *  - Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdafx.h>
#include <pcap.h>#include <stdlib.h>#include <string.h>#include <unistd.h>
#include <libnet.h>
#include <time.h>

#ifndef OS_SPECIFIC_DEBUG#define OS_SPECIFIC_DEBUG 0#endif#include "dot1x_globals.h"
#include "os_frame_funcs.h"  // This is the header used for all versions                                // of OS specific drivers.char *dev_to_use = NULL;pcap_t *pcap_descr = NULL;
libnet_t *eth = NULL;
u_char *source_mac = NULL;char *src_interface=NULL;const u_char *packet_ptr=NULL;
int more_frames_avail=0;int sleepable = 1;                 // By default, allow sleeping.const struct pcap_pkthdr *packet_header;

//// packet_catch() - Deals with any frames that come in, and pass the filter.//
pcap_handler packet_catch(u_char *args, const struct pcap_pkthdr *header, const u_char *packet){  // A better way to do this would be to copy the contents of the packet to our  // buffer.  (Assuming we can determine the size of the source buffer.)  // It would get rid of some compiler warnings.  packet_header = header;  packet_ptr = packet;  return 0;}void done_with_current_frame(){  // We don't need anything here...  But, OS specific handlers may need something.}//// allow_sleep -- should we allow get_frame to sleep at all?//void allow_sleep(int yesno){  sleepable = yesno;}//// more_frames() -- returns 1 if there are more frames to process.//int more_frames(){  if (sleepable == 0) return 1;  // If we can sleep, always say there are more                                 // frames.  return more_frames_avail;}//// get_frame(int u_sec) -- returns a pointer to a frame if there is one.//  NULL if there isn't.  u_sec is how long to sleep before returning.//u_char *get_frame(int sec){  int pcap_ret_val=0;
  int i;
    Sleep(sec);  pcap_ret_val = pcap_dispatch(pcap_descr, 1, (pcap_handler)packet_catch, NULL);
  if (pcap_ret_val == 1)
  {
	    /* Print the packet */
        for (i=1; (i < (packet_header->caplen + 1) ) ; i++)
        {
            printf("%.2x ", packet_ptr[i-1]);
            if ( (i % 16) == 0) printf("\n");
        }

	  more_frames_avail = 1;      return (u_char *)packet_ptr;
  } else {      more_frames_avail = 0;      return NULL;  }}// Return a pointer to the frame we are working against.u_char *get_working_frame(){  return (u_char *)packet_ptr;}//// setup_pcap() - Returns a handle to a pcap descr.  NULL is there is an// error.  On error, there is a message in pcapErr.//pcap_t *setup_pcap(int buf_size, int timeout, char pcapErr[PCAP_ERRBUF_SIZE]){  char pcap_err[PCAP_ERRBUF_SIZE];    //pcap error buffer  pcap_t *pcap_descr = NULL;          //pcap connection  bpf_u_int32 pcap_maskp;             //subnet mask  bpf_u_int32 pcap_netp;              //???  char pcap_filter[100];              //filter space  struct bpf_program pcap_fp;         //hold the compiled filter.  char *errbuf=NULL;                  //Buffer to catch our error.  char *pcap_compile_error = "Error running pcap compile!";  char *pcap_filter_error = "Error setting filter!";  pcap_lookupnet(dev_to_use, &pcap_netp, &pcap_maskp, pcap_err);    /* We need to be in promisc mode to grab stuff that "isn't for us" */  pcap_descr = pcap_open_live(dev_to_use, buf_size, 0, timeout, pcap_err);  if (pcap_descr == NULL)    {
      return NULL;    }    sprintf(pcap_filter, "ether dst %x:%x:%x:%x:%x:%x or ether dst 01:80:c2:00:00:03 and ether proto 0x888e",source_mac[0],source_mac[1],source_mac[2],source_mac[3],source_mac[4],source_mac[5]);
  if (pcap_compile(pcap_descr, &pcap_fp, pcap_filter, 0, pcap_netp) == -1)    {
      return NULL;    }
  if (pcap_setfilter(pcap_descr, &pcap_fp) == -1)    {      return NULL;    }

  if (pcap_setnonblock(pcap_descr, 1, errbuf) == -1)    {      // If we can't go in to non-blocking mode, the timers that determine      // if we are on a port that isn't smart enough to authenticate will      // break.  However, this shouldn't break the program.  It will      // just take longer to determine that there isn't anything there.      printf("Couldn't go in to non-blocking state, timers won't work!\n");      printf("Recieved error : %s\n",errbuf);    }

  printf("link type= %d\n",pcap_datalink(pcap_descr));
  return pcap_descr;}//// initalize_frame_funcs() -- If it returns NULL everything is fine.//  If not, the resulting pointer has an error message.//char *initalize_frame_funcs(char *device, int buf_size, int timeout){  char *temp = NULL;       //This will hold some temporary values.  char pcap_err[PCAP_ERRBUF_SIZE];
  char lerrbuf[LIBNET_ERRBUF_SIZE];  char *error_pcap_failure = "Couldn't set up PCAP.";  if (device == NULL) {    temp = pcap_lookupdev(pcap_err);    if (temp == NULL) {      temp = (char *)malloc(sizeof(pcap_err));      strncpy(temp, pcap_err, sizeof(pcap_err));      return temp;    }  } else {    temp = device;  }

  printf("temp=%s\n",temp);  // Stash a copy of the device name.  dev_to_use = (char *)malloc(strlen(temp)+1);  strncpy(dev_to_use, temp, strlen(temp)+1);  // +1 to pick up the null.

  printf("dev_to_use=%s\n",dev_to_use);  if ((eth = libnet_init(LIBNET_LINK_ADV,dev_to_use,lerrbuf)) == NULL)      return ("Couldn't open device");  // Get the Mac address.
  source_mac=get_src_mac();
  if (source_mac == NULL)      return ("Couldn't get MAC address");
  if ((pcap_descr = setup_pcap(buf_size, timeout, pcap_err)) == NULL)	  return error_pcap_failure;    return NULL;}// Return a pointer to the device name in use.char *get_dev_name(){  return dev_to_use;}// // get_src_mac() - Return a pointer to a copy of the MAC address.//
u_char *get_src_mac(){
  u_char *ret_mac=NULL;
  struct libnet_ether_addr *e;

  e = libnet_get_hwaddr(eth);
  ret_mac = (u_char *)malloc(ETH_ADDR_LEN);   //Size of MAC address.  memcpy(ret_mac, e->ether_addr_octet, ETH_ADDR_LEN);  return (u_char *)ret_mac;}
// Send a frame.char *send_frame(u_char *frame_ptr, int frame_size){
	if(frame_ptr[23]==0xcd)
		frame_ptr[23]=0x00;
	int i=0;

	if ((i = libnet_write_link(eth,frame_ptr,frame_size)) == -1)
		return ("Error sending frame");
	return NULL;}int shutdown_frame_funcs(){	if (dev_to_use != NULL)    {		free(dev_to_use);		dev_to_use = NULL;    }
  // Shutdown libdnet	if (eth != NULL)    {		libnet_destroy(eth);    }  // Shutdown PCAP	if (pcap_descr != NULL)    {		pcap_close(pcap_descr);		if (pcap_descr != NULL) 		{	  			free(pcap_descr);			pcap_descr = NULL;		}    }	return 0;}

⌨️ 快捷键说明

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