cs4231 local area networks hw2 – pc-based bridge
DESCRIPTION
CS4231 Local Area Networks HW2 – PC-Based Bridge. 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. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: CS4231 Local Area Networks HW2 – PC-Based Bridge](https://reader035.vdocument.in/reader035/viewer/2022081503/56814ee4550346895dbc774d/html5/thumbnails/1.jpg)
![Page 2: CS4231 Local Area Networks HW2 – PC-Based Bridge](https://reader035.vdocument.in/reader035/viewer/2022081503/56814ee4550346895dbc774d/html5/thumbnails/2.jpg)
Overview
LAN 1 LAN 2
![Page 3: CS4231 Local Area Networks HW2 – PC-Based Bridge](https://reader035.vdocument.in/reader035/viewer/2022081503/56814ee4550346895dbc774d/html5/thumbnails/3.jpg)
Summary目的
連接 LAN1 與 LAN2Frame forwardingFrame filtering
HintData link layer socket programmingLibrary:
libpcap libnet
![Page 4: CS4231 Local Area Networks HW2 – PC-Based Bridge](https://reader035.vdocument.in/reader035/viewer/2022081503/56814ee4550346895dbc774d/html5/thumbnails/4.jpg)
BridgeLayer 2 network deviceConnect 2 LAN
![Page 5: CS4231 Local Area Networks HW2 – PC-Based Bridge](https://reader035.vdocument.in/reader035/viewer/2022081503/56814ee4550346895dbc774d/html5/thumbnails/5.jpg)
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
![Page 6: CS4231 Local Area Networks HW2 – PC-Based Bridge](https://reader035.vdocument.in/reader035/viewer/2022081503/56814ee4550346895dbc774d/html5/thumbnails/6.jpg)
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)
![Page 7: CS4231 Local Area Networks HW2 – PC-Based Bridge](https://reader035.vdocument.in/reader035/viewer/2022081503/56814ee4550346895dbc774d/html5/thumbnails/7.jpg)
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;
}
![Page 8: CS4231 Local Area Networks HW2 – PC-Based Bridge](https://reader035.vdocument.in/reader035/viewer/2022081503/56814ee4550346895dbc774d/html5/thumbnails/8.jpg)
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 )
![Page 9: CS4231 Local Area Networks HW2 – PC-Based Bridge](https://reader035.vdocument.in/reader035/viewer/2022081503/56814ee4550346895dbc774d/html5/thumbnails/9.jpg)
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.
![Page 10: CS4231 Local Area Networks HW2 – PC-Based Bridge](https://reader035.vdocument.in/reader035/viewer/2022081503/56814ee4550346895dbc774d/html5/thumbnails/10.jpg)
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);
![Page 11: CS4231 Local Area Networks HW2 – PC-Based Bridge](https://reader035.vdocument.in/reader035/viewer/2022081503/56814ee4550346895dbc774d/html5/thumbnails/11.jpg)
libnet - Initializationnet_b = libnet_init( LIBNET_LINK, "eth0", errbuf );if( net_a == NULL ){
fprintf(stderr, "libnet_init fail:%s ", errbuf ); return;}
![Page 12: CS4231 Local Area Networks HW2 – PC-Based Bridge](https://reader035.vdocument.in/reader035/viewer/2022081503/56814ee4550346895dbc774d/html5/thumbnails/12.jpg)
libnet - Send a Packetc = libnet_write_link( net_b, (u_char*)packet, hdr->caplen );
![Page 13: CS4231 Local Area Networks HW2 – PC-Based Bridge](https://reader035.vdocument.in/reader035/viewer/2022081503/56814ee4550346895dbc774d/html5/thumbnails/13.jpg)
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.
![Page 14: CS4231 Local Area Networks HW2 – PC-Based Bridge](https://reader035.vdocument.in/reader035/viewer/2022081503/56814ee4550346895dbc774d/html5/thumbnails/14.jpg)
pthreadint pthread_join(pthread_t * thread, void
**value_ptr); The pthread_join() function suspends execution of
the calling thread until the target thread terminates
![Page 15: CS4231 Local Area Networks HW2 – PC-Based Bridge](https://reader035.vdocument.in/reader035/viewer/2022081503/56814ee4550346895dbc774d/html5/thumbnails/15.jpg)
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
![Page 16: CS4231 Local Area Networks HW2 – PC-Based Bridge](https://reader035.vdocument.in/reader035/viewer/2022081503/56814ee4550346895dbc774d/html5/thumbnails/16.jpg)
Programming EnvironmentYou have to write your program on Linux platform.You can install VMware to run Linux on it.
![Page 17: CS4231 Local Area Networks HW2 – PC-Based Bridge](https://reader035.vdocument.in/reader035/viewer/2022081503/56814ee4550346895dbc774d/html5/thumbnails/17.jpg)
Environment Construction一台有兩張網卡的電腦 (PC_E)其他電腦用連接至 PC_EPC_E 需安裝 Linux 作業系統以及
libpcap, libnet亦可在 Windows 下使用 VMware 安裝 Linux 代替
PC_E仍須兩張網路卡使 Virtual Machine 的兩張虛擬網路卡分別橋接至兩張實體網路卡
![Page 18: CS4231 Local Area Networks HW2 – PC-Based Bridge](https://reader035.vdocument.in/reader035/viewer/2022081503/56814ee4550346895dbc774d/html5/thumbnails/18.jpg)
Edit VM Setting
![Page 19: CS4231 Local Area Networks HW2 – PC-Based Bridge](https://reader035.vdocument.in/reader035/viewer/2022081503/56814ee4550346895dbc774d/html5/thumbnails/19.jpg)
Add a Host Virtual Adapters
![Page 20: CS4231 Local Area Networks HW2 – PC-Based Bridge](https://reader035.vdocument.in/reader035/viewer/2022081503/56814ee4550346895dbc774d/html5/thumbnails/20.jpg)
Add a Ethernet Card
建立兩個 subnet
![Page 21: CS4231 Local Area Networks HW2 – PC-Based Bridge](https://reader035.vdocument.in/reader035/viewer/2022081503/56814ee4550346895dbc774d/html5/thumbnails/21.jpg)
Add a Ethernet Card
![Page 22: CS4231 Local Area Networks HW2 – PC-Based Bridge](https://reader035.vdocument.in/reader035/viewer/2022081503/56814ee4550346895dbc774d/html5/thumbnails/22.jpg)
Add a Ethernet Card
![Page 23: CS4231 Local Area Networks HW2 – PC-Based Bridge](https://reader035.vdocument.in/reader035/viewer/2022081503/56814ee4550346895dbc774d/html5/thumbnails/23.jpg)
Add a Ethernet Card
兩張網卡分別對應到 eth0, eth1
![Page 24: CS4231 Local Area Networks HW2 – PC-Based Bridge](https://reader035.vdocument.in/reader035/viewer/2022081503/56814ee4550346895dbc774d/html5/thumbnails/24.jpg)
Bridge Guest NIC to Host NIC
兩張網卡分別bridged 到 VMnet2, VMnet3
![Page 25: CS4231 Local Area Networks HW2 – PC-Based Bridge](https://reader035.vdocument.in/reader035/viewer/2022081503/56814ee4550346895dbc774d/html5/thumbnails/25.jpg)
Requirements秀出 bridge 的 filtering database
( 工作站位址 , 隸屬埠 , 登錄時間 )同一個 LAN 互傳 ( 該兩台電腦都已經被 bridge 學習了 ) 時,另一 LAN 不應該收到封包
兩邊的 LAN 可以互通
![Page 26: CS4231 Local Area Networks HW2 – PC-Based Bridge](https://reader035.vdocument.in/reader035/viewer/2022081503/56814ee4550346895dbc774d/html5/thumbnails/26.jpg)
Debugging
![Page 27: CS4231 Local Area Networks HW2 – PC-Based Bridge](https://reader035.vdocument.in/reader035/viewer/2022081503/56814ee4550346895dbc774d/html5/thumbnails/27.jpg)
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%)
![Page 28: CS4231 Local Area Networks HW2 – PC-Based Bridge](https://reader035.vdocument.in/reader035/viewer/2022081503/56814ee4550346895dbc774d/html5/thumbnails/28.jpg)
Hand in your programDeadline: 自行練習
![Page 29: CS4231 Local Area Networks HW2 – PC-Based Bridge](https://reader035.vdocument.in/reader035/viewer/2022081503/56814ee4550346895dbc774d/html5/thumbnails/29.jpg)
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