Download presentation
Presentation is loading. Please wait.
Published byRosaline Mosley Modified over 9 years ago
2
Overview LAN 1LAN 2
3
Summary 目的 連接 LAN1 與 LAN2 Frame forwarding Frame filtering Hint Data link layer socket programming Library: libpcap libnet
4
Bridge Layer 2 network device Connect 2 LAN
5
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
6
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)
7
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; }
8
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 )
9
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.
10
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);
11
libnet - Initialization net_b = libnet_init( LIBNET_LINK, "eth0", errbuf ); if( net_a == NULL ) { fprintf(stderr, "libnet_init fail:%s ", errbuf ); return; }
12
libnet - Send a Packet c = libnet_write_link( net_b, (u_char*)packet, hdr->caplen );
13
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.
14
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
15
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
16
Programming Environment You have to write your program on Linux platform. You can install VMware to run Linux on it.
17
Environment Construction 一台有兩張網卡的電腦 (PC_E) 其他電腦用連接至 PC_E PC_E 需安裝 Linux 作業系統以及 libpcap, libnet 亦可在 Windows 下使用 VMware 安裝 Linux 代替 PC_E 仍須兩張網路卡 使 Virtual Machine 的兩張虛擬網路卡分別橋接至兩張實 體網路卡
18
Edit VM Setting
19
Add a Host Virtual Adapters
20
Add a Ethernet Card 建立兩個 subnet
21
Add a Ethernet Card
23
兩張網卡分別對 應到 eth0, eth1
24
Bridge Guest NIC to Host NIC 兩張網卡分別 bridged 到 VMnet2, VMnet3
25
Requirements 秀出 bridge 的 filtering database ( 工作站位址, 隸屬埠, 登錄時間 ) 同一個 LAN 互傳 ( 該兩台電腦都已經被 bridge 學習了 ) 時,另一 LAN 不應該收到封包 兩邊的 LAN 可以互通
26
Debugging
27
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%)
28
Hand in your program Deadline: 自行練習
29
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.