From 1e05b6480edcf7859dd6b2e4a2ebe389ec47086a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Fri, 6 Apr 2018 18:08:30 +0200 Subject: [PATCH] control SW --- c/.gitignore | 1 + c/README | 11 ++ c/prog.c | 367 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 379 insertions(+) create mode 100644 c/.gitignore create mode 100644 c/README create mode 100644 c/prog.c diff --git a/c/.gitignore b/c/.gitignore new file mode 100644 index 0000000..ea17491 --- /dev/null +++ b/c/.gitignore @@ -0,0 +1 @@ +prog diff --git a/c/README b/c/README new file mode 100644 index 0000000..64220e0 --- /dev/null +++ b/c/README @@ -0,0 +1,11 @@ +./prog + +mode: +0: rainbow +1: red +2: green +3: blue +4: off + +IFNAME: +Interface name like "eth0" or "enp5s0" diff --git a/c/prog.c b/c/prog.c new file mode 100644 index 0000000..60b25ba --- /dev/null +++ b/c/prog.c @@ -0,0 +1,367 @@ + +/* + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MY_DEST_MAC0 0x00 +#define MY_DEST_MAC1 0xDE +#define MY_DEST_MAC2 0xAD +#define MY_DEST_MAC3 0xBE +#define MY_DEST_MAC4 0xEF +#define MY_DEST_MAC5 0x00 + +#define DEFAULT_IF "enp5s0" +#define BUF_SIZ 1024 + +int main(int argc, char *argv[]) +{ + int sockfd; + struct ifreq if_idx; + struct ifreq if_mac; + int tx_len = 0; + int mode; + int i; + char sendbuf[BUF_SIZ]; + struct ether_header *eh = (struct ether_header *) sendbuf; + struct iphdr *iph = (struct iphdr *) (sendbuf + sizeof(struct ether_header)); + struct sockaddr_ll socket_address; + char ifName[IFNAMSIZ]; + + /* Get interface name */ + if (argc > 2) + strcpy(ifName, argv[2]); + else + strcpy(ifName, DEFAULT_IF); + + if (argc > 1) + mode = atoi(argv[1]); + else + mode = 0; + /* Open RAW socket to send on */ + if ((sockfd = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW)) == -1) { + perror("socket"); + } + + /* Get the index of the interface to send on */ + memset(&if_idx, 0, sizeof(struct ifreq)); + strncpy(if_idx.ifr_name, ifName, IFNAMSIZ-1); + if (ioctl(sockfd, SIOCGIFINDEX, &if_idx) < 0) + perror("SIOCGIFINDEX"); + /* Get the MAC address of the interface to send on */ + memset(&if_mac, 0, sizeof(struct ifreq)); + strncpy(if_mac.ifr_name, ifName, IFNAMSIZ-1); + if (ioctl(sockfd, SIOCGIFHWADDR, &if_mac) < 0) + perror("SIOCGIFHWADDR"); + + /* Construct the Ethernet header */ + memset(sendbuf, 0, BUF_SIZ); + /* Ethernet header */ + eh->ether_shost[0] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[0]; + eh->ether_shost[1] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[1]; + eh->ether_shost[2] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[2]; + eh->ether_shost[3] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[3]; + eh->ether_shost[4] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[4]; + eh->ether_shost[5] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[5]; + eh->ether_dhost[0] = MY_DEST_MAC0; + eh->ether_dhost[1] = MY_DEST_MAC1; + eh->ether_dhost[2] = MY_DEST_MAC2; + eh->ether_dhost[3] = MY_DEST_MAC3; + eh->ether_dhost[4] = MY_DEST_MAC4; + eh->ether_dhost[5] = MY_DEST_MAC5; + /* Ethertype field */ + eh->ether_type = htons(ETH_P_IP); + tx_len += sizeof(struct ether_header); + + /* Packet data */ + if (mode == 0) { + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + } else if (mode > 0 && mode < 4) { + for (i = 0; i< 60; i++) { + switch (mode) { + case 1: + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + break; + case 2: + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + sendbuf[tx_len++] = 0x00; + break; + case 3: + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0x00; + sendbuf[tx_len++] = 0xff; + break; + } + } + } else if (mode == 4) { + for (i = 0; i< 3*60; i++) { +sendbuf[tx_len++] = 0x00; + +} + + } + + /* Index of the network device */ + socket_address.sll_ifindex = if_idx.ifr_ifindex; + /* Address length*/ + socket_address.sll_halen = ETH_ALEN; + /* Destination MAC */ + socket_address.sll_addr[0] = MY_DEST_MAC0; + socket_address.sll_addr[1] = MY_DEST_MAC1; + socket_address.sll_addr[2] = MY_DEST_MAC2; + socket_address.sll_addr[3] = MY_DEST_MAC3; + socket_address.sll_addr[4] = MY_DEST_MAC4; + socket_address.sll_addr[5] = MY_DEST_MAC5; + + /* Send packet */ + if (sendto(sockfd, sendbuf, tx_len, 0, (struct sockaddr*)&socket_address, sizeof(struct sockaddr_ll)) < 0) + printf("Send failed\n"); + + return 0; +}