Docstoc

HW2

Document Sample
HW2 Powered By Docstoc
					Overview




    LAN 1   LAN 2
Summary
 目的
    連接LAN1與LAN2
    Frame forwarding
    Frame filtering
 Hint
    Data link layer socket programming
    Library:
        libpcap
        libnet
Bridge
 Layer 2 network device
 Connect 2 LAN
Network API - libpcap
 libpcap (Packet CAPture) provides a portable framework
  for low-level network monitoring.
 Applications include network statistics collection, security
  monitoring, network debugging, etc.
 libpcap is the library we are going to use to grab packets
  right as they come off of the network card
 Tutorial
    http://yuba.stanford.edu/~casado/pcap/section1.html
Libpcap - Functions
 pcap_t * pcap_open_live(char *device, int snaplen, int
  promisc, int to_ms, char *ebuf)
 int pcap_dispatch(pcap_t *p, int cnt, pcap_handler
  callback, u_char *user)
libpcap - Open up NIC for PCAP
dev = pcap_lookupdev(errbuf);
// 亦可寫成 dev = “eth0”
If (dev == NULL)
{
    fprintf(stderr,“%s\n”, errbuf);
    return -1;
}
descr = pcap_open_live(dev, BUFSIZ, promisc, pcap_time_out, errbuf);
If (descr == NULL)
{
    printf(“pcap_open_live(): %s\n”, errbuf);
    return -1;
}
libpcap - Capture a Packet
int ret;
ret = pcap_dispatch( pt_a, 0, dev_a_handle, NULL);
if ( ret == -1 )
{
    pcap_perror( pt_a, "pcap_dispatch err:");
}

void dev_a_handle( u_char *devId, const struct pcap_pkthdr *hdr, const
                                                   u_char *packet )
Network API - libnet
 Designed by Mike Schiffman, libnet is a portable, open
  source, C-language library for creating and injecting
  network packets.
 libnet supports packet creation at all network levels with
  the TCP/IP network model.
libnet - Functions
 libnet_t *libnet_init(int injection_type, char *device, char
  *err_buf);
 int libnet_write_link(struct libnet_link_int *l,
  const u_char *device, u_char *packet,
  int packet_size);
libnet - Initialization
net_b = libnet_init( LIBNET_LINK, "eth0", errbuf );
if( net_a == NULL )
{
    fprintf(stderr, "libnet_init fail:%s ", errbuf );
    return;
}
libnet - Send a Packet
c = libnet_write_link( net_b, (u_char*)packet, hdr->caplen );
pthread
 int pthread_create(pthread_t * thread, const
      pthread_attr_t * attr,
      void * (*start_routine)(void *),
      void *arg);
   thread - returns the thread id. (unsigned long int defined in
    bits/pthreadtypes.h)
   attr - Set to NULL if default thread attributes are used.
   void * (*start_routine) - pointer to the function to be threaded.
    Function has a single argument: pointer to void.
   *arg - pointer to argument of function. To pass multiple
    arguments, send a pointer to a structure.
pthread
 int pthread_join(pthread_t * thread, void
       **value_ptr);
 The pthread_join() function suspends execution of
  the calling thread until the target thread terminates
libpcap, libnet Installation
 Clean & update the package list
     sudo apt-get clean
     sudo apt-get update
 flex, bison
     sudo apt-get flex
     sudo apt-get bison
 libpcap
     sudo apt-get install libpcap0.8-dev
 libnet
     sudo apt-get install libnet1-dev
 Remember to Install these two library first
Programming Environment
 You have to write your program on Linux platform.
 You can install VMware to run Linux on it.
Environment Construction
 一台有兩張網卡的電腦 (PC_E)
 其他電腦用連接至 PC_E
 PC_E 需安裝 Linux作業系統以及
    libpcap, libnet
 亦可在 Windows 下使用 VMware 安裝 Linux 代替PC_E
   仍須兩張網路卡
   使 Virtual Machine 的兩張虛擬網路卡分別橋接至兩張實
    體網路卡
Edit VM Setting
Add a Host Virtual Adapters
Add a Ethernet Card

                      建立兩個
                      subnet
Add a Ethernet Card
Add a Ethernet Card
Add a Ethernet Card

                      兩張網卡分別對
                      應到eth0, eth1
Bridge Guest NIC to Host NIC



                          兩張網卡分別
                          bridged 到
                          VMnet2,
                          VMnet3
Requirements
 秀出 bridge 的 filtering database
   (工作站位址, 隸屬埠, 登錄時間)
 同一個 LAN 互傳(該兩台電腦都已經被 bridge 學習了)
  時,另一 LAN 不應該收到封包
 兩邊的 LAN 可以互通
Debugging
Grading
 Correctness (60%)
 Report (30%)
    How to run your program.
    What you’ve learned?
    What are you suffer from this HW?
    Any feedback?
 Coding Style (10%)
Hand in your program
 Deadline: 自行練習
Appendix
 libpcap / libnet
     http://web.zyline.com.cn/prolist.asp?id=4916
     http://dev.csdn.net/article/21/21009.shtm
 pthread
     http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThr
      eads.html

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:6
posted:8/1/2012
language:
pages:29