diff --git a/Blatt01/rnpAusarbeitung (1).pdf b/Blatt01/rnpAusarbeitung (1).pdf deleted file mode 100644 index 4abe34d..0000000 Binary files a/Blatt01/rnpAusarbeitung (1).pdf and /dev/null differ diff --git a/Blatt02/Blatt02.md b/Blatt02/Blatt02.md index e69de29..c11c207 100644 --- a/Blatt02/Blatt02.md +++ b/Blatt02/Blatt02.md @@ -0,0 +1 @@ +![image-20241110210209022](./assets/image-20241110210209022.png) \ No newline at end of file diff --git a/Blatt02/assets/image-20241110210209022.png b/Blatt02/assets/image-20241110210209022.png new file mode 100644 index 0000000..1e9a6b4 Binary files /dev/null and b/Blatt02/assets/image-20241110210209022.png differ diff --git a/Blatt02/scripts/103.sh b/Blatt02/scripts/103.sh new file mode 100644 index 0000000..7bac096 --- /dev/null +++ b/Blatt02/scripts/103.sh @@ -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 diff --git a/Blatt02/tuntap.c b/Blatt02/tuntap.c new file mode 100644 index 0000000..f0dcec6 --- /dev/null +++ b/Blatt02/tuntap.c @@ -0,0 +1,87 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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; +}