Overview LAN 1LAN 2 Summary 目的 連接 LAN1 與 LAN2 Frame forwarding Frame filtering Hint Data...

29

Transcript of Overview LAN 1LAN 2 Summary 目的 連接 LAN1 與 LAN2 Frame forwarding Frame filtering Hint Data...

Overview

LAN 1 LAN 2

Summary目的

連接 LAN1 與 LAN2Frame forwardingFrame filtering

HintData link layer socket programmingLibrary:

libpcap libnet

BridgeLayer 2 network deviceConnect 2 LAN

Network API - libpcaplibpcap (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 cardTutorial

http://yuba.stanford.edu/~casado/pcap/section1.html

Libpcap - Functionspcap_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 PCAPdev = 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 Packetint 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 - libnetDesigned 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 - Functionslibnet_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 - Initializationnet_b = libnet_init( LIBNET_LINK, "eth0", errbuf );if( net_a == NULL ){

fprintf(stderr, "libnet_init fail:%s ", errbuf ); return;}

libnet - Send a Packetc = libnet_write_link( net_b, (u_char*)packet, hdr->caplen );

pthreadint 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.

pthreadint 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 InstallationClean & update the package list

sudo apt-get cleansudo apt-get update

flex, bisonsudo apt-get flexsudo apt-get bison

libpcapsudo apt-get install libpcap0.8-dev

libnetsudo apt-get install libnet1-dev

Remember to Install these two library first

Programming EnvironmentYou have to write your program on Linux platform.You can install VMware to run Linux on it.

Environment Construction一台有兩張網卡的電腦 (PC_E)其他電腦用連接至 PC_EPC_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

GradingCorrectness (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 programDeadline: 自行練習

Appendixlibpcap / libnet

http://web.zyline.com.cn/prolist.asp?id=4916http://dev.csdn.net/article/21/21009.shtm

pthreadhttp://www.yolinux.com/TUTORIALS/

LinuxTutorialPosixThreads.html