Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,11 +36,10 @@ int main(int argc, char **argv) {

try {
if (*search) {
ProfinetTool profinetTool(timeout);
if (!interface->empty()) profinetTool = ProfinetTool(interface->as<std::string>(), 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);
}
Expand All @@ -46,6 +48,5 @@ int main(int argc, char **argv) {
return EXIT_FAILURE;
}


return 0;
}
18 changes: 14 additions & 4 deletions src/pcapInterface.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down