Initial commit, start writing EtherCat

master
BodgeMaster 2025-01-29 23:40:28 +01:00
commit da6461ad47
4 changed files with 65 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/ethercat

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# EtherCat
a tool to interface tap interfaces with stdin and stdout

3
build.sh Executable file
View File

@ -0,0 +1,3 @@
#!/usr/bin/env bash
rm ethercat
gcc -Wall -Wextra ethercat.c -o ethercat

58
ethercat.c Normal file
View File

@ -0,0 +1,58 @@
#include <errno.h>
#include <fcntl.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#define DEVICE_NODE_TUN /dev/net/tun
#define BUFFER_SIZE 0x1000
#define EXIT_SUCCESS 0
#define EXIT_USAGE 1
#define EXIT_RUNTIME 2
int file_descriptor;
char buffer[BUFFER_SIZE];
struct ifreq ifr;
int main(int argc, char** argv) {
if (argc != 2) {
fprintf(stderr, "Usage: %s TAP\n", argv[0]);
return EXIT_USAGE;
} else if ((strlen(argv[1])==2 && strncmp(argv[1], "-h", 2)==0) || (strlen(argv[1])==6 &&strncmp(argv[1], "--help", 6))) {
fprintf(stdout, "Usage: %s TAP\n", argv[0]);
return EXIT_SUCCESS;
}
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
strncpy(ifr.ifr_name, argv[1], IFNAMSIZ);
file_descriptor = open("/dev/net/tun", O_RDWR);
if (file_descriptor == -1) {
fprintf(stderr, "An error occurred while trying to open DEVICE_NODE_TUN: %i\n", errno);
return EXIT_RUNTIME;
}
if (ioctl(file_descriptor, TUNSETIFF, &ifr) == -1) {
fprintf(stderr, "An error occurred while trying to ioctl: %i\n", errno);
close(file_descriptor);
return EXIT_RUNTIME;
}
//TODO:
// while input not EOF
// and the tap is still a valid device (it might go away)
// read from tap to stdout
// read from stdin to tap
//TODO: signal handler for SIGINT (and SIGHUP? maybe others?)
if (fcntl(fd, F_GETFD) != -1 || errno != EBADF) {
close(file_descriptor);
}
return EXIT_SUCCESS;
}