add scripts and tuntap.c

This commit is contained in:
Hanzhang ma 2024-11-10 22:50:25 +01:00
parent e0f8f0bfe2
commit 7fb4a4b780
5 changed files with 159 additions and 0 deletions

Binary file not shown.

View File

@ -0,0 +1 @@
![image-20241110210209022](./assets/image-20241110210209022.png)

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

71
Blatt02/scripts/103.sh Normal file
View File

@ -0,0 +1,71 @@
#!/bin/bash
bash /home/rnp/2/101.sh
echo "Now running 103.sh"
assign_vlan(){
local dev=$1
local eth=$2
local id=$3
local ip=$4
ssh $dev "ip link add link $eth name $eth.$id type vlan id $id"
ssh $dev "ip link set dev $eth.$id up"
ssh $dev "ip addr flush dev $eth"
ssh $dev "ip addr add $ip dev $eth.$id"
}
assign_vlan "router4" "eth1" "100" "10.5.1.4/24"
assign_vlan "pc3" "eth1" "100" "10.5.1.3/24"
assign_vlan "pc2" "eth1" "200" "10.5.1.2/24"
assign_vlan "pc1" "eth1" "200" "10.5.1.1/24"
ping_dev(){
local dev=$1
local ip=$2
local eth_n=$3
# ssh "$dev" $cmd
loss=$(ssh $dev "ping -c 5 -W 2 -I eth$eth_n $ip | awk -F', ' '/packet loss/ {print \$3}' | awk '{print int(\$1)}'")
echo $loss
}
check(){
local dev1=$1
local dev2=$2
local ping_loss=$3
local num=$4
echo $ping_loss
if [ $ping_loss -eq $num ]; then
echo -e "from $dev1 to $dev2: \t yes"
else
echo -e "from $dev1 to $dev2: \t no"
fi
}
loss=$(ping_dev "router4" "10.5.1.1" "1.100")
check "router4" "pc1" "$loss" 100
loss=$(ping_dev "router4" "10.5.1.2" "1.100")
check "router4" "pc2" "$loss" 100
loss=$(ping_dev "router4" "10.5.1.3" "1.100")
check "router4" "pc3" "$loss" 0
loss=$(ping_dev "pc1" "10.5.1.2" "1.200")
check "pc1" "pc2" "$loss" 0
loss=$(ping_dev "pc1" "10.5.1.3" "1.200")
check "pc1" "pc3" "$loss" 100
loss=$(ping_dev "pc1" "10.5.1.4" "1.200")
check "pc1" "router4" "$loss" 100
loss=$(ping_dev "pc2" "10.5.1.1" "1.200")
check "pc2" "pc1" "$loss" 0
loss=$(ping_dev "pc2" "10.5.1.3" "1.200")
check "pc2" "pc3" "$loss" 100
loss=$(ping_dev "pc2" "10.5.1.4" "1.200")
check "pc2" "router4" "$loss" 100
loss=$(ping_dev "pc3" "10.5.1.1" "1.100")
check "pc3" "pc1" "$loss" 100
loss=$(ping_dev "pc3" "10.5.1.2" "1.100")
check "pc3" "pc2" "$loss" 100
loss=$(ping_dev "pc3" "10.5.1.4" "1.100")
check "pc3" "router4" "$loss" 0

87
Blatt02/tuntap.c Normal file
View File

@ -0,0 +1,87 @@
#include <assert.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/if_tun.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#define CLEAR(x) memset(&(x), 0x00, sizeof(x))
#define IFNAME "tap0"
#define BUFLEN 1600
static int tun_alloc(char *dev)
{
struct ifreq ifr;
int fd, err;
if( (fd = open("/dev/net/tun", O_RDWR)) < 0 ) {
perror("Cannot open TUN/TAP dev\n"
"Make sure one exists with "
"'$ mknod /dev/net/tun c 10 200'");
exit(1);
}
CLEAR(ifr);
/* Flags: IFF_TUN - TUN device (no Ethernet headers)
* IFF_TAP - TAP device
*
* IFF_NO_PI - Do not provide packet information
*/
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
if( *dev ) {
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
}
if( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ){
perror("ERR: Could not ioctl tun");
close(fd);
return err;
}
strcpy(dev, ifr.ifr_name);
return fd;
}
int main(int argc, char** argv) {
char dev[IFNAMSIZ];
int tun_fd;
if (argc > 1) {
assert(strlen(argv[1]) < IFNAMSIZ);
strcpy(dev, argv[1]);
} else {
strcpy(dev, IFNAME);
}
printf("device name: %s\n", dev);
tun_fd = tun_alloc(dev);
int running = 1;
uint8_t buf[BUFLEN];
while (running) {
int nread;
if ((nread = read(tun_fd, buf, BUFLEN)) < 0) {
perror("ERR: Read from tun_fd");
break;
}
printf("Read %d bytes from device %s\n", nread, dev);
}
close(tun_fd);
return EXIT_SUCCESS;
}