diff --git a/src/main.cpp b/src/main.cpp index df93062..2a60081 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,13 +7,16 @@ int main(int argc, char **argv) { app.require_subcommand(); CLI::App *search = app.add_subcommand("search", "Search for Profinet devices on the network."); - CLI::Option *interface = search->add_option("-i,--interface", "Interface to use"); + std::string interfaceName; + CLI::Option *interface = search->add_option("-i,--interface", interfaceName, "Interface to use"); int timeout = 1000; search->add_option("-t,--timeout", timeout, "Time to search for devices in milliseconds"); CLI::App *configure = app.add_subcommand("configure", "Configure Profinet devices on the network."); CLI::Option *device = configure->add_option("device", "The current name of the device to configure. Use EMPTY for blank string.") ->required(true); + std::string configureInterfaceName; + configure->add_option("-I,--interface", configureInterfaceName, "Interface to use"); configure->add_option("-t,--timeout", timeout, "Time to search for devices in milliseconds"); std::string newName; @@ -33,11 +36,10 @@ int main(int argc, char **argv) { try { if (*search) { - ProfinetTool profinetTool(timeout); - if (!interface->empty()) profinetTool = ProfinetTool(interface->as(), timeout); + ProfinetTool profinetTool = interface->empty() ? ProfinetTool(timeout) : ProfinetTool(interfaceName, timeout); profinetTool.searchForDevices(); } else if (*configure) { - ProfinetTool profinetTool(timeout); + ProfinetTool profinetTool = configureInterfaceName.empty() ? ProfinetTool(timeout) : ProfinetTool(configureInterfaceName, timeout); profinetTool.configureDevices(deviceName, newName, newIP, newSubnet, newGateway); } @@ -46,6 +48,5 @@ int main(int argc, char **argv) { return EXIT_FAILURE; } - return 0; } diff --git a/src/pcapInterface.c b/src/pcapInterface.c index dcab613..d0c17ad 100644 --- a/src/pcapInterface.c +++ b/src/pcapInterface.c @@ -87,14 +87,24 @@ void profinet_pcap_callback(unsigned char *args, const struct pcap_pkthdr *packe struct ether_header header; memcpy(&header, packet, sizeof(header)); - if (header.ether_type != htons(ETH_P_8021Q)) return; - const int packetLength = packetInfo->len; + int payloadOffset; + + if (header.ether_type == htons(ETH_P_8021Q)) { + /* 802.1Q tagged frame: 4-byte VLAN tag after Ethernet header */ + payloadOffset = (int) sizeof(struct ether_header) + 4; + } else if (header.ether_type == htons(0x8892)) { + /* Untagged Profinet frame */ + payloadOffset = (int) sizeof(struct ether_header); + } else { + return; + } - const int profinetPacketSize = packetLength - (int) sizeof(struct ether_header) - 4; + const int profinetPacketSize = packetLength - payloadOffset; + if (profinetPacketSize <= 0) return; unsigned char profinetPacketRaw[profinetPacketSize]; - memcpy(profinetPacketRaw, packet + sizeof(struct ether_header) + 4, profinetPacketSize); + memcpy(profinetPacketRaw, packet + payloadOffset, profinetPacketSize); struct profinet_dcp_header dcpHeader; memcpy(&dcpHeader, profinetPacketRaw, sizeof(dcpHeader));